mirror of
https://github.com/GTNewHorizons/Opis.git
synced 2026-11-20 12:19:50 +00:00
Readded Event Hook
This commit is contained in:
1 parent
84f73ab3a7
commit
f49826de53
6 files changed
+176
-18
No files matched your search
@@ -0,0 +1,149 @@
|
||||
package cpw.mods.fml.common.eventhandler;
|
||||
|
||||
import static org.objectweb.asm.Opcodes.*;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
|
||||
import mcp.mobius.mobiuscore.profiler.ProfilerSection;
|
||||
|
||||
import org.apache.logging.log4j.ThreadContext;
|
||||
import org.objectweb.asm.ClassWriter;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
import org.objectweb.asm.Type;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
import cpw.mods.fml.common.ModContainer;
|
||||
|
||||
|
||||
public class ASMEventHandler implements IEventListener
|
||||
{
|
||||
private static int IDs = 0;
|
||||
private static final String HANDLER_DESC = Type.getInternalName(IEventListener.class);
|
||||
private static final String HANDLER_FUNC_DESC = Type.getMethodDescriptor(IEventListener.class.getDeclaredMethods()[0]);
|
||||
private static final ASMClassLoader LOADER = new ASMClassLoader();
|
||||
private static final HashMap<Method, Class<?>> cache = Maps.newHashMap();
|
||||
|
||||
private final IEventListener handler;
|
||||
private final SubscribeEvent subInfo;
|
||||
private final String package_;
|
||||
private ModContainer owner;
|
||||
public ASMEventHandler(Object target, Method method, ModContainer owner) throws Exception
|
||||
{
|
||||
this.owner = owner;
|
||||
package_ = method.getDeclaringClass().getCanonicalName();
|
||||
handler = (IEventListener)createWrapper(method).getConstructor(Object.class).newInstance(target);
|
||||
subInfo = method.getAnnotation(SubscribeEvent.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(Event event)
|
||||
{
|
||||
if (owner != null)
|
||||
{
|
||||
ThreadContext.put("mod", owner.getName());
|
||||
}
|
||||
else
|
||||
{
|
||||
ThreadContext.put("mod", "");
|
||||
}
|
||||
if (handler != null)
|
||||
{
|
||||
if (!event.isCancelable() || !event.isCanceled() || subInfo.receiveCanceled())
|
||||
{
|
||||
ProfilerSection.EVENT_INVOKE.start();
|
||||
handler.invoke(event);
|
||||
ProfilerSection.EVENT_INVOKE.stop(event, package_, handler, owner.getName());
|
||||
}
|
||||
}
|
||||
ThreadContext.remove("mod");
|
||||
}
|
||||
|
||||
public EventPriority getPriority()
|
||||
{
|
||||
return subInfo.priority();
|
||||
}
|
||||
|
||||
public Class<?> createWrapper(Method callback)
|
||||
{
|
||||
if (cache.containsKey(callback))
|
||||
{
|
||||
return cache.get(callback);
|
||||
}
|
||||
|
||||
ClassWriter cw = new ClassWriter(0);
|
||||
MethodVisitor mv;
|
||||
|
||||
String name = getUniqueName(callback);
|
||||
String desc = name.replace('.', '/');
|
||||
String instType = Type.getInternalName(callback.getDeclaringClass());
|
||||
String eventType = Type.getInternalName(callback.getParameterTypes()[0]);
|
||||
|
||||
/*
|
||||
System.out.println("Name: " + name);
|
||||
System.out.println("Desc: " + desc);
|
||||
System.out.println("InstType: " + instType);
|
||||
System.out.println("Callback: " + callback.getName() + Type.getMethodDescriptor(callback));
|
||||
System.out.println("Event: " + eventType);
|
||||
*/
|
||||
|
||||
cw.visit(V1_6, ACC_PUBLIC | ACC_SUPER, desc, null, "java/lang/Object", new String[]{ HANDLER_DESC });
|
||||
|
||||
cw.visitSource(".dynamic", null);
|
||||
{
|
||||
cw.visitField(ACC_PUBLIC, "instance", "Ljava/lang/Object;", null, null).visitEnd();
|
||||
}
|
||||
{
|
||||
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(Ljava/lang/Object;)V", null, null);
|
||||
mv.visitCode();
|
||||
mv.visitVarInsn(ALOAD, 0);
|
||||
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
|
||||
mv.visitVarInsn(ALOAD, 0);
|
||||
mv.visitVarInsn(ALOAD, 1);
|
||||
mv.visitFieldInsn(PUTFIELD, desc, "instance", "Ljava/lang/Object;");
|
||||
mv.visitInsn(RETURN);
|
||||
mv.visitMaxs(2, 2);
|
||||
mv.visitEnd();
|
||||
}
|
||||
{
|
||||
mv = cw.visitMethod(ACC_PUBLIC, "invoke", HANDLER_FUNC_DESC, null, null);
|
||||
mv.visitCode();
|
||||
mv.visitVarInsn(ALOAD, 0);
|
||||
mv.visitFieldInsn(GETFIELD, desc, "instance", "Ljava/lang/Object;");
|
||||
mv.visitTypeInsn(CHECKCAST, instType);
|
||||
mv.visitVarInsn(ALOAD, 1);
|
||||
mv.visitTypeInsn(CHECKCAST, eventType);
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, instType, callback.getName(), Type.getMethodDescriptor(callback));
|
||||
mv.visitInsn(RETURN);
|
||||
mv.visitMaxs(2, 2);
|
||||
mv.visitEnd();
|
||||
}
|
||||
cw.visitEnd();
|
||||
Class<?> ret = LOADER.define(name, cw.toByteArray());
|
||||
cache.put(callback, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
private String getUniqueName(Method callback)
|
||||
{
|
||||
return String.format("%s_%d_%s_%s_%s", getClass().getName(), IDs++,
|
||||
callback.getDeclaringClass().getSimpleName(),
|
||||
callback.getName(),
|
||||
callback.getParameterTypes()[0].getSimpleName());
|
||||
}
|
||||
|
||||
private static class ASMClassLoader extends ClassLoader
|
||||
{
|
||||
private ASMClassLoader()
|
||||
{
|
||||
super(ASMClassLoader.class.getClassLoader());
|
||||
}
|
||||
|
||||
public Class<?> define(String name, byte[] data)
|
||||
{
|
||||
return defineClass(name, data, 0, data.length);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,10 +16,11 @@ public class DataEvent implements ISerializable, Comparable {
|
||||
public CachedString event;
|
||||
public CachedString handler;
|
||||
public CachedString package_;
|
||||
public CachedString mod;
|
||||
public long nCalls;
|
||||
public DataTiming update;
|
||||
|
||||
public DataEvent fill(Cell<Class, String, DescriptiveStatistics> cell){
|
||||
public DataEvent fill(Cell<Class, String, DescriptiveStatistics> cellData, String modName){
|
||||
/*
|
||||
String handlerName = cell.getColumnKey().getSimpleName();
|
||||
try {
|
||||
@@ -27,7 +28,7 @@ public class DataEvent implements ISerializable, Comparable {
|
||||
handlerName = splitHandler[2] + "." + splitHandler[3];
|
||||
} catch (Exception e){}
|
||||
*/
|
||||
String[] nameRaw = cell.getColumnKey().split("\\|");
|
||||
String[] nameRaw = cellData.getColumnKey().split("\\|");
|
||||
|
||||
String handlerName = nameRaw[1];
|
||||
try {
|
||||
@@ -37,10 +38,11 @@ public class DataEvent implements ISerializable, Comparable {
|
||||
|
||||
this.package_= new CachedString(nameRaw[0]);
|
||||
this.handler = new CachedString(handlerName);
|
||||
this.event = new CachedString(cell.getRowKey().getName().replace("net.minecraftforge.event.", ""));
|
||||
this.nCalls = cell.getValue().getN();
|
||||
this.event = new CachedString(cellData.getRowKey().getName().replace("net.minecraftforge.event.", ""));
|
||||
this.nCalls = cellData.getValue().getN();
|
||||
this.mod = new CachedString(modName);
|
||||
|
||||
this.update = new DataTiming(cell.getValue().getGeometricMean());
|
||||
this.update = new DataTiming(cellData.getValue().getGeometricMean());
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -50,6 +52,7 @@ public class DataEvent implements ISerializable, Comparable {
|
||||
this.package_.writeToStream(stream);
|
||||
this.handler.writeToStream(stream);
|
||||
this.update.writeToStream(stream);
|
||||
this.mod.writeToStream(stream);
|
||||
stream.writeLong(this.nCalls);
|
||||
}
|
||||
|
||||
@@ -59,6 +62,7 @@ public class DataEvent implements ISerializable, Comparable {
|
||||
retVal.package_= CachedString.readFromStream(stream);
|
||||
retVal.handler = CachedString.readFromStream(stream);
|
||||
retVal.update = DataTiming.readFromStream(stream);
|
||||
retVal.mod = CachedString.readFromStream(stream);
|
||||
retVal.nCalls = stream.readLong();
|
||||
return retVal;
|
||||
}
|
||||
|
||||
@@ -10,7 +10,8 @@ import com.google.common.collect.HashMultimap;
|
||||
public class ProfilerEvent extends ProfilerAbstract {
|
||||
|
||||
private IClock clock = Clock.getNewClock();
|
||||
public HashBasedTable<Class, String, DescriptiveStatistics> data = HashBasedTable.create();
|
||||
public HashBasedTable<Class, String, DescriptiveStatistics> data = HashBasedTable.create();
|
||||
public HashBasedTable<Class, String, String> dataMod = HashBasedTable.create();
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
@@ -23,16 +24,17 @@ public class ProfilerEvent extends ProfilerAbstract {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop(Object key1, Object key2, Object key3) {
|
||||
public void stop(Object event, Object pkg, Object handler, Object mod) {
|
||||
clock.stop();
|
||||
|
||||
try{
|
||||
String name = (String)key2 + "|" + key3.getClass().getSimpleName();
|
||||
data.get(key1.getClass(), name).addValue((double)clock.getDelta());
|
||||
String name = (String)pkg + "|" + handler.getClass().getSimpleName();
|
||||
data.get(event.getClass(), name).addValue((double)clock.getDelta());
|
||||
} catch (Exception e) {
|
||||
String name = (String)key2 + "|" + key3.getClass().getSimpleName();
|
||||
data.put(key1.getClass(), name, new DescriptiveStatistics(250));
|
||||
data.get(key1.getClass(), name).addValue((double)clock.getDelta());
|
||||
String name = (String)pkg + "|" + handler.getClass().getSimpleName();
|
||||
data.put(event.getClass(), name, new DescriptiveStatistics(250));
|
||||
dataMod.put(event.getClass(), name, (String)mod);
|
||||
data.get(event.getClass(), name).addValue((double)clock.getDelta());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -142,8 +142,9 @@ public enum OpisClientTickHandler{
|
||||
|
||||
ArrayList<DataEvent> timingEvents = new ArrayList<DataEvent>();
|
||||
HashBasedTable<Class, String, DescriptiveStatistics> eventData = ((ProfilerEvent)ProfilerSection.EVENT_INVOKE.getProfiler()).data;
|
||||
HashBasedTable<Class, String, String> eventMod = ((ProfilerEvent)ProfilerSection.EVENT_INVOKE.getProfiler()).dataMod;
|
||||
for (Cell<Class, String, DescriptiveStatistics> cell : eventData.cellSet()){
|
||||
timingEvents.add(new DataEvent().fill(cell));
|
||||
timingEvents.add(new DataEvent().fill(cell, eventMod.get(cell.getRowKey(), cell.getColumnKey())));
|
||||
}
|
||||
((PanelEventClient)(TabPanelRegistrar.INSTANCE.getTab(SelectedTab.CLIENTEVENTS))).setTable(timingEvents);
|
||||
|
||||
|
||||
@@ -281,8 +281,9 @@ public class PacketManager extends FMLIndexedMessageToMessageCodec<PacketBase>
|
||||
|
||||
ArrayList<DataEvent> timingEvents = new ArrayList<DataEvent>();
|
||||
HashBasedTable<Class, String, DescriptiveStatistics> eventData = ((ProfilerEvent)ProfilerSection.EVENT_INVOKE.getProfiler()).data;
|
||||
HashBasedTable<Class, String, String> eventMod = ((ProfilerEvent)ProfilerSection.EVENT_INVOKE.getProfiler()).dataMod;
|
||||
for (Cell<Class, String, DescriptiveStatistics> cell : eventData.cellSet()){
|
||||
timingEvents.add(new DataEvent().fill(cell));
|
||||
timingEvents.add(new DataEvent().fill(cell, eventMod.get(cell.getRowKey(), cell.getColumnKey())));
|
||||
}
|
||||
|
||||
//PacketManager.validateAndSend(NetDataList_OLD.create(Message.LIST_TIMING_HANDLERS, timingHandlers), player);
|
||||
|
||||
@@ -3,6 +3,7 @@ package mcp.mobius.opis.swing.panels.timingserver;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import mcp.mobius.opis.api.ITabPanel;
|
||||
import mcp.mobius.opis.data.holders.newtypes.CachedString;
|
||||
import mcp.mobius.opis.data.holders.newtypes.DataEvent;
|
||||
import mcp.mobius.opis.data.holders.newtypes.DataTiming;
|
||||
import mcp.mobius.opis.network.PacketBase;
|
||||
@@ -35,9 +36,9 @@ public class PanelTimingEvents extends JPanelMsgHandler implements ITabPanel {
|
||||
add(scrollPane, "cell 0 1 2 1,grow");
|
||||
|
||||
table = new JTableStats(
|
||||
new String[] {"Event", "Class", "Handler", "Calls", "Timing"},
|
||||
new Class[] {String.class, String.class, String.class, Long.class, DataTiming.class},
|
||||
new int[] {SwingConstants.LEFT, SwingConstants.LEFT, SwingConstants.LEFT, SwingConstants.CENTER, SwingConstants.CENTER}
|
||||
new String[] {"Event", "Mod", "Class", "Handler", "Calls", "Timing"},
|
||||
new Class[] {CachedString.class, CachedString.class, CachedString.class, CachedString.class, Long.class, DataTiming.class},
|
||||
new int[] {SwingConstants.LEFT, SwingConstants.LEFT, SwingConstants.LEFT, SwingConstants.LEFT, SwingConstants.CENTER, SwingConstants.CENTER}
|
||||
);
|
||||
scrollPane.setViewportView(table);
|
||||
}
|
||||
@@ -55,7 +56,7 @@ public class PanelTimingEvents extends JPanelMsgHandler implements ITabPanel {
|
||||
|
||||
for (Object o : rawdata.array){
|
||||
DataEvent data = (DataEvent)o;
|
||||
model.addRow(new Object[] {data.event, data.package_, data.handler, data.nCalls, data.update});
|
||||
model.addRow(new Object[] {data.event, data.mod, data.package_, data.handler, data.nCalls, data.update});
|
||||
}
|
||||
|
||||
this.getTable().dataUpdated(row);
|
||||
|
||||
Reference in new issue
Block a user