Skip to content

Commit

Permalink
Fix backward compatibility for Babric environment
Browse files Browse the repository at this point in the history
  • Loading branch information
thecatcore committed Apr 26, 2024
1 parent 07039b8 commit 8d900d5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.util.List;

public class MappingBuilder {
private static final boolean BABRIC = WhichFabricVariantAmIOn.getVariant() == FabricVariants.BABRIC || WhichFabricVariantAmIOn.getVariant() == FabricVariants.BABRIC_NEW_FORMAT;

private final String obfucated;
private final String intermediary;
private final List<Entry> entries = new ArrayList<>();
Expand Down Expand Up @@ -69,8 +71,8 @@ public List<String> build() {
}

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

for (Entry entry : this.entries) {
entry.accept(visitor);
Expand Down Expand Up @@ -99,10 +101,10 @@ public String toString(String className) {
}

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

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

Expand Down
18 changes: 17 additions & 1 deletion src/main/java/fr/catcore/modremapperapi/remapping/RemapUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
import fr.catcore.modremapperapi.utils.Constants;
import fr.catcore.modremapperapi.utils.FileUtils;
import fr.catcore.modremapperapi.utils.MappingsUtils;
import fr.catcore.wfvaio.FabricVariants;
import fr.catcore.wfvaio.WhichFabricVariantAmIOn;
import io.github.fabriccompatibiltylayers.modremappingapi.api.MappingUtils;
import io.github.fabriccompatibiltylayers.modremappingapi.impl.MappingsUtilsImpl;
import io.github.fabriccompatibiltylayers.modremappingapi.impl.VisitorInfosImpl;
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.adapter.MappingSourceNsSwitch;
import net.fabricmc.mappingio.format.MappingFormat;
import net.fabricmc.mappingio.tree.MappingTree;
import net.fabricmc.mappingio.tree.MemoryMappingTree;
Expand All @@ -32,6 +35,7 @@
import java.util.zip.ZipInputStream;

public class RemapUtil {
private static final boolean BABRIC = WhichFabricVariantAmIOn.getVariant() == FabricVariants.BABRIC || WhichFabricVariantAmIOn.getVariant() == FabricVariants.BABRIC_NEW_FORMAT;
private static MappingTree LOADER_TREE;
private static MappingTree MINECRAFT_TREE;
private static MappingTree MODS_TREE;
Expand Down Expand Up @@ -226,7 +230,11 @@ private static MappingTree generateMappings() {
MemoryMappingTree mappingTree = new MemoryMappingTree();

try {
MappingsUtilsImpl.initializeMappingTree(mappingTree);
if (BABRIC) {
MappingsUtilsImpl.initializeMappingTree(mappingTree, "intermediary", "official");
} else {
MappingsUtilsImpl.initializeMappingTree(mappingTree);
}

MappingList mappingList = new MappingList();

Expand All @@ -240,6 +248,14 @@ private static MappingTree generateMappings() {

MappingWriter mappingWriter = MappingWriter.create(Constants.EXTRA_MAPPINGS_FILE.toPath(), MappingFormat.TINY_2_FILE);
mappingTree.accept(mappingWriter);

if (Objects.equals(mappingTree.getSrcNamespace(), "intermediary")) {
MemoryMappingTree newTree = new MemoryMappingTree();
MappingVisitor visitor = new MappingSourceNsSwitch(newTree, "official");

mappingTree.accept(visitor);
mappingTree = newTree;
}
} catch (IOException e) {
throw new RuntimeException("Error while generating remappers mappings", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ private static void adaptVanillaMappings() {
VANILLA_MAPPINGS.accept(visitor);
} catch (IOException e) {
e.printStackTrace();
// throw new RuntimeException(e);
}
}

Expand Down Expand Up @@ -182,16 +181,21 @@ public static IMappingProvider createProvider(MappingTree mappings, String from,

@ApiStatus.Internal
public static void initializeMappingTree(MappingVisitor mappingVisitor) throws IOException {
initializeMappingTree(mappingVisitor, "official", "intermediary");
}

@ApiStatus.Internal
public static void initializeMappingTree(MappingVisitor mappingVisitor, String src, String target) throws IOException {
mappingVisitor.visitHeader();

List<String> namespaces = new ArrayList<>();
namespaces.add("intermediary");
namespaces.add(src);

if (getMinecraftMappings().getDstNamespaces().contains("named")) {
namespaces.add("named");
}

mappingVisitor.visitNamespaces("official", namespaces);
mappingVisitor.visitNamespaces(target, namespaces);
}

@ApiStatus.Internal
Expand Down

0 comments on commit 8d900d5

Please sign in to comment.