Skip to content

Commit

Permalink
fix: filter duplicated library path when building -cp
Browse files Browse the repository at this point in the history
1.20.2~ forge version file has duplicated library paths. it crashes the game
  • Loading branch information
AlphaBs committed Mar 9, 2024
1 parent bd3488a commit 0ce9e39
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
51 changes: 30 additions & 21 deletions CmlLib/Core/Launcher/MLaunch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class MLaunch
private const int DefaultServerPort = 25565;

public static readonly string SupportVersion = "1.17.1";
public readonly static string[] DefaultJavaParameter =
public readonly static string[] DefaultJavaParameter =
{
"-XX:+UnlockExperimentalVMOptions",
"-XX:+UseG1GC",
Expand All @@ -33,12 +33,12 @@ public MLaunch(MLaunchOption option)

private readonly MinecraftPath minecraftPath;
private readonly MLaunchOption launchOption;

public Process GetProcess()
{
string arg = string.Join(" ", CreateArg());
Process mc = new Process();
mc.StartInfo.FileName =
mc.StartInfo.FileName =
useNotNull(launchOption.GetStartVersion().JavaBinaryPath, launchOption.GetJavaPath()) ?? "";
mc.StartInfo.Arguments = arg;
mc.StartInfo.WorkingDirectory = minecraftPath.BasePath;
Expand All @@ -48,20 +48,29 @@ public Process GetProcess()

private string createClassPath(MVersion version)
{
var classpath = new List<string>(version.Libraries?.Length ?? 1);

if (version.Libraries != null)
IEnumerable<string> getLibraryPaths()
{
var libraries = version.Libraries
.Where(lib => lib.IsRequire && !lib.IsNative && !string.IsNullOrEmpty(lib.Path))
.Select(lib => Path.GetFullPath(Path.Combine(minecraftPath.Library, lib.Path!)));
classpath.AddRange(libraries);
}
var set = new HashSet<string>();

if (!string.IsNullOrEmpty(version.Jar))
classpath.Add(minecraftPath.GetVersionJarPath(version.Jar));
if (version.Libraries != null)
{
var libraries = version.Libraries
.Where(lib => lib.IsRequire && !lib.IsNative && !string.IsNullOrEmpty(lib.Path))
.Select(lib => Path.GetFullPath(Path.Combine(minecraftPath.Library, lib.Path!)))
.Where(lib => set.Add(lib));
foreach (var lib in libraries)
yield return lib;
}

if (!string.IsNullOrEmpty(version.Jar))
{
var jar = minecraftPath.GetVersionJarPath(version.Jar);
if (set.Add(jar))
yield return jar;
}
}

var classpathStr = IOUtil.CombinePath(classpath.ToArray());
var classpathStr = IOUtil.CombinePath(getLibraryPaths());
return classpathStr;
}

Expand All @@ -78,11 +87,11 @@ public string[] CreateArg()
{
MVersion version = launchOption.GetStartVersion();
var args = new List<string>();

var classpath = createClassPath(version);
var nativePath = createNativePath(version);
var session = launchOption.GetSession();

var argDict = new Dictionary<string, string?>
{
{ "library_directory", minecraftPath.Library },
Expand All @@ -91,7 +100,7 @@ public string[] CreateArg()
{ "launcher_version", useNotNull(launchOption.GameLauncherVersion, "2") },
{ "classpath_separator", Path.PathSeparator.ToString() },
{ "classpath", classpath },

{ "auth_player_name" , session.Username },
{ "version_name" , version.Id },
{ "game_directory" , minecraftPath.BasePath },
Expand All @@ -109,11 +118,11 @@ public string[] CreateArg()
};

// JVM argument

// version-specific jvm arguments
if (version.JvmArguments != null)
args.AddRange(Mapper.MapInterpolation(version.JvmArguments, argDict));

// default jvm arguments
if (launchOption.JVMArguments != null)
args.AddRange(launchOption.JVMArguments);
Expand All @@ -124,7 +133,7 @@ public string[] CreateArg()

if (launchOption.MinimumRamMb > 0)
args.Add("-Xms" + launchOption.MinimumRamMb + "m");

args.AddRange(DefaultJavaParameter);
}

Expand Down Expand Up @@ -196,7 +205,7 @@ public string[] CreateArg()
{
if (input == null)
return null;

if (input.Contains(" "))
return "\"" + input + "\"";
else
Expand Down
4 changes: 3 additions & 1 deletion CmlLib/Utils/IOUtil.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -42,7 +44,7 @@ public static void DeleteDirectory(string targetDir)
}
}

public static string CombinePath(string[] paths)
public static string CombinePath(IEnumerable<string> paths)
{
return string.Join(Path.PathSeparator.ToString(),
paths.Select(x =>
Expand Down

0 comments on commit 0ce9e39

Please sign in to comment.