|
| 1 | +/* |
| 2 | + * Copyright (c) Forge Development LLC and contributors |
| 3 | + * SPDX-License-Identifier: LGPL-2.1-only |
| 4 | + */ |
| 5 | +package net.minecraftforge.launcher; |
| 6 | + |
| 7 | +import joptsimple.NonOptionArgumentSpec; |
| 8 | +import joptsimple.OptionParser; |
| 9 | +import joptsimple.OptionSet; |
| 10 | +import net.minecraftforge.util.data.json.MinecraftVersion; |
| 11 | +import net.minecraftforge.util.logging.Logger; |
| 12 | +import net.minecraftforge.util.os.OS; |
| 13 | + |
| 14 | +import java.io.File; |
| 15 | +import java.io.FileOutputStream; |
| 16 | +import java.io.IOException; |
| 17 | +import java.io.InputStream; |
| 18 | +import java.lang.reflect.Field; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.Enumeration; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.zip.ZipEntry; |
| 26 | +import java.util.zip.ZipFile; |
| 27 | + |
| 28 | +/// This is the SlimeLauncher replacement for [LegacyDev](https://github.com/MinecraftForge/LegacyDev/). |
| 29 | +/// Which was written to Bridge 1.12.2 to FG3+ |
| 30 | +class LegacyDev { |
| 31 | + static final Logger LOGGER = Main.LOGGER; |
| 32 | + static final String LEGACYDEV = "net.minecraftforge.legacydev."; |
| 33 | + static final String LEGACYDEV_CLIENT = LEGACYDEV + "MainClient"; |
| 34 | + static final String LEGACYDEV_SERVER = LEGACYDEV + "MainServer"; |
| 35 | + |
| 36 | + static boolean is(String main) { |
| 37 | + return main.startsWith(LEGACYDEV); |
| 38 | + } |
| 39 | + |
| 40 | + static String getMainClass() { |
| 41 | + String mainClass = getenv("mainClass"); |
| 42 | + if (mainClass == null) |
| 43 | + throw new IllegalArgumentException("Must specify mainClass environment variable"); |
| 44 | + LOGGER.info("Legacy Main Class: " + mainClass); |
| 45 | + return mainClass; |
| 46 | + } |
| 47 | + |
| 48 | + private static Map<String, File> findAllClassPathEntries() { |
| 49 | + String[] parts = System.getProperty("java.class.path").split(File.pathSeparator); |
| 50 | + Map<String, File> files = new HashMap<>(); |
| 51 | + for (String part : parts) { |
| 52 | + File file = new File(part); |
| 53 | + if (file.exists() && file.isFile()) |
| 54 | + files.put(file.getName(), file); |
| 55 | + } |
| 56 | + return files; |
| 57 | + } |
| 58 | + |
| 59 | + /// Extract native libraries and inject them into the classloader. |
| 60 | + /// This mimics the behavior of the Vanilla Minecraft Launcher |
| 61 | + /// Because old versions of lwjgl didn't auto-extract their libraries |
| 62 | + static void setupNatives(MinecraftVersion versionJson, File cache) { |
| 63 | + OS currentOS = OS.current(); |
| 64 | + Map<String, File> classpath = findAllClassPathEntries(); |
| 65 | + LOGGER.info("Extracting natives to " + cache.getAbsolutePath()); |
| 66 | + LOGGER.push(); |
| 67 | + try { |
| 68 | + for (MinecraftVersion.Lib lib : versionJson.getLibs()) { |
| 69 | + if (!lib.allows(currentOS) || lib.info.extract == null || lib.dl == null || lib.dl == lib.info.downloads.artifact) // We only want natives |
| 70 | + continue; |
| 71 | + |
| 72 | + String name = lib.dl.path.substring(lib.dl.path.lastIndexOf('/') + 1); |
| 73 | + File file = classpath.get(name); |
| 74 | + if (file == null) { |
| 75 | + LOGGER.error(name + ": Missing"); |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + LOGGER.info(name + " from " + file.getAbsolutePath()); |
| 80 | + LOGGER.push(); |
| 81 | + try (ZipFile zip = new ZipFile(file)) { |
| 82 | + zipEntry: |
| 83 | + for (Enumeration<? extends ZipEntry> en = zip.entries(); en.hasMoreElements(); ) { |
| 84 | + ZipEntry entry = en.nextElement(); |
| 85 | + File output = new File(cache, entry.getName()); |
| 86 | + if (output.exists()) |
| 87 | + continue; // Assume its valid is already extracted |
| 88 | + |
| 89 | + // Skip anything the json says to filter |
| 90 | + if (lib.info.extract.exclude != null) { |
| 91 | + for (String exclude : lib.info.extract.exclude) { |
| 92 | + if (entry.getName().startsWith(exclude)) |
| 93 | + continue zipEntry; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if (output.getParentFile() != null) |
| 98 | + output.getParentFile().mkdirs(); |
| 99 | + |
| 100 | + try (FileOutputStream out = new FileOutputStream(output)) { |
| 101 | + InputStream stream = zip.getInputStream(entry); |
| 102 | + byte[] buf = new byte[8192]; |
| 103 | + int length; |
| 104 | + while ((length = stream.read(buf)) != -1) { |
| 105 | + out.write(buf, 0, length); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } catch (IOException e) { |
| 110 | + throw new RuntimeException(e); |
| 111 | + } finally { |
| 112 | + LOGGER.pop(); |
| 113 | + } |
| 114 | + } |
| 115 | + } finally { |
| 116 | + LOGGER.pop(); |
| 117 | + } |
| 118 | + |
| 119 | + String paths = System.getProperty("java.library.path"); |
| 120 | + if (paths == null || paths.isEmpty()) |
| 121 | + paths = cache.getAbsolutePath(); |
| 122 | + else |
| 123 | + paths += File.pathSeparator + cache.getAbsolutePath(); |
| 124 | + System.setProperty("java.library.path", paths); |
| 125 | + |
| 126 | + // Add the library path to the classloader if it has already been cached. It shouldn't be by now, and this only matters on java <= 8 so this reflection should be fairly safe |
| 127 | + try { |
| 128 | + final String[] usrPathsValue = paths.split(File.pathSeparator); |
| 129 | + final Field usrPathsField = ClassLoader.class.getDeclaredField("usr_paths"); |
| 130 | + usrPathsField.setAccessible(true); |
| 131 | + usrPathsField.set(null, usrPathsValue); |
| 132 | + } catch (Throwable t) { |
| 133 | + // If we catch an exception we're on a modern version of java, and thus probably don't need these hacks |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + /// Enhance the command line arguments like LegacyDev does |
| 138 | + /// This reads a lot of things from the environment because I was stupid when I originally designed Legacydev -Lex |
| 139 | + /// [Reference](https://github.com/MinecraftForge/LegacyDev/blob/master/src/main/java/net/minecraftforge/legacydev/Main.java#L81) |
| 140 | + static String[] enhanceArgs(String mainClass, String[] existing) { |
| 141 | + LOGGER.info("Enhancing Arguments"); |
| 142 | + LOGGER.push(); |
| 143 | + try { |
| 144 | + Map<String, String> values = new HashMap<>(); |
| 145 | + String tweak = getenv("tweakClass"); |
| 146 | + if (tweak != null && !tweak.isEmpty()) { |
| 147 | + LOGGER.info("tweakClass: " + tweak); |
| 148 | + values.put("tweakClass", tweak); |
| 149 | + } |
| 150 | + |
| 151 | + if (LEGACYDEV_CLIENT.equals(mainClass)) { |
| 152 | + LOGGER.info("version: " + getenv("MC_VERSION")); |
| 153 | + values.put("version", getenv("MC_VERSION")); |
| 154 | + values.put("assetIndex", "{asset_index}"); |
| 155 | + values.put("assetsDir", "{assets_root}"); |
| 156 | + values.put("accessToken", "Forge"); |
| 157 | + values.put("userProperties", "[]"); |
| 158 | + values.put("username", null); |
| 159 | + values.put("password", null); |
| 160 | + } |
| 161 | + |
| 162 | + final OptionParser parser = new OptionParser(); |
| 163 | + parser.allowsUnrecognizedOptions(); |
| 164 | + |
| 165 | + for (String key : values.keySet()) |
| 166 | + parser.accepts(key).withRequiredArg().ofType(String.class); |
| 167 | + |
| 168 | + final NonOptionArgumentSpec<String> nonOption = parser.nonOptions(); |
| 169 | + |
| 170 | + final OptionSet options = parser.parse(existing); |
| 171 | + for (String key : values.keySet()) { |
| 172 | + if (options.hasArgument(key)) { |
| 173 | + String value = (String) options.valueOf(key); |
| 174 | + values.put(key, value); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + List<String> extras = new ArrayList<>(nonOption.values(options)); |
| 179 | + LOGGER.info("Extra: " + extras); |
| 180 | + |
| 181 | + List<String> lst = new ArrayList<>(values.size() * 2 + extras.size()); |
| 182 | + for (Map.Entry<String, String> entry : values.entrySet()) { |
| 183 | + if (entry.getValue() == null || entry.getValue().isEmpty()) |
| 184 | + continue; |
| 185 | + lst.add("--" + entry.getKey()); |
| 186 | + lst.add(entry.getValue()); |
| 187 | + } |
| 188 | + lst.addAll(extras); |
| 189 | + return lst.toArray(new String[lst.size()]); |
| 190 | + } finally { |
| 191 | + LOGGER.pop(); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + private static String getenv(String name) { |
| 196 | + String value = System.getenv(name); |
| 197 | + return value == null || value.isEmpty() ? null : value; |
| 198 | + } |
| 199 | + |
| 200 | + private static boolean nullOrEmpty(String value) { |
| 201 | + return value == null || value.isEmpty(); |
| 202 | + } |
| 203 | +} |
0 commit comments