1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-05-25 08:34:05 +02:00

Move plugin config into a separate file

Closes 
This commit is contained in:
chylex 2017-04-28 17:29:08 +02:00
parent 4990afcdbb
commit 7e2e1645e9
6 changed files with 90 additions and 28 deletions

View File

@ -39,7 +39,7 @@ sealed class UserConfig{
public bool EnableUpdateCheck { get; set; } public bool EnableUpdateCheck { get; set; }
public string DismissedUpdate { get; set; } public string DismissedUpdate { get; set; }
public PluginConfig Plugins { get; private set; } [Obsolete] public PluginConfig Plugins { get; set; } // TODO remove eventually
public WindowState PluginsWindow { get; set; } public WindowState PluginsWindow { get; set; }
public string CustomCefArgs { get; set; } public string CustomCefArgs { get; set; }
@ -122,11 +122,7 @@ private UserConfig(string file){
EnableUpdateCheck = true; EnableUpdateCheck = true;
ExpandLinksOnHover = true; ExpandLinksOnHover = true;
EnableTrayHighlight = true; EnableTrayHighlight = true;
Plugins = new PluginConfig();
PluginsWindow = new WindowState(); PluginsWindow = new WindowState();
Plugins.DisableOfficialFromConfig("clear-columns");
Plugins.DisableOfficialFromConfig("reply-account");
} }
private void UpgradeFile(){ private void UpgradeFile(){
@ -148,7 +144,6 @@ private void UpgradeFile(){
if (fileVersion == 2){ if (fileVersion == 2){
BrowserWindow = new WindowState(); BrowserWindow = new WindowState();
Plugins = new PluginConfig();
PluginsWindow = new WindowState(); PluginsWindow = new WindowState();
++fileVersion; ++fileVersion;
} }
@ -160,8 +155,6 @@ private void UpgradeFile(){
} }
if (fileVersion == 4){ if (fileVersion == 4){
Plugins.DisableOfficialFromConfig("clear-columns");
Plugins.DisableOfficialFromConfig("reply-account");
++fileVersion; ++fileVersion;
} }

View File

@ -276,7 +276,6 @@ private void plugins_Reloaded(object sender, PluginErrorEventArgs e){
private void plugins_PluginChangedState(object sender, PluginChangedStateEventArgs e){ private void plugins_PluginChangedState(object sender, PluginChangedStateEventArgs e){
browser.ExecuteScriptAsync("window.TDPF_setPluginState", e.Plugin, e.IsEnabled); browser.ExecuteScriptAsync("window.TDPF_setPluginState", e.Plugin, e.IsEnabled);
Config.Save();
} }
private void updates_UpdateAccepted(object sender, UpdateAcceptedEventArgs e){ private void updates_UpdateAccepted(object sender, UpdateAcceptedEventArgs e){

View File

@ -31,6 +31,8 @@ public bool Export(ExportFileFlags flags){
} }
if (flags.HasFlag(ExportFileFlags.PluginData)){ if (flags.HasFlag(ExportFileFlags.PluginData)){
stream.WriteFile("plugin.config", Program.PluginConfigFilePath);
foreach(Plugin plugin in plugins.Plugins){ foreach(Plugin plugin in plugins.Plugins){
foreach(PathInfo path in EnumerateFilesRelative(plugin.GetPluginFolder(PluginFolder.Data))){ foreach(PathInfo path in EnumerateFilesRelative(plugin.GetPluginFolder(PluginFolder.Data))){
try{ try{
@ -69,6 +71,7 @@ public ExportFileFlags GetImportFlags(){
flags |= ExportFileFlags.Config; flags |= ExportFileFlags.Config;
break; break;
case "plugin.config":
case "plugin.data": case "plugin.data":
flags |= ExportFileFlags.PluginData; flags |= ExportFileFlags.PluginData;
break; break;
@ -103,6 +106,13 @@ public bool Import(ExportFileFlags flags){
break; break;
case "plugin.config":
if (flags.HasFlag(ExportFileFlags.PluginData)){
entry.WriteToFile(Program.PluginConfigFilePath);
}
break;
case "plugin.data": case "plugin.data":
if (flags.HasFlag(ExportFileFlags.PluginData)){ if (flags.HasFlag(ExportFileFlags.PluginData)){
string[] value = entry.KeyValue; string[] value = entry.KeyValue;

View File

@ -1,5 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Text;
using TweetDck.Plugins.Events; using TweetDck.Plugins.Events;
namespace TweetDck.Plugins{ namespace TweetDck.Plugins{
@ -11,7 +13,18 @@ sealed class PluginConfig{
public IEnumerable<string> DisabledPlugins => Disabled; public IEnumerable<string> DisabledPlugins => Disabled;
public bool AnyDisabled => Disabled.Count > 0; public bool AnyDisabled => Disabled.Count > 0;
private readonly HashSet<string> Disabled = new HashSet<string>(); private readonly HashSet<string> Disabled = new HashSet<string>{
"official/clear-columns",
"official/reply-account"
};
public void ImportLegacy(PluginConfig config){
Disabled.Clear();
foreach(string plugin in config.Disabled){
Disabled.Add(plugin);
}
}
public void SetEnabled(Plugin plugin, bool enabled){ public void SetEnabled(Plugin plugin, bool enabled){
if ((enabled && Disabled.Remove(plugin.Identifier)) || (!enabled && Disabled.Add(plugin.Identifier))){ if ((enabled && Disabled.Remove(plugin.Identifier)) || (!enabled && Disabled.Add(plugin.Identifier))){
@ -23,8 +36,40 @@ public bool IsEnabled(Plugin plugin){
return !Disabled.Contains(plugin.Identifier); return !Disabled.Contains(plugin.Identifier);
} }
public void DisableOfficialFromConfig(string pluginName){ public void Load(string file){
Disabled.Add("official/"+pluginName); try{
using(FileStream stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read))
using(StreamReader reader = new StreamReader(stream, Encoding.UTF8)){
string line = reader.ReadLine();
if (line == "#Disabled"){
Disabled.Clear();
while((line = reader.ReadLine()) != null){
Disabled.Add(line);
}
}
}
}catch(FileNotFoundException){
}catch(DirectoryNotFoundException){
}catch(Exception e){
Program.Reporter.HandleException("Plugin Configuration Error", "Could not read the plugin configuration file. If you continue, the list of disabled plugins will be reset to default.", true, e);
}
}
public void Save(string file){
try{
using(FileStream stream = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None))
using(StreamWriter writer = new StreamWriter(stream, Encoding.UTF8)){
writer.WriteLine("#Disabled");
foreach(string disabled in Disabled){
writer.WriteLine(disabled);
}
}
}catch(Exception e){
Program.Reporter.HandleException("Plugin Configuration Error", "Could not save the plugin configuration file.", true, e);
}
} }
} }
} }

View File

@ -19,7 +19,7 @@ sealed class PluginManager{
public string PathCustomPlugins => Path.Combine(rootPath, "user"); public string PathCustomPlugins => Path.Combine(rootPath, "user");
public IEnumerable<Plugin> Plugins => plugins; public IEnumerable<Plugin> Plugins => plugins;
public PluginConfig Config { get; private set; } public PluginConfig Config { get; }
public PluginBridge Bridge { get; } public PluginBridge Bridge { get; }
public event EventHandler<PluginErrorEventArgs> Reloaded; public event EventHandler<PluginErrorEventArgs> Reloaded;
@ -27,36 +27,50 @@ sealed class PluginManager{
public event EventHandler<PluginChangedStateEventArgs> PluginChangedState; public event EventHandler<PluginChangedStateEventArgs> PluginChangedState;
private readonly string rootPath; private readonly string rootPath;
private readonly string configPath;
private readonly HashSet<Plugin> plugins = new HashSet<Plugin>(); private readonly HashSet<Plugin> plugins = new HashSet<Plugin>();
private readonly Dictionary<int, Plugin> tokens = new Dictionary<int, Plugin>(); private readonly Dictionary<int, Plugin> tokens = new Dictionary<int, Plugin>();
private readonly Random rand = new Random(); private readonly Random rand = new Random();
private List<string> loadErrors; private List<string> loadErrors;
public PluginManager(string path, PluginConfig config){ public PluginManager(string rootPath, string configPath){
this.rootPath = path; this.rootPath = rootPath;
this.SetConfig(config); this.configPath = configPath;
this.Config = new PluginConfig();
this.Bridge = new PluginBridge(this); this.Bridge = new PluginBridge(this);
LoadConfig();
Config.InternalPluginChangedState += Config_InternalPluginChangedState;
Program.UserConfigReplaced += Program_UserConfigReplaced; Program.UserConfigReplaced += Program_UserConfigReplaced;
} }
private void LoadConfig(){
#pragma warning disable 612
if (Program.UserConfig.Plugins != null){
Config.ImportLegacy(Program.UserConfig.Plugins);
Config.Save(configPath);
Program.UserConfig.Plugins = null;
Program.UserConfig.Save();
}
#pragma warning restore 612
else{
Config.Load(configPath);
}
}
private void Program_UserConfigReplaced(object sender, EventArgs e){ private void Program_UserConfigReplaced(object sender, EventArgs e){
SetConfig(Program.UserConfig.Plugins); LoadConfig();
Reload(); Reload();
} }
private void Config_InternalPluginChangedState(object sender, PluginChangedStateEventArgs e){ private void Config_InternalPluginChangedState(object sender, PluginChangedStateEventArgs e){
PluginChangedState?.Invoke(this, e); PluginChangedState?.Invoke(this, e);
} Config.Save(configPath);
private void SetConfig(PluginConfig config){
if (this.Config != null){
this.Config.InternalPluginChangedState -= Config_InternalPluginChangedState;
}
this.Config = config;
this.Config.InternalPluginChangedState += Config_InternalPluginChangedState;
} }
public bool IsPluginInstalled(string identifier){ public bool IsPluginInstalled(string identifier){

View File

@ -29,8 +29,9 @@ static class Program{
public static readonly string ProgramPath = AppDomain.CurrentDomain.BaseDirectory; public static readonly string ProgramPath = AppDomain.CurrentDomain.BaseDirectory;
public static readonly string StoragePath = IsPortable ? Path.Combine(ProgramPath, "portable", "storage") : GetDataStoragePath(); public static readonly string StoragePath = IsPortable ? Path.Combine(ProgramPath, "portable", "storage") : GetDataStoragePath();
public static readonly string PluginDataPath = Path.Combine(StoragePath, "TD_Plugins");
public static readonly string ConfigFilePath = Path.Combine(StoragePath, "TD_UserConfig.cfg"); public static readonly string ConfigFilePath = Path.Combine(StoragePath, "TD_UserConfig.cfg");
public static readonly string PluginDataPath = Path.Combine(StoragePath, "TD_Plugins");
public static readonly string PluginConfigFilePath = Path.Combine(StoragePath, "TD_PluginConfig.cfg");
private static readonly string ErrorLogFilePath = Path.Combine(StoragePath, "TD_Log.txt"); private static readonly string ErrorLogFilePath = Path.Combine(StoragePath, "TD_Log.txt");
private static readonly string ConsoleLogFilePath = Path.Combine(StoragePath, "TD_Console.txt"); private static readonly string ConsoleLogFilePath = Path.Combine(StoragePath, "TD_Console.txt");
@ -155,7 +156,7 @@ private static void Main(){
Application.ApplicationExit += (sender, args) => ExitCleanup(); Application.ApplicationExit += (sender, args) => ExitCleanup();
PluginManager plugins = new PluginManager(PluginPath, UserConfig.Plugins); PluginManager plugins = new PluginManager(PluginPath, PluginConfigFilePath);
plugins.Reloaded += plugins_Reloaded; plugins.Reloaded += plugins_Reloaded;
plugins.Executed += plugins_Executed; plugins.Executed += plugins_Executed;
plugins.Reload(); plugins.Reload();