/
Loader.hx
60 lines (49 loc) · 1.32 KB
/
Loader.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package hxd.res;
class Loader {
/**
Set when initializing hxd.Res, or manually.
Allows code to resolve resources without compiling hxd.Res
*/
public static var currentInstance : Loader;
public var fs(default,null) : hxd.fs.FileSystem;
var cache : Map<String,Dynamic>;
public function new(fs) {
this.fs = fs;
cache = new Map<String,Dynamic>();
}
public function cleanCache() {
cache = new Map();
}
public function dir( path : String ) : Array<Any> {
var r : Array<Any> = [];
var entries = fs.dir(path);
for(e in entries)
r.push(new Any(this, e));
return r;
}
public function exists( path : String ) : Bool {
return fs.exists(path);
}
public function load( path : String ) : Any {
return new Any(this, fs.get(path));
}
public function loadCache<T:hxd.res.Resource>( path : String, c : Class<T> ) : T {
var res : T = cache.get(path);
if( res == null ) {
var entry = fs.get(path);
var old = currentInstance;
currentInstance = this;
res = Type.createInstance(c, [entry]);
currentInstance = old;
cache.set(path, res);
} else {
if( Std.downcast(res,c) == null )
throw path+" has been reintrepreted from "+Type.getClass(res)+" to "+c;
}
return res;
}
public function dispose() {
cleanCache();
fs.dispose();
}
}