1 module evael.system.AssetLoader;
2 					  
3 debug import std.experimental.logger;
4 
5 import evael.system.Asset;
6 
7 import evael.graphics.shaders.Shader;
8 
9 import evael.utils.Singleton;
10 
11 /**
12  * AssetLoader
13  */
14 class AssetLoader
15 {	
16 	mixin Singleton!();
17 
18 	private __gshared IAsset[string] m_assets;
19 
20 	/**
21 	 * AssetLoader constructor.
22 	 */
23 	@nogc @safe
24 	private this() pure nothrow
25 	{
26 
27 	}
28 
29 	/**
30 	 * AssetLoader destructor.
31 	 */
32 	public void dispose()
33 	{
34 		foreach (resource; this.m_assets.byValue())
35 		{
36 			resource.dispose();
37 		}
38 	}
39 
40 	/**
41 	 * Loads an asset.
42 	 * Params:
43      *		fileName : asset to load
44 	 *      params : variadic params
45 	 */
46 	public T load(T, Params...)(in string fileName, Params params)
47 	{
48 		import std.conv : to;
49 		import std.path : baseName;
50 
51 		immutable shortName = baseName(fileName);
52 
53 		if (shortName in this.m_assets)
54 		{
55 			return cast(T) this.m_assets[shortName];
56 		}
57 		else
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 	}
77 
78 	/**
79 	 * Returns all assets of type T.
80 	 */
81 	@safe
82 	public T[] getByType(T)() pure nothrow
83 	{
84 		import std.algorithm;
85 		import std.array;
86 
87 		return this.m_assets.byValue().filter!(r => (cast(T) r) !is null ).map!(e => cast(T) e).array();
88 	}
89 }