Downgrade transformers if running on java 8

This commit is contained in:
RaphiMC 2024-09-24 19:25:13 +02:00
parent ebd051e2e4
commit 8cbf58f91f
No known key found for this signature in database
GPG Key ID: 0F6BB0657A03AC94
2 changed files with 59 additions and 0 deletions

View File

@ -0,0 +1,57 @@
/*
* This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
* Copyright (C) 2021-2024 RK_01/RaphiMC and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.raphimc.viaproxy.injection;
import net.lenni0451.classtransform.transformer.IAnnotationHandlerPreprocessor;
import net.lenni0451.classtransform.utils.ASMUtils;
import net.lenni0451.classtransform.utils.Sneaky;
import org.objectweb.asm.tree.ClassNode;
import xyz.wagyourtail.jvmdg.runtime.ClassDowngradingAgent;
import java.lang.instrument.IllegalClassFormatException;
public class TransformerDowngrader implements IAnnotationHandlerPreprocessor {
private final ClassLoader classLoader;
private final ClassDowngradingAgent downgrader = new ClassDowngradingAgent();
public TransformerDowngrader(final ClassLoader classLoader) {
this.classLoader = classLoader;
}
@Override
public void process(final ClassNode node) {
}
@Override
public ClassNode replace(final ClassNode node) {
final byte[] bytes = ASMUtils.toStacklessBytes(node);
try {
final byte[] transformed = this.downgrader.transform(this.classLoader, node.name, null, null, bytes);
if (transformed != null) {
return ASMUtils.fromBytes(transformed);
} else {
return node;
}
} catch (IllegalClassFormatException e) {
Sneaky.sneakyThrow(e);
return null;
}
}
}

View File

@ -25,6 +25,7 @@ import net.lenni0451.classtransform.utils.loader.InjectionClassLoader;
import net.lenni0451.classtransform.utils.tree.IClassProvider;
import net.lenni0451.reflect.stream.RStream;
import net.raphimc.viaproxy.ViaProxy;
import net.raphimc.viaproxy.injection.TransformerDowngrader;
import net.raphimc.viaproxy.util.logging.Logger;
import org.objectweb.asm.Opcodes;
import org.yaml.snakeyaml.Yaml;
@ -104,6 +105,7 @@ public class PluginManager {
if (nativeClassVersion < Opcodes.V17) {
System.setProperty(Constants.ALLOW_MAVEN_LOOKUP, "false");
transformerManager.addClassFileTransformer(classLoader, new ClassDowngradingAgent());
transformerManager.addTransformerPreprocessor(new TransformerDowngrader(classLoader));
}
} catch (Throwable e) {
Logger.LOGGER.error("Failed to setup class downgrading", e);