From 313b936d96908f7eb10cd5eb442e41a57c1d45b4 Mon Sep 17 00:00:00 2001 From: DrakiaXYZ Date: Sat, 26 Oct 2024 08:16:03 +0000 Subject: [PATCH] Update registry handling to work with new JSON based registry redirect (!68) - Remove the "Wipe Registry" button - Remove the registry wipe on new profile creation/profile wipe This is a companion to [#172](https://dev.sp-tarkov.com/SPT/Modules/pulls/172) which moves SPTs "registry" into a JSON file to avoid cross contamination with live data Co-authored-by: DrakiaXYZ <565558+TheDgtl@users.noreply.github.com> Reviewed-on: https://dev.sp-tarkov.com/SPT/Launcher/pulls/68 Co-authored-by: DrakiaXYZ Co-committed-by: DrakiaXYZ --- .../Controllers/AccountManager.cs | 1 + .../Controllers/GameStarter.cs | 30 +++++++++---------- .../ViewModels/SettingsViewModel.cs | 17 ----------- project/SPT.Launcher/Views/SettingsView.axaml | 4 --- 4 files changed, 15 insertions(+), 37 deletions(-) diff --git a/project/SPT.Launcher.Base/Controllers/AccountManager.cs b/project/SPT.Launcher.Base/Controllers/AccountManager.cs index 4b759fc..3e5a154 100644 --- a/project/SPT.Launcher.Base/Controllers/AccountManager.cs +++ b/project/SPT.Launcher.Base/Controllers/AccountManager.cs @@ -289,6 +289,7 @@ namespace SPT.Launcher } SelectedAccount.edition = edition; + SelectedAccount.wipe = true; LogManager.Instance.Info($"Account Wiped: {data.username} -> {edition}"); return AccountStatus.OK; } diff --git a/project/SPT.Launcher.Base/Controllers/GameStarter.cs b/project/SPT.Launcher.Base/Controllers/GameStarter.cs index 2dcf1a2..bcd0bcf 100644 --- a/project/SPT.Launcher.Base/Controllers/GameStarter.cs +++ b/project/SPT.Launcher.Base/Controllers/GameStarter.cs @@ -21,6 +21,7 @@ using SPT.Launcher.Controllers; using SPT.Launcher.Interfaces; using System.Runtime.InteropServices; using SPT.Launcher.Models.SPT; +using Newtonsoft.Json.Linq; namespace SPT.Launcher { @@ -90,7 +91,7 @@ namespace SPT.Launcher if (account.wipe) { LogManager.Instance.Info("[LaunchGame] Wipe profile requested"); - RemoveRegistryKeys(); + RemoveProfileRegistryKeys(account.id); CleanTempFiles(); } @@ -288,27 +289,24 @@ namespace SPT.Launcher } /// - /// Remove the registry keys + /// Remove the SPT JSON-based registry keys associated with the given profile ID /// - /// returns true if the keys were removed. returns false if an exception occured - public bool RemoveRegistryKeys() + public void RemoveProfileRegistryKeys(string profileId) { - try - { - var key = Registry.CurrentUser.OpenSubKey(registrySettings, true); + var registryFile = new FileInfo(Path.Combine(Environment.CurrentDirectory, "user\\sptRegistry\\registry.json")); - foreach (var value in key.GetValueNames()) - { - key.DeleteValue(value); - } - } - catch (Exception ex) + if (!registryFile.Exists) { - LogManager.Instance.Exception(ex); - return false; + return; } - return true; + JObject registryData = JObject.Parse(File.ReadAllText(registryFile.FullName)); + + // Find any property that has a key containing the profileId, and remove it + var propsToRemove = registryData.Properties().Where(prop => prop.Name.Contains(profileId, StringComparison.CurrentCultureIgnoreCase)).ToList(); + propsToRemove.ForEach(prop => prop.Remove()); + + File.WriteAllText(registryFile.FullName, registryData.ToString()); } /// diff --git a/project/SPT.Launcher/ViewModels/SettingsViewModel.cs b/project/SPT.Launcher/ViewModels/SettingsViewModel.cs index 928bb49..b31eea6 100644 --- a/project/SPT.Launcher/ViewModels/SettingsViewModel.cs +++ b/project/SPT.Launcher/ViewModels/SettingsViewModel.cs @@ -151,23 +151,6 @@ namespace SPT.Launcher.ViewModels } } - public void RemoveRegistryKeysCommand() - { - LogManager.Instance.Info("[Settings] Removing registry keys ..."); - bool regKeysRemoved = gameStarter.RemoveRegistryKeys(); - - if (regKeysRemoved) - { - LogManager.Instance.Info("[Settings] Registry keys removed"); - SendNotification("", LocalizationProvider.Instance.remove_registry_keys_succeeded, Avalonia.Controls.Notifications.NotificationType.Success); - } - else - { - LogManager.Instance.Info("[Settings] Registry keys failed to remove"); - SendNotification("", LocalizationProvider.Instance.remove_registry_keys_failed, Avalonia.Controls.Notifications.NotificationType.Error); - } - } - public async Task ResetGameSettingsCommand() { LogManager.Instance.Info("[Settings] Reseting game settings ..."); diff --git a/project/SPT.Launcher/Views/SettingsView.axaml b/project/SPT.Launcher/Views/SettingsView.axaml index a8fe6cc..0cde6ce 100644 --- a/project/SPT.Launcher/Views/SettingsView.axaml +++ b/project/SPT.Launcher/Views/SettingsView.axaml @@ -16,10 +16,6 @@ Opacity=".7" /> -