Browse Source

potential fix for ignore next erasing cdb data

Nicolas Cannasse 5 years ago
parent
commit
415449ef39
3 changed files with 26 additions and 18 deletions
  1. 4 3
      hide/Ide.hx
  2. 21 14
      hide/tools/FileWatcher.hx
  3. 1 1
      hide/view/CdbTable.hx

+ 4 - 3
hide/Ide.hx

@@ -29,6 +29,7 @@ class Ide {
 	var databaseDiff : String;
 	var pakFile : hxd.fmt.pak.FileSystem;
 	var originDataBase : cdb.Database;
+	var dbWatcher : hide.tools.FileWatcher.FileWatchEvent;
 
 	var config : {
 		global : Config,
@@ -544,7 +545,7 @@ class Ide {
 			}
 		}
 		loadDatabase();
-		fileWatcher.register(databaseFile,function() {
+		dbWatcher = fileWatcher.register(databaseFile,function() {
 			loadDatabase(true);
 			hide.comp.cdb.Editor.refreshAll(true);
 		});
@@ -711,8 +712,8 @@ class Ide {
 	public function saveDatabase() {
 		hide.comp.cdb.DataFiles.save(function() {
 			if( databaseDiff != null ) {
-				fileWatcher.ignoreNextChange(databaseDiff);
 				sys.io.File.saveContent(getPath(databaseDiff), toJSON(new cdb.DiffFile().make(originDataBase,database)));
+				fileWatcher.ignorePrevChange(dbWatcher);
 			} else {
 				if( !sys.FileSystem.exists(getPath(databaseFile)) && fileExists(databaseFile) ) {
 					// was loaded from pak, cancel changes
@@ -720,8 +721,8 @@ class Ide {
 					hide.comp.cdb.Editor.refreshAll();
 					return;
 				}
-				fileWatcher.ignoreNextChange(databaseFile);
 				sys.io.File.saveContent(getPath(databaseFile), database.save());
+				fileWatcher.ignorePrevChange(dbWatcher);
 			}
 		});
 	}

+ 21 - 14
hide/tools/FileWatcher.hx

@@ -1,20 +1,23 @@
 package hide.tools;
 
-private typedef FileEvent = {path:String,fun:Void->Void,checkDel:Bool,element:js.html.Element};
+typedef FileWatchEvent = {path:String,fun:Void->Void,checkDel:Bool,element:js.html.Element,?ignoreCheck:String};
 
 class FileWatcher {
 
 	var ide : hide.Ide;
-	var watches : Map<String,{ events : Array<FileEvent>, w : js.node.fs.FSWatcher, ignoreNext : Int, wasChanged : Bool, changed : Bool, isDir : Bool }> = new Map();
+	var watches : Map<String,{ events : Array<FileWatchEvent>, w : js.node.fs.FSWatcher, wasChanged : Bool, changed : Bool, isDir : Bool }> = new Map();
 	var timer : haxe.Timer;
 
 	public function new() {
 		ide = hide.Ide.inst;
 	}
 
-	public function ignoreNextChange( path : String ) {
-		var w = getWatches(path);
-		w.ignoreNext++;
+	public function ignorePrevChange( f : FileWatchEvent ) {
+		f.ignoreCheck = getSignature(f.path);
+	}
+
+	function getSignature( path : String ) {
+		return try haxe.crypto.Md5.make(sys.io.File.getBytes(ide.getPath(path))).toHex() catch( e : Dynamic ) null;
 	}
 
 	public function dispose() {
@@ -28,13 +31,15 @@ class FileWatcher {
 		watches = new Map();
 	}
 
-	public function register( path : String, updateFun, ?checkDelete : Bool, ?element : Element ) {
+	public function register( path : String, updateFun, ?checkDelete : Bool, ?element : Element ) : FileWatchEvent {
 		var w = getWatches(path);
-		w.events.push({ path : path, fun : updateFun, checkDel : checkDelete, element : element == null ? null : element[0] });
+		var f : FileWatchEvent = { path : path, fun : updateFun, checkDel : checkDelete, element : element == null ? null : element[0] };
+		w.events.push(f);
 		if( element != null && timer == null ) {
 			timer = new haxe.Timer(1000);
 			timer.run = cleanEvents;
 		}
+		return f;
 	}
 
 	public function unregister( path : String, updateFun : Void -> Void ) {
@@ -68,7 +73,7 @@ class FileWatcher {
 				isLive(w.events, e);
 	}
 
-	function isLive( events : Array<FileEvent>, e : FileEvent ) {
+	function isLive( events : Array<FileWatchEvent>, e : FileWatchEvent ) {
 		if( e.element == null ) return true;
 		var elt = e.element;
 		while( elt != null ) {
@@ -88,7 +93,6 @@ class FileWatcher {
 				w : null,
 				changed : false,
 				isDir : try sys.FileSystem.isDirectory(fullPath) catch( e : Dynamic ) false,
-				ignoreNext : 0,
 				wasChanged : false,
 			};
 			w.w = try js.node.Fs.watch(fullPath, function(k:String, file:String) {
@@ -99,13 +103,16 @@ class FileWatcher {
 				haxe.Timer.delay(function() {
 					if( !w.changed ) return;
 					w.changed = false;
-					if( w.ignoreNext > 0 ) {
-						w.ignoreNext--;
-						return;
-					}
+					var sign = null;
 					for( e in w.events.copy() )
-						if( isLive(w.events,e) && (w.wasChanged || e.checkDel) )
+						if( isLive(w.events,e) && (w.wasChanged || e.checkDel) ) {
+							if( e.ignoreCheck != null ) {
+								if( sign == null ) sign = getSignature(path);
+								if( sign == e.ignoreCheck ) continue;
+								e.ignoreCheck = null;
+							}
 							e.fun();
+						}
 					w.wasChanged = false;
 				}, 100);
 			}) catch( e : Dynamic ) {

+ 1 - 1
hide/view/CdbTable.hx

@@ -113,7 +113,7 @@ class CdbTable extends hide.ui.View<{}> {
 			tabs.currentTab = tabContents[idx].parent();
 		}
 
-		watch(@:privateAccess ide.databaseFile, () -> rebuild());
+		watch(@:privateAccess ide.databaseFile, () -> syncTabs());
 	}
 
 	override function getTitle() {