/
LocalFileSystem.hx
421 lines (363 loc) · 11.1 KB
/
LocalFileSystem.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package hxd.fs;
#if (sys || nodejs)
@:allow(hxd.fs.LocalFileSystem)
@:allow(hxd.fs.FileConverter)
@:access(hxd.fs.LocalFileSystem)
class LocalEntry extends FileEntry {
var fs : LocalFileSystem;
var relPath : String;
var file : String;
var originalFile : String;
function new(fs, name, relPath, file) {
this.fs = fs;
this.name = name;
this.relPath = relPath;
this.file = file;
}
override function getBytes() : haxe.io.Bytes {
return sys.io.File.getBytes(file);
}
override function readBytes( out : haxe.io.Bytes, outPos : Int, pos : Int, len : Int ) : Int {
var f = sys.io.File.read(file);
f.seek(pos, SeekBegin);
var tot = f.readBytes(out, outPos, len);
f.close();
return tot;
}
var isDirCached : Null<Bool>;
override function get_isDirectory() : Bool {
if( isDirCached != null ) return isDirCached;
return isDirCached = sys.FileSystem.isDirectory(file);
}
override function load( ?onReady : Void -> Void ) : Void {
#if macro
onReady();
#else
if( onReady != null ) haxe.Timer.delay(onReady, 1);
#end
}
override function loadBitmap( onLoaded : hxd.fs.LoadedBitmap -> Void ) : Void {
#if js
#if (multidriver && !macro)
var engine = h3d.Engine.getCurrent(); // hide
#end
var image = new js.html.Image();
image.onload = function(_) {
#if (multidriver && !macro)
if( engine.driver == null ) return;
engine.setCurrent();
#end
onLoaded(new LoadedBitmap(image));
};
image.src = "file://"+file;
#elseif (macro || dataOnly)
throw "Not implemented";
#else
var bmp = new hxd.res.Image(this).toBitmap();
onLoaded(new hxd.fs.LoadedBitmap(bmp));
#end
}
override function get_path() {
return relPath == null ? "<root>" : relPath;
}
override function exists( name : String ) {
return fs.exists(relPath == null ? name : relPath + "/" + name);
}
override function get( name : String ) {
return fs.get(relPath == null ? name : relPath + "/" + name);
}
override function get_size() {
return sys.FileSystem.stat(file).size;
}
override function iterator() {
var arr = new Array<FileEntry>();
for( f in sys.FileSystem.readDirectory(file) ) {
switch( f ) {
case ".svn", ".git" if( sys.FileSystem.isDirectory(file+"/"+f) ):
continue;
case ".tmp" if( this == fs.root ):
continue;
default:
arr.push(fs.open(relPath == null ? f : relPath + "/" + f,false));
}
}
return new hxd.impl.ArrayIterator(arr);
}
var watchCallback : Void -> Void;
/*
When a resource is load, we add a watcher on it and wtih callback to call
when this resource is modified. The problem is that in editor, several engine works
in parallel, and if the same resource is load by different engine, we have
to reload this resource for each engine when the file is modified (resulting
in one file watcher with multiple callback). This problem occures only in editor (because
games contains only one engine) so this feature is editor only.
*/
#if editor var watchOnChangedHistory : Array<Null<Void -> Void>>; #end
#if (hl && (hl_ver >= version("1.12.0")) && !usesys)
var watchHandle : hl.uv.Fs;
var lastChanged : Float = 0;
var onChangedDelay : haxe.Timer;
#else
var watchTime : Float;
#end
static var WATCH_INDEX = 0;
static var WATCH_LIST : Array<LocalEntry> = null;
static var tmpDir : String = null;
inline function getModifTime(){
return try sys.FileSystem.stat(originalFile != null ? originalFile : file).mtime.getTime() catch( e : Dynamic ) 0;
}
#if hl
#if (hl_ver >= version("1.12.0")) @:hlNative("std","file_is_locked") #end static function fileIsLocked( b : hl.Bytes ) { return false; }
#end
#if (!hl || hl_ver < version("1.12.0"))
static function checkFiles() {
var filesToCheck = Math.ceil(WATCH_LIST.length / 60);
if( filesToCheck > LocalFileSystem.FILES_CHECK_MAX )
filesToCheck = LocalFileSystem.FILES_CHECK_MAX;
for( i in 0...filesToCheck )
checkNext();
}
static function checkNext() {
var w = WATCH_LIST[WATCH_INDEX++];
if( w == null ) {
WATCH_INDEX = 0;
return;
}
var t = try w.getModifTime() catch( e : Dynamic ) return;
if( t == w.watchTime ) return;
#if (sys || nodejs)
if( tmpDir == null ) {
tmpDir = Sys.getEnv("TEMP");
if( tmpDir == null ) tmpDir = Sys.getEnv("TMPDIR");
if( tmpDir == null ) tmpDir = Sys.getEnv("TMP");
}
var lockFile = tmpDir+"/"+w.file.split("/").pop()+".lock";
if( sys.FileSystem.exists(lockFile) ) return;
if( !w.isDirectory )
try {
#if nodejs
var cst = js.node.Fs.constants;
var fid = js.node.Fs.openSync(w.file, cast (cst.O_RDONLY | cst.O_EXCL | 0x10000000));
js.node.Fs.closeSync(fid);
#elseif hl
if( fileIsLocked(@:privateAccess Sys.getPath(w.file)) )
return;
#end
}catch( e : Dynamic ) return;
#end
w.watchTime = t;
w.watchCallback();
}
#end
override function watch( onChanged : Null < Void -> Void > ) {
if( onChanged == null ) {
if( watchCallback != null ) {
WATCH_LIST.remove(this);
watchCallback = null;
#if editor watchOnChangedHistory = null; #end
#if (hl && (hl_ver >= version("1.12.0")) && !usesys)
watchHandle.close();
watchHandle = null;
#end
}
return;
}
if( watchCallback == null ) {
if( WATCH_LIST == null ) {
WATCH_LIST = [];
#if (!macro && (!hl || (hl_ver < version("1.12.0"))))
haxe.MainLoop.add(checkFiles);
#end
}
var path = path;
for( w in WATCH_LIST )
if( w.path == path ) {
#if (hl && (hl_ver >= version("1.12.0")) && !usesys)
if(w.watchHandle != null) {
w.watchHandle.close();
w.watchHandle = null;
}
#end
w.watchCallback = null;
WATCH_LIST.remove(w);
#if editor
if (w.watchOnChangedHistory != null)
this.watchOnChangedHistory = w.watchOnChangedHistory.copy();
#end
}
WATCH_LIST.push(this);
}
#if (hl && (hl_ver >= version("1.12.0")) && !usesys)
if(watchHandle != null)
watchHandle.close();
lastChanged = getModifTime();
watchHandle = new hl.uv.Fs(originalFile, function(ev) {
switch(ev) {
case Change|Rename:
if(getModifTime() != lastChanged) {
lastChanged = getModifTime();
if(onChangedDelay != null)
onChangedDelay.stop();
onChangedDelay = haxe.Timer.delay(function() {
fs.convert.run(this);
onChanged();
}, 10);
}
}
});
#else
watchTime = getModifTime();
#end
#if editor
if (watchOnChangedHistory == null)
watchOnChangedHistory = [ onChanged ];
else
watchOnChangedHistory.push(onChanged);
#end
watchCallback = function() {
fs.convert.run(this);
#if editor
if (watchOnChangedHistory == null)
return;
var idx = watchOnChangedHistory.length - 1;
while (idx >= 0) {
if (watchOnChangedHistory[idx] != null)
watchOnChangedHistory[idx]();
idx--;
}
#else
onChanged();
#end
}
}
}
class LocalFileSystem implements FileSystem {
var root : FileEntry;
var fileCache = new Map<String,{r:LocalEntry}>();
public var baseDir(default,null) : String;
public var convert(default,null) : FileConverter;
static var isWindows = Sys.systemName() == "Windows";
public static var FILES_CHECK_MAX = 5;
public function new( dir : String, configuration : String, ?storagePath ) {
baseDir = dir;
if( configuration == null )
configuration = "default";
#if macro
var exePath = null;
#else
var pr = storagePath != null ? storagePath : Sys.programPath();
var exePath = pr == null ? null : pr.split("\\").join("/").split("/");
#end
if( exePath != null ) exePath.pop();
var froot = exePath == null ? baseDir : sys.FileSystem.fullPath(exePath.join("/") + "/" + baseDir);
if( froot == null || !sys.FileSystem.exists(froot) || !sys.FileSystem.isDirectory(froot) ) {
froot = sys.FileSystem.fullPath(baseDir);
if( froot == null || !sys.FileSystem.exists(froot) || !sys.FileSystem.isDirectory(froot) )
throw "Could not find dir " + dir;
}
baseDir = froot.split("\\").join("/");
if( !StringTools.endsWith(baseDir, "/") ) baseDir += "/";
convert = new FileConverter(baseDir, configuration);
root = new LocalEntry(this, "root", null, baseDir);
}
public function getAbsolutePath( f : FileEntry ) : String {
var f = cast(f, LocalEntry);
return f.file;
}
public function getRoot() : FileEntry {
return root;
}
var directoryCache : Map<String,Map<String,Bool>> = new Map();
function checkPath( path : String ) {
// make sure the file is loaded with correct case !
var baseDir = new haxe.io.Path(path).dir;
var c = directoryCache.get(baseDir);
var isNew = false;
if( c == null ) {
isNew = true;
c = new Map();
for( f in try sys.FileSystem.readDirectory(baseDir) catch( e : Dynamic ) [] )
c.set(f, true);
directoryCache.set(baseDir, c);
}
if( !c.exists(path.substr(baseDir.length+1)) ) {
// added since then?
if( !isNew ) {
directoryCache.remove(baseDir);
return checkPath(path);
}
return false;
}
return true;
}
function open( path : String, check = true ) {
var r = fileCache.get(path);
if( r != null )
return r.r;
var e = null;
var f = sys.FileSystem.fullPath(baseDir + path);
if( f == null )
return null;
f = f.split("\\").join("/");
if( !check || ((!isWindows || (isWindows && f == baseDir + path)) && sys.FileSystem.exists(f) && checkPath(f)) ) {
e = new LocalEntry(this, path.split("/").pop(), path, f);
convert.run(e);
if( e.file == null ) e = null;
}
fileCache.set(path, {r:e});
return e;
}
public function clearCache() {
for( path in fileCache.keys() ) {
var r = fileCache.get(path);
if( r.r == null ) fileCache.remove(path);
}
}
public function removePathFromCache(path : String) {
fileCache.remove(path);
}
public function exists( path : String ) {
var f = open(path);
return f != null;
}
public function get( path : String ) {
var f = open(path);
if( f == null )
throw new NotFound(path);
return f;
}
public function dispose() {
fileCache = new Map();
}
public function dir( path : String ) : Array<FileEntry> {
if( !sys.FileSystem.exists(baseDir + path) || !sys.FileSystem.isDirectory(baseDir + path) )
throw new NotFound(baseDir + path);
var files = sys.FileSystem.readDirectory(baseDir + path);
var r : Array<FileEntry> = [];
for(f in files)
r.push(open(path + "/" + f, false));
return r;
}
}
#else
class LocalFileSystem implements FileSystem {
public var baseDir(default,null) : String;
public function new( dir : String ) {
throw "Local file system is not supported for this platform";
}
public function exists(path:String) {
return false;
}
public function get(path:String) : FileEntry {
return null;
}
public function getRoot() : FileEntry {
return null;
}
public function dispose() {
}
public function dir( path : String ) : Array<FileEntry> {
return null;
}
}
#end