mlaumcherb/Млаумчерб.Клиент/зримое/Окне.axaml.cs
2024-11-04 01:45:06 +05:00

209 lines
7.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Avalonia;
using Avalonia.Controls;
using Avalonia.Data;
using Avalonia.Interactivity;
using Avalonia.Platform.Storage;
using Avalonia.Threading;
using Млаумчерб.Клиент.классы;
namespace Млаумчерб.Клиент.зримое;
public partial class Окне : Window
{
public static readonly StyledProperty<string> UsernameProperty =
AvaloniaProperty.Register<Окне, string>(nameof(Username),
defaultBindingMode: BindingMode.TwoWay);
public string Username
{
get => GetValue(UsernameProperty);
set => SetValue(UsernameProperty, value);
}
public static readonly StyledProperty<int> MemoryLimitProperty =
AvaloniaProperty.Register<Окне, int>(nameof(MemoryLimit),
defaultBindingMode: BindingMode.TwoWay, defaultValue: 2048);
public int MemoryLimit
{
get => GetValue(MemoryLimitProperty);
set => SetValue(MemoryLimitProperty, value);
}
public static readonly StyledProperty<bool> FullscreenProperty =
AvaloniaProperty.Register<Окне, bool>(nameof(Fullscreen),
defaultBindingMode: BindingMode.TwoWay, defaultValue: false);
public bool Fullscreen
{
get => GetValue(FullscreenProperty);
set => SetValue(FullscreenProperty, value);
}
public static readonly StyledProperty<bool> CheckGameFilesProperty =
AvaloniaProperty.Register<Окне, bool>(nameof(CheckGameFiles),
defaultBindingMode: BindingMode.TwoWay, defaultValue: false);
public bool CheckGameFiles
{
get => GetValue(CheckGameFilesProperty);
set => SetValue(CheckGameFilesProperty, value);
}
public static readonly StyledProperty<bool> EnableJavaDownloadProperty =
AvaloniaProperty.Register<Окне, bool>(nameof(EnableJavaDownload),
defaultBindingMode: BindingMode.TwoWay, defaultValue: true);
public bool EnableJavaDownload
{
get => GetValue(EnableJavaDownloadProperty);
set => SetValue(EnableJavaDownloadProperty, value);
}
public Окне()
{
InitializeComponent();
}
protected override async void OnLoaded(RoutedEventArgs e)
{
try
{
Приложение.Логгер.OnLogMessage += GuiLogMessage;
Username = Приложение.Настройки.имя_пользователя;
MemoryLimit = Приложение.Настройки.выделенная_память_мб;
Fullscreen = Приложение.Настройки.запускать_полноэкранное;
EnableJavaDownload = Приложение.Настройки.скачиватьабу;
Directory.Create(Пути.GetVersionDescriptorsDir());
VersionComboBox.SelectedIndex = 0;
VersionComboBox.IsEnabled = false;
var versions = await GameVersion.GetAllVersionsAsync();
Dispatcher.UIThread.Invoke(() =>
{
foreach (var p in versions)
{
VersionComboBox.Items.Add(new VersionItemView(p));
if (Приложение.Настройки.последняяапущенная_версия != null &&
p.Name == Приложение.Настройки.последняяапущенная_версия)
VersionComboBox.SelectedIndex = VersionComboBox.Items.Count - 1;
}
VersionComboBox.IsEnabled = true;
});
}
catch (Exception ex)
{
Ошибки.ПоказатьСообщение(nameof(Окне), ex);
}
}
private void GuiLogMessage(LauncherLogger.LogMessage msg)
{
if (msg.severity == LogSeverity.Debug) return;
Dispatcher.UIThread.Invoke(() =>
{
double offsetFromBottom = LogScrollViewer.Extent.Height - LogScrollViewer.Offset.Y - LogScrollViewer.Viewport.Height;
bool is_scrolled_to_end = offsetFromBottom < 20.0; // scrolled less then one line up
LogPanel.Children.Add(new LogMessageView(msg));
if (is_scrolled_to_end) LogScrollViewer.ScrollToEnd();
});
}
private async void Запуск(object? sender, RoutedEventArgs e)
{
try
{
Dispatcher.UIThread.Invoke(() => LaunchButton.IsEnabled = false);
var selectedVersionView = (VersionItemView?)VersionComboBox.SelectedItem;
var selectedVersion = selectedVersionView?.Props;
Приложение.Настройки.последняяапущенная_версия = selectedVersion?.Name;
Приложение.Настройки.имя_пользователя = Username;
Приложение.Настройки.выделенная_память_мб = MemoryLimit;
Приложение.Настройки.запускать_полноэкранное = Fullscreen;
Приложение.Настройки.скачиватьабу = EnableJavaDownload;
Приложение.Настройки.СохранитьВФайл();
if (selectedVersion == null)
return;
var v = await GameVersion.CreateFromPropsAsync(selectedVersion);
await v.UpdateFiles(CheckGameFiles, nt =>
{
Dispatcher.UIThread.Invoke(() =>
{
DownloadsPanel.Children.Add(new NetworkTaskView(nt,
ntv => DownloadsPanel.Children.Remove(ntv)));
});
}
);
Dispatcher.UIThread.Invoke(() =>
{
CheckGameFiles = false;
});
}
catch (Exception ex)
{
Ошибки.ПоказатьСообщение(nameof(Окне), ex);
}
finally
{
Dispatcher.UIThread.Invoke(() =>
{
LaunchButton.IsEnabled = true;
});
}
}
private void ОткрытьПапкуЛаунчера(object? s, RoutedEventArgs e)
{
try
{
Launcher.LaunchDirectoryInfoAsync(new DirectoryInfo(Directory.GetCurrent().ToString()))
.ConfigureAwait(false);
}
catch (Exception ex)
{
Ошибки.ПоказатьСообщение(nameof(Окне), ex);
}
}
private void ОткрытьФайлЛогов(object? sender, RoutedEventArgs e)
{
try
{
Launcher.LaunchFileInfoAsync(new FileInfo(Приложение.Логгер.LogfileName.ToString()))
.ConfigureAwait(false);
}
catch (Exception ex)
{
Ошибки.ПоказатьСообщение(nameof(Окне), ex);
}
}
private void ОткрытьРепозиторий(object? sender, RoutedEventArgs e)
{
try
{
Launcher.LaunchUriAsync(new Uri("https://timerix.ddns.net:3322/Timerix/mlaumcherb"))
.ConfigureAwait(false);
}
catch (Exception ex)
{
Ошибки.ПоказатьСообщение(nameof(Окне), ex);
}
}
private void ClearLogPanel(object? sender, RoutedEventArgs e)
{
LogPanel.Children.Clear();
}
private void ClearDownloadsPanel(object? sender, RoutedEventArgs e)
{
foreach (var control in DownloadsPanel.Children)
{
var nt = (NetworkTaskView)control;
nt.Task.Cancel();
}
DownloadsPanel.Children.Clear();
}
}