From beace9645c7194135d742773746821d2a0e60d54 Mon Sep 17 00:00:00 2001
From: Manuel Mayer <45011179+SuchByte@users.noreply.github.com>
Date: Sun, 8 Jan 2023 00:39:51 +0100
Subject: [PATCH] Develop (#337)
* Minor refactoring (#333)
* refactor duplicate code
* refactor duplicate code
* Update Magick dependency (#332)
Co-authored-by: Will <54237626+PhoenixWyllow@users.noreply.github.com>
---
MacroDeck/MacroDeck.csproj | 3 +-
MacroDeck/Plugins/PluginConfiguration.cs | 66 ++++++-------
MacroDeck/Plugins/PluginCredentials.cs | 120 +++++++++++------------
3 files changed, 90 insertions(+), 99 deletions(-)
diff --git a/MacroDeck/MacroDeck.csproj b/MacroDeck/MacroDeck.csproj
index b8c1448f..457891a3 100644
--- a/MacroDeck/MacroDeck.csproj
+++ b/MacroDeck/MacroDeck.csproj
@@ -108,8 +108,7 @@
-
-
+
diff --git a/MacroDeck/Plugins/PluginConfiguration.cs b/MacroDeck/Plugins/PluginConfiguration.cs
index a446e98d..57bed17d 100644
--- a/MacroDeck/Plugins/PluginConfiguration.cs
+++ b/MacroDeck/Plugins/PluginConfiguration.cs
@@ -6,23 +6,21 @@ namespace SuchByte.MacroDeck.Plugins;
public class PluginConfiguration
{
+ private static string FileName(MacroDeckPlugin plugin)
+ {
+ return $"{plugin.Author.ToLower()}_{plugin.Name.ToLower()}.json";
+ }
+
+ private static string FilePath(MacroDeckPlugin plugin)
+ {
+ return Path.Combine(MacroDeck.ApplicationPaths.PluginConfigPath, FileName(plugin));
+ }
+
public static void SetValue(MacroDeckPlugin plugin, string key, string value)
{
try
{
- Dictionary pluginConfig;
-
- if (!File.Exists(Path.Combine(MacroDeck.ApplicationPaths.PluginConfigPath, plugin.Author.ToLower() + "_" + plugin.Name.ToLower() + ".json")))
- {
- pluginConfig = new Dictionary();
- }
- else
- {
- pluginConfig = JsonConvert.DeserializeObject>(File.ReadAllText(Path.Combine(MacroDeck.ApplicationPaths.PluginConfigPath, plugin.Author.ToLower() + "_" + plugin.Name.ToLower() + ".json")), new JsonSerializerSettings
- {
- TypeNameHandling = TypeNameHandling.Auto,
- });
- }
+ var pluginConfig = GetConfig(plugin);
pluginConfig[key] = value;
@@ -32,14 +30,29 @@ public static void SetValue(MacroDeckPlugin plugin, string key, string value)
NullValueHandling = NullValueHandling.Ignore,
};
- using (var sw = new StreamWriter(Path.Combine(MacroDeck.ApplicationPaths.PluginConfigPath, plugin.Author.ToLower() + "_" + plugin.Name.ToLower() + ".json")))
- using (JsonWriter writer = new JsonTextWriter(sw))
- {
- serializer.Serialize(writer, pluginConfig);
- }
+ using var sw = new StreamWriter(FilePath(plugin));
+ using JsonWriter writer = new JsonTextWriter(sw);
+ serializer.Serialize(writer, pluginConfig);
} catch { }
}
+ private static Dictionary? GetConfig(MacroDeckPlugin plugin)
+ {
+ if (!File.Exists(FilePath(plugin)))
+ {
+ return new Dictionary();
+ }
+
+ return JsonConvert.DeserializeObject>(
+ File.ReadAllText(FilePath(plugin)),
+ new JsonSerializerSettings
+ {
+ TypeNameHandling = TypeNameHandling.Auto,
+ NullValueHandling = NullValueHandling.Ignore,
+ Error = (sender, args) => { args.ErrorContext.Handled = true; }
+ });
+ }
+
public static string GetValue(MacroDeckPlugin plugin = null, string key = "")
{
@@ -48,20 +61,7 @@ public static string GetValue(MacroDeckPlugin plugin = null, string key = "")
{
if (plugin == null || key == null) return value;
- Dictionary pluginConfig;
- if (!File.Exists(Path.Combine(MacroDeck.ApplicationPaths.PluginConfigPath, plugin.Author.ToLower() + "_" + plugin.Name.ToLower() + ".json")))
- {
- pluginConfig = new Dictionary();
- }
- else
- {
- pluginConfig = JsonConvert.DeserializeObject>(File.ReadAllText(Path.Combine(MacroDeck.ApplicationPaths.PluginConfigPath, plugin.Author.ToLower() + "_" + plugin.Name.ToLower() + ".json")), new JsonSerializerSettings
- {
- TypeNameHandling = TypeNameHandling.Auto,
- NullValueHandling = NullValueHandling.Ignore,
- Error = (sender, args) => { args.ErrorContext.Handled = true; }
- });
- }
+ Dictionary pluginConfig = GetConfig(plugin);
if (pluginConfig != null && !string.IsNullOrWhiteSpace(pluginConfig[key]))
{
@@ -76,7 +76,7 @@ public static void DeletePluginConfig(MacroDeckPlugin plugin)
{
try
{
- File.Delete(Path.Combine(MacroDeck.ApplicationPaths.PluginConfigPath, plugin.Author.ToLower() + "_" + plugin.Name.ToLower() + ".json"));
+ File.Delete(FilePath(plugin));
}catch { }
}
diff --git a/MacroDeck/Plugins/PluginCredentials.cs b/MacroDeck/Plugins/PluginCredentials.cs
index 609d2c7b..bc3fbacd 100644
--- a/MacroDeck/Plugins/PluginCredentials.cs
+++ b/MacroDeck/Plugins/PluginCredentials.cs
@@ -9,31 +9,18 @@ namespace SuchByte.MacroDeck.Plugins;
public class PluginCredentials
{
-
- public static void AddCredentials(MacroDeckPlugin plugin, Dictionary keyValuePairs)
+ private static string FileName(MacroDeckPlugin plugin)
{
- var keyValuePairsEncrypted = new Dictionary();
-
- foreach (var entry in keyValuePairs)
- {
- keyValuePairsEncrypted[entry.Key] = StringCipher.Encrypt(entry.Value, StringCipher.GetMachineGuid());
- }
-
- List> pluginCredentials;
-
- if (!File.Exists(MacroDeck.ApplicationPaths.PluginCredentialsPath + plugin.Author.ToLower() + "_" + plugin.Name.ToLower()))
- {
- pluginCredentials = new List>();
- } else
- {
- pluginCredentials = JsonConvert.DeserializeObject>>(File.ReadAllText(Path.Combine(MacroDeck.ApplicationPaths.PluginCredentialsPath, plugin.Author.ToLower() + "_" + plugin.Name.ToLower())), new JsonSerializerSettings
- {
- TypeNameHandling = TypeNameHandling.Auto,
- });
- }
+ return $"{plugin.Author.ToLower()}_{plugin.Name.ToLower()}";
+ }
- pluginCredentials.Add(keyValuePairsEncrypted);
+ private static string FilePath(MacroDeckPlugin plugin)
+ {
+ return Path.Combine(MacroDeck.ApplicationPaths.PluginCredentialsPath, FileName(plugin));
+ }
+ private static void Save(MacroDeckPlugin plugin, List> pluginCredentials)
+ {
var serializer = new JsonSerializer
{
TypeNameHandling = TypeNameHandling.Auto,
@@ -42,20 +29,17 @@ public static void AddCredentials(MacroDeckPlugin plugin, Dictionary keyValuePairs)
+ public static void AddCredentials(MacroDeckPlugin plugin, Dictionary keyValuePairs)
{
var keyValuePairsEncrypted = new Dictionary();
@@ -64,53 +48,44 @@ public static void SetCredentials(MacroDeckPlugin plugin, Dictionary> pluginCredentials;
+ var pluginCredentials = GetEncryptedCredentials(plugin);
- pluginCredentials = new List>();
pluginCredentials.Add(keyValuePairsEncrypted);
- var serializer = new JsonSerializer
- {
- TypeNameHandling = TypeNameHandling.Auto,
- NullValueHandling = NullValueHandling.Ignore,
- };
+ Save(plugin, pluginCredentials);
+ }
- try
- {
- using (var sw = new StreamWriter(Path.Combine(MacroDeck.ApplicationPaths.PluginCredentialsPath, plugin.Author.ToLower() + "_" + plugin.Name.ToLower())))
- using (JsonWriter writer = new JsonTextWriter(sw))
- {
- serializer.Serialize(writer, pluginCredentials);
- }
- }
- catch (Exception ex)
+ public static void SetCredentials(MacroDeckPlugin plugin, Dictionary keyValuePairs)
+ {
+ var keyValuePairsEncrypted = new Dictionary();
+
+ foreach (var entry in keyValuePairs)
{
- MacroDeckLogger.Error("Error while setting plugin credential: " + ex.Message);
+ keyValuePairsEncrypted[entry.Key] = StringCipher.Encrypt(entry.Value, StringCipher.GetMachineGuid());
}
+
+ List> pluginCredentials = new();
+ pluginCredentials.Add(keyValuePairsEncrypted);
+
+ Save(plugin, pluginCredentials);
}
public static void DeletePluginCredentials(MacroDeckPlugin plugin)
{
- File.Delete(Path.Combine(MacroDeck.ApplicationPaths.PluginCredentialsPath, plugin.Author.ToLower() + "_" + plugin.Name.ToLower()));
+ File.Delete(FilePath(plugin));
}
public static List> GetPluginCredentials(MacroDeckPlugin plugin)
{
- List> pluginCredentialsEncrypted;
- if (!File.Exists(Path.Combine(MacroDeck.ApplicationPaths.PluginCredentialsPath, plugin.Author.ToLower() + "_" + plugin.Name.ToLower())))
- {
- pluginCredentialsEncrypted = new List>();
- }
- else
- {
- pluginCredentialsEncrypted = JsonConvert.DeserializeObject>>(File.ReadAllText(Path.Combine(MacroDeck.ApplicationPaths.PluginCredentialsPath, plugin.Author.ToLower() + "_" + plugin.Name.ToLower())), new JsonSerializerSettings
- {
- TypeNameHandling = TypeNameHandling.Auto,
- });
- }
+ var pluginCredentialsEncrypted = GetEncryptedCredentials(plugin);
var pluginCredentialsDecrypted = new List>();
+ if (pluginCredentialsEncrypted == null)
+ {
+ return pluginCredentialsDecrypted;
+ }
+
foreach (var pluginCredentialEncrypted in pluginCredentialsEncrypted)
{
var pluginCredentialDecrypted = new Dictionary();
@@ -118,17 +93,34 @@ public static List> GetPluginCredentials(MacroDeckPlu
{
try
{
- pluginCredentialDecrypted[entry.Key] = StringCipher.Decrypt(entry.Value, StringCipher.GetMachineGuid());
- } catch
+ pluginCredentialDecrypted[entry.Key] =
+ StringCipher.Decrypt(entry.Value, StringCipher.GetMachineGuid());
+ }
+ catch
{
- MacroDeckLogger.Warning(typeof(PluginCredentials), $"Unable to decrypt credentials for {plugin.Name}. Perhaps the machine GUID changed?");
+ MacroDeckLogger.Warning(typeof(PluginCredentials),
+ $"Unable to decrypt credentials for {plugin.Name}. Perhaps the machine GUID changed?");
}
}
+
pluginCredentialsDecrypted.Add(pluginCredentialDecrypted);
}
return pluginCredentialsDecrypted;
}
-
+ private static List>? GetEncryptedCredentials(MacroDeckPlugin plugin)
+ {
+ if (!File.Exists(FilePath(plugin)))
+ {
+ return new();
+ }
+
+ return JsonConvert.DeserializeObject>>(
+ File.ReadAllText(FilePath(plugin)),
+ new JsonSerializerSettings
+ {
+ TypeNameHandling = TypeNameHandling.Auto,
+ });
+ }
}
\ No newline at end of file