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
{
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()
{
TomlTable toml = new TomlTable
{
["title"] = "Launcher tweaks file!",
["tweaks-version"] = currentTweaksVersion,
["runtime-path"] = new TomlString
{
Value = "./runtime",
Value = defaultRuntime,
Comment = "Path to folder that will hold all minecraft versions"
},
["last-version-launched"] = new TomlString
{
Value = "1.20.1",
Value = defaultVersion,
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"))
@ -45,8 +63,44 @@ public class ConfigManager
{
TomlTable table = TOML.Parse(reader);
// 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;
}
}
public static void WriteInConfig(string key, dynamic value)

View File

@ -1,14 +1,12 @@
<Window
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignWidth="500"
d:DesignHeight="400"
x:Class="LauncherGUI.Views.SettingsWindow"
Icon="/Assets/Sprites/icon.png"
Title="Launcher tweaks"
RequestedThemeVariant="Dark"
@ -16,6 +14,8 @@
Height="400"
MinWidth="500"
MinHeight="400"
MaxWidth="500"
MaxHeight="400"
Background="#353535"
>
@ -29,22 +29,33 @@
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<StackPanel Grid.Row="0" Grid.ColumnSpan="2">
<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">
FontFamily="{StaticResource QuicksandFont}"
/>
<Button Margin="5" Content="Select minecraft folder" Click="OnFilePickerClick" />
<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"
/>
</StackPanel>
<StackPanel Grid.Row="2" Grid.Column="1">
<Image Source="/Assets/Sprites/toml-paper.png"
Stretch="Uniform"/>
<Image Source="/Assets/Sprites/toml-paper.png" Stretch="Uniform" />
</StackPanel>

View File

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