Skip to content

Commit

Permalink
Move ReobfHelper to core
Browse files Browse the repository at this point in the history
  • Loading branch information
Brokkonaut committed Dec 4, 2021
1 parent df5f4e3 commit 6ae3f56
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
18 changes: 17 additions & 1 deletion nmsutils-core/pom.xml
Expand Up @@ -14,14 +14,30 @@
<version>1.16.3-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.fabricmc</groupId>
<artifactId>mapping-io</artifactId>
<version>0.3.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
</repository>
<repository>
<id>fabric-repo</id>
<url>https://maven.fabricmc.net/</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>11</release>
<release>17</release>
</configuration>
</plugin>
</plugins>
Expand Down
@@ -1,6 +1,5 @@
package de.cubeside.nmsutils.v1_18_R1;
package de.cubeside.nmsutils.util;

import io.papermc.paper.util.ObfHelper;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand All @@ -17,26 +16,23 @@
import net.fabricmc.mappingio.tree.MappingTree;
import net.fabricmc.mappingio.tree.MappingTree.FieldMapping;
import net.fabricmc.mappingio.tree.MemoryMappingTree;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.bukkit.Bukkit;

/**
* Modified from https://github.com/PaperMC/Paper/blob/675d1e3f58ab86d5a3b1bc8bbcf2beefc9696078/patches/server/0420-Deobfuscate-stacktraces-in-log-messages-crash-report.patch
* see there for the license
*/
@DefaultQualifier(NonNull.class)
public enum ReobfHelper {
INSTANCE;

public static final String MOJANG_PLUS_YARN_NAMESPACE = "mojang+yarn";
public static final String SPIGOT_NAMESPACE = "spigot";

private final @Nullable Map<String, ClassMapping> mappingsByObfName;
private final @Nullable Map<String, ClassMapping> mappingsByMojangName;
private final Map<String, ClassMapping> mappingsByObfName;
private final Map<String, ClassMapping> mappingsByMojangName;

ReobfHelper() {
final @Nullable Set<ClassMapping> maps = loadMappingsIfPresent();
final Set<ClassMapping> maps = loadMappingsIfPresent();
if (maps != null) {
this.mappingsByObfName = maps.stream().collect(Collectors.toUnmodifiableMap(ClassMapping::obfName, map -> map));
this.mappingsByMojangName = maps.stream().collect(Collectors.toUnmodifiableMap(ClassMapping::mojangName, map -> map));
Expand All @@ -46,11 +42,11 @@ public enum ReobfHelper {
}
}

public @Nullable Map<String, ClassMapping> mappingsByObfName() {
public Map<String, ClassMapping> mappingsByObfName() {
return this.mappingsByObfName;
}

public @Nullable Map<String, ClassMapping> mappingsByMojangName() {
public Map<String, ClassMapping> mappingsByMojangName() {
return this.mappingsByMojangName;
}

Expand Down Expand Up @@ -96,8 +92,8 @@ public String deobfClassName(final String fullyQualifiedObfName) {
return map.mojangName();
}

private static @Nullable Set<ClassMapping> loadMappingsIfPresent() {
try (final @Nullable InputStream mappingsInputStream = ObfHelper.class.getClassLoader().getResourceAsStream("META-INF/mappings/reobf.tiny")) {
private static Set<ClassMapping> loadMappingsIfPresent() {
try (final InputStream mappingsInputStream = Bukkit.class.getClassLoader().getResourceAsStream("META-INF/mappings/reobf.tiny")) {
if (mappingsInputStream == null) {
return null;
}
Expand Down Expand Up @@ -175,9 +171,7 @@ public static String getObfuscatedFieldName(Class<?> clazz, String field) {
public static Field getFieldByMojangName(Class<?> clazz, String fieldName) {
try {
Field field = clazz.getField(getObfuscatedFieldName(clazz, fieldName));
if (field != null) {
field.setAccessible(true);
}
field.setAccessible(true);
return field;
} catch (NoSuchFieldException | SecurityException e) {
throw new RuntimeException(e);
Expand Down
Expand Up @@ -3,6 +3,7 @@
import com.destroystokyo.paper.entity.ai.VanillaGoal;
import de.cubeside.nmsutils.EntityUtils;
import de.cubeside.nmsutils.NMSUtils;
import de.cubeside.nmsutils.util.ReobfHelper;
import java.lang.reflect.Field;
import java.util.function.Function;
import java.util.logging.Level;
Expand Down

0 comments on commit 6ae3f56

Please sign in to comment.