Version 1.1.0 | Settings and gitlab repo

This commit is contained in:
Max Chaev 2025-02-21 16:09:06 +03:00
parent 48220f8b23
commit 96cefe5749
23 changed files with 403 additions and 115 deletions

View File

@ -1,4 +1,4 @@
using System.Formats.Tar;
using System.Formats.Tar;
using System.Net;
using System.Runtime.InteropServices;
using System.Security.AccessControl;
@ -6,7 +6,7 @@ using CmlLib.Core;
using CmlLib.Core.Auth;
using CmlLib.Core.ProcessBuilder;
namespace BlightFlame
namespace PlombirLauncher
{
/*
Basically a class to manage everything launcher related.
@ -22,11 +22,11 @@ namespace BlightFlame
public int DownloadProgress;
public string DownloadStatus;
public Launcher(string userName, string version)
public Launcher(string userName, string version, string? location = null)
{
_version = version;
_nickname = userName;
_mcPath = new($"./runtime/{version}/minecraft");
_mcPath = new($"{location}/{version}/minecraft");
_mcLauncher = new(_mcPath);
}
@ -95,10 +95,8 @@ namespace BlightFlame
// File.Delete(archieveName);
// }
async public Task BuildLauncher()
async public Task BuildLauncher(CancellationToken token)
{
_mcLauncher.ByteProgressChanged += (_, args) =>
{
Console.WriteLine($"{(int)(args.ProgressedBytes * 0.000001)} MBytes / {(int)(args.TotalBytes * 0.000001)} MBytes");
@ -117,11 +115,13 @@ namespace BlightFlame
await _mcLauncher.InstallAsync(_version);
DownloadStatus = "Finished!";
Console.WriteLine(DownloadStatus);
return;
}
async public Task RunLauncher()
{
var process = await _mcLauncher.BuildProcessAsync(_version, new MLaunchOption{
var process = await _mcLauncher.BuildProcessAsync(_version, new MLaunchOption
{
Session = MSession.CreateOfflineSession(_nickname),
MaximumRamMb = 4096,
@ -137,4 +137,3 @@ namespace BlightFlame
}
}

View File

@ -1,4 +1,4 @@
namespace BlightFlame
namespace PlombirLauncher
{
sealed class Program
{

View File

@ -7,7 +7,7 @@ using SharpCompress.Readers;
using System.Diagnostics;
using CmlLib.Core;
namespace BlightFlame
namespace PlombirLauncher
{
static public class Utils
{

Binary file not shown.

After

Width:  |  Height:  |  Size: 892 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 939 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 911 B

View File

@ -0,0 +1,75 @@
using System;
using System.IO;
using Tommy;
namespace PlombirLauncher;
public class ConfigManager
{
public static void BuildDefault()
{
TomlTable toml = new TomlTable
{
["title"] = "Launcher tweaks file!",
["runtime-path"] = new TomlString
{
Value = "./runtime",
Comment = "Path to folder that will hold all minecraft versions"
},
["last-version-launched"] = new TomlString
{
Value = "1.20.1",
Comment = "Saving last launched version to use it after launch"
},
["nickname"] = "slugcat"
};
using(StreamWriter writer = File.CreateText("tweaks.toml"))
{
toml.WriteTo(writer);
// Remember to flush the data if needed!
writer.Flush();
}
}
public static dynamic? ReadConfig(string key)
{
if (!File.Exists("tweaks.toml"))
{
BuildDefault();
}
using(StreamReader reader = File.OpenText("tweaks.toml"))
{
TomlTable table = TOML.Parse(reader);
return table[key];
}
}
public static void WriteInConfig(string key, dynamic value)
{
if (!File.Exists("tweaks.toml"))
{
BuildDefault();
}
TomlTable table;
using(StreamReader reader = File.OpenText("tweaks.toml"))
{
table = TOML.Parse(reader);
table[key] = value;
}
using(StreamWriter writer = File.CreateText("tweaks.toml"))
{
table.WriteTo(writer);
// Remember to flush the data if needed!
writer.Flush();
}
}
}

View File

@ -28,6 +28,7 @@
<PrivateAssets Condition="'$(Configuration)' != 'Debug'">All</PrivateAssets>
</PackageReference>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.1" />
<PackageReference Include="Tommy" Version="3.1.2" />
</ItemGroup>
<ItemGroup>

View File

@ -0,0 +1,23 @@
using System.Threading.Tasks;
using Avalonia.Controls;
using LauncherGUI.ViewModels;
using LauncherGUI.Views;
namespace PlombirLauncher;
public class LauncherUtils
{
public async static Task CreateMinecraftInstance(MainWindowViewModel vm, Window windowCaller)
{
System.Console.WriteLine($"Creating minecraft instance for {vm.Usernick}, {vm.SelectedVersion} in {vm.RuntimeLocation}");
LoadingWindow loading = new(vm.Usernick, vm.SelectedVersion, vm.RuntimeLocation);
ConfigManager.WriteInConfig("last-version-launched", vm.SelectedVersion);
ConfigManager.WriteInConfig("nickname", vm.Usernick);
loading.Show();
await loading.InitLoading();
loading.Close();
await loading.RunMinecraft();
return;
}
}

View File

@ -1,5 +1,5 @@
using Avalonia;
using BlightFlame;
using PlombirLauncher;
using System;
namespace LauncherGUI;

View File

@ -2,7 +2,7 @@ namespace LauncherGUI.ViewModels;
public partial class LoadingWindowViewModel : ViewModelBase
{
private long _loadingProgress;
private long _loadingProgress = 0;
public long Progress {get => _loadingProgress; set {_loadingProgress = value; OnPropertyChanged(nameof(Progress)); }}
private string _loadingStatus = "Rotating transistors..";

View File

@ -1,5 +1,5 @@
using System.Collections.Generic;
using BlightFlame;
using PlombirLauncher;
namespace LauncherGUI.ViewModels;
@ -7,9 +7,12 @@ public partial class MainWindowViewModel : ViewModelBase
{
public string Greeting { get; } = "Plombir launcher";
private string _userNick = "slugcat";
private string _userNick = ConfigManager.ReadConfig("nickname");
public string Usernick {get => _userNick; set {_userNick = value; OnPropertyChanged(nameof(Usernick)); }}
private string _selectedVersion = "1.20.1";
private string _selectedVersion = ConfigManager.ReadConfig("last-version-launched");
public string SelectedVersion {get => _selectedVersion; set {_selectedVersion = value; OnPropertyChanged(nameof(SelectedVersion)); }}
private string _runtimeLocation = ConfigManager.ReadConfig("runtime-path");
public string RuntimeLocation {get => _runtimeLocation; set {_runtimeLocation = value; OnPropertyChanged(nameof(RuntimeLocation)); }}
}

View File

@ -0,0 +1,6 @@
namespace LauncherGUI.ViewModels;
public partial class SettingsWindowViewModel : ViewModelBase
{
// I save launcher data mostly in MainWindowViewModel.
}

View File

@ -1,10 +1,10 @@
using System.Collections.Generic;
using BlightFlame;
using PlombirLauncher;
using LauncherGUI.ViewModels;
namespace LauncherGUI.ViewModels;
public partial class VersionSelectorWindowViewModel : ViewModelBase
{
// I save launcher data mostly in MainWindowViewModel.
}

View File

@ -4,4 +4,5 @@ namespace LauncherGUI.ViewModels;
public class ViewModelBase : ObservableObject
{
// I save launcher data mostly in MainWindowViewModel.
}

View File

@ -1,24 +1,25 @@
<Window xmlns="https://github.com/avaloniaui"
<Window
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:LauncherGUI.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="400" d:DesignHeight="200"
mc:Ignorable="d"
d:DesignWidth="400"
d:DesignHeight="200"
x:Class="LauncherGUI.Views.LoadingWindow"
x:DataType="vm:LoadingWindowViewModel"
Icon="/Assets/icon.png"
Title="loading"
Title="Loading"
RequestedThemeVariant="Dark"
Width="400" Height="200"
MinWidth="400" MinHeight="200"
MaxWidth="400" MaxHeight="200"
Background="#353535">
<Design.DataContext>
<!-- This only sets the DataContext for the previewer in an IDE,
to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
<vm:LoadingWindowViewModel/>
</Design.DataContext>
Width="400"
Height="200"
MinWidth="400"
MinHeight="200"
MaxWidth="400"
MaxHeight="200"
Background="#353535"
>
<Grid>
<Grid.RowDefinitions>
@ -29,26 +30,32 @@
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<StackPanel
Grid.Row="0"
Grid.ColumnSpan="2" >
<TextBlock Text="Loading minecraft..."
<TextBlock
Text="Loading minecraft..."
FontFamily="{StaticResource QuicksandFont}"
HorizontalAlignment="Center"
Grid.Row="0" Grid.ColumnSpan="2"/>
HorizontalAlignment="Center" />
<!-- Todo: idk but installAsync ignores token. -->
<!-- <Button HorizontalAlignment="Center"
Content="Stop" Click="onStopDownloadClick" /> -->
</StackPanel>
<Border
Grid.Row="1" Grid.Column="1"
Grid.Row="1"
Grid.Column="1"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<TextBlock Text="{Binding LoadingStatus}" />
</Border>
<Border
Margin="10"
Grid.Row="2" Grid.ColumnSpan="2">
<ProgressBar
Minimum="0"
Value="{Binding Progress}"
Maximum="100"/>
<Border Margin="10" Grid.Row="2" Grid.ColumnSpan="2">
<ProgressBar Minimum="0" Value="{Binding Progress}" Maximum="100" />
</Border>
</Grid>

View File

@ -3,17 +3,22 @@ using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.VisualTree;
using BlightFlame;
using PlombirLauncher;
using LauncherGUI.ViewModels;
using System.Threading;
namespace LauncherGUI.Views;
public partial class LoadingWindow : Window
{
private Launcher ln;
public LoadingWindow(string nickname, string? version = null)
private Task _buildingTask;
// Tokens for controlling download task.
CancellationTokenSource tokenSource = new CancellationTokenSource();
public LoadingWindow(string nickname, string? version = null, string? location = null)
{
ln = new Launcher(nickname, version ?? "1.20.1");
ln = new Launcher(nickname, version ?? "1.20.1", location ?? "./runtime");
InitializeComponent();
DataContext = new LoadingWindowViewModel();
}
@ -25,6 +30,7 @@ public partial class LoadingWindow : Window
{
throw new InvalidOperationException("No DataContext set");
}
Task updateGui = new Task(async () => {
while (ln.DownloadStatus != "Finished!"){
viewModel.Progress = ln.DownloadProgress;
@ -33,10 +39,13 @@ public partial class LoadingWindow : Window
}
viewModel.Progress = 100;
});
updateGui.Start();
await ln.BuildLauncher();
_buildingTask = ln.BuildLauncher(tokenSource.Token);
await _buildingTask;
}
@ -45,4 +54,12 @@ public partial class LoadingWindow : Window
{
await ln.RunLauncher();
}
private void onStopDownloadClick(object sender, RoutedEventArgs e)
{
var button = sender as Button;
tokenSource.Cancel();
System.Console.WriteLine("Loading disabled gracefully.");
Close();
}
}

View File

@ -48,8 +48,25 @@
</Border>
<TextBlock FontFamily="{StaticResource QuicksandFont}"
HorizontalAlignment="Right" Text="v1.0.0 - nullmax17"/>
HorizontalAlignment="Right" Text="v1.1.0 - nullmax17"/>
</StackPanel>
<Canvas Grid.Row="0" Grid.Column="1">
<Border
Background="#353535"
CornerRadius="10"
Margin="5"
Canvas.Left="290">
<Image
Height="60"
Source="/Assets/cog.png"
Stretch="Uniform"
PointerPressed="OnCogPressed" />
</Border>
</Canvas>
@ -98,15 +115,13 @@
HorizontalAlignment="Center"/>
</Border>
</StackPanel>
<StackPanel Height="300" Grid.Row="1" Grid.Column="2">
<Border
Background="#353535"
HorizontalAlignment="Center"
Width="305"
Height="300"
CornerRadius="10"
Padding="15" Margin="15"
Grid.Row="1" Grid.Column="2">
Padding="15" Margin="15">
<StackPanel>
<TextBlock FontFamily="{StaticResource QuicksandFont}" Text="> Cross-platform minecraft launcher!"/>
@ -118,6 +133,17 @@
</StackPanel>
</Border>
<Canvas>
<Image
Height="60"
Source="/Assets/gitlab-logo.png"
Stretch="Uniform"
PointerPressed="OnGitlabPressed"
Canvas.Left="290"
Canvas.Top="80"/>
</Canvas>
</StackPanel>
</Grid>
</Window>

View File

@ -4,9 +4,11 @@ using System.Linq;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.VisualTree;
using BlightFlame;
using PlombirLauncher;
using CmlLib.Core.VersionMetadata;
using LauncherGUI.ViewModels;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace LauncherGUI.Views;
@ -17,7 +19,7 @@ public partial class MainWindow : Window
{
InitializeComponent();
DataContext = new MainWindowViewModel();
if (DataContext == null) throw new NullReferenceException("Cant load MainWindow DataContext");
if (DataContext == null) throw new NullReferenceException("Failed to set DataContext for MainWindow");
}
private async void OnLaunchMinecraftClick(object sender, RoutedEventArgs e)
{
@ -29,26 +31,59 @@ public partial class MainWindow : Window
if (root == null) return;
var vm = DataContext as MainWindowViewModel;
LoadingWindow loading = new(vm.Usernick, vm.SelectedVersion);
loading.Show(root);
await loading.InitLoading();
loading.Close();
await LauncherUtils.CreateMinecraftInstance(vm, root);
button.Content = "Minecraft launched";
await loading.RunMinecraft();
}
private void OnChooseVersionClick(object sender, RoutedEventArgs e)
{
if (sender is not Button button) return;
var button = sender as Button;
if (button == null) return;
var root = button.GetVisualRoot() as MainWindow;
if (root == null) return;
if (button.GetVisualRoot() is not MainWindow root) return;
VersionSelectorWindow selector = new(DataContext as MainWindowViewModel);
selector.Show(root);
}
private void OnCogPressed(object sender, RoutedEventArgs e)
{
if (sender is not Image img) return;
if (img.GetVisualRoot() is not MainWindow root) return;
SettingsWindow settings = new(DataContext as MainWindowViewModel);
settings.Show(root);
}
private void OnGitlabPressed(object sender, RoutedEventArgs e)
{
string url = "https://gitlab.com/nullmax17/PlombirLauncher";
try
{
Process.Start(url);
}
catch
{
// hack because of this: https://github.com/dotnet/corefx/issues/10361
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
url = url.Replace("&", "^&");
Process.Start(new ProcessStartInfo(url) { UseShellExecute = true });
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Process.Start("xdg-open", url);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
Process.Start("open", url);
}
else
{
throw;
}
}
}
}

View File

@ -0,0 +1,54 @@
<Window
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:LauncherGUI.ViewModels"
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"
x:DataType="vm:SettingsWindowViewModel"
Icon="/Assets/icon.png"
Title="Launcher tweaks"
RequestedThemeVariant="Dark"
Width="500"
Height="400"
MinWidth="500"
MinHeight="400"
Background="#353535"
>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<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 Grid.Row="2" Grid.Column="1">
<Image Source="/Assets/toml-paper.png"
Stretch="Uniform"/>
</StackPanel>
</Grid>
</Window>

View File

@ -0,0 +1,41 @@
using System;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Platform.Storage;
using LauncherGUI.ViewModels;
using PlombirLauncher;
namespace LauncherGUI.Views;
public partial class SettingsWindow : Window
{
private MainWindowViewModel _mainWindowVM;
public SettingsWindow(MainWindowViewModel mainWindowVM)
{
InitializeComponent();
_mainWindowVM = mainWindowVM;
}
private async void OnFilePickerClick(object sender, RoutedEventArgs args)
{
// Get top level from the current control. Alternatively, you can use Window reference instead.
var topLevel = TopLevel.GetTopLevel(this);
// Start async operation to open the dialog.
var files = await topLevel.StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions
{
Title = "Select directory for minecraft",
AllowMultiple = false
});
if (files.Count >= 1)
{
var location = files[0].TryGetLocalPath();
if (location is null) throw new NullReferenceException("Invalid location selected!");
_mainWindowVM.RuntimeLocation = location;
System.Console.WriteLine($"Selected {_mainWindowVM.RuntimeLocation}.");
ConfigManager.WriteInConfig("runtime-path", location);
}
}
}

View File

@ -4,6 +4,7 @@ using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Threading;
using LauncherGUI.ViewModels;
using PlombirLauncher;
namespace LauncherGUI.Views;
@ -16,7 +17,7 @@ public partial class VersionSelectorWindow : Window
_mainViewModel = mainViewModel;
//Load versions asynchronously
var versionsList = Task.Run(() => BlightFlame.Utils.GetAllMcVersions().GetAwaiter().GetResult());
var versionsList = Task.Run(() => PlombirLauncher.Utils.GetAllMcVersions().GetAwaiter().GetResult());
versions.ItemsSource = versionsList.Result;
@ -28,15 +29,10 @@ public partial class VersionSelectorWindow : Window
_mainViewModel.SelectedVersion = selectedVersion;
System.Console.WriteLine($"Selected version: {selectedVersion}");
LoadingWindow loading = new(_mainViewModel.Usernick, _mainViewModel.SelectedVersion);
loading.Show(this);
await loading.InitLoading();
loading.Close();
this.Close();
await loading.RunMinecraft();;
LauncherUtils.CreateMinecraftInstance(_mainViewModel, this);
Close();
}

View File

@ -0,0 +1,4 @@
title = "Launcher tweaks file!"
runtime-path = "/home/nullmax17/test_folder"
last-version-launched = "1.15"
nickname = "nullmax17"