VersionsDownloaderViewModel.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. using ReactiveUI;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.IO;
  6. using System.IO.Compression;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Net.Http;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using VeloeMinecraftLauncher.Entity.Assets;
  13. using VeloeMinecraftLauncher.Entity.VersionManifest;
  14. using VeloeMinecraftLauncher.MinecraftLauncher;
  15. namespace VeloeMinecraftLauncher.ViewModels
  16. {
  17. public class VersionsDownloaderViewModel : ViewModelBase
  18. {
  19. int i = 0;
  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. Task downloadMinecraftTask;
  36. Serilog.ILogger logger;
  37. ObservableCollection<Entity.VersionManifest.Version> filteredVersions;
  38. Entity.VersionManifest.Version filteredVersion;
  39. VersionManifest versionManifest;
  40. public VersionsDownloaderViewModel()
  41. {
  42. logger = Settings.logger;
  43. if (FilteredVerions is null)
  44. {
  45. FilteredVerions = new ObservableCollection<Entity.VersionManifest.Version>();
  46. }
  47. logger.Debug("Getting versionManifest.json");
  48. versionManifest = Downloader.DownloadAndDeserializeJsonData<VersionManifest>("https://launchermeta.mojang.com/mc/game/version_manifest_v2.json");
  49. logger.Debug("Updating available versions to download.");
  50. updateList();
  51. }
  52. public Entity.VersionManifest.Version FilteredVersion
  53. {
  54. get { return filteredVersion; }
  55. set {
  56. this.RaiseAndSetIfChanged(ref filteredVersion, value);
  57. InstallFabric = false;
  58. InstallForge = false;
  59. InstallOptifine = false;
  60. InstallForgeOptifine = false;
  61. using (var httpClient = new HttpClient())
  62. {
  63. try
  64. {
  65. var response = httpClient.GetAsync(@$"https://files.veloe.link/launcher/forge/Forge{value.id}/Forge{value.id}.json").Result;
  66. if (response.IsSuccessStatusCode)
  67. {
  68. InstallForgeVisible = true;
  69. response = httpClient.GetAsync(@$"https://files.veloe.link/launcher/forge/Forge{value.id}/Optifine{value.id}.jar").Result;
  70. if (response.IsSuccessStatusCode)
  71. InstallForgeOptifineVisible = true;
  72. }
  73. else
  74. {
  75. InstallForgeVisible = false;
  76. InstallForgeOptifineVisible = false;
  77. }
  78. response = httpClient.GetAsync(@$"https://files.veloe.link/launcher/fabric/Fabric{value.id}/Fabric{value.id}.json").Result;
  79. if (response.IsSuccessStatusCode)
  80. InstallFabricVisible = true;
  81. else
  82. installFabricVisible = false;
  83. response = httpClient.GetAsync(@$"https://files.veloe.link/launcher/optifine/Optifine{value.id}/Optifine{value.id}.json").Result;
  84. if (response.IsSuccessStatusCode)
  85. InstallOptifineVisible = true;
  86. else
  87. InstallOptifineVisible = false;
  88. }
  89. catch (Exception ex)
  90. {
  91. }
  92. }
  93. }
  94. }
  95. public ObservableCollection<Entity.VersionManifest.Version>FilteredVerions
  96. {
  97. get => filteredVersions; set
  98. {
  99. this.RaiseAndSetIfChanged(ref filteredVersions, value);
  100. }
  101. }
  102. public string StartButton
  103. {
  104. get => startButton; set
  105. {
  106. this.RaiseAndSetIfChanged(ref startButton, value);
  107. }
  108. }
  109. public bool ShowOld
  110. {
  111. get { return showOld; }
  112. set {
  113. this.RaiseAndSetIfChanged(ref showOld, value);
  114. updateList();
  115. }
  116. }
  117. public bool ShowSnaps
  118. {
  119. get { return showSnaps; }
  120. set {
  121. this.RaiseAndSetIfChanged(ref showSnaps, value);
  122. updateList();
  123. }
  124. }
  125. public int Progress
  126. {
  127. get { return progress; }
  128. set
  129. {
  130. this.RaiseAndSetIfChanged(ref progress, value);
  131. }
  132. }
  133. public string DownloadingFileName
  134. {
  135. get { return downloadingFileName; }
  136. set
  137. {
  138. this.RaiseAndSetIfChanged(ref downloadingFileName, value);
  139. }
  140. }
  141. public bool InstallForge
  142. {
  143. get { return installForge; }
  144. set {
  145. this.RaiseAndSetIfChanged(ref installForge, value);
  146. if (InstallFabric)
  147. InstallFabric = false;
  148. }
  149. }
  150. public bool InstallFabric
  151. {
  152. get { return installFabric; }
  153. set {
  154. this.RaiseAndSetIfChanged(ref installFabric, value);
  155. if (InstallForge)
  156. InstallForge = false;
  157. }
  158. }
  159. public bool InstallOptifine
  160. {
  161. get { return installOptifine; }
  162. set {
  163. this.RaiseAndSetIfChanged(ref installOptifine, value);
  164. }
  165. }
  166. public bool InstallForgeOptifine
  167. {
  168. get { return installForgeOptifine; }
  169. set
  170. {
  171. this.RaiseAndSetIfChanged(ref installForgeOptifine, value);
  172. if (value)
  173. InstallForge = true;
  174. }
  175. }
  176. public bool InstallForgeVisible
  177. {
  178. get { return installForgeVisible; }
  179. set => this.RaiseAndSetIfChanged(ref installForgeVisible, value);
  180. }
  181. public bool InstallFabricVisible
  182. {
  183. get { return installFabricVisible; }
  184. set => this.RaiseAndSetIfChanged(ref installFabricVisible, value);
  185. }
  186. public bool InstallOptifineVisible
  187. {
  188. get { return installOptifineVisible; }
  189. set => this.RaiseAndSetIfChanged(ref installOptifineVisible, value);
  190. }
  191. public bool InstallForgeOptifineVisible
  192. {
  193. get { return installForgeOptifineVisible; }
  194. set => this.RaiseAndSetIfChanged(ref installForgeOptifineVisible, value);
  195. }
  196. public bool DownloadJava
  197. {
  198. get { return downloadJava; }
  199. set { this.RaiseAndSetIfChanged(ref downloadJava, value); }
  200. }
  201. public bool IsControlsEnabled
  202. {
  203. get { return isControlsEnabled; }
  204. set { this.RaiseAndSetIfChanged(ref isControlsEnabled, value); }
  205. }
  206. public async Task OnStartBunttonClick()
  207. {
  208. Task.Run(()=>StartDownload());
  209. }
  210. public async Task<TaskStatus> StartDownload()
  211. {
  212. try
  213. {
  214. if (FilteredVersion is null)
  215. return TaskStatus.Faulted;
  216. IsControlsEnabled = false;
  217. logger.Debug("Downloading minecraft {0}", FilteredVersion.id);
  218. var webClient = new WebClient();
  219. logger.Debug("Downloading {0}.json", FilteredVersion.id);
  220. var versonJson = Downloader.DownloadAndDeserializeJsonData<Entity.Version.Version>(FilteredVersion.url);
  221. if (versonJson != null)
  222. {
  223. webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged;
  224. //download java
  225. if (DownloadJava)
  226. {
  227. logger.Debug("Getting required Java version.");
  228. if (versonJson.javaVersion.majorVersion != null)
  229. {
  230. string javaUrl = "";
  231. switch (versonJson.javaVersion.majorVersion)
  232. {
  233. case 8:
  234. if (Environment.Is64BitOperatingSystem && OperatingSystem.IsWindows())
  235. javaUrl = "https://files.veloe.link/launcher/java/8/windows64/java.zip";
  236. if (!Environment.Is64BitOperatingSystem && OperatingSystem.IsWindows())
  237. javaUrl = "https://files.veloe.link/launcher/java/8/windows32/java.zip";
  238. if (Environment.Is64BitOperatingSystem && OperatingSystem.IsLinux())
  239. javaUrl = "https://files.veloe.link/launcher/java/8/linux64/java.zip";
  240. if (!Environment.Is64BitOperatingSystem && OperatingSystem.IsLinux())
  241. javaUrl = "https://files.veloe.link/launcher/java/8/linux32/java.zip";
  242. break;
  243. case 16:
  244. if (Environment.Is64BitOperatingSystem && OperatingSystem.IsWindows())
  245. javaUrl = "https://files.veloe.link/launcher/java/16/windows64/java.zip";
  246. if (Environment.Is64BitOperatingSystem && OperatingSystem.IsLinux())
  247. javaUrl = "https://files.veloe.link/launcher/java/16/linux64/java.zip";
  248. break;
  249. case 17:
  250. if (Environment.Is64BitOperatingSystem && OperatingSystem.IsWindows())
  251. javaUrl = "https://files.veloe.link/launcher/java/17/windows64/java.zip";
  252. if (Environment.Is64BitOperatingSystem && OperatingSystem.IsLinux())
  253. javaUrl = "https://files.veloe.link/launcher/java/17/linux64/java.zip";
  254. break;
  255. case 18:
  256. if (Environment.Is64BitOperatingSystem && OperatingSystem.IsWindows())
  257. javaUrl = "https://files.veloe.link/launcher/java/18/windows64/java.zip";
  258. if (Environment.Is64BitOperatingSystem && OperatingSystem.IsLinux())
  259. javaUrl = "https://files.veloe.link/launcher/java/18/linux64/java.zip";
  260. break;
  261. }
  262. if (javaUrl != "")
  263. {
  264. logger.Debug("Downloading Java");
  265. DownloadingFileName = "java.zip";
  266. webClient.DownloadFileAsync(new System.Uri(javaUrl), Settings.MinecraftForlderPath + "java.zip");
  267. waitWhileBisy(ref webClient);
  268. logger.Debug("Unpacking Java");
  269. DownloadingFileName = "Unpacking java.zip";
  270. ZipFile.ExtractToDirectory($"{Settings.MinecraftForlderPath}java.zip", Settings.MinecraftForlderPath, true);
  271. File.Delete($"{Settings.MinecraftForlderPath}java.zip");
  272. }
  273. else
  274. logger.Debug("Required Java version don't found in json file."); //log that java was not downloaded;
  275. }
  276. }
  277. //download json
  278. var path = Settings.MinecraftForlderPath + "/versions/" + FilteredVersion.id;
  279. if (!Directory.Exists(path))
  280. {
  281. logger.Debug("Creating path: {0}", path);
  282. Directory.CreateDirectory(path);
  283. }
  284. logger.Debug("Downloading {0}", $"{FilteredVersion.id}.json");
  285. DownloadingFileName = $"{FilteredVersion.id}.json";
  286. webClient.DownloadFileAsync(new System.Uri(FilteredVersion.url), path + "/" + FilteredVersion.id + ".json");
  287. waitWhileBisy(ref webClient);
  288. //download jar
  289. if (!Directory.Exists(path))
  290. {
  291. logger.Debug("Creating path: {0}", path);
  292. Directory.CreateDirectory(path);
  293. }
  294. logger.Debug("Downloading {0}", $"{FilteredVersion.id}.jar");
  295. DownloadingFileName = $"{FilteredVersion.id}.jar";
  296. webClient.DownloadFileAsync(new System.Uri(versonJson.downloads.client.url), path + "/" + FilteredVersion.id + ".jar");
  297. waitWhileBisy(ref webClient);
  298. //download libraries
  299. logger.Debug("Downloading libraries.");
  300. foreach (var library in versonJson.libraries)
  301. {
  302. var libPath = Settings.MinecraftForlderPath + "libraries/";
  303. string[] libPathSplit = new[] { "" };
  304. string libUrl = "";
  305. // getting path and url if universal lib
  306. if (library.natives is null)
  307. {
  308. libPath = Settings.MinecraftForlderPath + "libraries/";
  309. libPathSplit = library.downloads.artifact.path.Split('/');
  310. libUrl = library.downloads.artifact.url;
  311. for (int i = 0; i < libPathSplit.Length - 1; i++)
  312. {
  313. libPath += libPathSplit[i] + "/";
  314. }
  315. }
  316. else
  317. { // getting path if native
  318. libPath = Settings.MinecraftForlderPath + "libraries/";
  319. libPathSplit = new[] { "" };
  320. libUrl = "";
  321. if (OperatingSystem.IsWindows() && library.natives.windows is not null)
  322. {
  323. if (library.downloads.classifiers.NativesWindows is not null)
  324. {
  325. libPathSplit = library.downloads.classifiers.NativesWindows.path.Split('/');
  326. libUrl = library.downloads.classifiers.NativesWindows.url;
  327. }
  328. else
  329. {
  330. if (Environment.Is64BitOperatingSystem)
  331. {
  332. libPathSplit = library.downloads.classifiers.NativesWindows64.path.Split('/');
  333. libUrl = library.downloads.classifiers.NativesWindows64.url;
  334. }
  335. else
  336. {
  337. libPathSplit = library.downloads.classifiers.NativesWindows32.path.Split('/');
  338. libUrl = library.downloads.classifiers.NativesWindows32.url;
  339. }
  340. }
  341. }
  342. if (OperatingSystem.IsLinux() && library.natives.linux is not null)
  343. {
  344. libPathSplit = library.downloads.classifiers.NativesLinux.path.Split('/');
  345. libUrl = library.downloads.classifiers.NativesLinux.url;
  346. }
  347. for (int i = 0; i < libPathSplit.Length - 1; i++)
  348. {
  349. libPath += libPathSplit[i] + "/";
  350. }
  351. }
  352. //if no lib url
  353. if (libUrl == String.Empty)
  354. continue;
  355. //checking rules
  356. if (library.rules == null)
  357. {
  358. if (!Directory.Exists(libPath))
  359. {
  360. logger.Debug("Creating path: {0}", path);
  361. Directory.CreateDirectory(libPath);
  362. }
  363. logger.Debug("Downloading: {0}", libPathSplit.Last());
  364. DownloadingFileName = libPathSplit.Last();
  365. webClient.DownloadFileAsync(new System.Uri(libUrl), libPath + "/" + libPathSplit.Last());
  366. waitWhileBisy(ref webClient);
  367. }
  368. else
  369. {
  370. foreach (var rule in library.rules)
  371. {
  372. if (rule.action == "allow" && rule.os is null)
  373. {
  374. if (!Directory.Exists(libPath))
  375. {
  376. logger.Debug("Creating path: {0}", path);
  377. Directory.CreateDirectory(libPath);
  378. }
  379. logger.Debug("Downloading: {0}", libPathSplit.Last());
  380. DownloadingFileName = libPathSplit.Last();
  381. webClient.DownloadFileAsync(new System.Uri(libUrl), libPath + "/" + libPathSplit.Last());
  382. waitWhileBisy(ref webClient);
  383. continue;
  384. }
  385. if (rule.action == "allow" && (rule.os.name == "windows" && OperatingSystem.IsWindows() || rule.os.name == "linux" && OperatingSystem.IsLinux()))
  386. {
  387. if (!Directory.Exists(libPath))
  388. {
  389. logger.Debug("Creating path: {0}", path);
  390. Directory.CreateDirectory(libPath);
  391. }
  392. logger.Debug("Downloading: {0}", libPathSplit.Last());
  393. DownloadingFileName = libPathSplit.Last();
  394. webClient.DownloadFileAsync(new System.Uri(libUrl), libPath + "/" + libPathSplit.Last());
  395. waitWhileBisy(ref webClient);
  396. }
  397. }
  398. }
  399. //unpacking native libs
  400. if (library.natives is not null || library.downloads.classifiers is not null)
  401. {
  402. try
  403. {
  404. if (!Directory.Exists(Settings.MinecraftForlderPath + "versions/" + versonJson.id + "/natives/"))
  405. {
  406. logger.Debug("Creating path: {0}", Settings.MinecraftForlderPath + "versions/" + versonJson.id + "/natives/");
  407. Directory.CreateDirectory(Settings.MinecraftForlderPath + "versions/" + versonJson.id + "/natives/");
  408. }
  409. logger.Debug("Extracting {0} to {1}", libPathSplit.Last(), Settings.MinecraftForlderPath + "versions/" + versonJson.id + "/natives/");
  410. ZipFile.ExtractToDirectory(libPath + "/" + libPathSplit.Last(), Settings.MinecraftForlderPath + "versions/" + versonJson.id + "/natives/", true);
  411. }
  412. catch (IOException ex)
  413. {
  414. logger.Error("IOException in VersionsDownloaderViewModel on native lib extraction");
  415. }
  416. //TODO delete META-INF and sha1 files after
  417. }
  418. }
  419. var assetsJson = Downloader.DownloadAndDeserializeJsonData<AssetsManifest>(versonJson.assetIndex.url);
  420. var assetsPath = Settings.MinecraftForlderPath + "assets/" + versonJson.assets + "/objects";
  421. var assetsUrl = "https://resources.download.minecraft.net/";
  422. //download assets json
  423. logger.Debug("Downloading: {0}", versonJson.assets + ".json");
  424. Downloader.DownloadFile(versonJson.assetIndex.url, Settings.MinecraftForlderPath + "/assets/" + versonJson.assets + "/indexes/", versonJson.assets + ".json");
  425. //download assets
  426. foreach (var asset in assetsJson.Objects)
  427. {
  428. var folder = asset.Value.hash.Substring(0, 2);
  429. if (!Directory.Exists(assetsPath + "/" + folder))
  430. Directory.CreateDirectory(assetsPath + "/" + folder);
  431. string chksum = String.Empty;
  432. //here hash check
  433. if (File.Exists(assetsPath + "/" + folder + "/" + asset.Value.hash))
  434. {
  435. FileStream fop = File.OpenRead(assetsPath + "/" + folder + "/" + asset.Value.hash);
  436. byte[] hash = System.Security.Cryptography.SHA1.Create().ComputeHash(fop);
  437. chksum = BitConverter.ToString(hash).Replace("-", String.Empty).ToLower();
  438. fop.Close();
  439. fop.Dispose();
  440. }
  441. if (chksum != asset.Value.hash)
  442. {
  443. logger.Debug("Downloading: {0}", asset.Value.hash);
  444. DownloadingFileName = $"Asset {asset.Value.hash}";
  445. webClient.DownloadFileAsync(new System.Uri(assetsUrl + folder + "/" + asset.Value.hash), assetsPath + "/" + folder + "/" + asset.Value.hash);
  446. waitWhileBisy(ref webClient);
  447. }
  448. }
  449. if (InstallForge)
  450. {
  451. var forgePath = $"Forge{versonJson.id}";
  452. var forgeUrl = $"https://files.veloe.link/launcher/forge/Forge{versonJson.id}/";
  453. if (!Directory.Exists($"{Settings.MinecraftForlderPath}versions/Forge{versonJson.id}"))
  454. {
  455. logger.Debug("Creating path: {0}", $"{Settings.MinecraftForlderPath}versions/" + forgePath);
  456. Directory.CreateDirectory($"{Settings.MinecraftForlderPath}versions/" + forgePath);
  457. }
  458. logger.Debug("Downloading: {0}", $"Forge{versonJson.id}.json");
  459. DownloadingFileName = $"Forge{versonJson.id}.json";
  460. webClient.DownloadFileAsync(new System.Uri($"{forgeUrl}Forge{versonJson.id}.json"), Settings.MinecraftForlderPath + "versions/" + forgePath + "/" + forgePath + ".json");
  461. waitWhileBisy(ref webClient);
  462. logger.Debug("Downloading: {0}", "libraries.zip");
  463. DownloadingFileName = "libraries.zip";
  464. webClient.DownloadFileAsync(new System.Uri(forgeUrl + "libraries.zip"), Settings.MinecraftForlderPath + "versions/" + forgePath + "/libraries.zip");
  465. waitWhileBisy(ref webClient);
  466. logger.Debug("Extracting: {0}", "libraries.zip");
  467. DownloadingFileName = "Unpacking libraries.zip";
  468. ZipFile.ExtractToDirectory($"{Settings.MinecraftForlderPath}versions/Forge{versonJson.id}/libraries.zip", Settings.MinecraftForlderPath, true);
  469. File.Delete($"{Settings.MinecraftForlderPath}versions/Forge{versonJson.id}/libraries.zip");
  470. }
  471. if (InstallOptifine)
  472. {
  473. var optifinePath = $"Optifine{versonJson.id}";
  474. var optifineUrl = $"https://files.veloe.link/launcher/optifine/Optifine{versonJson.id}/";
  475. if (!Directory.Exists($"{Settings.MinecraftForlderPath}versions/Optifine{versonJson.id}"))
  476. {
  477. logger.Debug("Creating path: {0}", $"{Settings.MinecraftForlderPath}Optifine/" + optifinePath);
  478. Directory.CreateDirectory($"{Settings.MinecraftForlderPath}Optifine/" + optifinePath);
  479. }
  480. logger.Debug("Downloading: {0}", $"Optifine{versonJson.id}.json");
  481. DownloadingFileName = $"Optifine{versonJson.id}.json";
  482. webClient.DownloadFileAsync(new System.Uri($"{optifineUrl}Optifine{versonJson.id}.json"), Settings.MinecraftForlderPath + "versions/" + optifinePath + "/" + optifinePath + ".json");
  483. waitWhileBisy(ref webClient);
  484. logger.Debug("Downloading: {0}", $"Optifine{versonJson.id}.json");
  485. DownloadingFileName = $"Optifine{versonJson.id}.json";
  486. webClient.DownloadFileAsync(new System.Uri($"{optifineUrl}Optifine{versonJson.id}.jar"), Settings.MinecraftForlderPath + "versions/" + optifinePath + "/" + optifinePath + ".jar");
  487. waitWhileBisy(ref webClient);
  488. logger.Debug("Downloading: {0}", "libraries.zip");
  489. DownloadingFileName = "libraries.zip";
  490. webClient.DownloadFileAsync(new System.Uri(optifineUrl + "libraries.zip"), Settings.MinecraftForlderPath + "versions/" + optifinePath + "/libraries.zip");
  491. waitWhileBisy(ref webClient);
  492. logger.Debug("Extracting: {0}", "libraries.zip");
  493. DownloadingFileName = "Unpacking libraries.zip";
  494. ZipFile.ExtractToDirectory($"{Settings.MinecraftForlderPath}versions/Optifine{versonJson.id}/libraries.zip", Settings.MinecraftForlderPath, true);
  495. File.Delete($"{Settings.MinecraftForlderPath}versions/Optifine{versonJson.id}/libraries.zip");
  496. }
  497. if (InstallForgeOptifine)
  498. {
  499. if (!Directory.Exists($"{Settings.MinecraftForlderPath}versions/Forge{versonJson.id}/mods"))
  500. {
  501. logger.Debug("Creating path: {0}", $"{Settings.MinecraftForlderPath}versions/Forge" + versonJson.id + "/mods");
  502. Directory.CreateDirectory($"{Settings.MinecraftForlderPath}versions/Forge" + versonJson.id + "/mods");
  503. }
  504. logger.Debug("Downloading: {0}", $"Optifine{versonJson.id}.jar");
  505. DownloadingFileName = $"Optifine{versonJson.id}.jar";
  506. webClient.DownloadFileAsync(new System.Uri(@$"https://files.veloe.link/launcher/forge/Forge{versonJson.id}/Optifine{versonJson.id}.jar"),
  507. Settings.MinecraftForlderPath + "versions/Forge" + versonJson.id + "/mods/" + "Optifine" + versonJson.id + ".jar");
  508. waitWhileBisy(ref webClient);
  509. }
  510. webClient.DownloadProgressChanged -= WebClient_DownloadProgressChanged;
  511. Progress = 100;
  512. logger.Debug("Downloading finished.");
  513. DownloadingFileName = "Finished!";
  514. IsControlsEnabled = true;
  515. }
  516. webClient.Dispose();
  517. }
  518. catch (Exception ex)
  519. {
  520. IsControlsEnabled = true;
  521. DownloadingFileName = $"Error occured while downloading {FilteredVersion.id}.";
  522. logger.Error(ex.Message);
  523. logger.Error(ex.StackTrace);
  524. return TaskStatus.Faulted;
  525. }
  526. return TaskStatus.RanToCompletion;
  527. }
  528. public async void OnStartMcTfcBunttonClick()
  529. {
  530. Task.Run(() => StartDownloadMcTfc());
  531. }
  532. private async Task<TaskStatus> StartDownloadMcTfc()
  533. {
  534. try
  535. {
  536. FilteredVersion = FilteredVerions.Where(x => x.id == "1.12.2").FirstOrDefault();
  537. if (FilteredVersion is null)
  538. return TaskStatus.Faulted;
  539. InstallForge = true;
  540. InstallForgeOptifine = true;
  541. await StartDownload();
  542. IsControlsEnabled = false;
  543. var mcTfcUrl = $"https://files.veloe.link/launcher/McTFC/McTFC.zip";
  544. if (!Directory.Exists($"{Settings.MinecraftForlderPath}versions/McTFC"))
  545. {
  546. logger.Debug("Creating path: {0}", $"{Settings.MinecraftForlderPath}versions/McTFC");
  547. Directory.CreateDirectory($"{Settings.MinecraftForlderPath}versions/McTFC");
  548. }
  549. WebClient webClient = new WebClient();
  550. webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged;
  551. logger.Debug("Downloading: {0}", "McTFC.zip");
  552. DownloadingFileName = "McTFC.zip";
  553. webClient.DownloadFileAsync(new System.Uri(mcTfcUrl), Settings.MinecraftForlderPath + "versions/McTFC.zip");
  554. waitWhileBisy(ref webClient);
  555. logger.Debug("Extracting: {0}", "McTFC.zip");
  556. DownloadingFileName = "Unpacking McTFC.zip";
  557. ZipFile.ExtractToDirectory($"{Settings.MinecraftForlderPath}versions/McTFC.zip", Settings.MinecraftForlderPath + "versions/McTFC", true);
  558. File.Delete($"{Settings.MinecraftForlderPath}versions/McTFC.zip");
  559. webClient.DownloadProgressChanged -= WebClient_DownloadProgressChanged;
  560. Progress = 100;
  561. logger.Debug("Downloading McTFC finished.");
  562. DownloadingFileName = "Finished McTFC!";
  563. IsControlsEnabled = true;
  564. webClient.Dispose();
  565. }
  566. catch (Exception ex)
  567. {
  568. IsControlsEnabled = true;
  569. DownloadingFileName = "Error occured while downloading McTFC.";
  570. logger.Error(ex.Message);
  571. logger.Error(ex.StackTrace);
  572. return TaskStatus.Faulted;
  573. }
  574. return TaskStatus.RanToCompletion;
  575. }
  576. private void waitWhileBisy(ref WebClient webClient)
  577. {
  578. while(webClient.IsBusy)
  579. {
  580. //Progress = webClientProgress;
  581. Task.Delay(100).Wait();
  582. }
  583. }
  584. private void WebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  585. {
  586. Progress = e.ProgressPercentage;
  587. }
  588. public void updateList()
  589. {
  590. FilteredVerions.Clear();
  591. if (versionManifest.versions is null)
  592. return;
  593. foreach (var version in versionManifest.versions)
  594. {
  595. if (ShowSnaps && version.type == "snapshot" || ShowOld && version.type is ("old_alpha" or "old_beta") || version.type == "release")
  596. FilteredVerions.Add(version);
  597. }
  598. }
  599. }
  600. }