Skip to content

Commit

Permalink
add GetInstalledJavaVersions(), GetDefaultJavaBinaryPath() in IJavaPa…
Browse files Browse the repository at this point in the history
…thResolver, MinecraftJAvaPathResolver
  • Loading branch information
AlphaBs committed Nov 22, 2021
1 parent f65bf1b commit cf67437
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 40 deletions.
53 changes: 17 additions & 36 deletions CmlLib/Core/CMLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -31,6 +31,8 @@ public CMLauncher(MinecraftPath mc)
e => FileChanged?.Invoke(e));
pProgressChanged = new Progress<ProgressChangedEventArgs>(
e => ProgressChanged?.Invoke(this, e));

JavaPathResolver = new MinecraftJavaPathResolver(mc);
}

public event DownloadFileChangedHandler? FileChanged;
Expand All @@ -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()
{
Expand All @@ -61,20 +64,20 @@ public async Task<MVersionCollection> 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<MVersion> GetVersionAsync(string versionname)
public async Task<MVersion> 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;
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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();
}
}
5 changes: 3 additions & 2 deletions CmlLib/Core/Java/IJavaPathResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
24 changes: 22 additions & 2 deletions CmlLib/Core/Java/MinecraftJavaPathResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit cf67437

Please sign in to comment.