Skip to content

Commit

Permalink
Improve mappings propagation to be incremental for each remapping ope…
Browse files Browse the repository at this point in the history
…ration
  • Loading branch information
thecatcore committed Jun 1, 2024
1 parent 926e80a commit 2e4e042
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 18 deletions.
19 changes: 16 additions & 3 deletions src/main/java/fr/catcore/modremapperapi/remapping/RemapUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public static void init(List<io.github.fabriccompatibiltylayers.modremappingapi.

MINECRAFT_TREE = MappingsUtilsImpl.getMinecraftMappings();

writeMcMappings();

LOADER_TREE = generateMappings();
MappingsUtilsImpl.addMappingsToContext(LOADER_TREE);

Expand Down Expand Up @@ -138,6 +140,8 @@ public static void remapMods(Map<Path, Path> pathMap) {
Constants.MAIN_LOGGER.debug("Remapper created!");
remapFiles(remapper, pathMap);
Constants.MAIN_LOGGER.debug("Jar remapping done!");

MappingsUtilsImpl.writeFullMappings();
}

public static List<String> makeModMappings(Path modPath) {
Expand Down Expand Up @@ -197,6 +201,15 @@ public static void generateModMappings() {
MappingsUtilsImpl.addMappingsToContext(MODS_TREE);
}

public static void writeMcMappings() {
try {
MappingWriter writer = MappingWriter.create(Constants.MC_MAPPINGS_FILE.toPath(), MappingFormat.TINY_2_FILE);
MINECRAFT_TREE.accept(writer);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static List<String> generateFolderMappings(File[] files) {
List<String> list = new ArrayList<>();

Expand Down Expand Up @@ -573,11 +586,11 @@ private static void remapFiles(TinyRemapper remapper, Map<Path, Path> paths) {
List<OutputConsumerPath.ResourceRemapper> resourceRemappers = new ArrayList<>(NonClassCopyMode.FIX_META_INF.remappers);
resourceRemappers.add(new RefmapRemapper());

applyRemapper(remapper, paths, outputConsumerPaths, resourceRemappers, true);
applyRemapper(remapper, paths, outputConsumerPaths, resourceRemappers, true, MappingsUtilsImpl.getSourceNamespace(), MappingsUtils.getTargetNamespace());
}

@ApiStatus.Internal
public static void applyRemapper(TinyRemapper remapper, Map<Path, Path> paths, List<OutputConsumerPath> outputConsumerPaths, List<OutputConsumerPath.ResourceRemapper> resourceRemappers, boolean analyzeMapping) {
public static void applyRemapper(TinyRemapper remapper, Map<Path, Path> paths, List<OutputConsumerPath> outputConsumerPaths, List<OutputConsumerPath.ResourceRemapper> resourceRemappers, boolean analyzeMapping, String srcNamespace, String targetNamespace) {
try {
Map<Path, InputTag> tagMap = new HashMap<>();

Expand All @@ -604,7 +617,7 @@ public static void applyRemapper(TinyRemapper remapper, Map<Path, Path> paths, L
Constants.MAIN_LOGGER.debug("Done 1!");
}

if (analyzeMapping) MappingsUtilsImpl.completeMappingsFromTr(remapper.getEnvironment());
if (analyzeMapping) MappingsUtilsImpl.completeMappingsFromTr(remapper.getEnvironment(), srcNamespace);
} catch (Exception e) {
remapper.finish();
outputConsumerPaths.forEach(o -> {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/fr/catcore/modremapperapi/utils/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class Constants {
);
public static final File EXTRA_MAPPINGS_FILE = new File(VERSIONED_FOLDER, "extra_mappings.tiny");
public static final File REMAPPED_MAPPINGS_FILE = new File(VERSIONED_FOLDER, "remapped_mappings.tiny");
public static final File MC_MAPPINGS_FILE = new File(VERSIONED_FOLDER, "mc_mappings.tiny");
public static final File FULL_MAPPINGS_FILE = new File(VERSIONED_FOLDER, "full_mappings.tiny");

public static final File LIB_FOLDER = new File(VERSIONED_FOLDER, "libs");
public static final Logger MAIN_LOGGER = Logger.get("ModRemappingAPI");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private static Path[] getMinecraftJar(List<Path> sourcePaths, String src, String

for (Path path :
originalClassPath) {
Constants.MAIN_LOGGER.info(path.toString());
Constants.MAIN_LOGGER.debug(path.toString());
paths.put(path, new File(
new File(Constants.LIB_FOLDER, target),
path.toFile().getName()).toPath()
Expand All @@ -98,7 +98,9 @@ private static Path[] getMinecraftJar(List<Path> sourcePaths, String src, String

List<OutputConsumerPath.ResourceRemapper> resourceRemappers = new ArrayList<>(NonClassCopyMode.FIX_META_INF.remappers);

RemapUtil.applyRemapper(remapper, paths, outputConsumerPaths, resourceRemappers, false);
RemapUtil.applyRemapper(remapper, paths, outputConsumerPaths, resourceRemappers, true, src, target);

Constants.MAIN_LOGGER.info("MC jar remapped successfully!");

return paths.values().toArray(new Path[0]);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.fabriccompatibiltylayers.modremappingapi.impl;

import fr.catcore.modremapperapi.utils.Constants;
import fr.catcore.wfvaio.WhichFabricVariantAmIOn;
import io.github.fabriccompatibiltylayers.modremappingapi.api.MappingUtils;
import io.github.fabriccompatibiltylayers.modremappingapi.impl.utils.MappingTreeHelper;
Expand All @@ -10,6 +11,7 @@
import net.fabricmc.mappingio.MappedElementKind;
import net.fabricmc.mappingio.MappingReader;
import net.fabricmc.mappingio.MappingVisitor;
import net.fabricmc.mappingio.MappingWriter;
import net.fabricmc.mappingio.adapter.MappingDstNsReorder;
import net.fabricmc.mappingio.adapter.MappingNsRenamer;
import net.fabricmc.mappingio.adapter.MappingSourceNsSwitch;
Expand Down Expand Up @@ -240,9 +242,9 @@ public static void addMappingsToContext(MappingTree mappingTreeView) {
}
}

public static void completeMappingsFromTr(TrEnvironment trEnvironment) {
int srcNamespace = FULL_MAPPINGS.getNamespaceId(getSourceNamespace());
int targetNamespace = FULL_MAPPINGS.getNamespaceId(getTargetNamespace());
public static void completeMappingsFromTr(TrEnvironment trEnvironment, String src) {
int srcNamespace = FULL_MAPPINGS.getNamespaceId(src);
int trueSrcNamespace = FULL_MAPPINGS.getNamespaceId(FULL_MAPPINGS.getSrcNamespace());

Map<ExtendedClassMember, List<String>> classMembers = new HashMap<>();

Expand All @@ -256,7 +258,12 @@ public static void completeMappingsFromTr(TrEnvironment trEnvironment) {
List<String> children = trClass.getChildren().stream().map(TrClass::getName).collect(Collectors.toList());

for (MappingTree.MethodMapping methodMapping : classMapping.getMethods()) {
TrMethod method = trClass.getMethod(methodMapping.getName(srcNamespace), methodMapping.getDesc(srcNamespace));
String methodName = methodMapping.getName(srcNamespace);
String methodDesc = methodMapping.getDesc(srcNamespace);

if (methodName == null || methodDesc == null) continue;

TrMethod method = trClass.getMethod(methodName, methodDesc);

if (method != null && method.isVirtual()) {
classMembers.put(new ExtendedClassMember(
Expand All @@ -270,45 +277,102 @@ public static void completeMappingsFromTr(TrEnvironment trEnvironment) {

int propagated = 0;

try {
FULL_MAPPINGS.visitHeader();
FULL_MAPPINGS.visitNamespaces(FULL_MAPPINGS.getSrcNamespace(), FULL_MAPPINGS.getDstNamespaces());
FULL_MAPPINGS.visitContent();
} catch (IOException e) {
throw new RuntimeException(e);
}

for (Map.Entry<ExtendedClassMember, List<String>> entry : classMembers.entrySet()) {
ExtendedClassMember member = entry.getKey();

for (String child : entry.getValue()) {

TrClass trClass = trEnvironment.getClass(child);
if (trClass == null) continue;

try {
FULL_MAPPINGS.visitClass(child);
if (srcNamespace == trueSrcNamespace) {
FULL_MAPPINGS.visitClass(child);
} else {
FULL_MAPPINGS.visitClass(FULL_MAPPINGS.mapClassName(child, srcNamespace, trueSrcNamespace));
}
} catch (IOException e) {
e.printStackTrace();
}

MappingTree.ClassMapping classMapping = FULL_MAPPINGS.getClass(child);
MappingTree.ClassMapping classMapping = FULL_MAPPINGS.getClass(child, srcNamespace);

if (classMapping == null) continue;

TrMethod trMethod = trClass.getMethod(member.name, member.desc);
if (trMethod == null) continue;

if (classMapping.getMethod(member.name, member.desc, srcNamespace) != null) continue;

try {
FULL_MAPPINGS.visitMethod(member.name, member.desc);
if (srcNamespace == trueSrcNamespace) {
FULL_MAPPINGS.visitMethod(member.name, member.desc);
} else {
MappingTree.MemberMapping memberMapping = FULL_MAPPINGS.getMethod(member.owner, member.name, member.desc, srcNamespace);
if (memberMapping == null) continue;

FULL_MAPPINGS.visitMethod(memberMapping.getSrcName(), memberMapping.getSrcDesc());

FULL_MAPPINGS.visitDstName(MappedElementKind.METHOD, srcNamespace, member.name);
FULL_MAPPINGS.visitDstDesc(MappedElementKind.METHOD, srcNamespace, member.desc);
}

MappingTree.MethodMapping methodMapping = FULL_MAPPINGS.getMethod(member.owner, member.name, member.desc, srcNamespace);
if (methodMapping == null) continue;

FULL_MAPPINGS.visitDstName(MappedElementKind.METHOD, targetNamespace, methodMapping.getName(targetNamespace));
FULL_MAPPINGS.visitDstDesc(MappedElementKind.METHOD, targetNamespace, methodMapping.getDesc(targetNamespace));
MappingTree.MethodMapping newMethodMapping = classMapping.getMethod(member.name, member.desc, srcNamespace);

boolean actualPropagated = false;

propagated++;
for (String namespace : FULL_MAPPINGS.getDstNamespaces()) {
int targetNamespace = FULL_MAPPINGS.getNamespaceId(namespace);

if (targetNamespace == srcNamespace) continue;

if (newMethodMapping.getName(targetNamespace) == null) {
String targetName = methodMapping.getName(targetNamespace);

if (targetName != null) {
FULL_MAPPINGS.visitDstName(MappedElementKind.METHOD, targetNamespace, targetName);
actualPropagated = true;
}
}

if (newMethodMapping.getDesc(targetNamespace) == null) {
String targetDesc = methodMapping.getDesc(targetNamespace);

if (targetDesc != null) {
FULL_MAPPINGS.visitDstDesc(MappedElementKind.METHOD, targetNamespace, targetDesc);
actualPropagated = true;
}
}
}

if (actualPropagated) propagated++;
} catch (IOException e) {
e.printStackTrace();
}
}
}

System.out.println("Propagated: " + propagated + " methods");
Constants.MAIN_LOGGER.info("Propagated: " + propagated + " methods from namespace " + src);
}



public static void writeFullMappings() {
try {
MappingWriter writer = MappingWriter.create(Constants.FULL_MAPPINGS_FILE.toPath(), MappingFormat.TINY_2_FILE);
FULL_MAPPINGS.accept(writer);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static void gatherChildClassCandidates(TrEnvironment trEnvironment, Map<ExtendedClassMember, List<String>> classMembers) {
Expand Down

0 comments on commit 2e4e042

Please sign in to comment.