From 14f45258995abf666aaf083a0c4ed9df0111c0bc Mon Sep 17 00:00:00 2001 From: Sebastian Hartte Date: Wed, 11 Dec 2024 21:16:58 +0100 Subject: [PATCH] Make it MergeMappings --- .../AddParamsToOfficialMappings.java | 67 --------------- .../installertools/MergeMappings.java | 83 ++++++++++++------- .../net/neoforged/installertools/Tasks.java | 1 - ...ppingsTest.java => MergeMappingsTest.java} | 10 +-- 4 files changed, 58 insertions(+), 103 deletions(-) delete mode 100644 src/main/java/net/neoforged/installertools/AddParamsToOfficialMappings.java rename src/test/java/net/neoforged/installertools/{AddParamsToOfficialMappingsTest.java => MergeMappingsTest.java} (84%) diff --git a/src/main/java/net/neoforged/installertools/AddParamsToOfficialMappings.java b/src/main/java/net/neoforged/installertools/AddParamsToOfficialMappings.java deleted file mode 100644 index c572676..0000000 --- a/src/main/java/net/neoforged/installertools/AddParamsToOfficialMappings.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * InstallerTools - * Copyright (c) 2019-2021. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation version 2.1 - * of the License. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ -package net.neoforged.installertools; - -import joptsimple.OptionException; -import joptsimple.OptionParser; -import joptsimple.OptionSet; -import joptsimple.OptionSpec; -import net.neoforged.srgutils.IMappingFile; - -import java.io.File; -import java.io.IOException; - -/** - * Takes a file containing the official Mojang mappings and adds mergers a TSRG mappings file into it with the purpose - * of adding parameter names. - * The result is written out as a TSRG file mapping obfuscated to named. - */ -public class AddParamsToOfficialMappings extends Task { - @Override - public void process(String[] args) throws IOException { - OptionParser parser = new OptionParser(); - OptionSpec official = parser.accepts("official").withRequiredArg().ofType(File.class).required(); - OptionSpec paramsTsrg = parser.accepts("params").withRequiredArg().ofType(File.class).required(); - OptionSpec outputTsrg = parser.accepts("output").withRequiredArg().ofType(File.class).required(); - - try { - OptionSet options = parser.parse(args); - - mergeParams( - options.valueOf(official), - options.valueOf(paramsTsrg), - options.valueOf(outputTsrg) - ); - } catch (OptionException e) { - parser.printHelpOn(System.out); - error("Please provide correct parameters"); - } - } - - private void mergeParams(File officialFile, File paramsTsrgFile, File outputTsrgFile) throws IOException { - log("Official Mappings: " + officialFile); - log("Parameter Mappings: " + paramsTsrgFile); - log("Output: " + outputTsrgFile); - - IMappingFile officialMappings = IMappingFile.load(officialFile).reverse(); - IMappingFile paramMappings = IMappingFile.load(paramsTsrgFile); - IMappingFile merged = officialMappings.merge(paramMappings); - merged.write(outputTsrgFile.toPath(), IMappingFile.Format.TSRG2, false); - } -} diff --git a/src/main/java/net/neoforged/installertools/MergeMappings.java b/src/main/java/net/neoforged/installertools/MergeMappings.java index b131265..791a34d 100644 --- a/src/main/java/net/neoforged/installertools/MergeMappings.java +++ b/src/main/java/net/neoforged/installertools/MergeMappings.java @@ -18,42 +18,65 @@ */ package net.neoforged.installertools; +import joptsimple.OptionException; +import joptsimple.OptionParser; +import joptsimple.OptionSet; +import joptsimple.OptionSpec; import net.neoforged.srgutils.IMappingFile; -import net.neoforged.srgutils.IRenamer; -import net.neoforged.srgutils.IMappingFile.IClass; -import net.neoforged.srgutils.IMappingFile.IField; -import net.neoforged.srgutils.IMappingFile.IMethod; -import net.neoforged.srgutils.IMappingFile.IPackage; -import net.neoforged.srgutils.IMappingFile.IParameter; -public class MergeMappings extends ChainMappings { +import java.io.File; +import java.io.IOException; + +public class MergeMappings extends Task { @Override - protected IRenamer makeRenamer(IMappingFile link, boolean classes, boolean fields, boolean methods, boolean params) { - return new IRenamer() { - public String rename(IPackage value) { - return link.remapPackage(value.getOriginal()); - } + public void process(String[] args) throws IOException { + OptionParser parser = new OptionParser(); + OptionSpec base = parser.accepts("base").withRequiredArg().ofType(File.class).required(); + OptionSpec reverseBase = parser.accepts("reverse-base"); + OptionSpec merge = parser.accepts("merge").withRequiredArg().ofType(File.class).required(); + OptionSpec reverseMerge = parser.accepts("reverse-merge"); + OptionSpec output = parser.accepts("output").withRequiredArg().ofType(File.class).required(); + OptionSpec reverseOutput = parser.accepts("reverse-output"); - public String rename(IClass value) { - return classes ? link.remapClass(value.getOriginal()) : value.getMapped(); - } + try { + OptionSet options = parser.parse(args); - public String rename(IField value) { - IClass cls = link.getClass(value.getParent().getOriginal()); - return cls == null || !fields ? value.getMapped() : cls.remapField(value.getOriginal()); - } + merge( + options.valueOf(base), + options.has(reverseBase), + options.valueOf(merge), + options.has(reverseMerge), + options.valueOf(output), + options.has(reverseOutput) + ); + } catch (OptionException e) { + parser.printHelpOn(System.out); + error("Please provide correct parameters"); + } + } - public String rename(IMethod value) { - IClass cls = link.getClass(value.getParent().getOriginal()); - return cls == null || !methods ? value.getMapped() : cls.remapMethod(value.getOriginal(), value.getDescriptor()); - } + private void merge(File baseFile, boolean reverseBase, + File mergeFile, boolean reverseMerge, + File outputFile, boolean reverseOutput) throws IOException { + log("Base Mappings: " + baseFile); + log("Reverse Base Mappings: " + reverseBase); + log("Merge Mappings: " + mergeFile); + log("Reverse Merge Mappings: " + reverseMerge); + log("Output: " + outputFile); + log("Reverse Output: " + reverseOutput); - public String rename(IParameter value) { - IMethod mtd = value.getParent(); - IClass cls = link.getClass(mtd.getParent().getOriginal()); - mtd = cls == null ? null : cls.getMethod(mtd.getOriginal(), mtd.getDescriptor()); - return mtd == null || !params ? value.getMapped() : mtd.remapParameter(value.getIndex(), value.getMapped()); - } - }; + IMappingFile baseMappings = IMappingFile.load(baseFile); + if (reverseBase) { + baseMappings = baseMappings.reverse(); + } + IMappingFile mergeMappings = IMappingFile.load(mergeFile); + if (reverseMerge) { + mergeMappings = mergeMappings.reverse(); + } + IMappingFile outputMappings = baseMappings.merge(mergeMappings); + if (reverseOutput) { + outputMappings = outputMappings.reverse(); + } + outputMappings.write(outputFile.toPath(), IMappingFile.Format.TSRG2, false); } } diff --git a/src/main/java/net/neoforged/installertools/Tasks.java b/src/main/java/net/neoforged/installertools/Tasks.java index cc54703..2156a95 100644 --- a/src/main/java/net/neoforged/installertools/Tasks.java +++ b/src/main/java/net/neoforged/installertools/Tasks.java @@ -31,7 +31,6 @@ public enum Tasks { DOWNLOAD_MOJMAPS(DownloadMojmaps::new), EXTRACT_FILES(ExtractFiles::new), BUNDLER_EXTRACT(BundlerExtract::new), - ADD_PARAMS_TO_OFFICIAL_MAPPINGS(AddParamsToOfficialMappings::new) ; private Supplier supplier; diff --git a/src/test/java/net/neoforged/installertools/AddParamsToOfficialMappingsTest.java b/src/test/java/net/neoforged/installertools/MergeMappingsTest.java similarity index 84% rename from src/test/java/net/neoforged/installertools/AddParamsToOfficialMappingsTest.java rename to src/test/java/net/neoforged/installertools/MergeMappingsTest.java index 4d9c597..4ba0dc1 100644 --- a/src/test/java/net/neoforged/installertools/AddParamsToOfficialMappingsTest.java +++ b/src/test/java/net/neoforged/installertools/MergeMappingsTest.java @@ -3,13 +3,12 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; -import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import static org.assertj.core.api.Assertions.assertThat; -class AddParamsToOfficialMappingsTest { +class MergeMappingsTest { @TempDir Path tempDir; @@ -21,10 +20,11 @@ void testMergeMappings() throws Exception { Path outputPath = tempDir.resolve("output.txt"); // This is what NeoForm calls - new AddParamsToOfficialMappings().process(new String[]{ - "--official", + new MergeMappings().process(new String[]{ + "--base", officialMappings.toAbsolutePath().toString(), - "--params", + "--reverse-base", + "--merge", neoFormMappings.toAbsolutePath().toString(), "--output", outputPath.toString()