Skip to content

Commit

Permalink
Add Language setting
Browse files Browse the repository at this point in the history
  • Loading branch information
cooolbros committed Jan 3, 2024
1 parent 1c0f8f5 commit 987e9c5
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 44 deletions.
84 changes: 45 additions & 39 deletions src/HUDMerger/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ public partial class App : Application
public Lazy<Settings> 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
{
Expand All @@ -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;
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/HUDMerger/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/HUDMerger/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
<Setting Name="TeamFortress2Folder" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="Language" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
</Settings>
</SettingsFile>
4 changes: 2 additions & 2 deletions src/HUDMerger/SettingsWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
Title="Settings"
Width="600"
MinWidth="600"
Height="225"
MinHeight="225"
Height="300"
MinHeight="300"
Icon="Resources\favicon.ico"
WindowStartupLocation="CenterScreen">
<Grid>
Expand Down
20 changes: 19 additions & 1 deletion src/HUDMerger/ViewModels/SettingsWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Windows;
using System.Windows.Input;
using HUDMerger.Models;
using Microsoft.Toolkit.Mvvm.Input;

namespace HUDMerger.ViewModels;
Expand All @@ -18,14 +19,29 @@ public string TeamFortress2Folder
}
}

private string _language;
public string Language
{
get => _language;
set
{
_language = value;
OnPropertyChanged();
}
}

public ICommand CancelCommand { get; }
public ICommand ApplyCommand { get; }

public event EventHandler? Close;

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);
}
Expand All @@ -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());
Expand Down
6 changes: 4 additions & 2 deletions src/HUDMerger/Views/SettingsWindowView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label FontSize="22" Grid.Row="0" Margin="10,0,0,0">Settings</Label>
<StackPanel Name="SettingsContainer" Grid.Row="1" Margin="10,0,10,10">
<StackPanel Grid.Row="1" Margin="10,0,10,10">
<Label Content="Team Fortress 2 Folder" FontSize="15" ToolTip="Path to Team Fortress 2 Installation. Used for extracting required HUD files." />
<TextBox Style="{StaticResource TextBoxStyle1}" HorizontalAlignment="Left" MinWidth="480" Text="{Binding TeamFortress2Folder, Mode=TwoWay}" />
<TextBox Style="{StaticResource TextBoxStyle1}" HorizontalAlignment="Left" MinWidth="480" Text="{Binding TeamFortress2Folder, Mode=TwoWay}" Margin="0,0,0,5" />
<Label Content="Language" FontSize="15" />
<TextBox Style="{StaticResource TextBoxStyle1}" HorizontalAlignment="Left" MinWidth="200" Text="{Binding Language, Mode=TwoWay}" />
</StackPanel>
<WrapPanel HorizontalAlignment="Left" VerticalAlignment="Bottom" Grid.Row="2" Margin="10,5,10,10">
<Button Style="{StaticResource EnabledButton}" FontSize="14" Padding="20,8" Command="{Binding CancelCommand}" Margin="0,0,10,0" IsCancel="True">Cancel</Button>
Expand Down

0 comments on commit 987e9c5

Please sign in to comment.