180 lines
4.7 KiB
C#
180 lines
4.7 KiB
C#
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 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: ");
|
|
var localPlugins = new StringBuilder("Local plugins: ");
|
|
foreach (var id in EnabledPlugins)
|
|
{
|
|
if (!plugins.TryGetPlugin(id, out var plugin))
|
|
{
|
|
LogFile.Log.Debug($"{id} was in the config but is no longer available", false);
|
|
toRemove.Add(id);
|
|
}
|
|
else if (!plugin.IsLocal)
|
|
{
|
|
sb.Append(id).Append(", ");
|
|
}
|
|
else
|
|
{
|
|
localPlugins.Append(id).Append(", ");
|
|
}
|
|
}
|
|
|
|
|
|
if (EnabledPlugins.Count > 0)
|
|
sb.Length -= 2;
|
|
else
|
|
sb.Append("None");
|
|
LogFile.Log.Debug(sb.ToString());
|
|
|
|
if (localPlugins.Length > 15)
|
|
localPlugins.Length -= 2;
|
|
else
|
|
localPlugins.Append("None");
|
|
LogFile.Log.Debug(localPlugins.ToString(), false);
|
|
|
|
foreach (var id in toRemove)
|
|
EnabledPlugins.Remove(id);
|
|
|
|
if (toRemove.Count > 0)
|
|
Save();
|
|
}
|
|
|
|
public void Disable()
|
|
{
|
|
EnabledPlugins.Clear();
|
|
}
|
|
|
|
|
|
public void Save()
|
|
{
|
|
try
|
|
{
|
|
LogFile.Log.Debug("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.Log.Debug("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.Log.Debug("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);
|
|
}
|
|
}
|
|
} |