Skip to content

Commit

Permalink
PacketJumper completed
Browse files Browse the repository at this point in the history
  • Loading branch information
LadyCailin committed Mar 3, 2024
1 parent 6b03bd8 commit a49aee2
Show file tree
Hide file tree
Showing 51 changed files with 1,980 additions and 844 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public static void set(Class clazz, Object instance, String variableName, Object
* @param methodName The name of the method.
* @return The invocation result, null if void.
*/
public static Object invokeMethod(Class clazz, Object instance, String methodName) throws ReflectionException {
public static <T> T invokeMethod(Class clazz, Object instance, String methodName) throws ReflectionException {
return invokeMethod(clazz, instance, methodName, new Class[]{}, new Object[]{});
}

Expand All @@ -204,7 +204,7 @@ public static Object invokeMethod(Class clazz, Object instance, String methodNam
* @throws ReflectionException
*/
@SuppressWarnings({"ThrowableInstanceNotThrown", "ThrowableInstanceNeverThrown"})
public static Object invokeMethod(Object instance, String methodName, Object... params) throws ReflectionException {
public static <T> T invokeMethod(Object instance, String methodName, Object... params) throws ReflectionException {
Class c = instance.getClass();
Class[] argTypes;
{
Expand Down Expand Up @@ -237,7 +237,7 @@ public static Object invokeMethod(Object instance, String methodName, Object...
}
}
m.setAccessible(true);
return m.invoke(instance, params);
return (T) m.invoke(instance, params);
}
} catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
throw new ReflectionException(ex);
Expand All @@ -260,13 +260,13 @@ public static Object invokeMethod(Object instance, String methodName, Object...
* @throws ReflectionException
*/
@SuppressWarnings({"ThrowableInstanceNotThrown", "ThrowableInstanceNeverThrown"})
public static Object invokeMethod(Object instance, String methodName) throws ReflectionException {
public static <T> T invokeMethod(Object instance, String methodName) throws ReflectionException {
Class c = instance.getClass();
while(c != null) {
for(Method m : c.getDeclaredMethods()) {
if(methodName.equals(m.getName())) {
if(methodName.equals(m.getName()) && m.getParameterCount() == 0) {
try {
return m.invoke(instance);
return (T) m.invoke(instance);
} catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
throw new ReflectionException(ex);
}
Expand All @@ -290,12 +290,12 @@ public static Object invokeMethod(Object instance, String methodName) throws Ref
* @param args The arguments.
* @return The invocation result, null if void.
*/
public static Object invokeMethod(Class clazz, Object instance, String methodName, Class[] argTypes, Object[] args)
public static <T> T invokeMethod(Class clazz, Object instance, String methodName, Class[] argTypes, Object[] args)
throws ReflectionException {
try {
Method m = clazz.getDeclaredMethod(methodName, argTypes);
m.setAccessible(true);
return m.invoke(instance, args);
return (T) m.invoke(instance, args);
} catch(InvocationTargetException | NoSuchMethodException | IllegalArgumentException
| IllegalAccessException | SecurityException ex) {
throw new ReflectionException(ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ public boolean equals(Object obj) {
public int hashCode() {
return wb.hashCode();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.laytonsmith.core.Static;
import com.laytonsmith.core.constructs.CString;
import com.laytonsmith.core.constructs.Target;
import com.laytonsmith.core.events.AbstractEvent;
import com.laytonsmith.core.events.AbstractGenericEvent;
import com.laytonsmith.core.events.BindableEvent;
import com.laytonsmith.core.events.EventMixinInterface;
import com.laytonsmith.core.exceptions.EventException;
Expand All @@ -26,9 +26,9 @@

public class BukkitAbstractEventMixin implements EventMixinInterface {

AbstractEvent mySuper;
AbstractGenericEvent mySuper;

public BukkitAbstractEventMixin(AbstractEvent mySuper) {
public BukkitAbstractEventMixin(AbstractGenericEvent mySuper) {
this.mySuper = mySuper;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
import com.laytonsmith.core.apps.AppsApiUtil;
import com.laytonsmith.core.constructs.Target;
import com.laytonsmith.core.extensions.ExtensionManager;
import com.laytonsmith.core.protocollib.PacketJumper;
import com.laytonsmith.core.packetjumper.PacketJumper;
import com.laytonsmith.core.telemetry.DefaultTelemetry;
import com.laytonsmith.core.telemetry.Telemetry;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -443,6 +443,7 @@ public void onDisable() {
}

ExtensionManager.Cleanup();
PacketJumper.Shutdown();

ac = null;
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/laytonsmith/core/AliasCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import com.laytonsmith.core.functions.Scheduling;
import com.laytonsmith.core.natives.interfaces.MEnumType;
import com.laytonsmith.core.natives.interfaces.Mixed;
import com.laytonsmith.core.packetjumper.PacketJumper;
import com.laytonsmith.core.profiler.ProfilePoint;
import com.laytonsmith.core.profiler.Profiler;
import com.laytonsmith.core.taskmanager.TaskManagerImpl;
Expand Down Expand Up @@ -261,6 +262,8 @@ public final void reload(MCPlayer player, String[] settings, final boolean first
}
}

PacketJumper.Shutdown();

MSLog.initialize(fileLocations.getConfigDirectory());

Profiler profiler;
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/com/laytonsmith/core/Static.java
Original file line number Diff line number Diff line change
Expand Up @@ -1487,9 +1487,18 @@ public static Construct getMSObject(Object object, Target t) {
return CNull.NULL;
} else if(object instanceof Boolean) {
return CBoolean.get((boolean) object);
} else if((object instanceof Byte) || (object instanceof Short) || (object instanceof Integer) || (object instanceof Long)) {
} else if(object instanceof Byte b) {
// These are required due to unboxing
return new CInt(b.longValue(), t);
} else if(object instanceof Short s) {
return new CInt(s.longValue(), t);
} else if(object instanceof Integer i) {
return new CInt(i.longValue(), t);
} else if(object instanceof Long) {
return new CInt((long) object, t);
} else if((object instanceof Float) || (object instanceof Double)) {
} else if(object instanceof Float f) {
return new CDouble(f.doubleValue(), t);
} else if(object instanceof Double) {
return new CDouble((double) object, t);
} else if(object instanceof Character) {
return new CString((char) object, t);
Expand Down
81 changes: 68 additions & 13 deletions src/main/java/com/laytonsmith/core/constructs/CPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
import com.laytonsmith.abstraction.bukkit.entities.BukkitMCPlayer;
import com.laytonsmith.annotations.typeof;
import com.laytonsmith.core.MSVersion;
import com.laytonsmith.core.exceptions.CRE.CREPluginInternalException;
import com.laytonsmith.core.natives.interfaces.ArrayAccess;
import com.laytonsmith.core.natives.interfaces.Mixed;
import com.laytonsmith.core.protocollib.Conversions;
import com.laytonsmith.core.protocollib.PacketKind;
import com.laytonsmith.core.protocollib.PacketUtils;
import com.laytonsmith.core.packetjumper.Conversions;
import com.laytonsmith.core.packetjumper.PacketJumper;
import com.laytonsmith.core.packetjumper.PacketKind;
import com.laytonsmith.core.packetjumper.PacketUtils;
import java.lang.reflect.Field;
import net.fabricmc.mappingio.tree.MappingTree;

/**
* Created by JunHyung Im on 2020-07-05
Expand Down Expand Up @@ -82,32 +85,40 @@ public CClassType[] getInterfaces() {
return CClassType.EMPTY_CLASS_ARRAY;
}

public int indexFromString(String field, Target t) {
CArray packetData = (CArray)((ArrayAccess)((ArrayAccess)PacketUtils.getAllPackets()
.get(protocol.name(), t))
.get(sender == PacketType.Sender.CLIENT ? "IN" : "OUT", t))
.get(packetType, t);
CArray fieldData = (CArray)((ArrayAccess)packetData.get("fields", t)).get(field, t);
int index = (int)((CInt)fieldData.get("field", t)).getInt();
return index;
}

public Object read(int index) {
return packet.getModifier().read(index);
}

public Mixed readMixed(int index, Target target) {
return Conversions.convertObjectToMixed(read(index), target);
return Conversions.convertObjectToMixed(read(index));
}

public Mixed readMixed(String index, Target target) {
return readMixed(indexFromString(index, target), target);
}

public void write(int index, Object object) {
packet.getModifier().write(index, object);
}

public void writeMixed(int index, Mixed mixed) {
public void writeMixed(int index, Mixed mixed, Target t) {
Field field = packet.getModifier().getField(index);
Class<?> type = field.getType();
write(index, Conversions.adjustObject(Conversions.convertMixedToObject(mixed, type), type));
write(index, Conversions.adjustObject(Conversions.convertMixedToObject(mixed, type, t), type));
}

public void writeMixed(String field, Mixed mixed, Target t) {
CArray packetData = (CArray)((ArrayAccess)((ArrayAccess)PacketUtils.getAllPackets()
.get(protocol.name(), t))
.get(sender == PacketType.Sender.CLIENT ? "IN" : "OUT", t))
.get(packetType, t);
CArray fieldData = (CArray)((CArray)packetData.get("fields", t)).get(field, t);
int index = (int)((CInt)fieldData.get("field", t)).getInt();
writeMixed(index, mixed);
writeMixed(indexFromString(field, t), mixed, t);
}

public CArray getFields(Target target) {
Expand All @@ -129,4 +140,48 @@ public PacketKind getKind() {
return PacketUtils.getPacketKind(packet.getType());
}

public CArray toCArray() {
CArray array = new CArray(Target.UNKNOWN);
MappingTree tree = PacketJumper.GetMappingTree();
for(FieldAccessor field : packet.getModifier().getFields()) {
Mixed value;
Object instance = field.get(packet.getHandle());
String type = field.getField().getType().getSimpleName();
String name;
Class clazz = packet.getHandle().getClass();
MappingTree.ClassMapping classMapping
= tree.getClass(clazz.getName().replace(".", "/"), PacketJumper.GetServerNamespace());
MappingTree.FieldMapping fm;
do {
fm = classMapping.getField(field.getField().getName(), null);
if(fm != null) {
break;
}
clazz = clazz.getSuperclass();
if(clazz == Object.class || clazz == Record.class) {
break;
}
classMapping = tree.getClass(clazz.getName().replace(".", "/"),
PacketJumper.GetServerNamespace());
if(classMapping == null) {
throw new CREPluginInternalException("Cannot find packet superclass.", Target.UNKNOWN);
}
} while(true);
name = fm.getDstName(PacketJumper.GetMojangNamespace());
if(instance == null) {
value = CNull.NULL;
} else if(Conversions.getTypeConversion(field.getField().getType()) == null) {
value = new CString("<UNSUPPORTED:" + instance.toString() + ">", Target.UNKNOWN);
} else {
value = Conversions.convertObjectToMixed(instance);
}
CArray descriptor = new CArray(Target.UNKNOWN);
descriptor.set("name", name);
descriptor.set("type", type);
descriptor.set("value", value, Target.UNKNOWN);
array.set(name, descriptor, Target.UNKNOWN);
}
return array;
}

}
Loading

0 comments on commit a49aee2

Please sign in to comment.