multiplugin support
This commit is contained in:
parent
3e85dce2c7
commit
8ea402e70b
|
@ -1,6 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <jni.h>
|
#include <jni.h>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#define DEBUG 1
|
#define DEBUG 1
|
||||||
|
|
||||||
|
@ -16,6 +17,7 @@ extern jclass MAPCLASS;
|
||||||
extern jclass LISTENERCLASS;
|
extern jclass LISTENERCLASS;
|
||||||
extern jfieldID EVENTDATAF;
|
extern jfieldID EVENTDATAF;
|
||||||
extern jfieldID EVENTNAMEF;
|
extern jfieldID EVENTNAMEF;
|
||||||
|
extern jfieldID PLUGINIDF;
|
||||||
extern jmethodID MAPGET;
|
extern jmethodID MAPGET;
|
||||||
extern jmethodID MAPPUT;
|
extern jmethodID MAPPUT;
|
||||||
extern jmethodID MAPCLEAR;
|
extern jmethodID MAPCLEAR;
|
||||||
|
@ -31,11 +33,12 @@ enum JNIDATA : long {
|
||||||
JNI_LISTENERCLASS = 8,
|
JNI_LISTENERCLASS = 8,
|
||||||
JNI_EVENTDATAFIELD = 16 | JNI_EVENTCLASS,
|
JNI_EVENTDATAFIELD = 16 | JNI_EVENTCLASS,
|
||||||
JNI_EVENTNAMEFIELD = 32 | JNI_EVENTCLASS,
|
JNI_EVENTNAMEFIELD = 32 | JNI_EVENTCLASS,
|
||||||
JNI_MAPGETMETH = 64 | JNI_MAPCLASS,
|
JNI_PLUGINIDFIELD = 64 | JNI_PLUGINCLASS,
|
||||||
JNI_MAPPUTMETH = 128 | JNI_MAPCLASS,
|
JNI_MAPGETMETH = 128 | JNI_MAPCLASS,
|
||||||
JNI_MAPCLEARMETH = 256 | JNI_MAPCLASS,
|
JNI_MAPPUTMETH = 256 | JNI_MAPCLASS,
|
||||||
JNI_LISTENERCONSTR = 512 | JNI_LISTENERCLASS,
|
JNI_MAPCLEARMETH = 512 | JNI_MAPCLASS,
|
||||||
JNI_LISTENERSMALLCONSTR = 1024 | JNI_LISTENERCLASS,
|
JNI_LISTENERCONSTR = 1024 | JNI_LISTENERCLASS,
|
||||||
|
JNI_LISTENERSMALLCONSTR = 2048 | JNI_LISTENERCLASS,
|
||||||
|
|
||||||
JNI_EVENT = JNI_EVENTCLASS | JNI_EVENTDATAFIELD | JNI_EVENTNAMEFIELD,
|
JNI_EVENT = JNI_EVENTCLASS | JNI_EVENTDATAFIELD | JNI_EVENTNAMEFIELD,
|
||||||
JNI_MAP = JNI_MAPCLASS | JNI_MAPGETMETH | JNI_MAPPUTMETH,
|
JNI_MAP = JNI_MAPCLASS | JNI_MAPGETMETH | JNI_MAPPUTMETH,
|
||||||
|
@ -49,4 +52,6 @@ long operator|(long a, JNIDATA b);
|
||||||
long operator&(long a, JNIDATA b);*/
|
long operator&(long a, JNIDATA b);*/
|
||||||
|
|
||||||
class CppPlugin;
|
class CppPlugin;
|
||||||
extern CppPlugin* plugin; //what happens, with more than one plugin?
|
extern std::vector<CppPlugin*> plugins;
|
||||||
|
|
||||||
|
CppPlugin* getPlugin(JNIEnv* env = 0, jobject thisobj = 0, int id = -1);
|
|
@ -16,6 +16,7 @@ typedef CppPlugin* (*initfptr)();
|
||||||
class CppPlugin {
|
class CppPlugin {
|
||||||
public:
|
public:
|
||||||
void* handle;
|
void* handle;
|
||||||
|
std::string name;
|
||||||
virtual void onLoad();
|
virtual void onLoad();
|
||||||
virtual void onEnable();
|
virtual void onEnable();
|
||||||
virtual void onDisable();
|
virtual void onDisable();
|
||||||
|
@ -23,4 +24,3 @@ public:
|
||||||
virtual ~CppPlugin();
|
virtual ~CppPlugin();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <dlfcn.h> //dynamic linking
|
#include <dlfcn.h> //dynamic linking
|
||||||
|
#include <libgen.h> //basename
|
||||||
|
|
||||||
#include <thread> //thread id
|
#include <thread> //thread id
|
||||||
|
|
||||||
|
@ -15,15 +16,15 @@ jclass MAPCLASS;
|
||||||
jclass LISTENERCLASS;
|
jclass LISTENERCLASS;
|
||||||
jfieldID EVENTDATAF;
|
jfieldID EVENTDATAF;
|
||||||
jfieldID EVENTNAMEF;
|
jfieldID EVENTNAMEF;
|
||||||
|
jfieldID PLUGINIDF;
|
||||||
jmethodID MAPGET;
|
jmethodID MAPGET;
|
||||||
jmethodID MAPPUT;
|
jmethodID MAPPUT;
|
||||||
jmethodID MAPCLEAR;
|
jmethodID MAPCLEAR;
|
||||||
jmethodID LISTENERCONTR;
|
jmethodID LISTENERCONTR;
|
||||||
jmethodID LISTENERSMALLCONTR;
|
jmethodID LISTENERSMALLCONTR;
|
||||||
|
|
||||||
CppPlugin* plugin; //what happens, with more than one plugin?
|
std::vector<CppPlugin*> plugins;
|
||||||
|
/*
|
||||||
|
|
||||||
void fixcap(JNIEnv* env) {
|
void fixcap(JNIEnv* env) {
|
||||||
DEB("ensure 1024" );
|
DEB("ensure 1024" );
|
||||||
env->EnsureLocalCapacity(1024);
|
env->EnsureLocalCapacity(1024);
|
||||||
|
@ -31,6 +32,15 @@ void fixcap(JNIEnv* env) {
|
||||||
DEB("push frame 1024" );
|
DEB("push frame 1024" );
|
||||||
env->PushLocalFrame(1024);
|
env->PushLocalFrame(1024);
|
||||||
|
|
||||||
|
}*/
|
||||||
|
|
||||||
|
CppPlugin* getPlugin(JNIEnv* env, jobject thisobj, int id) {
|
||||||
|
//get id
|
||||||
|
if(id < 0) {
|
||||||
|
load(env, JNI_PLUGINIDFIELD);
|
||||||
|
id = env->GetIntField(thisobj, PLUGINIDF);
|
||||||
|
}
|
||||||
|
return plugins.at(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
void load(JNIEnv* env, long data) {
|
void load(JNIEnv* env, long data) {
|
||||||
|
@ -42,13 +52,15 @@ void load(JNIEnv* env, long data) {
|
||||||
MAPCLASS = env->FindClass("java/util/Map");
|
MAPCLASS = env->FindClass("java/util/Map");
|
||||||
if(data & JNI_LISTENERCLASS)
|
if(data & JNI_LISTENERCLASS)
|
||||||
LISTENERCLASS = env->FindClass("de/mrbesen/cppplugins/CppPlugin$CppListener");
|
LISTENERCLASS = env->FindClass("de/mrbesen/cppplugins/CppPlugin$CppListener");
|
||||||
DEB("find classes: " << (!!CPPPLUGINCLASS) << (!!EVENTCLASS) << (!!MAPCLASS) << (!!LISTENERCLASS));
|
|
||||||
|
|
||||||
if(data & JNI_EVENTDATAFIELD)
|
if(data & JNI_EVENTDATAFIELD)
|
||||||
EVENTDATAF = env->GetFieldID(EVENTCLASS, "data", "Ljava/util/Map;");
|
EVENTDATAF = env->GetFieldID(EVENTCLASS, "data", "Ljava/util/Map;");
|
||||||
if(data & JNI_EVENTNAMEFIELD)
|
if(data & JNI_EVENTNAMEFIELD)
|
||||||
EVENTNAMEF = env->GetFieldID(EVENTCLASS, "name", "Ljava/lang/String;");
|
EVENTNAMEF = env->GetFieldID(EVENTCLASS, "name", "Ljava/lang/String;");
|
||||||
DEB("findfields: " << (!!EVENTDATAF) << (!!EVENTNAMEF));
|
if(data & JNI_PLUGINIDFIELD)
|
||||||
|
PLUGINIDF = env->GetFieldID(CPPPLUGINCLASS, "id", "I");
|
||||||
|
|
||||||
|
|
||||||
if(data & JNI_MAPGETMETH)
|
if(data & JNI_MAPGETMETH)
|
||||||
MAPGET = env->GetMethodID(MAPCLASS, "get", "(Ljava/lang/Object;)Ljava/lang/Object;");
|
MAPGET = env->GetMethodID(MAPCLASS, "get", "(Ljava/lang/Object;)Ljava/lang/Object;");
|
||||||
|
@ -61,7 +73,16 @@ void load(JNIEnv* env, long data) {
|
||||||
if(data & JNI_LISTENERSMALLCONSTR)
|
if(data & JNI_LISTENERSMALLCONSTR)
|
||||||
LISTENERSMALLCONTR = env->GetMethodID(LISTENERCLASS, "<init>", "()V");
|
LISTENERSMALLCONTR = env->GetMethodID(LISTENERCLASS, "<init>", "()V");
|
||||||
|
|
||||||
DEB("find methods: " << (!!MAPGET) << (!!MAPPUT) << (!!MAPCLEAR) << (!!LISTENERCONTR) << (!!LISTENERSMALLCONTR));
|
|
||||||
|
if(((!CPPPLUGINCLASS) + (!EVENTCLASS) + (!MAPCLASS) + (!LISTENERCLASS) //wenn eins == nullptr ist -> wird zu 1 -> endergebniss != 0 -> fehler
|
||||||
|
+ (!EVENTDATAF) + (!EVENTNAMEF) + (!PLUGINIDF)
|
||||||
|
+ (!MAPGET) + (!MAPPUT) + (!MAPCLEAR) + (!LISTENERCONTR) + (!LISTENERSMALLCONTR)) == 0) {
|
||||||
|
|
||||||
|
DEB("Error loading a information: ");
|
||||||
|
DEB("find classes: " << (!!CPPPLUGINCLASS) << (!!EVENTCLASS) << (!!MAPCLASS) << (!!LISTENERCLASS));
|
||||||
|
DEB("findfields: " << (!!EVENTDATAF) << (!!EVENTNAMEF) << (!!PLUGINIDF));
|
||||||
|
DEB("find methods: " << (!!MAPGET) << (!!MAPPUT) << (!!MAPCLEAR) << (!!LISTENERCONTR) << (!!LISTENERSMALLCONTR));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT jboolean JNICALL Java_de_mrbesen_cppplugins_CppPlugin_init_1(JNIEnv* env, jobject thisobj) {
|
JNIEXPORT jboolean JNICALL Java_de_mrbesen_cppplugins_CppPlugin_init_1(JNIEnv* env, jobject thisobj) {
|
||||||
|
@ -95,11 +116,20 @@ JNIEXPORT jboolean JNICALL Java_de_mrbesen_cppplugins_CppPlugin_loadPlugin(JNIEn
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
plugin = initf();
|
CppPlugin* plugin = initf();
|
||||||
plugin->handle = handle;
|
plugin->handle = handle;
|
||||||
|
|
||||||
//store data
|
//set name / handle id
|
||||||
//setData(data, env, thisobj);
|
plugin->name = std::string(cpath);
|
||||||
|
plugin->name = plugin->name.substr(plugin->name.rfind('/'));
|
||||||
|
|
||||||
|
// add to vector
|
||||||
|
plugins.push_back(plugin);
|
||||||
|
|
||||||
|
//store id
|
||||||
|
load(env, JNI_PLUGINIDFIELD);
|
||||||
|
env->SetIntField(thisobj, PLUGINIDF, plugins.size()-1);
|
||||||
|
|
||||||
return (bool) plugin; // converted to boolean here
|
return (bool) plugin; // converted to boolean here
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,9 +139,8 @@ JNIEXPORT jboolean JNICALL Java_de_mrbesen_cppplugins_CppPlugin_loadPlugin(JNIEn
|
||||||
* Signature: ()V
|
* Signature: ()V
|
||||||
*/
|
*/
|
||||||
JNIEXPORT void JNICALL Java_de_mrbesen_cppplugins_CppPlugin_onLoad(JNIEnv* env, jobject thisobj) {
|
JNIEXPORT void JNICALL Java_de_mrbesen_cppplugins_CppPlugin_onLoad(JNIEnv* env, jobject thisobj) {
|
||||||
//TODO
|
|
||||||
DEB("lib::onLoad()");
|
DEB("lib::onLoad()");
|
||||||
plugin->onLoad();
|
getPlugin(env, thisobj)->onLoad();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -120,9 +149,8 @@ JNIEXPORT void JNICALL Java_de_mrbesen_cppplugins_CppPlugin_onLoad(JNIEnv* env,
|
||||||
* Signature: ()V
|
* Signature: ()V
|
||||||
*/
|
*/
|
||||||
JNIEXPORT void JNICALL Java_de_mrbesen_cppplugins_CppPlugin_onEnable_1(JNIEnv* env, jobject thisobj) {
|
JNIEXPORT void JNICALL Java_de_mrbesen_cppplugins_CppPlugin_onEnable_1(JNIEnv* env, jobject thisobj) {
|
||||||
//TODO
|
|
||||||
DEB("lib::onEnable()");
|
DEB("lib::onEnable()");
|
||||||
plugin->onEnable();
|
getPlugin(env, thisobj)->onEnable();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -133,6 +161,7 @@ JNIEXPORT void JNICALL Java_de_mrbesen_cppplugins_CppPlugin_onEnable_1(JNIEnv* e
|
||||||
JNIEXPORT void JNICALL Java_de_mrbesen_cppplugins_CppPlugin_onDisable_1(JNIEnv* env, jobject thisobj) {
|
JNIEXPORT void JNICALL Java_de_mrbesen_cppplugins_CppPlugin_onDisable_1(JNIEnv* env, jobject thisobj) {
|
||||||
//TODO
|
//TODO
|
||||||
DEB("lib::onDisable()" );
|
DEB("lib::onDisable()" );
|
||||||
|
CppPlugin* plugin = getPlugin(env, thisobj);
|
||||||
plugin->onDisable();
|
plugin->onDisable();
|
||||||
dlclose(plugin->handle);
|
dlclose(plugin->handle);
|
||||||
}
|
}
|
||||||
|
@ -158,7 +187,7 @@ JNIEXPORT jobject JNICALL Java_de_mrbesen_cppplugins_CppPlugin_fireEvent(JNIEnv*
|
||||||
Event event(env, jevent);
|
Event event(env, jevent);
|
||||||
|
|
||||||
//call it
|
//call it
|
||||||
f(env, plugin, &event);
|
f(env, getPlugin(env, thisobj), &event);
|
||||||
|
|
||||||
//put changed fields back in the jevent
|
//put changed fields back in the jevent
|
||||||
event.reapply(env, jevent);
|
event.reapply(env, jevent);
|
||||||
|
@ -185,7 +214,7 @@ JNIEXPORT jobjectArray JNICALL Java_de_mrbesen_cppplugins_CppPlugin_getListener(
|
||||||
load(env, JNI_LISTENER);
|
load(env, JNI_LISTENER);
|
||||||
|
|
||||||
//call getEvents
|
//call getEvents
|
||||||
std::map<std::string, eventfptr> events = plugin->getEvents();
|
std::map<std::string, eventfptr> events = getPlugin(env, thisobj)->getEvents();
|
||||||
DEB("found " << events.size() << " Events");
|
DEB("found " << events.size() << " Events");
|
||||||
|
|
||||||
//create array
|
//create array
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
#include <plugin.h>
|
#include <plugin.h>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
void CppPlugin::onLoad() {
|
void CppPlugin::onLoad() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -13,6 +15,7 @@ void CppPlugin::onDisable() {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map<std::string, eventfptr> CppPlugin::getEvents() {
|
std::map<std::string, eventfptr> CppPlugin::getEvents() {
|
||||||
|
std::cout << "Defaults::getEvents()" << std::endl;
|
||||||
return std::map<std::string, eventfptr>();
|
return std::map<std::string, eventfptr>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,25 +2,28 @@ package de.mrbesen.cppplugins;
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.event.Event;
|
import org.bukkit.event.Event;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class CppEvent {
|
public class CppEvent {
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private final String name;
|
private final String name;
|
||||||
Map<String, Object> data = new HashMap<>();
|
private Map<String, Object> data = new HashMap<>();
|
||||||
|
private static Logger log = Bukkit.getLogger();
|
||||||
|
|
||||||
public static CppEvent cppify(Event e) {
|
public static CppEvent cppify(Event e) {
|
||||||
System.out.println("cppifying event: " + e.getClass().getSimpleName());
|
log.info("cppifying event: " + e.getClass().getSimpleName());
|
||||||
CppEvent cppe = new CppEvent(e.getClass().getSimpleName());
|
CppEvent cppe = new CppEvent(e.getClass().getSimpleName());
|
||||||
|
|
||||||
for(Field field : e.getClass().getDeclaredFields()) {
|
for(Field field : e.getClass().getDeclaredFields()) {
|
||||||
System.out.println("try to read Field: " + field.getName());
|
log.fine("try to read Field: " + field.getName());
|
||||||
if(!field.isAccessible()) field.setAccessible(true);
|
if(!field.isAccessible()) field.setAccessible(true);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -34,19 +37,19 @@ public class CppEvent {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void uncppify(CppEvent cppe, Event original) {
|
public static void uncppify(CppEvent cppe, Event original) {
|
||||||
System.out.println("uncppifying event: " + original.getClass().getSimpleName() + " values: " + cppe.data.size());
|
log.info("uncppifying event: " + original.getClass().getSimpleName() + " values: " + cppe.data.size());
|
||||||
if(!original.getClass().getSimpleName().equals(cppe.name))
|
if(!original.getClass().getSimpleName().equals(cppe.name))
|
||||||
throw new IllegalArgumentException("CppEvent does not match original event type!");
|
throw new IllegalArgumentException("CppEvent does not match original event type!");
|
||||||
|
|
||||||
for(Field field : original.getClass().getDeclaredFields()) {
|
for(Field field : original.getClass().getDeclaredFields()) {
|
||||||
System.out.println("try to set Field: " + field.getName());
|
log.fine("try to set Field: " + field.getName());
|
||||||
|
|
||||||
Object value = cppe.data.get(field.getName());
|
Object value = cppe.data.get(field.getName());
|
||||||
if(value == null) {
|
if(value == null) {
|
||||||
System.out.println("Data not available - skip");
|
log.fine("Data not available - skip");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
System.out.println("value: " + value);
|
log.fine("value: " + value);
|
||||||
|
|
||||||
if(!field.isAccessible()) field.setAccessible(true);
|
if(!field.isAccessible()) field.setAccessible(true);
|
||||||
|
|
||||||
|
@ -56,6 +59,5 @@ public class CppEvent {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,7 +44,7 @@ public class CppLoader implements PluginLoader {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Plugin loadPlugin(File file) throws InvalidPluginException, UnknownDependencyException {
|
public Plugin loadPlugin(File file) throws InvalidPluginException, UnknownDependencyException {
|
||||||
System.out.println("try to load: " + file.getName());
|
log.info("try to load: " + file.getName());
|
||||||
return new CppPlugin(file, this);
|
return new CppPlugin(file, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,11 +61,11 @@ public class CppLoader implements PluginLoader {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<Class<? extends Event>, Set<RegisteredListener>> createRegisteredListeners(Listener listenerabcdeg, Plugin plugin) {
|
public Map<Class<? extends Event>, Set<RegisteredListener>> createRegisteredListeners(Listener listenerabcdeg, Plugin plugin) {
|
||||||
System.out.println("register Listener for: " + plugin.getName());
|
log.info("register Listener for: " + plugin.getName());
|
||||||
CppPlugin.CppListener[] listener = ((CppPlugin) plugin).getListener();
|
CppPlugin.CppListener[] listener = ((CppPlugin) plugin).getListener();
|
||||||
Map<Class<? extends Event>, Set<RegisteredListener>> out = new HashMap<>();
|
Map<Class<? extends Event>, Set<RegisteredListener>> out = new HashMap<>();
|
||||||
|
|
||||||
System.out.println("found " + listener.length + " Events");
|
log.fine("found " + listener.length + " Events");
|
||||||
|
|
||||||
for(CppPlugin.CppListener lis : listener) {
|
for(CppPlugin.CppListener lis : listener) {
|
||||||
if(lis.functionid == 0) continue;
|
if(lis.functionid == 0) continue;
|
||||||
|
@ -73,11 +73,11 @@ public class CppLoader implements PluginLoader {
|
||||||
//search for Class
|
//search for Class
|
||||||
Class clas = findClass(lis.eventfilter);
|
Class clas = findClass(lis.eventfilter);
|
||||||
if(clas == null) {
|
if(clas == null) {
|
||||||
System.out.println("Class " + lis.eventfilter + " is unknown event");
|
log.warning("Class " + lis.eventfilter + " is unknown event");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("register event " + clas.getName() + " for function: " + lis.functionid );
|
log.fine("register event " + clas.getName() + " for function: " + lis.functionid );
|
||||||
|
|
||||||
AdapterExecutor adapt = new AdapterExecutor((CppPlugin) plugin, lis.functionid);
|
AdapterExecutor adapt = new AdapterExecutor((CppPlugin) plugin, lis.functionid);
|
||||||
Bukkit.getPluginManager().registerEvent(clas, lis, EventPriority.NORMAL, adapt, plugin, true);
|
Bukkit.getPluginManager().registerEvent(clas, lis, EventPriority.NORMAL, adapt, plugin, true);
|
||||||
|
|
|
@ -18,12 +18,13 @@ import java.util.List;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
public class CppPlugin implements Plugin {
|
public class CppPlugin implements Plugin {
|
||||||
|
|
||||||
private boolean enabled = false;
|
private boolean enabled = false;
|
||||||
@Getter
|
@Getter
|
||||||
private String name;
|
private String name;
|
||||||
private static boolean initdone = false;
|
private static boolean initdone = false;
|
||||||
private CppLoader loader;
|
private CppLoader loader;
|
||||||
private long data; //for JNI, do not remove
|
private int id; //for JNI, do not remove
|
||||||
|
|
||||||
//library operations
|
//library operations
|
||||||
protected native boolean init_(); //false: failed, true: success
|
protected native boolean init_(); //false: failed, true: success
|
||||||
|
@ -56,7 +57,7 @@ public class CppPlugin implements Plugin {
|
||||||
File folder = CppPlugins.getInstance().getDataFolder();
|
File folder = CppPlugins.getInstance().getDataFolder();
|
||||||
folder.mkdirs();
|
folder.mkdirs();
|
||||||
String path = folder.getAbsolutePath() + "/libplugin.so";
|
String path = folder.getAbsolutePath() + "/libplugin.so";
|
||||||
System.out.println("load library from: " + path);
|
getLogger().info("load library from: " + path);
|
||||||
try {
|
try {
|
||||||
System.load(path);
|
System.load(path);
|
||||||
return init_();
|
return init_();
|
||||||
|
@ -149,7 +150,7 @@ public class CppPlugin implements Plugin {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Logger getLogger() {
|
public Logger getLogger() {
|
||||||
return null;
|
return Logger.getLogger(getName().substring(0, 3));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -6,12 +6,11 @@ import org.bukkit.plugin.Plugin;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.util.function.Consumer;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
public class CppPlugins extends JavaPlugin {
|
public class CppPlugins extends JavaPlugin {
|
||||||
|
|
||||||
public static String testplugin = "simpleevents.so";
|
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private static CppPlugins instance;
|
private static CppPlugins instance;
|
||||||
|
|
||||||
|
@ -24,14 +23,25 @@ public class CppPlugins extends JavaPlugin {
|
||||||
Bukkit.getPluginManager().registerInterface(CppLoader.class);
|
Bukkit.getPluginManager().registerInterface(CppLoader.class);
|
||||||
getLogger().info("registered");
|
getLogger().info("registered");
|
||||||
|
|
||||||
try {
|
File pluginsFolder = new File("plugins/");
|
||||||
Bukkit.getPluginManager().loadPlugin(new File("plugins/" + testplugin)); //TODO: do loading in another way
|
File[] pluginFiles = pluginsFolder.listFiles((dir, name) -> name.endsWith(".so"));
|
||||||
System.out.println("loaded " + testplugin);
|
for(File pluginFile : pluginFiles) {
|
||||||
|
try {
|
||||||
//TODO: trigger onLoad in those Plugins
|
Bukkit.getPluginManager().loadPlugin(pluginFile);
|
||||||
} catch (Throwable e) {
|
System.out.println("loaded " + pluginFile.getName());
|
||||||
getLogger().log(Level.SEVERE, "Error ocoured during loading " + testplugin, e);
|
} catch (Throwable e) {
|
||||||
|
getLogger().log(Level.SEVERE, "Error occurred during loading in plugin: " + pluginFile.getName(), e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//trigger on Load
|
||||||
|
forEach(pl -> {
|
||||||
|
try {
|
||||||
|
pl.onLoad();
|
||||||
|
} catch (Throwable t) {
|
||||||
|
getLogger().log(Level.WARNING, "Error in onLoad() of Plugin: " + pl.getName(), t);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -46,4 +56,14 @@ public class CppPlugins extends JavaPlugin {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//iteratoe over all CppPlugins
|
||||||
|
public void forEach(Consumer<CppPlugin> cons) {
|
||||||
|
Plugin[] pls = Bukkit.getPluginManager().getPlugins();
|
||||||
|
for (int i = 0; i < pls.length; i++) {
|
||||||
|
if(pls[i] instanceof CppPlugin) {
|
||||||
|
cons.accept((CppPlugin) pls[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
||||||
class MyPlugin : public CppPlugin {
|
class EventsPlugin : public CppPlugin {
|
||||||
public:
|
public:
|
||||||
virtual void onLoad();
|
virtual void onLoad();
|
||||||
virtual void onEnable();
|
virtual void onEnable();
|
||||||
|
@ -14,21 +14,21 @@ public:
|
||||||
virtual std::map<std::string, eventfptr> getEvents();
|
virtual std::map<std::string, eventfptr> getEvents();
|
||||||
};
|
};
|
||||||
|
|
||||||
void MyPlugin::onLoad() {
|
void EventsPlugin::onLoad() {
|
||||||
std::cout << "onload();" << std::endl;
|
std::cout << "Events::onload();" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MyPlugin::onEnable() {
|
void EventsPlugin::onEnable() {
|
||||||
std::cout << "onEnable();" << std::endl;
|
std::cout << "Events::onEnable();" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MyPlugin::onDisable() {
|
void EventsPlugin::onDisable() {
|
||||||
std::cout << "onDisable();" << std::endl;
|
std::cout << "Events::onDisable();" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
CppPlugin* init() {
|
CppPlugin* init() {
|
||||||
std::cout << "init im plugin!" << std::endl;
|
std::cout << "init im plugin!" << std::endl;
|
||||||
return new MyPlugin();
|
return new EventsPlugin();
|
||||||
}
|
}
|
||||||
|
|
||||||
void eventJoin(JNIEnv* env, CppPlugin* pl, Event* e) {
|
void eventJoin(JNIEnv* env, CppPlugin* pl, Event* e) {
|
||||||
|
@ -43,8 +43,8 @@ void eventQuit(JNIEnv* env, CppPlugin* pl, Event* e) {
|
||||||
std::cout << "quit " << e->getName() << std::endl;
|
std::cout << "quit " << e->getName() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map<std::string, eventfptr> MyPlugin::getEvents() {
|
std::map<std::string, eventfptr> EventsPlugin::getEvents() {
|
||||||
std::cout << "MyPlugin::getEvents" << std::endl;
|
std::cout << "EventsPlugin::getEvents" << std::endl;
|
||||||
std::map<std::string, eventfptr> out;
|
std::map<std::string, eventfptr> out;
|
||||||
out.insert({"PlayerJoinEvent", eventJoin});
|
out.insert({"PlayerJoinEvent", eventJoin});
|
||||||
out.insert({"PlayerQuitEvent", eventQuit});
|
out.insert({"PlayerQuitEvent", eventQuit});
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
||||||
class MyPlugin : public CppPlugin {
|
class SimplePlugin : public CppPlugin {
|
||||||
public:
|
public:
|
||||||
virtual void onLoad();
|
virtual void onLoad();
|
||||||
virtual void onEnable();
|
virtual void onEnable();
|
||||||
|
@ -14,21 +14,21 @@ public:
|
||||||
//virtual std::map<std::string, eventfptr> getEvents();
|
//virtual std::map<std::string, eventfptr> getEvents();
|
||||||
};
|
};
|
||||||
|
|
||||||
void MyPlugin::onLoad() {
|
void SimplePlugin::onLoad() {
|
||||||
std::cout << "onload();" << std::endl;
|
std::cout << "onload();" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MyPlugin::onEnable() {
|
void SimplePlugin::onEnable() {
|
||||||
std::cout << "onEnable();" << std::endl;
|
std::cout << "onEnable();" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MyPlugin::onDisable() {
|
void SimplePlugin::onDisable() {
|
||||||
std::cout << "onDisable();" << std::endl;
|
std::cout << "onDisable();" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
CppPlugin* init() {
|
CppPlugin* init() {
|
||||||
std::cout << "init im plugin!" << std::endl;
|
std::cout << "init im plugin!" << std::endl;
|
||||||
return new MyPlugin();
|
return new SimplePlugin();
|
||||||
}
|
}
|
||||||
|
|
||||||
}//extern "C"
|
}//extern "C"
|
Loading…
Reference in New Issue