embed plugin loader directly into the launcher
This commit is contained in:
181
PluginLoader/PluginConfig.cs
Normal file
181
PluginLoader/PluginConfig.cs
Normal file
@@ -0,0 +1,181 @@
|
||||
using System.Text;
|
||||
using System.Xml.Serialization;
|
||||
using PluginLoader.Data;
|
||||
|
||||
namespace PluginLoader;
|
||||
|
||||
public class PluginConfig
|
||||
{
|
||||
private const string fileName = "config.xml";
|
||||
|
||||
[XmlIgnore] public readonly Dictionary<string, Profile> ProfileMap = new();
|
||||
|
||||
private string filePath;
|
||||
|
||||
private int networkTimeout = 5000;
|
||||
|
||||
[XmlArray]
|
||||
[XmlArrayItem("Id")]
|
||||
public string[] Plugins
|
||||
{
|
||||
get => EnabledPlugins.ToArray();
|
||||
set => EnabledPlugins = new(value);
|
||||
}
|
||||
|
||||
[XmlIgnore] public HashSet<string> EnabledPlugins { get; private set; } = new();
|
||||
|
||||
[XmlArray]
|
||||
[XmlArrayItem("Plugin")]
|
||||
public LocalFolderPlugin.Config[] LocalFolderPlugins
|
||||
{
|
||||
get => PluginFolders.Values.ToArray();
|
||||
set { PluginFolders = value.ToDictionary(x => x.Folder); }
|
||||
}
|
||||
|
||||
[XmlIgnore] public Dictionary<string, LocalFolderPlugin.Config> PluginFolders { get; private set; } = new();
|
||||
|
||||
[XmlArray]
|
||||
[XmlArrayItem("Profile")]
|
||||
public Profile[] Profiles
|
||||
{
|
||||
get => ProfileMap.Values.ToArray();
|
||||
set
|
||||
{
|
||||
ProfileMap.Clear();
|
||||
foreach (var profile in value)
|
||||
ProfileMap[profile.Key] = profile;
|
||||
}
|
||||
}
|
||||
|
||||
public string ListHash { get; set; }
|
||||
|
||||
// Base URL for the statistics server, change to http://localhost:5000 in config.xml for local development
|
||||
// ReSharper disable once UnassignedGetOnlyAutoProperty
|
||||
public string StatsServerBaseUrl { get; }
|
||||
|
||||
// User consent to use the StatsServer
|
||||
public bool DataHandlingConsent { get; set; }
|
||||
public string DataHandlingConsentDate { get; set; }
|
||||
|
||||
public int NetworkTimeout
|
||||
{
|
||||
get => networkTimeout;
|
||||
set
|
||||
{
|
||||
if (value < 100)
|
||||
networkTimeout = 100;
|
||||
else if (value > 60000)
|
||||
networkTimeout = 60000;
|
||||
else
|
||||
networkTimeout = value;
|
||||
}
|
||||
}
|
||||
|
||||
public int Count => EnabledPlugins.Count;
|
||||
|
||||
public void Init(PluginList plugins)
|
||||
{
|
||||
// Remove plugins from config that no longer exist
|
||||
var toRemove = new List<string>();
|
||||
|
||||
var sb = new StringBuilder("Enabled plugins: ");
|
||||
foreach (var id in EnabledPlugins)
|
||||
if (!plugins.Contains(id))
|
||||
{
|
||||
LogFile.WriteLine($"{id} was in the config but is no longer available");
|
||||
toRemove.Add(id);
|
||||
}
|
||||
else
|
||||
{
|
||||
sb.Append(id).Append(", ");
|
||||
}
|
||||
|
||||
if (EnabledPlugins.Count > 0)
|
||||
sb.Length -= 2;
|
||||
else
|
||||
sb.Append("None");
|
||||
LogFile.WriteLine(sb.ToString());
|
||||
|
||||
|
||||
foreach (var id in toRemove)
|
||||
EnabledPlugins.Remove(id);
|
||||
|
||||
if (toRemove.Count > 0)
|
||||
Save();
|
||||
}
|
||||
|
||||
public void Disable()
|
||||
{
|
||||
EnabledPlugins.Clear();
|
||||
}
|
||||
|
||||
|
||||
public void Save()
|
||||
{
|
||||
try
|
||||
{
|
||||
LogFile.WriteLine("Saving config");
|
||||
var serializer = new XmlSerializer(typeof(PluginConfig));
|
||||
if (File.Exists(filePath))
|
||||
File.Delete(filePath);
|
||||
var fs = File.OpenWrite(filePath);
|
||||
serializer.Serialize(fs, this);
|
||||
fs.Flush();
|
||||
fs.Close();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LogFile.WriteLine("An error occurred while saving plugin config: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
public static PluginConfig Load(string mainDirectory)
|
||||
{
|
||||
var path = Path.Combine(mainDirectory, fileName);
|
||||
if (File.Exists(path))
|
||||
try
|
||||
{
|
||||
var serializer = new XmlSerializer(typeof(PluginConfig));
|
||||
var fs = File.OpenRead(path);
|
||||
var config = (PluginConfig)serializer.Deserialize(fs);
|
||||
fs.Close();
|
||||
config.filePath = path;
|
||||
return config;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LogFile.WriteLine("An error occurred while loading plugin config: " + e);
|
||||
}
|
||||
|
||||
return new()
|
||||
{
|
||||
filePath = path
|
||||
};
|
||||
}
|
||||
|
||||
public IEnumerator<string> GetEnumerator()
|
||||
{
|
||||
return EnabledPlugins.GetEnumerator();
|
||||
}
|
||||
|
||||
public bool IsEnabled(string id)
|
||||
{
|
||||
return EnabledPlugins.Contains(id);
|
||||
}
|
||||
|
||||
public void SetEnabled(string id, bool enabled)
|
||||
{
|
||||
if (EnabledPlugins.Contains(id) == enabled)
|
||||
return;
|
||||
|
||||
if (enabled)
|
||||
{
|
||||
EnabledPlugins.Add(id);
|
||||
Main.Instance.List.SubscribeToItem(id);
|
||||
}
|
||||
else
|
||||
{
|
||||
EnabledPlugins.Remove(id);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user