mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
Start of ContentManager
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using xxHash;
|
||||
|
||||
namespace GlitchyEngine.Content
|
||||
{
|
||||
class ContentId
|
||||
{
|
||||
private readonly String _string;
|
||||
private readonly XXH64_hash _hash;
|
||||
|
||||
public String String => _string;
|
||||
public XXH64_hash Hash => _hash;
|
||||
|
||||
[AllowAppend]
|
||||
public this(StringView id)
|
||||
{
|
||||
String str = append String(id);
|
||||
|
||||
_string = str;
|
||||
|
||||
_hash = xxHash.ComputeHash(id);
|
||||
}
|
||||
}
|
||||
|
||||
interface IContentManager
|
||||
{
|
||||
Stream GetFile(String filename);
|
||||
}
|
||||
|
||||
class ContentManager : IContentManager
|
||||
{
|
||||
private String _contentRoot;
|
||||
|
||||
[AllowAppend]
|
||||
public this(String contentRoot)
|
||||
{
|
||||
String cntRoot = append String(contentRoot);
|
||||
_contentRoot = cntRoot;
|
||||
|
||||
Runtime.Assert(Directory.Exists(contentRoot), "Content root directory doesn't exist.");
|
||||
}
|
||||
|
||||
public Stream GetFile(String filename)
|
||||
{
|
||||
String fullpath = scope .(_contentRoot.Length + 1 + filename.Length);
|
||||
Path.InternalCombine(fullpath, _contentRoot, filename);
|
||||
|
||||
FileStream stream = new FileStream();
|
||||
var result = stream.Open(fullpath, .Read, .Read);
|
||||
|
||||
if (result case .Err(let error))
|
||||
{
|
||||
Log.EngineLogger.Error($"Failed to open file \"{fullpath}\". Error: {error}");
|
||||
return null;
|
||||
}
|
||||
|
||||
return stream;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user