From 7b3c8570420318b0e763231011456fc9ca36b977 Mon Sep 17 00:00:00 2001 From: Robert McRackan Date: Sat, 17 Jul 2021 08:13:23 -0400 Subject: [PATCH] unsafe migration helper: Get => TryGet --- LibationLauncher/Program.cs | 16 +++++++++------- LibationLauncher/UNSAFE_MigrationHelper.cs | 18 ++++++++---------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/LibationLauncher/Program.cs b/LibationLauncher/Program.cs index fb36c1d7..a49cdbc6 100644 --- a/LibationLauncher/Program.cs +++ b/LibationLauncher/Program.cs @@ -13,7 +13,6 @@ using Microsoft.Extensions.Configuration; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Serilog; -using Serilog.Events; namespace LibationLauncher { @@ -266,18 +265,21 @@ namespace LibationLauncher { { var settingsKey = "DownloadsInProgressEnum"; - UNSAFE_MigrationHelper.Settings_Update(settingsKey, translatePath(UNSAFE_MigrationHelper.Settings_Get(settingsKey))); + if (UNSAFE_MigrationHelper.Settings_TryGet(settingsKey, out var value)) + UNSAFE_MigrationHelper.Settings_Update(settingsKey, translatePath(value)); } { var settingsKey = "DecryptInProgressEnum"; - UNSAFE_MigrationHelper.Settings_Update(settingsKey, translatePath(UNSAFE_MigrationHelper.Settings_Get(settingsKey))); + if (UNSAFE_MigrationHelper.Settings_TryGet(settingsKey, out var value)) + UNSAFE_MigrationHelper.Settings_Update(settingsKey, translatePath(value)); } - UNSAFE_MigrationHelper.AppSettings_Update( - UNSAFE_MigrationHelper.LIBATION_FILES_KEY, - translatePath(UNSAFE_MigrationHelper.AppSettings_Get(UNSAFE_MigrationHelper.LIBATION_FILES_KEY)) - ); + { // appsettings.json + var appSettingsKey = UNSAFE_MigrationHelper.LIBATION_FILES_KEY; + if (UNSAFE_MigrationHelper.AppSettings_TryGet(appSettingsKey, out var value)) + UNSAFE_MigrationHelper.AppSettings_Update(appSettingsKey, translatePath(value)); + } } private static string translatePath(string path) diff --git a/LibationLauncher/UNSAFE_MigrationHelper.cs b/LibationLauncher/UNSAFE_MigrationHelper.cs index 1fd9f282..d4f830c7 100644 --- a/LibationLauncher/UNSAFE_MigrationHelper.cs +++ b/LibationLauncher/UNSAFE_MigrationHelper.cs @@ -16,16 +16,15 @@ namespace LibationLauncher public static bool AppSettingsJson_Exists => File.Exists(APPSETTINGS_JSON); - public static string AppSettings_Get(string key) + public static bool AppSettings_TryGet(string key, out string value) { bool success = false; JToken val = null; process_AppSettingsJson(jObj => success = jObj.TryGetValue(key, out val), false); - if (success) - return val.Value(); - return null; + value = success ? val.Value() : null; + return success; } /// only insert if not exists @@ -80,22 +79,21 @@ namespace LibationLauncher { get { - var value = AppSettings_Get(LIBATION_FILES_KEY); - return value is null ? null : Path.Combine(value, SETTINGS_JSON); + var success = AppSettings_TryGet(LIBATION_FILES_KEY, out var value); + return !success || value is null ? null : Path.Combine(value, SETTINGS_JSON); } } public static bool SettingsJson_Exists => SettingsJsonPath is not null && File.Exists(SettingsJsonPath); - public static string Settings_Get(string key) + public static bool Settings_TryGet(string key, out string value) { bool success = false; JToken val = null; process_SettingsJson(jObj => success = jObj.TryGetValue(key, out val), false); - if (success) - return val.Value(); - return null; + value = success ? val.Value() : null; + return success; } /// only insert if not exists