From 92222b3c9dcf7462006fddf79d3ab9395a8ff1a3 Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Sat, 14 Aug 2021 03:06:17 -0700 Subject: [PATCH] [ci skip] Add some helper methods to the `ObfHelper` util class (#6374) --- ...ktraces-in-log-messages-crash-report.patch | 70 ++++++++++++++++--- ...nd-timings-for-sensors-and-behaviors.patch | 30 +++----- 2 files changed, 70 insertions(+), 30 deletions(-) diff --git a/patches/server/Deobfuscate-stacktraces-in-log-messages-crash-report.patch b/patches/server/Deobfuscate-stacktraces-in-log-messages-crash-report.patch index 9c1f9938d3..f3a6cf1569 100644 --- a/patches/server/Deobfuscate-stacktraces-in-log-messages-crash-report.patch +++ b/patches/server/Deobfuscate-stacktraces-in-log-messages-crash-report.patch @@ -167,12 +167,14 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + +import com.google.common.base.Charsets; +import com.google.common.collect.ImmutableMap; ++import com.google.common.collect.ImmutableSet; +import com.mojang.datafixers.util.Pair; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.Map; ++import java.util.Set; +import net.fabricmc.mapping.tree.ClassDef; +import net.fabricmc.mapping.tree.MethodDef; +import net.fabricmc.mapping.tree.TinyMappingFactory; @@ -188,23 +190,75 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + public static final String MOJANG_PLUS_YARN_NAMESPACE = "mojang+yarn"; + public static final String SPIGOT_NAMESPACE = "spigot"; + -+ private final @Nullable Map mappings; ++ private final @Nullable Map mappingsByObfName; ++ private final @Nullable Map mappingsByMojangName; + + ObfHelper() { -+ this.mappings = loadMappingsIfPresent(); ++ final @Nullable Set maps = loadMappingsIfPresent(); ++ if (maps != null) { ++ this.mappingsByObfName = maps.stream().collect(ImmutableMap.toImmutableMap(ClassMapping::obfName, map -> map)); ++ this.mappingsByMojangName = maps.stream().collect(ImmutableMap.toImmutableMap(ClassMapping::mojangName, map -> map)); ++ } else { ++ this.mappingsByObfName = null; ++ this.mappingsByMojangName = null; ++ } + } + -+ public @Nullable Map mappings() { -+ return this.mappings; ++ public @Nullable Map mappingsByObfName() { ++ return this.mappingsByObfName; + } + -+ private static @Nullable Map loadMappingsIfPresent() { ++ public @Nullable Map mappingsByMojangName() { ++ return this.mappingsByMojangName; ++ } ++ ++ /** ++ * Attempts to get the obf name for a given class by its Mojang name. Will ++ * return the input string if mappings are not present. ++ * ++ * @param fullyQualifiedMojangName fully qualified class name (dotted) ++ * @return mapped or original fully qualified (dotted) class name ++ */ ++ public String reobfClassName(final String fullyQualifiedMojangName) { ++ if (this.mappingsByMojangName == null) { ++ return fullyQualifiedMojangName; ++ } ++ ++ final ClassMapping map = this.mappingsByMojangName.get(fullyQualifiedMojangName); ++ if (map == null) { ++ return fullyQualifiedMojangName; ++ } ++ ++ return map.obfName(); ++ } ++ ++ /** ++ * Attempts to get the Mojang name for a given class by its obf name. Will ++ * return the input string if mappings are not present. ++ * ++ * @param fullyQualifiedObfName fully qualified class name (dotted) ++ * @return mapped or original fully qualified (dotted) class name ++ */ ++ public String deobfClassName(final String fullyQualifiedObfName) { ++ if (this.mappingsByObfName == null) { ++ return fullyQualifiedObfName; ++ } ++ ++ final ClassMapping map = this.mappingsByObfName.get(fullyQualifiedObfName); ++ if (map == null) { ++ return fullyQualifiedObfName; ++ } ++ ++ return map.mojangName(); ++ } ++ ++ private static @Nullable Set loadMappingsIfPresent() { + try (final @Nullable InputStream mappingsInputStream = StacktraceDeobfuscator.class.getClassLoader().getResourceAsStream("META-INF/mappings/reobf.tiny")) { + if (mappingsInputStream == null) { + return null; + } + final TinyTree tree = TinyMappingFactory.loadWithDetection(new BufferedReader(new InputStreamReader(mappingsInputStream, Charsets.UTF_8))); -+ final var builder = ImmutableMap.builder(); ++ final var builder = ImmutableSet.builder(); + + for (final ClassDef classDef : tree.getClasses()) { + final String obfClassName = classDef.getName(SPIGOT_NAMESPACE).replace('/', '.'); @@ -227,7 +281,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + classDef.getName(MOJANG_PLUS_YARN_NAMESPACE).replace('/', '.'), + methodMappings.build() + ); -+ builder.put(map.obfName(), map); ++ builder.add(map); + } + + return builder.build(); @@ -307,7 +361,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + return traceElements; + } + -+ final @Nullable Map mappings = ObfHelper.INSTANCE.mappings(); ++ final @Nullable Map mappings = ObfHelper.INSTANCE.mappingsByObfName(); + if (mappings == null || traceElements.length == 0) { + return traceElements; + } diff --git a/patches/server/Rate-options-and-timings-for-sensors-and-behaviors.patch b/patches/server/Rate-options-and-timings-for-sensors-and-behaviors.patch index 692336710d..7faeedc2e5 100644 --- a/patches/server/Rate-options-and-timings-for-sensors-and-behaviors.patch +++ b/patches/server/Rate-options-and-timings-for-sensors-and-behaviors.patch @@ -130,17 +130,10 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 this.maxDuration = maxRunTime; this.entryCondition = requiredMemoryState; + // Paper start - configurable behavior tick rate and timings -+ String key = this.getClass().getSimpleName(); -+ final var mappings = io.papermc.paper.util.ObfHelper.INSTANCE.mappings(); -+ if (mappings != null) { -+ final var classMapping = mappings.get(this.getClass().getName()); -+ if (classMapping != null) { -+ key = classMapping.mojangName(); -+ int lastSeparator = key.lastIndexOf('.'); -+ if (lastSeparator != -1) { -+ key = key.substring(lastSeparator + 1); -+ } -+ } ++ String key = io.papermc.paper.util.ObfHelper.INSTANCE.deobfClassName(this.getClass().getName()); ++ int lastSeparator = key.lastIndexOf('.'); ++ if (lastSeparator != -1) { ++ key = key.substring(lastSeparator + 1); + } + this.configKey = key.toLowerCase(java.util.Locale.ROOT); + this.timing = co.aikar.timings.MinecraftTimings.getBehaviorTimings(configKey); @@ -197,17 +190,10 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 public Sensor(int senseInterval) { + // Paper start - configurable sensor tick rate and timings -+ String key = this.getClass().getSimpleName(); -+ final var mappings = io.papermc.paper.util.ObfHelper.INSTANCE.mappings(); -+ if (mappings != null) { -+ final var classMapping = mappings.get(this.getClass().getName()); -+ if (classMapping != null) { -+ key = classMapping.mojangName(); -+ int lastSeparator = key.lastIndexOf('.'); -+ if (lastSeparator != -1) { -+ key = key.substring(lastSeparator + 1); -+ } -+ } ++ String key = io.papermc.paper.util.ObfHelper.INSTANCE.deobfClassName(this.getClass().getName()); ++ int lastSeparator = key.lastIndexOf('.'); ++ if (lastSeparator != -1) { ++ key = key.substring(lastSeparator + 1); + } + this.configKey = key.toLowerCase(java.util.Locale.ROOT); + this.timing = co.aikar.timings.MinecraftTimings.getSensorTimings(configKey, senseInterval);