Skip to content

Commit

Permalink
Use mapping io more internally
Browse files Browse the repository at this point in the history
  • Loading branch information
thecatcore committed Apr 16, 2024
1 parent f35ff9e commit e20f88f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package fr.catcore.modremapperapi.remapping;

import fr.catcore.modremapperapi.ModRemappingAPI;
import net.fabricmc.mappingio.MappedElementKind;
import net.fabricmc.mappingio.MappingVisitor;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -65,6 +68,15 @@ public List<String> build() {
return list;
}

public void accept(MappingVisitor visitor) throws IOException {
visitor.visitClass(obfucated);
visitor.visitDstName(MappedElementKind.CLASS, 0, this.intermediary);

for (Entry entry : this.entries) {
entry.accept(visitor);
}
}

public static class Entry {
private final String obfuscated;
private final String intermediary;
Expand All @@ -85,6 +97,13 @@ public String toString(String className) {
return MappingBuilder.toString(this.type.name(), className, this.description, this.obfuscated, this.intermediary, this.intermediary);
}
}

public void accept(MappingVisitor visitor) throws IOException {
if (type == Type.FIELD) visitor.visitField(obfuscated, description);
else visitor.visitMethod(obfuscated, description);

visitor.visitDstName(type == Type.FIELD ? MappedElementKind.FIELD : MappedElementKind.METHOD, 0, intermediary);
}
}

public enum Type {
Expand Down
80 changes: 44 additions & 36 deletions src/main/java/fr/catcore/modremapperapi/remapping/RemapUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import fr.catcore.modremapperapi.utils.MappingsUtils;
import net.fabricmc.api.EnvType;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.mappingio.MappingVisitor;
import net.fabricmc.mappingio.MappingWriter;
import net.fabricmc.mappingio.format.MappingFormat;
import net.fabricmc.mappingio.tree.MappingTree;
import net.fabricmc.mappingio.tree.MemoryMappingTree;
import net.fabricmc.tinyremapper.*;
Expand All @@ -25,15 +28,14 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import static fr.catcore.modremapperapi.utils.MappingsUtils.getNativeNamespace;
import static fr.catcore.modremapperapi.utils.MappingsUtils.getTargetNamespace;

public class RemapUtil {
private static MappingTree LOADER_TREE;
private static MappingTree MINECRAFT_TREE;
private static MappingTree MODS_TREE;

private static final Map<String, String> MOD_MAPPINGS = new HashMap<>();
private static final MappingList MOD_MAPPINGS = new MappingList();

protected static final Map<String, List<String>> MIXINED = new HashMap<>();

Expand All @@ -43,7 +45,6 @@ public class RemapUtil {

public static void init() {
downloadRemappingLibs();
generateMappings();

for (ModRemapper remapper : ModRemappingAPI.MOD_REMAPPERS) {
Optional<String> pkg = remapper.getDefaultPackage();
Expand All @@ -54,7 +55,7 @@ public static void init() {
}
}

LOADER_TREE = makeTree(Constants.EXTRA_MAPPINGS_FILE);
LOADER_TREE = generateMappings();
MINECRAFT_TREE = MappingsUtils.getMinecraftMappings();

for (MappingTree.ClassMapping classView : MINECRAFT_TREE.getClasses()) {
Expand Down Expand Up @@ -155,32 +156,31 @@ public static List<String> makeModMappings(Path modPath) {
}
}

classes.forEach(cl -> MOD_MAPPINGS.put(cl, (cl.contains("/") ? "" : defaultPackage) + cl));
classes.forEach(cl -> MOD_MAPPINGS.add(cl, (cl.contains("/") ? "" : defaultPackage) + cl));

return files;
}

public static void generateModMappings() {
if (Constants.REMAPPED_MAPPINGS_FILE.exists()) {
Constants.REMAPPED_MAPPINGS_FILE.delete();
}
MemoryMappingTree mappingTree = new MemoryMappingTree();

MappingList mappings = new MappingList();
MOD_MAPPINGS.forEach(mappings::add);
try {
mappingTree.visitHeader();
List<String> namespaces = new ArrayList<>();
namespaces.add("target");
mappingTree.visitNamespaces("source", namespaces);

List<String> lines = new ArrayList<>();
MOD_MAPPINGS.accept(mappingTree);

if (ModRemappingAPI.BABRIC) {
lines.add(toString("v1", "intermediary", "glue", "server", "client", "named"));
} else {
lines.add(toString("v1", "official", "intermediary", "named"));
}
mappingTree.visitEnd();

mappings.forEach(mappingBuilder -> lines.addAll(mappingBuilder.build()));

FileUtils.writeTextFile(lines, Constants.REMAPPED_MAPPINGS_FILE);
MappingWriter writer = MappingWriter.create(Constants.REMAPPED_MAPPINGS_FILE.toPath(), MappingFormat.TINY_2_FILE);
mappingTree.accept(writer);
} catch (IOException e) {
throw new RuntimeException("Error while generating mods mappings", e);
}

MODS_TREE = makeTree(Constants.REMAPPED_MAPPINGS_FILE);
MODS_TREE = mappingTree;
}

private static List<String> generateFolderMappings(File[] files) {
Expand Down Expand Up @@ -216,30 +216,38 @@ public MappingBuilder add(String name) {
this.add(builder);
return builder;
}
}

private static void generateMappings() {
if (Constants.EXTRA_MAPPINGS_FILE.exists()) {
Constants.EXTRA_MAPPINGS_FILE.delete();
public void accept(MappingVisitor visitor) throws IOException {
for (MappingBuilder builder : this) builder.accept(visitor);
}
}

List<String> lines = new ArrayList<>();
private static MappingTree generateMappings() {
MemoryMappingTree mappingTree = new MemoryMappingTree();

if (ModRemappingAPI.BABRIC) {
lines.add(toString("v1", "intermediary", "glue", "server", "client", "named"));
} else {
lines.add(toString("v1", "official", "intermediary", "named"));
}
try {
mappingTree.visitHeader();
List<String> namespaces = new ArrayList<>();
namespaces.add("target");
mappingTree.visitNamespaces("source", namespaces);

MappingList mappingList = new MappingList();
MappingList mappingList = new MappingList();

for (ModRemapper remapper : ModRemappingAPI.MOD_REMAPPERS) {
remapper.getMappingList(mappingList);
}
for (ModRemapper remapper : ModRemappingAPI.MOD_REMAPPERS) {
remapper.getMappingList(mappingList);
}

mappingList.forEach(mappingBuilder -> lines.addAll(mappingBuilder.build()));
mappingList.accept(mappingTree);

mappingTree.visitEnd();

MappingWriter mappingWriter = MappingWriter.create(Constants.EXTRA_MAPPINGS_FILE.toPath(), MappingFormat.TINY_2_FILE);
mappingTree.accept(mappingWriter);
} catch (IOException e) {
throw new RuntimeException("Error while generating remappers mappings", e);
}

FileUtils.writeTextFile(lines, Constants.EXTRA_MAPPINGS_FILE);
return mappingTree;
}

/**
Expand Down

0 comments on commit e20f88f

Please sign in to comment.