From e198a6640a4b953023442d7ec0092af339d3a61a Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Fri, 18 Feb 2022 19:12:01 +0100 Subject: [PATCH] Improved HTML rendering of obf mapping --- .../lib/paper/reflect/NMSReflect.java | 72 ++++++++++++------- .../fr/pandacube/lib/paper/reflect/Type.java | 11 ++- 2 files changed, 55 insertions(+), 28 deletions(-) diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/NMSReflect.java b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/NMSReflect.java index c0d4e88..094af51 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/NMSReflect.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/NMSReflect.java @@ -149,25 +149,25 @@ public class NMSReflect { + "" + title + "\n" + """ """ + "\n" + "

" + title + "

\n" + ""); - out.println(""); + out.println(""); for (ClassMapping clazz : CLASSES_BY_OBF.values()) { clazz.printHTML(out); } - out.println("
Namespace" + OBF_NAMESPACE + "" + MOJ_NAMESPACE + "
ns" + OBF_NAMESPACE + "" + MOJ_NAMESPACE + "
"); + out.println("

Generated by marcbal" + + " using this tool" + + " running on " + Bukkit.getName() + " version " + Bukkit.getVersion() + "

" + + ""); } @@ -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 = "" + classSimpleName + ""; + String typeHTML = "" + classSimpleName + ""; return typeHTML; } @@ -306,27 +321,31 @@ public class NMSReflect { private void printHTML(PrintStream out) { - out.println("" + classKind() + "" + nameToHTML(true) + "" + nameToHTML(false) + ""); + out.println("" + classKind() + "" + nameToHTML(true) + "" + nameToHTML(false) + ""); 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 + ".") : "") + "" + classSimpleName + ""; Type superClass = superClass(obf); - String superClassHTML = superClass == null ? "" : (" extends " + superClass.toHTML(obf)); + String superClassHTML = superClass == null ? "" : (" extends " + superClass.toHTML(obf)); List superInterfaces = superInterfaces(obf); String superInterfacesHTML = superInterfaces.isEmpty() ? "" - : (" implements " + superInterfaces.stream().map(t -> t.toHTML(obf)).collect(Collectors.joining(", "))); + : (" implements " + 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 = "" + name + "(" + paramsHTML + ")"; + String identifierHTML = "" + name + "(" + 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 = "" + n + ""; + identifierHTML = "" + n + ""; return returnType.toHTML(isObfClass) + " " + identifierHTML; } @@ -435,6 +454,7 @@ public class NMSReflect { private String type; /* package */ MemberDesc obfDesc, mojDesc; private MemberMapping(String type, MemberDesc obfDesc, MemberDesc mojDesc) { + this.type = type; this.obfDesc = obfDesc; this.mojDesc = mojDesc; } diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/Type.java b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/Type.java index e9e5625..24fd1da 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/Type.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/Type.java @@ -112,10 +112,10 @@ public class Type implements Comparable { String classSimpleName = classToPrint.substring(classToPrint.lastIndexOf('.') + 1); String htmlTitle = classSimpleName.equals(classToPrint) ? "" : (" title='" + classToPrint + "'"); if (!htmlTitle.equals("")) { - typeHTML = "" + classSimpleName + ""; + typeHTML = "" + classSimpleName + ""; } else { - typeHTML = classSimpleName; + typeHTML = "" + classSimpleName + ""; } } @@ -128,6 +128,13 @@ public class Type implements Comparable { } + public boolean isPrimitive() { + try { + return toClass().isPrimitive(); + } catch (ClassNotFoundException e) { + return false; + } + }