From 464bc0f770880737597954fee8d48035b3274d6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Wed, 27 Sep 2017 04:04:19 +0200 Subject: [PATCH] GH-1997 replace use of weird hacks with normal java arguments This affects classpath and java.library.path. The catch is that if the strings cannot be expressed in system codepage on Windows, it tries to use 8.3 paths. --- .../minecraft/launch/LauncherPartLaunch.cpp | 60 +++++++++++++++- libraries/launcher/org/multimc/Utils.java | 71 ------------------- .../org/multimc/onesix/OneSixLauncher.java | 26 ------- 3 files changed, 59 insertions(+), 98 deletions(-) diff --git a/api/logic/minecraft/launch/LauncherPartLaunch.cpp b/api/logic/minecraft/launch/LauncherPartLaunch.cpp index b641968c..3f9cf05a 100644 --- a/api/logic/minecraft/launch/LauncherPartLaunch.cpp +++ b/api/logic/minecraft/launch/LauncherPartLaunch.cpp @@ -28,6 +28,27 @@ LauncherPartLaunch::LauncherPartLaunch(LaunchTask *parent) : LaunchStep(parent) connect(&m_process, &LoggedProcess::stateChanged, this, &LauncherPartLaunch::on_state); } +#ifdef Q_OS_WIN +// returns 8.3 file format from long path +#include +QString shortPathName(const QString & file) +{ + auto input = file.toStdWString(); + std::wstring output; + long length = GetShortPathNameW(input, NULL, 0); + output.resize(length); + GetShortPathNameW(input,output,length); + QString ret = QString::fromStdWString(output); + return ret; +} +#endif + +// if the string survives roundtrip through local 8bit encoding... +bool fitsInLocal8bit(const QString & string) +{ + return string == QString::fromLocal8Bit(string.toLocal8Bit()); +} + void LauncherPartLaunch::executeTask() { auto instance = m_parent->instance(); @@ -45,7 +66,44 @@ void LauncherPartLaunch::executeTask() // make detachable - this will keep the process running even if the object is destroyed m_process.setDetachable(true); - args << "-jar" << FS::PathCombine(ENV.getJarsPath(), "NewLaunch.jar"); + auto classPath = minecraftInstance->getClassPath(); + classPath.prepend(FS::PathCombine(ENV.getJarsPath(), "NewLaunch.jar")); + + auto natPath = minecraftInstance->getNativePath(); +#ifdef Q_OS_WIN + if (!fitsInLocal8bit(natPath)) + { + args << "-Djava.library.path=" + shortPathName(natPath); + } + else + { + args << "-Djava.library.path=" + natPath; + } +#else + args << "-Djava.library.path=" + natPath; +#endif + + args << "-cp"; +#ifdef Q_OS_WIN + QStringList processed; + for(auto & item: classPath) + { + if (!fitsInLocal8bit(item)) + { + processed << shortPathName(item); + } + else + { + processed << item; + } + } + args << processed.join(';'); +#else + args << classPath.join(':'); +#endif + args << "org.multimc.EntryPoint"; + + qDebug() << args.join(' '); QString wrapperCommandStr = instance->getWrapperCommand().trimmed(); if(!wrapperCommandStr.isEmpty()) diff --git a/libraries/launcher/org/multimc/Utils.java b/libraries/launcher/org/multimc/Utils.java index 860c0864..94b2eaa9 100644 --- a/libraries/launcher/org/multimc/Utils.java +++ b/libraries/launcher/org/multimc/Utils.java @@ -67,77 +67,6 @@ public class Utils return sb.toString(); } - /** - * Adds the specified library to the classpath - * - * @param s the path to add - * @throws Exception - */ - public static void addToClassPath(String s) throws Exception - { - File f = new File(s); - URL u = f.toURI().toURL(); - URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader(); - Class urlClass = URLClassLoader.class; - Method method = urlClass.getDeclaredMethod("addURL", new Class[]{URL.class}); - method.setAccessible(true); - method.invoke(urlClassLoader, new Object[]{u}); - } - - /** - * Adds many libraries to the classpath - * - * @param jars the paths to add - */ - public static boolean addToClassPath(List jars) - { - boolean pure = true; - // initialize the class path - for (String jar : jars) - { - try - { - Utils.addToClassPath(jar); - } catch (Exception e) - { - System.err.println("Unable to load: " + jar); - e.printStackTrace(System.err); - pure = false; - } - } - return pure; - } - - /** - * Adds the specified path to the java library path - * - * @param pathToAdd the path to add - * @throws Exception - */ - @Deprecated - public static void addLibraryPath(String pathToAdd) throws Exception - { - final Field usrPathsField = ClassLoader.class.getDeclaredField("usr_paths"); - usrPathsField.setAccessible(true); - - //get array of paths - final String[] paths = (String[]) usrPathsField.get(null); - - //check if the path to add is already present - for (String path : paths) - { - if (path.equals(pathToAdd)) - { - return; - } - } - - //add the new path - final String[] newPaths = Arrays.copyOf(paths, paths.length + 1); - newPaths[newPaths.length - 1] = pathToAdd; - usrPathsField.set(null, newPaths); - } - /** * Finds a field that looks like a Minecraft base folder in a supplied class * diff --git a/libraries/launcher/org/multimc/onesix/OneSixLauncher.java b/libraries/launcher/org/multimc/onesix/OneSixLauncher.java index f057e55a..8f0a498f 100644 --- a/libraries/launcher/org/multimc/onesix/OneSixLauncher.java +++ b/libraries/launcher/org/multimc/onesix/OneSixLauncher.java @@ -214,32 +214,6 @@ public class OneSixLauncher implements Launcher return -1; } - // add libraries to classpath - if(!Utils.addToClassPath(libraries)) - { - System.err.println("Halting launch due to previous errors."); - return -1; - } - - // set the native libs path... the brute force way - try - { - System.setProperty("java.library.path", nativePath); - System.setProperty("org.lwjgl.librarypath", nativePath); - System.setProperty("net.java.games.input.librarypath", nativePath); - // by the power of reflection, initialize native libs again. DIRTY! - // this is SO BAD. imagine doing that to ld - Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths"); - fieldSysPath.setAccessible( true ); - fieldSysPath.set( null, null ); - } - catch (Exception e) - { - System.err.println("Failed to set the native library path:"); - e.printStackTrace(System.err); - System.err.println("Minecraft might fail to launch..."); - } - // grab the system classloader and ... cl = ClassLoader.getSystemClassLoader();