Skip to content

Commit

Permalink
Fix NeoForge ATs not being fully remapped (#185)
Browse files Browse the repository at this point in the history
Fixes #184. Also rewrites all code using the CadixDev AT library to use our fork.
  • Loading branch information
Juuxel authored Dec 16, 2023
1 parent 5b3e7c7 commit 45bb684
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 24 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ dependencies {
implementation libs.opencsv
implementation libs.forge.diffpatch
implementation libs.datafixerupper
implementation libs.at

// Forge mods.toml parsing
implementation libs.night.config.toml
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ opencsv = "5.4"
forge-diffpatch = "2.0.7"
night-config = "3.6.6"
datafixerupper = "6.0.8"
at = "1.0.1"

[libraries]
# Loom compile libraries
Expand Down Expand Up @@ -62,6 +63,7 @@ opencsv = { module = "com.opencsv:opencsv", version.ref = "opencsv" }
forge-diffpatch = { module = "net.minecraftforge:DiffPatch", version.ref = "forge-diffpatch" }
night-config-toml = { module = "com.electronwill.night-config:toml", version.ref = "night-config" }
datafixerupper = { module = "com.mojang:datafixerupper", version.ref = "datafixerupper" }
at = { module = "dev.architectury:at", version.ref = "at" }

[plugins]
kotlin = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
import java.util.jar.JarFile;
import java.util.jar.Manifest;

import org.cadixdev.at.AccessTransformSet;
import org.cadixdev.at.io.AccessTransformFormats;
import org.cadixdev.lorenz.MappingSet;
import dev.architectury.at.AccessTransformSet;
import dev.architectury.at.io.AccessTransformFormats;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.SetProperty;
import org.jetbrains.annotations.Nullable;
Expand All @@ -29,7 +28,6 @@
import net.fabricmc.loom.util.LfWriter;
import net.fabricmc.loom.util.aw2at.Aw2At;
import net.fabricmc.loom.util.service.UnsafeWorkQueueHelper;
import net.fabricmc.lorenztiny.TinyMappingsReader;

public final class ModBuildExtensions {
public static Set<String> readMixinConfigsFromManifest(File jarFile) {
Expand Down Expand Up @@ -87,11 +85,7 @@ public static void convertAwToAt(SetProperty<String> atAccessWidenersProperty, P
}

MappingsService service = UnsafeWorkQueueHelper.get(mappingBuildServiceUuid, MappingsService.class);

try (TinyMappingsReader reader = new TinyMappingsReader(service.getMemoryMappingTree(), service.getFromNamespace(), service.getToNamespace())) {
MappingSet mappingSet = reader.read();
at = at.remap(mappingSet);
}
at = at.remap(service.getMemoryMappingTree(), service.getFromNamespace(), service.getToNamespace());

try (Writer writer = new LfWriter(Files.newBufferedWriter(atPath))) {
AccessTransformFormats.FML.write(writer, at);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import net.fabricmc.loom.LoomGradlePlugin;
import net.fabricmc.loom.configuration.ifaceinject.InterfaceInjectionProcessor;
import net.fabricmc.loom.util.ModPlatform;

public final class ArchitecturyCommonJson implements JsonBackedModMetadataFile, SingleIdModMetadataFile {
public static final String FILE_NAME = "architectury.common.json";
Expand Down Expand Up @@ -68,6 +69,11 @@ public Set<String> getAccessWideners() {
}
}

@Override
public Set<String> getAccessTransformers(ModPlatform platform) {
return Set.of();
}

@Override
public List<InterfaceInjectionProcessor.InjectedInterface> getInjectedInterfaces(@Nullable String modId) {
if (modId == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.jetbrains.annotations.VisibleForTesting;

import net.fabricmc.loom.configuration.ifaceinject.InterfaceInjectionProcessor;
import net.fabricmc.loom.util.ModPlatform;

/**
* A fallback mod metadata file that represents a non-fatal format error
Expand All @@ -31,6 +32,11 @@ public Set<String> getAccessWideners() {
return Set.of();
}

@Override
public Set<String> getAccessTransformers(ModPlatform platform) {
return Set.of();
}

@Override
public List<InterfaceInjectionProcessor.InjectedInterface> getInjectedInterfaces(@Nullable String modId) {
return List.of();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.jetbrains.annotations.Nullable;

import net.fabricmc.loom.configuration.ifaceinject.InterfaceInjectionProcessor;
import net.fabricmc.loom.util.ModPlatform;
import net.fabricmc.loom.util.function.CollectionUtil;

/**
Expand All @@ -32,6 +33,13 @@ public interface ModMetadataFile {
*/
Set<String> getAccessWideners();

/**
* {@return the paths to the access transformer files of this mod, or an empty set if absent}.
*
* @param platform the platform to run the query on
*/
Set<String> getAccessTransformers(ModPlatform platform);

/**
* {@return the injected interface data in this mod metadata file}.
*
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/dev/architectury/loom/metadata/ModsToml.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand All @@ -19,6 +20,7 @@

import net.fabricmc.loom.configuration.ifaceinject.InterfaceInjectionProcessor;
import net.fabricmc.loom.util.ExceptionUtil;
import net.fabricmc.loom.util.ModPlatform;

public final class ModsToml implements ModMetadataFile {
public static final String FILE_PATH = "META-INF/mods.toml";
Expand Down Expand Up @@ -72,6 +74,26 @@ public Set<String> getAccessWideners() {
return Set.of();
}

@Override
public Set<String> getAccessTransformers(ModPlatform platform) {
if (platform == ModPlatform.NEOFORGE) {
final List<? extends Config> ats = config.get("accessTransformers");

if (ats != null) {
final Set<String> result = new HashSet<>();

for (Config atEntry : ats) {
final String file = atEntry.get("file");
if (file != null) result.add(file);
}

return result;
}
}

return Set.of();
}

@Override
public List<InterfaceInjectionProcessor.InjectedInterface> getInjectedInterfaces(@Nullable String modId) {
return List.of();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import net.fabricmc.loom.LoomGradlePlugin;
import net.fabricmc.loom.configuration.ifaceinject.InterfaceInjectionProcessor;
import net.fabricmc.loom.util.ModPlatform;
import net.fabricmc.loom.util.function.CollectionUtil;

public final class QuiltModJson implements JsonBackedModMetadataFile, SingleIdModMetadataFile {
Expand Down Expand Up @@ -84,6 +85,11 @@ public Set<String> getAccessWideners() {
}
}

@Override
public Set<String> getAccessTransformers(ModPlatform platform) {
return Set.of();
}

@Override
public List<InterfaceInjectionProcessor.InjectedInterface> getInjectedInterfaces(@Nullable String modId) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package dev.architectury.loom.neoforge;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Set;

import dev.architectury.at.AccessTransformSet;
import dev.architectury.at.io.AccessTransformFormats;
import dev.architectury.loom.metadata.ModMetadataFile;
import dev.architectury.loom.metadata.ModMetadataFiles;

import net.fabricmc.loom.util.Constants;
import net.fabricmc.loom.util.FileSystemUtil;
import net.fabricmc.loom.util.ModPlatform;
import net.fabricmc.mappingio.tree.MappingTreeView;

public final class NeoForgeModDependencies {
public static void remapAts(Path jar, MappingTreeView mappings, String from, String to) throws IOException {
final ModMetadataFile modMetadata = ModMetadataFiles.fromJar(jar);
Set<String> atPaths = Set.of(Constants.Forge.ACCESS_TRANSFORMER_PATH);

if (modMetadata != null) {
final Set<String> modsTomlAts = modMetadata.getAccessTransformers(ModPlatform.NEOFORGE);

if (!modsTomlAts.isEmpty()) {
atPaths = modsTomlAts;
}
}

try (FileSystemUtil.Delegate fs = FileSystemUtil.getJarFileSystem(jar)) {
for (String atPathStr : atPaths) {
final Path atPath = fs.getPath(atPathStr);

if (Files.exists(atPath)) {
AccessTransformSet ats = AccessTransformFormats.FML.read(atPath);
ats = ats.remap(mappings, from, to);
AccessTransformFormats.FML.write(atPath, ats);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@

import com.google.common.hash.Hashing;
import com.google.common.io.MoreFiles;
import dev.architectury.at.AccessTransformSet;
import dev.architectury.at.io.AccessTransformFormats;
import dev.architectury.loom.util.TempFiles;
import org.cadixdev.at.AccessTransformSet;
import org.cadixdev.at.io.AccessTransformFormats;
import org.gradle.api.Project;
import org.gradle.api.file.FileCollection;
import org.gradle.api.logging.Logger;
Expand All @@ -61,7 +61,6 @@
import net.fabricmc.loom.util.ForgeToolExecutor;
import net.fabricmc.loom.util.LoomVersions;
import net.fabricmc.loom.util.fmj.FabricModJson;
import net.fabricmc.lorenztiny.TinyMappingsReader;

public class AccessTransformerJarProcessor implements MinecraftJarProcessor<AccessTransformerJarProcessor.Spec> {
private static final Logger LOGGER = Logging.getLogger(AccessTransformerJarProcessor.class);
Expand Down Expand Up @@ -139,7 +138,7 @@ private Path mergeAndRemapAccessTransformers(ProcessorContext context, List<Acce
}
}

accessTransformSet = accessTransformSet.remap(new TinyMappingsReader(context.getMappings(), IntermediaryNamespaces.intermediary(project), MappingsNamespace.NAMED.toString()).read());
accessTransformSet = accessTransformSet.remap(context.getMappings(), IntermediaryNamespaces.intermediary(project), MappingsNamespace.NAMED.toString());

final Path accessTransformerPath = tempFiles.file("accesstransformer-merged", ".cfg");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

import com.google.common.base.Stopwatch;
import com.google.gson.JsonObject;
import dev.architectury.loom.neoforge.NeoForgeModDependencies;
import dev.architectury.loom.util.MappingOption;
import dev.architectury.tinyremapper.InputTag;
import dev.architectury.tinyremapper.NonClassCopyMode;
Expand All @@ -67,7 +68,7 @@
import net.fabricmc.loom.util.kotlin.KotlinClasspathService;
import net.fabricmc.loom.util.kotlin.KotlinRemapperClassloader;
import net.fabricmc.loom.util.service.SharedServiceManager;
import net.fabricmc.loom.util.srg.AtRemapper;
import net.fabricmc.loom.util.srg.AtClassRemapper;
import net.fabricmc.loom.util.srg.CoreModClassRemapper;
import net.fabricmc.mappingio.tree.MemoryMappingTree;

Expand Down Expand Up @@ -271,7 +272,14 @@ private void remapJars(List<ModDependency> remapList) throws IOException {
remapJarManifestEntries(output);

if (extension.isForgeLike()) {
AtRemapper.remap(project, output, mappings);
if (extension.isNeoForge()) {
// NeoForge: Fully map ATs
NeoForgeModDependencies.remapAts(output, mappings, fromM, toM);
} else {
// Forge: only map class names, the rest are mapped srg -> named at runtime
AtClassRemapper.remap(project, output, mappings);
}

CoreModClassRemapper.remapJar(project, extension.getPlatform().get(), output, mappings);
}

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/net/fabricmc/loom/util/aw2at/Aw2At.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2021 FabricMC
* Copyright (c) 2021-2023 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
Expand Down Expand Up @@ -29,10 +29,10 @@
import java.io.IOException;
import java.nio.file.Path;

import org.cadixdev.at.AccessChange;
import org.cadixdev.at.AccessTransform;
import org.cadixdev.at.AccessTransformSet;
import org.cadixdev.at.ModifierChange;
import dev.architectury.at.AccessChange;
import dev.architectury.at.AccessTransform;
import dev.architectury.at.AccessTransformSet;
import dev.architectury.at.ModifierChange;
import org.cadixdev.bombe.type.signature.MethodSignature;
import org.gradle.api.Project;
import org.gradle.api.tasks.SourceSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
*
* @author Juuz
*/
public final class AtRemapper {
public final class AtClassRemapper {
public static void remap(Project project, Path jar, MappingTree mappings) throws IOException {
final Logger logger = project.getLogger();
final String sourceNamespace = IntermediaryNamespaces.intermediary(project);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2022 FabricMC
* Copyright (c) 2022-2023 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
Expand All @@ -24,8 +24,8 @@

package net.fabricmc.loom.test.unit.forge

import org.cadixdev.at.AccessChange
import org.cadixdev.at.ModifierChange
import dev.architectury.at.AccessChange
import dev.architectury.at.ModifierChange
import spock.lang.Specification

import net.fabricmc.accesswidener.AccessWidenerReader
Expand Down

0 comments on commit 45bb684

Please sign in to comment.