From 987e9c5e3c16b0ab0dc26c00bfa358e3dff5f1aa Mon Sep 17 00:00:00 2001 From: cooolbros Date: Wed, 3 Jan 2024 16:10:37 +1100 Subject: [PATCH] Add Language setting --- src/HUDMerger/App.xaml.cs | 84 ++++++++++--------- src/HUDMerger/Properties/Settings.Designer.cs | 12 +++ src/HUDMerger/Properties/Settings.settings | 3 + src/HUDMerger/SettingsWindow.xaml | 4 +- .../ViewModels/SettingsWindowViewModel.cs | 20 ++++- src/HUDMerger/Views/SettingsWindowView.xaml | 6 +- 6 files changed, 85 insertions(+), 44 deletions(-) diff --git a/src/HUDMerger/App.xaml.cs b/src/HUDMerger/App.xaml.cs index 47289bb..9f62b94 100644 --- a/src/HUDMerger/App.xaml.cs +++ b/src/HUDMerger/App.xaml.cs @@ -23,13 +23,18 @@ public partial class App : Application public Lazy Settings = new(() => { string? teamFortress2Folder = null; - string? language = null; - if (!string.IsNullOrEmpty(HUDMerger.Properties.Settings.Default.TeamFortress2Folder)) { teamFortress2Folder = HUDMerger.Properties.Settings.Default.TeamFortress2Folder; } - else + + string? language = null; + if (!string.IsNullOrEmpty(HUDMerger.Properties.Settings.Default.Language)) + { + language = HUDMerger.Properties.Settings.Default.Language; + } + + if (teamFortress2Folder == null || language == null) { try { @@ -40,55 +45,56 @@ public partial class App : Application : null ) ?? throw new Exception(); - try + if (teamFortress2Folder == null) { - KeyValues libraryFolder = VDFSerializer - .Deserialize(File.ReadAllText(Path.Join(installPath, "steamapps\\libraryfolders.vdf"))) - .Header("libraryfolders") - .First((keyValue) => - keyValue.Value is KeyValues indexValues && indexValues.Any((kv) => kv.Key.Equals("apps") && kv.Value is KeyValues appsKeyValues && appsKeyValues.Any((kv) => kv.Key == "440")) - ) - .Value; - - string libraryFolderPath = libraryFolder.First((kv) => kv.Key.Equals("path", StringComparison.OrdinalIgnoreCase) && kv.Value is string str).Value; - - string libraryFolderTeamFortress2Path = $"{libraryFolderPath.Replace("\\\\", "\\")}\\steamapps\\common\\Team Fortress 2"; - if (Directory.Exists(libraryFolderTeamFortress2Path)) + try + { + KeyValues libraryFolder = VDFSerializer + .Deserialize(File.ReadAllText(Path.Join(installPath, "steamapps\\libraryfolders.vdf"))) + .Header("libraryfolders") + .First((keyValue) => + keyValue.Value is KeyValues indexValues && indexValues.Any((kv) => kv.Key.Equals("apps") && kv.Value is KeyValues appsKeyValues && appsKeyValues.Any((kv) => kv.Key == "440")) + ) + .Value; + + string libraryFolderPath = libraryFolder.First((kv) => kv.Key.Equals("path", StringComparison.OrdinalIgnoreCase) && kv.Value is string str).Value; + + string libraryFolderTeamFortress2Path = $"{libraryFolderPath.Replace("\\\\", "\\")}\\steamapps\\common\\Team Fortress 2"; + if (Directory.Exists(libraryFolderTeamFortress2Path)) + { + teamFortress2Folder = libraryFolderTeamFortress2Path; + } + } + catch (Exception) { - teamFortress2Folder = libraryFolderTeamFortress2Path; } - } - catch (Exception) - { - teamFortress2Folder = null; } - try + if (language == null) { - string appManifestPath = Path.Join(installPath, "steamapps\\appmanifest_440.acf"); + try + { + string appManifestPath = Path.Join(installPath, "steamapps\\appmanifest_440.acf"); - KeyValues userConfig = VDFSerializer - .Deserialize(File.ReadAllText(Path.Join(installPath, "steamapps\\appmanifest_440.acf"))) - .Header("AppState") - .First((kv) => kv.Key.Equals("UserConfig", StringComparison.OrdinalIgnoreCase) && kv.Value is KeyValues) - .Value; + KeyValues userConfig = VDFSerializer + .Deserialize(File.ReadAllText(Path.Join(installPath, "steamapps\\appmanifest_440.acf"))) + .Header("AppState") + .First((kv) => kv.Key.Equals("UserConfig", StringComparison.OrdinalIgnoreCase) && kv.Value is KeyValues) + .Value; - string userConfigLanguage = userConfig - .First((kv) => kv.Key.Equals("language", StringComparison.OrdinalIgnoreCase) && kv.Value is string) - .Value; + string userConfigLanguage = userConfig + .First((kv) => kv.Key.Equals("language", StringComparison.OrdinalIgnoreCase) && kv.Value is string) + .Value; - language = userConfigLanguage; - } - catch (Exception) - { - language = null; + language = userConfigLanguage; + } + catch (Exception) + { + } } } catch (Exception) { - // Registry Exception - teamFortress2Folder ??= null; - language ??= null; } } diff --git a/src/HUDMerger/Properties/Settings.Designer.cs b/src/HUDMerger/Properties/Settings.Designer.cs index 381b5a6..de31d09 100644 --- a/src/HUDMerger/Properties/Settings.Designer.cs +++ b/src/HUDMerger/Properties/Settings.Designer.cs @@ -34,5 +34,17 @@ public string TeamFortress2Folder { this["TeamFortress2Folder"] = value; } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("")] + public string Language { + get { + return ((string)(this["Language"])); + } + set { + this["Language"] = value; + } + } } } diff --git a/src/HUDMerger/Properties/Settings.settings b/src/HUDMerger/Properties/Settings.settings index d0383c3..c43bd7d 100644 --- a/src/HUDMerger/Properties/Settings.settings +++ b/src/HUDMerger/Properties/Settings.settings @@ -5,5 +5,8 @@ + + + \ No newline at end of file diff --git a/src/HUDMerger/SettingsWindow.xaml b/src/HUDMerger/SettingsWindow.xaml index ee15fae..05ad044 100644 --- a/src/HUDMerger/SettingsWindow.xaml +++ b/src/HUDMerger/SettingsWindow.xaml @@ -9,8 +9,8 @@ Title="Settings" Width="600" MinWidth="600" - Height="225" - MinHeight="225" + Height="300" + MinHeight="300" Icon="Resources\favicon.ico" WindowStartupLocation="CenterScreen"> diff --git a/src/HUDMerger/ViewModels/SettingsWindowViewModel.cs b/src/HUDMerger/ViewModels/SettingsWindowViewModel.cs index 44c0d50..541185f 100644 --- a/src/HUDMerger/ViewModels/SettingsWindowViewModel.cs +++ b/src/HUDMerger/ViewModels/SettingsWindowViewModel.cs @@ -1,6 +1,7 @@ using System; using System.Windows; using System.Windows.Input; +using HUDMerger.Models; using Microsoft.Toolkit.Mvvm.Input; namespace HUDMerger.ViewModels; @@ -18,6 +19,17 @@ public string TeamFortress2Folder } } + private string _language; + public string Language + { + get => _language; + set + { + _language = value; + OnPropertyChanged(); + } + } + public ICommand CancelCommand { get; } public ICommand ApplyCommand { get; } @@ -25,7 +37,11 @@ public string TeamFortress2Folder public SettingsWindowViewModel() { - _teamFortress2Folder = ((App)Application.Current).Settings.Value.TeamFortress2Folder; + Settings settings = ((App)Application.Current).Settings.Value; + + _teamFortress2Folder = settings.TeamFortress2Folder; + _language = settings.Language; + CancelCommand = new RelayCommand(Cancel); ApplyCommand = new RelayCommand(Apply); } @@ -38,8 +54,10 @@ private void Cancel() private void Apply() { ((App)Application.Current).Settings.Value.TeamFortress2Folder = TeamFortress2Folder; + ((App)Application.Current).Settings.Value.Language = Language; Properties.Settings.Default.TeamFortress2Folder = TeamFortress2Folder; + Properties.Settings.Default.Language = Language; Properties.Settings.Default.Save(); Close?.Invoke(this, new EventArgs()); diff --git a/src/HUDMerger/Views/SettingsWindowView.xaml b/src/HUDMerger/Views/SettingsWindowView.xaml index 49a031f..acc2802 100644 --- a/src/HUDMerger/Views/SettingsWindowView.xaml +++ b/src/HUDMerger/Views/SettingsWindowView.xaml @@ -14,9 +14,11 @@ - +