From 3015e70c1e63f1013fceacb2e3c3081bf764eaa1 Mon Sep 17 00:00:00 2001 From: AlphaBs Date: Sat, 5 Jun 2021 23:14:18 +0900 Subject: [PATCH 01/15] allow empty options.txt file in GameOptionsFile.cs --- CmlLib/Utils/GameOptionsFile.cs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/CmlLib/Utils/GameOptionsFile.cs b/CmlLib/Utils/GameOptionsFile.cs index 1df8aee..de365e5 100644 --- a/CmlLib/Utils/GameOptionsFile.cs +++ b/CmlLib/Utils/GameOptionsFile.cs @@ -1,4 +1,5 @@ -using System.Collections; +using System; +using System.Collections; using System.Collections.Generic; using System.Globalization; using System.IO; @@ -59,7 +60,17 @@ public static KeyValuePair FromKeyValueString(string keyvalue) public string FilePath { get; private set; } private readonly Dictionary options; - private GameOptionsFile(Dictionary options, string path) + public GameOptionsFile() + { + this.options = new Dictionary(); + } + + public GameOptionsFile(Dictionary options) + { + this.options = options; + } + + public GameOptionsFile(Dictionary options, string path) { this.options = options; this.FilePath = path; @@ -166,6 +177,8 @@ public void SetValue(string key, object obj) public void Save() { + if (string.IsNullOrEmpty(FilePath)) + throw new ArgumentNullException("this.FilePath was null"); Save(FilePath); } From de7ddb203d308df29c6403dec8f61ae798f6a0fc Mon Sep 17 00:00:00 2001 From: AlphaBs Date: Sat, 5 Jun 2021 23:15:52 +0900 Subject: [PATCH 02/15] fix LocalVersionLoader.cs if version directory is not existent --- CmlLib/Core/VersionLoader/LocalVersionLoader.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CmlLib/Core/VersionLoader/LocalVersionLoader.cs b/CmlLib/Core/VersionLoader/LocalVersionLoader.cs index 1722bdc..9669256 100644 --- a/CmlLib/Core/VersionLoader/LocalVersionLoader.cs +++ b/CmlLib/Core/VersionLoader/LocalVersionLoader.cs @@ -27,7 +27,11 @@ public Task GetVersionMetadatasAsync() private List getFromLocal(MinecraftPath path) { - var dirs = new DirectoryInfo(path.Versions).GetDirectories(); + var versionDirectory = new DirectoryInfo(path.Versions); + if (!versionDirectory.Exists) + return new List(); + + var dirs = versionDirectory.GetDirectories(); var arr = new List(dirs.Length); for (int i = 0; i < dirs.Length; i++) From 8ae351f09e4c7107a17f460470070214e6a66088 Mon Sep 17 00:00:00 2001 From: AlphaBs Date: Sun, 6 Jun 2021 13:04:49 +0900 Subject: [PATCH 03/15] add DownloadFileChangedEventArgs.IsDownloader to determine event source --- CmlLib/Core/CMLauncher.cs | 8 ++++---- CmlLib/Core/Downloader/AsyncParallelDownloader.cs | 4 ++-- CmlLib/Core/Downloader/DownloadFileChangedEventArgs.cs | 4 +++- CmlLib/Core/Downloader/ParallelDownloader.cs | 2 +- CmlLib/Core/Downloader/SequenceDownloader.cs | 4 ++-- CmlLib/Core/Files/AssetChecker.cs | 2 +- CmlLib/Core/Files/ClientChecker.cs | 8 ++++---- CmlLib/Core/Files/LibraryChecker.cs | 2 +- CmlLib/Core/Files/ModChecker.cs | 4 ++-- CmlLib/Core/Installer/MForge.cs | 2 +- 10 files changed, 21 insertions(+), 19 deletions(-) diff --git a/CmlLib/Core/CMLauncher.cs b/CmlLib/Core/CMLauncher.cs index 03cf8ce..6154f3f 100644 --- a/CmlLib/Core/CMLauncher.cs +++ b/CmlLib/Core/CMLauncher.cs @@ -80,28 +80,28 @@ public async Task GetVersionAsync(string versionname) public string CheckJRE() { pFileChanged.Report( - new DownloadFileChangedEventArgs(MFile.Runtime, "java", 1, 0)); + new DownloadFileChangedEventArgs(MFile.Runtime, true, "java", 1, 0)); var mjava = new MJava(); mjava.ProgressChanged += (sender, e) => pProgressChanged.Report(e); var j = mjava.CheckJava(); pFileChanged.Report( - new DownloadFileChangedEventArgs(MFile.Runtime, "java", 1, 1)); + new DownloadFileChangedEventArgs(MFile.Runtime, true, "java", 1, 1)); return j; } public async Task CheckJREAsync() { pFileChanged.Report( - new DownloadFileChangedEventArgs(MFile.Runtime, "java", 1, 0)); + new DownloadFileChangedEventArgs(MFile.Runtime, true, "java", 1, 0)); var mjava = new MJava(); var j = await mjava.CheckJavaAsync(pProgressChanged) .ConfigureAwait(false); pFileChanged.Report( - new DownloadFileChangedEventArgs(MFile.Runtime, "java", 1, 1)); + new DownloadFileChangedEventArgs(MFile.Runtime, true, "java", 1, 1)); return j; } diff --git a/CmlLib/Core/Downloader/AsyncParallelDownloader.cs b/CmlLib/Core/Downloader/AsyncParallelDownloader.cs index afb2483..47c7059 100644 --- a/CmlLib/Core/Downloader/AsyncParallelDownloader.cs +++ b/CmlLib/Core/Downloader/AsyncParallelDownloader.cs @@ -64,7 +64,7 @@ public async Task DownloadFiles(DownloadFile[] files, } fileProgress?.Report( - new DownloadFileChangedEventArgs(files[0].Type, null, files.Length, 0)); + new DownloadFileChangedEventArgs(files[0].Type, true, null, files.Length, 0)); await ForEachAsyncSemaphore(files, MaxThread, doDownload).ConfigureAwait(false); isRunning = false; @@ -127,7 +127,7 @@ private async Task doDownload(DownloadFile file, int retry) Interlocked.Increment(ref progressedFiles); pChangeFile?.Report( - new DownloadFileChangedEventArgs(file.Type, file.Name, totalFiles, progressedFiles)); + new DownloadFileChangedEventArgs(file.Type, true, file.Name, totalFiles, progressedFiles)); } catch (Exception ex) { diff --git a/CmlLib/Core/Downloader/DownloadFileChangedEventArgs.cs b/CmlLib/Core/Downloader/DownloadFileChangedEventArgs.cs index 8e70dd8..decc679 100644 --- a/CmlLib/Core/Downloader/DownloadFileChangedEventArgs.cs +++ b/CmlLib/Core/Downloader/DownloadFileChangedEventArgs.cs @@ -8,17 +8,19 @@ public enum MFile { Runtime, Library, Resource, Minecraft, Others } public class DownloadFileChangedEventArgs : EventArgs { - public DownloadFileChangedEventArgs(MFile kind, string? filename, int total, int progressed) + public DownloadFileChangedEventArgs(MFile kind, bool isDownloader, string? filename, int total, int progressed) { FileKind = kind; FileName = filename; TotalFileCount = total; ProgressedFileCount = progressed; + this.IsDownloader = isDownloader; } public MFile FileKind { get; private set; } public string? FileName { get; private set; } public int TotalFileCount { get; private set; } public int ProgressedFileCount { get; private set; } + public bool IsDownloader { get; private set; } } } diff --git a/CmlLib/Core/Downloader/ParallelDownloader.cs b/CmlLib/Core/Downloader/ParallelDownloader.cs index 45db1aa..754a470 100644 --- a/CmlLib/Core/Downloader/ParallelDownloader.cs +++ b/CmlLib/Core/Downloader/ParallelDownloader.cs @@ -99,7 +99,7 @@ private bool doDownload(DownloadFile file, int failedCount) private void fireDownloadFileChangedEvent(MFile file, string name, int totalFiles, int progressedFiles) { - var e = new DownloadFileChangedEventArgs(file, name, totalFiles, progressedFiles); + var e = new DownloadFileChangedEventArgs(file, true, name, totalFiles, progressedFiles); fireDownloadFileChangedEvent(e); } diff --git a/CmlLib/Core/Downloader/SequenceDownloader.cs b/CmlLib/Core/Downloader/SequenceDownloader.cs index c2453af..3ab5b1c 100644 --- a/CmlLib/Core/Downloader/SequenceDownloader.cs +++ b/CmlLib/Core/Downloader/SequenceDownloader.cs @@ -24,7 +24,7 @@ public async Task DownloadFiles(DownloadFile[] files, downloader.FileDownloadProgressChanged += Downloader_FileDownloadProgressChanged; fileProgress?.Report( - new DownloadFileChangedEventArgs(files[0].Type, null, files.Length, 0)); + new DownloadFileChangedEventArgs(files[0].Type, true, null, files.Length, 0)); for (int i = 0; i < files.Length; i++) { @@ -47,7 +47,7 @@ public async Task DownloadFiles(DownloadFile[] files, } fileProgress?.Report( - new DownloadFileChangedEventArgs(file.Type, file.Name, files.Length, i)); + new DownloadFileChangedEventArgs(file.Type, true, file.Name, files.Length, i)); } catch (Exception ex) { diff --git a/CmlLib/Core/Files/AssetChecker.cs b/CmlLib/Core/Files/AssetChecker.cs index 8303c40..de92be1 100644 --- a/CmlLib/Core/Files/AssetChecker.cs +++ b/CmlLib/Core/Files/AssetChecker.cs @@ -116,7 +116,7 @@ private void checkIndex(MinecraftPath path, MVersion version) if (progressed % 50 == 0) // prevent ui freezing progress?.Report( - new DownloadFileChangedEventArgs(MFile.Resource, "", total, progressed)); + new DownloadFileChangedEventArgs(MFile.Resource, false, "", total, progressed)); } return downloadRequiredFiles.Distinct().ToArray(); // 10ms diff --git a/CmlLib/Core/Files/ClientChecker.cs b/CmlLib/Core/Files/ClientChecker.cs index 0348196..527c28c 100644 --- a/CmlLib/Core/Files/ClientChecker.cs +++ b/CmlLib/Core/Files/ClientChecker.cs @@ -13,9 +13,9 @@ public sealed class ClientChecker : IFileChecker public DownloadFile[]? CheckFiles(MinecraftPath path, MVersion version, IProgress? progress) { - progress?.Report(new DownloadFileChangedEventArgs(MFile.Minecraft, version.Jar, 1, 0)); + progress?.Report(new DownloadFileChangedEventArgs(MFile.Minecraft, false, version.Jar, 1, 0)); DownloadFile? result = checkClientFile(path, version); - progress?.Report(new DownloadFileChangedEventArgs(MFile.Minecraft, version.Jar, 1, 1)); + progress?.Report(new DownloadFileChangedEventArgs(MFile.Minecraft, false, version.Jar, 1, 1)); if (result == null) return null; @@ -26,10 +26,10 @@ public sealed class ClientChecker : IFileChecker public async Task CheckFilesTaskAsync(MinecraftPath path, MVersion version, IProgress? progress) { - progress?.Report(new DownloadFileChangedEventArgs(MFile.Minecraft, version.Jar, 1, 0)); + progress?.Report(new DownloadFileChangedEventArgs(MFile.Minecraft, false, version.Jar, 1, 0)); DownloadFile? result = await Task.Run(() => checkClientFile(path, version)) .ConfigureAwait(false); - progress?.Report(new DownloadFileChangedEventArgs(MFile.Minecraft, version.Jar, 1, 1)); + progress?.Report(new DownloadFileChangedEventArgs(MFile.Minecraft, false, version.Jar, 1, 1)); if (result == null) return null; diff --git a/CmlLib/Core/Files/LibraryChecker.cs b/CmlLib/Core/Files/LibraryChecker.cs index db5509a..9597711 100644 --- a/CmlLib/Core/Files/LibraryChecker.cs +++ b/CmlLib/Core/Files/LibraryChecker.cs @@ -82,7 +82,7 @@ public string LibraryServer progressed++; progress?.Report(new DownloadFileChangedEventArgs( - MFile.Library, library.Name, libs.Length, progressed)); + MFile.Library, false, library.Name, libs.Length, progressed)); } return files.Distinct().ToArray(); } diff --git a/CmlLib/Core/Files/ModChecker.cs b/CmlLib/Core/Files/ModChecker.cs index c96589e..fb9e6c7 100644 --- a/CmlLib/Core/Files/ModChecker.cs +++ b/CmlLib/Core/Files/ModChecker.cs @@ -50,11 +50,11 @@ public class ModChecker : IFileChecker progressed++; progress?.Report(new DownloadFileChangedEventArgs( - MFile.Others, mod.Name, Mods.Length, progressed)); + MFile.Others, false, mod.Name, Mods.Length, progressed)); } progress?.Report(new DownloadFileChangedEventArgs( - MFile.Others, lastModName, Mods.Length, Mods.Length)); + MFile.Others, false, lastModName, Mods.Length, Mods.Length)); return files.Distinct().ToArray(); } diff --git a/CmlLib/Core/Installer/MForge.cs b/CmlLib/Core/Installer/MForge.cs index 255b5f6..ca3f238 100644 --- a/CmlLib/Core/Installer/MForge.cs +++ b/CmlLib/Core/Installer/MForge.cs @@ -368,7 +368,7 @@ private void writeProfile(JToken profileObj, string versionPath) private void fireEvent(MFile kind, string name, int total, int progressed) { - FileChanged?.Invoke(new DownloadFileChangedEventArgs(kind, name, total, progressed)); + FileChanged?.Invoke(new DownloadFileChangedEventArgs(kind, false, name, total, progressed)); } } } From 10f1215b34020adde1334c623447aaa849390c10 Mon Sep 17 00:00:00 2001 From: AlphaBs Date: Sun, 6 Jun 2021 13:05:31 +0900 Subject: [PATCH 04/15] move MinecraftPath.NormalizePath to IOUtil --- CmlLib/Core/MinecraftPath.cs | 5 ++--- CmlLib/Utils/IOUtil.cs | 7 +++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/CmlLib/Core/MinecraftPath.cs b/CmlLib/Core/MinecraftPath.cs index 1ecdeaf..a56d644 100644 --- a/CmlLib/Core/MinecraftPath.cs +++ b/CmlLib/Core/MinecraftPath.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using CmlLib.Utils; namespace CmlLib.Core { @@ -100,9 +101,7 @@ protected static string Dir(string path) protected static string NormalizePath(string path) { - return Path.GetFullPath(path) - .Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar) - .TrimEnd(Path.DirectorySeparatorChar); + return IOUtil.NormalizePath(path); } } } diff --git a/CmlLib/Utils/IOUtil.cs b/CmlLib/Utils/IOUtil.cs index 27ebe70..5132fbe 100644 --- a/CmlLib/Utils/IOUtil.cs +++ b/CmlLib/Utils/IOUtil.cs @@ -12,6 +12,13 @@ internal static class IOUtil { private const int DefaultBufferSize = 4096; + public static string NormalizePath(string path) + { + return Path.GetFullPath(path) + .Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar) + .TrimEnd(Path.DirectorySeparatorChar); + } + public static void DeleteDirectory(string target_dir) { try From d965339d7ac53af1ba589293c0cb1c609301ebbe Mon Sep 17 00:00:00 2001 From: AlphaBs Date: Sun, 6 Jun 2021 13:05:53 +0900 Subject: [PATCH 05/15] do not use any proxy to improve performance --- CmlLib/Utils/WebDownload.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CmlLib/Utils/WebDownload.cs b/CmlLib/Utils/WebDownload.cs index b9f94fb..dd837e4 100644 --- a/CmlLib/Utils/WebDownload.cs +++ b/CmlLib/Utils/WebDownload.cs @@ -15,6 +15,8 @@ protected override WebRequest GetWebRequest(Uri uri) { WebRequest w = base.GetWebRequest(uri); w.Timeout = 20 * 1000; + w.Proxy = null; + this.Proxy = null; return w; } } From 7c5a78f803242f2d5df5b87697a9aead2392ef7c Mon Sep 17 00:00:00 2001 From: AlphaBs Date: Sun, 6 Jun 2021 13:06:10 +0900 Subject: [PATCH 06/15] parse JavaVersion --- CmlLib/Core/Version/MVersion.cs | 37 +++++++++++++++++---------- CmlLib/Core/Version/MVersionParser.cs | 9 ++++--- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/CmlLib/Core/Version/MVersion.cs b/CmlLib/Core/Version/MVersion.cs index 1b716af..59e85bc 100644 --- a/CmlLib/Core/Version/MVersion.cs +++ b/CmlLib/Core/Version/MVersion.cs @@ -15,27 +15,33 @@ public MVersion(string id) public string Id { get; set; } - public string? AssetId { get; set; } = ""; - public string? AssetUrl { get; set; } = ""; - public string? AssetHash { get; set; } = ""; - - public string? Jar { get; set; } = ""; - public string? ClientDownloadUrl { get; set; } = ""; - public string? ClientHash { get; set; } = ""; + public string? AssetId { get; set; } + public string? AssetUrl { get; set; } + public string? AssetHash { get; set; } + + public string? JavaVersion { get; set; } + public string? Jar { get; set; } + public string? ClientDownloadUrl { get; set; } + public string? ClientHash { get; set; } public MLibrary[]? Libraries { get; set; } - public string? MainClass { get; set; } = ""; - public string? MinecraftArguments { get; set; } = ""; + public string? MainClass { get; set; } + public string? MinecraftArguments { get; set; } public string[]? GameArguments { get; set; } public string[]? JvmArguments { get; set; } - public string? ReleaseTime { get; set; } = ""; + public string? ReleaseTime { get; set; } public MVersionType Type { get; set; } = MVersionType.Custom; - public string? TypeStr { get; set; } = ""; + public string? TypeStr { get; set; } public void InheritFrom(MVersion parentVersion) { - // Inherit list - // Overload : AssetId, AssetUrl, AssetHash, ClientDownloadUrl, ClientHash, MainClass, MinecraftArguments - // Combine : Libraries, GameArguments, JvmArguments + /* + Overload : + AssetId, AssetUrl, AssetHash, ClientDownloadUrl, + ClientHash, MainClass, MinecraftArguments, JavaVersion + + Combine : + Libraries, GameArguments, JvmArguments + */ // Overloads @@ -60,6 +66,9 @@ public void InheritFrom(MVersion parentVersion) if (nc(MinecraftArguments)) MinecraftArguments = parentVersion.MinecraftArguments; + if (nc(JavaVersion)) + JavaVersion = parentVersion.JavaVersion; + Jar = parentVersion.Jar; // Combine diff --git a/CmlLib/Core/Version/MVersionParser.cs b/CmlLib/Core/Version/MVersionParser.cs index aedf179..78e7ca8 100644 --- a/CmlLib/Core/Version/MVersionParser.cs +++ b/CmlLib/Core/Version/MVersionParser.cs @@ -85,9 +85,12 @@ public static MVersion ParseFromJson(string json) var id = job["id"]?.ToString(); if (string.IsNullOrEmpty(id)) throw new MVersionParseException("Empty version id"); - + var version = new MVersion(id); + // javaVersion + version.JavaVersion = job["javaVersion"]?["component"]?.ToString(); + // assets var assetindex = job["assetIndex"]; var assets = job["assets"]; @@ -127,9 +130,7 @@ public static MVersion ParseFromJson(string json) version.MainClass = job["mainClass"]?.ToString(); // argument - var ma = job["minecraftArguments"]?.ToString(); - if (ma != null) - version.MinecraftArguments = ma; + version.MinecraftArguments = job["minecraftArguments"]?.ToString(); var ag = job["arguments"]; if (ag != null) From 63683b839dfd933ad64a534b5b796521eacc255c Mon Sep 17 00:00:00 2001 From: AlphaBs Date: Sun, 6 Jun 2021 15:36:07 +0900 Subject: [PATCH 07/15] add JavaChecker.cs, use new java installer --- CmlLib/Core/CMLauncher.cs | 40 +--- CmlLib/Core/Files/FileCheckerCollection.cs | 7 +- CmlLib/Core/Files/JavaChecker.cs | 216 +++++++++++++++++++++ CmlLib/Core/Installer/MJava.cs | 43 ++-- CmlLib/Core/Launcher/MLaunch.cs | 11 +- CmlLib/Core/Version/MVersion.cs | 1 + 6 files changed, 253 insertions(+), 65 deletions(-) create mode 100644 CmlLib/Core/Files/JavaChecker.cs diff --git a/CmlLib/Core/CMLauncher.cs b/CmlLib/Core/CMLauncher.cs index 6154f3f..9227e36 100644 --- a/CmlLib/Core/CMLauncher.cs +++ b/CmlLib/Core/CMLauncher.cs @@ -76,35 +76,7 @@ public async Task GetVersionAsync(string versionname) .ConfigureAwait(false); return version; } - - public string CheckJRE() - { - pFileChanged.Report( - new DownloadFileChangedEventArgs(MFile.Runtime, true, "java", 1, 0)); - - var mjava = new MJava(); - mjava.ProgressChanged += (sender, e) => pProgressChanged.Report(e); - var j = mjava.CheckJava(); - - pFileChanged.Report( - new DownloadFileChangedEventArgs(MFile.Runtime, true, "java", 1, 1)); - return j; - } - - public async Task CheckJREAsync() - { - pFileChanged.Report( - new DownloadFileChangedEventArgs(MFile.Runtime, true, "java", 1, 0)); - - var mjava = new MJava(); - var j = await mjava.CheckJavaAsync(pProgressChanged) - .ConfigureAwait(false); - - pFileChanged.Report( - new DownloadFileChangedEventArgs(MFile.Runtime, true, "java", 1, 1)); - return j; - } - + public string CheckForge(string mcversion, string forgeversion, string java) { if (Versions == null) @@ -193,9 +165,6 @@ public async Task CheckAndDownloadAsync(MVersion version) public Process CreateProcess(string mcversion, string forgeversion, MLaunchOption option) { - if (string.IsNullOrEmpty(option.JavaPath)) - option.JavaPath = CheckJRE(); - CheckAndDownload(GetVersion(mcversion)); var versionName = CheckForge(mcversion, forgeversion, option.JavaPath); @@ -229,10 +198,6 @@ public Process CreateProcess(MLaunchOption option) { if (option.Path == null) option.Path = MinecraftPath; - - if (string.IsNullOrEmpty(option.JavaPath)) - option.JavaPath = CheckJRE(); - var launch = new MLaunch(option); return launch.GetProcess(); } @@ -242,9 +207,6 @@ public async Task CreateProcessAsync(MLaunchOption option) if (option.Path == null) option.Path = MinecraftPath; - if (string.IsNullOrEmpty(option.JavaPath)) - option.JavaPath = await CheckJREAsync().ConfigureAwait(false); - var launch = new MLaunch(option); return await Task.Run(launch.GetProcess).ConfigureAwait(false); } diff --git a/CmlLib/Core/Files/FileCheckerCollection.cs b/CmlLib/Core/Files/FileCheckerCollection.cs index 71eb831..02ab89c 100644 --- a/CmlLib/Core/Files/FileCheckerCollection.cs +++ b/CmlLib/Core/Files/FileCheckerCollection.cs @@ -58,6 +58,8 @@ public LibraryChecker? LibraryFileChecker } } + private JavaChecker? java; + public FileCheckerCollection() { checkers = new List(3); @@ -65,10 +67,11 @@ public FileCheckerCollection() library = new LibraryChecker(); asset = new AssetChecker(); client = new ClientChecker(); - + java = new JavaChecker(); + checkers.AddRange(new IFileChecker[] { - library, asset, client + library, asset, client, java }); } diff --git a/CmlLib/Core/Files/JavaChecker.cs b/CmlLib/Core/Files/JavaChecker.cs new file mode 100644 index 0000000..f1456b2 --- /dev/null +++ b/CmlLib/Core/Files/JavaChecker.cs @@ -0,0 +1,216 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Net; +using System.Threading.Tasks; +using CmlLib.Core.Downloader; +using CmlLib.Core.Installer; +using CmlLib.Core.Version; +using CmlLib.Utils; +using Newtonsoft.Json.Linq; + +namespace CmlLib.Core.Files +{ + public class JavaChecker : IFileChecker + { + public bool CheckHash { get; set; } + + public DownloadFile[]? CheckFiles(MinecraftPath path, MVersion version, + IProgress? downloadProgress) + { + var javaVersion = version.JavaVersion; + if (string.IsNullOrEmpty(javaVersion)) + javaVersion = "jre-legacy"; + + version.JavaBinaryPath = Path.Combine(path.Runtime, javaVersion, "bin", getDefaultBinaryName()); + + var osName = getJavaOSName(); + var javaVersions = getJavaVersionsForOS(osName); + if (javaVersions != null) + { + var javaManifest = getJavaVersionManifest(javaVersions, javaVersion); + + if (javaManifest == null) + javaManifest = getJavaVersionManifest(javaVersions, "jre-legacy"); + if (javaManifest == null) + return legacyJavaChecker(path); + + var files = javaManifest?["files"] as JObject; + if (files == null) + return legacyJavaChecker(path); + + return toDownloadFiles(javaVersion, files, path, downloadProgress); + } + else + return legacyJavaChecker(path); + } + + public Task CheckFilesTaskAsync(MinecraftPath path, MVersion version, + IProgress? downloadProgress) + { + return Task.Run(() => CheckFiles(path, version, downloadProgress)); + } + + private string getJavaOSName() + { + string osName = ""; + + if (MRule.OSName == MRule.Windows) + { + if (MRule.Arch == "64") + osName = "windows-x64"; + else + osName = "windows-x86"; + } + else if (MRule.OSName == MRule.Linux) + { + if (MRule.Arch == "64") + osName = "linux"; + else + osName = "linux-i386"; + } + else if (MRule.OSName == MRule.OSX) + { + osName = "mac-os"; + } + + return osName; + } + + private string getDefaultBinaryName() + { + string binaryName = "java"; + if (MRule.OSName == MRule.Windows) + binaryName = "javaw.exe"; + return binaryName; + } + + private JObject? getJavaVersionsForOS(string osName) + { + var url = + "https://launchermeta.mojang.com/v1/products/java-runtime/2ec0cc96c44e5a76b9c8b7c39df7210883d12871/all.json"; + string response; + + using (var wc = new WebClient()) + { + response = wc.DownloadString(url); + } + + var job = JObject.Parse(response); + return job[osName] as JObject; + } + + private JObject? getJavaVersionManifest(JObject job, string version) + { + var versionArr = job[version] as JArray; + if (versionArr == null || versionArr.Count == 0) + return null; + + var manifestUrl = versionArr[0]?["manifest"]?["url"]?.ToString(); + if (string.IsNullOrEmpty(manifestUrl)) + return null; + + string response; + using (var wc = new WebClient()) + { + response = wc.DownloadString(manifestUrl); + } + + return JObject.Parse(response); + } + + private DownloadFile[]? toDownloadFiles(string javaVersionName, JObject manifest, MinecraftPath path, + IProgress? progress) + { + var progressed = 0; + var files = new List(manifest.Count); + string? exeFileName = null; + foreach (var prop in manifest) + { + var name = prop.Key; + var value = prop.Value; + + var type = value?["type"]?.ToString(); + if (type == "file") + { + var filePath = Path.Combine(path.Runtime, javaVersionName, name); + filePath = IOUtil.NormalizePath(filePath); + + bool.TryParse(value?["executable"]?.ToString(), out bool executable); + + var file = checkJavaFile(value, filePath); + if (file != null) + { + file.Name = name; + files.Add(file); + } + } + else + { + if (type != "directory") + Debug.WriteLine(type); + } + + progressed++; + progress?.Report(new DownloadFileChangedEventArgs( + MFile.Runtime, false, name, manifest.Count, progressed)); + } + return files.ToArray(); + } + + private DownloadFile? checkJavaFile(JToken? value, string filePath) + { + var downloadObj = value?["downloads"]?["raw"]; + if (downloadObj == null) + return null; + + var url = downloadObj["url"]?.ToString(); + if (string.IsNullOrEmpty(url)) + return null; + + var hash = downloadObj["sha1"]?.ToString(); + var sizeStr = downloadObj["size"]?.ToString(); + long.TryParse(sizeStr, out long size); + + if (IOUtil.CheckFileValidation(filePath, hash, CheckHash)) + return null; + + return new DownloadFile(filePath, url) + { + Size = size + }; + } + + private DownloadFile[]? legacyJavaChecker(MinecraftPath path) + { + string legacyJavaPath = Path.Combine(path.Runtime, "m-legacy"); + MJava mJava = new MJava(legacyJavaPath); + + if (mJava.CheckJavaExistence()) + return new DownloadFile[] {}; + + string javaUrl = mJava.GetJavaUrl(); + string lzmaPath = Path.Combine(Path.GetTempPath(), "jre.lzma"); + string zipPath = Path.Combine(Path.GetTempPath(), "jre.zip"); + + DownloadFile file = new DownloadFile(lzmaPath, javaUrl) + { + Name = "jre.lzma", + Type = MFile.Runtime, + AfterDownload = new Func[] + { + () => Task.Run(() => + { + SevenZipWrapper.DecompressFileLZMA(lzmaPath, zipPath); + + var z = new SharpZip(zipPath); + z.Unzip(legacyJavaPath); + }) + } + }; + + return new[] {file}; + } + } +} \ No newline at end of file diff --git a/CmlLib/Core/Installer/MJava.cs b/CmlLib/Core/Installer/MJava.cs index c01e73a..be42c2d 100644 --- a/CmlLib/Core/Installer/MJava.cs +++ b/CmlLib/Core/Installer/MJava.cs @@ -13,10 +13,10 @@ public class MJava public static readonly string DefaultRuntimeDirectory = Path.Combine(MinecraftPath.GetOSDefaultPath(), "runtime"); - public event ProgressChangedEventHandler ProgressChanged; + public event ProgressChangedEventHandler? ProgressChanged; public string RuntimeDirectory { get; private set; } - private IProgress pProgressChanged; + private IProgress? pProgressChanged; public MJava() : this(DefaultRuntimeDirectory) { } @@ -33,22 +33,35 @@ public string GetDefaultBinaryName() return binaryName; } + public string GetBinaryPath() + => GetBinaryPath(GetDefaultBinaryName()); + + public string GetBinaryPath(string binaryName) + => Path.Combine(RuntimeDirectory, "bin", binaryName); + public string CheckJava() { string binaryName = GetDefaultBinaryName(); return CheckJava(binaryName); } + public bool CheckJavaExistence() + => CheckJavaExistence(GetDefaultBinaryName()); + + public bool CheckJavaExistence(string binaryName) + => File.Exists(GetBinaryPath(binaryName)); + + public string CheckJava(string binaryName) { pProgressChanged = new Progress( (e) => ProgressChanged?.Invoke(this, e)); - string javapath = Path.Combine(RuntimeDirectory, "bin", binaryName); + string javapath = GetBinaryPath(binaryName); - if (!File.Exists(javapath)) + if (!CheckJavaExistence(binaryName)) { - string javaUrl = getJavaUrl(); + string javaUrl = GetJavaUrl(); string lzmaPath = downloadJavaLzma(javaUrl); decompressJavaFile(lzmaPath); @@ -66,17 +79,17 @@ public string CheckJava(string binaryName) public Task CheckJavaAsync() => CheckJavaAsync(null); - public Task CheckJavaAsync(IProgress progress) + public Task CheckJavaAsync(IProgress? progress) { string binaryName = GetDefaultBinaryName(); return CheckJavaAsync(binaryName, progress); } - public async Task CheckJavaAsync(string binaryName, IProgress progress) + public async Task CheckJavaAsync(string binaryName, IProgress? progress) { - string javapath = Path.Combine(RuntimeDirectory, "bin", binaryName); + string javapath = GetBinaryPath(binaryName); - if (!File.Exists(javapath)) + if (!CheckJavaExistence(binaryName)) { if (progress == null) { @@ -88,7 +101,7 @@ public async Task CheckJavaAsync(string binaryName, IProgress decompressJavaFile(lzmaPath)); @@ -104,7 +117,7 @@ public async Task CheckJavaAsync(string binaryName, IProgress getJavaUrlAsync() + public async Task GetJavaUrlAsync() { using (var wc = new WebClient()) { @@ -174,14 +187,14 @@ private void decompressJavaFile(string lzmaPath) z.Unzip(RuntimeDirectory); } - private void Z_ProgressEvent(object sender, int e) + private void Z_ProgressEvent(object? sender, int e) { - pProgressChanged.Report(new ProgressChangedEventArgs(50 + e / 2, null)); + pProgressChanged?.Report(new ProgressChangedEventArgs(50 + e / 2, null)); } private void Downloader_DownloadProgressChangedEvent(object sender, ProgressChangedEventArgs e) { - pProgressChanged.Report(new ProgressChangedEventArgs(e.ProgressPercentage / 2, null)); + pProgressChanged?.Report(new ProgressChangedEventArgs(e.ProgressPercentage / 2, null)); } } } diff --git a/CmlLib/Core/Launcher/MLaunch.cs b/CmlLib/Core/Launcher/MLaunch.cs index 6c6b53d..5d83b52 100644 --- a/CmlLib/Core/Launcher/MLaunch.cs +++ b/CmlLib/Core/Launcher/MLaunch.cs @@ -33,14 +33,6 @@ public MLaunch(MLaunchOption option) private readonly MinecraftPath minecraftPath; public MLaunchOption LaunchOption { get; private set; } - /// - /// Start Game - /// - public void Start() - { - GetProcess().Start(); - } - /// /// Build game process and return it /// @@ -48,7 +40,8 @@ public Process GetProcess() { string arg = string.Join(" ", CreateArg()); Process mc = new Process(); - mc.StartInfo.FileName = LaunchOption.GetJavaPath(); + mc.StartInfo.FileName = + useNotNull(LaunchOption.GetJavaPath(), LaunchOption.GetStartVersion().JavaBinaryPath); mc.StartInfo.Arguments = arg; mc.StartInfo.WorkingDirectory = minecraftPath.BasePath; diff --git a/CmlLib/Core/Version/MVersion.cs b/CmlLib/Core/Version/MVersion.cs index 59e85bc..497e648 100644 --- a/CmlLib/Core/Version/MVersion.cs +++ b/CmlLib/Core/Version/MVersion.cs @@ -20,6 +20,7 @@ public MVersion(string id) public string? AssetHash { get; set; } public string? JavaVersion { get; set; } + public string? JavaBinaryPath { get; set; } public string? Jar { get; set; } public string? ClientDownloadUrl { get; set; } public string? ClientHash { get; set; } From 8f4e09652fa7bb618033c5add39ba760b2ac46bc Mon Sep 17 00:00:00 2001 From: AlphaBs Date: Sun, 6 Jun 2021 16:29:41 +0900 Subject: [PATCH 08/15] improve JavaChecker API add JavaManifestServer, JavaBinaryName add Chmod755 to set executable java binary file --- CmlLib/Core/Files/FileCheckerCollection.cs | 24 +++++- CmlLib/Core/Files/JavaChecker.cs | 90 ++++++++++++++-------- CmlLib/Core/Installer/MJava.cs | 2 +- CmlLib/Core/MojangServer.cs | 4 +- 4 files changed, 80 insertions(+), 40 deletions(-) diff --git a/CmlLib/Core/Files/FileCheckerCollection.cs b/CmlLib/Core/Files/FileCheckerCollection.cs index 02ab89c..bd1d5a0 100644 --- a/CmlLib/Core/Files/FileCheckerCollection.cs +++ b/CmlLib/Core/Files/FileCheckerCollection.cs @@ -60,6 +60,21 @@ public LibraryChecker? LibraryFileChecker private JavaChecker? java; + public JavaChecker? JavaFileChecker + { + get => java; + set + { + if (java != null) + checkers.Remove(java); + + java = value; + + if (java != null) + checkers.Add(java); + } + } + public FileCheckerCollection() { checkers = new List(3); @@ -81,11 +96,12 @@ public void Add(IFileChecker item) checkers.Add(item); } - public void AddRange(IEnumerable items) + public void AddRange(IEnumerable items) { - foreach (IFileChecker item in items) + foreach (IFileChecker? item in items) { - Add(item); + if (item != null) + Add(item); } } @@ -118,6 +134,8 @@ private void CheckArgument(IFileChecker item) throw new ArgumentException($"Set {nameof(AssetFileChecker)} property."); if (item is ClientChecker) throw new ArgumentException($"Set {nameof(ClientFileChecker)} property."); + if (item is JavaChecker) + throw new ArgumentException($"Set {nameof(JavaFileChecker)} property."); } public IEnumerator GetEnumerator() diff --git a/CmlLib/Core/Files/JavaChecker.cs b/CmlLib/Core/Files/JavaChecker.cs index f1456b2..46dcf41 100644 --- a/CmlLib/Core/Files/JavaChecker.cs +++ b/CmlLib/Core/Files/JavaChecker.cs @@ -14,36 +14,49 @@ namespace CmlLib.Core.Files { public class JavaChecker : IFileChecker { + public string JavaManifestServer { get; set; } = MojangServer.JavaManifest; + public string JavaBinaryName { get; set; } = MJava.GetDefaultBinaryName(); public bool CheckHash { get; set; } public DownloadFile[]? CheckFiles(MinecraftPath path, MVersion version, IProgress? downloadProgress) { + if (!string.IsNullOrEmpty(version.JavaBinaryPath) && File.Exists(version.JavaBinaryPath)) + return null; + var javaVersion = version.JavaVersion; if (string.IsNullOrEmpty(javaVersion)) javaVersion = "jre-legacy"; - version.JavaBinaryPath = Path.Combine(path.Runtime, javaVersion, "bin", getDefaultBinaryName()); + version.JavaBinaryPath = Path.Combine(path.Runtime, javaVersion, "bin", JavaBinaryName); - var osName = getJavaOSName(); - var javaVersions = getJavaVersionsForOS(osName); - if (javaVersions != null) + try { - var javaManifest = getJavaVersionManifest(javaVersions, javaVersion); + var osName = getJavaOSName(); // safe + var javaVersions = getJavaVersionsForOs(osName); // Net, JsonParse Exception + if (javaVersions != null) + { + var javaManifest = getJavaVersionManifest(javaVersions, javaVersion); // Net, JsonParse - if (javaManifest == null) - javaManifest = getJavaVersionManifest(javaVersions, "jre-legacy"); - if (javaManifest == null) - return legacyJavaChecker(path); + if (javaManifest == null) + javaManifest = getJavaVersionManifest(javaVersions, "jre-legacy"); + if (javaManifest == null) + return legacyJavaChecker(path); - var files = javaManifest?["files"] as JObject; - if (files == null) - return legacyJavaChecker(path); + var files = javaManifest["files"] as JObject; + if (files == null) + return legacyJavaChecker(path); - return toDownloadFiles(javaVersion, files, path, downloadProgress); + return toDownloadFiles(javaVersion, files, path, downloadProgress); + } + else + return legacyJavaChecker(path); + } + catch (Exception e) + { + Debug.WriteLine(e); + return null; } - else - return legacyJavaChecker(path); } public Task CheckFilesTaskAsync(MinecraftPath path, MVersion version, @@ -78,26 +91,16 @@ private string getJavaOSName() return osName; } - private string getDefaultBinaryName() + private JObject? getJavaVersionsForOs(string osName) { - string binaryName = "java"; - if (MRule.OSName == MRule.Windows) - binaryName = "javaw.exe"; - return binaryName; - } - - private JObject? getJavaVersionsForOS(string osName) - { - var url = - "https://launchermeta.mojang.com/v1/products/java-runtime/2ec0cc96c44e5a76b9c8b7c39df7210883d12871/all.json"; string response; using (var wc = new WebClient()) { - response = wc.DownloadString(url); + response = wc.DownloadString(JavaManifestServer); // ex } - var job = JObject.Parse(response); + var job = JObject.Parse(response); // ex return job[osName] as JObject; } @@ -107,25 +110,24 @@ private string getDefaultBinaryName() if (versionArr == null || versionArr.Count == 0) return null; - var manifestUrl = versionArr[0]?["manifest"]?["url"]?.ToString(); + var manifestUrl = versionArr[0]["manifest"]?["url"]?.ToString(); if (string.IsNullOrEmpty(manifestUrl)) return null; string response; using (var wc = new WebClient()) { - response = wc.DownloadString(manifestUrl); + response = wc.DownloadString(manifestUrl); // ex } - return JObject.Parse(response); + return JObject.Parse(response); // ex } - private DownloadFile[]? toDownloadFiles(string javaVersionName, JObject manifest, MinecraftPath path, + private DownloadFile[] toDownloadFiles(string javaVersionName, JObject manifest, MinecraftPath path, IProgress? progress) { var progressed = 0; var files = new List(manifest.Count); - string? exeFileName = null; foreach (var prop in manifest) { var name = prop.Key; @@ -143,6 +145,11 @@ private string getDefaultBinaryName() if (file != null) { file.Name = name; + if (executable) + file.AfterDownload = new Func[] + { + () => Task.Run(() => TryChmod755(filePath)) + }; files.Add(file); } } @@ -182,7 +189,7 @@ private string getDefaultBinaryName() }; } - private DownloadFile[]? legacyJavaChecker(MinecraftPath path) + private DownloadFile[] legacyJavaChecker(MinecraftPath path) { string legacyJavaPath = Path.Combine(path.Runtime, "m-legacy"); MJava mJava = new MJava(legacyJavaPath); @@ -206,11 +213,26 @@ private string getDefaultBinaryName() var z = new SharpZip(zipPath); z.Unzip(legacyJavaPath); + + TryChmod755(mJava.GetBinaryPath()); }) } }; return new[] {file}; } + + private void TryChmod755(string path) + { + try + { + if (MRule.OSName != MRule.Windows) + IOUtil.Chmod(path, IOUtil.Chmod755); + } + catch (Exception e) + { + Debug.WriteLine(e); + } + } } } \ No newline at end of file diff --git a/CmlLib/Core/Installer/MJava.cs b/CmlLib/Core/Installer/MJava.cs index be42c2d..fba1486 100644 --- a/CmlLib/Core/Installer/MJava.cs +++ b/CmlLib/Core/Installer/MJava.cs @@ -25,7 +25,7 @@ public MJava(string runtimePath) RuntimeDirectory = runtimePath; } - public string GetDefaultBinaryName() + public static string GetDefaultBinaryName() { string binaryName = "java"; if (MRule.OSName == MRule.Windows) diff --git a/CmlLib/Core/MojangServer.cs b/CmlLib/Core/MojangServer.cs index 3f81e2b..d9e6bda 100644 --- a/CmlLib/Core/MojangServer.cs +++ b/CmlLib/Core/MojangServer.cs @@ -3,11 +3,11 @@ public static class MojangServer { public static readonly string - Auth = "https://authserver.mojang.com/", Version = "https://launchermeta.mojang.com/mc/game/version_manifest.json", Library = "https://libraries.minecraft.net/", ResourceDownload = "http://resources.download.minecraft.net/", - LauncherMeta = "http://launchermeta.mojang.com/mc/launcher.json"; + LauncherMeta = "http://launchermeta.mojang.com/mc/launcher.json", + JavaManifest = "https://launchermeta.mojang.com/v1/products/java-runtime/2ec0cc96c44e5a76b9c8b7c39df7210883d12871/all.json"; } } From 6a78a577971cd51084e186f976fe186ed77c0a07 Mon Sep 17 00:00:00 2001 From: AlphaBs Date: Sun, 6 Jun 2021 16:30:12 +0900 Subject: [PATCH 09/15] expose WebDownload add IgnoreProxy, DefaultWebRequestTimout --- CmlLib/Utils/WebDownload.cs | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/CmlLib/Utils/WebDownload.cs b/CmlLib/Utils/WebDownload.cs index dd837e4..cc2b972 100644 --- a/CmlLib/Utils/WebDownload.cs +++ b/CmlLib/Utils/WebDownload.cs @@ -7,16 +7,24 @@ namespace CmlLib.Utils { - internal class WebDownload + public class WebDownload { + public static bool IgnoreProxy = true; + public static int DefaultWebRequestTimeout = 20 * 1000; + private class TimeoutWebClient : WebClient { protected override WebRequest GetWebRequest(Uri uri) { WebRequest w = base.GetWebRequest(uri); - w.Timeout = 20 * 1000; - w.Proxy = null; - this.Proxy = null; + w.Timeout = DefaultWebRequestTimeout; + + if (IgnoreProxy) + { + w.Proxy = null; + this.Proxy = null; + } + return w; } } @@ -24,10 +32,10 @@ protected override WebRequest GetWebRequest(Uri uri) private static readonly int DefaultBufferSize = 1024 * 64; // 64kb private readonly object locker = new object(); - public event EventHandler? FileDownloadProgressChanged; - public event ProgressChangedEventHandler? DownloadProgressChangedEvent; + internal event EventHandler? FileDownloadProgressChanged; + internal event ProgressChangedEventHandler? DownloadProgressChangedEvent; - public void DownloadFile(string url, string path) + internal void DownloadFile(string url, string path) { var req = WebRequest.CreateHttp(url); // Request var response = req.GetResponse(); @@ -62,7 +70,7 @@ public void DownloadFile(string url, string path) fileStream.Dispose(); } - public async Task DownloadFileAsync(DownloadFile file) + internal async Task DownloadFileAsync(DownloadFile file) { string? directoryName = Path.GetDirectoryName(file.Path); if (!string.IsNullOrEmpty(directoryName)) @@ -92,7 +100,7 @@ await wc.DownloadFileTaskAsync(file.Url, file.Path) } } - public void DownloadFileLimit(string url, string path) + internal void DownloadFileLimit(string url, string path) { string? directoryName = Path.GetDirectoryName(path); if (!string.IsNullOrEmpty(directoryName)) From 0efdd8d18a0093e48a6d1190e3d3ccf31ba0ccf1 Mon Sep 17 00:00:00 2001 From: AlphaBs Date: Mon, 14 Jun 2021 12:46:51 +0900 Subject: [PATCH 10/15] fix JavaChecker: legacyJavaChecker doesn't set JavaBinaryPath property --- CmlLib/Core/Files/JavaChecker.cs | 41 ++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/CmlLib/Core/Files/JavaChecker.cs b/CmlLib/Core/Files/JavaChecker.cs index 46dcf41..2f756b5 100644 --- a/CmlLib/Core/Files/JavaChecker.cs +++ b/CmlLib/Core/Files/JavaChecker.cs @@ -28,7 +28,23 @@ public class JavaChecker : IFileChecker if (string.IsNullOrEmpty(javaVersion)) javaVersion = "jre-legacy"; - version.JavaBinaryPath = Path.Combine(path.Runtime, javaVersion, "bin", JavaBinaryName); + var files = internalCheckFile( + javaVersion, path, downloadProgress, out string binPath); + + version.JavaBinaryPath = binPath; + return files; + } + + public Task CheckFilesTaskAsync(MinecraftPath path, MVersion version, + IProgress? downloadProgress) + { + return Task.Run(() => CheckFiles(path, version, downloadProgress)); + } + + private DownloadFile[]? internalCheckFile(string javaVersion, MinecraftPath path, + IProgress? downloadProgress, out string binPath) + { + binPath = Path.Combine(path.Runtime, javaVersion, "bin", JavaBinaryName); try { @@ -41,30 +57,24 @@ public class JavaChecker : IFileChecker if (javaManifest == null) javaManifest = getJavaVersionManifest(javaVersions, "jre-legacy"); if (javaManifest == null) - return legacyJavaChecker(path); + return legacyJavaChecker(path, out binPath); var files = javaManifest["files"] as JObject; if (files == null) - return legacyJavaChecker(path); + return legacyJavaChecker(path, out binPath); return toDownloadFiles(javaVersion, files, path, downloadProgress); } else - return legacyJavaChecker(path); + return legacyJavaChecker(path, out binPath); } catch (Exception e) { Debug.WriteLine(e); - return null; + return legacyJavaChecker(path, out binPath); } } - public Task CheckFilesTaskAsync(MinecraftPath path, MVersion version, - IProgress? downloadProgress) - { - return Task.Run(() => CheckFiles(path, version, downloadProgress)); - } - private string getJavaOSName() { string osName = ""; @@ -148,7 +158,7 @@ private DownloadFile[] toDownloadFiles(string javaVersionName, JObject manifest, if (executable) file.AfterDownload = new Func[] { - () => Task.Run(() => TryChmod755(filePath)) + () => Task.Run(() => tryChmod755(filePath)) }; files.Add(file); } @@ -189,10 +199,11 @@ private DownloadFile[] toDownloadFiles(string javaVersionName, JObject manifest, }; } - private DownloadFile[] legacyJavaChecker(MinecraftPath path) + private DownloadFile[] legacyJavaChecker(MinecraftPath path, out string binPath) { string legacyJavaPath = Path.Combine(path.Runtime, "m-legacy"); MJava mJava = new MJava(legacyJavaPath); + binPath = mJava.GetBinaryPath(); if (mJava.CheckJavaExistence()) return new DownloadFile[] {}; @@ -214,7 +225,7 @@ private DownloadFile[] legacyJavaChecker(MinecraftPath path) var z = new SharpZip(zipPath); z.Unzip(legacyJavaPath); - TryChmod755(mJava.GetBinaryPath()); + tryChmod755(mJava.GetBinaryPath()); }) } }; @@ -222,7 +233,7 @@ private DownloadFile[] legacyJavaChecker(MinecraftPath path) return new[] {file}; } - private void TryChmod755(string path) + private void tryChmod755(string path) { try { From cad6b02d54f2ad11823e21dcf6f76ec3e605b5cb Mon Sep 17 00:00:00 2001 From: AlphaBs Date: Mon, 14 Jun 2021 12:47:42 +0900 Subject: [PATCH 11/15] add override method of CreateProcess: CreateProcess(MVersion version, MLaunchOption option) --- CmlLib/Core/CMLauncher.cs | 41 ++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/CmlLib/Core/CMLauncher.cs b/CmlLib/Core/CMLauncher.cs index 9227e36..365c33a 100644 --- a/CmlLib/Core/CMLauncher.cs +++ b/CmlLib/Core/CMLauncher.cs @@ -171,11 +171,14 @@ public Process CreateProcess(string mcversion, string forgeversion, MLaunchOptio return CreateProcess(versionName, option); } + + public Process CreateProcess(string versionName, MLaunchOption option) + => CreateProcess(GetVersion(versionName), option); [MethodTimer.Time] - public Process CreateProcess(string versionname, MLaunchOption option) + public Process CreateProcess(MVersion version, MLaunchOption option) { - option.StartVersion = GetVersion(versionname); + option.StartVersion = version; if (this.FileDownloader != null) CheckAndDownload(option.StartVersion); @@ -183,10 +186,15 @@ public Process CreateProcess(string versionname, MLaunchOption option) return CreateProcess(option); } - [MethodTimer.Time] - public async Task CreateProcessAsync(string versionname, MLaunchOption option) + public async Task CreateProcessAsync(string versionName, MLaunchOption option) + { + var version = await GetVersionAsync(versionName).ConfigureAwait(false); + return await CreateProcessAsync(version, option).ConfigureAwait(false); + } + + public async Task CreateProcessAsync(MVersion version, MLaunchOption option) { - option.StartVersion = await GetVersionAsync(versionname).ConfigureAwait(false); + option.StartVersion = version; if (this.FileDownloader != null) await CheckAndDownloadAsync(option.StartVersion).ConfigureAwait(false); @@ -196,34 +204,39 @@ public async Task CreateProcessAsync(string versionname, MLaunchOption public Process CreateProcess(MLaunchOption option) { - if (option.Path == null) - option.Path = MinecraftPath; + checkLaunchOption(option); var launch = new MLaunch(option); return launch.GetProcess(); } public async Task CreateProcessAsync(MLaunchOption option) { - if (option.Path == null) - option.Path = MinecraftPath; - + checkLaunchOption(option); var launch = new MLaunch(option); return await Task.Run(launch.GetProcess).ConfigureAwait(false); } - public Process Launch(string versionname, MLaunchOption option) + public Process Launch(string versionName, MLaunchOption option) { - Process process = CreateProcess(versionname, option); + Process process = CreateProcess(versionName, option); process.Start(); return process; } - public async Task LaunchAsync(string versionname, MLaunchOption option) + public async Task LaunchAsync(string versionName, MLaunchOption option) { - Process process = await CreateProcessAsync(versionname, option) + Process process = await CreateProcessAsync(versionName, option) .ConfigureAwait(false); process.Start(); return process; } + + private void checkLaunchOption(MLaunchOption option) + { + if (option.Path == null) + option.Path = MinecraftPath; + if (!string.IsNullOrEmpty(option.JavaPath) && option.StartVersion != null) + option.StartVersion.JavaBinaryPath = option.JavaPath; + } } } From 8d3f0fac3dd8cbff15ca70dbd842c95ecfe13b11 Mon Sep 17 00:00:00 2001 From: AlphaBs Date: Mon, 14 Jun 2021 12:53:38 +0900 Subject: [PATCH 12/15] 128x128 icon --- icon.png | Bin 2234 -> 4782 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/icon.png b/icon.png index 87440eb0a11820590a62f05a01169c925aee6faf..25f20feec1513493cc441cb28110f7ade426f9ea 100644 GIT binary patch literal 4782 zcmV;f5>f4mP)2h4=f4>2?=SXMQ9)QH8Y*LcO|XRvf9Vp zl~%r=vDig>XTJIUzV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lGcpPIDadXKS}p+bc+N2M^Xs9%PTgJd0fiMriAu)mE<>@IwjfG#eSox$&PMw~ zbzF;-emA|c01RiG+A|P7GqsE^7neIyaEIet;qY;R|K~6d)&o=-f9zhIN835~b>dae8ZKZ=rTf|%gkoIG} zk##mxYOP~WyvU2l4vj7_PVF7!PvvA1YXI?g3@ZW0%iTm`_4OJ~=6X0;*h;B&k0rC>-k@QdR4rTc=JJf!;aG3f|_nFnrZg< z_#1|DEHg&A|x{&rv!3-^@Zg-6Y`&S(x zZu^S2U9E?_EN442d;IGBKa&A!I@Ua^1K{nEVBhKjU~NapJB0GTxY}O#Qg&!C8b6$V zzjyw8J^;kkSG?tJJ=_EwP@xqTTF*_tmk$6@^&DS$v}E()dh7s1JDUC!q~c(p z7vcDk28C-J&8@Dl2sn!~-LwFA)W1=@`K<=zpcbf)78R=rBD87{KoV$|>?)e#knDh_ z6r@Ccfp|rl1~j#-vIrpVIqP39Zs}@VN4CZZO<)Na@L_L>U{k<05c}|u9G4+DyP=&XvMs;Ep=0N>QmpB0VMAg4m?mbqg zawsRZ&lLdqW&m&FJqOF1kJTv!C@*Xu2ROb387aXkcn+2|zf+%I>NS9h(_46x;Q3RT zx4T|-=9zj8zxCA|K`XSJcj`&`KNSRL?1fTrq5uE@07*qo IM6N<$f~p2HMgRZ+ From 75714f0f64c8890c844c322c1716da8e16b3b13e Mon Sep 17 00:00:00 2001 From: AlphaBs Date: Mon, 14 Jun 2021 13:03:13 +0900 Subject: [PATCH 13/15] remove unused api (CmlLib.Core.Mojang.Skin) --- CmlLib/Core/Mojang/MojangAPI.cs | 52 ------------------------------- CmlLib/Core/Mojang/Skin.cs | 50 ----------------------------- CmlLib/Core/Mojang/UserProfile.cs | 3 +- 3 files changed, 1 insertion(+), 104 deletions(-) delete mode 100644 CmlLib/Core/Mojang/Skin.cs diff --git a/CmlLib/Core/Mojang/MojangAPI.cs b/CmlLib/Core/Mojang/MojangAPI.cs index 70d5c3a..6bab6de 100644 --- a/CmlLib/Core/Mojang/MojangAPI.cs +++ b/CmlLib/Core/Mojang/MojangAPI.cs @@ -12,15 +12,6 @@ namespace CmlLib.Core.Mojang [Obsolete("use MojangAPI library: https://github.com/CmlLib/MojangAPI")] public static class MojangAPI { - private static void writeReq(WebRequest req, string data) - { - using (var reqStream = req.GetRequestStream()) - using (var sw = new StreamWriter(reqStream)) - { - sw.Write(data); - } - } - private static string readRes(WebResponse res) { using (var resStream = res.GetResponseStream()) @@ -63,49 +54,6 @@ public static UserProfile GetProfileUsingToken(string bearerToken) { UUID = job["id"]?.ToString(), Name = job["name"]?.ToString(), - Skin = new Skin - ( - url: skinObj?["url"]?.ToString(), - type: skinObj?["alias"]?.ToString() - ) - }; - } - - public static UserProfile GetProfileUsingUUID(string uuid) - { - var url = "https://sessionserver.mojang.com/session/minecraft/profile/" + uuid; - var req = WebRequest.CreateHttp(url); - req.Method = "GET"; - - var res = req.GetResponse(); - var resBody = readRes(res); - var job = JObject.Parse(resBody); - - JObject? propObj = null; - var propValue = job["properties"]?[0]?["value"]; - if (propValue != null) - { - var decoded = Convert.FromBase64String(propValue.ToString()); - propObj = JObject.Parse(Encoding.UTF8.GetString(decoded)); - } - - var skinObj = propObj?["textures"]?["SKIN"]; - - Skin skin; - if (skinObj == null) - skin = new Skin(null, Skin.GetDefaultSkinType(uuid)); - else - skin = new Skin - ( - url: skinObj["url"]?.ToString(), - type: skinObj["metadata"]?["model"]?.ToString() - ); - - return new UserProfile - { - UUID = propObj?["profileId"]?.ToString(), - Name = propObj?["profileName"]?.ToString(), - Skin = skin }; } } diff --git a/CmlLib/Core/Mojang/Skin.cs b/CmlLib/Core/Mojang/Skin.cs deleted file mode 100644 index 819bf3a..0000000 --- a/CmlLib/Core/Mojang/Skin.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; - -namespace CmlLib.Core.Mojang -{ - public enum SkinType { Steve, Alex } - - public class Skin - { - public Skin() - { - - } - - public Skin(string? url, SkinType type) - { - this.Url = url; - this.Model = type; - } - - public Skin(string? url, string? type) - { - this.Url = url; - - string? lowerType = type?.ToLower(); - if (lowerType == "alex" || lowerType == "slim") - Model = SkinType.Alex; - else - Model = SkinType.Steve; - } - - public static SkinType GetDefaultSkinType(string userUUID) - { - int hex2int(char input) - { - return Convert.ToInt32(input.ToString(), 16); - } - - int lsbsEven = - hex2int(userUUID[7]) ^ - hex2int(userUUID[15]) ^ - hex2int(userUUID[23]) ^ - hex2int(userUUID[31]); - - return (lsbsEven == 0) ? SkinType.Steve : SkinType.Alex; - } - - public string? Url { get; set; } - public SkinType Model { get; set; } - } -} diff --git a/CmlLib/Core/Mojang/UserProfile.cs b/CmlLib/Core/Mojang/UserProfile.cs index 3d22f09..4c396f7 100644 --- a/CmlLib/Core/Mojang/UserProfile.cs +++ b/CmlLib/Core/Mojang/UserProfile.cs @@ -4,6 +4,5 @@ public class UserProfile { public string? UUID { get; set; } public string? Name { get; set; } - public Skin? Skin { get; set; } } -} +} \ No newline at end of file From 9e79f67483145c8beb2d2a8a9ca2d5dc854a690a Mon Sep 17 00:00:00 2001 From: AlphaBs Date: Mon, 14 Jun 2021 13:03:22 +0900 Subject: [PATCH 14/15] release 3.3.0 --- CmlLib/CmlLib.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CmlLib/CmlLib.csproj b/CmlLib/CmlLib.csproj index 0c7f47f..408acc0 100644 --- a/CmlLib/CmlLib.csproj +++ b/CmlLib/CmlLib.csproj @@ -4,7 +4,7 @@ netcoreapp3.1;net462;net5.0 8.0 enable - 3.2.0 + 3.3.0 Minecraft Launcher Library for .NET Core and .NET Framework Support All versions, forge, optifine see github to learn how to use From 863a1b21dc4632acd931bd2737ea1497a551aa44 Mon Sep 17 00:00:00 2001 From: AlphaBs Date: Mon, 14 Jun 2021 13:23:04 +0900 Subject: [PATCH 15/15] update CmlLibWinFormSample --- CmlLibWinFormSample/JavaForm.Designer.cs | 153 ++++++----------------- CmlLibWinFormSample/JavaForm.cs | 21 ++-- CmlLibWinFormSample/MainForm.Designer.cs | 14 ++- CmlLibWinFormSample/MainForm.cs | 58 ++++----- 4 files changed, 88 insertions(+), 158 deletions(-) diff --git a/CmlLibWinFormSample/JavaForm.Designer.cs b/CmlLibWinFormSample/JavaForm.Designer.cs index 8015e1b..23393d8 100644 --- a/CmlLibWinFormSample/JavaForm.Designer.cs +++ b/CmlLibWinFormSample/JavaForm.Designer.cs @@ -28,162 +28,83 @@ protected override void Dispose(bool disposing) /// private void InitializeComponent() { - this.rbAutoJava = new System.Windows.Forms.RadioButton(); - this.rbUserJava = new System.Windows.Forms.RadioButton(); - this.label1 = new System.Windows.Forms.Label(); - this.txtJavaDirectory = new System.Windows.Forms.TextBox(); - this.gAutoJava = new System.Windows.Forms.GroupBox(); - this.gUserJava = new System.Windows.Forms.GroupBox(); - this.label2 = new System.Windows.Forms.Label(); + this.label4 = new System.Windows.Forms.Label(); this.txtUserJava = new System.Windows.Forms.TextBox(); this.label3 = new System.Windows.Forms.Label(); - this.label4 = new System.Windows.Forms.Label(); this.btnClose = new System.Windows.Forms.Button(); - this.gAutoJava.SuspendLayout(); - this.gUserJava.SuspendLayout(); + this.cbAutoJava = new System.Windows.Forms.CheckBox(); this.SuspendLayout(); // - // rbAutoJava - // - this.rbAutoJava.AutoSize = true; - this.rbAutoJava.Checked = true; - this.rbAutoJava.Location = new System.Drawing.Point(12, 12); - this.rbAutoJava.Name = "rbAutoJava"; - this.rbAutoJava.Size = new System.Drawing.Size(283, 16); - this.rbAutoJava.TabIndex = 0; - this.rbAutoJava.TabStop = true; - this.rbAutoJava.Text = "Check Java and Download Java automatically"; - this.rbAutoJava.UseVisualStyleBackColor = true; - this.rbAutoJava.CheckedChanged += new System.EventHandler(this.rbAutoJava_CheckedChanged); - // - // rbUserJava - // - this.rbUserJava.AutoSize = true; - this.rbUserJava.Location = new System.Drawing.Point(12, 139); - this.rbUserJava.Name = "rbUserJava"; - this.rbUserJava.Size = new System.Drawing.Size(96, 16); - this.rbUserJava.TabIndex = 1; - this.rbUserJava.Text = "Set java path"; - this.rbUserJava.UseVisualStyleBackColor = true; - this.rbUserJava.CheckedChanged += new System.EventHandler(this.rbUserJava_CheckedChanged); - // - // label1 - // - this.label1.AutoSize = true; - this.label1.Location = new System.Drawing.Point(14, 23); - this.label1.Name = "label1"; - this.label1.Size = new System.Drawing.Size(97, 12); - this.label1.TabIndex = 2; - this.label1.Text = "Java Directory : "; - // - // txtJavaDirectory - // - this.txtJavaDirectory.Location = new System.Drawing.Point(117, 20); - this.txtJavaDirectory.Name = "txtJavaDirectory"; - this.txtJavaDirectory.Size = new System.Drawing.Size(258, 21); - this.txtJavaDirectory.TabIndex = 3; - // - // gAutoJava - // - this.gAutoJava.Controls.Add(this.label2); - this.gAutoJava.Controls.Add(this.txtJavaDirectory); - this.gAutoJava.Controls.Add(this.label1); - this.gAutoJava.Location = new System.Drawing.Point(12, 34); - this.gAutoJava.Name = "gAutoJava"; - this.gAutoJava.Size = new System.Drawing.Size(394, 88); - this.gAutoJava.TabIndex = 4; - this.gAutoJava.TabStop = false; - this.gAutoJava.Text = "MJava"; - // - // gUserJava - // - this.gUserJava.Controls.Add(this.label4); - this.gUserJava.Controls.Add(this.txtUserJava); - this.gUserJava.Controls.Add(this.label3); - this.gUserJava.Enabled = false; - this.gUserJava.Location = new System.Drawing.Point(12, 161); - this.gUserJava.Name = "gUserJava"; - this.gUserJava.Size = new System.Drawing.Size(394, 90); - this.gUserJava.TabIndex = 5; - this.gUserJava.TabStop = false; - this.gUserJava.Text = "Your Java"; - // - // label2 + // label4 // - this.label2.AutoSize = true; - this.label2.Location = new System.Drawing.Point(14, 53); - this.label2.Name = "label2"; - this.label2.Size = new System.Drawing.Size(277, 24); - this.label2.TabIndex = 4; - this.label2.Text = "Launcher will check the existence of JRE and\r\ndownload JRE into this directory if" + - " it is not exist.\r\n"; + this.label4.AutoSize = true; + this.label4.Location = new System.Drawing.Point(14, 96); + this.label4.Name = "label4"; + this.label4.Size = new System.Drawing.Size(175, 30); + this.label4.TabIndex = 5; + this.label4.Text = "Input java binary file path.\r\nex) java.exe, javaw.exe"; // // txtUserJava // - this.txtUserJava.Location = new System.Drawing.Point(117, 20); + this.txtUserJava.Location = new System.Drawing.Point(107, 52); + this.txtUserJava.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.txtUserJava.Name = "txtUserJava"; - this.txtUserJava.Size = new System.Drawing.Size(258, 21); + this.txtUserJava.Size = new System.Drawing.Size(357, 25); this.txtUserJava.TabIndex = 6; // // label3 // this.label3.AutoSize = true; - this.label3.Location = new System.Drawing.Point(39, 25); + this.label3.Location = new System.Drawing.Point(14, 55); this.label3.Name = "label3"; - this.label3.Size = new System.Drawing.Size(72, 12); + this.label3.Size = new System.Drawing.Size(87, 15); this.label3.TabIndex = 5; this.label3.Text = "Java Path : "; // - // label4 - // - this.label4.AutoSize = true; - this.label4.Location = new System.Drawing.Point(39, 53); - this.label4.Name = "label4"; - this.label4.Size = new System.Drawing.Size(150, 24); - this.label4.TabIndex = 5; - this.label4.Text = "Input java binary file path.\r\nex) java.exe, javaw.exe"; - // // btnClose // - this.btnClose.Location = new System.Drawing.Point(12, 257); + this.btnClose.Location = new System.Drawing.Point(14, 130); + this.btnClose.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.btnClose.Name = "btnClose"; - this.btnClose.Size = new System.Drawing.Size(394, 36); + this.btnClose.Size = new System.Drawing.Size(450, 45); this.btnClose.TabIndex = 6; this.btnClose.Text = "Close"; this.btnClose.UseVisualStyleBackColor = true; this.btnClose.Click += new System.EventHandler(this.btnClose_Click); // + // cbAutoJava + // + this.cbAutoJava.Location = new System.Drawing.Point(12, 21); + this.cbAutoJava.Name = "cbAutoJava"; + this.cbAutoJava.Size = new System.Drawing.Size(215, 24); + this.cbAutoJava.TabIndex = 7; + this.cbAutoJava.Text = "Set java path automatically"; + this.cbAutoJava.UseVisualStyleBackColor = true; + this.cbAutoJava.CheckedChanged += new System.EventHandler(this.cbAutoJava_CheckedChanged); + // // JavaForm // - this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F); + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(419, 299); + this.ClientSize = new System.Drawing.Size(479, 188); + this.Controls.Add(this.cbAutoJava); + this.Controls.Add(this.label4); this.Controls.Add(this.btnClose); - this.Controls.Add(this.gUserJava); - this.Controls.Add(this.gAutoJava); - this.Controls.Add(this.rbUserJava); - this.Controls.Add(this.rbAutoJava); + this.Controls.Add(this.label3); + this.Controls.Add(this.txtUserJava); + this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.Name = "JavaForm"; this.Text = "JavaForm"; + this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.JavaForm_FormClosing); this.Load += new System.EventHandler(this.JavaForm_Load); - this.gAutoJava.ResumeLayout(false); - this.gAutoJava.PerformLayout(); - this.gUserJava.ResumeLayout(false); - this.gUserJava.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); - } + private System.Windows.Forms.CheckBox cbAutoJava; + #endregion - private System.Windows.Forms.RadioButton rbAutoJava; - private System.Windows.Forms.RadioButton rbUserJava; - private System.Windows.Forms.Label label1; - private System.Windows.Forms.TextBox txtJavaDirectory; - private System.Windows.Forms.GroupBox gAutoJava; - private System.Windows.Forms.Label label2; - private System.Windows.Forms.GroupBox gUserJava; private System.Windows.Forms.Label label4; private System.Windows.Forms.TextBox txtUserJava; private System.Windows.Forms.Label label3; diff --git a/CmlLibWinFormSample/JavaForm.cs b/CmlLibWinFormSample/JavaForm.cs index cfaece6..a679375 100644 --- a/CmlLibWinFormSample/JavaForm.cs +++ b/CmlLibWinFormSample/JavaForm.cs @@ -5,24 +5,19 @@ namespace CmlLibWinFormSample { public partial class JavaForm : Form { - public bool UseMJava { get; set; } - public string MJavaDirectory { get; set; } public string JavaBinaryPath { get; set; } - public JavaForm(bool useMJava, string mjavaDir, string javaPath) + public JavaForm(string javaPath) { - this.UseMJava = useMJava; - this.MJavaDirectory = mjavaDir; this.JavaBinaryPath = javaPath; - InitializeComponent(); } private void JavaForm_Load(object sender, EventArgs e) { - rbAutoJava.Checked = UseMJava; - txtJavaDirectory.Text = MJavaDirectory; txtUserJava.Text = JavaBinaryPath; + if (string.IsNullOrEmpty(JavaBinaryPath)) + cbAutoJava.Checked = true; } private void btnClose_Click(object sender, EventArgs e) @@ -30,14 +25,16 @@ private void btnClose_Click(object sender, EventArgs e) this.Close(); } - private void rbAutoJava_CheckedChanged(object sender, EventArgs e) + private void JavaForm_FormClosing(object sender, FormClosingEventArgs e) { - gAutoJava.Enabled = rbAutoJava.Checked; + this.JavaBinaryPath = txtUserJava.Text; } - private void rbUserJava_CheckedChanged(object sender, EventArgs e) + private void cbAutoJava_CheckedChanged(object sender, EventArgs e) { - gUserJava.Enabled = rbUserJava.Checked; + if (cbAutoJava.Checked) + txtUserJava.Clear(); + txtUserJava.Enabled = !cbAutoJava.Checked; } } } diff --git a/CmlLibWinFormSample/MainForm.Designer.cs b/CmlLibWinFormSample/MainForm.Designer.cs index bc53657..3fd8ea2 100644 --- a/CmlLibWinFormSample/MainForm.Designer.cs +++ b/CmlLibWinFormSample/MainForm.Designer.cs @@ -85,6 +85,7 @@ private void InitializeComponent() this.btnSetLastVersion = new System.Windows.Forms.Button(); this.btnMojangServer = new System.Windows.Forms.Button(); this.btnOptions = new System.Windows.Forms.Button(); + this.lbLibraryVersion = new System.Windows.Forms.Label(); this.groupBox2.SuspendLayout(); this.groupBox1.SuspendLayout(); this.groupBox3.SuspendLayout(); @@ -507,7 +508,7 @@ private void InitializeComponent() // label12 // this.label12.AutoSize = true; - this.label12.Location = new System.Drawing.Point(485, 565); + this.label12.Location = new System.Drawing.Point(485, 559); this.label12.Name = "label12"; this.label12.Size = new System.Drawing.Size(242, 15); this.label12.TabIndex = 23; @@ -679,11 +680,20 @@ private void InitializeComponent() this.btnOptions.UseVisualStyleBackColor = true; this.btnOptions.Click += new System.EventHandler(this.btnOptions_Click); // + // lbLibraryVersion + // + this.lbLibraryVersion.Location = new System.Drawing.Point(485, 576); + this.lbLibraryVersion.Name = "lbLibraryVersion"; + this.lbLibraryVersion.Size = new System.Drawing.Size(234, 23); + this.lbLibraryVersion.TabIndex = 31; + this.lbLibraryVersion.Text = "CmlLib.Core"; + // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(918, 608); + this.Controls.Add(this.lbLibraryVersion); this.Controls.Add(this.btnOptions); this.Controls.Add(this.btnMojangServer); this.Controls.Add(this.groupBox4); @@ -713,6 +723,8 @@ private void InitializeComponent() this.PerformLayout(); } + private System.Windows.Forms.Label lbLibraryVersion; + #endregion private System.Windows.Forms.GroupBox groupBox2; private System.Windows.Forms.TextBox Txt_JavaArgs; diff --git a/CmlLibWinFormSample/MainForm.cs b/CmlLibWinFormSample/MainForm.cs index 7f8f53e..9ee5505 100644 --- a/CmlLibWinFormSample/MainForm.cs +++ b/CmlLibWinFormSample/MainForm.cs @@ -6,6 +6,7 @@ using System.ComponentModel; using System.Diagnostics; using System.IO; +using System.Reflection; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; @@ -23,18 +24,16 @@ public MainForm(MSession session) CMLauncher launcher; readonly MSession session; - MinecraftPath gamePath; - - bool useMJava = true; - string javaPath = "java.exe"; + MinecraftPath gamePath; + string javaPath; GameLog logForm; private async void MainForm_Shown(object sender, EventArgs e) { + lbLibraryVersion.Text = "CmlLib.Core " + getLibraryVersion(); + // Initialize launcher - this.Refresh(); - var defaultPath = new MinecraftPath(MinecraftPath.GetOSDefaultPath()); await initializeLauncher(defaultPath); } @@ -43,10 +42,7 @@ private async Task initializeLauncher(MinecraftPath path) { txtPath.Text = path.BasePath; this.gamePath = path; - - if (useMJava) - lbJavaPath.Text = path.Runtime; - + launcher = new CMLauncher(path); launcher.FileChanged += Launcher_FileChanged; launcher.ProgressChanged += Launcher_ProgressChanged; @@ -121,9 +117,9 @@ private async void Btn_Launch_Click(object sender, EventArgs e) DockIcon = Txt_DockIcon.Text }; - if (!useMJava) + if (!string.IsNullOrEmpty(javaPath)) launchOption.JavaPath = javaPath; - + if (!string.IsNullOrEmpty(txtXms.Text)) launchOption.MinimumRamMb = int.Parse(txtXms.Text); @@ -229,17 +225,14 @@ private async void btnChangePath_Click(object sender, EventArgs e) private void btnChangeJava_Click(object sender, EventArgs e) { - var form = new JavaForm(useMJava, this.gamePath.Runtime, javaPath); + var form = new JavaForm(javaPath); form.ShowDialog(); - - useMJava = form.UseMJava; - this.gamePath.Runtime = form.MJavaDirectory; javaPath = form.JavaBinaryPath; - - if (useMJava) - lbJavaPath.Text = form.MJavaDirectory; + + if (string.IsNullOrEmpty(javaPath)) + lbJavaPath.Text = "Use default java"; else - lbJavaPath.Text = form.JavaBinaryPath; + lbJavaPath.Text = javaPath; } private void btnAutoRamSet_Click(object sender, EventArgs e) @@ -278,15 +271,9 @@ private void btnForgeInstall_Click(object sender, EventArgs e) new Thread(() => { var forgeJava = ""; - - if (useMJava) - { - var java = new MJava(); - java.ProgressChanged += Launcher_ProgressChanged; - forgeJava = java.CheckJava(); - } - else - forgeJava = javaPath; + var java = new MJava(); + java.ProgressChanged += Launcher_ProgressChanged; + forgeJava = java.CheckJava(); Invoke(new Action(async () => { @@ -375,5 +362,18 @@ private void start(string url) MessageBox.Show(ex.ToString()); } } + + private string getLibraryVersion() + { + try + { + return Assembly.GetAssembly(typeof(CMLauncher)).GetName().Version.ToString(); + } + catch (Exception ex) + { + Console.WriteLine(ex); + return null; + } + } } }