|
@@ -9,14 +9,556 @@ using System.Net.Http;
|
|
|
using System.Text;
|
|
|
using System.Text.Json;
|
|
|
using System.Threading.Tasks;
|
|
|
+using System.IO.Compression;
|
|
|
+using VeloeMinecraftLauncher.Entity.Assets;
|
|
|
using VeloeMinecraftLauncher.ViewModels;
|
|
|
using VeloeMinecraftLauncher.Views;
|
|
|
+using VeloeMinecraftLauncher.Entity.VersionManifest;
|
|
|
+using System.Collections.ObjectModel;
|
|
|
|
|
|
namespace VeloeMinecraftLauncher.MinecraftLauncher
|
|
|
{
|
|
|
internal static class Downloader
|
|
|
{
|
|
|
- public static T DownloadAndDeserializeJsonData<T>(string url) where T : new()
|
|
|
+ public static async Task<TaskStatus> StartDownload(
|
|
|
+ Action<string> DownloadingFileName,
|
|
|
+ Action<bool> IsControlsEnabled,
|
|
|
+ Action<WebClient> SetProgressBar,
|
|
|
+ Action<WebClient> RemoveProgressBar,
|
|
|
+ Entity.Version.Version versionJson,
|
|
|
+ bool DownloadJava = false,
|
|
|
+ bool InstallForge = false,
|
|
|
+ bool InstallOptifine = false,
|
|
|
+ bool InstallForgeOptifine = false,
|
|
|
+ bool InstallFabric = false
|
|
|
+ )
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ Settings.logger.Debug("Downloading minecraft {0}", versionJson.id);
|
|
|
+ var webClient = new WebClient();
|
|
|
+
|
|
|
+ SetProgressBar(webClient);
|
|
|
+ //webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged;
|
|
|
+ //download java
|
|
|
+ if (DownloadJava)
|
|
|
+ {
|
|
|
+ Settings.logger.Debug("Getting required Java version.");
|
|
|
+ if (versionJson.javaVersion.majorVersion != null)
|
|
|
+ {
|
|
|
+ string javaUrl = "";
|
|
|
+ switch (versionJson.javaVersion.majorVersion)
|
|
|
+ {
|
|
|
+ case 8:
|
|
|
+ if (Environment.Is64BitOperatingSystem && OperatingSystem.IsWindows())
|
|
|
+ javaUrl = "https://files.veloe.link/launcher/java/8/windows64/java.zip";
|
|
|
+ if (!Environment.Is64BitOperatingSystem && OperatingSystem.IsWindows())
|
|
|
+ javaUrl = "https://files.veloe.link/launcher/java/8/windows32/java.zip";
|
|
|
+
|
|
|
+ if (Environment.Is64BitOperatingSystem && OperatingSystem.IsLinux())
|
|
|
+ javaUrl = "https://files.veloe.link/launcher/java/8/linux64/java.zip";
|
|
|
+ if (!Environment.Is64BitOperatingSystem && OperatingSystem.IsLinux())
|
|
|
+ javaUrl = "https://files.veloe.link/launcher/java/8/linux32/java.zip";
|
|
|
+ break;
|
|
|
+ case 16:
|
|
|
+ if (Environment.Is64BitOperatingSystem && OperatingSystem.IsWindows())
|
|
|
+ javaUrl = "https://files.veloe.link/launcher/java/16/windows64/java.zip";
|
|
|
+ if (Environment.Is64BitOperatingSystem && OperatingSystem.IsLinux())
|
|
|
+ javaUrl = "https://files.veloe.link/launcher/java/16/linux64/java.zip";
|
|
|
+ break;
|
|
|
+ case 17:
|
|
|
+ if (Environment.Is64BitOperatingSystem && OperatingSystem.IsWindows())
|
|
|
+ javaUrl = "https://files.veloe.link/launcher/java/17/windows64/java.zip";
|
|
|
+ if (Environment.Is64BitOperatingSystem && OperatingSystem.IsLinux())
|
|
|
+ javaUrl = "https://files.veloe.link/launcher/java/17/linux64/java.zip";
|
|
|
+
|
|
|
+ break;
|
|
|
+ case 18:
|
|
|
+ if (Environment.Is64BitOperatingSystem && OperatingSystem.IsWindows())
|
|
|
+ javaUrl = "https://files.veloe.link/launcher/java/18/windows64/java.zip";
|
|
|
+ if (Environment.Is64BitOperatingSystem && OperatingSystem.IsLinux())
|
|
|
+ javaUrl = "https://files.veloe.link/launcher/java/18/linux64/java.zip";
|
|
|
+
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (javaUrl != "")
|
|
|
+ {
|
|
|
+ Settings.logger.Debug("Downloading Java");
|
|
|
+ DownloadingFileName("java.zip");
|
|
|
+ webClient.DownloadFileAsync(new System.Uri(javaUrl), Settings.MinecraftForlderPath + "java.zip");
|
|
|
+ waitWhileBisy(ref webClient);
|
|
|
+
|
|
|
+ Settings.logger.Debug("Unpacking Java");
|
|
|
+ DownloadingFileName("Unpacking java.zip");
|
|
|
+ ZipFile.ExtractToDirectory($"{Settings.MinecraftForlderPath}java.zip", Settings.MinecraftForlderPath, true);
|
|
|
+ File.Delete($"{Settings.MinecraftForlderPath}java.zip");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ Settings.logger.Debug("Required Java version don't found in json file."); //log that java was not downloaded;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //download json
|
|
|
+ var path = Settings.MinecraftForlderPath + "/versions/" + versionJson.id;
|
|
|
+ if (!Directory.Exists(path))
|
|
|
+ {
|
|
|
+ Settings.logger.Debug("Creating path: {0}", path);
|
|
|
+ Directory.CreateDirectory(path);
|
|
|
+ }
|
|
|
+
|
|
|
+ //Settings.logger.Debug("Downloading {0}", $"{versionJson.id}.json");
|
|
|
+ //DownloadingFileName($"{versionJson.id}.json");
|
|
|
+ //webClient.DownloadFileAsync(new System.Uri(versionJson.url), path + "/" + versionJson.id + ".json");
|
|
|
+ //waitWhileBisy(ref webClient);
|
|
|
+
|
|
|
+ //download jar
|
|
|
+ if (!Directory.Exists(path))
|
|
|
+ {
|
|
|
+ Settings.logger.Debug("Creating path: {0}", path);
|
|
|
+ Directory.CreateDirectory(path);
|
|
|
+ }
|
|
|
+
|
|
|
+ string chksum = String.Empty;
|
|
|
+ //here hash check
|
|
|
+ if (File.Exists(path + "/" + versionJson.id + ".jar"))
|
|
|
+ {
|
|
|
+ DownloadingFileName($"Checking hash: {versionJson.id}.jar");
|
|
|
+ FileStream fop = File.OpenRead(path + "/" + versionJson.id + ".jar");
|
|
|
+ byte[] hash = System.Security.Cryptography.SHA1.Create().ComputeHash(fop);
|
|
|
+ chksum = BitConverter.ToString(hash).Replace("-", String.Empty).ToLower();
|
|
|
+
|
|
|
+ fop.Close();
|
|
|
+ fop.Dispose();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (chksum != versionJson.downloads.client.sha1)
|
|
|
+ {
|
|
|
+ Settings.logger.Debug("Downloading {0}", $"{versionJson.id}.jar");
|
|
|
+ DownloadingFileName($"{versionJson.id}.jar");
|
|
|
+ webClient.DownloadFileAsync(new System.Uri(versionJson.downloads.client.url), path + "/" + versionJson.id + ".jar");
|
|
|
+ waitWhileBisy(ref webClient);
|
|
|
+ }
|
|
|
+ //end download jar
|
|
|
+ //download libraries
|
|
|
+ Settings.logger.Debug("Downloading libraries.");
|
|
|
+ foreach (var library in versionJson.libraries)
|
|
|
+ {
|
|
|
+ var libPath = Settings.MinecraftForlderPath + "libraries/";
|
|
|
+ string[] libPathSplit = new[] { "" };
|
|
|
+ string libUrl = "";
|
|
|
+ string sha1 = String.Empty;
|
|
|
+ // getting path and url if universal lib
|
|
|
+ if (library.natives is null)
|
|
|
+ {
|
|
|
+ libPath = Settings.MinecraftForlderPath + "libraries/";
|
|
|
+ libPathSplit = library.downloads.artifact.path.Split('/');
|
|
|
+ libUrl = library.downloads.artifact.url;
|
|
|
+ sha1 = library.downloads.artifact.sha1;
|
|
|
+ for (int i = 0; i < libPathSplit.Length - 1; i++)
|
|
|
+ {
|
|
|
+ libPath += libPathSplit[i] + "/";
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ else
|
|
|
+ { // getting path if native
|
|
|
+ libPath = Settings.MinecraftForlderPath + "libraries/";
|
|
|
+ libPathSplit = new[] { "" };
|
|
|
+ libUrl = "";
|
|
|
+
|
|
|
+ if (OperatingSystem.IsWindows() && library.natives.windows is not null)
|
|
|
+ {
|
|
|
+ if (library.downloads.classifiers.NativesWindows is not null)
|
|
|
+ {
|
|
|
+ libPathSplit = library.downloads.classifiers.NativesWindows.path.Split('/');
|
|
|
+ libUrl = library.downloads.classifiers.NativesWindows.url;
|
|
|
+ sha1 = library.downloads.classifiers.NativesWindows.sha1;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if (Environment.Is64BitOperatingSystem)
|
|
|
+ {
|
|
|
+ libPathSplit = library.downloads.classifiers.NativesWindows64.path.Split('/');
|
|
|
+ libUrl = library.downloads.classifiers.NativesWindows64.url;
|
|
|
+ sha1 = library.downloads.classifiers.NativesWindows64.sha1;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ libPathSplit = library.downloads.classifiers.NativesWindows32.path.Split('/');
|
|
|
+ libUrl = library.downloads.classifiers.NativesWindows32.url;
|
|
|
+ sha1 = library.downloads.classifiers.NativesWindows32.sha1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (OperatingSystem.IsLinux() && library.natives.linux is not null)
|
|
|
+ {
|
|
|
+ libPathSplit = library.downloads.classifiers.NativesLinux.path.Split('/');
|
|
|
+ libUrl = library.downloads.classifiers.NativesLinux.url;
|
|
|
+ sha1 = library.downloads.classifiers.NativesLinux.url;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ for (int i = 0; i < libPathSplit.Length - 1; i++)
|
|
|
+ {
|
|
|
+ libPath += libPathSplit[i] + "/";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //if no lib url
|
|
|
+ if (libUrl == String.Empty)
|
|
|
+ continue;
|
|
|
+
|
|
|
+ //checking rules
|
|
|
+ if (library.rules == null)
|
|
|
+ {
|
|
|
+ if (!Directory.Exists(libPath))
|
|
|
+ {
|
|
|
+ Settings.logger.Debug("Creating path: {0}", path);
|
|
|
+ Directory.CreateDirectory(libPath);
|
|
|
+ }
|
|
|
+
|
|
|
+ chksum = String.Empty;
|
|
|
+
|
|
|
+ if (File.Exists(libPath + "/" + libPathSplit.Last()))
|
|
|
+ {
|
|
|
+ DownloadingFileName($"Checking hash: {libPathSplit.Last()}");
|
|
|
+ FileStream fop = File.OpenRead(libPath + "/" + libPathSplit.Last());
|
|
|
+ byte[] hash = System.Security.Cryptography.SHA1.Create().ComputeHash(fop);
|
|
|
+ chksum = BitConverter.ToString(hash).Replace("-", String.Empty).ToLower();
|
|
|
+
|
|
|
+ fop.Close();
|
|
|
+ fop.Dispose();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (chksum != sha1)
|
|
|
+ {
|
|
|
+ Settings.logger.Debug("Downloading: {0}", libPathSplit.Last());
|
|
|
+ DownloadingFileName(libPathSplit.Last());
|
|
|
+ webClient.DownloadFileAsync(new System.Uri(libUrl), libPath + "/" + libPathSplit.Last());
|
|
|
+ waitWhileBisy(ref webClient);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ foreach (var rule in library.rules)
|
|
|
+ {
|
|
|
+ if (rule.action == "allow") // && rule.os is null
|
|
|
+ {
|
|
|
+ if (!Directory.Exists(libPath))
|
|
|
+ {
|
|
|
+ Settings.logger.Debug("Creating path: {0}", path);
|
|
|
+ Directory.CreateDirectory(libPath);
|
|
|
+ }
|
|
|
+
|
|
|
+ chksum = String.Empty;
|
|
|
+
|
|
|
+ if (File.Exists(libPath + "/" + libPathSplit.Last()) && sha1 != String.Empty)
|
|
|
+ {
|
|
|
+ DownloadingFileName($"Checking hash: {libPathSplit.Last()}");
|
|
|
+ FileStream fop = File.OpenRead(libPath + "/" + libPathSplit.Last());
|
|
|
+ byte[] hash = System.Security.Cryptography.SHA1.Create().ComputeHash(fop);
|
|
|
+ chksum = BitConverter.ToString(hash).Replace("-", String.Empty).ToLower();
|
|
|
+
|
|
|
+ fop.Close();
|
|
|
+ fop.Dispose();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (chksum != sha1)
|
|
|
+ {
|
|
|
+ Settings.logger.Debug("Downloading: {0}", libPathSplit.Last());
|
|
|
+ DownloadingFileName(libPathSplit.Last());
|
|
|
+ webClient.DownloadFileAsync(new System.Uri(libUrl), libPath + "/" + libPathSplit.Last());
|
|
|
+ waitWhileBisy(ref webClient);
|
|
|
+ }
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ //fabic on start needs all libraries for all os, so os check removed
|
|
|
+ /*
|
|
|
+ if (rule.action == "allow" && (rule.os.name == "windows" || rule.os.name == "linux" || rule.os.name == "osx" ))
|
|
|
+ {
|
|
|
+ if (!Directory.Exists(libPath))
|
|
|
+ {
|
|
|
+ Settings.logger.Debug("Creating path: {0}", path);
|
|
|
+ Directory.CreateDirectory(libPath);
|
|
|
+ }
|
|
|
+ Settings.logger.Debug("Downloading: {0}", libPathSplit.Last());
|
|
|
+ DownloadingFileName = libPathSplit.Last();
|
|
|
+ webClient.DownloadFileAsync(new System.Uri(libUrl), libPath + "/" + libPathSplit.Last());
|
|
|
+ waitWhileBisy(ref webClient);
|
|
|
+ }
|
|
|
+ */
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //unpacking native libs
|
|
|
+ if (((library.natives is not null || library.downloads.classifiers is not null) && File.Exists(libPath + "/" + libPathSplit.Last())) /*&& chksum != sha1*/)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (!((
|
|
|
+ library.downloads.classifiers.NativesWindows is not null ||
|
|
|
+ library.downloads.classifiers.NativesWindows64 is not null ||
|
|
|
+ library.downloads.classifiers.NativesWindows32 is not null) && OperatingSystem.IsWindows() ||
|
|
|
+ library.downloads.classifiers.NativesLinux is not null && OperatingSystem.IsLinux()))
|
|
|
+ continue;
|
|
|
+
|
|
|
+ if (!Directory.Exists(Settings.MinecraftForlderPath + "versions/" + versionJson.id + "/natives/"))
|
|
|
+ {
|
|
|
+ Settings.logger.Debug("Creating path: {0}", Settings.MinecraftForlderPath + "versions/" + versionJson.id + "/natives/");
|
|
|
+ Directory.CreateDirectory(Settings.MinecraftForlderPath + "versions/" + versionJson.id + "/natives/");
|
|
|
+ }
|
|
|
+ Settings.logger.Debug("Extracting {0} to {1}", libPathSplit.Last(), Settings.MinecraftForlderPath + "versions/" + versionJson.id + "/natives/");
|
|
|
+ ZipFile.ExtractToDirectory(libPath + "/" + libPathSplit.Last(), Settings.MinecraftForlderPath + "versions/" + versionJson.id + "/natives/", true);
|
|
|
+ }
|
|
|
+ catch (IOException ex)
|
|
|
+ {
|
|
|
+ Settings.logger.Error("IOException in VersionsDownloaderViewModel on native lib extraction");
|
|
|
+ }
|
|
|
+ //TODO delete META-INF and sha1 files after
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var assetsJson = Downloader.DownloadAndDeserializeJsonData<AssetsManifest>(versionJson.assetIndex.url);
|
|
|
+ var assetsPath = Settings.MinecraftForlderPath + "assets/" + versionJson.assets + "/objects";
|
|
|
+ var assetsUrl = "https://resources.download.minecraft.net/";
|
|
|
+
|
|
|
+ //download assets json
|
|
|
+ Settings.logger.Debug("Downloading: {0}", versionJson.assets + ".json");
|
|
|
+
|
|
|
+ if (!Directory.Exists(Settings.MinecraftForlderPath + "/assets/" + versionJson.assets + "/indexes/"))
|
|
|
+ Directory.CreateDirectory(Settings.MinecraftForlderPath + "/assets/" + versionJson.assets + "/indexes/");
|
|
|
+ webClient.DownloadFileAsync(new System.Uri(versionJson.assetIndex.url), Settings.MinecraftForlderPath + "/assets/" + versionJson.assets + "/indexes/" + versionJson.assets + ".json");
|
|
|
+
|
|
|
+ //download assets
|
|
|
+ foreach (var asset in assetsJson.Objects)
|
|
|
+ {
|
|
|
+ var folder = asset.Value.hash.Substring(0, 2);
|
|
|
+ if (!Directory.Exists(assetsPath + "/" + folder))
|
|
|
+ Directory.CreateDirectory(assetsPath + "/" + folder);
|
|
|
+
|
|
|
+ chksum = String.Empty;
|
|
|
+ //here hash check
|
|
|
+ if (File.Exists(assetsPath + "/" + folder + "/" + asset.Value.hash))
|
|
|
+ {
|
|
|
+ DownloadingFileName($"Checking hash: {asset.Value.hash}");
|
|
|
+ FileStream fop = File.OpenRead(assetsPath + "/" + folder + "/" + asset.Value.hash);
|
|
|
+ byte[] hash = System.Security.Cryptography.SHA1.Create().ComputeHash(fop);
|
|
|
+ chksum = BitConverter.ToString(hash).Replace("-", String.Empty).ToLower();
|
|
|
+
|
|
|
+ fop.Close();
|
|
|
+ fop.Dispose();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (chksum != asset.Value.hash)
|
|
|
+ {
|
|
|
+ Settings.logger.Debug("Downloading: {0}", asset.Value.hash);
|
|
|
+ DownloadingFileName($"Asset {asset.Value.hash}");
|
|
|
+ webClient.DownloadFileAsync(new System.Uri(assetsUrl + folder + "/" + asset.Value.hash), assetsPath + "/" + folder + "/" + asset.Value.hash);
|
|
|
+ waitWhileBisy(ref webClient);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (InstallForge)
|
|
|
+ {
|
|
|
+ var forgePath = $"Forge{versionJson.id}";
|
|
|
+ var forgeUrl = $"https://files.veloe.link/launcher/forge/Forge{versionJson.id}/";
|
|
|
+
|
|
|
+ if (!Directory.Exists($"{Settings.MinecraftForlderPath}versions/Forge{versionJson.id}"))
|
|
|
+ {
|
|
|
+ Settings.logger.Debug("Creating path: {0}", $"{Settings.MinecraftForlderPath}versions/" + forgePath);
|
|
|
+ Directory.CreateDirectory($"{Settings.MinecraftForlderPath}versions/" + forgePath);
|
|
|
+ }
|
|
|
+ waitWhileBisy(ref webClient);
|
|
|
+
|
|
|
+ Settings.logger.Debug("Downloading: {0}", $"Forge{versionJson.id}.json");
|
|
|
+ DownloadingFileName($"Forge{versionJson.id}.json");
|
|
|
+ webClient.DownloadFileAsync(new System.Uri($"{forgeUrl}Forge{versionJson.id}.json"), Settings.MinecraftForlderPath + "versions/" + forgePath + "/" + forgePath + ".json");
|
|
|
+ waitWhileBisy(ref webClient);
|
|
|
+
|
|
|
+ Settings.logger.Debug("Downloading: {0}", "libraries.zip");
|
|
|
+ DownloadingFileName("libraries.zip");
|
|
|
+ webClient.DownloadFileAsync(new System.Uri(forgeUrl + "libraries.zip"), Settings.MinecraftForlderPath + "versions/" + forgePath + "/libraries.zip");
|
|
|
+ waitWhileBisy(ref webClient);
|
|
|
+
|
|
|
+ Settings.logger.Debug("Extracting: {0}", "libraries.zip");
|
|
|
+ DownloadingFileName("Unpacking libraries.zip");
|
|
|
+ ZipFile.ExtractToDirectory($"{Settings.MinecraftForlderPath}versions/Forge{versionJson.id}/libraries.zip", Settings.MinecraftForlderPath, true);
|
|
|
+ File.Delete($"{Settings.MinecraftForlderPath}versions/Forge{versionJson.id}/libraries.zip");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (InstallOptifine)
|
|
|
+ {
|
|
|
+ var optifinePath = $"Optifine{versionJson.id}";
|
|
|
+ var optifineUrl = $"https://files.veloe.link/launcher/optifine/Optifine{versionJson.id}/";
|
|
|
+
|
|
|
+ if (!Directory.Exists($"{Settings.MinecraftForlderPath}versions/Optifine{versionJson.id}"))
|
|
|
+ {
|
|
|
+ Settings.logger.Debug("Creating path: {0}", $"{Settings.MinecraftForlderPath}Optifine/" + optifinePath);
|
|
|
+ Directory.CreateDirectory($"{Settings.MinecraftForlderPath}Optifine/" + optifinePath);
|
|
|
+ }
|
|
|
+
|
|
|
+ Settings.logger.Debug("Downloading: {0}", $"Optifine{versionJson.id}.json");
|
|
|
+ DownloadingFileName($"Optifine{versionJson.id}.json");
|
|
|
+ webClient.DownloadFileAsync(new System.Uri($"{optifineUrl}Optifine{versionJson.id}.json"), Settings.MinecraftForlderPath + "versions/" + optifinePath + "/" + optifinePath + ".json");
|
|
|
+ waitWhileBisy(ref webClient);
|
|
|
+
|
|
|
+ Settings.logger.Debug("Downloading: {0}", $"Optifine{versionJson.id}.json");
|
|
|
+ DownloadingFileName($"Optifine{versionJson.id}.json");
|
|
|
+ webClient.DownloadFileAsync(new System.Uri($"{optifineUrl}Optifine{versionJson.id}.jar"), Settings.MinecraftForlderPath + "versions/" + optifinePath + "/" + optifinePath + ".jar");
|
|
|
+ waitWhileBisy(ref webClient);
|
|
|
+
|
|
|
+ Settings.logger.Debug("Downloading: {0}", "libraries.zip");
|
|
|
+ DownloadingFileName("libraries.zip");
|
|
|
+ webClient.DownloadFileAsync(new System.Uri(optifineUrl + "libraries.zip"), Settings.MinecraftForlderPath + "versions/" + optifinePath + "/libraries.zip");
|
|
|
+ waitWhileBisy(ref webClient);
|
|
|
+
|
|
|
+ Settings.logger.Debug("Extracting: {0}", "libraries.zip");
|
|
|
+ DownloadingFileName("Unpacking libraries.zip");
|
|
|
+ ZipFile.ExtractToDirectory($"{Settings.MinecraftForlderPath}versions/Optifine{versionJson.id}/libraries.zip", Settings.MinecraftForlderPath, true);
|
|
|
+ File.Delete($"{Settings.MinecraftForlderPath}versions/Optifine{versionJson.id}/libraries.zip");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (InstallForgeOptifine)
|
|
|
+ {
|
|
|
+ if (!Directory.Exists($"{Settings.MinecraftForlderPath}versions/Forge{versionJson.id}/mods"))
|
|
|
+ {
|
|
|
+ Settings.logger.Debug("Creating path: {0}", $"{Settings.MinecraftForlderPath}versions/Forge" + versionJson.id + "/mods");
|
|
|
+ Directory.CreateDirectory($"{Settings.MinecraftForlderPath}versions/Forge" + versionJson.id + "/mods");
|
|
|
+ }
|
|
|
+
|
|
|
+ Settings.logger.Debug("Downloading: {0}", $"Optifine{versionJson.id}.jar");
|
|
|
+ DownloadingFileName($"Optifine{versionJson.id}.jar");
|
|
|
+ webClient.DownloadFileAsync(new System.Uri(@$"https://files.veloe.link/launcher/forge/Forge{versionJson.id}/Optifine{versionJson.id}.jar"),
|
|
|
+ Settings.MinecraftForlderPath + "versions/Forge" + versionJson.id + "/mods/" + "Optifine" + versionJson.id + ".jar");
|
|
|
+ waitWhileBisy(ref webClient);
|
|
|
+ }
|
|
|
+
|
|
|
+ RemoveProgressBar(webClient);
|
|
|
+ //Progress = 100;
|
|
|
+ Settings.logger.Debug("Downloading finished.");
|
|
|
+ DownloadingFileName("Finished!");
|
|
|
+ IsControlsEnabled(true);
|
|
|
+
|
|
|
+ webClient.Dispose();
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ IsControlsEnabled(true);
|
|
|
+ DownloadingFileName($"Error occured while downloading {versionJson.id}.");
|
|
|
+ Settings.logger.Error(ex.Message);
|
|
|
+ Settings.logger.Error(ex.StackTrace);
|
|
|
+ return TaskStatus.Faulted;
|
|
|
+ }
|
|
|
+ return TaskStatus.RanToCompletion;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static async Task<TaskStatus> StartDownloadMcTfc(Action<string> DownloadingFileName,
|
|
|
+ Action<bool> IsControlsEnabled,
|
|
|
+ Action<WebClient> SetProgressBar,
|
|
|
+ Action<WebClient> RemoveProgressBar,
|
|
|
+ ObservableCollection<Entity.VersionManifest.Version> FilteredVersions,
|
|
|
+ bool DownloadJava = false,
|
|
|
+ bool InstallForge = false,
|
|
|
+ bool InstallOptifine = false,
|
|
|
+ bool InstallForgeOptifine = false,
|
|
|
+ bool InstallFabric = false)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var FilteredVersion = FilteredVersions.Where(x => x.id == "1.12.2").FirstOrDefault();
|
|
|
+
|
|
|
+ if (FilteredVersion is null)
|
|
|
+ return TaskStatus.Faulted;
|
|
|
+
|
|
|
+ InstallForge = true;
|
|
|
+ InstallForgeOptifine = true;
|
|
|
+
|
|
|
+ Settings.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");
|
|
|
+
|
|
|
+ await Downloader.StartDownload(
|
|
|
+ DownloadingFileName,
|
|
|
+ IsControlsEnabled,
|
|
|
+ SetProgressBar,
|
|
|
+ RemoveProgressBar,
|
|
|
+ versionJson,
|
|
|
+ DownloadJava,
|
|
|
+ InstallForge,
|
|
|
+ InstallOptifine,
|
|
|
+ InstallForgeOptifine,
|
|
|
+ InstallFabric);
|
|
|
+
|
|
|
+ IsControlsEnabled(false);
|
|
|
+ var mcTfcUrl = $"https://files.veloe.link/launcher/McTFC/McTFC.zip";
|
|
|
+
|
|
|
+ if (!Directory.Exists($"{Settings.MinecraftForlderPath}versions/McTFC"))
|
|
|
+ {
|
|
|
+ Settings.logger.Debug("Creating path: {0}", $"{Settings.MinecraftForlderPath}versions/McTFC");
|
|
|
+ Directory.CreateDirectory($"{Settings.MinecraftForlderPath}versions/McTFC");
|
|
|
+ }
|
|
|
+
|
|
|
+ WebClient webClient = new WebClient();
|
|
|
+
|
|
|
+ SetProgressBar(webClient);
|
|
|
+
|
|
|
+ Settings.logger.Debug("Downloading: {0}", "McTFC.zip");
|
|
|
+ DownloadingFileName("McTFC.zip");
|
|
|
+ webClient.DownloadFileAsync(new System.Uri(mcTfcUrl), Settings.MinecraftForlderPath + "versions/McTFC.zip");
|
|
|
+ waitWhileBisy(ref webClient);
|
|
|
+
|
|
|
+ Settings.logger.Debug("Extracting: {0}", "McTFC.zip");
|
|
|
+ DownloadingFileName("Unpacking McTFC.zip");
|
|
|
+ ZipFile.ExtractToDirectory($"{Settings.MinecraftForlderPath}versions/McTFC.zip", Settings.MinecraftForlderPath + "versions/McTFC", true);
|
|
|
+ File.Delete($"{Settings.MinecraftForlderPath}versions/McTFC.zip");
|
|
|
+
|
|
|
+ RemoveProgressBar(webClient);
|
|
|
+
|
|
|
+ //Progress = 100;
|
|
|
+ Settings.logger.Debug("Downloading McTFC finished.");
|
|
|
+ DownloadingFileName("Finished McTFC!");
|
|
|
+ IsControlsEnabled(true);
|
|
|
+ webClient.Dispose();
|
|
|
+
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ IsControlsEnabled(true);
|
|
|
+ DownloadingFileName("Error occured while downloading McTFC.");
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ Dispatcher.UIThread.InvokeAsync(() =>
|
|
|
+ {
|
|
|
+ if (Avalonia.Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
|
|
+ {
|
|
|
+ Dispatcher.UIThread.InvokeAsync(() =>
|
|
|
+ {
|
|
|
+ var ErrorMessageViewModel = new ErrorWindowViewModel(message, stackTrace);
|
|
|
+ var ErrorMessage = new ErrorWindow()
|
|
|
+ {
|
|
|
+ DataContext = ErrorMessageViewModel
|
|
|
+ };
|
|
|
+ ErrorMessage.ShowDialog(desktop.MainWindow);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return TaskStatus.Faulted;
|
|
|
+ }
|
|
|
+ return TaskStatus.RanToCompletion;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static T DownloadAndDeserializeJsonData<T>(string url, string path = "", string filename = "") where T : new()
|
|
|
{
|
|
|
using (var webClient = new HttpClient())
|
|
|
{
|
|
@@ -25,6 +567,31 @@ namespace VeloeMinecraftLauncher.MinecraftLauncher
|
|
|
{
|
|
|
Settings.logger.Debug($"Downloading: {url.Split('/').Last()}");
|
|
|
jsonData = webClient.GetStringAsync(url).Result;
|
|
|
+
|
|
|
+
|
|
|
+ if (string.IsNullOrEmpty(jsonData))
|
|
|
+ {
|
|
|
+ Settings.logger.Warning("Empty string!");
|
|
|
+ return new T();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if (filename != "")
|
|
|
+ {
|
|
|
+ if (!Directory.Exists(path))
|
|
|
+ {
|
|
|
+ Settings.logger.Debug("Creating path: {0}", path);
|
|
|
+ Directory.CreateDirectory(path);
|
|
|
+ }
|
|
|
+ File.WriteAllText(path + filename, jsonData);
|
|
|
+ }
|
|
|
+ //Settings.logger.Debug("Return serialized string.");
|
|
|
+ return JsonSerializer.Deserialize<T>(jsonData);
|
|
|
+ }
|
|
|
+ /*
|
|
|
+ return string.IsNullOrEmpty(jsonData)
|
|
|
+ ? new T()
|
|
|
+ : JsonSerializer.Deserialize<T>(jsonData);*/
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
@@ -57,33 +624,16 @@ namespace VeloeMinecraftLauncher.MinecraftLauncher
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- if (string.IsNullOrEmpty(jsonData))
|
|
|
- {
|
|
|
- Settings.logger.Warning("Empty string!");
|
|
|
return new T();
|
|
|
}
|
|
|
- else
|
|
|
- {
|
|
|
- //Settings.logger.Debug("Return serialized string.");
|
|
|
- return JsonSerializer.Deserialize<T>(jsonData);
|
|
|
- }
|
|
|
- /*
|
|
|
- return string.IsNullOrEmpty(jsonData)
|
|
|
- ? new T()
|
|
|
- : JsonSerializer.Deserialize<T>(jsonData);*/
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static void DownloadFile(string url, string path, string filename)
|
|
|
+ private static void waitWhileBisy(ref WebClient webClient)
|
|
|
{
|
|
|
- using (var webClient = new WebClient())
|
|
|
+ while (webClient.IsBusy)
|
|
|
{
|
|
|
- if (!Directory.Exists(path))
|
|
|
- Directory.CreateDirectory(path);
|
|
|
- webClient.DownloadFileAsync(new System.Uri(url), path + "/" + filename);
|
|
|
+ Task.Delay(100).Wait();
|
|
|
}
|
|
|
}
|
|
|
}
|