From 0ce9e39120b16d1b9cda6eb3cfa903a1d66545f5 Mon Sep 17 00:00:00 2001 From: AlphaBs Date: Sat, 9 Mar 2024 18:56:24 +0900 Subject: [PATCH] fix: filter duplicated library path when building -cp 1.20.2~ forge version file has duplicated library paths. it crashes the game --- CmlLib/Core/Launcher/MLaunch.cs | 51 +++++++++++++++++++-------------- CmlLib/Utils/IOUtil.cs | 4 ++- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/CmlLib/Core/Launcher/MLaunch.cs b/CmlLib/Core/Launcher/MLaunch.cs index 75dbf47..82add46 100644 --- a/CmlLib/Core/Launcher/MLaunch.cs +++ b/CmlLib/Core/Launcher/MLaunch.cs @@ -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", @@ -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; @@ -48,20 +48,29 @@ public Process GetProcess() private string createClassPath(MVersion version) { - var classpath = new List(version.Libraries?.Length ?? 1); - - if (version.Libraries != null) + IEnumerable 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(); - 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; } @@ -78,11 +87,11 @@ public string[] CreateArg() { MVersion version = launchOption.GetStartVersion(); var args = new List(); - + var classpath = createClassPath(version); var nativePath = createNativePath(version); var session = launchOption.GetSession(); - + var argDict = new Dictionary { { "library_directory", minecraftPath.Library }, @@ -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 }, @@ -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); @@ -124,7 +133,7 @@ public string[] CreateArg() if (launchOption.MinimumRamMb > 0) args.Add("-Xms" + launchOption.MinimumRamMb + "m"); - + args.AddRange(DefaultJavaParameter); } @@ -196,7 +205,7 @@ public string[] CreateArg() { if (input == null) return null; - + if (input.Contains(" ")) return "\"" + input + "\""; else diff --git a/CmlLib/Utils/IOUtil.cs b/CmlLib/Utils/IOUtil.cs index ad27b40..c1ca387 100644 --- a/CmlLib/Utils/IOUtil.cs +++ b/CmlLib/Utils/IOUtil.cs @@ -1,4 +1,6 @@ using System; +using System.Collections; +using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; @@ -42,7 +44,7 @@ public static void DeleteDirectory(string targetDir) } } - public static string CombinePath(string[] paths) + public static string CombinePath(IEnumerable paths) { return string.Join(Path.PathSeparator.ToString(), paths.Select(x =>