From cf6743765019f271b588baa58a731acb583d4024 Mon Sep 17 00:00:00 2001 From: AlphaBs Date: Tue, 23 Nov 2021 04:30:10 +0900 Subject: [PATCH] add GetInstalledJavaVersions(), GetDefaultJavaBinaryPath() in IJavaPathResolver, MinecraftJAvaPathResolver --- CmlLib/Core/CMLauncher.cs | 53 ++++++------------- CmlLib/Core/Java/IJavaPathResolver.cs | 5 +- CmlLib/Core/Java/MinecraftJavaPathResolver.cs | 24 ++++++++- 3 files changed, 42 insertions(+), 40 deletions(-) diff --git a/CmlLib/Core/CMLauncher.cs b/CmlLib/Core/CMLauncher.cs index 46aa4d0..ce56a53 100644 --- a/CmlLib/Core/CMLauncher.cs +++ b/CmlLib/Core/CMLauncher.cs @@ -7,7 +7,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; -using System.Linq; +using System.IO; using System.Threading.Tasks; using CmlLib.Core.Java; @@ -31,6 +31,8 @@ public CMLauncher(MinecraftPath mc) e => FileChanged?.Invoke(e)); pProgressChanged = new Progress( e => ProgressChanged?.Invoke(this, e)); + + JavaPathResolver = new MinecraftJavaPathResolver(mc); } public event DownloadFileChangedHandler? FileChanged; @@ -42,11 +44,12 @@ public CMLauncher(MinecraftPath mc) public MinecraftPath MinecraftPath { get; private set; } public MVersionCollection? Versions { get; private set; } - public IVersionLoader VersionLoader { get; set; } + public FileCheckerCollection GameFileCheckers { get; private set; } - public IDownloader? FileDownloader { get; set; } + + public IJavaPathResolver JavaPathResolver { get; set; } public MVersionCollection GetAllVersions() { @@ -61,20 +64,20 @@ public async Task GetAllVersionsAsync() return Versions; } - public MVersion GetVersion(string versionname) + public MVersion GetVersion(string versionName) { if (Versions == null) GetAllVersions(); - return Versions!.GetVersion(versionname); + return Versions!.GetVersion(versionName); } - public async Task GetVersionAsync(string versionname) + public async Task GetVersionAsync(string versionName) { if (Versions == null) await GetAllVersionsAsync().ConfigureAwait(false); - var version = await Versions!.GetVersionAsync(versionname) + var version = await Versions!.GetVersionAsync(versionName) .ConfigureAwait(false); return version; } @@ -186,7 +189,9 @@ public Process CreateProcess(string mcversion, string forgeversion, MLaunchOptio { CheckAndDownload(GetVersion(mcversion)); - var versionName = CheckForge(mcversion, forgeversion, option.JavaPath); + var javaPath = option.JavaPath ?? GetDefaultJavaPath() + ?? throw new FileNotFoundException("Cannot find java path"); + var versionName = CheckForge(mcversion, forgeversion, javaPath); return CreateProcess(versionName, option); } @@ -270,36 +275,12 @@ private void checkLaunchOption(MLaunchOption option) public string? GetJavaPath(MVersion version) { - var javaPathResolver = new MinecraftJavaPathResolver(MinecraftPath); - - var javaPath = ""; - if (!string.IsNullOrEmpty(version.JavaVersion)) - javaPath = javaPathResolver.GetJavaBinaryPath(version.JavaVersion, MRule.OSName); + if (string.IsNullOrEmpty(version.JavaVersion)) + return null; - return javaPath; + return JavaPathResolver.GetJavaBinaryPath(version.JavaVersion, MRule.OSName); } - public string? GetDefaultJavaPath() - => GetDefaultJavaPath(new MinecraftJavaPathResolver(MinecraftPath)); - - public string? GetDefaultJavaPath(MinecraftJavaPathResolver javaPathResolver) - { - var javaVersions = javaPathResolver.GetInstalledJavaVersions(); - string? javaPath = null; - - if (string.IsNullOrEmpty(javaPath) && - javaVersions.Contains(MinecraftJavaPathResolver.JreLegacyVersionName)) - javaPath = javaPathResolver.GetJavaBinaryPath(MinecraftJavaPathResolver.JreLegacyVersionName, MRule.OSName); - - if (string.IsNullOrEmpty(javaPath) && - javaVersions.Contains(MinecraftJavaPathResolver.CmlLegacyVersionName)) - javaPath = javaPathResolver.GetJavaBinaryPath(MinecraftJavaPathResolver.CmlLegacyVersionName, MRule.OSName); - - if (string.IsNullOrEmpty(javaPath) && - javaVersions.Length > 0) - javaPath = javaPathResolver.GetJavaBinaryPath(javaVersions[0], MRule.OSName); - - return javaPath; - } + public string? GetDefaultJavaPath() => JavaPathResolver.GetDefaultJavaBinaryPath(); } } diff --git a/CmlLib/Core/Java/IJavaPathResolver.cs b/CmlLib/Core/Java/IJavaPathResolver.cs index 765a187..0d87c81 100644 --- a/CmlLib/Core/Java/IJavaPathResolver.cs +++ b/CmlLib/Core/Java/IJavaPathResolver.cs @@ -2,9 +2,10 @@ { public interface IJavaPathResolver { - //string GetJavaBinaryPath(string osName); + string[] GetInstalledJavaVersions(); + string? GetDefaultJavaBinaryPath(); + string GetJavaBinaryPath(string javaVersionName); string GetJavaBinaryPath(string javaVersionName, string osName); - string GetJavaDirPath(); string GetJavaDirPath(string javaVersionName); } } \ No newline at end of file diff --git a/CmlLib/Core/Java/MinecraftJavaPathResolver.cs b/CmlLib/Core/Java/MinecraftJavaPathResolver.cs index 5d2b060..9ab21df 100644 --- a/CmlLib/Core/Java/MinecraftJavaPathResolver.cs +++ b/CmlLib/Core/Java/MinecraftJavaPathResolver.cs @@ -30,9 +30,29 @@ public string[] GetInstalledJavaVersions() .Select(x => x.Name) .ToArray(); } + + public string? GetDefaultJavaBinaryPath() + { + var javaVersions = GetInstalledJavaVersions(); + string? javaPath = null; + + if (string.IsNullOrEmpty(javaPath) && + javaVersions.Contains(MinecraftJavaPathResolver.JreLegacyVersionName)) + javaPath = GetJavaBinaryPath(MinecraftJavaPathResolver.JreLegacyVersionName, MRule.OSName); + + if (string.IsNullOrEmpty(javaPath) && + javaVersions.Contains(MinecraftJavaPathResolver.CmlLegacyVersionName)) + javaPath = GetJavaBinaryPath(MinecraftJavaPathResolver.CmlLegacyVersionName, MRule.OSName); + + if (string.IsNullOrEmpty(javaPath) && + javaVersions.Length > 0) + javaPath = GetJavaBinaryPath(javaVersions[0], MRule.OSName); + + return javaPath; + } - //public string GetJavaBinaryPath(string osName) - // => GetJavaBinaryPath(JreLegacyVersionName, osName); + public string GetJavaBinaryPath(string javaVersionName) + => GetJavaBinaryPath(javaVersionName, MRule.OSName); public string GetJavaBinaryPath(string javaVersionName, string osName) {