|
@@ -1,27 +1,67 @@
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Configuration;
|
|
|
-using System.Linq;
|
|
|
-using System.Text;
|
|
|
-using System.Threading.Tasks;
|
|
|
+using System.IO;
|
|
|
+using System.Reflection.Metadata;
|
|
|
+using Newtonsoft.Json;
|
|
|
|
|
|
namespace PixiEditor.Models.UserPreferences
|
|
|
{
|
|
|
public static class PreferencesSettings
|
|
|
{
|
|
|
+ public static bool IsLoaded { get; private set; } = false;
|
|
|
+
|
|
|
+ public static string PathToUserPreferences { get; } = Path.Join(
|
|
|
+ Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
|
|
+ "PixiEditor",
|
|
|
+ "user_preferences.json");
|
|
|
+
|
|
|
+ public static Dictionary<string, object> Preferences { get; set; } = new Dictionary<string, object>();
|
|
|
+
|
|
|
+ public static void Init()
|
|
|
+ {
|
|
|
+ if (IsLoaded == false)
|
|
|
+ {
|
|
|
+ string dir = Path.GetDirectoryName(PathToUserPreferences);
|
|
|
+ if (!Directory.Exists(dir))
|
|
|
+ {
|
|
|
+ Directory.CreateDirectory(dir);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!File.Exists(PathToUserPreferences))
|
|
|
+ {
|
|
|
+ File.WriteAllText(PathToUserPreferences, "{\n}");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ string json = File.ReadAllText(PathToUserPreferences);
|
|
|
+ Preferences = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
|
|
|
+ }
|
|
|
+
|
|
|
+ IsLoaded = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public static void UpdatePreference(string name, object value)
|
|
|
{
|
|
|
- Properties.Settings.Default.Reload();
|
|
|
- if (Properties.Settings.Default.Properties[name] != null)
|
|
|
+ if (IsLoaded == false)
|
|
|
{
|
|
|
- Properties.Settings.Default.Properties[name].DefaultValue = value;
|
|
|
+ Init();
|
|
|
}
|
|
|
- else
|
|
|
+
|
|
|
+ Preferences[name] = value;
|
|
|
+
|
|
|
+ Save();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void Save()
|
|
|
+ {
|
|
|
+ if (IsLoaded == false)
|
|
|
{
|
|
|
- Properties.Settings.Default.Properties.Add(new SettingsProperty(name) { DefaultValue = value });
|
|
|
+ Init();
|
|
|
}
|
|
|
|
|
|
- Properties.Settings.Default.Save();
|
|
|
+ File.WriteAllText(PathToUserPreferences, JsonConvert.SerializeObject(Preferences));
|
|
|
}
|
|
|
|
|
|
#nullable enable
|
|
@@ -33,8 +73,13 @@ namespace PixiEditor.Models.UserPreferences
|
|
|
|
|
|
public static T? GetPreference<T>(string name, T? fallbackValue)
|
|
|
{
|
|
|
- return Properties.Settings.Default.Properties[name] != null
|
|
|
- ? (T)Convert.ChangeType(Properties.Settings.Default.Properties[name].DefaultValue, typeof(T))
|
|
|
+ if (IsLoaded == false)
|
|
|
+ {
|
|
|
+ Init();
|
|
|
+ }
|
|
|
+
|
|
|
+ return Preferences.ContainsKey(name)
|
|
|
+ ? (T)Preferences[name]
|
|
|
: fallbackValue;
|
|
|
}
|
|
|
}
|