瀏覽代碼

Make a backup (filename.old) in editor when saving XML format scenes, layouts or materials, delete after successful save. This prevents losing the file if XML save crashes. Closes #530.

Lasse Öörni 11 年之前
父節點
當前提交
4c6bce8174

+ 11 - 0
Bin/Data/Scripts/Editor.as

@@ -322,3 +322,14 @@ void SaveConfig()
 
     config.Save(File(configFileName, FILE_WRITE));
 }
+
+void MakeBackup(const String&in fileName)
+{
+    fileSystem.Rename(fileName, fileName + ".old");
+}
+
+void RemoveBackup(bool success, const String&in fileName)
+{
+    if (success)
+        fileSystem.Delete(fileName + ".old");
+}

+ 5 - 1
Bin/Data/Scripts/Editor/EditorMaterial.as

@@ -411,8 +411,10 @@ void SaveMaterial()
     if (fullName.empty)
         return;
 
+    MakeBackup(fullName);
     File saveFile(fullName, FILE_WRITE);
-    editMaterial.Save(saveFile);
+    bool success = editMaterial.Save(saveFile);
+    RemoveBackup(success, fullName);
 }
 
 void SaveMaterialAs()
@@ -453,10 +455,12 @@ void SaveMaterialAsDone(StringHash eventType, VariantMap& eventData)
     if (GetExtension(fullName).empty && filter != "*.*")
         fullName = fullName + filter.Substring(1);
 
+    MakeBackup(fullName);
     File saveFile(fullName, FILE_WRITE);
     if (editMaterial.Save(saveFile))
     {
         saveFile.Close();
+        RemoveBackup(true, fullName);
 
         // Load the new resource to update the name in the editor
         Material@ newMat = cache.GetResource("Material", GetResourceNameFromFullName(fullName));

+ 5 - 0
Bin/Data/Scripts/Editor/EditorScene.as

@@ -234,9 +234,11 @@ bool SaveScene(const String&in fileName)
     // Unpause when saving so that the scene will work properly when loaded outside the editor
     editorScene.updateEnabled = true;
 
+    MakeBackup(fileName);
     File file(fileName, FILE_WRITE);
     String extension = GetExtension(fileName);
     bool success = (extension != ".xml" ? editorScene.Save(file) : editorScene.SaveXML(file));
+    RemoveBackup(success, fileName);
 
     editorScene.updateEnabled = false;
 
@@ -418,6 +420,7 @@ bool SaveNode(const String&in fileName)
 
     ui.cursor.shape = CS_BUSY;
 
+    MakeBackup(fileName);
     File file(fileName, FILE_WRITE);
     if (!file.open)
     {
@@ -427,6 +430,8 @@ bool SaveNode(const String&in fileName)
 
     String extension = GetExtension(fileName);
     bool success = (extension != ".xml" ? editNode.Save(file) : editNode.SaveXML(file));
+    RemoveBackup(success, fileName);
+
     if (success)
         instantiateFileName = fileName;
     else

+ 6 - 0
Bin/Data/Scripts/Editor/EditorUIElement.as

@@ -246,6 +246,7 @@ bool SaveUILayout(const String&in fileName)
 
     ui.cursor.shape = CS_BUSY;
 
+    MakeBackup(fileName);
     File file(fileName, FILE_WRITE);
     if (!file.open)
     {
@@ -260,6 +261,8 @@ bool SaveUILayout(const String&in fileName)
     XMLFile@ elementData = XMLFile();
     XMLElement rootElem = elementData.CreateRoot("element");
     bool success = element.SaveXML(rootElem);
+    RemoveBackup(success, fileName);
+
     if (success)
     {
         FilterInternalVars(rootElem);
@@ -349,6 +352,7 @@ bool SaveChildUIElement(const String&in fileName)
 
     ui.cursor.shape = CS_BUSY;
 
+    MakeBackup(fileName);
     File file(fileName, FILE_WRITE);
     if (!file.open)
     {
@@ -359,6 +363,8 @@ bool SaveChildUIElement(const String&in fileName)
     XMLFile@ elementData = XMLFile();
     XMLElement rootElem = elementData.CreateRoot("element");
     bool success = editUIElement.SaveXML(rootElem);
+    RemoveBackup(success, fileName);
+    
     if (success)
     {
         FilterInternalVars(rootElem);