Browse Source

[filebrowser] Use .meta file for thumbnail cache invalidation instead of just mtimes, reverted changes to Config.hx

Clément Espeute 2 months ago
parent
commit
f5660e7d08
2 changed files with 27 additions and 24 deletions
  1. 1 9
      hide/Config.hx
  2. 26 15
      hide/tools/ThumbnailGenerator.hx

+ 1 - 9
hide/Config.hx

@@ -56,7 +56,6 @@ class Config {
 	var ide : Ide;
 	var parent : Config;
 	public var path(default,null) : String;
-	var lastPathMTime : Float = 0;
 	public var source(default, null) : ConfigDef = cast {};
 	public var current : ConfigDef = cast {};
 
@@ -66,10 +65,6 @@ class Config {
 		sync();
 	}
 
-	public function getMTime() : Float {
-		return parent != null ? hxd.Math.max(parent.getMTime(), lastPathMTime) : lastPathMTime;
-	}
-
 	public function isLocal() {
 		if( path == null && parent != null ) return parent.isLocal();
 		return path == null || StringTools.startsWith(path, ide.projectDir);
@@ -78,11 +73,8 @@ class Config {
 	public function load( path : String ) {
 		this.path = path;
 		var fullPath = ide.getPath(path);
-		if( sys.FileSystem.exists(fullPath) ) {
-			var stat = js.node.Fs.statSync(fullPath);
-			lastPathMTime = stat.mtime.getTime();
+		if( sys.FileSystem.exists(fullPath) )
 			source = try ide.parseJSON(sys.io.File.getContent(fullPath)) catch( e : Dynamic ) throw e+" (in "+fullPath+")";
-		}
 		else
 			source = cast {};
 		sync();

+ 26 - 15
hide/tools/ThumbnailGenerator.hx

@@ -84,8 +84,6 @@ class ThumbnailGenerator {
 	var bufferSize = 0;
 	static final maxBufferSize = 16384;
 
-	var renderPropsPath : String;
-
 	function new() {
 		if (Ide.inst.ideConfig.filebrowserDebugShowWindow) {
 			nw.Window.get().show(true);
@@ -146,13 +144,12 @@ class ThumbnailGenerator {
 		switch((message.type:FileManager.ManagerToGenCommand)) {
 			case queue:
 					var thumbPath = getThumbPath(message.path).toString();
-
+					var metaPath = thumbPath + ".meta";
 					var shouldGenerate = true;
-					if (!Ide.inst.ideConfig.filebrowserDebugIgnoreThumbnailCache && sys.FileSystem.exists(thumbPath)) {
-						var thumbStat = sys.FileSystem.stat(thumbPath);
-						var fileStat = sys.FileSystem.stat(message.path);
-						var config = Config.loadForFile(Ide.inst, message.path);
-						if (thumbStat.mtime.getTime() > fileStat.mtime.getTime() && thumbStat.mtime.getTime() > config.getMTime()) {
+					if (!Ide.inst.ideConfig.filebrowserDebugIgnoreThumbnailCache && sys.FileSystem.exists(thumbPath) && sys.FileSystem.exists(metaPath)) {
+						var savedHash = sys.io.File.getContent(metaPath);
+						var hash = getThumbnailHash(message.path);
+						if (hash == savedHash) {
 							shouldGenerate = false;
 							sendSuccess(message.path, thumbPath);
 						}
@@ -195,19 +192,32 @@ class ThumbnailGenerator {
 		return path;
 	}
 
-	function handleModel(toRender: RenderInfo) {
-		renderCanvas.engine.setCurrent();
-
-		renderCanvas.s3d.removeChildren();
-
-		var config = Config.loadForFile(Ide.inst, toRender.path);
-		renderPropsPath = config.getLocal("thumbnail.renderProps");
+	static function getRenderProps(filePath: String, config: Config) : String {
+		var renderPropsPath = config.getLocal("thumbnail.renderProps");
 		if (renderPropsPath == null) {
 			var renderPropsList = hide.comp.ScenePreview.listRenderPropsStatic(config);
 			if (renderPropsList.length > 0) {
 				renderPropsPath =  renderPropsList[0].value;
 			}
 		}
+		return renderPropsPath;
+	}
+
+	static function getThumbnailHash(filePath: String) : String {
+		var config = Config.loadForFile(Ide.inst, filePath);
+		var toHash = "";
+		toHash += getRenderProps(filePath, config);
+		toHash += sys.FileSystem.stat(filePath).mtime.getTime();
+		return haxe.crypto.Md5.encode(toHash);
+	}
+
+	function handleModel(toRender: RenderInfo) {
+		renderCanvas.engine.setCurrent();
+
+		renderCanvas.s3d.removeChildren();
+
+		var config = Config.loadForFile(Ide.inst, toRender.path);
+		var renderPropsPath = getRenderProps(toRender.path, config);
 
 		if (renderPropsPath != null)
 			renderCanvas.setRenderProps(renderPropsPath);
@@ -327,6 +337,7 @@ class ThumbnailGenerator {
 		});
 
 		sys.io.File.saveBytes(path, bytes.getBytes());
+		sys.io.File.saveContent(path + ".meta", getThumbnailHash(basePath));
 		return path;
 	}