123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- using Avalonia.Controls.ApplicationLifetimes;
- using Avalonia.Threading;
- using DynamicData;
- using ReactiveUI;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Threading.Tasks;
- using VeloeMinecraftLauncher.Entity.VersionManifest;
- using VeloeMinecraftLauncher.MinecraftLauncher;
- using VeloeMinecraftLauncher.Models.Entity;
- using VeloeMinecraftLauncher.Views;
- namespace VeloeMinecraftLauncher.ViewModels
- {
- public class VersionsDownloaderViewModel : ViewModelBase
- {
- private string startButton = "Download";
- private bool showOld = false;
- private bool showSnaps = false;
- private bool installFabric = false;
- private bool installForge = false;
- private bool installOptifine = false;
- private bool installForgeOptifine = false;
- private bool installFabricVisible = false;
- private bool installForgeVisible = false;
- private bool installOptifineVisible = false;
- private bool installForgeOptifineVisible = false;
- private bool downloadJava = false;
- private bool isControlsEnabled = true;
- private int progress = 0;
- private string downloadingFileName;
- Serilog.ILogger logger;
- ObservableCollection<Entity.VersionManifest.Version> filteredVersions;
- ObservableCollection<Modpack> modpackVersions;
- Entity.VersionManifest.Version filteredVersion;
- Modpack selectedModpack;
- VersionManifest versionManifest;
- public VersionsDownloaderViewModel()
- {
- try
- {
- Task.Run(() =>
- {
- logger = Settings.logger;
- if (FilteredVersions is null)
- {
- FilteredVersions = new();
- }
- if (ModpackVersions is null)
- {
- ModpackVersions = new();
- }
- logger.Debug("Getting versionManifest.json");
- versionManifest = Downloader.DownloadAndDeserializeJsonData<VersionManifest>("https://launchermeta.mojang.com/mc/game/version_manifest_v2.json");
- ModpackVersions.AddRange(Downloader.DownloadAndDeserializeJsonData<List<Modpack>>("https://files.veloe.link/launcher/modpacks.json"));
- logger.Debug("Updating available versions to download.");
- updateList();
- });
- }
- catch (Exception ex)
- {
- var message = ex.Message;
- var stackTrace = ex.StackTrace;
- Settings.logger.Error(ex.Message);
- Settings.logger.Error(ex.StackTrace);
- Exception innerException = ex.InnerException;
- while (innerException is not null)
- {
- message += "\n" + innerException.Message;
- stackTrace += "\n" + innerException.StackTrace;
- Settings.logger.Error(innerException.Message);
- Settings.logger.Error(innerException.StackTrace);
- innerException = innerException.InnerException;
- }
- if (Avalonia.Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
- {
- var ErrorMessageViewModel = new ErrorWindowViewModel(message, stackTrace);
- var ErrorMessage = new ErrorWindow()
- {
- DataContext = ErrorMessageViewModel
- };
- //var versionsDownloaderTask =
- ErrorMessage.ShowDialog(desktop.MainWindow);
- //versionsDownloaderTask.Wait();
- }
- }
- }
- public Entity.VersionManifest.Version FilteredVersion
- {
- get { return filteredVersion; }
- set {
- this.RaiseAndSetIfChanged(ref filteredVersion, value);
- InstallFabric = false;
- InstallForge = false;
- InstallOptifine = false;
- InstallForgeOptifine = false;
- try
- {
- using (var httpClient = new HttpClient())
- {
- var response = httpClient.GetAsync(@$"https://files.veloe.link/launcher/forge/Forge{value.id}/Forge{value.id}.json").Result;
- if (response.IsSuccessStatusCode)
- {
- InstallForgeVisible = true;
- response = httpClient.GetAsync(@$"https://files.veloe.link/launcher/forge/Forge{value.id}/Optifine{value.id}.jar").Result;
- if (response.IsSuccessStatusCode)
- InstallForgeOptifineVisible = true;
- }
- else
- {
- InstallForgeVisible = false;
- InstallForgeOptifineVisible = false;
- }
- response = httpClient.GetAsync(@$"https://files.veloe.link/launcher/fabric/Fabric{value.id}/Fabric{value.id}.json").Result;
- if (response.IsSuccessStatusCode)
- InstallFabricVisible = true;
- else
- installFabricVisible = false;
- response = httpClient.GetAsync(@$"https://files.veloe.link/launcher/optifine/Optifine{value.id}/Optifine{value.id}.json").Result;
- if (response.IsSuccessStatusCode)
- InstallOptifineVisible = true;
- else
- InstallOptifineVisible = false;
-
- }
- }
- catch (Exception ex)
- {
- var message = ex.Message;
- var stackTrace = ex.StackTrace;
- logger.Error(ex.Message);
- logger.Error(ex.StackTrace);
- Exception innerException = ex.InnerException;
- while (innerException is not null)
- {
- message += "\n" + innerException.Message;
- stackTrace += "\n" + innerException.StackTrace;
- logger.Error(innerException.Message);
- logger.Error(innerException.StackTrace);
- innerException = innerException.InnerException;
- }
- Dispatcher.UIThread.InvokeAsync(async () => {
- if (Avalonia.Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
- {
- var ErrorMessageViewModel = new ErrorWindowViewModel(message, stackTrace);
- var ErrorMessage = new ErrorWindow()
- {
- DataContext = ErrorMessageViewModel
- };
- ErrorMessage.ShowDialog(desktop.MainWindow);
- }
- });
- }
- }
- }
- public Modpack SelectedModpack
- {
- get => selectedModpack;
- set => this.RaiseAndSetIfChanged(ref selectedModpack, value);
- }
- public ObservableCollection<Entity.VersionManifest.Version> FilteredVersions
- {
- get => filteredVersions;
- set => this.RaiseAndSetIfChanged(ref filteredVersions, value);
- }
- public ObservableCollection<Models.Entity.Modpack> ModpackVersions
- {
- get => modpackVersions;
- set => this.RaiseAndSetIfChanged(ref modpackVersions, value);
- }
- public string StartButton
- {
- get => startButton;
- set => this.RaiseAndSetIfChanged(ref startButton, value);
- }
- public bool ShowOld
- {
- get => showOld;
- set {
- this.RaiseAndSetIfChanged(ref showOld, value);
- updateList();
- }
- }
- public bool ShowSnaps
- {
- get => showSnaps;
- set {
- this.RaiseAndSetIfChanged(ref showSnaps, value);
- updateList();
- }
- }
- public int Progress
- {
- get => progress;
- set => this.RaiseAndSetIfChanged(ref progress, value);
- }
- public string DownloadingFileName
- {
- get => downloadingFileName;
- set => this.RaiseAndSetIfChanged(ref downloadingFileName, value);
- }
- public bool InstallForge
- {
- get => installForge;
- set
- {
- this.RaiseAndSetIfChanged(ref installForge, value);
- if (InstallFabric)
- InstallFabric = false;
- }
- }
- public bool InstallFabric
- {
- get => installFabric;
- set
- {
- this.RaiseAndSetIfChanged(ref installFabric, value);
- if (InstallForge)
- InstallForge = false;
- }
- }
- public bool InstallOptifine
- {
- get => installOptifine;
- set => this.RaiseAndSetIfChanged(ref installOptifine, value);
- }
- public bool InstallForgeOptifine
- {
- get { return installForgeOptifine; }
- set
- {
- this.RaiseAndSetIfChanged(ref installForgeOptifine, value);
- if (value)
- InstallForge = true;
- }
- }
- public bool InstallForgeVisible
- {
- get => installForgeVisible;
- set => this.RaiseAndSetIfChanged(ref installForgeVisible, value);
- }
- public bool InstallFabricVisible
- {
- get => installFabricVisible;
- set => this.RaiseAndSetIfChanged(ref installFabricVisible, value);
- }
- public bool InstallOptifineVisible
- {
- get => installOptifineVisible;
- set => this.RaiseAndSetIfChanged(ref installOptifineVisible, value);
- }
- public bool InstallForgeOptifineVisible
- {
- get => installForgeOptifineVisible;
- set => this.RaiseAndSetIfChanged(ref installForgeOptifineVisible, value);
- }
- public bool DownloadJava
- {
- get => downloadJava;
- set => this.RaiseAndSetIfChanged(ref downloadJava, value);
- }
- public bool IsControlsEnabled
- {
- get => isControlsEnabled;
- set => this.RaiseAndSetIfChanged(ref isControlsEnabled, value);
- }
- public async Task OnStartBunttonClick()
- {
- Task.Run(() => {
- if (FilteredVersion is null)
- return TaskStatus.Faulted;
- logger.Debug("Downloading {0}.json", FilteredVersion.id);
- DownloadingFileName = $"{FilteredVersion.id}.json";
- var versionJson = Downloader.DownloadAndDeserializeJsonData<Entity.Version.Version>(FilteredVersion.url, Settings.MinecraftForlderPath + "versions/" + FilteredVersion.id + "/", FilteredVersion.id + ".json");
- if (versionJson is not null)
- Downloader.StartDownload(
- value => DownloadingFileName = value,
- value => IsControlsEnabled = value,
- value => value.DownloadProgressChanged += WebClient_DownloadProgressChanged,
- value => value.DownloadProgressChanged -= WebClient_DownloadProgressChanged,
- versionJson,
- DownloadJava,
- InstallForge,
- InstallOptifine,
- InstallForgeOptifine,
- InstallFabric);
- return TaskStatus.RanToCompletion; });
- }
-
- public async void OnStartModpackBunttonClick()
- {
- FilteredVersion = FilteredVersions.Where(x => x.id == SelectedModpack.version).FirstOrDefault();
- Task.Run(() => Downloader.StartDownloadModpack(
- value => DownloadingFileName = value,
- value => IsControlsEnabled = value,
- value => value.DownloadProgressChanged += WebClient_DownloadProgressChanged,
- value => value.DownloadProgressChanged -= WebClient_DownloadProgressChanged,
- FilteredVersions,
- SelectedModpack,
- DownloadJava,
- InstallFabric = SelectedModpack.fabric,
- InstallForge = SelectedModpack.forge,
- InstallOptifine = SelectedModpack.optifine,
- InstallForgeOptifine = SelectedModpack.forgeoptifine));
- }
- private void WebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
- {
- Progress = e.ProgressPercentage;
- }
- public void updateList()
- {
- try
- {
- FilteredVersions.Clear();
- if (versionManifest.versions is null)
- return;
- foreach (var version in versionManifest.versions)
- {
- if (ShowSnaps && version.type == "snapshot" || ShowOld && version.type is ("old_alpha" or "old_beta") || version.type == "release")
- FilteredVersions.Add(version);
- }
- }
- catch (Exception ex)
- {
- var message = ex.Message;
- var stackTrace = ex.StackTrace;
- logger.Error(ex.Message);
- logger.Error(ex.StackTrace);
- Exception innerException = ex.InnerException;
- while (innerException is not null)
- {
- message += "\n" + innerException.Message;
- stackTrace += "\n" + innerException.StackTrace;
- logger.Error(innerException.Message);
- logger.Error(innerException.StackTrace);
- innerException = innerException.InnerException;
- }
- if (Avalonia.Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
- {
- var ErrorMessageViewModel = new ErrorWindowViewModel(message, stackTrace);
- var ErrorMessage = new ErrorWindow()
- {
- DataContext = ErrorMessageViewModel
- };
- ErrorMessage.ShowDialog(desktop.MainWindow);
- }
- }
- }
- }
- }
|