Bläddra i källkod

Fixed Preference Settings not converting a Dictionary and added extension preferences

CPKreuz 4 år sedan
förälder
incheckning
5aabe633eb

+ 1 - 1
PixiEditor.ExtensionExample/PixiEditor.ExtensionExample.csproj

@@ -10,7 +10,7 @@
   </PropertyGroup>
 
   <Target Name="PostBuild" AfterTargets="PostBuildEvent">
-    <Exec Command="copy /Y /B $(TargetPath) /B %25LocalAppData%25\PixiEditor\Extensions\$(TargetFileName)" />
+    <Exec Command="echo F|xcopy $(TargetPath) %25LocalAppData%25\PixiEditor\Extensions\$(TargetName)\$(TargetFileName) /y" />
   </Target>
 
   <ItemGroup>

+ 5 - 0
PixiEditor.ExtensionExample/SomeExtension.cs

@@ -20,6 +20,11 @@ namespace PixiEditor.ExtensionExample
 
         public override void Load(ExtensionLoadingInformation information)
         {
+            if (Preferences.GetLocalPreference("Test", true))
+            {
+                Preferences.UpdateLocalPreference("Test", false);
+            }
+
             information
                 .AddDocumentParser<ExampleDocumentParser>();
         }

+ 2 - 0
PixiEditor.SDK/Extension.cs

@@ -17,6 +17,8 @@ namespace PixiEditor.SDK
 
         public abstract Version Version { get; }
 
+        public Preferences Preferences { get; internal set; }
+
         internal List<string> SupportedDocumentFileExtensions { get; set; } = new List<string>();
 
         internal List<string> SupportedImageFileExtensions { get; set; } = new List<string>();

+ 0 - 0
PixiEditor.SDK/ListDictionary.cs → PixiEditor.SDK/Internal/ListDictionary.cs


+ 3 - 1
PixiEditor.SDK/Internal/SDKManager.cs

@@ -27,7 +27,7 @@ namespace PixiEditor.SDK
                 Directory.CreateDirectory(extensionLocation);
             }
 
-            foreach (string path in Directory.EnumerateFiles(extensionLocation, "*.dll"))
+            foreach (string path in Directory.GetFiles(extensionLocation, "*.dll", SearchOption.AllDirectories))
             {
                 try
                 {
@@ -65,6 +65,8 @@ namespace PixiEditor.SDK
             {
                 try
                 {
+                    extension.Preferences = new Preferences(extension);
+
                     ExtensionLoadingInformation information = new(extension);
                     extension.Load(information);
 

+ 1 - 0
PixiEditor.SDK/PixiEditor.SDK.csproj

@@ -7,6 +7,7 @@
   </PropertyGroup>
 
   <ItemGroup>
+    <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
     <PackageReference Include="PixiEditor.Parser" Version="1.1.2.1" />
   </ItemGroup>
 

+ 23 - 17
PixiEditor.SDK/Preferences.cs

@@ -1,4 +1,5 @@
-using System;
+using Newtonsoft.Json.Linq;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
@@ -17,6 +18,7 @@ namespace PixiEditor.SDK
         internal static void Init(Action<Extension, string, object, PreferenceStorageLocation> savePreference, Func<Extension, string, PreferenceStorageLocation, object> loadPreference)
         {
             SavePreferenceCallback = savePreference;
+            LoadPreferenceCallback = loadPreference;
         }
 
         internal Preferences(Extension extension)
@@ -111,34 +113,38 @@ namespace PixiEditor.SDK
             return GetLocalPreference(name, default(T));
         }
 
-        public T? GetPreference<T>(string name, T? fallbackValue)
-        {
-            T? obj = (T?)Convert.ChangeType(LoadPreferenceCallback(Extension, name, PreferenceStorageLocation.Roaming), typeof(T));
-
-            if (obj == null)
-            {
-                return fallbackValue;
-            }
-
-            return obj;
-        }
+        public T? GetPreference<T>(string name, T? fallbackValue) =>
+            GetPreference(name, fallbackValue, PreferenceStorageLocation.Roaming);
 
         public T? GetLocalPreference<T>(string name)
         {
             return GetLocalPreference(name, default(T));
         }
 
-        public T? GetLocalPreference<T>(string name, T? fallbackValue)
+        public T? GetLocalPreference<T>(string name, T? fallbackValue) => 
+            GetPreference(name, fallbackValue, PreferenceStorageLocation.Local);
+
+        private T? GetPreference<T>(string name, T? fallbackValue, PreferenceStorageLocation location)
         {
-            T? obj = (T?)Convert.ChangeType(LoadPreferenceCallback(Extension, name, PreferenceStorageLocation.Local), typeof(T));
+            object value;
 
-            if (obj == null)
+            try
+            {
+                value = LoadPreferenceCallback(Extension, name, location);
+            }
+            catch (KeyNotFoundException)
             {
                 return fallbackValue;
             }
 
-            return obj;
+            if (value is JObject jObj)
+            {
+                return jObj.ToObject<T>();
+            }
+            else
+            {
+                return (T?)Convert.ChangeType(value, typeof(T));
+            }
         }
-
     }
 }

+ 31 - 17
PixiEditor/Models/UserPreferences/PreferencesSettings.cs

@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.Diagnostics;
 using System.IO;
 using Newtonsoft.Json;
+using Newtonsoft.Json.Linq;
 using PixiEditor.ViewModels;
 
 namespace PixiEditor.Models.UserPreferences
@@ -134,19 +135,7 @@ namespace PixiEditor.Models.UserPreferences
                 Init();
             }
 
-            try
-            {
-                return Preferences.ContainsKey(name)
-                        ? (T)Convert.ChangeType(Preferences[name], typeof(T))
-                        : fallbackValue;
-            }
-            catch (InvalidCastException)
-            {
-                Preferences.Remove(name);
-                Save();
-
-                return fallbackValue;
-            }
+            return GetValueFromDictionary(Preferences, name, fallbackValue);
         }
 
         public T? GetLocalPreference<T>(string name)
@@ -161,15 +150,40 @@ namespace PixiEditor.Models.UserPreferences
                 Init();
             }
 
+            return GetValueFromDictionary(LocalPreferences, name, fallbackValue);
+        }
+
+        private T? GetValueFromDictionary<T>(Dictionary<string, object> preferences, string name, T? fallbackValue)
+        {
+            if (!preferences.ContainsKey(name))
+            {
+                return fallbackValue;
+            }
+
+            object value = preferences[name];
+
+            if (value is JObject obj)
+            {
+                try
+                {
+                    return obj.ToObject<T>();
+                }
+                catch
+                {
+                    preferences.Remove(name);
+                    Save();
+
+                    return fallbackValue;
+                }
+            }
+
             try
             {
-                return LocalPreferences.ContainsKey(name)
-                    ? (T)Convert.ChangeType(LocalPreferences[name], typeof(T))
-                    : fallbackValue;
+                return (T)Convert.ChangeType(value, typeof(T));
             }
             catch (InvalidCastException)
             {
-                LocalPreferences.Remove(name);
+                preferences.Remove(name);
                 Save();
 
                 return fallbackValue;

+ 55 - 0
PixiEditor/ViewModels/SubViewModels/Main/ExtensionViewModel.cs

@@ -1,6 +1,8 @@
 using PixiEditor.Models;
+using PixiEditor.Models.UserPreferences;
 using PixiEditor.SDK;
 using System;
+using System.Collections.Generic;
 using System.IO;
 
 namespace PixiEditor.ViewModels.SubViewModels.Main
@@ -14,10 +16,63 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
         {
             SDKManager = new SDKManager();
 
+            Preferences.Init(SavePreference, LoadPreference);
+
+            // Responsible for parsing .pixi, .png, .jpeg
             SDKManager.AddBaseExtension(new BaseExtension());
 
             SDKManager.LoadExtensions(Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "PixiEditor", "Extensions"));
             SDKManager.SetupExtensions();
+
+        }
+
+        private void SavePreference(Extension ext, string name, object obj, PreferenceStorageLocation location)
+        {
+            Dictionary<string, object> extensionSettings;
+
+            if (location == PreferenceStorageLocation.Local)
+            {
+                extensionSettings = IPreferences.Current.GetLocalPreference<Dictionary<string, object>>(ext.Name, new());
+            }
+            else
+            {
+                extensionSettings = IPreferences.Current.GetPreference<Dictionary<string, object>>(ext.Name, new());
+            }
+
+            if (!extensionSettings.ContainsKey(name))
+            {
+                extensionSettings.Add(name, obj);
+            }
+            else
+            {
+                extensionSettings[name] = obj;
+            }
+
+            if (location == PreferenceStorageLocation.Local)
+            {
+                IPreferences.Current.UpdateLocalPreference(ext.Name, extensionSettings);
+            }
+            else
+            {
+                IPreferences.Current.UpdatePreference(ext.Name, extensionSettings);
+            }
+        }
+
+        private object LoadPreference(Extension ext, string name, PreferenceStorageLocation location)
+        {
+            Dictionary<string, object> extensionSettings;
+
+            if (location == PreferenceStorageLocation.Local)
+            {
+                extensionSettings = IPreferences.Current.GetLocalPreference<Dictionary<string, object>>(ext.Name, new());
+            }
+            else
+            {
+                extensionSettings = IPreferences.Current.GetPreference<Dictionary<string, object>>(ext.Name, new());
+            }
+
+            // KeyNotFoundException is handled by the SDK's Preferences
+            return extensionSettings[name];
         }
     }
 }