1 module evael.system.asset_loader;
2                       
3 debug import std.experimental.logger;
4 
5 import evael.system.asset;
6 
7 import evael.utils.singleton;
8 
9 import evael.lib.memory;
10 
11 /**
12  * AssetLoader
13  */
14 class AssetLoader : NoGCClass
15 {	
16     mixin Singleton!();
17 
18     private IAsset[string] m_assets;
19 
20     /**
21      * AssetLoader constructor.
22      */
23     @nogc
24     private this() nothrow
25     {
26 
27     }
28 
29     /**
30      * AssetLoader destructor.
31      */
32     @nogc
33     public ~this()
34     {
35         foreach (resource; this.m_assets.byValue())
36         {
37             MemoryHelper.dispose(resource);
38         }
39     }
40 
41     /**
42      * Loads an asset.
43      * Params:
44      *		fileName : asset to load
45      *      params : variadic params
46      */
47     public T load(T, Params...)(in string fileName, Params params)
48     {
49         import std.conv : to;
50         import std.path : baseName;
51 
52         immutable shortName = baseName(fileName);
53 
54         if (shortName in this.m_assets)
55         {
56             return cast(T) this.m_assets[shortName];
57         }
58 
59         debug infof("Loading asset: %s", fileName);
60         
61         IAsset asset;
62 
63         static if (params.length)
64         {
65             asset = T.load(fileName, params[0]);
66         }
67         else
68         {
69             asset = T.load(fileName);
70         }
71 
72         this.m_assets[shortName] = asset;
73 
74         return cast(T) asset;
75     }
76 }