From 221d326f6bc3464fabb65a94c53390dd7a530213 Mon Sep 17 00:00:00 2001 From: Cat Core Date: Mon, 30 Oct 2023 21:26:22 +0100 Subject: [PATCH] Working Dev environment for 1.3+, client and server separated but not pre 1.3 client-server-merged --- build.gradle | 5 +- gradle.properties | 4 +- .../remapping/MappingBuilder.java | 4 +- .../remapping/MixinPostApplyVisitor.java | 3 - .../modremapperapi/remapping/RemapUtil.java | 22 +-- .../modremapperapi/utils/MappingsUtils.java | 175 +++++++++++++++++- src/main/resources/fabric.mod.json | 2 +- 7 files changed, 181 insertions(+), 34 deletions(-) diff --git a/build.gradle b/build.gradle index 2bb64eb..fef8e92 100644 --- a/build.gradle +++ b/build.gradle @@ -118,8 +118,9 @@ publishing { modrinth { token = System.getenv("MODRINTH_TOKEN") projectId = "mod-remapping-api" - changelog = """Move out annotations to an included library (CursedMixinExtensions).\ -Update bundled MixinExtras version to 0.2.0-rc.4. + changelog = """ +Working Dev environment for 1.3+, client and server separated but not pre 1.3 client-server-merged. +Update bundled MixinExtras version to 0.2.0. """ uploadFile = remapJar diff --git a/gradle.properties b/gradle.properties index 267db92..75f218e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,9 +6,9 @@ org.gradle.jvmargs=-Xmx1G # Fabric Properties minecraft_version = 1.8.9 yarn_build = 458 - loader_version = 0.14.22 + loader_version = 0.14.24 fabric_version = 1.9.1+1.8.9 - mixin_extras_version=0.2.0-rc.4 + mixin_extras_version=0.2.0 spasm_version=0.2 # Mod Properties diff --git a/src/main/java/fr/catcore/modremapperapi/remapping/MappingBuilder.java b/src/main/java/fr/catcore/modremapperapi/remapping/MappingBuilder.java index f676606..06d36b3 100644 --- a/src/main/java/fr/catcore/modremapperapi/remapping/MappingBuilder.java +++ b/src/main/java/fr/catcore/modremapperapi/remapping/MappingBuilder.java @@ -55,7 +55,7 @@ public MappingBuilder method(String name, String description) { public List build() { List list = new ArrayList<>(); if (ModRemappingAPI.BABRIC) { - list.add(toString("CLASS", this.intermediary, this.intermediary, this.obfucated, this.obfucated)); + list.add(toString("CLASS", this.intermediary, this.intermediary, this.obfucated, this.obfucated, this.intermediary)); } else { list.add(toString("CLASS", this.obfucated, this.intermediary, this.intermediary)); } @@ -80,7 +80,7 @@ public Entry(String obfuscated, String intermediary, String description, Type ty public String toString(String className) { if (ModRemappingAPI.BABRIC) { - return MappingBuilder.toString(this.type.name(), className, this.description, this.intermediary, this.intermediary, this.obfuscated, this.obfuscated); + return MappingBuilder.toString(this.type.name(), className, this.description, this.intermediary, this.intermediary, this.obfuscated, this.obfuscated, this.intermediary); } else { return MappingBuilder.toString(this.type.name(), className, this.description, this.obfuscated, this.intermediary, this.intermediary); } diff --git a/src/main/java/fr/catcore/modremapperapi/remapping/MixinPostApplyVisitor.java b/src/main/java/fr/catcore/modremapperapi/remapping/MixinPostApplyVisitor.java index 810def6..d957b3d 100644 --- a/src/main/java/fr/catcore/modremapperapi/remapping/MixinPostApplyVisitor.java +++ b/src/main/java/fr/catcore/modremapperapi/remapping/MixinPostApplyVisitor.java @@ -13,9 +13,6 @@ import java.util.ArrayList; import java.util.List; -import static fr.catcore.modremapperapi.utils.MappingsUtils.getNativeNamespace; -import static fr.catcore.modremapperapi.utils.MappingsUtils.getTargetNamespace; - public class MixinPostApplyVisitor implements TinyRemapper.ApplyVisitorProvider{ private final List classDefs = new ArrayList<>(); public MixinPostApplyVisitor(MappingTree[] trees) { diff --git a/src/main/java/fr/catcore/modremapperapi/remapping/RemapUtil.java b/src/main/java/fr/catcore/modremapperapi/remapping/RemapUtil.java index 139d7f0..ecbe298 100644 --- a/src/main/java/fr/catcore/modremapperapi/remapping/RemapUtil.java +++ b/src/main/java/fr/catcore/modremapperapi/remapping/RemapUtil.java @@ -8,7 +8,6 @@ import fr.catcore.modremapperapi.utils.MappingsUtils; import net.fabricmc.api.EnvType; import net.fabricmc.loader.api.FabricLoader; -import net.fabricmc.loader.impl.launch.FabricLauncherBase; import net.fabricmc.mappingio.tree.MappingTree; import net.fabricmc.mappingio.tree.MemoryMappingTree; import net.fabricmc.tinyremapper.*; @@ -171,7 +170,7 @@ public static void generateModMappings() { List lines = new ArrayList<>(); if (ModRemappingAPI.BABRIC) { - lines.add(toString("v1", "intermediary", "glue", "server", "client")); + lines.add(toString("v1", "intermediary", "glue", "server", "client", "named")); } else { lines.add(toString("v1", "official", "intermediary", "named")); } @@ -226,7 +225,7 @@ private static void generateMappings() { List lines = new ArrayList<>(); if (ModRemappingAPI.BABRIC) { - lines.add(toString("v1", "intermediary", "glue", "server", "client")); + lines.add(toString("v1", "intermediary", "glue", "server", "client", "named")); } else { lines.add(toString("v1", "official", "intermediary", "named")); } @@ -319,20 +318,7 @@ private static TinyRemapper makeRemapper(MappingTree... trees) { TinyRemapper remapper = builder.build(); - if (FabricLoader.getInstance().isDevelopmentEnvironment()) { - try { - remapper.readClassPathAsync(getRemapClasspath().toArray(new Path[0])); - } catch (IOException e) { - throw new RuntimeException("Failed to populate default remap classpath", e); - } - } else { - remapper.readClassPathAsync((Path) FabricLoader.getInstance().getObjectShare().get("fabric-loader:inputGameJar")); - - for (Path path : FabricLauncherBase.getLauncher().getClassPath()) { - Constants.MAIN_LOGGER.debug("Appending '%s' to remapper classpath", path); - remapper.readClassPathAsync(path); - } - } + MappingsUtils.addMinecraftJar(remapper); for (ModRemapper modRemapper : ModRemappingAPI.MOD_REMAPPERS) { for (RemapLibrary library : modRemapper.getRemapLibraries()) { @@ -469,7 +455,7 @@ public static EnvType getEnvironment() { return FabricLoader.getInstance().getEnvironmentType(); } - private static List getRemapClasspath() throws IOException { + public static List getRemapClasspath() throws IOException { String remapClasspathFile = System.getProperty("fabric.remapClasspathFile"); if (remapClasspathFile == null) { diff --git a/src/main/java/fr/catcore/modremapperapi/utils/MappingsUtils.java b/src/main/java/fr/catcore/modremapperapi/utils/MappingsUtils.java index 4abaf6d..1f44800 100644 --- a/src/main/java/fr/catcore/modremapperapi/utils/MappingsUtils.java +++ b/src/main/java/fr/catcore/modremapperapi/utils/MappingsUtils.java @@ -1,8 +1,10 @@ package fr.catcore.modremapperapi.utils; import fr.catcore.modremapperapi.ModRemappingAPI; +import fr.catcore.modremapperapi.remapping.RefmapRemapper; import net.fabricmc.api.EnvType; import net.fabricmc.loader.api.FabricLoader; +import net.fabricmc.loader.impl.launch.FabricLauncherBase; import net.fabricmc.loader.impl.launch.MappingConfiguration; import net.fabricmc.loader.impl.util.ManifestUtil; import net.fabricmc.loader.impl.util.log.Log; @@ -15,19 +17,23 @@ import net.fabricmc.mappingio.format.Tiny2Reader; import net.fabricmc.mappingio.tree.MappingTree; import net.fabricmc.mappingio.tree.MemoryMappingTree; -import net.fabricmc.tinyremapper.IMappingProvider; +import net.fabricmc.tinyremapper.*; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.Reader; +import java.io.*; import java.net.JarURLConnection; import java.net.URL; import java.net.URLConnection; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.jar.Attributes; import java.util.jar.Manifest; import java.util.zip.ZipError; +import static fr.catcore.modremapperapi.remapping.RemapUtil.getRemapClasspath; + public class MappingsUtils { private static MemoryMappingTree MINECRAFT_MAPPINGS; private static boolean initialized = false; @@ -41,7 +47,7 @@ public static String getNativeNamespace() { } public static String getTargetNamespace() { - return "intermediary"; + return !FabricLoader.getInstance().isDevelopmentEnvironment() ? "intermediary" : FabricLoader.getInstance().getMappingResolver().getCurrentRuntimeNamespace(); } private static void initialize() { @@ -179,7 +185,164 @@ public static IMappingProvider createProvider(MappingTree mappings) { }; } + private static IMappingProvider createBackwardProvider(MappingTree mappings) { + return (acceptor) -> { + final int fromId = mappings.getNamespaceId(getTargetNamespace()); + final int toId = mappings.getNamespaceId(getNativeNamespace()); + + for (MappingTree.ClassMapping classDef : mappings.getClasses()) { + String className = classDef.getName(fromId); + String dstName = classDef.getName(toId); + + if (ModRemappingAPI.BABRIC && dstName == null) { + if (className == null) continue; + dstName = className; + } + + if (className == null) { + className = dstName; + } + + acceptor.acceptClass(className, dstName); + + for (MappingTree.FieldMapping field : classDef.getFields()) { + String fieldName = field.getName(fromId); + String dstFieldName = field.getName(toId); + String fieldDesc = field.getDesc(fromId); + String dstFieldDesc = field.getDesc(toId); + + if (ModRemappingAPI.BABRIC && dstFieldName == null) { + if (fieldName == null) continue; + dstFieldName = fieldName; + } + + if (ModRemappingAPI.BABRIC && dstFieldDesc == null) { + if (fieldDesc == null) continue; + dstFieldDesc = fieldDesc; + } + + acceptor.acceptField(memberOf(className, fieldName, fieldDesc), dstFieldName); + } + + for (MappingTree.MethodMapping method : classDef.getMethods()) { + String methodName = method.getName(fromId); + String dstMethodName = method.getName(toId); + String methodDesc = method.getDesc(fromId); + String dstMethodDesc = method.getDesc(toId); + + if (ModRemappingAPI.BABRIC && dstMethodName == null) { + if (methodName == null) continue; + dstMethodName = methodName; + } + + if (ModRemappingAPI.BABRIC && dstMethodDesc == null) { + if (methodDesc == null) continue; + dstMethodDesc = methodDesc; + } + + IMappingProvider.Member methodIdentifier = memberOf(className, methodName, methodDesc); + acceptor.acceptMethod(methodIdentifier, dstMethodName); + } + } + }; + } + private static IMappingProvider.Member memberOf(String className, String memberName, String descriptor) { return new IMappingProvider.Member(className, memberName, descriptor); } + + private static Path[] getMinecraftJar() throws IOException { + Path[] originalClassPath = getRemapClasspath().toArray(new Path[0]); + + Map paths = new HashMap<>(); + + for (Path path : + originalClassPath) { + Constants.MAIN_LOGGER.info(path.toString()); + paths.put(path, new File(Constants.LIB_FOLDER, path.toFile().getName()).toPath()); + paths.get(path).toFile().delete(); + } + + TinyRemapper.Builder builder = TinyRemapper + .newRemapper() + .renameInvalidLocals(true) + .ignoreFieldDesc(false) + .propagatePrivate(true) + .ignoreConflicts(true) + .fixPackageAccess(true) + .withMappings(createBackwardProvider(getMinecraftMappings())); + + TinyRemapper remapper = builder.build(); + + Constants.MAIN_LOGGER.info("Remapping minecraft jar back to obfuscated!"); + + List outputConsumerPaths = new ArrayList<>(); + + List resourceRemappers = new ArrayList<>(NonClassCopyMode.FIX_META_INF.remappers); + + try { + Map tagMap = new HashMap<>(); + + Constants.MAIN_LOGGER.debug("Creating InputTags!"); + for (Path input : paths.keySet()) { + InputTag tag = remapper.createInputTag(); + tagMap.put(input, tag); + remapper.readInputsAsync(tag, input); + } + + Constants.MAIN_LOGGER.debug("Initializing remapping!"); + for (Map.Entry entry : paths.entrySet()) { + Constants.MAIN_LOGGER.debug("Starting remapping " + entry.getKey().toString() + " to " + entry.getValue().toString()); + OutputConsumerPath outputConsumer = new OutputConsumerPath.Builder(entry.getValue()).build(); + + outputConsumerPaths.add(outputConsumer); + + Constants.MAIN_LOGGER.debug("Apply remapper!"); + remapper.apply(outputConsumer, tagMap.get(entry.getKey())); + + Constants.MAIN_LOGGER.debug("Add input as non class file!"); + outputConsumer.addNonClassFiles(entry.getKey(), remapper, resourceRemappers); + + Constants.MAIN_LOGGER.debug("Done 1!"); + } + } catch (Exception e) { + remapper.finish(); + outputConsumerPaths.forEach(o -> { + try { + o.close(); + } catch (IOException e2) { + e2.printStackTrace(); + } + }); + throw new RuntimeException("Failed to remap jar", e); + } finally { + remapper.finish(); + outputConsumerPaths.forEach(o -> { + try { + o.close(); + } catch (IOException e) { + e.printStackTrace(); + } + }); + } + + return paths.values().toArray(new Path[0]); + } + + public static void addMinecraftJar(TinyRemapper remapper) { + if (FabricLoader.getInstance().isDevelopmentEnvironment()) { + try { + remapper.readClassPathAsync(getMinecraftJar()); + } catch (IOException e) { + throw new RuntimeException("Failed to populate default remap classpath", e); + } + } else { + remapper.readClassPathAsync((Path) FabricLoader.getInstance().getObjectShare().get("fabric-loader:inputGameJar")); + + for (Path path : FabricLauncherBase.getLauncher().getClassPath()) { + Constants.MAIN_LOGGER.debug("Appending '%s' to remapper classpath", path); + remapper.readClassPathAsync(path); + } + } + } } diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 9b69c60..44db8bd 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -32,7 +32,7 @@ "depends": { "fabricloader": ">=0.14.18", "spasm": ">=0.2.0", - "com_github_llamalad7_mixinextras": ">=0.2.0-rc.4" + "com_github_llamalad7_mixinextras": ">=0.2.0" }, "mixins": [ "mixins.mod-remapping-api.json"