Skip to content

Commit 0ce9e39

Browse files
committed
fix: filter duplicated library path when building -cp
1.20.2~ forge version file has duplicated library paths. it crashes the game
1 parent bd3488a commit 0ce9e39

File tree

2 files changed

+33
-22
lines changed

2 files changed

+33
-22
lines changed

CmlLib/Core/Launcher/MLaunch.cs

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class MLaunch
1212
private const int DefaultServerPort = 25565;
1313

1414
public static readonly string SupportVersion = "1.17.1";
15-
public readonly static string[] DefaultJavaParameter =
15+
public readonly static string[] DefaultJavaParameter =
1616
{
1717
"-XX:+UnlockExperimentalVMOptions",
1818
"-XX:+UseG1GC",
@@ -33,12 +33,12 @@ public MLaunch(MLaunchOption option)
3333

3434
private readonly MinecraftPath minecraftPath;
3535
private readonly MLaunchOption launchOption;
36-
36+
3737
public Process GetProcess()
3838
{
3939
string arg = string.Join(" ", CreateArg());
4040
Process mc = new Process();
41-
mc.StartInfo.FileName =
41+
mc.StartInfo.FileName =
4242
useNotNull(launchOption.GetStartVersion().JavaBinaryPath, launchOption.GetJavaPath()) ?? "";
4343
mc.StartInfo.Arguments = arg;
4444
mc.StartInfo.WorkingDirectory = minecraftPath.BasePath;
@@ -48,20 +48,29 @@ public Process GetProcess()
4848

4949
private string createClassPath(MVersion version)
5050
{
51-
var classpath = new List<string>(version.Libraries?.Length ?? 1);
52-
53-
if (version.Libraries != null)
51+
IEnumerable<string> getLibraryPaths()
5452
{
55-
var libraries = version.Libraries
56-
.Where(lib => lib.IsRequire && !lib.IsNative && !string.IsNullOrEmpty(lib.Path))
57-
.Select(lib => Path.GetFullPath(Path.Combine(minecraftPath.Library, lib.Path!)));
58-
classpath.AddRange(libraries);
59-
}
53+
var set = new HashSet<string>();
6054

61-
if (!string.IsNullOrEmpty(version.Jar))
62-
classpath.Add(minecraftPath.GetVersionJarPath(version.Jar));
55+
if (version.Libraries != null)
56+
{
57+
var libraries = version.Libraries
58+
.Where(lib => lib.IsRequire && !lib.IsNative && !string.IsNullOrEmpty(lib.Path))
59+
.Select(lib => Path.GetFullPath(Path.Combine(minecraftPath.Library, lib.Path!)))
60+
.Where(lib => set.Add(lib));
61+
foreach (var lib in libraries)
62+
yield return lib;
63+
}
64+
65+
if (!string.IsNullOrEmpty(version.Jar))
66+
{
67+
var jar = minecraftPath.GetVersionJarPath(version.Jar);
68+
if (set.Add(jar))
69+
yield return jar;
70+
}
71+
}
6372

64-
var classpathStr = IOUtil.CombinePath(classpath.ToArray());
73+
var classpathStr = IOUtil.CombinePath(getLibraryPaths());
6574
return classpathStr;
6675
}
6776

@@ -78,11 +87,11 @@ public string[] CreateArg()
7887
{
7988
MVersion version = launchOption.GetStartVersion();
8089
var args = new List<string>();
81-
90+
8291
var classpath = createClassPath(version);
8392
var nativePath = createNativePath(version);
8493
var session = launchOption.GetSession();
85-
94+
8695
var argDict = new Dictionary<string, string?>
8796
{
8897
{ "library_directory", minecraftPath.Library },
@@ -91,7 +100,7 @@ public string[] CreateArg()
91100
{ "launcher_version", useNotNull(launchOption.GameLauncherVersion, "2") },
92101
{ "classpath_separator", Path.PathSeparator.ToString() },
93102
{ "classpath", classpath },
94-
103+
95104
{ "auth_player_name" , session.Username },
96105
{ "version_name" , version.Id },
97106
{ "game_directory" , minecraftPath.BasePath },
@@ -109,11 +118,11 @@ public string[] CreateArg()
109118
};
110119

111120
// JVM argument
112-
121+
113122
// version-specific jvm arguments
114123
if (version.JvmArguments != null)
115124
args.AddRange(Mapper.MapInterpolation(version.JvmArguments, argDict));
116-
125+
117126
// default jvm arguments
118127
if (launchOption.JVMArguments != null)
119128
args.AddRange(launchOption.JVMArguments);
@@ -124,7 +133,7 @@ public string[] CreateArg()
124133

125134
if (launchOption.MinimumRamMb > 0)
126135
args.Add("-Xms" + launchOption.MinimumRamMb + "m");
127-
136+
128137
args.AddRange(DefaultJavaParameter);
129138
}
130139

@@ -196,7 +205,7 @@ public string[] CreateArg()
196205
{
197206
if (input == null)
198207
return null;
199-
208+
200209
if (input.Contains(" "))
201210
return "\"" + input + "\"";
202211
else

CmlLib/Utils/IOUtil.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
24
using System.IO;
35
using System.Linq;
46
using System.Text;
@@ -42,7 +44,7 @@ public static void DeleteDirectory(string targetDir)
4244
}
4345
}
4446

45-
public static string CombinePath(string[] paths)
47+
public static string CombinePath(IEnumerable<string> paths)
4648
{
4749
return string.Join(Path.PathSeparator.ToString(),
4850
paths.Select(x =>

0 commit comments

Comments
 (0)