Inject class. Experimenting.

This commit is contained in:
Adrian Bergqvist 2023-02-08 16:47:04 +01:00
parent a48f2ae309
commit 4be2b3760a
No known key found for this signature in database
GPG Key ID: FAE7D8EDE225E686
6 changed files with 181 additions and 1 deletions

View File

@ -4,4 +4,14 @@ plugins {
}
group = "org.adde0109"
version = "1.1.7-alpha-bungeecord"
version = "1.1.7-alpha-bungeecord"
repositories {
mavenCentral()
maven { url = uri("https://oss.sonatype.org/content/repositories/snapshots") } // This lets gradle find the BungeeCord files online
}
dependencies {
compileOnly("net.md-5:bungeecord-api:1.19-R0.1-SNAPSHOT")
implementation("org.javassist:javassist:3.29.2-GA")
}

View File

@ -0,0 +1,15 @@
package org.adde0109.ambassador;
import net.md_5.bungee.api.plugin.Plugin;
import org.adde0109.ambassador.forgeCommandArgument.Injector;
public class AmbassadorPlugin extends Plugin {
@Override
public void onEnable() {
// You should not put an enable message in your plugin.
// BungeeCord already does so
getLogger().info("Yay! It loads!");
Injector.inject();
}
}

View File

@ -0,0 +1,39 @@
/*
* Copyright (C) 2018 Velocity 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 <https://www.gnu.org/licenses/>.
*/
package org.adde0109.ambassador.forgeCommandArgument;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
public class EnumArgumentProperty {
private final String className;
public EnumArgumentProperty(String className) {
this.className = className;
}
public String getClassName() {
return this.className;
}
public String parse(StringReader reader) throws CommandSyntaxException {
return reader.readUnquotedString();
}
}

View File

@ -0,0 +1,60 @@
/*
* Copyright (C) 2018 Velocity 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 <https://www.gnu.org/licenses/>.
*/
package org.adde0109.ambassador.forgeCommandArgument;
import io.netty.buffer.ByteBuf;
import net.md_5.bungee.protocol.DefinedPacket;
import javax.annotation.Nullable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
/**
* An argument property serializer that will serialize and deserialize nothing.
*/
public class EnumArgumentPropertySerializer {
public static final EnumArgumentPropertySerializer ENUM;
static {
try {
ENUM = new EnumArgumentPropertySerializer();
} catch (InvocationTargetException | InstantiationException | IllegalAccessException | ClassNotFoundException |
NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
EnumArgumentPropertySerializer() throws InvocationTargetException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException {
Constructor constructor = EnumArgumentPropertySerializer.class.getSuperclass().getDeclaredConstructor(Class.forName("net.md_5.bungee.protocol.packet.Commands$1"));
constructor.setAccessible(true);
constructor.newInstance(this);
}
protected Object read(ByteBuf buf) {
return new EnumArgumentProperty(DefinedPacket.readString(buf));
}
protected void write(ByteBuf buf, Object t) {
DefinedPacket.writeString(((EnumArgumentProperty) t).getClassName(),buf);
}
}

View File

@ -0,0 +1,52 @@
package org.adde0109.ambassador.forgeCommandArgument;
import javassist.*;
import javassist.bytecode.ClassFile;
import javassist.expr.MethodCall;
import net.md_5.bungee.protocol.Protocol;
import net.md_5.bungee.protocol.packet.Commands;
import org.adde0109.ambassador.forgeCommandArgument.EnumArgumentPropertySerializer;
import org.checkerframework.checker.units.qual.C;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
public class Injector{
static public void inject() {
try {
Class<?> argumentRegistryClass = Class.forName("net.md_5.bungee.protocol.packet.Commands$ArgumentRegistry");
Class<?> networkNodeClass = Class.forName("net.md_5.bungee.protocol.packet.Commands$NetworkNode");
Class<?> argumentSerializerClass = Class.forName("net.md_5.bungee.protocol.packet.Commands$ArgumentRegistry$ArgumentSerializer");
ClassPool.getDefault().insertClassPath(new ClassClassPath(EnumArgumentPropertySerializer.class));
ClassPool.getDefault().insertClassPath(new ClassClassPath(EnumArgumentProperty.class));
CtClass cc = ClassPool.getDefault().get("org.adde0109.ambassador.forgeCommandArgument.EnumArgumentPropertySerializer");
CtClass scc = ClassPool.getDefault().get("net.md_5.bungee.protocol.packet.Commands$ArgumentRegistry$ArgumentSerializer");
CtClass newcc = scc.getDeclaringClass().makeNestedClass("EnumArgumentPropertySerializer",true);
newcc.addMethod(new CtMethod(cc.getDeclaredMethod("read"), newcc, null));
newcc.addMethod(new CtMethod(cc.getDeclaredMethod("write"), newcc, null));
newcc.setSuperclass(scc);
//CtConstructor ctconstructor = new CtConstructor(cc.getDeclaredConstructor(null),newcc,null);
//newcc.addConstructor(ctconstructor);
Class<?> loaded= newcc.toClass(argumentSerializerClass);
Method method = argumentRegistryClass.getDeclaredMethod("register",String.class,argumentSerializerClass);
//Constructor<?> superconstructor = argumentSerializerClass.getDeclaredConstructors()[0];
//method.setAccessible(true);
//Constructor<?> constructor = loaded.getDeclaredConstructor();
//constructor.setAccessible(true);
//method.invoke(null,"forge:enum", constructor.newInstance());
CtClass dcc = scc.getDeclaringClass();
dcc.getClassInitializer().insertAfter("$class.getDeclaredMethod(\"register\",String.class,Class.forName(\"net.md_5.bungee.protocol.packet.Commands$ArgumentRegistry$ArgumentSerializer\"));");
dcc.toClass(networkNodeClass);
MethodCall
} catch (ReflectiveOperationException | NotFoundException | CannotCompileException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,4 @@
name: Ambassador
main: org.adde0109.ambassador.AmbassadorPlugin
version: 0.0.0-Bungeecord
author: adde0109