|
| 1 | +using log4net; |
| 2 | +using System; |
| 3 | +using System.IO; |
| 4 | +using System.Text.Json; |
| 5 | +using System.Text.Json.Serialization; |
| 6 | + |
| 7 | +namespace NETworkManager.Settings; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Manages local application settings that are stored outside the main settings file. |
| 11 | +/// This is used for settings that control where the main settings file is located. |
| 12 | +/// </summary> |
| 13 | +public static class LocalSettingsManager |
| 14 | +{ |
| 15 | + #region Variables |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Logger for logging. |
| 19 | + /// </summary> |
| 20 | + private static readonly ILog Log = LogManager.GetLogger(typeof(LocalSettingsManager)); |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Settings file name. |
| 24 | + /// </summary> |
| 25 | + private static string SettingsFileName => "Settings.json"; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Settings that are currently loaded. |
| 29 | + /// </summary> |
| 30 | + public static LocalSettingsInfo Current { get; private set; } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// JSON serializer options for consistent serialization/deserialization. |
| 34 | + /// </summary> |
| 35 | + private static readonly JsonSerializerOptions JsonOptions = new() |
| 36 | + { |
| 37 | + WriteIndented = true, |
| 38 | + PropertyNameCaseInsensitive = true, |
| 39 | + DefaultIgnoreCondition = JsonIgnoreCondition.Never, |
| 40 | + Converters = { new JsonStringEnumConverter() } |
| 41 | + }; |
| 42 | + #endregion |
| 43 | + |
| 44 | + #region Methods |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Method to get the path of the settings folder. |
| 48 | + /// </summary> |
| 49 | + /// <returns>Path to the settings folder.</returns> |
| 50 | + private static string GetSettingsFolderLocation() |
| 51 | + { |
| 52 | + return Path.Combine( |
| 53 | + Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), |
| 54 | + AssemblyManager.Current.Name); |
| 55 | + } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Method to get the settings file path |
| 59 | + /// </summary> |
| 60 | + /// <returns>Settings file path.</returns> |
| 61 | + private static string GetSettingsFilePath() |
| 62 | + { |
| 63 | + return Path.Combine( |
| 64 | + GetSettingsFolderLocation(), |
| 65 | + SettingsFileName); |
| 66 | + } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// Initialize new settings (<see cref="SettingsInfo" />) and save them (to a file). |
| 70 | + /// </summary> |
| 71 | + private static void Initialize() |
| 72 | + { |
| 73 | + Log.Info("Initializing new local settings."); |
| 74 | + |
| 75 | + Current = new LocalSettingsInfo(); |
| 76 | + |
| 77 | + Save(); |
| 78 | + } |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// Method to load the settings from a file. |
| 82 | + /// </summary> |
| 83 | + public static void Load() |
| 84 | + { |
| 85 | + var filePath = GetSettingsFilePath(); |
| 86 | + |
| 87 | + if (File.Exists(filePath)) |
| 88 | + { |
| 89 | + try |
| 90 | + { |
| 91 | + Log.Info($"Loading local settings from: {filePath}"); |
| 92 | + |
| 93 | + var jsonString = File.ReadAllText(filePath); |
| 94 | + |
| 95 | + // Treat empty or JSON "null" as "no settings" instead of crashing |
| 96 | + if (string.IsNullOrWhiteSpace(jsonString)) |
| 97 | + { |
| 98 | + Log.Info("Local settings file is empty, initializing new local settings."); |
| 99 | + } |
| 100 | + else |
| 101 | + { |
| 102 | + Current = JsonSerializer.Deserialize<LocalSettingsInfo>(jsonString, JsonOptions) ?? new LocalSettingsInfo(); |
| 103 | + |
| 104 | + Log.Info("Local settings loaded successfully."); |
| 105 | + |
| 106 | + // Reset change tracking |
| 107 | + Current.SettingsChanged = false; |
| 108 | + |
| 109 | + return; |
| 110 | + } |
| 111 | + } |
| 112 | + catch (Exception ex) |
| 113 | + { |
| 114 | + Log.Error($"Failed to load local settings from: {filePath}", ex); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + // Initialize new local settings if file does not exist or loading failed |
| 119 | + Initialize(); |
| 120 | + } |
| 121 | + |
| 122 | + /// <summary> |
| 123 | + /// Method to save the current settings to a file. |
| 124 | + /// </summary> |
| 125 | + public static void Save() |
| 126 | + { |
| 127 | + // Create the directory if it does not exist |
| 128 | + Directory.CreateDirectory(GetSettingsFolderLocation()); |
| 129 | + |
| 130 | + // Serialize to file |
| 131 | + var filePath = GetSettingsFilePath(); |
| 132 | + |
| 133 | + var jsonString = JsonSerializer.Serialize(Current, JsonOptions); |
| 134 | + File.WriteAllText(filePath, jsonString); |
| 135 | + |
| 136 | + Log.Info($"Local settings saved to {filePath}"); |
| 137 | + |
| 138 | + // Reset change tracking |
| 139 | + Current.SettingsChanged = false; |
| 140 | + } |
| 141 | + #endregion |
| 142 | +} |
0 commit comments