Improved HTML rendering of obf mapping

This commit is contained in:
Marc Baloup 2022-02-18 19:12:01 +01:00
parent 419c6a6028
commit e198a6640a
2 changed files with 55 additions and 28 deletions

View File

@ -149,25 +149,25 @@ public class NMSReflect {
+ "<title>" + title + "</title>\n" + "<title>" + title + "</title>\n"
+ """ + """
<style> <style>
html {
background-color: #2F2F2F;
color: white;
}
table { table {
border-collapse: collapse; border-collapse: collapse;
width: 1880px; width: 100%;
max-width: 100%;
margin: auto; margin: auto;
font-family: monospace;
} }
tr:nth-child(2n) { tr:nth-child(2n) {
background-color: #eeeeee; background-color: #373737;
} }
tr:hover { tr:hover {
background-color: lightgray; background-color: #555;
} }
tr > *:first-child { tr > *:first-child {
text-align: right; text-align: right;
padding-right: .5em; padding-right: .5em;
width: 6em;
}
tr > *:not(:first-child) {
font-family: monospace;
} }
td { td {
padding-top: 0; padding-top: 0;
@ -176,21 +176,36 @@ public class NMSReflect {
th { th {
text-align: left; text-align: left;
font-size: 1.1em; font-size: 1.1em;
border-top: solid 1px black; border-top: solid 1px white;
} }
a, a:visited { a, a:visited {
color: #222; color: #ddd;
}
.kw {
color: #CC6C1D;
}
.cl {
color: #1290C3;
}
.mtd {
color: #1EB540;
}
.fld {
color: #8DDAF8;
} }
</style> </style>
""" """
+ "</head><body>\n" + "</head><body>\n"
+ "<h1>" + title + "</h1>\n" + "<h1>" + title + "</h1>\n"
+ "<table>"); + "<table>");
out.println("<tr><th>Namespace</th><th>" + OBF_NAMESPACE + "</th><th>" + MOJ_NAMESPACE + "</th></tr>"); out.println("<tr><th>ns</th><th>" + OBF_NAMESPACE + "</th><th>" + MOJ_NAMESPACE + "</th></tr>");
for (ClassMapping clazz : CLASSES_BY_OBF.values()) { for (ClassMapping clazz : CLASSES_BY_OBF.values()) {
clazz.printHTML(out); clazz.printHTML(out);
} }
out.println("</table></body></html>"); out.println("</table><p>Generated by <a href='https://github.com/marcbal'>marcbal</a>"
+ " using <a href='https://github.com/PandacubeFr/PandaLib/blob/master/Paper/src/main/java/fr/pandacube/lib/paper/reflect/NMSReflect.java'>this tool</a>"
+ " running on <a href='https://papermc.io/'>" + Bukkit.getName() + "</a> version " + Bukkit.getVersion() + "</p>"
+ "</body></html>");
} }
@ -292,7 +307,7 @@ public class NMSReflect {
String classToPrint = isObfClass ? obfName : mojName; String classToPrint = isObfClass ? obfName : mojName;
String classSimpleName = classToPrint.substring(classToPrint.lastIndexOf('.') + 1); String classSimpleName = classToPrint.substring(classToPrint.lastIndexOf('.') + 1);
String htmlTitle = classSimpleName.equals(classToPrint) ? "" : (" title='" + classToPrint + "'"); String htmlTitle = classSimpleName.equals(classToPrint) ? "" : (" title='" + classToPrint + "'");
String typeHTML = "<a href='#c" + id + "'" + htmlTitle + ">" + classSimpleName + "</a>"; String typeHTML = "<a href='#c" + id + "'" + htmlTitle + " class='cl'>" + classSimpleName + "</a>";
return typeHTML; return typeHTML;
} }
@ -306,27 +321,31 @@ public class NMSReflect {
private void printHTML(PrintStream out) { private void printHTML(PrintStream out) {
out.println("<tr id='c" + id + "'><th>" + classKind() + "</th><th>" + nameToHTML(true) + "</th><th>" + nameToHTML(false) + "</th></tr>"); out.println("<tr id='c" + id + "'><th class='kw'>" + classKind() + "</th><th>" + nameToHTML(true) + "</th><th>" + nameToHTML(false) + "</th></tr>");
fieldsByObf.values().forEach(f -> f.printHTML(out)); fieldsByObf.values().forEach(f -> f.printHTML(out));
methodsByObf.values().forEach(m -> m.printHTML(out)); methodsByObf.values().forEach(m -> m.printHTML(out));
} }
private String nameToHTML(boolean obf) { private String nameToHTML(boolean obf) {
String name = obf ? obfName : mojName; String classToPrint = obf ? obfName : mojName;
int packageSep = classToPrint.lastIndexOf('.');
String classSimpleName = classToPrint.substring(packageSep + 1);
String classPackages = classToPrint.substring(0, packageSep > 0 ? packageSep : 0);
String classHTML = (packageSep >= 0 ? (classPackages + ".") : "") + "<b class='cl'>" + classSimpleName + "</b>";
Type superClass = superClass(obf); Type superClass = superClass(obf);
String superClassHTML = superClass == null ? "" : (" extends " + superClass.toHTML(obf)); String superClassHTML = superClass == null ? "" : (" <span class='kw'>extends</span> " + superClass.toHTML(obf));
List<Type> superInterfaces = superInterfaces(obf); List<Type> superInterfaces = superInterfaces(obf);
String superInterfacesHTML = superInterfaces.isEmpty() ? "" String superInterfacesHTML = superInterfaces.isEmpty() ? ""
: (" implements " + superInterfaces.stream().map(t -> t.toHTML(obf)).collect(Collectors.joining(", "))); : (" <span class='kw'>implements</span> " + superInterfaces.stream().map(t -> t.toHTML(obf)).collect(Collectors.joining(", ")));
return name + superClassHTML + superInterfacesHTML; return classHTML + superClassHTML + superInterfacesHTML;
} }
private Type superClass(boolean obf) { private Type superClass(boolean obf) {
Class<?> superClass = runtimeClass().getSuperclass(); Class<?> superClass = runtimeClass().getSuperclass();
if (superClass == null) if (superClass == null || superClass.equals(Object.class) || superClass.equals(Enum.class) || superClass.equals(Record.class))
return null; return null;
ClassMapping cm = (IS_SERVER_OBFUSCATED ? CLASSES_BY_OBF : CLASSES_BY_MOJ).get(superClass.getName()); ClassMapping cm = (IS_SERVER_OBFUSCATED ? CLASSES_BY_OBF : CLASSES_BY_MOJ).get(superClass.getName());
return (cm != null) ? cm.toType(obf) : Type.of(superClass); return (cm != null) ? cm.toType(obf) : Type.of(superClass);
@ -345,15 +364,15 @@ public class NMSReflect {
private String classKind() { private String classKind() {
Class<?> clazz = runtimeClass(); Class<?> clazz = runtimeClass();
if (clazz.isEnum()) if (clazz.isEnum())
return "Enum"; return "enum";
if (clazz.isAnnotation()) if (clazz.isAnnotation())
return "Annotation"; return "annotation";
if (clazz.isInterface()) if (clazz.isInterface())
return "Interface"; return "interface";
if (clazz.isRecord()) if (clazz.isRecord())
return "Record"; return "record";
if (clazz.isPrimitive()) if (clazz.isPrimitive())
return "Primitive"; return "primitive";
return "Class"; return "Class";
} }
} }
@ -376,7 +395,7 @@ public class NMSReflect {
private String toHTML(boolean isObfClass) { private String toHTML(boolean isObfClass) {
String paramsHTML = parametersType.stream().map(p -> p.toHTML(isObfClass)).collect(Collectors.joining(", ")); String paramsHTML = parametersType.stream().map(p -> p.toHTML(isObfClass)).collect(Collectors.joining(", "));
String identifierHTML = "<b>" + name + "</b>(" + paramsHTML + ")"; String identifierHTML = "<b class='mtd'>" + name + "</b>(" + paramsHTML + ")";
return identifierHTML; return identifierHTML;
} }
@ -395,7 +414,7 @@ public class NMSReflect {
if (identifier instanceof MethodId mId) if (identifier instanceof MethodId mId)
identifierHTML = mId.toHTML(isObfClass); identifierHTML = mId.toHTML(isObfClass);
else if (identifier instanceof String n) else if (identifier instanceof String n)
identifierHTML = "<b>" + n + "</b>"; identifierHTML = "<b class='fld'>" + n + "</b>";
return returnType.toHTML(isObfClass) + " " + identifierHTML; return returnType.toHTML(isObfClass) + " " + identifierHTML;
} }
@ -435,6 +454,7 @@ public class NMSReflect {
private String type; private String type;
/* package */ MemberDesc<I> obfDesc, mojDesc; /* package */ MemberDesc<I> obfDesc, mojDesc;
private MemberMapping(String type, MemberDesc<I> obfDesc, MemberDesc<I> mojDesc) { private MemberMapping(String type, MemberDesc<I> obfDesc, MemberDesc<I> mojDesc) {
this.type = type;
this.obfDesc = obfDesc; this.obfDesc = obfDesc;
this.mojDesc = mojDesc; this.mojDesc = mojDesc;
} }

View File

@ -112,10 +112,10 @@ public class Type implements Comparable<Type> {
String classSimpleName = classToPrint.substring(classToPrint.lastIndexOf('.') + 1); String classSimpleName = classToPrint.substring(classToPrint.lastIndexOf('.') + 1);
String htmlTitle = classSimpleName.equals(classToPrint) ? "" : (" title='" + classToPrint + "'"); String htmlTitle = classSimpleName.equals(classToPrint) ? "" : (" title='" + classToPrint + "'");
if (!htmlTitle.equals("")) { if (!htmlTitle.equals("")) {
typeHTML = "<span" + htmlTitle + ">" + classSimpleName + "</span>"; typeHTML = "<span" + htmlTitle + " class='cl'>" + classSimpleName + "</span>";
} }
else { else {
typeHTML = classSimpleName; typeHTML = "<span class='" + (isPrimitive() ? "kw" : "cl") + "'>" + classSimpleName + "</span>";
} }
} }
@ -128,6 +128,13 @@ public class Type implements Comparable<Type> {
} }
public boolean isPrimitive() {
try {
return toClass().isPrimitive();
} catch (ClassNotFoundException e) {
return false;
}
}