using System; using System.Collections.Generic; using System.IO; using System.IO.Compression; using System.Linq; using System.Threading.Tasks; using Avalonia.Controls; using Avalonia.Interactivity; using Avalonia.Platform.Storage; using MetaforceInstaller.Core; using MetaforceInstaller.Core.Intefaces; using MetaforceInstaller.Core.Models; using MetaforceInstaller.Core.Services; namespace MetaforceInstaller.UI.Windows; public partial class NewInstallationDialog : Window { private string? _zipPath; private InstallationParts? _installationParts; private readonly IStorageService _storageService; public NewInstallationDialog(IStorageService storageService) { InitializeComponent(); _storageService = storageService; RefreshCheckboxes(); CancelButton.Click += OnCancelClick; ChooseZip.Click += OnChooseZipClick; InstallButton.IsEnabled = false; InstallButton.Click += OnInstallClick; } private void RefreshCheckboxes() { var serverCheckbox = ServerCheckBox; var pcAdminCheckbox = PcAdminCheckBox; var androidAdminCheckbox = AndroidAdminCheckbox; var vrClientCheckbox = VrClientCheckbox; serverCheckbox.Content = Lang.Resources.InstallServerCheckbox; serverCheckbox.IsEnabled = true; pcAdminCheckbox.Content = Lang.Resources.InstallAdminCheckbox; pcAdminCheckbox.IsEnabled = true; androidAdminCheckbox.Content = Lang.Resources.SaveAndroidAdminCheckbox; androidAdminCheckbox.IsEnabled = true; vrClientCheckbox.Content = Lang.Resources.SaveVRClientCheckbox; vrClientCheckbox.IsEnabled = true; } private async void OnChooseZipClick(object? sender, RoutedEventArgs e) { var topLevel = GetTopLevel(this); var files = await topLevel!.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions { Title = "Выберите архив с контентом", AllowMultiple = false, FileTypeFilter = [ new FilePickerFileType("ZIP Files") { Patterns = ["*.zip"] } ] }); if (files.Count >= 1) { _zipPath = files[0].Path.LocalPath; using var archive = ZipFile.OpenRead(_zipPath); _installationParts = ZipScrapper.PeekFiles(archive); UpdateCheckboxes(); archive.Dispose(); } } private async void OnInstallClick(object? sender, RoutedEventArgs e) { using var archive = ZipFile.OpenRead(_zipPath); var title = TitleTextBox.Text ?? Path.GetFileNameWithoutExtension(_zipPath); var installationGuid = Guid.NewGuid(); var progress = new Progress(value => { ProgressBar.Value = value; }); string extractedPath = null; await Task.Run(() => { extractedPath = ZipScrapper.ExtractZip( archive, Defaults.StoragePath, installationGuid, progress); }); InstallButton.IsEnabled = false; var appData = _storageService.Load(); var updatedParts = ZipScrapper.UpdatePathsAfterExtraction(_installationParts, extractedPath); var installationData = new InstallationData { Id = installationGuid, Title = title, Parts = updatedParts }; appData.Installations.Add(installationData); _storageService.Save(appData); Close(); } private void UpdateCheckboxes() { RefreshCheckboxes(); var serverCheckbox = ServerCheckBox; var pcAdminCheckbox = PcAdminCheckBox; var androidAdminCheckbox = AndroidAdminCheckbox; var vrClientCheckbox = VrClientCheckbox; if (string.IsNullOrEmpty(_installationParts.WindowsServerPath)) { serverCheckbox.IsEnabled = false; serverCheckbox.Content += $"\n{Lang.Resources.NoServerError}"; } if (string.IsNullOrEmpty(_installationParts.WindowsAdminPath)) { pcAdminCheckbox.IsEnabled = false; pcAdminCheckbox.Content += $"\n{Lang.Resources.NoPCAdminError}"; } if (string.IsNullOrEmpty(_installationParts.WindowsContentPath)) { pcAdminCheckbox.IsEnabled = false; pcAdminCheckbox.Content += $"\n{Lang.Resources.NoWindowsContentError}"; } if (string.IsNullOrEmpty(_installationParts.AndroidContentPath)) { vrClientCheckbox.IsEnabled = false; vrClientCheckbox.Content += $"\n{Lang.Resources.NoAndroidContentError}"; androidAdminCheckbox.IsEnabled = false; androidAdminCheckbox.Content += $"\n{Lang.Resources.NoAndroidContentError}"; } if (string.IsNullOrEmpty(_installationParts.PicoClientPath) && string.IsNullOrEmpty(_installationParts.OculusClientPath)) { vrClientCheckbox.IsEnabled = false; vrClientCheckbox.Content += $"\n{Lang.Resources.NoVRClientsError}"; } InstallButton.IsEnabled = new List { serverCheckbox, pcAdminCheckbox, androidAdminCheckbox, vrClientCheckbox }.Any(x => x?.IsEnabled == true); } private void OnCancelClick(object? sender, RoutedEventArgs e) { Close(); } }