mirror of
https://github.com/chylex/TweetDuck.git
synced 2025-05-04 17:34:07 +02:00
parent
4990afcdbb
commit
7e2e1645e9
@ -39,7 +39,7 @@ sealed class UserConfig{
|
||||
public bool EnableUpdateCheck { 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 string CustomCefArgs { get; set; }
|
||||
@ -122,11 +122,7 @@ private UserConfig(string file){
|
||||
EnableUpdateCheck = true;
|
||||
ExpandLinksOnHover = true;
|
||||
EnableTrayHighlight = true;
|
||||
Plugins = new PluginConfig();
|
||||
PluginsWindow = new WindowState();
|
||||
|
||||
Plugins.DisableOfficialFromConfig("clear-columns");
|
||||
Plugins.DisableOfficialFromConfig("reply-account");
|
||||
}
|
||||
|
||||
private void UpgradeFile(){
|
||||
@ -148,7 +144,6 @@ private void UpgradeFile(){
|
||||
|
||||
if (fileVersion == 2){
|
||||
BrowserWindow = new WindowState();
|
||||
Plugins = new PluginConfig();
|
||||
PluginsWindow = new WindowState();
|
||||
++fileVersion;
|
||||
}
|
||||
@ -160,8 +155,6 @@ private void UpgradeFile(){
|
||||
}
|
||||
|
||||
if (fileVersion == 4){
|
||||
Plugins.DisableOfficialFromConfig("clear-columns");
|
||||
Plugins.DisableOfficialFromConfig("reply-account");
|
||||
++fileVersion;
|
||||
}
|
||||
|
||||
|
@ -276,7 +276,6 @@ private void plugins_Reloaded(object sender, PluginErrorEventArgs e){
|
||||
|
||||
private void plugins_PluginChangedState(object sender, PluginChangedStateEventArgs e){
|
||||
browser.ExecuteScriptAsync("window.TDPF_setPluginState", e.Plugin, e.IsEnabled);
|
||||
Config.Save();
|
||||
}
|
||||
|
||||
private void updates_UpdateAccepted(object sender, UpdateAcceptedEventArgs e){
|
||||
|
@ -31,6 +31,8 @@ public bool Export(ExportFileFlags flags){
|
||||
}
|
||||
|
||||
if (flags.HasFlag(ExportFileFlags.PluginData)){
|
||||
stream.WriteFile("plugin.config", Program.PluginConfigFilePath);
|
||||
|
||||
foreach(Plugin plugin in plugins.Plugins){
|
||||
foreach(PathInfo path in EnumerateFilesRelative(plugin.GetPluginFolder(PluginFolder.Data))){
|
||||
try{
|
||||
@ -69,6 +71,7 @@ public ExportFileFlags GetImportFlags(){
|
||||
flags |= ExportFileFlags.Config;
|
||||
break;
|
||||
|
||||
case "plugin.config":
|
||||
case "plugin.data":
|
||||
flags |= ExportFileFlags.PluginData;
|
||||
break;
|
||||
@ -103,6 +106,13 @@ public bool Import(ExportFileFlags flags){
|
||||
|
||||
break;
|
||||
|
||||
case "plugin.config":
|
||||
if (flags.HasFlag(ExportFileFlags.PluginData)){
|
||||
entry.WriteToFile(Program.PluginConfigFilePath);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case "plugin.data":
|
||||
if (flags.HasFlag(ExportFileFlags.PluginData)){
|
||||
string[] value = entry.KeyValue;
|
||||
|
@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using TweetDck.Plugins.Events;
|
||||
|
||||
namespace TweetDck.Plugins{
|
||||
@ -11,7 +13,18 @@ sealed class PluginConfig{
|
||||
public IEnumerable<string> DisabledPlugins => Disabled;
|
||||
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){
|
||||
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);
|
||||
}
|
||||
|
||||
public void DisableOfficialFromConfig(string pluginName){
|
||||
Disabled.Add("official/"+pluginName);
|
||||
public void Load(string file){
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ sealed class PluginManager{
|
||||
public string PathCustomPlugins => Path.Combine(rootPath, "user");
|
||||
|
||||
public IEnumerable<Plugin> Plugins => plugins;
|
||||
public PluginConfig Config { get; private set; }
|
||||
public PluginConfig Config { get; }
|
||||
public PluginBridge Bridge { get; }
|
||||
|
||||
public event EventHandler<PluginErrorEventArgs> Reloaded;
|
||||
@ -27,36 +27,50 @@ sealed class PluginManager{
|
||||
public event EventHandler<PluginChangedStateEventArgs> PluginChangedState;
|
||||
|
||||
private readonly string rootPath;
|
||||
private readonly string configPath;
|
||||
|
||||
private readonly HashSet<Plugin> plugins = new HashSet<Plugin>();
|
||||
private readonly Dictionary<int, Plugin> tokens = new Dictionary<int, Plugin>();
|
||||
private readonly Random rand = new Random();
|
||||
|
||||
private List<string> loadErrors;
|
||||
|
||||
public PluginManager(string path, PluginConfig config){
|
||||
this.rootPath = path;
|
||||
this.SetConfig(config);
|
||||
public PluginManager(string rootPath, string configPath){
|
||||
this.rootPath = rootPath;
|
||||
this.configPath = configPath;
|
||||
|
||||
this.Config = new PluginConfig();
|
||||
this.Bridge = new PluginBridge(this);
|
||||
|
||||
LoadConfig();
|
||||
|
||||
Config.InternalPluginChangedState += Config_InternalPluginChangedState;
|
||||
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){
|
||||
SetConfig(Program.UserConfig.Plugins);
|
||||
LoadConfig();
|
||||
Reload();
|
||||
}
|
||||
|
||||
private void Config_InternalPluginChangedState(object sender, PluginChangedStateEventArgs e){
|
||||
PluginChangedState?.Invoke(this, e);
|
||||
}
|
||||
|
||||
private void SetConfig(PluginConfig config){
|
||||
if (this.Config != null){
|
||||
this.Config.InternalPluginChangedState -= Config_InternalPluginChangedState;
|
||||
}
|
||||
|
||||
this.Config = config;
|
||||
this.Config.InternalPluginChangedState += Config_InternalPluginChangedState;
|
||||
Config.Save(configPath);
|
||||
}
|
||||
|
||||
public bool IsPluginInstalled(string identifier){
|
||||
|
@ -29,8 +29,9 @@ static class Program{
|
||||
public static readonly string ProgramPath = AppDomain.CurrentDomain.BaseDirectory;
|
||||
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 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 ConsoleLogFilePath = Path.Combine(StoragePath, "TD_Console.txt");
|
||||
|
||||
@ -155,7 +156,7 @@ private static void Main(){
|
||||
|
||||
Application.ApplicationExit += (sender, args) => ExitCleanup();
|
||||
|
||||
PluginManager plugins = new PluginManager(PluginPath, UserConfig.Plugins);
|
||||
PluginManager plugins = new PluginManager(PluginPath, PluginConfigFilePath);
|
||||
plugins.Reloaded += plugins_Reloaded;
|
||||
plugins.Executed += plugins_Executed;
|
||||
plugins.Reload();
|
||||
|
Loading…
Reference in New Issue
Block a user