Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module resourcemanager;
- import std.file;
- class ResourceManager{
- /**
- Add a resource to the manager
- */
- void AddRes(T)(in string sName, ref T res){
- TypeInfo ti = typeid(T);
- if(!(ti in m_loadedRes && sName in m_loadedRes[ti]))
- m_loadedRes[typeid(T)][sName] = res;
- }
- /**
- Gets the resource with its name
- */
- ref T Get(T)(string sName){
- TypeInfo ti = typeid(T);
- if(ti in m_loadedRes && sName in m_loadedRes[ti]){
- return *(cast(T*)&(m_loadedRes[ti][sName]));
- }
- throw new Exception("Resource '"~sName~"' not found");
- }
- /**
- Loads the resources contained in directory matching filePatern
- The first argument of the resource constructor must be a DirEntry, followed by any arguments provided with ctorArgs
- */
- void LoadFromFiles(T, VT...)(in string directory, in string filePatern, in bool recursive, VT ctorArgs){
- foreach(ref file ; dirEntries(directory, filePatern, recursive?SpanMode.depth:SpanMode.shallow)){
- if(file.isFile){
- AddRes!T(file.name, new T(file, ctorArgs));
- }
- }
- }
- private:
- Object[string][TypeInfo] m_loadedRes;
- }
- version( unittest )
- class Foo{
- this(){}
- this(DirEntry file, int i){s = file.name;}
- string s = "goto bar";
- }
- unittest {
- auto rm = new ResourceManager;
- auto foo = new Foo;
- rm.AddRes!Foo("yolo", foo);
- assert(rm.Get!Foo("yolo") == foo);
- assert(rm.Get!Foo("yolo") is foo);
- rm.LoadFromFiles!Foo(".", "dub.json", true, 5);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement