VersionsDownloaderViewModel.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. using DynamicData;
  2. using ReactiveUI;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.ComponentModel;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text.Json;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using VeloeMinecraftLauncher.Entity.LauncherProfiles;
  13. using VeloeMinecraftLauncher.Entity.VersionManifest;
  14. using VeloeMinecraftLauncher.Utils;
  15. using VeloeMinecraftLauncher.Models.Entity;
  16. using VeloeMinecraftLauncher.Entity.Version;
  17. using System.Diagnostics;
  18. namespace VeloeMinecraftLauncher.ViewModels;
  19. public class VersionsDownloaderViewModel : ViewModelBase
  20. {
  21. private string _downloadButtonText = "Download";
  22. private bool _showOld = false;
  23. private bool _showSnaps = false;
  24. private bool _installFabric = false;
  25. private bool _installForge = false;
  26. private bool _installOptifine = false;
  27. private bool _installForgeOptifine = false;
  28. private bool _installFabricVisible = false;
  29. private bool _installForgeVisible = false;
  30. private bool _installOptifineVisible = false;
  31. private bool _installForgeOptifineVisible = false;
  32. private bool _downloadJava = false;
  33. private bool _isControlsEnabled = true;
  34. private long _progress = 0;
  35. private long _maxprogressvalue = 100;
  36. private string _downloadingFileName = "No active downloads";
  37. private string _tasksStatusLine = "No tasks started yet";
  38. /// <summary>
  39. /// Token source for downloading tasks
  40. /// </summary>
  41. private CancellationTokenSource _tokenSource = new();
  42. /// <summary>
  43. /// Token source for checking downloading options on selection from verions available for downloading
  44. /// </summary>
  45. private CancellationTokenSource _filteredVersionTokenSource = new();
  46. Serilog.ILogger _logger;
  47. ObservableCollection<Entity.VersionManifest.Version> _filteredVersions;
  48. ObservableCollection<DownloadedVersion> _downloadedVersions;
  49. ObservableCollection<Modpack> _modpackVersions;
  50. ObservableCollection<TreeNode> _downloadedVersionTree;
  51. TreeNode _libraries;
  52. List<Entity.VersionManifest.Version> _modpackVersionsAsVersion;
  53. Entity.VersionManifest.Version _filteredVersion;
  54. DownloadedVersion _downloadedVersion;
  55. Modpack? _selectedModpack;
  56. VersionManifest _versionManifest;
  57. public VersionsDownloaderViewModel()
  58. {
  59. IsControlsEnabled = false;
  60. try
  61. {
  62. _logger = Settings.logger;
  63. Task.Run(async () =>
  64. {
  65. if (FilteredVersions is null)
  66. {
  67. FilteredVersions = new();
  68. }
  69. if (_modpackVersions is null)
  70. {
  71. _modpackVersions = new();
  72. }
  73. if (DownloadedVersionTree is null)
  74. {
  75. _downloadedVersionTree = new();
  76. }
  77. if (DownloadedVersionsDictionary is null)
  78. {
  79. DownloadedVersionsDictionary = new();
  80. }
  81. _logger.Debug("Getting versionManifest.json");
  82. _versionManifest = await Downloader.DownloadAndDeserializeJsonData<VersionManifest>("https://launchermeta.mojang.com/mc/game/version_manifest_v2.json") ?? new();
  83. _modpackVersions.AddRange(await Downloader.DownloadAndDeserializeJsonData<List<Modpack>>("https://files.veloe.link/launcher/modpacks.json") ?? new());
  84. _modpackVersionsAsVersion = _modpackVersions.Select(v=> new Entity.VersionManifest.Version() { Id = v.Name, Type = "modpack", ComplianceLevel = v.Revision}).ToList();
  85. _logger.Debug("Updating available versions to download.");
  86. UpdateList();
  87. SearchGameFolderForVersions();
  88. IsControlsEnabled = true;
  89. });
  90. }
  91. catch (Exception ex)
  92. {
  93. OpenErrorWindow(ex);
  94. }
  95. }
  96. public Entity.VersionManifest.Version FilteredVersion
  97. {
  98. get { return _filteredVersion; }
  99. set {
  100. this.RaiseAndSetIfChanged(ref _filteredVersion, value);
  101. InstallFabric = false;
  102. InstallForge = false;
  103. InstallOptifine = false;
  104. InstallForgeOptifine = false;
  105. if (value is null)
  106. return;
  107. if (value.Type == "modpack")
  108. {
  109. try
  110. {
  111. if (System.IO.File.Exists(Settings.minecraftForlderPath + $"versions/{value.Id}/revision.json"))
  112. {
  113. if (value.ComplianceLevel > JsonSerializer.Deserialize<int>(System.IO.File.ReadAllText(Settings.minecraftForlderPath + $"versions/{value.Id}/revision.json")))
  114. DownloadButtonText = "Update Modpack";
  115. else
  116. DownloadButtonText = "Reinstall Modpack";
  117. }
  118. else
  119. if (System.IO.Directory.Exists($"{Settings.minecraftForlderPath}versions/{value.Id}") && System.IO.File.Exists($"{Settings.minecraftForlderPath}versions/{value.Id}/{value.Id}.json"))
  120. DownloadButtonText = "Update Modpack";
  121. else
  122. DownloadButtonText = "Download Modpack";
  123. }
  124. catch (Exception)
  125. {
  126. DownloadButtonText = "Update Modpack";
  127. }
  128. }
  129. else
  130. {
  131. if (System.IO.File.Exists(Settings.minecraftForlderPath + $"versions/{value.Id}/{value.Id}.json"))
  132. DownloadButtonText = "Reinstall";
  133. else
  134. DownloadButtonText = "Download";
  135. }
  136. try
  137. {
  138. Task.Run(() =>
  139. {
  140. _filteredVersionTokenSource.Cancel();
  141. _filteredVersionTokenSource.Dispose();
  142. _filteredVersionTokenSource = new();
  143. try
  144. {
  145. if (Downloader.IsFileAvaliable(@$"https://files.veloe.link/launcher/forge/Forge{value.Id}/Forge{value.Id}.json", _filteredVersionTokenSource.Token).Result)
  146. {
  147. if (Downloader.IsFileAvaliable(@$"https://files.veloe.link/launcher/forge/Forge{value.Id}/Optifine{value.Id}.jar", _filteredVersionTokenSource.Token).Result)
  148. InstallForgeOptifineVisible = true;
  149. InstallForgeVisible = true;
  150. }
  151. else
  152. {
  153. InstallForgeVisible = false;
  154. InstallForgeOptifineVisible = false;
  155. }
  156. if (Downloader.IsFileAvaliable(@$"https://files.veloe.link/launcher/fabric/Fabric{value.Id}/Fabric{value.Id}.json", _filteredVersionTokenSource.Token).Result)
  157. InstallFabricVisible = true;
  158. else
  159. InstallFabricVisible = false;
  160. if (Downloader.IsFileAvaliable(@$"https://files.veloe.link/launcher/optifine/Optifine{value.Id}/Optifine{value.Id}.json", _filteredVersionTokenSource.Token).Result)
  161. InstallOptifineVisible = true;
  162. else
  163. InstallOptifineVisible = false;
  164. }
  165. catch (OperationCanceledException)
  166. {
  167. InstallForgeVisible = false;
  168. InstallForgeOptifineVisible = false;
  169. InstallFabricVisible = false;
  170. InstallOptifineVisible = false;
  171. }
  172. });
  173. }
  174. catch (Exception ex)
  175. {
  176. OpenErrorWindow(ex);
  177. }
  178. }
  179. }
  180. public DownloadedVersion DownloadedVersion
  181. {
  182. get => _downloadedVersion;
  183. set
  184. {
  185. this.RaiseAndSetIfChanged(ref _downloadedVersion, value);
  186. Task.Run(() =>
  187. {
  188. try
  189. {
  190. IsControlsEnabled = false;
  191. DownloadedVersionTree.Clear();
  192. DownloadedVersionTree = new();
  193. this.RaisePropertyChanged(nameof(DownloadedVersionTree));
  194. DownloadedVersionsDictionary.Clear();
  195. if (value is null)
  196. {
  197. IsControlsEnabled = true;
  198. return;
  199. }
  200. Entity.Version.Version? version = null;
  201. foreach (var dversion in DownloadedVersions)
  202. {
  203. string json;
  204. using (StreamReader reader = new StreamReader(dversion.path))
  205. {
  206. json = reader.ReadToEnd();
  207. }
  208. var versionObject = JsonSerializer.Deserialize<Entity.Version.Version>(json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
  209. DownloadedVersionsDictionary.Add(dversion.version, versionObject);
  210. if (dversion == _downloadedVersion)
  211. {
  212. version = versionObject;
  213. }
  214. }
  215. if (version is null)
  216. {
  217. OpenErrorWindow("Json file is invalid!");
  218. return;
  219. }
  220. var versionDir = new FileInfo(value.path)?.Directory?.FullName;
  221. if (versionDir is null)
  222. {
  223. OpenErrorWindow("Version folder is invalid!");
  224. return;
  225. }
  226. //get version
  227. DownloadedVersionTree.Add(new TreeNode() { Title = "Version: " + (version.InheritsFrom ?? version.Id) });
  228. //calc Size
  229. long allSize = 0;
  230. string[] size_label = { "bytes", "KB", "MB", "GB" };
  231. var dirInfo = new DirectoryInfo(versionDir);
  232. foreach (var file in dirInfo.EnumerateFiles("*", SearchOption.AllDirectories))
  233. { allSize += file.Length; }
  234. if (version.InheritsFrom is null)
  235. {
  236. //TODO add subnodes with size
  237. dirInfo = new DirectoryInfo(Settings.minecraftForlderPath);
  238. foreach (var dir in dirInfo.GetDirectories())
  239. {
  240. if (dir.Name != ".mixin.out" &&
  241. dir.Name != "assets" &&
  242. dir.Name != "javaruntime" &&
  243. dir.Name != "libraries" &&
  244. dir.Name != "logs" &&
  245. dir.Name != "versions")
  246. foreach (var file in dir.EnumerateFiles("*", SearchOption.AllDirectories))
  247. { allSize += file.Length; }
  248. }
  249. foreach (var file in dirInfo.GetFiles("*"))
  250. {
  251. if (!(file.Name.Contains("Veloe") &&
  252. file.Name.Contains("log") &&
  253. file.Name.Contains("exe")) &&
  254. file.Name == "launcher_profiles.json")
  255. allSize += file.Length;
  256. }
  257. }
  258. var j = 0;
  259. var result = (double)allSize;
  260. while (j < size_label.Length && result > 1024)
  261. {
  262. result = result / 1024;
  263. j++;
  264. }
  265. DownloadedVersionTree.Add(new TreeNode() { Title = "Size: " + string.Format("{0:N2}", result) + " " + size_label[j] });
  266. //calc Worlds
  267. dirInfo = null;
  268. if (version.InheritsFrom is null && Directory.Exists(Settings.minecraftForlderPath + "saves"))
  269. dirInfo = new DirectoryInfo(Settings.minecraftForlderPath + "saves");
  270. else
  271. if (Directory.Exists(versionDir + "/saves"))
  272. dirInfo = new DirectoryInfo(versionDir + "/saves");
  273. if (dirInfo is not null)
  274. {
  275. var worldsTreeNode = new TreeNode() { Title = "Worlds: " + dirInfo.GetDirectories().Count() };
  276. foreach (var world in dirInfo.GetDirectories())
  277. { worldsTreeNode.SubNode.Add(new TreeNode() { Title = world.Name }); }
  278. DownloadedVersionTree.Add(worldsTreeNode);
  279. }
  280. //check modloader
  281. var modsTreeNode = new TreeNode();
  282. if (version.InheritsFrom is null)
  283. modsTreeNode.Title = "Modloader: No";
  284. else if (version.Libraries.Any(l => l.Downloads?.Artifact?.Url?.Contains("forge") ?? false))
  285. modsTreeNode.Title = "Modloader: Forge";
  286. else if (version.Libraries.Any(l => l.Name.Contains("fabric")))
  287. modsTreeNode.Title = "Modloader: Fabric";
  288. //get mods list
  289. if (modsTreeNode.Title != "No" && Directory.Exists(versionDir + "/mods"))
  290. {
  291. dirInfo = new DirectoryInfo(versionDir + "/mods");
  292. modsTreeNode.Title += " (" + dirInfo.EnumerateFiles("*.jar", SearchOption.TopDirectoryOnly).Count() + ")";
  293. foreach (var mod in dirInfo.EnumerateFiles("*.jar", SearchOption.TopDirectoryOnly))
  294. { modsTreeNode.SubNode.Add(new TreeNode() { Title = mod.Name }); }
  295. }
  296. DownloadedVersionTree.Add(modsTreeNode);
  297. //calc resourcepacks
  298. dirInfo = null;
  299. if (version.InheritsFrom is null && Directory.Exists(Settings.minecraftForlderPath + "resourcepacks"))
  300. dirInfo = new DirectoryInfo(Settings.minecraftForlderPath + "resourcepacks");
  301. else
  302. if (Directory.Exists(versionDir + "/resourcepacks"))
  303. dirInfo = new DirectoryInfo(versionDir + "/resourcepacks");
  304. if (dirInfo is not null)
  305. {
  306. var resourcepacksTreeNode = new TreeNode();
  307. resourcepacksTreeNode.Title = "Resoursepacks: " + (dirInfo?.GetDirectories()?.Count() + dirInfo?.GetFiles().Count() ?? 0).ToString();
  308. foreach (var resourcepack in dirInfo?.GetFileSystemInfos())
  309. { resourcepacksTreeNode.SubNode.Add(new TreeNode() { Title = resourcepack.Name }); }
  310. DownloadedVersionTree.Add(resourcepacksTreeNode);
  311. }
  312. //calc assets
  313. var assetsFolderName = version.Assets;
  314. if (version.Assets is null &&
  315. version.InheritsFrom is not null &&
  316. DownloadedVersionsDictionary.TryGetValue(version.InheritsFrom, out var outVersion) &&
  317. outVersion?.Assets is not null)
  318. {
  319. assetsFolderName = outVersion.Assets;
  320. }
  321. if (Directory.Exists(Settings.minecraftForlderPath + "assets/" + assetsFolderName))
  322. {
  323. dirInfo = new DirectoryInfo(Settings.minecraftForlderPath + "assets/" + assetsFolderName);
  324. allSize = 0;
  325. foreach (var file in dirInfo.EnumerateFiles("*", SearchOption.AllDirectories))
  326. { allSize += file.Length; }
  327. j = 0;
  328. result = (double)allSize;
  329. while (j < size_label.Length && result > 1024)
  330. {
  331. result = result / 1024;
  332. j++;
  333. }
  334. var assetsTreeNode = new TreeNode();
  335. var usedByVersionCount = 0;
  336. foreach (var dictionaryVersion in DownloadedVersionsDictionary)
  337. {
  338. if ((dictionaryVersion.Value?.Assets == assetsFolderName &&
  339. dictionaryVersion.Value?.InheritsFrom is null) ||
  340. (dictionaryVersion.Value?.InheritsFrom is not null &&
  341. DownloadedVersionsDictionary.TryGetValue(dictionaryVersion.Value.InheritsFrom, out outVersion) &&
  342. outVersion?.Assets == assetsFolderName))
  343. {
  344. usedByVersionCount++;
  345. assetsTreeNode.SubNode.Add(new TreeNode() { Title = dictionaryVersion.Key });
  346. }
  347. }
  348. assetsTreeNode.Title = "Assets: " + version.Assets + " (" + string.Format("{0:N2}", result) + " " + size_label[j] + ") (" + usedByVersionCount + ")";
  349. DownloadedVersionTree.Add(assetsTreeNode);
  350. }
  351. // calc libraries
  352. var usedLibraries = version.Libraries;
  353. var libTreeNode = new TreeNode();
  354. libTreeNode.Title = "Libraries";
  355. if (version.InheritsFrom is not null && DownloadedVersionsDictionary.TryGetValue(version.InheritsFrom, out outVersion) && outVersion is not null)
  356. {
  357. usedLibraries.AddRange(outVersion.Libraries);
  358. }
  359. usedLibraries =
  360. usedLibraries
  361. .Where(l =>
  362. File.Exists(Path.Combine(Settings.minecraftForlderPath + "libraries/" + l.Downloads?.Artifact?.Path)) ||
  363. File.Exists(Path.Combine(Settings.minecraftForlderPath + "libraries/" + l.Downloads?.Classifiers?.NativesLinux?.Path)) ||
  364. File.Exists(Path.Combine(Settings.minecraftForlderPath + "libraries/" + l.Downloads?.Classifiers?.NativesWindows?.Path)) ||
  365. File.Exists(Path.Combine(Settings.minecraftForlderPath + "libraries/" + l.Downloads?.Classifiers?.NativesWindows64?.Path)) ||
  366. File.Exists(Path.Combine(Settings.minecraftForlderPath + "libraries/" + l.Downloads?.Classifiers?.NativesWindows32?.Path)) ||
  367. File.Exists(Path.Combine(Settings.minecraftForlderPath + "libraries/" + StartCommandBuilder.GetLibPathFromName(l.Name, true)))
  368. )
  369. .ToList();
  370. var libraries = GetLibrariesFromVersions(DownloadedVersionsDictionary)
  371. .Where(v => usedLibraries.Any(u => v.Value.Name == u.Name))
  372. .GroupBy(a => a.Value.Name)
  373. .Select(g =>
  374. {
  375. return new {
  376. Name = g.Key,
  377. Count = g.Select(a => a.Key).Distinct().Count(),
  378. Versions = g.Select(a => a.Key).Distinct(),
  379. LibraryUniqueInstances = g.Select(a => a.Value).Distinct() //IEnumerable cause there can be several lib blocks with different rules in one version json
  380. };
  381. });
  382. foreach (var lib in libraries)
  383. {
  384. var subLibTreeNode = new TreeNode()
  385. {
  386. Title = lib.Name + " (" + lib.Count + ")",
  387. };
  388. //if only one version uses it (selected version), then add path to lib in Tag for deletion
  389. if (lib.Count == 1)
  390. {
  391. subLibTreeNode.Tag = lib.LibraryUniqueInstances.Where(l => l.Downloads?.Artifact?.Path is not null).Select(l => l.Downloads?.Artifact?.Path).FirstOrDefault() ?? string.Empty;
  392. //if fabric library
  393. if (File.Exists(Settings.minecraftForlderPath + "libraries/" + StartCommandBuilder.GetLibPathFromName(lib.Name,true)))
  394. subLibTreeNode.Tag = StartCommandBuilder.GetLibPathFromName(lib.Name,true);
  395. }
  396. var subLibUsedVersionsTreeNodeHeader = new TreeNode() { Title = "Using in versions:" };
  397. foreach (var ver in lib.Versions)
  398. subLibUsedVersionsTreeNodeHeader.SubNode.Add(new TreeNode() { Title = ver });
  399. if (subLibUsedVersionsTreeNodeHeader.SubNode.Count > 0)
  400. subLibTreeNode.SubNode.Add(subLibUsedVersionsTreeNodeHeader);
  401. if (lib.LibraryUniqueInstances.Any(l=>l.Natives is not null) || lib.LibraryUniqueInstances.Any(l=>l.Downloads?.Classifiers is not null))
  402. {
  403. var subLibNativesTreeNodeHeader = new TreeNode() { Title = "Natives:" };
  404. //check classifiers
  405. var libraryNativeInstance = lib.LibraryUniqueInstances.Where(l => l.Natives is not null || l.Downloads?.Classifiers is not null).First();
  406. if (libraryNativeInstance.Downloads?.Classifiers?.NativesWindows is not null)
  407. {
  408. subLibNativesTreeNodeHeader.SubNode.Add(new TreeNode()
  409. {
  410. Title = Path.GetFileName(libraryNativeInstance.Downloads?.Classifiers?.NativesWindows.Path) ?? "No lib name",
  411. Tag = lib.Count == 1 ? libraryNativeInstance.Downloads?.Classifiers?.NativesWindows?.Path ?? string.Empty : string.Empty
  412. });
  413. }
  414. if (libraryNativeInstance.Downloads?.Classifiers?.NativesWindows32 is not null)
  415. {
  416. subLibNativesTreeNodeHeader.SubNode.Add(new TreeNode()
  417. {
  418. Title = Path.GetFileName(libraryNativeInstance.Downloads?.Classifiers?.NativesWindows32.Path) ?? "No lib name",
  419. Tag = lib.Count == 1 ? libraryNativeInstance.Downloads?.Classifiers?.NativesWindows32?.Path ?? string.Empty : string.Empty
  420. });
  421. }
  422. if (libraryNativeInstance.Downloads?.Classifiers?.NativesWindows64 is not null)
  423. {
  424. subLibNativesTreeNodeHeader.SubNode.Add(new TreeNode()
  425. {
  426. Title = Path.GetFileName(libraryNativeInstance.Downloads?.Classifiers?.NativesWindows64.Path) ?? "No lib name",
  427. Tag = lib.Count == 1 ? libraryNativeInstance.Downloads?.Classifiers?.NativesWindows64?.Path ?? string.Empty : string.Empty
  428. });
  429. }
  430. if (libraryNativeInstance.Downloads?.Classifiers?.NativesLinux is not null)
  431. {
  432. subLibNativesTreeNodeHeader.SubNode.Add(new TreeNode()
  433. {
  434. Title = Path.GetFileName(libraryNativeInstance.Downloads?.Classifiers?.NativesLinux.Path) ?? "No lib name",
  435. Tag = lib.Count == 1 ? libraryNativeInstance.Downloads?.Classifiers?.NativesLinux?.Path ?? string.Empty : string.Empty
  436. });
  437. }
  438. if (subLibNativesTreeNodeHeader.SubNode.Count > 0)
  439. subLibTreeNode.SubNode.Add(subLibNativesTreeNodeHeader);
  440. }
  441. libTreeNode.SubNode.Add(subLibTreeNode);
  442. }
  443. _libraries = libTreeNode;
  444. DownloadedVersionTree.Add(libTreeNode);
  445. }
  446. finally
  447. {
  448. this.RaisePropertyChanged(nameof(DownloadedVersionTree));
  449. IsControlsEnabled = true;
  450. }
  451. });
  452. }
  453. }
  454. public Dictionary<string,Entity.Version.Version?> DownloadedVersionsDictionary { get; set; }
  455. public ObservableCollection<Entity.VersionManifest.Version> FilteredVersions
  456. {
  457. get => _filteredVersions;
  458. set => this.RaiseAndSetIfChanged(ref _filteredVersions, value);
  459. }
  460. public ObservableCollection<DownloadedVersion> DownloadedVersions
  461. {
  462. get => _downloadedVersions;
  463. set => this.RaiseAndSetIfChanged(ref _downloadedVersions, value);
  464. }
  465. public ObservableCollection<TreeNode> DownloadedVersionTree
  466. {
  467. get => _downloadedVersionTree;
  468. set => this.RaiseAndSetIfChanged(ref _downloadedVersionTree, value);
  469. }
  470. public string DownloadButtonText
  471. {
  472. get => _downloadButtonText;
  473. set => this.RaiseAndSetIfChanged(ref _downloadButtonText, value);
  474. }
  475. public bool ShowOld
  476. {
  477. get => _showOld;
  478. set {
  479. this.RaiseAndSetIfChanged(ref _showOld, value);
  480. UpdateList();
  481. }
  482. }
  483. public bool ShowSnaps
  484. {
  485. get => _showSnaps;
  486. set {
  487. this.RaiseAndSetIfChanged(ref _showSnaps, value);
  488. UpdateList();
  489. }
  490. }
  491. public long Progress
  492. {
  493. get => _progress;
  494. set => this.RaiseAndSetIfChanged(ref _progress, value);
  495. }
  496. public long MaxProgressValue
  497. {
  498. get => _maxprogressvalue;
  499. set => this.RaiseAndSetIfChanged(ref _maxprogressvalue, value);
  500. }
  501. public string DownloadingFileName
  502. {
  503. get => _downloadingFileName;
  504. set => this.RaiseAndSetIfChanged(ref _downloadingFileName, value);
  505. }
  506. public string TasksStatusLine
  507. {
  508. get => _tasksStatusLine;
  509. set => this.RaiseAndSetIfChanged(ref _tasksStatusLine, value);
  510. }
  511. public bool InstallForge
  512. {
  513. get => _installForge;
  514. set
  515. {
  516. this.RaiseAndSetIfChanged(ref _installForge, value);
  517. if (InstallFabric)
  518. InstallFabric = false;
  519. }
  520. }
  521. public bool InstallFabric
  522. {
  523. get => _installFabric;
  524. set
  525. {
  526. this.RaiseAndSetIfChanged(ref _installFabric, value);
  527. if (InstallForge)
  528. InstallForge = false;
  529. }
  530. }
  531. public bool InstallOptifine
  532. {
  533. get => _installOptifine;
  534. set => this.RaiseAndSetIfChanged(ref _installOptifine, value);
  535. }
  536. public bool InstallForgeOptifine
  537. {
  538. get { return _installForgeOptifine; }
  539. set
  540. {
  541. this.RaiseAndSetIfChanged(ref _installForgeOptifine, value);
  542. if (value)
  543. InstallForge = true;
  544. }
  545. }
  546. public bool InstallForgeVisible
  547. {
  548. get => _installForgeVisible;
  549. set => this.RaiseAndSetIfChanged(ref _installForgeVisible, value);
  550. }
  551. public bool InstallFabricVisible
  552. {
  553. get => _installFabricVisible;
  554. set => this.RaiseAndSetIfChanged(ref _installFabricVisible, value);
  555. }
  556. public bool InstallOptifineVisible
  557. {
  558. get => _installOptifineVisible;
  559. set => this.RaiseAndSetIfChanged(ref _installOptifineVisible, value);
  560. }
  561. public bool InstallForgeOptifineVisible
  562. {
  563. get => _installForgeOptifineVisible;
  564. set => this.RaiseAndSetIfChanged(ref _installForgeOptifineVisible, value);
  565. }
  566. public bool DownloadJava
  567. {
  568. get => _downloadJava;
  569. set => this.RaiseAndSetIfChanged(ref _downloadJava, value);
  570. }
  571. public bool IsControlsEnabled
  572. {
  573. get => _isControlsEnabled;
  574. set => this.RaiseAndSetIfChanged(ref _isControlsEnabled, value);
  575. }
  576. public async Task OnStartBunttonClick()
  577. {
  578. await Task.Run(async () => {
  579. if (FilteredVersion is null)
  580. return TaskStatus.Faulted;
  581. if (FilteredVersion.Type == "modpack")
  582. {
  583. _selectedModpack = _modpackVersions.Where(m => m.Name == FilteredVersion.Id).FirstOrDefault();
  584. if (_selectedModpack is null)
  585. return TaskStatus.Faulted;
  586. if (FilteredVersions.Where(x => x.Id == _selectedModpack.Version).FirstOrDefault() is null)
  587. return TaskStatus.Faulted;
  588. await Task.Run(async () => await Downloader.StartDownloadModpack(
  589. value => DownloadingFileName = value,
  590. value => TasksStatusLine = value,
  591. value => IsControlsEnabled = value,
  592. value => Progress = value,
  593. FilteredVersions,
  594. _selectedModpack,
  595. _tokenSource.Token,
  596. DownloadJava,
  597. InstallFabric = _selectedModpack.Fabric,
  598. InstallForge = _selectedModpack.Forge,
  599. InstallOptifine = _selectedModpack.Optifine,
  600. InstallForgeOptifine = _selectedModpack.ForgeOptifine));
  601. }
  602. else
  603. {
  604. _logger.Debug("Downloading {0}.json", FilteredVersion.Id);
  605. DownloadingFileName = $"{FilteredVersion.Id}.json";
  606. var versionJson = await Downloader.DownloadAndDeserializeJsonData<Entity.Version.Version>(FilteredVersion.Url, Settings.minecraftForlderPath + "versions/" + FilteredVersion.Id + "/", FilteredVersion.Id + ".json");
  607. if (versionJson is not null)
  608. await Downloader.StartDownload(
  609. value => DownloadingFileName = value,
  610. value => TasksStatusLine = value,
  611. value => IsControlsEnabled = value,
  612. value => Progress = value,
  613. versionJson,
  614. _tokenSource.Token,
  615. DownloadJava,
  616. InstallForge,
  617. InstallOptifine,
  618. InstallForgeOptifine,
  619. InstallFabric);
  620. }
  621. SearchGameFolderForVersions();
  622. return TaskStatus.RanToCompletion;
  623. });
  624. }
  625. public async Task OnDeleteButtonClick()
  626. {
  627. await Task.Run(async () => {
  628. try
  629. {
  630. IsControlsEnabled = false;
  631. DownloadedVersionTree.Clear();
  632. DownloadedVersionTree = new();
  633. this.RaisePropertyChanged(nameof(DownloadedVersionTree));
  634. DownloadedVersionsDictionary.Clear();
  635. DownloadedVersionsDictionary = new();
  636. Entity.Version.Version? version = null;
  637. foreach (var dversion in DownloadedVersions)
  638. {
  639. string json;
  640. using (StreamReader reader = new StreamReader(dversion.path))
  641. {
  642. json = reader.ReadToEnd();
  643. }
  644. var versionObject = JsonSerializer.Deserialize<Entity.Version.Version>(json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
  645. DownloadedVersionsDictionary.Add(dversion.version, versionObject);
  646. if (dversion == _downloadedVersion)
  647. {
  648. version = versionObject;
  649. }
  650. }
  651. if (version is null)
  652. {
  653. OpenErrorWindow("Json file is invalid!");
  654. return;
  655. }
  656. var versionDir = new FileInfo(DownloadedVersion.path)?.Directory?.FullName;
  657. if (versionDir is null)
  658. {
  659. OpenErrorWindow("Version folder is invalid!");
  660. return;
  661. }
  662. var dirInfo = new DirectoryInfo(versionDir);
  663. if (version.InheritsFrom is null && DownloadedVersionsDictionary.Where(v=>v.Value.InheritsFrom is null).Count() == 1)
  664. {
  665. //if vanilla
  666. Debug.WriteLine("Vanilla root directories deleted!");
  667. if (Directory.Exists(Settings.minecraftForlderPath + "saves"))
  668. Directory.Delete(Settings.minecraftForlderPath + "saves", true);
  669. if (Directory.Exists(Settings.minecraftForlderPath + "resourcepacks"))
  670. Directory.Delete(Settings.minecraftForlderPath + "resourcepacks", true);
  671. if (Directory.Exists(Settings.minecraftForlderPath + "shaderpacks"))
  672. Directory.Delete(Settings.minecraftForlderPath + "shaderpacks", true);
  673. }
  674. //calc assets
  675. var assetsFolderName = version.Assets;
  676. if (version.Assets is null &&
  677. version.InheritsFrom is not null &&
  678. DownloadedVersionsDictionary.TryGetValue(version.InheritsFrom, out var outVersion) &&
  679. outVersion?.Assets is not null)
  680. {
  681. assetsFolderName = outVersion.Assets;
  682. }
  683. if (Directory.Exists(Settings.minecraftForlderPath + "assets/" + assetsFolderName))
  684. {
  685. var usedByVersionCount = 0;
  686. foreach (var dictionaryVersion in DownloadedVersionsDictionary)
  687. {
  688. if ((dictionaryVersion.Value?.Assets == assetsFolderName &&
  689. dictionaryVersion.Value?.InheritsFrom is null) ||
  690. (dictionaryVersion.Value?.InheritsFrom is not null &&
  691. DownloadedVersionsDictionary.TryGetValue(dictionaryVersion.Value.InheritsFrom, out outVersion) &&
  692. outVersion?.Assets == assetsFolderName))
  693. {
  694. usedByVersionCount++;
  695. }
  696. }
  697. //delete assets if only deleting version used it
  698. if (usedByVersionCount == 1)
  699. {
  700. Debug.WriteLine(Settings.minecraftForlderPath + "assets/" + assetsFolderName);
  701. Directory.Delete(Settings.minecraftForlderPath + "assets/" + assetsFolderName,true);
  702. }
  703. }
  704. // calc libraries
  705. DeleteLibrariesFromTreeNode(_libraries);
  706. // delete version dir
  707. Debug.WriteLine(versionDir);
  708. Directory.Delete(versionDir, true);
  709. }
  710. catch (Exception ex)
  711. {
  712. OpenErrorWindow(ex);
  713. }
  714. finally
  715. {
  716. SearchGameFolderForVersions();
  717. IsControlsEnabled = true;
  718. }
  719. });
  720. }
  721. private void DeleteLibrariesFromTreeNode(TreeNode library)
  722. {
  723. foreach (var node in library.SubNode)
  724. DeleteLibrariesFromTreeNode(node);
  725. if (!string.IsNullOrEmpty(library.Tag))
  726. {
  727. var libFileInfo = new FileInfo(Settings.minecraftForlderPath + "libraries/" + library.Tag);
  728. var libDir = libFileInfo.Directory;
  729. if(libDir?.Exists == false)
  730. {
  731. Debug.WriteLine($"Directory {libDir.FullName} does not exists! Subnode: {library.Title}");
  732. _logger.Warning($"Directory {libDir.FullName} does not exists!");
  733. return;
  734. }
  735. if (libDir?.GetFileSystemInfos().Count() > 1)
  736. {
  737. Debug.WriteLine(libFileInfo.FullName);
  738. libFileInfo.Delete();
  739. }
  740. else
  741. {
  742. Debug.WriteLine(libDir?.FullName);
  743. libDir?.Delete(true);
  744. }
  745. }
  746. }
  747. public void UpdateList()
  748. {
  749. try
  750. {
  751. FilteredVersions.Clear();
  752. if (_versionManifest.Versions is null)
  753. return;
  754. FilteredVersions.AddRange(_modpackVersionsAsVersion);
  755. FilteredVersions.AddRange(_versionManifest.Versions.Where(version => ShowSnaps && version.Type == "snapshot" || ShowOld && version.Type is ("old_alpha" or "old_beta") || version.Type == "release"));
  756. }
  757. catch (Exception ex)
  758. {
  759. OpenErrorWindow(ex);
  760. }
  761. }
  762. public void SearchGameFolderForVersions()
  763. {
  764. if (DownloadedVersions is null)
  765. DownloadedVersions = new();
  766. DownloadedVersions.Clear();
  767. DownloadedVersions = new();
  768. DirectoryInfo versions = new(Settings.minecraftForlderPath + "versions");
  769. try
  770. {
  771. var dirs = versions.GetDirectories("*", SearchOption.TopDirectoryOnly);
  772. LauncherProfiles profiles = new LauncherProfiles();
  773. profiles.SelectedProfile = "NotImplemented";
  774. foreach (var dir in dirs)
  775. {
  776. _logger.Debug("Checking folder {0}", dir.Name);
  777. string checkedPath;
  778. if (File.Exists(checkedPath = dir.FullName + "/" + dir.Name + ".json"))
  779. {
  780. _logger.Debug("Found version {0}", dir.Name);
  781. DownloadedVersions.Add(new DownloadedVersion(checkedPath, dir.Name));
  782. profiles.Profiles.Add($"Version {dir.Name}", new Profile() { Name = dir.Name, LastVersionId = dir.Name, LauncherVisibilityOnGameClose = "keep the launcher open" });
  783. }
  784. }
  785. }
  786. catch (DirectoryNotFoundException)
  787. {
  788. Directory.CreateDirectory(Settings.minecraftForlderPath + "versions");
  789. return;
  790. }
  791. }
  792. public void OnOpenForlder()
  793. {
  794. if (DownloadedVersion?.version is not null && Path.Exists(Settings.minecraftForlderPath + "versions/" + DownloadedVersion.version))
  795. Process.Start(new System.Diagnostics.ProcessStartInfo() { FileName = Settings.minecraftForlderPath + "versions/" + DownloadedVersion.version, UseShellExecute = true });
  796. }
  797. public void OnClosing(object sender, CancelEventArgs args)
  798. {
  799. _tokenSource.Cancel();
  800. _tokenSource.Dispose();
  801. _filteredVersionTokenSource.Cancel();
  802. _filteredVersionTokenSource.Dispose();
  803. }
  804. public IEnumerable<KeyValuePair<string,Library>> GetLibrariesFromVersions(Dictionary<string, Entity.Version.Version?> versions)
  805. {
  806. foreach (var version in versions)
  807. {
  808. if (version.Value is null) continue;
  809. if (version.Value.InheritsFrom is not null && versions.TryGetValue(version.Value.InheritsFrom,out var inheritVersion) && inheritVersion?.Libraries is not null)
  810. {
  811. foreach (var lib in inheritVersion.Libraries)
  812. yield return new KeyValuePair<string, Library>(version.Key, lib);
  813. }
  814. foreach (var lib in version.Value.Libraries)
  815. yield return new KeyValuePair<string, Library>(version.Key,lib);
  816. }
  817. }
  818. }
  819. public class TreeNode
  820. {
  821. public TreeNode()
  822. {
  823. Title = "Default";
  824. SubNode = new();
  825. Tag = string.Empty;
  826. }
  827. public string Title { get; set; }
  828. public string Tag { get; set; }
  829. public List<TreeNode> SubNode { get; set; }
  830. public override string ToString()
  831. {
  832. return Title;
  833. }
  834. }