I18NCommander/I18N Commander/Processor/AppSettings.cs

465 lines
16 KiB
C#
Raw Normal View History

using DataModel.Database;
using DataModel.Database.Common;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace Processor;
public static class AppSettings
{
2022-11-01 11:28:51 +00:00
#region Common DB Code
2022-07-30 21:07:04 +00:00
2022-11-01 11:28:51 +00:00
private static readonly Dictionary<string, object> CACHES = new();
private static readonly Dictionary<string, bool> CACHE_LOADED = new();
2022-07-30 13:49:19 +00:00
2022-11-01 11:28:51 +00:00
private static async Task<T> GetSetting<T>(string settingName, T defaultValue)
{
2022-11-01 11:28:51 +00:00
// When possible, use the cache:
if (CACHE_LOADED.ContainsKey(settingName) && CACHE_LOADED[settingName])
return (T)CACHES[settingName];
2022-11-01 11:28:51 +00:00
var settingValue = defaultValue;
2022-10-03 15:53:49 +00:00
try
{
2022-10-03 15:53:49 +00:00
// Get the database:
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
// Check, if the setting is already set:
2022-11-01 11:28:51 +00:00
if (await db.Settings.FirstOrDefaultAsync(n => n.Code == settingName) is { } existingSetting)
{
settingValue = settingValue switch
{
bool => (T)(object)existingSetting.BoolValue,
string => (T)(object)existingSetting.TextValue,
int => (T)(object)existingSetting.IntegerValue,
Guid => (T)(object)existingSetting.GuidValue,
SettingDeepLMode or
SettingDeepLAction or
SettingGeneratorMode => (T)(object)existingSetting.IntegerValue,
_ => defaultValue,
};
return settingValue;
2022-10-03 15:53:49 +00:00
}
// Does not exist, so create it:
var setting = new Setting
{
2022-11-01 11:28:51 +00:00
Code = settingName,
2022-10-03 15:53:49 +00:00
};
2022-11-01 11:28:51 +00:00
switch (settingValue)
{
case bool:
setting.BoolValue = (bool)(object)settingValue;
break;
case string:
setting.TextValue = (string)(object)settingValue;
break;
case int:
setting.IntegerValue = (int)(object)settingValue;
break;
case Guid:
setting.GuidValue = (Guid)(object)settingValue;
break;
case SettingDeepLMode:
case SettingDeepLAction:
case SettingGeneratorMode:
setting.IntegerValue = (int)(object)settingValue;
break;
}
2022-10-03 15:53:49 +00:00
await db.Settings.AddAsync(setting);
await db.SaveChangesAsync();
2022-11-01 11:28:51 +00:00
return settingValue;
2022-10-03 15:53:49 +00:00
}
finally
{
2022-11-01 11:28:51 +00:00
CACHE_LOADED[settingName] = true;
CACHES[settingName] = settingValue!;
2022-10-03 15:53:49 +00:00
}
}
2022-07-30 13:49:19 +00:00
2022-11-01 11:28:51 +00:00
public static async Task SetSetting<T>(string settingName, T settingValue)
2022-07-30 13:49:19 +00:00
{
2022-07-30 15:04:32 +00:00
// Update the cache:
2022-11-01 11:28:51 +00:00
CACHES[settingName] = settingValue!;
CACHE_LOADED[settingName] = true;
2022-07-30 15:04:32 +00:00
2022-07-30 13:49:19 +00:00
// Get the database:
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
// Check, if the setting is already set:
2022-11-01 11:28:51 +00:00
if (await db.Settings.FirstOrDefaultAsync(n => n.Code == settingName) is { } existingSetting)
{
switch (settingValue)
{
case bool:
existingSetting.BoolValue = (bool)(object)settingValue;
break;
case string:
existingSetting.TextValue = (string)(object)settingValue;
break;
case int:
existingSetting.IntegerValue = (int)(object)settingValue;
break;
case Guid:
existingSetting.GuidValue = (Guid)(object)settingValue;
break;
case SettingDeepLMode:
case SettingDeepLAction:
case SettingGeneratorMode:
existingSetting.IntegerValue = (int)(object)settingValue;
break;
}
2022-07-30 13:49:19 +00:00
await db.SaveChangesAsync();
}
// Does not exist, so create it:
else
{
var setting = new Setting
{
2022-11-01 11:28:51 +00:00
Code = SettingNames.GENERATOR_DOTNET_ENABLED,
2022-07-30 13:49:19 +00:00
};
2022-11-01 11:28:51 +00:00
switch (settingValue)
{
case bool:
setting.BoolValue = (bool)(object)settingValue;
break;
case string:
setting.TextValue = (string)(object)settingValue;
break;
case int:
setting.IntegerValue = (int)(object)settingValue;
break;
case Guid:
setting.GuidValue = (Guid)(object)settingValue;
break;
case SettingDeepLMode:
case SettingDeepLAction:
case SettingGeneratorMode:
setting.IntegerValue = (int)(object)settingValue;
break;
}
2022-07-30 13:49:19 +00:00
await db.Settings.AddAsync(setting);
await db.SaveChangesAsync();
}
}
2022-11-01 11:28:51 +00:00
#endregion
2022-07-30 13:49:19 +00:00
2022-11-01 11:28:51 +00:00
#region DeepL Settings
2022-10-03 15:53:49 +00:00
2022-11-01 11:28:51 +00:00
#region DeepL Mode
2022-10-03 15:53:49 +00:00
public static async Task SetDeepLMode(SettingDeepLMode mode)
{
DeepL.ResetState();
await AppSettings.SetSetting(SettingNames.DEEPL_MODE, mode);
}
2022-10-03 15:53:49 +00:00
2022-11-01 11:28:51 +00:00
public static async Task<SettingDeepLMode> GetDeepLMode() => await AppSettings.GetSetting(SettingNames.DEEPL_MODE, SettingDeepLMode.DISABLED);
2022-10-03 15:53:49 +00:00
2022-07-30 13:49:19 +00:00
#endregion
2022-07-30 14:38:38 +00:00
2022-11-01 11:28:51 +00:00
#region DeepL API Key
2022-07-30 15:04:32 +00:00
public static async Task SetDeepLAPIKey(string apiKey)
{
DeepL.ResetState();
await AppSettings.SetSetting(SettingNames.DEEPL_API_KEY, apiKey);
}
2022-11-01 11:28:51 +00:00
public static async Task<string> GetDeepLAPIKey() => await AppSettings.GetSetting(SettingNames.DEEPL_API_KEY, string.Empty);
#endregion
2022-07-30 14:38:38 +00:00
2022-11-01 11:28:51 +00:00
#region DeepL Action
2022-10-03 15:53:49 +00:00
2022-11-01 11:28:51 +00:00
public static async Task SetDeepLAction(SettingDeepLAction action) => await AppSettings.SetSetting(SettingNames.DEEPL_ACTION, action);
2022-10-03 15:53:49 +00:00
2022-11-01 11:28:51 +00:00
public static async Task<SettingDeepLAction> GetDeepLAction() => await AppSettings.GetSetting(SettingNames.DEEPL_ACTION, SettingDeepLAction.MANUAL);
2022-10-03 15:53:49 +00:00
2022-07-30 14:38:38 +00:00
#endregion
2022-07-30 21:07:04 +00:00
#region DeepL Source Culture
2022-11-01 11:28:51 +00:00
public static async Task SetDeepLSourceCultureIndex(int cultureIndex) => await AppSettings.SetSetting(SettingNames.DEEPL_SOURCE_CULTURE, cultureIndex);
2022-10-03 15:53:49 +00:00
2022-11-01 11:28:51 +00:00
public static async Task<int> GetDeepLSourceCultureIndex() => await AppSettings.GetSetting(SettingNames.DEEPL_SOURCE_CULTURE, -1);
2022-10-03 15:53:49 +00:00
#endregion
2022-07-30 21:07:04 +00:00
#endregion
2022-11-01 11:28:51 +00:00
#region Culture Settings
2022-07-30 21:07:04 +00:00
2022-08-05 19:11:02 +00:00
#region List of culture indices
2022-07-30 21:17:57 +00:00
private static List<int> CACHE_CULTURES_INDICES = new();
2022-07-30 21:07:04 +00:00
public static async Task<List<int>> GetCultureIndices()
2022-07-30 21:07:04 +00:00
{
2022-07-30 21:17:57 +00:00
// When possible, use the cache:
if (CACHE_CULTURES_INDICES.Count > 0)
return CACHE_CULTURES_INDICES;
2022-07-30 21:17:57 +00:00
2022-07-30 21:07:04 +00:00
// Get the database:
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
// Count the number of cultures:
var list = await db.Settings.Where(n => n.Code.StartsWith(SettingNames.CULTURE)).OrderBy(n => n.IntegerValue).Select(n => n.IntegerValue).ToListAsync();
2022-07-30 21:07:04 +00:00
// We have at least one default culture:
if(list.Count == 0)
2022-07-30 21:17:57 +00:00
{
// Add the default culture, which is en-US:
await AppSettings.SetCultureCode(1, "en-US");
2022-07-30 21:17:57 +00:00
}
else
CACHE_CULTURES_INDICES = list;
return CACHE_CULTURES_INDICES;
2022-07-30 21:07:04 +00:00
}
#endregion
#region Get a culture
2022-07-30 21:14:39 +00:00
// Cache the cultures:
private static readonly Dictionary<int, string> CACHE_CULTURES = new();
2022-07-30 21:07:04 +00:00
public static async Task<string> GetCultureCode(int index)
{
2022-07-30 21:14:39 +00:00
// Check the cache:
if (CACHE_CULTURES.TryGetValue(index, out var cultureCode))
return cultureCode;
2022-07-30 21:07:04 +00:00
// Get the database:
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
// Get the culture code:
2022-07-31 19:19:13 +00:00
var code = await db.Settings.FirstOrDefaultAsync(n => n.Code.StartsWith(SettingNames.CULTURE) && n.IntegerValue == index);
2022-07-30 21:07:04 +00:00
2022-07-30 21:14:39 +00:00
// When the culture code is not set & index = 1, use the default culture en-US:
if(code is null && index == 1)
{
CACHE_CULTURES.Add(index, "en-US");
2022-07-30 21:07:04 +00:00
return "en-US";
2022-07-30 21:14:39 +00:00
}
2022-07-30 21:07:04 +00:00
2022-07-30 21:14:39 +00:00
// Update the cache & return the code:
var codeText = code?.TextValue ?? string.Empty;
CACHE_CULTURES[index] = codeText;
return codeText;
2022-07-30 21:07:04 +00:00
}
#endregion
#region Set a culture
public static async Task SetCultureCode(int index, string code)
{
// Keep a copy of the previous name:
var previousCode = await AppSettings.GetCultureCode(index);
2022-07-30 21:14:39 +00:00
// Update the cache:
CACHE_CULTURES[index] = code;
2022-07-30 21:07:04 +00:00
// Get the database:
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
// Check, if the setting is already set:
2022-07-31 19:19:13 +00:00
if (await db.Settings.FirstOrDefaultAsync(n => n.Code.StartsWith(SettingNames.CULTURE) && n.IntegerValue == index) is {} existingSetting)
2022-07-30 21:07:04 +00:00
{
existingSetting.TextValue = code;
await db.SaveChangesAsync();
}
// Does not exist, so create it:
else
{
var setting = new Setting
{
2022-07-31 19:19:13 +00:00
Code = $"{SettingNames.CULTURE} {index}",
2022-07-30 21:07:04 +00:00
IntegerValue = index,
TextValue = code,
};
await db.Settings.AddAsync(setting);
await db.SaveChangesAsync();
2022-07-30 21:17:57 +00:00
// Update the list of cultures indices:
CACHE_CULTURES_INDICES.Add(index);
2022-07-30 21:07:04 +00:00
}
// Next, we need to rename the culture inside all translations as well:
var translations = await db.Translations.Where(n => n.Culture == previousCode).ToListAsync();
foreach (var translation in translations)
translation.Culture = code;
await db.SaveChangesAsync();
2022-07-30 21:07:04 +00:00
}
#endregion
#region Delete a culture
public static async Task DeleteCulture(int index)
{
// Get the database:
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
// Check, if the setting is already set:
if (await db.Settings.FirstOrDefaultAsync(n => n.Code.StartsWith(SettingNames.CULTURE) && n.IntegerValue == index) is {} existingSetting)
{
db.Settings.Remove(existingSetting);
await db.SaveChangesAsync();
}
// Update the list of cultures indices:
CACHE_CULTURES_INDICES.Remove(index);
// Update the cache:
CACHE_CULTURES.Remove(index);
}
#endregion
#region Get a list of cultures
public readonly record struct CultureInfo(string Code, int Index);
public static async Task<List<CultureInfo>> GetCultureInfos()
{
// Get the number of cultures:
var cultureIndices = await AppSettings.GetCultureIndices();
// Get the culture codes:
var cultureInfos = new List<CultureInfo>();
foreach (var cultureIndex in cultureIndices)
cultureInfos.Add(new CultureInfo
{
Code = await AppSettings.GetCultureCode(cultureIndex),
Index = cultureIndex,
});
return cultureInfos;
}
#endregion
2022-07-30 21:07:04 +00:00
#endregion
2022-10-03 16:13:07 +00:00
#region Generator Settings
#region Generator Mode
2022-11-01 11:28:51 +00:00
public static async Task<SettingGeneratorMode> GetGeneratorMode() => await AppSettings.GetSetting(SettingNames.GENERATOR_MODE, SettingGeneratorMode.MANUAL);
2022-10-03 16:13:07 +00:00
2022-11-01 11:28:51 +00:00
public static async Task SetGeneratorMode(SettingGeneratorMode mode) => await AppSettings.SetSetting(SettingNames.GENERATOR_MODE, mode);
2022-10-03 16:13:07 +00:00
#endregion
2022-10-03 17:10:39 +00:00
#region .NET Generator Enabled/Disabled
2022-11-01 11:28:51 +00:00
public static async Task<bool> GetGeneratorDotnetEnabled() => await AppSettings.GetSetting(SettingNames.GENERATOR_DOTNET_ENABLED, false);
2022-10-03 17:10:39 +00:00
2022-11-01 11:28:51 +00:00
public static async Task SetGeneratorDotnetEnabled(bool enabled) => await AppSettings.SetSetting(SettingNames.GENERATOR_DOTNET_ENABLED, enabled);
2022-10-03 17:10:39 +00:00
#endregion
#region .NET Generator Destination Path
2022-11-01 11:28:51 +00:00
public static async Task<string> GetGeneratorDotnetDestinationPath() => await AppSettings.GetSetting(SettingNames.GENERATOR_DOTNET_DESTINATION_PATH, string.Empty);
2022-10-03 17:10:39 +00:00
2022-11-01 11:28:51 +00:00
public static async Task SetGeneratorDotnetDestinationPath(string path) => await AppSettings.SetSetting(SettingNames.GENERATOR_DOTNET_DESTINATION_PATH, path);
2022-10-03 17:10:39 +00:00
#endregion
#region .NET Generator Namespace
2022-11-01 11:28:51 +00:00
public static async Task<string> GetGeneratorDotnetNamespace() => await AppSettings.GetSetting(SettingNames.GENERATOR_DOTNET_NAMESPACE, "I18N");
2022-11-01 11:28:51 +00:00
public static async Task SetGeneratorDotnetNamespace(string updatedNamespace) => await AppSettings.SetSetting(SettingNames.GENERATOR_DOTNET_NAMESPACE, updatedNamespace);
#endregion
#region .NET Generator Default Culture
2022-11-01 11:28:51 +00:00
public static async Task<int> GetGeneratorDotnetDefaultCultureIndex() => await AppSettings.GetSetting(SettingNames.GENERATOR_DOTNET_DEFAULT_CULTURE, 0);
2022-11-01 11:28:51 +00:00
public static async Task SetGeneratorDotnetDefaultCultureIndex(int updatedCulture) => await AppSettings.SetSetting(SettingNames.GENERATOR_DOTNET_DEFAULT_CULTURE, updatedCulture);
#endregion
2022-10-29 16:48:38 +00:00
#region Godot Generator Enabled/Disabled
2022-11-01 11:28:51 +00:00
public static async Task<bool> GetGeneratorGodotEnabled() => await AppSettings.GetSetting(SettingNames.GENERATOR_GODOT_ENABLED, false);
2022-10-29 16:48:38 +00:00
2022-11-01 11:28:51 +00:00
public static async Task SetGeneratorGodotEnabled(bool enabled) => await AppSettings.SetSetting(SettingNames.GENERATOR_GODOT_ENABLED, enabled);
2022-10-29 16:48:38 +00:00
#endregion
#region Godot Generator Destination Path
2022-11-01 11:28:51 +00:00
public static async Task<string> GetGeneratorGodotDestinationPath() => await AppSettings.GetSetting(SettingNames.GENERATOR_GODOT_DESTINATION_PATH, string.Empty);
2022-10-29 16:48:38 +00:00
2022-11-01 11:28:51 +00:00
public static async Task SetGeneratorGodotDestinationPath(string path) => await AppSettings.SetSetting(SettingNames.GENERATOR_GODOT_DESTINATION_PATH, path);
2022-10-29 16:48:38 +00:00
#endregion
2022-10-03 16:13:07 +00:00
#endregion
2023-01-22 12:02:08 +00:00
#region Auto-Export Settings
#region Auto-Export Enabled/Disabled
public static async Task<bool> GetAutoExportEnabled() => await AppSettings.GetSetting(SettingNames.AUTO_EXPORT_ENABLED, false);
public static async Task SetAutoExportEnabled(bool enabled) => await AppSettings.SetSetting(SettingNames.AUTO_EXPORT_ENABLED, enabled);
#endregion
#region Auto-Export Destination Path
public static async Task<string> GetAutoExportDestinationPath() => await AppSettings.GetSetting(SettingNames.AUTO_EXPORT_DESTINATION_PATH, string.Empty);
public static async Task SetAutoExportDestinationPath(string path) => await AppSettings.SetSetting(SettingNames.AUTO_EXPORT_DESTINATION_PATH, path);
#endregion
#region Auto-Export Filename
public static async Task<string> GetAutoExportFilename() => await AppSettings.GetSetting(SettingNames.AUTO_EXPORT_FILENAME, "I18NCommander.json");
public static async Task SetAutoExportFilename(string filename) => await AppSettings.SetSetting(SettingNames.AUTO_EXPORT_FILENAME, filename);
#endregion
#region Auto-Export Sensitive Data
public static async Task<bool> GetAutoExportSensitiveData() => await AppSettings.GetSetting(SettingNames.AUTO_EXPORT_SENSITIVE_DATA, false);
public static async Task SetAutoExportSensitiveData(bool enabled) => await AppSettings.SetSetting(SettingNames.AUTO_EXPORT_SENSITIVE_DATA, enabled);
#endregion
#endregion
}