From 93b082263b52530fd6104996427b116fb1dcfadf Mon Sep 17 00:00:00 2001 From: pizzaboxer <41478239+pizzaboxer@users.noreply.github.com> Date: Sat, 4 Feb 2023 17:03:26 +0000 Subject: [PATCH] Refactor settings management --- Bloxstrap/App.xaml.cs | 16 ++-- Bloxstrap/Bootstrapper.cs | 52 ++++++------ Bloxstrap/Dialogs/BootstrapperDialogForm.cs | 2 +- Bloxstrap/Dialogs/LegacyDialog2011.cs | 2 +- Bloxstrap/Dialogs/ProgressDialog.cs | 8 +- Bloxstrap/Dialogs/VistaDialog.cs | 2 +- Bloxstrap/Enums/BootstrapperStyle.cs | 27 ++---- .../Integrations/DiscordRichPresence.cs | 2 +- .../Helpers/Integrations/RbxFpsUnlocker.cs | 6 +- Bloxstrap/Helpers/Integrations/ReShade.cs | 18 ++-- Bloxstrap/Helpers/JsonManager.cs | 65 +++++++++++++++ Bloxstrap/Helpers/Protocol.cs | 10 +-- .../Models/{SettingsFormat.cs => Settings.cs} | 2 +- Bloxstrap/Properties/launchSettings.json | 10 ++- Bloxstrap/SettingsManager.cs | 83 ------------------- Bloxstrap/ViewModels/BootstrapperViewModel.cs | 26 +++--- Bloxstrap/ViewModels/InstallationViewModel.cs | 14 ++-- Bloxstrap/ViewModels/IntegrationsViewModel.cs | 26 +++--- Bloxstrap/ViewModels/MainWindowViewModel.cs | 8 +- Bloxstrap/ViewModels/ModsViewModel.cs | 12 +-- Bloxstrap/Views/MainWindow.xaml.cs | 2 +- 21 files changed, 184 insertions(+), 209 deletions(-) create mode 100644 Bloxstrap/Helpers/JsonManager.cs rename Bloxstrap/Models/{SettingsFormat.cs => Settings.cs} (98%) delete mode 100644 Bloxstrap/SettingsManager.cs diff --git a/Bloxstrap/App.xaml.cs b/Bloxstrap/App.xaml.cs index 0c8aced..1915af3 100644 --- a/Bloxstrap/App.xaml.cs +++ b/Bloxstrap/App.xaml.cs @@ -37,8 +37,7 @@ namespace Bloxstrap public static string Version = Assembly.GetExecutingAssembly().GetName().Version!.ToString()[..^2]; - public static SettingsManager SettingsManager = new(); - public static SettingsFormat Settings = SettingsManager.Settings; + public static readonly JsonManager Settings = new(); public static readonly HttpClient HttpClient = new(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.All }); // shorthand @@ -52,7 +51,7 @@ namespace Bloxstrap public static void Terminate(int code = Bootstrapper.ERROR_SUCCESS) { - SettingsManager.Save(); + Settings.Save(); Environment.Exit(code); } @@ -90,7 +89,6 @@ namespace Bloxstrap if (registryKey is null) { IsFirstRun = true; - Settings = SettingsManager.Settings; BaseDirectory = Path.Combine(Directories.LocalAppData, ProjectName); if (!IsQuiet) @@ -111,14 +109,14 @@ namespace Bloxstrap Directories.Initialize(BaseDirectory); - SettingsManager.SaveLocation = Path.Combine(Directories.Base, "Settings.json"); + //Settings.FileLocation = Path.Combine(Directories.Base, "Settings.json"); // we shouldn't save settings on the first run until the first installation is finished, // just in case the user decides to cancel the install if (!IsFirstRun) { - Settings = SettingsManager.Settings; - SettingsManager.ShouldSave = true; + Settings.Load(); + Settings.ShouldSave = true; } #if !DEBUG @@ -163,8 +161,8 @@ namespace Bloxstrap if (!String.IsNullOrEmpty(commandLine)) { - DeployManager.Channel = Settings.Channel; - Settings.BootstrapperStyle.Show(new Bootstrapper(commandLine)); + DeployManager.Channel = Settings.Prop.Channel; + Settings.Prop.BootstrapperStyle.Show(new Bootstrapper(commandLine)); } Terminate(); diff --git a/Bloxstrap/Bootstrapper.cs b/Bloxstrap/Bootstrapper.cs index c320f46..c2f5795 100644 --- a/Bloxstrap/Bootstrapper.cs +++ b/Bloxstrap/Bootstrapper.cs @@ -86,7 +86,7 @@ namespace Bloxstrap public Bootstrapper(string? launchCommandLine = null) { LaunchCommandLine = launchCommandLine; - FreshInstall = String.IsNullOrEmpty(App.Settings.VersionGuid); + FreshInstall = String.IsNullOrEmpty(App.Settings.Prop.VersionGuid); } // this is called from BootstrapperStyleForm.SetupDialog() @@ -99,7 +99,7 @@ namespace Bloxstrap } #if !DEBUG - if (!App.IsFirstRun && App.Settings.CheckForUpdates) + if (!App.IsFirstRun && App.Settings.Prop.CheckForUpdates) await CheckForUpdates(); #endif @@ -107,14 +107,14 @@ namespace Bloxstrap // if bloxstrap is installing for the first time but is running, prompt to close roblox // if roblox needs updating but is running, ignore update for now - if (!Directory.Exists(VersionFolder) && CheckIfRunning(true) || App.Settings.VersionGuid != VersionGuid && !CheckIfRunning(false)) + if (!Directory.Exists(VersionFolder) && CheckIfRunning(true) || App.Settings.Prop.VersionGuid != VersionGuid && !CheckIfRunning(false)) await InstallLatestVersion(); + + if (App.IsFirstRun) + App.Settings.ShouldSave = true; await ApplyModifications(); - if (App.IsFirstRun) - App.SettingsManager.ShouldSave = true; - if (App.IsFirstRun || FreshInstall) Register(); @@ -122,7 +122,7 @@ namespace Bloxstrap await RbxFpsUnlocker.CheckInstall(); - App.SettingsManager.Save(); + App.Settings.Save(); if (App.IsFirstRun && App.IsNoLaunch) Dialog.ShowSuccess($"{App.ProjectName} has successfully installed"); @@ -167,7 +167,7 @@ namespace Bloxstrap foreach (string arg in App.LaunchArgs) startInfo.ArgumentList.Add(arg); - App.SettingsManager.Save(); + App.Settings.Save(); Process.Start(startInfo); @@ -178,7 +178,7 @@ namespace Bloxstrap { Dialog.Message = "Connecting to Roblox..."; - ClientVersion clientVersion = await DeployManager.GetLastDeploy(App.Settings.Channel); + ClientVersion clientVersion = await DeployManager.GetLastDeploy(App.Settings.Prop.Channel); VersionGuid = clientVersion.VersionGuid; VersionFolder = Path.Combine(Directories.Versions, VersionGuid); VersionPackageManifest = await PackageManifest.Get(VersionGuid); @@ -217,7 +217,7 @@ namespace Bloxstrap Dialog.Message = "Starting Roblox..."; - if (LaunchCommandLine == "--app" && App.Settings.UseDisableAppPatch) + if (LaunchCommandLine == "--app" && App.Settings.Prop.UseDisableAppPatch) { Utilities.OpenWebsite("https://www.roblox.com/games"); return; @@ -226,8 +226,8 @@ namespace Bloxstrap // launch time isn't really required for all launches, but it's usually just safest to do this LaunchCommandLine += " --launchtime=" + DateTimeOffset.Now.ToUnixTimeMilliseconds(); - if (App.Settings.Channel.ToLower() != DeployManager.DefaultChannel.ToLower()) - LaunchCommandLine += " -channel " + App.Settings.Channel.ToLower(); + if (App.Settings.Prop.Channel.ToLower() != DeployManager.DefaultChannel.ToLower()) + LaunchCommandLine += " -channel " + App.Settings.Prop.Channel.ToLower(); LaunchCommandLine += " -startEvent " + startEventName; @@ -247,7 +247,7 @@ namespace Bloxstrap return; } - if (App.Settings.RFUEnabled && Process.GetProcessesByName("rbxfpsunlocker").Length == 0) + if (App.Settings.Prop.RFUEnabled && Process.GetProcessesByName("rbxfpsunlocker").Length == 0) { ProcessStartInfo startInfo = new() { @@ -257,11 +257,11 @@ namespace Bloxstrap rbxFpsUnlocker = Process.Start(startInfo); - if (App.Settings.RFUAutoclose) + if (App.Settings.Prop.RFUAutoclose) shouldWait = true; } - if (App.Settings.MultiInstanceLaunching) + if (App.Settings.Prop.MultiInstanceLaunching) { // this might be a bit problematic since this mutex will be released when the first launched instance is closed... singletonMutex = new Mutex(true, "ROBLOX_singletonMutex"); @@ -271,7 +271,7 @@ namespace Bloxstrap // event fired, wait for 3 seconds then close await Task.Delay(3000); - if (App.Settings.UseDiscordRichPresence) + if (App.Settings.Prop.UseDiscordRichPresence) { richPresence = new DiscordRichPresence(); richPresence.MonitorGameActivity(); @@ -287,7 +287,7 @@ namespace Bloxstrap richPresence?.Dispose(); - if (App.Settings.RFUAutoclose && rbxFpsUnlocker is not null) + if (App.Settings.Prop.RFUAutoclose && rbxFpsUnlocker is not null) rbxFpsUnlocker.Kill(); } @@ -400,7 +400,7 @@ namespace Bloxstrap .WriteToFile(newMenuShortcut); } - if (App.Settings.CreateDesktopIcon && !File.Exists(Path.Combine(Directories.Desktop, "Play Roblox.lnk"))) + if (App.Settings.Prop.CreateDesktopIcon && !File.Exists(Path.Combine(Directories.Desktop, "Play Roblox.lnk"))) { ShellLink.Shortcut.CreateShortcut(Directories.Application, "", Directories.Application, 0) .WriteToFile(Path.Combine(Directories.Desktop, "Play Roblox.lnk")); @@ -413,7 +413,7 @@ namespace Bloxstrap Dialog.Message = $"Uninstalling {App.ProjectName}..."; - App.SettingsManager.ShouldSave = false; + App.Settings.ShouldSave = false; // check if stock bootstrapper is still installed RegistryKey? bootstrapperKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall\roblox-player"); @@ -540,9 +540,9 @@ namespace Bloxstrap File.Delete(filename); } - string oldVersionFolder = Path.Combine(Directories.Versions, App.Settings.VersionGuid); + string oldVersionFolder = Path.Combine(Directories.Versions, App.Settings.Prop.VersionGuid); - if (VersionGuid != App.Settings.VersionGuid && Directory.Exists(oldVersionFolder)) + if (VersionGuid != App.Settings.Prop.VersionGuid && Directory.Exists(oldVersionFolder)) { // and also to delete our old version folder Directory.Delete(oldVersionFolder, true); @@ -551,7 +551,7 @@ namespace Bloxstrap Dialog.CancelEnabled = false; - App.Settings.VersionGuid = VersionGuid; + App.Settings.Prop.VersionGuid = VersionGuid; } private async Task ApplyModifications() @@ -567,10 +567,10 @@ namespace Bloxstrap if (!Directory.Exists(modFolder)) Directory.CreateDirectory(modFolder); - await CheckModPreset(App.Settings.UseOldDeathSound, @"content\sounds\ouch.ogg", "OldDeath.ogg"); - await CheckModPreset(App.Settings.UseOldMouseCursor, @"content\textures\Cursors\KeyboardMouse\ArrowCursor.png", "OldCursor.png"); - await CheckModPreset(App.Settings.UseOldMouseCursor, @"content\textures\Cursors\KeyboardMouse\ArrowFarCursor.png", "OldFarCursor.png"); - await CheckModPreset(App.Settings.UseDisableAppPatch, @"ExtraContent\places\Mobile.rbxl", ""); + await CheckModPreset(App.Settings.Prop.UseOldDeathSound, @"content\sounds\ouch.ogg", "OldDeath.ogg"); + await CheckModPreset(App.Settings.Prop.UseOldMouseCursor, @"content\textures\Cursors\KeyboardMouse\ArrowCursor.png", "OldCursor.png"); + await CheckModPreset(App.Settings.Prop.UseOldMouseCursor, @"content\textures\Cursors\KeyboardMouse\ArrowFarCursor.png", "OldFarCursor.png"); + await CheckModPreset(App.Settings.Prop.UseDisableAppPatch, @"ExtraContent\places\Mobile.rbxl", ""); await ReShade.CheckModifications(); diff --git a/Bloxstrap/Dialogs/BootstrapperDialogForm.cs b/Bloxstrap/Dialogs/BootstrapperDialogForm.cs index facbdba..3139b30 100644 --- a/Bloxstrap/Dialogs/BootstrapperDialogForm.cs +++ b/Bloxstrap/Dialogs/BootstrapperDialogForm.cs @@ -82,7 +82,7 @@ namespace Bloxstrap.Dialogs public void SetupDialog() { this.Text = App.ProjectName; - this.Icon = App.Settings.BootstrapperIcon.GetIcon(); + this.Icon = App.Settings.Prop.BootstrapperIcon.GetIcon(); if (Bootstrapper is null) { diff --git a/Bloxstrap/Dialogs/LegacyDialog2011.cs b/Bloxstrap/Dialogs/LegacyDialog2011.cs index e9094dd..83ad947 100644 --- a/Bloxstrap/Dialogs/LegacyDialog2011.cs +++ b/Bloxstrap/Dialogs/LegacyDialog2011.cs @@ -40,7 +40,7 @@ namespace Bloxstrap.Dialogs Bootstrapper = bootstrapper; // have to convert icon -> bitmap since winforms scaling is poop - this.IconBox.BackgroundImage = App.Settings.BootstrapperIcon.GetIcon().ToBitmap(); + this.IconBox.BackgroundImage = App.Settings.Prop.BootstrapperIcon.GetIcon().ToBitmap(); ScaleWindow(); SetupDialog(); diff --git a/Bloxstrap/Dialogs/ProgressDialog.cs b/Bloxstrap/Dialogs/ProgressDialog.cs index d64c283..b76bdee 100644 --- a/Bloxstrap/Dialogs/ProgressDialog.cs +++ b/Bloxstrap/Dialogs/ProgressDialog.cs @@ -38,7 +38,7 @@ namespace Bloxstrap.Dialogs { InitializeComponent(); - if (App.Settings.Theme.GetFinal() == Theme.Dark) + if (App.Settings.Prop.Theme.GetFinal() == Theme.Dark) { this.labelMessage.ForeColor = SystemColors.Window; this.buttonCancel.Image = Properties.Resources.DarkCancelButton; @@ -48,14 +48,14 @@ namespace Bloxstrap.Dialogs Bootstrapper = bootstrapper; - this.IconBox.BackgroundImage = App.Settings.BootstrapperIcon.GetBitmap(); + this.IconBox.BackgroundImage = App.Settings.Prop.BootstrapperIcon.GetBitmap(); SetupDialog(); } private void ButtonCancel_MouseEnter(object sender, EventArgs e) { - if (App.Settings.Theme.GetFinal() == Theme.Dark) + if (App.Settings.Prop.Theme.GetFinal() == Theme.Dark) { this.buttonCancel.Image = Properties.Resources.DarkCancelButtonHover; } @@ -67,7 +67,7 @@ namespace Bloxstrap.Dialogs private void ButtonCancel_MouseLeave(object sender, EventArgs e) { - if (App.Settings.Theme.GetFinal() == Theme.Dark) + if (App.Settings.Prop.Theme.GetFinal() == Theme.Dark) { this.buttonCancel.Image = Properties.Resources.DarkCancelButton; } diff --git a/Bloxstrap/Dialogs/VistaDialog.cs b/Bloxstrap/Dialogs/VistaDialog.cs index 099d5a0..ff2cc01 100644 --- a/Bloxstrap/Dialogs/VistaDialog.cs +++ b/Bloxstrap/Dialogs/VistaDialog.cs @@ -68,7 +68,7 @@ namespace Bloxstrap.Dialogs Dialog = new TaskDialogPage() { - Icon = new TaskDialogIcon(App.Settings.BootstrapperIcon.GetIcon()), + Icon = new TaskDialogIcon(App.Settings.Prop.BootstrapperIcon.GetIcon()), Caption = App.ProjectName, Buttons = { TaskDialogButton.Cancel }, diff --git a/Bloxstrap/Enums/BootstrapperStyle.cs b/Bloxstrap/Enums/BootstrapperStyle.cs index 7fa546e..32f1c5f 100644 --- a/Bloxstrap/Enums/BootstrapperStyle.cs +++ b/Bloxstrap/Enums/BootstrapperStyle.cs @@ -16,27 +16,14 @@ namespace Bloxstrap.Enums { public static void Show(this BootstrapperStyle bootstrapperStyle, Bootstrapper? bootstrapper = null) { - Form dialog; - - switch (bootstrapperStyle) + Form dialog = bootstrapperStyle switch { - case BootstrapperStyle.VistaDialog: - dialog = new VistaDialog(bootstrapper); - break; - - case BootstrapperStyle.LegacyDialog2009: - dialog = new LegacyDialog2009(bootstrapper); - break; - - case BootstrapperStyle.LegacyDialog2011: - dialog = new LegacyDialog2011(bootstrapper); - break; - - case BootstrapperStyle.ProgressDialog: - default: - dialog = new ProgressDialog(bootstrapper); - break; - } + BootstrapperStyle.VistaDialog => new VistaDialog(bootstrapper), + BootstrapperStyle.LegacyDialog2009 => new LegacyDialog2009(bootstrapper), + BootstrapperStyle.LegacyDialog2011 => new LegacyDialog2011(bootstrapper), + BootstrapperStyle.ProgressDialog => new ProgressDialog(bootstrapper), + _ => new ProgressDialog(bootstrapper) + }; if (bootstrapper is null) { diff --git a/Bloxstrap/Helpers/Integrations/DiscordRichPresence.cs b/Bloxstrap/Helpers/Integrations/DiscordRichPresence.cs index fbd3a42..52c50f9 100644 --- a/Bloxstrap/Helpers/Integrations/DiscordRichPresence.cs +++ b/Bloxstrap/Helpers/Integrations/DiscordRichPresence.cs @@ -174,7 +174,7 @@ namespace Bloxstrap.Helpers.Integrations } }; - if (!App.Settings.HideRPCButtons) + if (!App.Settings.Prop.HideRPCButtons) { buttons.Insert(0, new DiscordRPC.Button() { diff --git a/Bloxstrap/Helpers/Integrations/RbxFpsUnlocker.cs b/Bloxstrap/Helpers/Integrations/RbxFpsUnlocker.cs index f162a3e..40610ad 100644 --- a/Bloxstrap/Helpers/Integrations/RbxFpsUnlocker.cs +++ b/Bloxstrap/Helpers/Integrations/RbxFpsUnlocker.cs @@ -60,7 +60,7 @@ namespace Bloxstrap.Helpers.Integrations string fileLocation = Path.Combine(folderLocation, "rbxfpsunlocker.exe"); string settingsLocation = Path.Combine(folderLocation, "settings"); - if (!App.Settings.RFUEnabled) + if (!App.Settings.Prop.RFUEnabled) { if (Directory.Exists(folderLocation)) { @@ -83,7 +83,7 @@ namespace Bloxstrap.Helpers.Integrations if (File.Exists(fileLocation)) { // no new release published, return - if (App.Settings.RFUVersion == releaseInfo.TagName) + if (App.Settings.Prop.RFUVersion == releaseInfo.TagName) return; CheckIfRunning(); @@ -104,7 +104,7 @@ namespace Bloxstrap.Helpers.Integrations if (!File.Exists(settingsLocation)) await File.WriteAllTextAsync(settingsLocation, Settings); - App.Settings.RFUVersion = releaseInfo.TagName; + App.Settings.Prop.RFUVersion = releaseInfo.TagName; } } } diff --git a/Bloxstrap/Helpers/Integrations/ReShade.cs b/Bloxstrap/Helpers/Integrations/ReShade.cs index 162a1e4..f7da18a 100644 --- a/Bloxstrap/Helpers/Integrations/ReShade.cs +++ b/Bloxstrap/Helpers/Integrations/ReShade.cs @@ -129,7 +129,7 @@ namespace Bloxstrap.Helpers.Integrations // config synchronization will be done whenever roblox updates or whenever we launch roblox string modFolderConfigPath = ConfigLocation; - string versionFolderConfigPath = Path.Combine(Directories.Versions, App.Settings.VersionGuid, "ReShade.ini"); + string versionFolderConfigPath = Path.Combine(Directories.Versions, App.Settings.Prop.VersionGuid, "ReShade.ini"); // we shouldn't be here if the mod config doesn't already exist if (!File.Exists(modFolderConfigPath)) @@ -323,13 +323,13 @@ namespace Bloxstrap.Helpers.Integrations string injectorLocation = Path.Combine(Directories.Modifications, "dxgi.dll"); - if (!App.Settings.UseReShadeExtraviPresets) + if (!App.Settings.Prop.UseReShadeExtraviPresets) { UninstallExtraviPresets(); - App.Settings.ExtraviPresetsVersion = ""; + App.Settings.Prop.ExtraviPresetsVersion = ""; } - if (!App.Settings.UseReShade) + if (!App.Settings.Prop.UseReShade) { Debug.WriteLine("[ReShade] Uninstalling ReShade..."); @@ -337,7 +337,7 @@ namespace Bloxstrap.Helpers.Integrations File.Delete(injectorLocation); File.Delete(ConfigLocation); - App.Settings.ReShadeConfigVersion = ""; + App.Settings.Prop.ReShadeConfigVersion = ""; //DeleteShaders("Stock"); if (Directory.Exists(BaseDirectory)) @@ -373,7 +373,7 @@ namespace Bloxstrap.Helpers.Integrations // check if we should download a fresh copy of the config // extravi may need to update the config ota, in which case we'll redownload it - if (!File.Exists(ConfigLocation) || versionManifest is not null && App.Settings.ReShadeConfigVersion != versionManifest.ConfigFile) + if (!File.Exists(ConfigLocation) || versionManifest is not null && App.Settings.Prop.ReShadeConfigVersion != versionManifest.ConfigFile) shouldFetchConfig = true; if (shouldFetchReShade) @@ -393,15 +393,15 @@ namespace Bloxstrap.Helpers.Integrations await DownloadConfig(); if (versionManifest is not null) - App.Settings.ReShadeConfigVersion = versionManifest.ConfigFile; + App.Settings.Prop.ReShadeConfigVersion = versionManifest.ConfigFile; } await DownloadShaders("Stock"); - if (App.Settings.UseReShadeExtraviPresets && App.Settings.ExtraviPresetsVersion != versionManifest!.Presets) + if (App.Settings.Prop.UseReShadeExtraviPresets && App.Settings.Prop.ExtraviPresetsVersion != versionManifest!.Presets) { await InstallExtraviPresets(); - App.Settings.ExtraviPresetsVersion = versionManifest.Presets; + App.Settings.Prop.ExtraviPresetsVersion = versionManifest.Presets; } SynchronizeConfigFile(); diff --git a/Bloxstrap/Helpers/JsonManager.cs b/Bloxstrap/Helpers/JsonManager.cs new file mode 100644 index 0000000..d17f8ed --- /dev/null +++ b/Bloxstrap/Helpers/JsonManager.cs @@ -0,0 +1,65 @@ +using Bloxstrap.Models; +using Bloxstrap.Properties; +using Newtonsoft.Json.Linq; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Text.Json; +using System.Threading; + +namespace Bloxstrap.Helpers +{ + public class JsonManager where T : new() + { + public T Prop { get; set; } = new T(); + public bool ShouldSave { get; set; } = true; + public string FileLocation => Path.Combine(Directories.Base, $"{typeof(T).Name}.json"); + //public string? FileLocation { get; set; } = null; + + public void Load() + { + //if (String.IsNullOrEmpty(FileLocation)) + // throw new ArgumentNullException("No FileLocation has been set"); + + Debug.WriteLine($"[JsonManager<{typeof(T).Name}>] Loading JSON from {FileLocation}..."); + + try + { + T? settings = JsonSerializer.Deserialize(File.ReadAllText(FileLocation)); + Prop = settings ?? throw new ArgumentNullException("Deserialization returned null"); + Debug.WriteLine($"[JsonManager<{typeof(T).Name}>] JSON loaded successfully!"); + + } + catch (Exception ex) + { + Debug.WriteLine($"[JsonManager<{typeof(T).Name}>] Failed to load JSON! ({ex.Message})"); + } + } + + public void Save() + { + Debug.WriteLine($"[JsonManager<{typeof(T).Name}>] Attempting to save JSON to {FileLocation}..."); + + //if (!ShouldSave || String.IsNullOrEmpty(FileLocation)) + //{ + // Debug.WriteLine($"[JsonManager<{typeof(T).Name}>] Aborted save (ShouldSave set to false or FileLocation not set)"); + // return; + //} + + if (!ShouldSave) + { + Debug.WriteLine($"[JsonManager<{typeof(T).Name}>] Aborted save (ShouldSave set to false)"); + return; + } + + string json = JsonSerializer.Serialize(Prop, new JsonSerializerOptions { WriteIndented = true }); + File.WriteAllText(FileLocation, json); + + Debug.WriteLine($"[JsonManager<{typeof(T).Name}>] JSON saved!"); + } + } +} diff --git a/Bloxstrap/Helpers/Protocol.cs b/Bloxstrap/Helpers/Protocol.cs index b50daff..aada861 100644 --- a/Bloxstrap/Helpers/Protocol.cs +++ b/Bloxstrap/Helpers/Protocol.cs @@ -51,17 +51,17 @@ namespace Bloxstrap.Helpers if (key == "channel") { - if (val.ToLower() != App.Settings.Channel.ToLower()) + if (val.ToLower() != App.Settings.Prop.Channel.ToLower()) { - MessageBoxResult result = !App.Settings.PromptChannelChange ? MessageBoxResult.Yes : App.ShowMessageBox( - $"{App.ProjectName} was launched with the Roblox build channel set to {val}, however your current preferred channel is {App.Settings.Channel}.\n\n" + - $"Would you like to switch channels from {App.Settings.Channel} to {val}?", + MessageBoxResult result = !App.Settings.Prop.PromptChannelChange ? MessageBoxResult.Yes : App.ShowMessageBox( + $"{App.ProjectName} was launched with the Roblox build channel set to {val}, however your current preferred channel is {App.Settings.Prop.Channel}.\n\n" + + $"Would you like to switch channels from {App.Settings.Prop.Channel} to {val}?", MessageBoxImage.Question, MessageBoxButton.YesNo ); if (result == MessageBoxResult.Yes) - App.Settings.Channel = val; + App.Settings.Prop.Channel = val; } // we'll set the arg when launching diff --git a/Bloxstrap/Models/SettingsFormat.cs b/Bloxstrap/Models/Settings.cs similarity index 98% rename from Bloxstrap/Models/SettingsFormat.cs rename to Bloxstrap/Models/Settings.cs index 2f8db32..8ad7176 100644 --- a/Bloxstrap/Models/SettingsFormat.cs +++ b/Bloxstrap/Models/Settings.cs @@ -3,7 +3,7 @@ using Bloxstrap.Helpers; namespace Bloxstrap.Models { - public class SettingsFormat + public class Settings { // could these be moved to a separate file (something like State.json)? // the only problem is i havent yet figured out a way to boil down the settings handler to reduce boilerplate diff --git a/Bloxstrap/Properties/launchSettings.json b/Bloxstrap/Properties/launchSettings.json index 2e70619..95e6857 100644 --- a/Bloxstrap/Properties/launchSettings.json +++ b/Bloxstrap/Properties/launchSettings.json @@ -3,10 +3,18 @@ "Bloxstrap (Launch)": { "commandName": "Project" }, - "Bloxstrap (Quiet)": { + "Bloxstrap (Quiet Launch)": { "commandName": "Project", "commandLineArgs": "-quiet" }, + "Bloxstrap (Uninstall)": { + "commandName": "Project", + "commandLineArgs": "-uninstall" + }, + "Bloxstrap (Quiet Uninstall)": { + "commandName": "Project", + "commandLineArgs": "-uninstall -quiet" + }, "Bloxstrap (Menu)": { "commandName": "Project", "commandLineArgs": "-preferences" diff --git a/Bloxstrap/SettingsManager.cs b/Bloxstrap/SettingsManager.cs deleted file mode 100644 index f66b227..0000000 --- a/Bloxstrap/SettingsManager.cs +++ /dev/null @@ -1,83 +0,0 @@ -using System; -using System.Diagnostics; -using System.IO; -using System.Text.Json; -using System.Threading; - -using Bloxstrap.Models; - -namespace Bloxstrap -{ - public class SettingsManager - { - public SettingsFormat Settings = new(); - public bool ShouldSave = false; - private bool IsSaving = false; - - private string? _saveLocation; - public string? SaveLocation - { - get => _saveLocation; - - set - { - if (!String.IsNullOrEmpty(_saveLocation)) - return; - - _saveLocation = value; - - string settingsJson = ""; - - if (File.Exists(_saveLocation)) - settingsJson = File.ReadAllText(_saveLocation); - - Debug.WriteLine(settingsJson); - - try - { - var settings = JsonSerializer.Deserialize(settingsJson); - - if (settings is null) - throw new Exception("Deserialization returned null"); - - Settings = settings; - } - catch (Exception ex) - { - Debug.WriteLine($"Failed to fetch settings! Reverting to defaults... ({ex.Message})"); - // Settings = new(); - } - } - } - - public void Save() - { - if (IsSaving) - { - // sometimes Save() is called at the same time from both Main() and Exit(), - // so this is here to avoid the program exiting before saving - - Thread.Sleep(1000); - return; - } - - IsSaving = true; - - Debug.WriteLine("Attempting to save..."); - - string SettingsJson = JsonSerializer.Serialize(Settings, new JsonSerializerOptions { WriteIndented = true }); - Debug.WriteLine(SettingsJson); - - if (!ShouldSave || SaveLocation is null) - { - Debug.WriteLine("ShouldSave set to false, not saving..."); - return; - } - - // save settings - File.WriteAllText(SaveLocation, SettingsJson); - - IsSaving = false; - } - } -} diff --git a/Bloxstrap/ViewModels/BootstrapperViewModel.cs b/Bloxstrap/ViewModels/BootstrapperViewModel.cs index f03bc10..1420644 100644 --- a/Bloxstrap/ViewModels/BootstrapperViewModel.cs +++ b/Bloxstrap/ViewModels/BootstrapperViewModel.cs @@ -20,7 +20,7 @@ namespace Bloxstrap.ViewModels public ICommand PreviewBootstrapperCommand => new RelayCommand(PreviewBootstrapper); - private void PreviewBootstrapper() => App.Settings.BootstrapperStyle.Show(); + private void PreviewBootstrapper() => App.Settings.Prop.BootstrapperStyle.Show(); public BootstrapperViewModel(Page page) { @@ -29,20 +29,20 @@ namespace Bloxstrap.ViewModels public bool UpdateCheckingEnabled { - get => App.Settings.CheckForUpdates; - set => App.Settings.CheckForUpdates = value; + get => App.Settings.Prop.CheckForUpdates; + set => App.Settings.Prop.CheckForUpdates = value; } public bool ChannelChangePromptingEnabled { - get => App.Settings.PromptChannelChange; - set => App.Settings.PromptChannelChange = value; + get => App.Settings.Prop.PromptChannelChange; + set => App.Settings.Prop.PromptChannelChange = value; } public bool MultiInstanceLaunchingEnabled { - get => App.Settings.MultiInstanceLaunching; - set => App.Settings.MultiInstanceLaunching = value; + get => App.Settings.Prop.MultiInstanceLaunching; + set => App.Settings.Prop.MultiInstanceLaunching = value; } public IReadOnlyDictionary Themes { get; set; } = new Dictionary() @@ -54,10 +54,10 @@ namespace Bloxstrap.ViewModels public string Theme { - get => Themes.FirstOrDefault(x => x.Value == App.Settings.Theme).Key; + get => Themes.FirstOrDefault(x => x.Value == App.Settings.Prop.Theme).Key; set { - App.Settings.Theme = Themes[value]; + App.Settings.Prop.Theme = Themes[value]; ((MainWindow)Window.GetWindow(_page)!).SetTheme(); } } @@ -72,8 +72,8 @@ namespace Bloxstrap.ViewModels public string Dialog { - get => Dialogs.FirstOrDefault(x => x.Value == App.Settings.BootstrapperStyle).Key; - set => App.Settings.BootstrapperStyle = Dialogs[value]; + get => Dialogs.FirstOrDefault(x => x.Value == App.Settings.Prop.BootstrapperStyle).Key; + set => App.Settings.Prop.BootstrapperStyle = Dialogs[value]; } public IReadOnlyDictionary Icons { get; set; } = new Dictionary() @@ -90,8 +90,8 @@ namespace Bloxstrap.ViewModels public string Icon { - get => Icons.FirstOrDefault(x => x.Value == App.Settings.BootstrapperIcon).Key; - set => App.Settings.BootstrapperIcon = Icons[value]; + get => Icons.FirstOrDefault(x => x.Value == App.Settings.Prop.BootstrapperIcon).Key; + set => App.Settings.Prop.BootstrapperIcon = Icons[value]; } } } diff --git a/Bloxstrap/ViewModels/InstallationViewModel.cs b/Bloxstrap/ViewModels/InstallationViewModel.cs index 5fab280..46ccd79 100644 --- a/Bloxstrap/ViewModels/InstallationViewModel.cs +++ b/Bloxstrap/ViewModels/InstallationViewModel.cs @@ -19,8 +19,8 @@ namespace Bloxstrap.ViewModels public event PropertyChangedEventHandler? PropertyChanged; public void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); - private IEnumerable _channels = DeployManager.ChannelsAbstracted.Contains(App.Settings.Channel) ? DeployManager.ChannelsAbstracted : DeployManager.ChannelsAll; - private bool _showAllChannels = !DeployManager.ChannelsAbstracted.Contains(App.Settings.Channel); + private IEnumerable _channels = DeployManager.ChannelsAbstracted.Contains(App.Settings.Prop.Channel) ? DeployManager.ChannelsAbstracted : DeployManager.ChannelsAll; + private bool _showAllChannels = !DeployManager.ChannelsAbstracted.Contains(App.Settings.Prop.Channel); public ICommand BrowseInstallLocationCommand => new RelayCommand(BrowseInstallLocation); @@ -28,7 +28,7 @@ namespace Bloxstrap.ViewModels public InstallationViewModel() { - Task.Run(() => LoadChannelDeployInfo(App.Settings.Channel)); + Task.Run(() => LoadChannelDeployInfo(App.Settings.Prop.Channel)); } private async Task LoadChannelDeployInfo(string channel) @@ -62,8 +62,8 @@ namespace Bloxstrap.ViewModels public bool CreateDesktopIcon { - get => App.Settings.CreateDesktopIcon; - set => App.Settings.CreateDesktopIcon = value; + get => App.Settings.Prop.CreateDesktopIcon; + set => App.Settings.Prop.CreateDesktopIcon = value; } public IEnumerable Channels @@ -74,12 +74,12 @@ namespace Bloxstrap.ViewModels public string Channel { - get => App.Settings.Channel; + get => App.Settings.Prop.Channel; set { //Task.Run(() => GetChannelInfo(value)); Task.Run(() => LoadChannelDeployInfo(value)); - App.Settings.Channel = value; + App.Settings.Prop.Channel = value; } } diff --git a/Bloxstrap/ViewModels/IntegrationsViewModel.cs b/Bloxstrap/ViewModels/IntegrationsViewModel.cs index 6f60921..c7013d5 100644 --- a/Bloxstrap/ViewModels/IntegrationsViewModel.cs +++ b/Bloxstrap/ViewModels/IntegrationsViewModel.cs @@ -22,7 +22,7 @@ namespace Bloxstrap.ViewModels public ICommand OpenReShadeFolderCommand => new RelayCommand(OpenReShadeFolder); public ICommand ShowReShadeHelpCommand => new RelayCommand(ShowReShadeHelp); - public bool CanOpenReShadeFolder => App.Settings.UseReShade; + public bool CanOpenReShadeFolder => App.Settings.Prop.UseReShade; public IntegrationsViewModel(Page page) { @@ -41,10 +41,10 @@ namespace Bloxstrap.ViewModels public bool DiscordActivityEnabled { - get => App.Settings.UseDiscordRichPresence; + get => App.Settings.Prop.UseDiscordRichPresence; set { - App.Settings.UseDiscordRichPresence = value; + App.Settings.Prop.UseDiscordRichPresence = value; if (!value) { @@ -56,16 +56,16 @@ namespace Bloxstrap.ViewModels public bool DiscordActivityJoinEnabled { - get => !App.Settings.HideRPCButtons; - set => App.Settings.HideRPCButtons = !value; + get => !App.Settings.Prop.HideRPCButtons; + set => App.Settings.Prop.HideRPCButtons = !value; } public bool ReShadeEnabled { - get => App.Settings.UseReShade; + get => App.Settings.Prop.UseReShade; set { - App.Settings.UseReShade = value; + App.Settings.Prop.UseReShade = value; ReShadePresetsEnabled = value; OnPropertyChanged(nameof(ReShadePresetsEnabled)); } @@ -73,16 +73,16 @@ namespace Bloxstrap.ViewModels public bool ReShadePresetsEnabled { - get => App.Settings.UseReShadeExtraviPresets; - set => App.Settings.UseReShadeExtraviPresets = value; + get => App.Settings.Prop.UseReShadeExtraviPresets; + set => App.Settings.Prop.UseReShadeExtraviPresets = value; } public bool RbxFpsUnlockerEnabled { - get => App.Settings.RFUEnabled; + get => App.Settings.Prop.RFUEnabled; set { - App.Settings.RFUEnabled = value; + App.Settings.Prop.RFUEnabled = value; RbxFpsUnlockerAutocloseEnabled = value; OnPropertyChanged(nameof(RbxFpsUnlockerAutocloseEnabled)); } @@ -90,8 +90,8 @@ namespace Bloxstrap.ViewModels public bool RbxFpsUnlockerAutocloseEnabled { - get => App.Settings.RFUAutoclose; - set => App.Settings.RFUAutoclose = value; + get => App.Settings.Prop.RFUAutoclose; + set => App.Settings.Prop.RFUAutoclose = value; } } } diff --git a/Bloxstrap/ViewModels/MainWindowViewModel.cs b/Bloxstrap/ViewModels/MainWindowViewModel.cs index 508082d..6e3be81 100644 --- a/Bloxstrap/ViewModels/MainWindowViewModel.cs +++ b/Bloxstrap/ViewModels/MainWindowViewModel.cs @@ -64,13 +64,13 @@ namespace Bloxstrap.ViewModels if (!App.IsFirstRun) { - App.SettingsManager.ShouldSave = true; + App.Settings.ShouldSave = true; if (App.BaseDirectory != _originalBaseDirectory) { App.ShowMessageBox($"{App.ProjectName} will install to the new location you've set the next time it runs.", MessageBoxImage.Information); - App.Settings.VersionGuid = ""; + App.Settings.Prop.VersionGuid = ""; using (RegistryKey registryKey = Registry.CurrentUser.CreateSubKey($@"Software\{App.ProjectName}")) { @@ -82,9 +82,9 @@ namespace Bloxstrap.ViewModels // preserve settings // we don't need to copy the bootstrapper over since the install process will do that automatically - App.SettingsManager.Save(); + App.Settings.Save(); - File.Copy(Path.Combine(App.BaseDirectory, "Settings.json"), Path.Combine(App.BaseDirectory, "Settings.json")); + //File.Copy(Path.Combine(App.BaseDirectory, "Settings.json"), Path.Combine(App.BaseDirectory, "Settings.json")); } } diff --git a/Bloxstrap/ViewModels/ModsViewModel.cs b/Bloxstrap/ViewModels/ModsViewModel.cs index 99ce853..ee2c2b5 100644 --- a/Bloxstrap/ViewModels/ModsViewModel.cs +++ b/Bloxstrap/ViewModels/ModsViewModel.cs @@ -18,20 +18,20 @@ namespace Bloxstrap.ViewModels public bool OldDeathSoundEnabled { - get => App.Settings.UseOldDeathSound; - set => App.Settings.UseOldDeathSound = value; + get => App.Settings.Prop.UseOldDeathSound; + set => App.Settings.Prop.UseOldDeathSound = value; } public bool OldMouseCursorEnabled { - get => App.Settings.UseOldMouseCursor; - set => App.Settings.UseOldMouseCursor = value; + get => App.Settings.Prop.UseOldMouseCursor; + set => App.Settings.Prop.UseOldMouseCursor = value; } public bool DisableAppPatchEnabled { - get => App.Settings.UseDisableAppPatch; - set => App.Settings.UseDisableAppPatch = value; + get => App.Settings.Prop.UseDisableAppPatch; + set => App.Settings.Prop.UseDisableAppPatch = value; } } } diff --git a/Bloxstrap/Views/MainWindow.xaml.cs b/Bloxstrap/Views/MainWindow.xaml.cs index 69c4b87..aea3d2a 100644 --- a/Bloxstrap/Views/MainWindow.xaml.cs +++ b/Bloxstrap/Views/MainWindow.xaml.cs @@ -27,7 +27,7 @@ namespace Bloxstrap.Views { var theme = ThemeType.Light; - if (App.Settings.Theme.GetFinal() == Enums.Theme.Dark) + if (App.Settings.Prop.Theme.GetFinal() == Enums.Theme.Dark) theme = ThemeType.Dark; _themeService.SetTheme(theme);