Config Manager update

Auto fix for broken and outdated configs and base for work in future
This commit is contained in:
Max Chaev 2025-02-25 22:43:25 +03:00
parent 45ffb1dfbb
commit d5c8d36d61
4 changed files with 90 additions and 24 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 543 B

View File

@ -6,25 +6,43 @@ namespace Plombir;
public class ConfigManager public class ConfigManager
{ {
static private readonly string defaultRuntime = "./runtime";
static private readonly string defaultVersion = "1.20.1";
static private readonly string defaultNickname = "slugcat";
static private readonly string currentTweaksVersion = "1.2.0";
static private readonly string defaultLoginType = "offline";
static private readonly string defaultUuid = "offline";
static private readonly string defaultAccsessToken = "offline";
static private readonly string defaultClientToken = "offline";
public static void BuildDefault() public static void BuildDefault()
{ {
TomlTable toml = new TomlTable TomlTable toml = new TomlTable
{ {
["title"] = "Launcher tweaks file!", ["title"] = "Launcher tweaks file!",
["tweaks-version"] = currentTweaksVersion,
["runtime-path"] = new TomlString ["runtime-path"] = new TomlString
{ {
Value = "./runtime", Value = defaultRuntime,
Comment = "Path to folder that will hold all minecraft versions" Comment = "Path to folder that will hold all minecraft versions"
}, },
["last-version-launched"] = new TomlString ["last-version-launched"] = new TomlString
{ {
Value = "1.20.1", Value = defaultVersion,
Comment = "Saving last launched version to use it after launch" Comment = "Saving last launched version to use it after launch"
}, },
["nickname"] = "slugcat" ["nickname"] = defaultNickname,
["login-method"] = defaultLoginType,
["uuid"] = defaultUuid,
["accsess-token"] = defaultAccsessToken,
["client-token"] = defaultClientToken,
}; };
using (StreamWriter writer = File.CreateText("tweaks.toml")) using (StreamWriter writer = File.CreateText("tweaks.toml"))
@ -45,7 +63,43 @@ public class ConfigManager
{ {
TomlTable table = TOML.Parse(reader); TomlTable table = TOML.Parse(reader);
return table[key]; // If config don't contains it means it's oudated or broken.
if (!table.TryGetNode(key, out var node))
{
WriteInConfig(key, "NaN");
}
// In case if config is fine return its value
if (table[key].ToString() != "NaN")
{
if ((key == "tweaks-version") & (table[key] != currentTweaksVersion))
{
Console.WriteLine("Warning! Tweaks version mismatch!");
}
return table[key];
}
// Handling out-dated or broken toml files by filling with defaults
switch (key)
{
case "runtime-path": { WriteInConfig(key, defaultRuntime); return defaultRuntime; }
case "last-version-launched": { WriteInConfig(key, defaultVersion); return defaultVersion; }
case "nickname": { WriteInConfig(key, defaultNickname); return defaultNickname; }
case "accsess-token": { WriteInConfig(key, defaultAccsessToken); return defaultAccsessToken; }
case "client-token": { WriteInConfig(key, defaultClientToken); return defaultClientToken; }
case "uuid": { WriteInConfig(key, defaultUuid); return defaultUuid; }
case "login-method": { WriteInConfig(key, defaultLoginType); return defaultLoginType; }
case "tweaks-version":
{
WriteInConfig(key, currentTweaksVersion);
Console.WriteLine("Warning! Can't locate tweaks version! Attempting auto recovery...");
return "possibly broken";
}
}
return null;
} }
} }

View File

@ -1,14 +1,12 @@
<Window <Window
xmlns="https://github.com/avaloniaui" xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" mc:Ignorable="d"
d:DesignWidth="500" d:DesignWidth="500"
d:DesignHeight="400" d:DesignHeight="400"
x:Class="LauncherGUI.Views.SettingsWindow" x:Class="LauncherGUI.Views.SettingsWindow"
Icon="/Assets/Sprites/icon.png" Icon="/Assets/Sprites/icon.png"
Title="Launcher tweaks" Title="Launcher tweaks"
RequestedThemeVariant="Dark" RequestedThemeVariant="Dark"
@ -16,37 +14,50 @@
Height="400" Height="400"
MinWidth="500" MinWidth="500"
MinHeight="400" MinHeight="400"
MaxWidth="500"
MaxHeight="400"
Background="#353535" Background="#353535"
> >
<Grid> <Grid>
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="*"/> <RowDefinition Height="*" />
<RowDefinition Height="*"/> <RowDefinition Height="*" />
<RowDefinition Height="Auto"/> <RowDefinition Height="Auto" />
</Grid.RowDefinitions> </Grid.RowDefinitions>
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/> <ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<StackPanel Grid.Row="0" Grid.ColumnSpan="2">
<TextBlock
HorizontalAlignment="Center"
Text="All tweaks can be found in TOML near executable."
FontFamily="{StaticResource QuicksandFont}"
/>
<TextBlock
HorizontalAlignment="Center"
x:Name="tweaksVersion"
FontFamily="{StaticResource QuicksandFont}"
/>
</StackPanel>
<StackPanel Grid.Row="1" Grid.Column="0">
<Button
Margin="5"
Content="Select minecraft folder"
Click="OnFilePickerClick"
/>
<TextBlock
Grid.Row="0" Grid.ColumnSpan="2"
HorizontalAlignment="Center"
Text="All tweaks can be found in TOML near executable."
FontFamily="{StaticResource QuicksandFont}" />
<StackPanel
Grid.Row="1" Grid.Column="0">
<Button Margin="5" Content="Select minecraft folder" Click="OnFilePickerClick" />
</StackPanel> </StackPanel>
<StackPanel Grid.Row="2" Grid.Column="1"> <StackPanel Grid.Row="2" Grid.Column="1">
<Image Source="/Assets/Sprites/toml-paper.png" <Image Source="/Assets/Sprites/toml-paper.png" Stretch="Uniform" />
Stretch="Uniform"/>
</StackPanel> </StackPanel>
</Grid> </Grid>

View File

@ -14,6 +14,7 @@ public partial class SettingsWindow : Window
{ {
InitializeComponent(); InitializeComponent();
_mainWindowVM = mainWindowVM; _mainWindowVM = mainWindowVM;
tweaksVersion.Text = $"Current tweaks version: {_mainWindowVM.TweaksVersion}";
} }
private async void OnFilePickerClick(object sender, RoutedEventArgs args) private async void OnFilePickerClick(object sender, RoutedEventArgs args)