Skip to content

Commit b9ce3c5

Browse files
authored
Merge pull request #3750 from marticliment/use-const-strings-for-settings-names
2 parents c36a253 + 7a98788 commit b9ce3c5

62 files changed

Lines changed: 647 additions & 438 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/UniGetUI.Core.IconStore/IconDatabase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ public async Task LoadIconAndScreenshotsDatabaseAsync()
6060
try
6161
{
6262
Uri DownloadUrl = new("https://raw.githubusercontent.com/marticliment/UniGetUI/main/WebBasedData/screenshot-database-v2.json");
63-
if (Settings.Get("IconDataBaseURL"))
63+
if (Settings.Get(Settings.K.IconDataBaseURL))
6464
{
65-
DownloadUrl = new Uri(Settings.GetValue("IconDataBaseURL"));
65+
DownloadUrl = new Uri(Settings.GetValue(Settings.K.IconDataBaseURL));
6666
}
6767

6868
using (HttpClient client = new(CoreTools.GenericHttpClientParameters))

src/UniGetUI.Core.LanguageEngine/LanguageData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ private static Person[] LoadLanguageTranslatorList()
132132
Name: (url is not null ? "@" : "") + (translator["name"] ?? "").ToString(),
133133
ProfilePicture: url is not null ? new Uri(url.ToString() + ".png") : null,
134134
GitHubUrl: url,
135-
Language: !LangShown ? LanguageData.LanguageReference[langKey.Key] : ""
135+
Language: !LangShown ? LanguageReference[langKey.Key] : ""
136136
);
137137
LangShown = true;
138138
result.Add(person);

src/UniGetUI.Core.LanguageEngine/LanguageEngine.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class LanguageEngine
2121

2222
public LanguageEngine(string ForceLanguage = "")
2323
{
24-
string LangName = Settings.GetValue("PreferredLanguage");
24+
string LangName = Settings.GetValue(Settings.K.PreferredLanguage);
2525
if (LangName is "default" or "")
2626
{
2727
LangName = CultureInfo.CurrentUICulture.ToString().Replace("-", "_");
@@ -93,7 +93,7 @@ public Dictionary<string, string> LoadLanguageFile(string LangKey)
9393

9494
string CachedLangFileToLoad = Path.Join(CoreData.UniGetUICacheDirectory_Lang, "lang_" + LangKey + ".json");
9595

96-
if (Settings.Get("DisableLangAutoUpdater"))
96+
if (Settings.Get(Settings.K.DisableLangAutoUpdater))
9797
{
9898
Logger.Warn("User has updated translations disabled");
9999
}
@@ -120,7 +120,7 @@ public Dictionary<string, string> LoadLanguageFile(string LangKey)
120120
}
121121
}
122122

123-
if (!Settings.Get("DisableLangAutoUpdater"))
123+
if (!Settings.Get(Settings.K.DisableLangAutoUpdater))
124124
_ = DownloadUpdatedLanguageFile(LangKey);
125125

126126
return LangDict;

src/UniGetUI.Core.SecureSettings/SecureSettings.cs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
11
using System.Diagnostics;
22
using UniGetUI.Core.Data;
33
using UniGetUI.Core.Tools;
4+
using YamlDotNet.Core.Tokens;
45
using YamlDotNet.Serialization;
56

67
namespace UniGetUI.Core.SettingsEngine.SecureSettings;
78

89
public static class SecureSettings
910
{
1011
// Various predefined secure settings keys
11-
public const string ALLOW_CLI_ARGUMENTS = "AllowCLIArguments";
12-
public const string ALLOW_IMPORTING_CLI_ARGUMENTS = "AllowImportingCLIArguments";
13-
public const string ALLOW_PREPOST_OPERATIONS = "AllowPrePostInstallCommands";
14-
public const string ALLOW_IMPORT_PREPOST_OPERATIONS = "AllowImportingPrePostInstallCommands";
15-
public const string FORCE_USER_GSUDO = "ForceUserGSudo";
12+
public enum K
13+
{
14+
AllowCLIArguments,
15+
AllowImportingCLIArguments,
16+
AllowPrePostOpCommand,
17+
AllowImportPrePostOpCommands,
18+
ForceUserGSudo,
19+
Unset
20+
};
21+
22+
public static string ResolveKey(K key)
23+
{
24+
return key switch
25+
{
26+
K.AllowCLIArguments => "AllowCLIArguments",
27+
K.AllowImportingCLIArguments => "AllowImportingCLIArguments",
28+
K.AllowPrePostOpCommand => "AllowPrePostInstallCommands",
29+
K.AllowImportPrePostOpCommands => "AllowImportingPrePostInstallCommands",
30+
K.ForceUserGSudo => "ForceUserGSudo",
31+
32+
K.Unset => throw new InvalidDataException("SecureSettings key was unset!"),
33+
_ => throw new KeyNotFoundException($"The SecureSettings key {key} was not found on the ResolveKey map")
34+
};
35+
}
1636

1737

1838
private static readonly Dictionary<string, bool> _cache = new();
@@ -23,9 +43,9 @@ public static class Args
2343
public const string DISABLE_FOR_USER = "--disable-secure-setting-for-user";
2444
}
2545

26-
public static bool Get(string setting)
46+
public static bool Get(K key)
2747
{
28-
string purifiedSetting = CoreTools.MakeValidFileName(setting);
48+
string purifiedSetting = CoreTools.MakeValidFileName(ResolveKey(key));
2949
if (_cache.TryGetValue(purifiedSetting, out var value))
3050
{
3151
return value;
@@ -48,9 +68,9 @@ public static bool Get(string setting)
4868
return exists;
4969
}
5070

51-
public static async Task<bool> TrySet(string setting, bool enabled)
71+
public static async Task<bool> TrySet(K key, bool enabled)
5272
{
53-
string purifiedSetting = CoreTools.MakeValidFileName(setting);
73+
string purifiedSetting = CoreTools.MakeValidFileName(ResolveKey(key));
5474
_cache.Remove(purifiedSetting);
5575

5676
string purifiedUser = CoreTools.MakeValidFileName(Environment.UserName);

0 commit comments

Comments
 (0)