Remap reflection calls in plugins using internals

Co-authored-by: Jason Penilla <11360596+jpenilla@users.noreply.github.com>
This commit is contained in:
Nassim Jahnke
2022-10-30 23:47:26 +01:00
parent 4cd66b0305
commit fae673b95c
12 changed files with 518 additions and 28 deletions

View File

@@ -131,36 +131,26 @@ public class Commodore {
}
// Paper start - Plugin rewrites
private static final Map<String, String> SEARCH_AND_REMOVE = initReplacementsMap();
private static Map<String, String> initReplacementsMap() {
Map<String, String> getAndRemove = new HashMap<>();
// Be wary of maven shade's relocations
final java.util.jar.Manifest manifest = io.papermc.paper.util.JarManifests.manifest(Commodore.class);
if (Boolean.getBoolean( "debug.rewriteForIde") && manifest != null)
{
// unversion incoming calls for pre-relocate debug work
final String NMS_REVISION_PACKAGE = "v" + manifest.getMainAttributes().getValue("CraftBukkit-Package-Version") + "/";
getAndRemove.put("org/bukkit/".concat("craftbukkit/" + NMS_REVISION_PACKAGE), NMS_REVISION_PACKAGE);
private static final String CB_PACKAGE_PREFIX = "org/bukkit/".concat("craftbukkit/");
private static final String LEGACY_CB_PACKAGE_PREFIX = CB_PACKAGE_PREFIX + io.papermc.paper.util.MappingEnvironment.LEGACY_CB_VERSION + "/";
private static String runtimeCbPkgPrefix() {
if (io.papermc.paper.util.MappingEnvironment.reobf()) {
return LEGACY_CB_PACKAGE_PREFIX;
}
return getAndRemove;
return CB_PACKAGE_PREFIX;
}
@Nonnull
private static String getOriginalOrRewrite(@Nonnull String original)
{
String rewrite = null;
for ( Map.Entry<String, String> entry : SEARCH_AND_REMOVE.entrySet() )
{
if ( original.contains( entry.getKey() ) )
{
rewrite = original.replace( entry.getValue(), "" );
// Relocation is applied in reobf, and when mappings are present they handle the relocation
if (!io.papermc.paper.util.MappingEnvironment.reobf() && !io.papermc.paper.util.MappingEnvironment.hasMappings()) {
if (original.contains(LEGACY_CB_PACKAGE_PREFIX)) {
original = original.replace(LEGACY_CB_PACKAGE_PREFIX, CB_PACKAGE_PREFIX);
}
}
return rewrite != null ? rewrite : original;
return original;
}
// Paper end - Plugin rewrites
@@ -245,6 +235,7 @@ public class Commodore {
visitor = new LimitedClassRemapper(cw, new SimpleRemapper(Commodore.ENUM_RENAMES));
}
visitor = io.papermc.paper.pluginremap.reflect.ReflectionRemapper.visitor(visitor); // Paper
cr.accept(new ClassRemapper(new ClassVisitor(Opcodes.ASM9, visitor) {
final Set<RerouteMethodData> rerouteMethodData = new HashSet<>();
String className;

View File

@@ -75,6 +75,7 @@ import org.bukkit.potion.PotionType;
@SuppressWarnings("deprecation")
public final class CraftMagicNumbers implements UnsafeValues {
public static final CraftMagicNumbers INSTANCE = new CraftMagicNumbers();
public static final boolean DISABLE_OLD_API_SUPPORT = Boolean.getBoolean("paper.disableOldApiSupport"); // Paper
private final Commodore commodore = new Commodore();
@@ -347,7 +348,7 @@ public final class CraftMagicNumbers implements UnsafeValues {
throw new InvalidPluginException("Plugin API version " + pdf.getAPIVersion() + " is lower than the minimum allowed version. Please update or replace it.");
}
if (toCheck.isOlderThan(ApiVersion.FLATTENING)) {
if (!DISABLE_OLD_API_SUPPORT && toCheck.isOlderThan(ApiVersion.FLATTENING)) { // Paper
CraftLegacy.init();
}
@@ -362,6 +363,12 @@ public final class CraftMagicNumbers implements UnsafeValues {
@Override
public byte[] processClass(PluginDescriptionFile pdf, String path, byte[] clazz) {
// Paper start
if (DISABLE_OLD_API_SUPPORT) {
// Make sure we still go through our reflection rewriting if needed
return io.papermc.paper.pluginremap.reflect.ReflectionRemapper.processClass(clazz);
}
// Paper end
try {
clazz = this.commodore.convert(clazz, pdf.getName(), ApiVersion.getOrCreateVersion(pdf.getAPIVersion()), ((CraftServer) Bukkit.getServer()).activeCompatibilities);
} catch (Exception ex) {