Revert "Attempt to use MSCOM"

This reverts commit ea6c2485
This commit is contained in:
Norbi Peti 2019-04-21 23:27:58 +02:00
parent ea6c248534
commit b7d0b49ac6
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
6 changed files with 27 additions and 66 deletions

View file

@ -129,7 +129,7 @@
</dependency>
<dependency>
<groupId>org.virtualbox</groupId>
<artifactId>VirtualBox</artifactId>
<artifactId>VirtualBox-MSCOM</artifactId>
<version>6.0</version>
<scope>compile</scope>
</dependency>

View file

@ -61,11 +61,6 @@
<artifactId>VirtualBox-MSCOM</artifactId>
<version>6.0</version>
</dependency>
<dependency>
<groupId>com.jacob</groupId>
<artifactId>Jacob</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>craftbukkit</artifactId>

View file

@ -23,7 +23,6 @@ import java.util.Arrays;
import java.util.function.Predicate;
public class PluginMain extends JavaPlugin {
public static final boolean MSCOM = true; //TODO: Implement
private static final int MCX = 5;
private static final int MCY = 4;
private BukkitTask mousetask;

View file

@ -1,13 +1,16 @@
package sznp.virtualcomputer.events;
import lombok.val;
import org.bukkit.Bukkit;
import org.mozilla.interfaces.IEvent;
import org.mozilla.interfaces.IEventListener;
import org.virtualbox_6_0.IEventSource;
import org.virtualbox_6_0.VBoxEventType;
import sznp.virtualcomputer.PluginMain;
import sznp.virtualcomputer.util.COMObjectBase;
import sznp.virtualcomputer.util.Utils;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Map;
@ -28,15 +31,29 @@ public abstract class EventHandlerBase extends COMObjectBase implements IEventLi
@Override
public final void handleEvent(IEvent iEvent) {
//val cl=eventMap.get((int)iEvent.getType()); - We can afford to search through the events for this handler
if (!enabled)
return;
Utils.handleEvent(this, eventMap, iEvent, false);
val kv = eventMap.entrySet().stream().filter(e -> e.getKey().value() == iEvent.getType()).findAny();
if (!kv.isPresent()) return; //Event not supported
val cl = kv.get().getValue();
for (Method method : getClass().getMethods()) {
if (method.isAnnotationPresent(org.bukkit.event.EventHandler.class)
&& method.getParameterCount() == 1 && method.getParameterTypes()[0] == cl) {
try {
method.invoke(this, Utils.getEvent(iEvent, cl));
return;
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
Bukkit.getLogger().warning("Error while handling VirtualBox event!");
e.getCause().printStackTrace();
}
}
}
}
public <T extends EventHandlerBase> org.virtualbox_6_0.IEventListener registerTo(IEventSource source) {
if (PluginMain.MSCOM)
return Utils.registerListenerMSCOM(source, this, new ArrayList<>(eventMap.keySet()));
else
return Utils.registerListener(source, this, new ArrayList<>(eventMap.keySet()));
}

View file

@ -1,4 +0,0 @@
package sznp.virtualcomputer.events;
public class EventHandlerBaseMSCOM {
}

View file

@ -1,21 +1,16 @@
package sznp.virtualcomputer.util;
import com.jacob.com.Variant;
import lombok.val;
import org.bukkit.Bukkit;
import org.mozilla.interfaces.IEventListener;
import org.virtualbox_6_0.IEvent;
import org.virtualbox_6_0.IEventSource;
import org.virtualbox_6_0.VBoxEventType;
import org.virtualbox_6_0.xpcom.IUnknown;
import sznp.virtualcomputer.events.EventHandlerBase;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class Utils {
/**
@ -44,20 +39,17 @@ public class Utils {
usrPathsField.set(null, newPaths);
}
//public static void registerListener(IEventSource source, IEventListener listener, VBoxEventType... types) {
public static org.virtualbox_6_0.IEventListener registerListener(IEventSource source, IEventListener listener, List<VBoxEventType> types) {
val ret = new org.virtualbox_6_0.IEventListener(listener);
source.registerListener(ret, types, true);
return ret;
}
public static org.virtualbox_6_0.IEventListener registerListenerMSCOM(IEventSource source, Object listener, List<VBoxEventType> types) {
val ret = new org.virtualbox_6_0.IEventListener(new Variant(listener).toDispatch());
source.registerListener(ret, types, true);
return ret;
}
@SuppressWarnings("unchecked")
public static <T extends IEvent> T getEvent(org.mozilla.interfaces.IEvent event, Class<T> cl) {
//if (event.getType() != type.value()) return null;
//return (T) T.queryInterface(new IEvent(event)); - Probably won't work
try {
val method = cl.getMethod("queryInterface", IUnknown.class);
return (T) method.invoke(null, new IEvent(event));
@ -66,42 +58,4 @@ public class Utils {
return null;
}
}
@SuppressWarnings("unchecked")
public static <T extends IEvent> T getEventMSCOM(Object event, Class<T> cl) {
try {
val method = cl.getMethod("queryInterface", org.virtualbox_6_0.mscom.IUnknown.class);
return (T) method.invoke(null, new IEvent(new Variant(event).toDispatch()));
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
return null;
}
}
public static void handleEvent(EventHandlerBase handler, Map<VBoxEventType, Class<? extends IEvent>> eventMap, Object iEvent, boolean mscom) {
Class<?> cl;
if (mscom) {
val kv = eventMap.entrySet().stream().filter(e -> e.getKey().value() == iEvent.getType().value()).findAny();
if (!kv.isPresent()) return; //Event not supported
cl = kv.get().getValue();
} else {
val kv = eventMap.entrySet().stream().filter(e -> e.getKey().value() == iEvent.getType()).findAny();
if (!kv.isPresent()) return; //Event not supported
cl = kv.get().getValue();
}
for (Method method : handler.getClass().getMethods()) {
if (method.isAnnotationPresent(org.bukkit.event.EventHandler.class)
&& method.getParameterCount() == 1 && method.getParameterTypes()[0] == cl) {
try {
method.invoke(handler, Utils.getEvent(iEvent, cl));
return;
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
Bukkit.getLogger().warning("Error while handling VirtualBox event!");
e.getCause().printStackTrace();
}
}
}
}
}