Skip to content

Commit

Permalink
fix checkstyle
Browse files Browse the repository at this point in the history
  • Loading branch information
Jab125 committed Jul 9, 2024
1 parent f7f6338 commit 9054d29
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/main/java/dev/architectury/loom/util/ForgeSrgToMojangUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import net.fabricmc.mappingio.tree.MappingTree;
import net.fabricmc.mappingio.tree.MemoryMappingTree;
import net.fabricmc.tinyremapper.ClassInstance;
Expand All @@ -13,18 +14,19 @@

// FIXME: There has to be a better way to fix the mappings
public final class ForgeSrgToMojangUtil {
private ForgeSrgToMojangUtil() {

}
private ForgeSrgToMojangUtil() { }

@SuppressWarnings("unchecked")
public static void replaceSrgWithMojangMappings(TinyRemapper remapper, MemoryMappingTree mappings) {
Map<String, ClassInstance> readClasses;

try {
Field readClassesField = TinyRemapper.class.getDeclaredField("readClasses");
readClassesField.setAccessible(true);
readClasses = (Map<String, ClassInstance>) readClassesField.get(remapper);
} catch (Throwable t) {throw new RuntimeException(t);}
} catch (Throwable t) {
throw new RuntimeException(t);
}

for (ClassInstance value : readClasses.values()) {
replaceSrgWithMojangMappings(value, mappings);
Expand All @@ -36,42 +38,53 @@ public static void replaceSrgWithMojangMappings(ClassInstance value, MemoryMappi
int srg = tree.getNamespaceId("srg");
int mojang = tree.getNamespaceId("mojang");
HashMap<String, MemberInstance> members;

try {
Field membersField = ClassInstance.class.getDeclaredField("members");
membersField.setAccessible(true);
members = (HashMap<String, MemberInstance>) membersField.get(value);
} catch (Throwable t) {
throw new RuntimeException(t);
}

Map<String, MemberInstance> copy = (Map<String, MemberInstance>) members.clone();
members.clear();

for (Map.Entry<String, MemberInstance> entry : copy.entrySet()) {
TrMember.MemberType type = entry.getValue().getType();
MemberInstance memberInstance = entry.getValue();
String name;

if (type == TrMember.MemberType.FIELD) {
MappingTree.FieldMapping field = tree.getField(value.getName(), memberInstance.getName(), memberInstance.getDesc(), srg);

if (field == null) {
field = tree.getField(value.getName(), memberInstance.getName(), null, srg);

if (field == null) {
members.put(memberInstance.getId(), memberInstance);
continue;
}
}

name = field.getName(mojang);
} else if (type == TrMember.MemberType.METHOD) {
MappingTree.MethodMapping method = tree.getMethod(value.getName(), memberInstance.getName(), memberInstance.getDesc(), srg);

if (method == null) {
method = tree.getMethod(value.getName(), memberInstance.getName(), null, srg);

if (method == null) {
members.put(memberInstance.getId(), memberInstance);
continue;
}
}

name = method.getName(mojang);
} else {
throw new AssertionError();
}

MemberInstance instance = of(type, value, name, memberInstance.getDesc(), memberInstance.getAccess(), memberInstance.getIndex());
members.put(instance.getId(), instance);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ private void remapJars(List<ModDependency> remapList) throws IOException {
final TinyRemapper remapper = builder.build();

remapper.readClassPath(extension.getMinecraftJars(IntermediaryNamespaces.intermediaryNamespace(project)).toArray(Path[]::new));

if (extension.isForge() && extension.getForgeProvider().usesMojangAtRuntime()) {
ForgeSrgToMojangUtil.replaceSrgWithMojangMappings(remapper, mappings);
}
Expand Down

0 comments on commit 9054d29

Please sign in to comment.