mlaumcherb/Млаумчерб.Клиент/видимое/Окне.axaml.cs
2024-09-29 08:21:48 +05:00

179 lines
7.1 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 Окне()
{
InitializeComponent();
}
protected override async void OnLoaded(RoutedEventArgs e)
{
try
{
Приложение.Логгер.OnLogMessage += (context, severity, message, format) =>
{
if(severity == LogSeverity.Debug)
return;
StringBuilder b = new();
b.Append(DateTime.Now.ToString("[HH:mm:ss]["));
b.Append(severity);
b.Append("]: ");
b.Append(message);
b.Append('\n');
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
LogTextBox.Text += b.ToString();
if (is_scrolled_to_end)
LogScrollViewer.ScrollToEnd();
});
};
Username = Приложение.Настройки.имя_пользователя;
MemoryLimit = Приложение.Настройки.выделенная_память_мб;
Fullscreen = Приложение.Настройки.открыватьаесь_экран;
Directory.Create(Пути.GetVersionDescriptorDir());
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 async void Запуск(object? sender, RoutedEventArgs e)
{
try
{
var selectedVersionView = (VersionItemView?)VersionComboBox.SelectedItem;
var selectedVersion = selectedVersionView?.Props;
Приложение.Настройки.последняяапущенная_версия = selectedVersion?.Name;
Приложение.Настройки.имя_пользователя = Username;
Приложение.Настройки.выделенная_память_мб = MemoryLimit;
Приложение.Настройки.открыватьаесь_экран = Fullscreen;
Приложение.Настройки.СохранитьВФайл();
if (selectedVersion == null)
return;
var v = await GameVersion.CreateFromPropsAsync(selectedVersion);
var updateTasks = await v.CreateUpdateTasksAsync(CheckGameFiles);
foreach (var t in updateTasks)
{
var updateTask = t.StartAsync();
Dispatcher.UIThread.Invoke(() =>
{
var view = new DownloadTaskView(t, view => DownloadsPanel.Children.Remove(view));
DownloadsPanel.Children.Add(view);
});
await updateTask;
}
Dispatcher.UIThread.Invoke(() => CheckGameFiles = false);
}
catch (Exception ex)
{
Ошибки.ПоказатьСообщение(nameof(Окне), ex);
}
}
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);
}
}
}