VersionsDownloaderViewModel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. using Avalonia.Controls.ApplicationLifetimes;
  2. using Avalonia.Threading;
  3. using DynamicData;
  4. using ReactiveUI;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.Linq;
  9. using System.Net;
  10. using System.Net.Http;
  11. using System.Threading.Tasks;
  12. using VeloeMinecraftLauncher.Entity.VersionManifest;
  13. using VeloeMinecraftLauncher.MinecraftLauncher;
  14. using VeloeMinecraftLauncher.Models.Entity;
  15. using VeloeMinecraftLauncher.Views;
  16. namespace VeloeMinecraftLauncher.ViewModels
  17. {
  18. public class VersionsDownloaderViewModel : ViewModelBase
  19. {
  20. private string startButton = "Download";
  21. private bool showOld = false;
  22. private bool showSnaps = false;
  23. private bool installFabric = false;
  24. private bool installForge = false;
  25. private bool installOptifine = false;
  26. private bool installForgeOptifine = false;
  27. private bool installFabricVisible = false;
  28. private bool installForgeVisible = false;
  29. private bool installOptifineVisible = false;
  30. private bool installForgeOptifineVisible = false;
  31. private bool downloadJava = false;
  32. private bool isControlsEnabled = true;
  33. private int progress = 0;
  34. private string downloadingFileName;
  35. Serilog.ILogger logger;
  36. ObservableCollection<Entity.VersionManifest.Version> filteredVersions;
  37. ObservableCollection<Modpack> modpackVersions;
  38. Entity.VersionManifest.Version filteredVersion;
  39. Modpack selectedModpack;
  40. VersionManifest versionManifest;
  41. public VersionsDownloaderViewModel()
  42. {
  43. try
  44. {
  45. Task.Run(() =>
  46. {
  47. logger = Settings.logger;
  48. if (FilteredVersions is null)
  49. {
  50. FilteredVersions = new();
  51. }
  52. if (ModpackVersions is null)
  53. {
  54. ModpackVersions = new();
  55. }
  56. logger.Debug("Getting versionManifest.json");
  57. versionManifest = Downloader.DownloadAndDeserializeJsonData<VersionManifest>("https://launchermeta.mojang.com/mc/game/version_manifest_v2.json");
  58. ModpackVersions.AddRange(Downloader.DownloadAndDeserializeJsonData<List<Modpack>>("https://files.veloe.link/launcher/modpacks.json"));
  59. logger.Debug("Updating available versions to download.");
  60. updateList();
  61. });
  62. }
  63. catch (Exception ex)
  64. {
  65. var message = ex.Message;
  66. var stackTrace = ex.StackTrace;
  67. Settings.logger.Error(ex.Message);
  68. Settings.logger.Error(ex.StackTrace);
  69. Exception innerException = ex.InnerException;
  70. while (innerException is not null)
  71. {
  72. message += "\n" + innerException.Message;
  73. stackTrace += "\n" + innerException.StackTrace;
  74. Settings.logger.Error(innerException.Message);
  75. Settings.logger.Error(innerException.StackTrace);
  76. innerException = innerException.InnerException;
  77. }
  78. if (Avalonia.Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
  79. {
  80. var ErrorMessageViewModel = new ErrorWindowViewModel(message, stackTrace);
  81. var ErrorMessage = new ErrorWindow()
  82. {
  83. DataContext = ErrorMessageViewModel
  84. };
  85. //var versionsDownloaderTask =
  86. ErrorMessage.ShowDialog(desktop.MainWindow);
  87. //versionsDownloaderTask.Wait();
  88. }
  89. }
  90. }
  91. public Entity.VersionManifest.Version FilteredVersion
  92. {
  93. get { return filteredVersion; }
  94. set {
  95. this.RaiseAndSetIfChanged(ref filteredVersion, value);
  96. InstallFabric = false;
  97. InstallForge = false;
  98. InstallOptifine = false;
  99. InstallForgeOptifine = false;
  100. try
  101. {
  102. using (var httpClient = new HttpClient())
  103. {
  104. var response = httpClient.GetAsync(@$"https://files.veloe.link/launcher/forge/Forge{value.id}/Forge{value.id}.json").Result;
  105. if (response.IsSuccessStatusCode)
  106. {
  107. InstallForgeVisible = true;
  108. response = httpClient.GetAsync(@$"https://files.veloe.link/launcher/forge/Forge{value.id}/Optifine{value.id}.jar").Result;
  109. if (response.IsSuccessStatusCode)
  110. InstallForgeOptifineVisible = true;
  111. }
  112. else
  113. {
  114. InstallForgeVisible = false;
  115. InstallForgeOptifineVisible = false;
  116. }
  117. response = httpClient.GetAsync(@$"https://files.veloe.link/launcher/fabric/Fabric{value.id}/Fabric{value.id}.json").Result;
  118. if (response.IsSuccessStatusCode)
  119. InstallFabricVisible = true;
  120. else
  121. installFabricVisible = false;
  122. response = httpClient.GetAsync(@$"https://files.veloe.link/launcher/optifine/Optifine{value.id}/Optifine{value.id}.json").Result;
  123. if (response.IsSuccessStatusCode)
  124. InstallOptifineVisible = true;
  125. else
  126. InstallOptifineVisible = false;
  127. }
  128. }
  129. catch (Exception ex)
  130. {
  131. var message = ex.Message;
  132. var stackTrace = ex.StackTrace;
  133. logger.Error(ex.Message);
  134. logger.Error(ex.StackTrace);
  135. Exception innerException = ex.InnerException;
  136. while (innerException is not null)
  137. {
  138. message += "\n" + innerException.Message;
  139. stackTrace += "\n" + innerException.StackTrace;
  140. logger.Error(innerException.Message);
  141. logger.Error(innerException.StackTrace);
  142. innerException = innerException.InnerException;
  143. }
  144. Dispatcher.UIThread.InvokeAsync(async () => {
  145. if (Avalonia.Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
  146. {
  147. var ErrorMessageViewModel = new ErrorWindowViewModel(message, stackTrace);
  148. var ErrorMessage = new ErrorWindow()
  149. {
  150. DataContext = ErrorMessageViewModel
  151. };
  152. ErrorMessage.ShowDialog(desktop.MainWindow);
  153. }
  154. });
  155. }
  156. }
  157. }
  158. public Modpack SelectedModpack
  159. {
  160. get => selectedModpack;
  161. set => this.RaiseAndSetIfChanged(ref selectedModpack, value);
  162. }
  163. public ObservableCollection<Entity.VersionManifest.Version> FilteredVersions
  164. {
  165. get => filteredVersions;
  166. set => this.RaiseAndSetIfChanged(ref filteredVersions, value);
  167. }
  168. public ObservableCollection<Models.Entity.Modpack> ModpackVersions
  169. {
  170. get => modpackVersions;
  171. set => this.RaiseAndSetIfChanged(ref modpackVersions, value);
  172. }
  173. public string StartButton
  174. {
  175. get => startButton;
  176. set => this.RaiseAndSetIfChanged(ref startButton, value);
  177. }
  178. public bool ShowOld
  179. {
  180. get => showOld;
  181. set {
  182. this.RaiseAndSetIfChanged(ref showOld, value);
  183. updateList();
  184. }
  185. }
  186. public bool ShowSnaps
  187. {
  188. get => showSnaps;
  189. set {
  190. this.RaiseAndSetIfChanged(ref showSnaps, value);
  191. updateList();
  192. }
  193. }
  194. public int Progress
  195. {
  196. get => progress;
  197. set => this.RaiseAndSetIfChanged(ref progress, value);
  198. }
  199. public string DownloadingFileName
  200. {
  201. get => downloadingFileName;
  202. set => this.RaiseAndSetIfChanged(ref downloadingFileName, value);
  203. }
  204. public bool InstallForge
  205. {
  206. get => installForge;
  207. set
  208. {
  209. this.RaiseAndSetIfChanged(ref installForge, value);
  210. if (InstallFabric)
  211. InstallFabric = false;
  212. }
  213. }
  214. public bool InstallFabric
  215. {
  216. get => installFabric;
  217. set
  218. {
  219. this.RaiseAndSetIfChanged(ref installFabric, value);
  220. if (InstallForge)
  221. InstallForge = false;
  222. }
  223. }
  224. public bool InstallOptifine
  225. {
  226. get => installOptifine;
  227. set => this.RaiseAndSetIfChanged(ref installOptifine, value);
  228. }
  229. public bool InstallForgeOptifine
  230. {
  231. get { return installForgeOptifine; }
  232. set
  233. {
  234. this.RaiseAndSetIfChanged(ref installForgeOptifine, value);
  235. if (value)
  236. InstallForge = true;
  237. }
  238. }
  239. public bool InstallForgeVisible
  240. {
  241. get => installForgeVisible;
  242. set => this.RaiseAndSetIfChanged(ref installForgeVisible, value);
  243. }
  244. public bool InstallFabricVisible
  245. {
  246. get => installFabricVisible;
  247. set => this.RaiseAndSetIfChanged(ref installFabricVisible, value);
  248. }
  249. public bool InstallOptifineVisible
  250. {
  251. get => installOptifineVisible;
  252. set => this.RaiseAndSetIfChanged(ref installOptifineVisible, value);
  253. }
  254. public bool InstallForgeOptifineVisible
  255. {
  256. get => installForgeOptifineVisible;
  257. set => this.RaiseAndSetIfChanged(ref installForgeOptifineVisible, value);
  258. }
  259. public bool DownloadJava
  260. {
  261. get => downloadJava;
  262. set => this.RaiseAndSetIfChanged(ref downloadJava, value);
  263. }
  264. public bool IsControlsEnabled
  265. {
  266. get => isControlsEnabled;
  267. set => this.RaiseAndSetIfChanged(ref isControlsEnabled, value);
  268. }
  269. public async Task OnStartBunttonClick()
  270. {
  271. Task.Run(() => {
  272. if (FilteredVersion is null)
  273. return TaskStatus.Faulted;
  274. logger.Debug("Downloading {0}.json", FilteredVersion.id);
  275. DownloadingFileName = $"{FilteredVersion.id}.json";
  276. var versionJson = Downloader.DownloadAndDeserializeJsonData<Entity.Version.Version>(FilteredVersion.url, Settings.MinecraftForlderPath + "versions/" + FilteredVersion.id + "/", FilteredVersion.id + ".json");
  277. if (versionJson is not null)
  278. Downloader.StartDownload(
  279. value => DownloadingFileName = value,
  280. value => IsControlsEnabled = value,
  281. value => value.DownloadProgressChanged += WebClient_DownloadProgressChanged,
  282. value => value.DownloadProgressChanged -= WebClient_DownloadProgressChanged,
  283. versionJson,
  284. DownloadJava,
  285. InstallForge,
  286. InstallOptifine,
  287. InstallForgeOptifine,
  288. InstallFabric);
  289. return TaskStatus.RanToCompletion; });
  290. }
  291. public async void OnStartModpackBunttonClick()
  292. {
  293. FilteredVersion = FilteredVersions.Where(x => x.id == SelectedModpack.version).FirstOrDefault();
  294. Task.Run(() => Downloader.StartDownloadModpack(
  295. value => DownloadingFileName = value,
  296. value => IsControlsEnabled = value,
  297. value => value.DownloadProgressChanged += WebClient_DownloadProgressChanged,
  298. value => value.DownloadProgressChanged -= WebClient_DownloadProgressChanged,
  299. FilteredVersions,
  300. SelectedModpack,
  301. DownloadJava,
  302. InstallFabric = SelectedModpack.fabric,
  303. InstallForge = SelectedModpack.forge,
  304. InstallOptifine = SelectedModpack.optifine,
  305. InstallForgeOptifine = SelectedModpack.forgeoptifine));
  306. }
  307. private void WebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  308. {
  309. Progress = e.ProgressPercentage;
  310. }
  311. public void updateList()
  312. {
  313. try
  314. {
  315. FilteredVersions.Clear();
  316. if (versionManifest.versions is null)
  317. return;
  318. foreach (var version in versionManifest.versions)
  319. {
  320. if (ShowSnaps && version.type == "snapshot" || ShowOld && version.type is ("old_alpha" or "old_beta") || version.type == "release")
  321. FilteredVersions.Add(version);
  322. }
  323. }
  324. catch (Exception ex)
  325. {
  326. var message = ex.Message;
  327. var stackTrace = ex.StackTrace;
  328. logger.Error(ex.Message);
  329. logger.Error(ex.StackTrace);
  330. Exception innerException = ex.InnerException;
  331. while (innerException is not null)
  332. {
  333. message += "\n" + innerException.Message;
  334. stackTrace += "\n" + innerException.StackTrace;
  335. logger.Error(innerException.Message);
  336. logger.Error(innerException.StackTrace);
  337. innerException = innerException.InnerException;
  338. }
  339. if (Avalonia.Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
  340. {
  341. var ErrorMessageViewModel = new ErrorWindowViewModel(message, stackTrace);
  342. var ErrorMessage = new ErrorWindow()
  343. {
  344. DataContext = ErrorMessageViewModel
  345. };
  346. ErrorMessage.ShowDialog(desktop.MainWindow);
  347. }
  348. }
  349. }
  350. }
  351. }