Skip to content

Commit

Permalink
fix forge gametest hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Jab125 committed Jul 18, 2024
1 parent bef3ea8 commit 1563c35
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
import dev.architectury.loom.forge.ModDirTransformerDiscovererPatch;
import dev.architectury.loom.neoforge.LaunchHandlerPatcher;
import dev.architectury.loom.util.ClassVisitorUtil;

import net.fabricmc.loom.util.srg.RemapForgeGameTestHooksVisitor;

Check failure on line 39 in src/main/java/net/fabricmc/loom/configuration/providers/forge/ForgeLibrariesProvider.java

View workflow job for this annotation

GitHub Actions / build

Unused import - net.fabricmc.loom.util.srg.RemapForgeGameTestHooksVisitor.

import org.gradle.api.Project;

Check failure on line 41 in src/main/java/net/fabricmc/loom/configuration/providers/forge/ForgeLibrariesProvider.java

View workflow job for this annotation

GitHub Actions / build

Wrong order for 'org.gradle.api.Project' import.
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.ModuleDependency;
Expand Down Expand Up @@ -63,7 +66,6 @@ public class ForgeLibrariesProvider {
private static final String FANCYML_LOADER_GROUP = "net.neoforged.fancymodloader";
private static final String FANCYML_LOADER_NAME = "loader";

private static final String FORGE_GAME_TEST_HOOLS_FILE = "net/minecraftforge/gametest/ForgeGameTestHooks.class";
private static final String FORGE_OBJECT_HOLDER_FILE = "net/minecraftforge/fml/common/asm/ObjectHolderDefinalize.class";
private static final String FORGE_MOD_DIR_TRANSFORMER_DISCOVERER_FILE = "net/minecraftforge/fml/loading/ModDirTransformerDiscoverer.class";
private static final String NEOFORGE_OBJECT_HOLDER_FILE = "net/neoforged/fml/common/asm/ObjectHolderDefinalize.class";
Expand Down Expand Up @@ -186,10 +188,6 @@ private static Object remapFmlLoader(Project project, ResolvedArtifact artifact,
remapObjectHolder(project, outputJar, mappingConfiguration);
}

if (Files.exists(fs.get().getPath(FORGE_GAME_TEST_HOOLS_FILE))) {
remapObjectHolder(project, outputJar, mappingConfiguration);
}

if (Files.exists(fs.getPath(FORGE_MOD_DIR_TRANSFORMER_DISCOVERER_FILE))) {
ClassVisitorUtil.rewriteClassFile(fs.getPath(FORGE_MOD_DIR_TRANSFORMER_DISCOVERER_FILE), ModDirTransformerDiscovererPatch::new);
}
Expand Down Expand Up @@ -234,24 +232,6 @@ private static void remapObjectHolder(Project project, Path outputJar, MappingCo
}
}

private static void remapGameTestHooks(Project project, Path outputJar, MappingConfiguration mappingConfiguration) throws IOException {
try {
// Merge SRG mappings. The real SRG mapping file hasn't been created yet since the usual SRG merging
// process occurs after all Forge libraries have been provided.
// Forge libs are needed for MC, which is needed for the mappings.
final ForgeMappingsMerger.ExtraMappings extraMappings = ForgeMappingsMerger.ExtraMappings.ofMojmapTsrg(MappingConfiguration.getMojmapSrgFileIfPossible(project));
final MemoryMappingTree mappings = ForgeMappingsMerger.mergeSrg(MappingConfiguration.getRawSrgFile(project), mappingConfiguration.tinyMappings, extraMappings, true);

// Remap the game test hooks.
RemapObjectHolderVisitor.remapObjectHolder(
outputJar, "net.minecraftforge.gametest.ForgeGameTestHooks", mappings,
MappingsNamespace.SRG.toString(), MappingsNamespace.NAMED.toString()
);
} catch (IOException e) {
throw new IOException("Could not remap object holders in " + outputJar, e);
}
}

private static void remapNeoForgeObjectHolder(Project project, Path outputJar, MappingConfiguration mappingConfiguration) throws IOException {
try {
// Merge Mojang mappings. The real Mojang mapping file hasn't been created yet since the usual Mojang merging
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
import dev.architectury.loom.forge.UserdevConfig;
import dev.architectury.loom.util.MappingOption;
import dev.architectury.loom.util.TempFiles;

import net.fabricmc.loom.api.mappings.layered.MappingsNamespace;
import net.fabricmc.loom.configuration.providers.mappings.MappingConfiguration;

Check failure on line 60 in src/main/java/net/fabricmc/loom/configuration/providers/forge/MinecraftPatchedProvider.java

View workflow job for this annotation

GitHub Actions / build

Unused import - net.fabricmc.loom.configuration.providers.mappings.MappingConfiguration.
import net.fabricmc.loom.util.srg.ForgeMappingsMerger;

Check failure on line 61 in src/main/java/net/fabricmc/loom/configuration/providers/forge/MinecraftPatchedProvider.java

View workflow job for this annotation

GitHub Actions / build

Unused import - net.fabricmc.loom.util.srg.ForgeMappingsMerger.
import net.fabricmc.loom.util.srg.RemapForgeGameTestHooksVisitor;

import org.gradle.api.Project;
import org.gradle.api.logging.LogLevel;
import org.gradle.api.logging.Logger;
Expand Down Expand Up @@ -100,6 +106,7 @@ public class MinecraftPatchedProvider {
private static final String LOOM_PATCH_VERSION_KEY = "Loom-Patch-Version";
private static final String CURRENT_LOOM_PATCH_VERSION = "9";
private static final String NAME_MAPPING_SERVICE_PATH = "/inject/META-INF/services/cpw.mods.modlauncher.api.INameMappingService";
private static final String FORGE_GAME_TEST_HOOKS_FILE = "net/minecraftforge/gametest/ForgeGameTestHooks.class";

private final Project project;
private final Logger logger;
Expand Down Expand Up @@ -465,9 +472,25 @@ private void remapJar(TinyRemapper remapper, SharedServiceManager serviceManager

if (forgeUserdevJar != null) copyUserdevFiles(forgeUserdevJar, mcOutput);
if (remapCoreMods) remapCoreMods(mcOutput, serviceManager);
if (getExtension().isForge() && getExtension().getForgeProvider().usesMojangAtRuntime()) remapGameTestHooks(mcOutput, serviceManager);
applyLoomPatchVersion(mcOutput);
}

private void remapGameTestHooks(Path outputJar, SharedServiceManager serviceManager) throws IOException {
try {
final MappingOption mappingOption = MappingOption.forPlatform(getExtension());
final TinyMappingsService mappingsService = getExtension().getMappingConfiguration().getMappingsService(serviceManager, mappingOption);
final MappingTree mappings = mappingsService.getMappingTree();
// Remap the game test hooks.
RemapForgeGameTestHooksVisitor.remapForgeGameTestHooks(
outputJar, "net.minecraftforge.gametest.ForgeGameTestHooks", mappings,
MappingsNamespace.SRG.toString(), MappingsNamespace.NAMED.toString()
);
} catch (IOException e) {
throw new IOException("Could not remap gametest hooks in " + outputJar, e);
}
}

private void remapCoreMods(Path patchedJar, SharedServiceManager serviceManager) throws Exception {
final MappingOption mappingOption = MappingOption.forPlatform(getExtension());
final TinyMappingsService mappingsService = getExtension().getMappingConfiguration().getMappingsService(serviceManager, mappingOption);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2022 FabricMC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package net.fabricmc.loom.util.srg;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Collection;

Check failure on line 31 in src/main/java/net/fabricmc/loom/util/srg/RemapForgeGameTestHooksVisitor.java

View workflow job for this annotation

GitHub Actions / build

Unused import - java.util.Collection.

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;

import net.fabricmc.loom.util.Constants;
import net.fabricmc.loom.util.FileSystemUtil;
import net.fabricmc.mappingio.tree.MappingTree;

import org.objectweb.asm.Type;

Check failure on line 42 in src/main/java/net/fabricmc/loom/util/srg/RemapForgeGameTestHooksVisitor.java

View workflow job for this annotation

GitHub Actions / build

Wrong order for 'org.objectweb.asm.Type' import.

public class RemapForgeGameTestHooksVisitor extends ClassVisitor {
private final MappingTree mappings;
private final int from;
private final int to;

public RemapForgeGameTestHooksVisitor(int api, ClassVisitor classVisitor, MappingTree mappings, String from, String to) {
super(api, classVisitor);
this.mappings = mappings;
this.from = this.mappings.getNamespaceId(from);
this.to = this.mappings.getNamespaceId(to);
}

public static void remapForgeGameTestHooks(Path jar, String className, MappingTree mappings, String from, String to) throws IOException {
System.out.println("Remapping Forge GameTest hooks");

Check failure on line 57 in src/main/java/net/fabricmc/loom/util/srg/RemapForgeGameTestHooksVisitor.java

View workflow job for this annotation

GitHub Actions / build

missing blank line before block at same indentation level
try (FileSystemUtil.Delegate fs = FileSystemUtil.getJarFileSystem(jar, false)) {
Path classPath = fs.get().getPath(className.replace('.', '/') + ".class");

if (Files.exists(classPath)) {
ClassReader reader = new ClassReader(Files.readAllBytes(classPath));
ClassWriter writer = new ClassWriter(0);
ClassVisitor classVisitor = new RemapForgeGameTestHooksVisitor(Constants.ASM_VERSION, writer, mappings, from, to);
reader.accept(classVisitor, 0);
Files.write(classPath, writer.toByteArray(), StandardOpenOption.TRUNCATE_EXISTING);
}
}
}

@Override
public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
MethodVisitor methodVisitor = super.visitMethod(access, name, descriptor, signature, exceptions);

Check failure on line 73 in src/main/java/net/fabricmc/loom/util/srg/RemapForgeGameTestHooksVisitor.java

View workflow job for this annotation

GitHub Actions / build

missing blank line before block at same indentation level
if ("getDefaultBatch".equals(name) && "()Ljava/lang/String;".equals(descriptor) && from != MappingTree.NULL_NAMESPACE_ID && to != MappingTree.NULL_NAMESPACE_ID) {
return new MethodVisitor(api, methodVisitor) {
Type type;
@Override
public void visitLdcInsn(Object value) {
if (value instanceof Type type && type.getClassName().startsWith("net.minecraft.")) {
this.type = type;
}

Check failure on line 81 in src/main/java/net/fabricmc/loom/util/srg/RemapForgeGameTestHooksVisitor.java

View workflow job for this annotation

GitHub Actions / build

missing blank line after block at same indentation level

Check failure on line 81 in src/main/java/net/fabricmc/loom/util/srg/RemapForgeGameTestHooksVisitor.java

View workflow job for this annotation

GitHub Actions / build

missing blank line before block at same indentation level
if (value instanceof String str && str.startsWith("m_")) {
// This should not fail
value = mappings.getClass(type.getInternalName(), from).getMethod(str, null, from).getName(to);
}

super.visitLdcInsn(value);
}
};
}

return methodVisitor;
}
}

0 comments on commit 1563c35

Please sign in to comment.