Browse Source

Proper removal on LocalFileSystem watch by engine.

clementlandrin 11 months ago
parent
commit
6238c26ad1
3 changed files with 47 additions and 23 deletions
  1. 8 0
      h3d/Engine.hx
  2. 3 0
      hxd/fs/FileEntry.hx
  3. 36 23
      hxd/fs/LocalFileSystem.hx

+ 8 - 0
h3d/Engine.hx

@@ -434,6 +434,14 @@ class Engine {
 		window.removeResizeEvent(onWindowResize);
 		if ( mem != null )
 			mem.dispose();
+		#if multidriver
+		for ( r in resCache ) {
+			var resource = Std.downcast(r, hxd.res.Resource);
+			if ( resource != null ) {
+				resource.entry.unwatch(id);
+			}
+		}
+		#end
 	}
 
 	function get_fps() {

+ 3 - 0
hxd/fs/FileEntry.hx

@@ -50,6 +50,9 @@ class FileEntry {
 	public function load( ?onReady : Void -> Void ) : Void { if( !isAvailable ) throw "load() not implemented"; else if( onReady != null ) onReady(); }
 	public function loadBitmap( onLoaded : LoadedBitmap -> Void ) : Void { throw "loadBitmap() not implemented"; }
 	public function watch( onChanged : Null<Void -> Void> ) { }
+	#if multidriver
+	public function unwatch( id : Int ) { }
+	#end
 	public function exists( name : String ) : Bool return false;
 	public function get( name : String ) : FileEntry return null;
 

+ 36 - 23
hxd/fs/LocalFileSystem.hx

@@ -104,13 +104,12 @@ class LocalEntry extends FileEntry {
 
 	/*
 	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
+	when this resource is modified. The problem is that in case several engine works
+	in parallel, and if the same resource is loaded 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.
+	in one file watcher with multiple callback).
 	*/
-	#if editor var watchOnChangedHistory : Array<Null<Void -> Void>>; #end
+	#if multidriver var watchByEngine : Array<Null<Void -> Void>>; #end
 
 	#if (hl && (hl_ver >= version("1.12.0")) && !usesys)
 	var watchHandle : hl.uv.Fs;
@@ -163,7 +162,7 @@ class LocalEntry extends FileEntry {
 				#if nodejs
 				var cst = js.node.Fs.constants;
 				var path = w.file;
-				#if editor
+				#if (multidriver && !macro) // Hide
 				// Fix searching path in hide/bin folder
 				path = hide.Ide.inst.getPath(path);
 				#end
@@ -187,7 +186,7 @@ class LocalEntry extends FileEntry {
 			if( watchCallback != null ) {
 				WATCH_LIST.remove(this);
 				watchCallback = null;
-				#if editor watchOnChangedHistory = null; #end
+				#if multidriver watchByEngine = null; #end
 				#if (hl && (hl_ver >= version("1.12.0")) && !usesys)
 				watchHandle.close();
 				watchHandle = null;
@@ -215,9 +214,9 @@ class LocalEntry extends FileEntry {
 					w.watchCallback = null;
 					WATCH_LIST.remove(w);
 
-					#if editor
-					if (w.watchOnChangedHistory != null)
-						this.watchOnChangedHistory = w.watchOnChangedHistory.copy();
+					#if multidriver
+					if (w.watchByEngine != null)
+						this.watchByEngine = w.watchByEngine.copy();
 					#end
 				}
 			WATCH_LIST.push(this);
@@ -244,35 +243,34 @@ class LocalEntry extends FileEntry {
 		watchTime = getModifTime();
 		#end
 
-		#if editor
-		if (watchOnChangedHistory == null)
-			watchOnChangedHistory = [ onChanged ];
-		else
-			watchOnChangedHistory.push(onChanged);
+		#if multidriver
+		if (watchByEngine == null)
+			watchByEngine = [];
+		watchByEngine[h3d.Engine.getCurrent().id] = onChanged;
 		#end
 
 		watchCallback = function() {
-			#if editor
+			#if (js && multidriver && !macro)
 			try {
-			#end
 				fs.convert.run(this);
-			#if editor
 			} catch ( e : Dynamic ) {
 				hide.Ide.inst.quickMessage('Failed convert for ${name}, trying again');
 				// Convert failed, let's mark this watch as not performed.
 				watchTime = -1;
 				return;
 			}
+			#else
+			fs.convert.run(this);
 			#end
 
-			#if editor
-			if (watchOnChangedHistory == null)
+			#if multidriver
+			if (watchByEngine == null)
 				return;
 
-			var idx = watchOnChangedHistory.length - 1;
+			var idx = watchByEngine.length - 1;
 			while (idx >= 0) {
-				if (watchOnChangedHistory[idx] != null)
-					watchOnChangedHistory[idx]();
+				if (watchByEngine[idx] != null)
+					watchByEngine[idx]();
 				idx--;
 			}
 			#else
@@ -280,6 +278,21 @@ class LocalEntry extends FileEntry {
 			#end
 		}
 	}
+
+	#if multidriver
+	override function unwatch(id : Int) {
+		if ( watchByEngine == null || watchByEngine.length <= id )
+			return;
+		watchByEngine[id] = null;
+		var i = watchByEngine.length;
+		while ( i > 0 ) {
+			if ( watchByEngine[i-1] != null )
+				break;
+			i--;
+		}
+		watchByEngine.resize(i);
+	}
+	#end
 }
 
 class LocalFileSystem implements FileSystem {