Skip to content

Commit

Permalink
Develop (#337)
Browse files Browse the repository at this point in the history
* Minor refactoring (#333)

* refactor duplicate code

* refactor duplicate code

* Update Magick dependency (#332)

Co-authored-by: Will <54237626+PhoenixWyllow@users.noreply.github.com>
  • Loading branch information
manuelmayer-dev and PhoenixWyllow authored Jan 7, 2023
1 parent a7d3abc commit beace96
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 99 deletions.
3 changes: 1 addition & 2 deletions MacroDeck/MacroDeck.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@
<PackageReference Include="Dax-FCTB" Version="2.16.26.120" />
<PackageReference Include="Fleck" Version="1.2.0" />
<PackageReference Include="GiphyDotNet" Version="2.0.0" />
<PackageReference Include="Magick.NET-Q16-x64" Version="8.3.1" />
<PackageReference Include="Magick.NET.Core" Version="8.3.1" />
<PackageReference Include="Magick.NET-Q16-x64" Version="12.2.2" />
<PackageReference Include="Magick.NET.SystemDrawing" Version="4.0.6" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.2" />
Expand Down
66 changes: 33 additions & 33 deletions MacroDeck/Plugins/PluginConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> pluginConfig;

if (!File.Exists(Path.Combine(MacroDeck.ApplicationPaths.PluginConfigPath, plugin.Author.ToLower() + "_" + plugin.Name.ToLower() + ".json")))
{
pluginConfig = new Dictionary<string, string>();
}
else
{
pluginConfig = JsonConvert.DeserializeObject<Dictionary<string, string>>(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;

Expand All @@ -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<string, string>? GetConfig(MacroDeckPlugin plugin)
{
if (!File.Exists(FilePath(plugin)))
{
return new Dictionary<string, string>();
}

return JsonConvert.DeserializeObject<Dictionary<string, string>>(
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 = "")
{
Expand All @@ -48,20 +61,7 @@ public static string GetValue(MacroDeckPlugin plugin = null, string key = "")
{
if (plugin == null || key == null) return value;

Dictionary<string, string> pluginConfig;
if (!File.Exists(Path.Combine(MacroDeck.ApplicationPaths.PluginConfigPath, plugin.Author.ToLower() + "_" + plugin.Name.ToLower() + ".json")))
{
pluginConfig = new Dictionary<string, string>();
}
else
{
pluginConfig = JsonConvert.DeserializeObject<Dictionary<string, string>>(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<string, string> pluginConfig = GetConfig(plugin);

if (pluginConfig != null && !string.IsNullOrWhiteSpace(pluginConfig[key]))
{
Expand All @@ -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 { }
}

Expand Down
120 changes: 56 additions & 64 deletions MacroDeck/Plugins/PluginCredentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,18 @@ namespace SuchByte.MacroDeck.Plugins;

public class PluginCredentials
{

public static void AddCredentials(MacroDeckPlugin plugin, Dictionary<string, string> keyValuePairs)
private static string FileName(MacroDeckPlugin plugin)
{
var keyValuePairsEncrypted = new Dictionary<string, string>();

foreach (var entry in keyValuePairs)
{
keyValuePairsEncrypted[entry.Key] = StringCipher.Encrypt(entry.Value, StringCipher.GetMachineGuid());
}

List<Dictionary<string, string>> pluginCredentials;

if (!File.Exists(MacroDeck.ApplicationPaths.PluginCredentialsPath + plugin.Author.ToLower() + "_" + plugin.Name.ToLower()))
{
pluginCredentials = new List<Dictionary<string, string>>();
} else
{
pluginCredentials = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(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<Dictionary<string, string>> pluginCredentials)
{
var serializer = new JsonSerializer
{
TypeNameHandling = TypeNameHandling.Auto,
Expand All @@ -42,20 +29,17 @@ public static void AddCredentials(MacroDeckPlugin plugin, Dictionary<string, str

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);
}
using var sw = new StreamWriter(FilePath(plugin));
using JsonWriter writer = new JsonTextWriter(sw);
serializer.Serialize(writer, pluginCredentials);
}
catch (Exception ex)
{
MacroDeckLogger.Error("Error while adding plugin credential: " + ex.Message);
MacroDeckLogger.Error(typeof(PluginCredentials), "Error while adding plugin credential: " + ex.Message);
}

}

public static void SetCredentials(MacroDeckPlugin plugin, Dictionary<string, string> keyValuePairs)
public static void AddCredentials(MacroDeckPlugin plugin, Dictionary<string, string> keyValuePairs)
{
var keyValuePairsEncrypted = new Dictionary<string, string>();

Expand All @@ -64,71 +48,79 @@ public static void SetCredentials(MacroDeckPlugin plugin, Dictionary<string, str
keyValuePairsEncrypted[entry.Key] = StringCipher.Encrypt(entry.Value, StringCipher.GetMachineGuid());
}

List<Dictionary<string, string>> pluginCredentials;
var pluginCredentials = GetEncryptedCredentials(plugin);

pluginCredentials = new List<Dictionary<string, string>>();
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<string, string> keyValuePairs)
{
var keyValuePairsEncrypted = new Dictionary<string, string>();

foreach (var entry in keyValuePairs)
{
MacroDeckLogger.Error("Error while setting plugin credential: " + ex.Message);
keyValuePairsEncrypted[entry.Key] = StringCipher.Encrypt(entry.Value, StringCipher.GetMachineGuid());
}

List<Dictionary<string, string>> 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<Dictionary<string, string>> GetPluginCredentials(MacroDeckPlugin plugin)
{
List<Dictionary<string, string>> pluginCredentialsEncrypted;
if (!File.Exists(Path.Combine(MacroDeck.ApplicationPaths.PluginCredentialsPath, plugin.Author.ToLower() + "_" + plugin.Name.ToLower())))
{
pluginCredentialsEncrypted = new List<Dictionary<string, string>>();
}
else
{
pluginCredentialsEncrypted = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(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<Dictionary<string, string>>();

if (pluginCredentialsEncrypted == null)
{
return pluginCredentialsDecrypted;
}

foreach (var pluginCredentialEncrypted in pluginCredentialsEncrypted)
{
var pluginCredentialDecrypted = new Dictionary<string, string>();
foreach (var entry in pluginCredentialEncrypted)
{
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<Dictionary<string, string>>? GetEncryptedCredentials(MacroDeckPlugin plugin)
{
if (!File.Exists(FilePath(plugin)))
{
return new();
}

return JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(
File.ReadAllText(FilePath(plugin)),
new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
});
}
}

0 comments on commit beace96

Please sign in to comment.