Parcourir la source

auto-save backup files for l3D and fx editors

trethaller il y a 6 ans
Parent
commit
7bd69bbce0
3 fichiers modifiés avec 36 ajouts et 2 suppressions
  1. 4 1
      hide/view/FXEditor.hx
  2. 28 0
      hide/view/FileView.hx
  3. 4 1
      hide/view/l3d/Level3D.hx

+ 4 - 1
hide/view/FXEditor.hx

@@ -172,7 +172,10 @@ class FXEditor extends FileView {
 
 	override function save() {
 		var content = ide.toJSON(data.save());
-		currentSign = haxe.crypto.Md5.encode(content);
+		var newSign = haxe.crypto.Md5.encode(content);
+		if(newSign != currentSign)
+			haxe.Timer.delay(saveBackup.bind(content), 0);
+		currentSign = newSign;
 		sys.io.File.saveContent(getPath(), content);
 		super.save();
 	}

+ 28 - 0
hide/view/FileView.hx

@@ -60,6 +60,34 @@ class FileView extends hide.ui.View<{ path : String }> {
 		lastSaveTag = undo.currentID;
 	}
 
+	function saveBackup(content: String) {
+		var tmpPath = ide.resourceDir + "/.tmp/" + state.path;
+		var baseName = haxe.io.Path.withoutExtension(tmpPath);
+		var tmpDir = haxe.io.Path.directory(tmpPath);
+
+		// Save backup file
+		try {
+			sys.FileSystem.createDirectory(tmpDir);
+			var dateFmt = DateTools.format(Date.now(), "%Y%m%d-%H%M%S");
+			sys.io.File.saveContent(baseName + "-backup" + dateFmt + "." + haxe.io.Path.extension(state.path), content);
+		}
+		catch (e: Dynamic) {
+			trace("Cannot save backup", e);
+		}
+
+		// Delete old files
+		var allTemp = [];
+		for( f in try sys.FileSystem.readDirectory(tmpDir) catch( e : Dynamic ) [] ) {
+			if(~/-backup[0-9]{8}-[0-9]{6}$/.match(haxe.io.Path.withoutExtension(f))) {
+				allTemp.push(f);
+			}
+		}
+		allTemp.sort(Reflect.compare);
+		if(allTemp.length > 10) {
+			sys.FileSystem.deleteFile(tmpDir + "/" + allTemp[0]);
+		}
+	}
+
 	public function saveAs() {
 		ide.chooseFileSave(state.path, function(target) {
 			if( target == null ) return;

+ 4 - 1
hide/view/l3d/Level3D.hx

@@ -472,7 +472,10 @@ class Level3D extends FileView {
 
 	override function save() {
 		var content = ide.toJSON(data.save());
-		currentSign = haxe.crypto.Md5.encode(content);
+		var newSign = haxe.crypto.Md5.encode(content);
+		if(newSign != currentSign)
+			haxe.Timer.delay(saveBackup.bind(content), 0);
+		currentSign = newSign;
 		sys.io.File.saveContent(getPath(), content);
 		super.save();
 	}