Init.
This commit is contained in:
10
Source/Resources/Resource.cs
Executable file
10
Source/Resources/Resource.cs
Executable file
@@ -0,0 +1,10 @@
|
||||
namespace DaggerFramework
|
||||
{
|
||||
public abstract class Resource : IDisposable
|
||||
{
|
||||
public string Path { get; set; }
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
7
Source/Resources/ResourceLoader.cs
Executable file
7
Source/Resources/ResourceLoader.cs
Executable file
@@ -0,0 +1,7 @@
|
||||
namespace DaggerFramework
|
||||
{
|
||||
public class ResourceLoader
|
||||
{
|
||||
public virtual Resource Load(string path) { return default; }
|
||||
}
|
||||
}
|
||||
87
Source/Resources/ResourceManager.cs
Executable file
87
Source/Resources/ResourceManager.cs
Executable file
@@ -0,0 +1,87 @@
|
||||
using System.IO;
|
||||
|
||||
namespace DaggerFramework
|
||||
{
|
||||
public static class ResourceManager
|
||||
{
|
||||
public static string ResourceRoot { get; set; } = "Resources/";
|
||||
public static T Load<T>(string relativePath) where T : Resource
|
||||
{
|
||||
var path = GlobalizePath(relativePath);
|
||||
var loader = GetAssociatedResourceLoader<T>();
|
||||
var res = loader.Load(path);
|
||||
res.Path = path;
|
||||
return res as T;
|
||||
}
|
||||
|
||||
public static T Load<T>(string name, string relativePath) where T : Resource
|
||||
{
|
||||
T res = null;
|
||||
if (TryGetResourceFromCache<T>(name, out res)) return res as T;
|
||||
|
||||
res = Load<T>(relativePath);
|
||||
AddResourceToCache(name, res);
|
||||
return res as T;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads a list of resources from a folder. Their names will be derived from the actual file name, excluding the extension.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Resource type for all of the resources.</typeparam>
|
||||
/// <param name="localPath">Path to the folder, relative to the ResourceRoot.</param>
|
||||
/// <returns></returns>
|
||||
public static void LoadFolder<T>(string relativePath) where T : Resource
|
||||
{
|
||||
var files = Directory.GetFiles(GlobalizePath(relativePath));
|
||||
foreach (var file in files)
|
||||
{
|
||||
var resName = Path.GetFileNameWithoutExtension(file);
|
||||
var resRelativePath = Path.GetRelativePath(ResourceRoot, file);
|
||||
|
||||
Load<T>(resName, resRelativePath);
|
||||
}
|
||||
}
|
||||
|
||||
public static T Get<T>(string name) where T : Resource
|
||||
{
|
||||
T res = null;
|
||||
if (TryGetResourceFromCache<T>(name, out res)) return res as T;
|
||||
return res;
|
||||
}
|
||||
|
||||
public static string GlobalizePath(string relativePath)
|
||||
{
|
||||
return Path.Join(ResourceRoot, relativePath);
|
||||
}
|
||||
|
||||
private static void AddResourceToCache(string name, Resource resource)
|
||||
{
|
||||
_resourceCache.Add(name, resource);
|
||||
}
|
||||
|
||||
private static bool TryGetResourceFromCache<T>(string name, out T resource) where T : Resource
|
||||
{
|
||||
resource = null;
|
||||
if (!_resourceCache.ContainsKey(name)) return false;
|
||||
resource = _resourceCache[name] as T;
|
||||
return true;
|
||||
}
|
||||
|
||||
private static ResourceLoader GetAssociatedResourceLoader<T>() where T : Resource
|
||||
{
|
||||
return _resourceLoaderAssociations[typeof(T)];
|
||||
}
|
||||
|
||||
private static KeyValuePair<Type, ResourceLoader> CreateResourceAssociation(Type type, ResourceLoader loader)
|
||||
{
|
||||
return new KeyValuePair<Type, ResourceLoader>(type, loader);
|
||||
}
|
||||
|
||||
private static Dictionary<string, Resource> _resourceCache = new Dictionary<string, Resource>();
|
||||
private static readonly Dictionary<Type, ResourceLoader> _resourceLoaderAssociations = new Dictionary<Type, ResourceLoader>()
|
||||
{
|
||||
{typeof(Texture2d), new Texture2dLoader()},
|
||||
{typeof(Sound), new SoundLoader()}
|
||||
};
|
||||
}
|
||||
}
|
||||
13
Source/Resources/Sound.cs
Normal file
13
Source/Resources/Sound.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
namespace DaggerFramework
|
||||
{
|
||||
public class Sound : Resource
|
||||
{
|
||||
public float PitchScale { get; set; } = 1.0f;
|
||||
public float Volume { get; set; } = 1.0f;
|
||||
public Sound(byte[] data)
|
||||
{
|
||||
Data = data;
|
||||
}
|
||||
public byte[] Data;
|
||||
}
|
||||
}
|
||||
15
Source/Resources/SoundLoader.cs
Normal file
15
Source/Resources/SoundLoader.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace DaggerFramework
|
||||
{
|
||||
public class SoundLoader : ResourceLoader
|
||||
{
|
||||
public override Resource Load(string path)
|
||||
{
|
||||
// TODO
|
||||
// var data = File.ReadAllBytes(path);
|
||||
var sound = new Sound(null);
|
||||
sound.Path = path;
|
||||
|
||||
return sound;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Source/Resources/Texture2d.cs
Executable file
11
Source/Resources/Texture2d.cs
Executable file
@@ -0,0 +1,11 @@
|
||||
namespace DaggerFramework
|
||||
{
|
||||
public class Texture2d : Resource
|
||||
{
|
||||
public Texture2d(byte[] data)
|
||||
{
|
||||
Data = data;
|
||||
}
|
||||
public byte[] Data;
|
||||
}
|
||||
}
|
||||
14
Source/Resources/Texture2dLoader.cs
Executable file
14
Source/Resources/Texture2dLoader.cs
Executable file
@@ -0,0 +1,14 @@
|
||||
namespace DaggerFramework
|
||||
{
|
||||
public class Texture2dLoader : ResourceLoader
|
||||
{
|
||||
public override Resource Load(string path)
|
||||
{
|
||||
var data = File.ReadAllBytes(path);
|
||||
var texture = new Texture2d(data);
|
||||
texture.Path = path;
|
||||
|
||||
return texture;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user