From 6fbc663197bcba30253ae6cf5bea24771c6d4bda Mon Sep 17 00:00:00 2001 From: Ali Albarrak Date: Mon, 6 Nov 2023 07:34:22 +0300 Subject: [PATCH 1/4] Fix project settings file getting corrupted --- source/VersionHandlerImpl/src/ProjectSettings.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/source/VersionHandlerImpl/src/ProjectSettings.cs b/source/VersionHandlerImpl/src/ProjectSettings.cs index a2c0001a..766a0a19 100644 --- a/source/VersionHandlerImpl/src/ProjectSettings.cs +++ b/source/VersionHandlerImpl/src/ProjectSettings.cs @@ -817,9 +817,10 @@ private static void Save() { PROJECT_SETTINGS_FILE), LogLevel.Error); return; } + string tmpFile = Path.GetTempFileName(); try { using (var writer = - XmlWriter.Create(PROJECT_SETTINGS_FILE, + XmlWriter.Create(tmpFile, new XmlWriterSettings { Encoding = new UTF8Encoding(false), Indent = true, @@ -839,6 +840,8 @@ private static void Save() { } writer.WriteEndElement(); } + + File.Copy(tmpFile, PROJECT_SETTINGS_FILE, true); } catch (Exception exception) { if (exception is IOException || exception is UnauthorizedAccessException) { logger.Log(String.Format("Unable to write to '{0}' ({1}, " + @@ -848,6 +851,10 @@ private static void Save() { } throw exception; } + finally + { + File.Delete(tmpFile); + } } } } From 214444485495962a80a0703c68a9078201b7d8be Mon Sep 17 00:00:00 2001 From: Ali Albarrak Date: Wed, 13 Dec 2023 10:27:21 +0300 Subject: [PATCH 2/4] Only overwrite project settings file if settings where modified --- .../VersionHandlerImpl/src/ProjectSettings.cs | 45 +++++++++++++++++-- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/source/VersionHandlerImpl/src/ProjectSettings.cs b/source/VersionHandlerImpl/src/ProjectSettings.cs index 766a0a19..733c81a2 100644 --- a/source/VersionHandlerImpl/src/ProjectSettings.cs +++ b/source/VersionHandlerImpl/src/ProjectSettings.cs @@ -51,6 +51,12 @@ public enum SettingsLocation { /// This is a UnityEditor.EditorPrefs compatible interface. /// public interface ISettings { + + /// + /// Determine whether setting are out of sync and and to be persisted or not + /// + bool IsModified { get; set; } + /// /// Set an int property. /// @@ -131,6 +137,13 @@ public interface ISettings { /// Default implementation of system wide settings. /// internal class EditorSettings : ISettings { + + /// + /// Determine whether setting are out of sync and and to be persisted or not + /// Always true as editor settings are always persisted by Unity + /// + public bool IsModified { get { return false; } set { } } + /// /// Set a int property. /// @@ -224,6 +237,12 @@ public IEnumerable Keys { /// In-memory settings storage. /// internal class InMemorySettings : ISettings { + + /// + /// Determine whether setting are out of sync and and to be persisted or not + /// + public bool IsModified { get; set; } + /// /// In-memory storage for settings. /// @@ -234,8 +253,14 @@ internal class InMemorySettings : ISettings { /// /// Name of the value. /// Value to set. - private void Set(string name, T value) { - settings[name] = value.ToString(); + private void Set(string name, T value) + { + string stringValue = value.ToString(); + + if (!settings.ContainsKey(name) || settings[name] != stringValue) + IsModified = true; + + settings[name] = stringValue; } /// @@ -333,6 +358,7 @@ public bool HasKey(string name) { /// Name of the value to delete. public void DeleteKey(string name) { settings.Remove(name); + IsModified = true; } /// @@ -347,6 +373,16 @@ public void DeleteKey(string name) { /// either application or project level settings based upon the UseProjectSettings flag. /// public class ProjectSettings : ISettings { + + /// + /// Determine whether setting are out of sync and and to be persisted or not + /// + public bool IsModified + { + get { return systemSettings.IsModified || projectSettings.IsModified; } + set { systemSettings.IsModified = projectSettings.IsModified = value; } + } + /// /// Whether to load settings from and save settings to disk. /// Exposed for testing. @@ -798,6 +834,9 @@ private static bool Load() { })) { return false; } + + // Project settings were just loaded so there shouldn't be any stale values + projectSettings.IsModified = false; return true; } } @@ -807,7 +846,7 @@ private static bool Load() { /// private static void Save() { lock (classLock) { - if (projectSettings == null || !persistenceEnabled) { + if (projectSettings == null || !persistenceEnabled || !projectSettings.IsModified) { return; } Directory.CreateDirectory(Path.GetDirectoryName(PROJECT_SETTINGS_FILE)); From 4268cbae1f1c1d07a4c7cc4fdf4acde434fa47aa Mon Sep 17 00:00:00 2001 From: Ali Albarrak Date: Sun, 17 Dec 2023 07:54:41 +0300 Subject: [PATCH 3/4] Fix typos --- source/VersionHandlerImpl/src/ProjectSettings.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/source/VersionHandlerImpl/src/ProjectSettings.cs b/source/VersionHandlerImpl/src/ProjectSettings.cs index 733c81a2..06af78a3 100644 --- a/source/VersionHandlerImpl/src/ProjectSettings.cs +++ b/source/VersionHandlerImpl/src/ProjectSettings.cs @@ -53,7 +53,7 @@ public enum SettingsLocation { public interface ISettings { /// - /// Determine whether setting are out of sync and and to be persisted or not + /// Determine whether setting are out of sync and needs to be persisted or not /// bool IsModified { get; set; } @@ -139,8 +139,8 @@ public interface ISettings { internal class EditorSettings : ISettings { /// - /// Determine whether setting are out of sync and and to be persisted or not - /// Always true as editor settings are always persisted by Unity + /// Determine whether setting are out of sync and needs to be persisted or not + /// Always false as editor settings are always persisted by Unity /// public bool IsModified { get { return false; } set { } } @@ -239,7 +239,7 @@ public IEnumerable Keys { internal class InMemorySettings : ISettings { /// - /// Determine whether setting are out of sync and and to be persisted or not + /// Determine whether setting are out of sync and needs to be persisted or not /// public bool IsModified { get; set; } @@ -375,7 +375,7 @@ public void DeleteKey(string name) { public class ProjectSettings : ISettings { /// - /// Determine whether setting are out of sync and and to be persisted or not + /// Determine whether setting are out of sync and needs to be persisted or not /// public bool IsModified { From dfd1a145adcf7c4d223bcbd6cf854cab5427838d Mon Sep 17 00:00:00 2001 From: Ali Albarrak Date: Sun, 3 Mar 2024 09:10:24 +0300 Subject: [PATCH 4/4] Fix coding style --- source/VersionHandlerImpl/src/ProjectSettings.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/VersionHandlerImpl/src/ProjectSettings.cs b/source/VersionHandlerImpl/src/ProjectSettings.cs index 06af78a3..6edb9f31 100644 --- a/source/VersionHandlerImpl/src/ProjectSettings.cs +++ b/source/VersionHandlerImpl/src/ProjectSettings.cs @@ -257,8 +257,9 @@ private void Set(string name, T value) { string stringValue = value.ToString(); - if (!settings.ContainsKey(name) || settings[name] != stringValue) + if (!settings.ContainsKey(name) || settings[name] != stringValue) { IsModified = true; + } settings[name] = stringValue; }