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"
+ """
<style>
html {
background-color: #2F2F2F;
color: white;
}
table {
border-collapse: collapse;
width: 1880px;
max-width: 100%;
width: 100%;
margin: auto;
font-family: monospace;
}
tr:nth-child(2n) {
background-color: #eeeeee;
background-color: #373737;
}
tr:hover {
background-color: lightgray;
background-color: #555;
}
tr > *:first-child {
text-align: right;
padding-right: .5em;
width: 6em;
}
tr > *:not(:first-child) {
font-family: monospace;
}
td {
padding-top: 0;
@ -176,21 +176,36 @@ public class NMSReflect {
th {
text-align: left;
font-size: 1.1em;
border-top: solid 1px black;
border-top: solid 1px white;
}
a, a:visited {
color: #222;
color: #ddd;
}
.kw {
color: #CC6C1D;
}
.cl {
color: #1290C3;
}
.mtd {
color: #1EB540;
}
.fld {
color: #8DDAF8;
}
</style>
"""
+ "</head><body>\n"
+ "<h1>" + title + "</h1>\n"
+ "<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()) {
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 classSimpleName = classToPrint.substring(classToPrint.lastIndexOf('.') + 1);
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;
}
@ -306,27 +321,31 @@ public class NMSReflect {
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));
methodsByObf.values().forEach(m -> m.printHTML(out));
}
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);
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);
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) {
Class<?> superClass = runtimeClass().getSuperclass();
if (superClass == null)
if (superClass == null || superClass.equals(Object.class) || superClass.equals(Enum.class) || superClass.equals(Record.class))
return null;
ClassMapping cm = (IS_SERVER_OBFUSCATED ? CLASSES_BY_OBF : CLASSES_BY_MOJ).get(superClass.getName());
return (cm != null) ? cm.toType(obf) : Type.of(superClass);
@ -345,15 +364,15 @@ public class NMSReflect {
private String classKind() {
Class<?> clazz = runtimeClass();
if (clazz.isEnum())
return "Enum";
return "enum";
if (clazz.isAnnotation())
return "Annotation";
return "annotation";
if (clazz.isInterface())
return "Interface";
return "interface";
if (clazz.isRecord())
return "Record";
return "record";
if (clazz.isPrimitive())
return "Primitive";
return "primitive";
return "Class";
}
}
@ -376,7 +395,7 @@ public class NMSReflect {
private String toHTML(boolean isObfClass) {
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;
}
@ -395,7 +414,7 @@ public class NMSReflect {
if (identifier instanceof MethodId mId)
identifierHTML = mId.toHTML(isObfClass);
else if (identifier instanceof String n)
identifierHTML = "<b>" + n + "</b>";
identifierHTML = "<b class='fld'>" + n + "</b>";
return returnType.toHTML(isObfClass) + " " + identifierHTML;
}
@ -435,6 +454,7 @@ public class NMSReflect {
private String type;
/* package */ MemberDesc<I> obfDesc, mojDesc;
private MemberMapping(String type, MemberDesc<I> obfDesc, MemberDesc<I> mojDesc) {
this.type = type;
this.obfDesc = obfDesc;
this.mojDesc = mojDesc;
}

View File

@ -112,10 +112,10 @@ public class Type implements Comparable<Type> {
String classSimpleName = classToPrint.substring(classToPrint.lastIndexOf('.') + 1);
String htmlTitle = classSimpleName.equals(classToPrint) ? "" : (" title='" + classToPrint + "'");
if (!htmlTitle.equals("")) {
typeHTML = "<span" + htmlTitle + ">" + classSimpleName + "</span>";
typeHTML = "<span" + htmlTitle + " class='cl'>" + classSimpleName + "</span>";
}
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;
}
}