mirror of
https://github.com/9ParsonsB/Pulsar.git
synced 2025-04-05 17:39:39 -04:00
feat: better internal handling for plugin settings
This commit is contained in:
parent
f644a56918
commit
78ffcc167b
@ -74,34 +74,23 @@ namespace Observatory.PluginManagement
|
|||||||
|
|
||||||
private void LoadSettings(IObservatoryPlugin plugin)
|
private void LoadSettings(IObservatoryPlugin plugin)
|
||||||
{
|
{
|
||||||
string settingsFile = GetSettingsFile(plugin);
|
string savedSettings = Properties.Core.Default.PluginSettings;
|
||||||
bool createFile = !File.Exists(settingsFile);
|
Dictionary<string, object> pluginSettings;
|
||||||
|
|
||||||
if (!createFile)
|
if (!String.IsNullOrWhiteSpace(savedSettings))
|
||||||
{
|
{
|
||||||
try
|
pluginSettings = JsonSerializer.Deserialize<Dictionary<string, object>>(savedSettings);
|
||||||
{
|
}
|
||||||
string settingsJson = File.ReadAllText(settingsFile);
|
else
|
||||||
if (settingsJson != "null")
|
{
|
||||||
plugin.Settings = JsonSerializer.Deserialize(settingsJson, plugin.Settings.GetType());
|
pluginSettings = new();
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
//Invalid settings file, remove and recreate
|
|
||||||
File.Delete(settingsFile);
|
|
||||||
createFile = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (createFile)
|
if (pluginSettings.ContainsKey(plugin.Name))
|
||||||
{
|
{
|
||||||
string settingsJson = JsonSerializer.Serialize(plugin.Settings);
|
var settingsElement = (JsonElement)pluginSettings[plugin.Name];
|
||||||
string settingsDirectory = new FileInfo(settingsFile).DirectoryName;
|
var settingsObject = JsonSerializer.Deserialize(settingsElement.GetRawText(), plugin.Settings.GetType());
|
||||||
if (!Directory.Exists(settingsDirectory))
|
plugin.Settings = settingsObject;
|
||||||
{
|
|
||||||
Directory.CreateDirectory(settingsDirectory);
|
|
||||||
}
|
|
||||||
File.WriteAllText(settingsFile, settingsJson);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -130,26 +119,41 @@ namespace Observatory.PluginManagement
|
|||||||
|
|
||||||
public void SaveSettings(IObservatoryPlugin plugin, object settings)
|
public void SaveSettings(IObservatoryPlugin plugin, object settings)
|
||||||
{
|
{
|
||||||
string settingsFile = GetSettingsFile(plugin);
|
string savedSettings = Properties.Core.Default.PluginSettings;
|
||||||
|
Dictionary<string, object> pluginSettings;
|
||||||
|
|
||||||
string settingsJson = JsonSerializer.Serialize(settings, new JsonSerializerOptions()
|
if (!String.IsNullOrWhiteSpace(savedSettings))
|
||||||
|
{
|
||||||
|
pluginSettings = JsonSerializer.Deserialize<Dictionary<string, object>>(savedSettings);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
pluginSettings = new();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pluginSettings.ContainsKey(plugin.Name))
|
||||||
|
{
|
||||||
|
pluginSettings[plugin.Name] = settings;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
pluginSettings.Add(plugin.Name, settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
string newSettings = JsonSerializer.Serialize(pluginSettings, new JsonSerializerOptions()
|
||||||
{
|
{
|
||||||
ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.Preserve
|
ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.Preserve
|
||||||
});
|
});
|
||||||
string settingsDirectory = new FileInfo(settingsFile).DirectoryName;
|
|
||||||
if (!Directory.Exists(settingsDirectory))
|
Properties.Core.Default.PluginSettings = newSettings;
|
||||||
{
|
Properties.Core.Default.Save();
|
||||||
Directory.CreateDirectory(settingsDirectory);
|
|
||||||
}
|
|
||||||
File.WriteAllText(settingsFile, settingsJson);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string GetSettingsFile(IObservatoryPlugin plugin)
|
//private static string GetSettingsFile(IObservatoryPlugin plugin)
|
||||||
{
|
//{
|
||||||
var configDirectory = new FileInfo(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath).Directory;
|
// var configDirectory = new FileInfo(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath).Directory;
|
||||||
return configDirectory.FullName + "\\" + plugin.Name + ".json";
|
// return configDirectory.FullName + "\\" + plugin.Name + ".json";
|
||||||
}
|
//}
|
||||||
|
|
||||||
private static List<string> LoadPlugins(out List<(IObservatoryWorker plugin, PluginStatus signed)> observatoryWorkers, out List<(IObservatoryNotifier plugin, PluginStatus signed)> observatoryNotifiers)
|
private static List<string> LoadPlugins(out List<(IObservatoryWorker plugin, PluginStatus signed)> observatoryWorkers, out List<(IObservatoryNotifier plugin, PluginStatus signed)> observatoryNotifiers)
|
||||||
{
|
{
|
||||||
@ -214,6 +218,28 @@ namespace Observatory.PluginManagement
|
|||||||
|
|
||||||
private static string LoadPluginAssembly(string dllPath, List<(IObservatoryWorker plugin, PluginStatus signed)> workers, List<(IObservatoryNotifier plugin, PluginStatus signed)> notifiers)
|
private static string LoadPluginAssembly(string dllPath, List<(IObservatoryWorker plugin, PluginStatus signed)> workers, List<(IObservatoryNotifier plugin, PluginStatus signed)> notifiers)
|
||||||
{
|
{
|
||||||
|
System.Runtime.Loader.AssemblyLoadContext.Default.Resolving += (context, name) => {
|
||||||
|
if (name.Name.EndsWith("resources"))
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Importing Observatory.Framework in the Explorer Lua scripts causes an attempt to reload
|
||||||
|
//the assembly, just hand it back the one we already have.
|
||||||
|
if (name.Name.StartsWith("Observatory.Framework"))
|
||||||
|
{
|
||||||
|
return context.Assemblies.Where(a => a.FullName.Contains("ObservatoryFramework")).First();
|
||||||
|
}
|
||||||
|
|
||||||
|
var foundDlls = Directory.GetFileSystemEntries(new FileInfo($".{Path.DirectorySeparatorChar}plugins{Path.DirectorySeparatorChar}deps").FullName, name.Name + ".dll", SearchOption.TopDirectoryOnly);
|
||||||
|
if (foundDlls.Any())
|
||||||
|
{
|
||||||
|
return context.LoadFromAssemblyPath(foundDlls[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return context.LoadFromAssemblyName(name);
|
||||||
|
};
|
||||||
|
|
||||||
var pluginAssembly = System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromAssemblyPath(new FileInfo(dllPath).FullName);
|
var pluginAssembly = System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromAssemblyPath(new FileInfo(dllPath).FullName);
|
||||||
Type[] types;
|
Type[] types;
|
||||||
string err = string.Empty;
|
string err = string.Empty;
|
||||||
|
12
ObservatoryCore/Properties/Core.Designer.cs
generated
12
ObservatoryCore/Properties/Core.Designer.cs
generated
@ -130,5 +130,17 @@ namespace Observatory.Properties {
|
|||||||
this["CoreVersion"] = value;
|
this["CoreVersion"] = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[global::System.Configuration.UserScopedSettingAttribute()]
|
||||||
|
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
|
||||||
|
[global::System.Configuration.DefaultSettingValueAttribute("")]
|
||||||
|
public string PluginSettings {
|
||||||
|
get {
|
||||||
|
return ((string)(this["PluginSettings"]));
|
||||||
|
}
|
||||||
|
set {
|
||||||
|
this["PluginSettings"] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -29,5 +29,8 @@
|
|||||||
<Setting Name="CoreVersion" Type="System.String" Scope="User">
|
<Setting Name="CoreVersion" Type="System.String" Scope="User">
|
||||||
<Value Profile="(Default)" />
|
<Value Profile="(Default)" />
|
||||||
</Setting>
|
</Setting>
|
||||||
|
<Setting Name="PluginSettings" Type="System.String" Scope="User">
|
||||||
|
<Value Profile="(Default)" />
|
||||||
|
</Setting>
|
||||||
</Settings>
|
</Settings>
|
||||||
</SettingsFile>
|
</SettingsFile>
|
Loading…
x
Reference in New Issue
Block a user