|
| 1 | +/* |
| 2 | + * Copyright (c) Forge Development LLC and contributors |
| 3 | + * SPDX-License-Identifier: LGPL-2.1-only |
| 4 | + */ |
| 5 | +package net.minecraftforge.mcmaven.cli; |
| 6 | + |
| 7 | +import java.io.File; |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.util.zip.ZipFile; |
| 10 | + |
| 11 | +import joptsimple.OptionParser; |
| 12 | +import net.minecraftforge.mcmaven.impl.MinecraftMaven; |
| 13 | +import net.minecraftforge.mcmaven.impl.cache.Cache; |
| 14 | +import net.minecraftforge.mcmaven.impl.mappings.Mappings; |
| 15 | +import net.minecraftforge.mcmaven.impl.mappings.ParchmentMappings; |
| 16 | +import net.minecraftforge.mcmaven.impl.repo.mcpconfig.MCPConfigRepo; |
| 17 | +import net.minecraftforge.mcmaven.impl.util.Artifact; |
| 18 | +import net.minecraftforge.srgutils.IMappingFile; |
| 19 | +import net.minecraftforge.srgutils.IRenamer; |
| 20 | +import net.minecraftforge.srgutils.IMappingFile.IField; |
| 21 | +import net.minecraftforge.srgutils.IMappingFile.IMethod; |
| 22 | +import net.minecraftforge.srgutils.IMappingFile.IParameter; |
| 23 | +import net.minecraftforge.util.logging.Log; |
| 24 | + |
| 25 | +// TODO [Mavenizer][MCPDataTask] This is a copy of FG6's ExtractMCPData task. |
| 26 | +// its not the best, but I dont want to re-wrok INSTALLER_TOOLS to put the tsrg in the mappings zip |
| 27 | +public class MCPDataTask { |
| 28 | + public static void run(String[] args) throws Exception { |
| 29 | + int ret = runI(args); |
| 30 | + if (ret != 0) { |
| 31 | + Log.release(); |
| 32 | + //System.exit(ret); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + private static int runI(String[] args) throws Exception { |
| 37 | + // TODO [MCMavenizer] Make this into a --log [level] option |
| 38 | + Log.enabled = Log.Level.INFO; |
| 39 | + |
| 40 | + var parser = new OptionParser(); |
| 41 | + parser.allowsUnrecognizedOptions(); |
| 42 | + |
| 43 | + //@formatter:off |
| 44 | + // help message |
| 45 | + var helpO = parser.accepts("help", |
| 46 | + "Displays this help message and exits") |
| 47 | + .forHelp(); |
| 48 | + |
| 49 | + // root cache directory |
| 50 | + var cacheO = parser.accepts("cache", |
| 51 | + "Directory to store data needed for this program") |
| 52 | + .withRequiredArg().ofType(File.class).defaultsTo(new File("cache")); |
| 53 | + |
| 54 | + // jdk cache directory |
| 55 | + var jdkCacheO = parser.accepts("jdk-cache", |
| 56 | + "Directory to store jdks downloaded from the disoco api") |
| 57 | + .withRequiredArg().ofType(File.class).defaultsTo(new File("cache/jdks")); |
| 58 | + |
| 59 | + var outputO = parser.accepts("output", |
| 60 | + "File to place output data into") |
| 61 | + .withRequiredArg().ofType(File.class); |
| 62 | + |
| 63 | + var artifactO = parser.accepts("artifact", |
| 64 | + "MCPConfig artifact coordinates") |
| 65 | + .withRequiredArg(); |
| 66 | + |
| 67 | + var versionO = parser.accepts("version", |
| 68 | + "MCPConfig artifact version") |
| 69 | + .availableUnless(artifactO) |
| 70 | + .withRequiredArg(); |
| 71 | + |
| 72 | + var officialO = parser.accepts("mappings", |
| 73 | + "Use to enable using official mappings"); |
| 74 | + |
| 75 | + var parchmentO = parser.accepts("parchment", |
| 76 | + "Version of parchment mappings to use, snapshots are not supported") |
| 77 | + .availableUnless(officialO) |
| 78 | + .withRequiredArg(); |
| 79 | + |
| 80 | + officialO |
| 81 | + .availableUnless(parchmentO); |
| 82 | + |
| 83 | + var keyO = parser.accepts("key", |
| 84 | + "The key for which data file to extract") |
| 85 | + .withRequiredArg(); |
| 86 | + //@formatter:on |
| 87 | + |
| 88 | + var options = parser.parse(args); |
| 89 | + if (options.has(helpO)) { |
| 90 | + parser.printHelpOn(Log.INFO); |
| 91 | + return -1; |
| 92 | + } |
| 93 | + |
| 94 | + var output = options.valueOf(outputO); |
| 95 | + var cacheRoot = options.valueOf(cacheO); |
| 96 | + var jdkCacheRoot = !options.has(cacheO) || options.has(jdkCacheO) |
| 97 | + ? options.valueOf(jdkCacheO) |
| 98 | + : new File(cacheRoot, "jdks"); |
| 99 | + |
| 100 | + var artifact = |
| 101 | + options.has(artifactO) ? Artifact.from(options.valueOf(artifactO)) : |
| 102 | + options.has(versionO) ? Artifact.from("de.oceanlabs.mcp", "mcp_config", options.valueOf(versionO), null, "zip") : |
| 103 | + null; |
| 104 | + |
| 105 | + if (artifact == null) { |
| 106 | + Log.error("Missing mcp --version or --artifact"); |
| 107 | + return -2; |
| 108 | + } |
| 109 | + |
| 110 | + var mcVersion = MinecraftMaven.mcpToMcVersion(artifact.getVersion()); |
| 111 | + Mappings mappings = null; |
| 112 | + if (options.has(officialO)) |
| 113 | + mappings = new Mappings("official", null).withMCVersion(mcVersion); |
| 114 | + else if (options.has(parchmentO)) |
| 115 | + mappings = new ParchmentMappings(options.valueOf(parchmentO)).withMCVersion(mcVersion); |
| 116 | + else |
| 117 | + mappings = null; |
| 118 | + |
| 119 | + var key = options.valueOf(keyO); |
| 120 | + if (key == null) { |
| 121 | + Log.error("Missing --key option"); |
| 122 | + return -3; |
| 123 | + } |
| 124 | + |
| 125 | + var repo = new MCPConfigRepo(new Cache(cacheRoot, jdkCacheRoot)); |
| 126 | + Log.info(" Output: " + output.getAbsolutePath()); |
| 127 | + Log.info(" Cache: " + cacheRoot.getAbsolutePath()); |
| 128 | + Log.info(" JDK Cache: " + jdkCacheRoot.getAbsolutePath()); |
| 129 | + Log.info(" Artifact: " + artifact); |
| 130 | + Log.info(" Key: " + key); |
| 131 | + if (mappings != null) |
| 132 | + Log.info(" Mappings: " + mappings); |
| 133 | + Log.info(); |
| 134 | + |
| 135 | + var mcp = repo.get(artifact); |
| 136 | + var side = mcp.getSide("joined"); |
| 137 | + |
| 138 | + var cfg = mcp.getConfig().getData("joined"); |
| 139 | + var path = cfg.get(key); |
| 140 | + if (path == null && "statics".equals(key)) |
| 141 | + path = "config/static_methods.txt"; |
| 142 | + |
| 143 | + if (path == null) { |
| 144 | + Log.error("Could not find data entry for '%s'".formatted(key)); |
| 145 | + return -4; |
| 146 | + } |
| 147 | + |
| 148 | + try (ZipFile zip = new ZipFile(mcp.getData())) { |
| 149 | + var entry = zip.getEntry(path); |
| 150 | + if (entry == null) { |
| 151 | + Log.error("Invalid config zip, missing file: " + path); |
| 152 | + return -5; |
| 153 | + } |
| 154 | + |
| 155 | + if ("mappings".equals(key)) { |
| 156 | + var ret = IMappingFile.load(zip.getInputStream(entry)); |
| 157 | + |
| 158 | + if (mcp.getConfig().official) { |
| 159 | + var mc = mcp.getMinecraftTasks(); |
| 160 | + var client = mc.versionFile("client_mappings", "txt"); |
| 161 | + var server = mc.versionFile("server_mappings", "txt"); |
| 162 | + |
| 163 | + var obf2OffClient = IMappingFile.load(client.execute()); |
| 164 | + var obf2OffServer = IMappingFile.load(server.execute()); |
| 165 | + var obf2Off = obf2OffClient.merge(obf2OffServer); |
| 166 | + |
| 167 | + ret = ret.rename(new IRenamer() { |
| 168 | + @Override |
| 169 | + public String rename(IMappingFile.IClass value) { |
| 170 | + return obf2Off.remapClass(value.getOriginal()); |
| 171 | + } |
| 172 | + }); |
| 173 | + } |
| 174 | + |
| 175 | + if (mappings != null) { |
| 176 | + var csv = mappings.getCsvZip(side).execute(); |
| 177 | + var names = Mappings.load(csv).names(); |
| 178 | + ret = ret.rename(new IRenamer() { |
| 179 | + @Override |
| 180 | + public String rename(IField value) { |
| 181 | + return names.getOrDefault(value.getMapped(), value.getMapped()); |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public String rename(IMethod value) { |
| 186 | + return names.getOrDefault(value.getMapped(), value.getMapped()); |
| 187 | + } |
| 188 | + |
| 189 | + @Override |
| 190 | + public String rename(IParameter value) { |
| 191 | + return names.getOrDefault(value.getMapped(), value.getMapped()); |
| 192 | + } |
| 193 | + }); |
| 194 | + } |
| 195 | + |
| 196 | + |
| 197 | + ret.write(output.getAbsoluteFile().toPath(), IMappingFile.Format.TSRG2, false); |
| 198 | + } else { |
| 199 | + Files.copy(zip.getInputStream(entry), output.toPath()); |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + return 0; |
| 204 | + } |
| 205 | +} |
0 commit comments