Skip to content

Commit

Permalink
Fix Proguard mapping generator
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoPangxie732 committed Dec 7, 2024
1 parent c6439cf commit 62267b9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,25 +88,24 @@ public static String java2Descriptor(@NotNull String javaName) {
}
}

// NOTE: Not strictly FIELD_DESC_PATTERN: this allows void/V
public static String descriptor2Java(@NotNull @Pattern(Constants.FIELD_DESC_PATTERN) String descriptor) {
if (descriptor.isBlank()) return "";
char c0 = descriptor.charAt(0);
if (c0 != '[') {
if (c0 == 'L') return asJavaName(descriptor.substring(1, descriptor.length() - 1));
else return primitive2Java(c0);
} else {
int dim = 1;
while (descriptor.charAt(dim++) == '[');
String s = "[]".repeat(--dim);
return switch (descriptor.charAt(dim)) {
case 'L' -> asJavaName(descriptor.substring(dim + 1, descriptor.length() - 1)) + s;
case 'V' -> DescriptorUtil.throwInvalid(false);
default -> primitive2Java(descriptor.charAt(dim));
} + s;
}
return switch (c0) {
case 'L' -> asJavaName(descriptor.substring(1, descriptor.length() - 1));
case '[' -> {
int dim = 0;
while (descriptor.charAt(++dim) == '[');
char c1 = descriptor.charAt(dim);
yield (c1 == 'L' ? asJavaName(descriptor.substring(dim + 1, descriptor.length() - 1)) :
primitive2Java(c1)) + "[]".repeat(dim);
}
default -> primitive2Java(c0);
};
}

public static String primitive2Java(@NotNull char desc) {
public static String primitive2Java(char desc) {
return switch (desc) {
case 'Z' -> "boolean";
case 'B' -> "byte";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,25 @@ public ObjectList<String> generate(ClassifiedMapping<PairedMapping> mappings, Cl
PairedMapping mapping = cls.mapping;
lines.add(NamingUtil.asJavaName(mapping.mappedName) + " -> " +
NamingUtil.asJavaName(mapping.unmappedName) + ':');
cls.getFields().parallelStream().forEach(field -> {
for (PairedMapping field : cls.getFields()) {
MappingUtil.checkOwner(field.getOwned(), cls);
String mappedDesc;
if (field.hasComponent(Descriptor.Mapped.class)) {
mappedDesc = field.getComponent(Descriptor.Mapped.class).mappedDescriptor;
} else if (remapper != null && field.hasComponent(Descriptor.class)) {
mappedDesc = remapper.mapDesc(field.getComponent(Descriptor.class).unmappedDescriptor);
} else throw new UnsupportedOperationException();
synchronized (lines) {
lines.add(" " + NamingUtil.descriptor2Java(mappedDesc) + ' ' + field.mappedName +
" -> " + field.unmappedName);
}
});
cls.getMethods().parallelStream().forEach(method -> {
lines.add(" " + NamingUtil.descriptor2Java(mappedDesc) + ' ' + field.mappedName +
" -> " + field.unmappedName);
}
for (PairedMapping method : cls.getMethods()) {
MappingUtil.checkOwner(method.getOwned(), cls);
String mappedDesc;
if (method.hasComponent(Descriptor.Mapped.class)) {
mappedDesc = method.getComponent(Descriptor.Mapped.class).mappedDescriptor;
} else if (remapper != null && method.hasComponent(Descriptor.class)) {
mappedDesc = remapper.mapMethodDesc(method.getComponent(Descriptor.class).unmappedDescriptor);
} else throw new UnsupportedOperationException();
// String args = String.join(",", Utils.mapArray(Type.getArgumentTypes(mappedDesc),
// String[]::new, Type::getClassName));
StringBuilder args = new StringBuilder(mappedDesc.length());
int end = mappedDesc.lastIndexOf(')'), last = 1;
for (int i = 1; i < end; i++) {
Expand All @@ -79,18 +75,16 @@ public ObjectList<String> generate(ClassifiedMapping<PairedMapping> mappings, Cl
args.append(NamingUtil.descriptor2Java(mappedDesc.substring(last, last = i + 1))).append(',');
}
}
args.deleteCharAt(args.length() - 1);
if (end > 1) args.deleteCharAt(args.length() - 1);
String ret = NamingUtil.descriptor2Java(mappedDesc.substring(end + 1));
if (method.hasComponent(LineNumber.class)) {
LineNumber lineNumber = method.getComponent(LineNumber.class);
synchronized (lines) {
lines.add(" " + lineNumber.startLineNumber + ':' + lineNumber.endLineNumber + ':' +
ret + ' ' + method.mappedName + '(' + args + ") -> " + method.unmappedName);
}
} else synchronized (lines) {
lines.add(" " + lineNumber.startLineNumber + ':' + lineNumber.endLineNumber + ':' +
ret + ' ' + method.mappedName + '(' + args + ") -> " + method.unmappedName);
} else {
lines.add(" " + ret + ' ' + method.mappedName + '(' + args + ") -> " + method.unmappedName);
}
});
}
}
return lines;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

exports cn.maxpixel.mcdecompiler.mapping.detector;

uses DetectionUnit;
provides DetectionUnit with DefaultDetectionUnit;
uses cn.maxpixel.mcdecompiler.mapping.detector.DetectionUnit;
provides cn.maxpixel.mcdecompiler.mapping.detector.DetectionUnit with cn.maxpixel.mcdecompiler.mapping.detector.DefaultDetectionUnit;
}

0 comments on commit 62267b9

Please sign in to comment.