Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically propose field Codec names #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/main/java/net/fabricmc/stitch/util/FieldNameFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,45 @@ public Map<EntryTriple, String> findNames(Map<String, Set<String>> allEnumFields
}
}

// For Codecs
for (Map.Entry<String, List<MethodNode>> entry : classes.entrySet()) {
String owner = entry.getKey();

for (MethodNode node : entry.getValue()) {
List<String> names = new ArrayList<>();

// Codecs should be set in class initialization
if ("<clinit>".equals(node.name)) {
for (AbstractInsnNode instruction : node.instructions) {
// Look for PUTSTATIC instructions
if (instruction instanceof FieldInsnNode
&& instruction.getOpcode() == Opcodes.PUTSTATIC) {
FieldInsnNode fieldInsnNode = (FieldInsnNode) instruction;

// Find PUTSTATIC which set to this class and use Codec as their descriptor
if (owner.equals(fieldInsnNode.owner)
&& fieldInsnNode.desc.equals("Lcom/mojang/serialization/Codec;")) {
names.add(fieldInsnNode.name);
}
}
}
}

if (names.isEmpty()) {
// Nothing was found
continue;
}

if (names.size() == 1 && !fieldNamesUsed.computeIfAbsent(owner, (s) -> new HashSet<>()).contains("CODEC")) {
// No duplicates were found
fieldNames.put(new EntryTriple(owner, names.get(0), "Lcom/mojang/serialization/Codec;"), "CODEC");
continue;
}

System.err.println("Warning: Duplicate key: CODEC in " + owner);
}
}

return fieldNames;
}

Expand Down