Compare commits
2 Commits
74935055d0
...
90e2de5ed9
Author | SHA1 | Date |
---|---|---|
MrBesen | 90e2de5ed9 | |
MrBesen | 48b0a4b7d9 |
10
Makefile
10
Makefile
|
@ -17,18 +17,18 @@ plugins: $(INSTALLDIR)$(NAME)
|
|||
installplugins: plugins
|
||||
bash testplugins/install.sh
|
||||
|
||||
install: $(INSTALLDIR)$(NAME)
|
||||
install: $(BUILDDIR)$(NAME)
|
||||
|
||||
uninstall:
|
||||
sudo rm -f $(INSTALLDIR)$(NAME)
|
||||
|
||||
link:
|
||||
sudo ln -s $(realpath $(BUILDDIR)$(NAME)) $(INSTALLDIR)
|
||||
link: $(BUILDDIR)$(NAME)
|
||||
sudo ln -fs $(realpath $(BUILDDIR)$(NAME)) $(INSTALLDIR)
|
||||
|
||||
$(INSTALLDIR)$(NAME): $(BUILDDIR)$(NAME)
|
||||
cp $(BUILDDIR)$(NAME) $(INSTALLDIR)
|
||||
|
||||
all: createhfiles $(BUILDDIR)$(NAME)
|
||||
all: createhfiles link
|
||||
|
||||
$(BUILDDIR)%.so: $(SRCDIR)%.cpp $(BUILDDIR)
|
||||
$(CXX) -fPIC -shared $(CFLAGS) $(SRCDIR)*.cpp -o $@ $(LDFLAGS)
|
||||
|
@ -38,7 +38,7 @@ $(BUILDDIR)%: $(SRCDIR)%.cpp $(BUILDDIR)
|
|||
|
||||
clean:
|
||||
$(RM) -r $(BUILDDIR)
|
||||
$(RM) $(SERVER)plugins/*.so $(SERVER)plugins/CppPlugins/*.so testplugins/*/*.so
|
||||
$(RM) $(SERVER)plugins/*.so $(SERVER)plugins/CppPlugins/*.so testplugins/*/*.so
|
||||
|
||||
$(BUILDDIR):
|
||||
mkdir -p $@
|
||||
|
|
|
@ -2,13 +2,32 @@
|
|||
|
||||
#include <jni.h>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#define DEBUG 0
|
||||
#define COLORS 0
|
||||
|
||||
#if COLORS == 1
|
||||
#define CBLUE std::string("\033[94m")
|
||||
#define CRED std::string("\033[91m")
|
||||
#define CYELLOW std::string("\033[93m")
|
||||
#define CGREEN std::string("\033[92m")
|
||||
#define CRESET std::string("\033[0m")
|
||||
#define CGREY std::string("\033[90m")
|
||||
#else
|
||||
#define CBLUE std::string()
|
||||
#define CRED std::string()
|
||||
#define CYELLOW std::string()
|
||||
#define CGREEN std::string()
|
||||
#define CRESET std::string()
|
||||
#define CGREY std::string()
|
||||
#endif
|
||||
|
||||
#define DEBUG 1
|
||||
|
||||
#if DEBUG == 1
|
||||
#define DEB(A) std::cout << A << std::endl
|
||||
#define DEB(A) std::cout << A << CRESET << std::endl
|
||||
#else
|
||||
#define DEB(A) ;
|
||||
#define DEB(A) ;
|
||||
#endif
|
||||
|
||||
namespace Plugin {
|
||||
|
|
|
@ -29,6 +29,8 @@ public:
|
|||
virtual void onDisable(JNIEnv*);
|
||||
virtual std::map<std::string, eventfptr> getEvents();
|
||||
virtual ~CppPlugin();
|
||||
void printStacktrace();
|
||||
void log(std::string msg);
|
||||
};
|
||||
|
||||
bool registerCmd(JNIEnv* env, CppPlugin* plugin, std::string& cname, cmdfptr function);
|
||||
|
|
|
@ -59,7 +59,7 @@ void Event::reapply(JNIEnv* env, jobject jevent) {
|
|||
env->CallObjectMethod(map, MAPCLEAR);
|
||||
|
||||
for(auto it : data) {
|
||||
std::cout << "write back to java: " << it.first << " Value: " << it.second << std::endl;
|
||||
DEB(CYELLOW << "write back to java: " << it.first << " Value: " << CGREEN<< it.second);
|
||||
jstring key = env->NewStringUTF(it.first.c_str());
|
||||
|
||||
env->CallObjectMethod(map, MAPPUT, key, it.second);
|
||||
|
|
|
@ -111,7 +111,7 @@ void load(JNIEnv* env, long data) {
|
|||
+ (!EVENTDATAF) + (!EVENTNAMEF) + (!PLUGINIDF) + (!BUKKITSERVER) + (!SERVERCMDMAP)
|
||||
+ (!MAPGET) + (!MAPPUT) + (!MAPCLEAR) + (!LISTENERCONTR) + (!LISTENERSMALLCONTR) + (!CMDCONSTR) + (!CMDMAPREGISTER)) != 0) {
|
||||
|
||||
DEB("Error loading a Java Information:");
|
||||
DEB(CRED << "Error loading a Java Information:");
|
||||
DEB("find classes: " << (!!CPPPLUGINCLASS) << (!!EVENTCLASS) << (!!MAPCLASS) << (!!LISTENERCLASS) << (!!CMDCLASS) << (!!BUKKITCLASS) << (!!CBSERVERCLASS) << (!!CMDMAPCLASS));
|
||||
DEB("find fields: " << (!!EVENTDATAF) << (!!EVENTNAMEF) << (!!PLUGINIDF) << (!!BUKKITSERVER) << (!!SERVERCMDMAP));
|
||||
DEB("find methods: " << (!!MAPGET) << (!!MAPPUT) << (!!MAPCLEAR) << (!!LISTENERCONTR) << (!!LISTENERSMALLCONTR) << (!!CMDCONSTR) << (!!CMDMAPREGISTER));
|
||||
|
@ -119,7 +119,7 @@ void load(JNIEnv* env, long data) {
|
|||
}
|
||||
|
||||
bool registerCmd(JNIEnv* env, CppPlugin* plugin, std::string& cname, cmdfptr function) {
|
||||
DEB("Register Cmd " + cname + " for " + plugin->name);
|
||||
DEB(CYELLOW << "Register Cmd " << CGREEN << cname << CYELLOW << " for " << CGREEN << plugin->name);
|
||||
load(env, JNI_BUKKITCLASS | JNI_SERVERCMDMAPFIELD | JNI_CMDCONSTR | JNI_CMDMAPREGISTERMETH);
|
||||
|
||||
// create cnd
|
||||
|
@ -163,7 +163,7 @@ JNIEXPORT jboolean JNICALL Java_de_mrbesen_cppplugins_CppPlugin_loadPlugin(JNIEn
|
|||
const char* cpath = env->GetStringUTFChars(path, 0);
|
||||
|
||||
//open plugin
|
||||
DEB("open plugin " << cpath);
|
||||
DEB(CBLUE << "open plugin " << cpath);
|
||||
void* handle = dlopen(cpath, RTLD_LAZY);
|
||||
if (!handle) {
|
||||
std::cout << "could not create handle of: " << cpath << " error: " << dlerror() << std::endl;
|
||||
|
@ -180,25 +180,25 @@ JNIEXPORT jboolean JNICALL Java_de_mrbesen_cppplugins_CppPlugin_loadPlugin(JNIEn
|
|||
}
|
||||
|
||||
Plugin::CppPlugin* plugin = initf();
|
||||
DEB("Initilized");
|
||||
DEB(CGREY << "Initilized");
|
||||
|
||||
plugin->handle = handle;
|
||||
|
||||
//set name / handle id
|
||||
plugin->name = std::string(cpath);
|
||||
plugin->name = plugin->name.substr(plugin->name.rfind('/')+1);
|
||||
DEB("name set");
|
||||
DEB(CGREY << "name set");
|
||||
|
||||
// add to vector
|
||||
Plugin::plugins.push_back(plugin);
|
||||
int id = Plugin::plugins.size()-1;
|
||||
plugin->id = id;
|
||||
DEB("plugin added to vector");
|
||||
DEB(CGREY << "plugin added to vector");
|
||||
|
||||
//store id
|
||||
Plugin::load(env,Plugin:: JNI_ALL);
|
||||
env->SetIntField(thisobj,Plugin:: PLUGINIDF, id);
|
||||
DEB("Internal id set");
|
||||
DEB(CGREY << "Internal id set");
|
||||
|
||||
return (bool) plugin; // converted to boolean here
|
||||
}
|
||||
|
@ -209,7 +209,7 @@ JNIEXPORT jboolean JNICALL Java_de_mrbesen_cppplugins_CppPlugin_loadPlugin(JNIEn
|
|||
* Signature: ()V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_de_mrbesen_cppplugins_CppPlugin_onLoad(JNIEnv* env, jobject thisobj) {
|
||||
DEB("lib::onLoad()");
|
||||
DEB(CGREY << "lib::onLoad()");
|
||||
Plugin::getPlugin(env, thisobj)->onLoad(env);
|
||||
}
|
||||
|
||||
|
@ -219,7 +219,7 @@ JNIEXPORT void JNICALL Java_de_mrbesen_cppplugins_CppPlugin_onLoad(JNIEnv* env,
|
|||
* Signature: ()V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_de_mrbesen_cppplugins_CppPlugin_onEnable_1(JNIEnv* env, jobject thisobj) {
|
||||
DEB("lib::onEnable()");
|
||||
DEB(CGREY << "lib::onEnable()");
|
||||
Plugin::getPlugin(env, thisobj)->onEnable(env);
|
||||
}
|
||||
|
||||
|
@ -230,7 +230,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) {
|
||||
//TODO
|
||||
DEB("lib::onDisable()" );
|
||||
DEB(CGREY << "lib::onDisable()");
|
||||
Plugin::CppPlugin* plugin = Plugin::getPlugin(env, thisobj);
|
||||
plugin->onDisable(env);
|
||||
dlclose(plugin->handle);
|
||||
|
@ -242,9 +242,9 @@ JNIEXPORT void JNICALL Java_de_mrbesen_cppplugins_CppPlugin_onDisable_1(JNIEnv*
|
|||
* Signature: (Lde/mrbesen/cppplugins/CppEvent;I)Lde/mrbesen/cppplugins/CppEvent;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_de_mrbesen_cppplugins_CppPlugin_fireEvent(JNIEnv* env, jobject thisobj, jobject jevent, jlong functionid) {
|
||||
DEB("lib::fireEvent()" );
|
||||
DEB(CGREY << "lib::fireEvent()");
|
||||
if(functionid == 0) {
|
||||
DEB("invalid function id");
|
||||
DEB(CRED << "invalid function id");
|
||||
return jevent;
|
||||
}
|
||||
|
||||
|
@ -256,8 +256,16 @@ JNIEXPORT jobject JNICALL Java_de_mrbesen_cppplugins_CppPlugin_fireEvent(JNIEnv*
|
|||
//cast event
|
||||
Plugin::Event event(env, jevent);
|
||||
|
||||
//get Plugin
|
||||
Plugin::CppPlugin* plugin = Plugin::getPlugin(env, thisobj);
|
||||
|
||||
//call it
|
||||
f(env, Plugin::getPlugin(env, thisobj), &event);
|
||||
try {
|
||||
f(env, plugin, &event);
|
||||
} catch(...) {
|
||||
std::cout << CRED << "Error while processing Event " << event.getName() << " in plugin: " << plugin->name << CRESET << std::endl;
|
||||
return jevent;
|
||||
}
|
||||
|
||||
//put changed fields back in the jevent
|
||||
event.reapply(env, jevent);
|
||||
|
@ -291,7 +299,7 @@ JNIEXPORT jboolean JNICALL Java_de_mrbesen_cppplugins_CppPlugin_onCmd(JNIEnv* en
|
|||
try {
|
||||
result = cmdf(env, plugin, sender, cmd, clabel, cargs);
|
||||
} catch(...) {
|
||||
std::cout << "Error while executing Command " << clabel << " in plugin: " << plugin->name << std::endl;
|
||||
std::cout << CRED << "Error while executing Command " << clabel << " in plugin: " << plugin->name << CRESET << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -308,13 +316,13 @@ JNIEXPORT jobjectArray JNICALL Java_de_mrbesen_cppplugins_CppPlugin_getListener(
|
|||
|
||||
//call getEvents
|
||||
std::map<std::string, Plugin::eventfptr> events = Plugin::getPlugin(env, thisobj)->getEvents();
|
||||
DEB("found " << events.size() << " Events");
|
||||
DEB(CYELLOW << "found " << events.size() << " Events" );
|
||||
|
||||
//create array
|
||||
jobject initial = env->NewObject(Plugin::LISTENERCLASS, Plugin::LISTENERSMALLCONTR);
|
||||
jobjectArray arr = env->NewObjectArray(events.size(), Plugin::LISTENERCLASS, initial);
|
||||
|
||||
DEB("populate array");
|
||||
DEB(CGREY << "populate array" );
|
||||
//populate array
|
||||
int i = 0;
|
||||
for(auto it : events) {
|
||||
|
@ -327,7 +335,7 @@ JNIEXPORT jobjectArray JNICALL Java_de_mrbesen_cppplugins_CppPlugin_getListener(
|
|||
env->SetObjectArrayElement(arr, i++, listener);
|
||||
}
|
||||
|
||||
DEB("Array finished");
|
||||
DEB(CGREY << "Array finished" );
|
||||
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,11 @@
|
|||
#include <plugin.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <execinfo.h> //backtrace
|
||||
#include <time.h> //time logging
|
||||
#include <stdio.h> //snprintf for time logging
|
||||
|
||||
#define STACKTRACEBUFFER 100
|
||||
|
||||
namespace Plugin {
|
||||
|
||||
|
@ -17,7 +22,7 @@ void CppPlugin::onDisable(JNIEnv*) {
|
|||
}
|
||||
|
||||
std::map<std::string, eventfptr> CppPlugin::getEvents() {
|
||||
std::cout << "Defaults::getEvents()" << std::endl;
|
||||
//std::cout << "Defaults::getEvents()" << std::endl;
|
||||
return std::map<std::string, eventfptr>();
|
||||
}
|
||||
|
||||
|
@ -25,5 +30,41 @@ CppPlugin::~CppPlugin() {
|
|||
|
||||
}
|
||||
|
||||
void CppPlugin::printStacktrace() {
|
||||
//create stacktrace
|
||||
void *buffer[STACKTRACEBUFFER];
|
||||
int count = backtrace(buffer, STACKTRACEBUFFER)-1; //dont print current frame
|
||||
|
||||
std::cout << "Plugin: " << name << " Creating Stacktrace: ";
|
||||
std::cout << count << " Frames traced\n";
|
||||
|
||||
char** strings = nullptr;
|
||||
|
||||
strings = backtrace_symbols(buffer+1, count);
|
||||
|
||||
if(strings == nullptr) {
|
||||
std::cout << "Error, could not read symbols - compiled with \"-g\"?" << std::endl;
|
||||
} else {
|
||||
for(int i = 0; i < count; i++) {
|
||||
std::cout << strings[i] << "\n";
|
||||
}
|
||||
free(strings);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void CppPlugin::log(std::string msg) {
|
||||
//create time stamp
|
||||
time_t rawt;
|
||||
struct tm *timeinfo;
|
||||
time(&rawt); //get time
|
||||
timeinfo = localtime(&rawt);
|
||||
size_t length = name.length() + 22;
|
||||
char *timebuf = new char[length];
|
||||
snprintf(timebuf, length, "\r[%02d:%02d:%02d INFO]: [%s] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, name.c_str());
|
||||
std::cout << timebuf << msg << std::endl;
|
||||
|
||||
free(timebuf);
|
||||
}
|
||||
|
||||
} //namespace
|
||||
|
|
|
@ -17,15 +17,15 @@ public:
|
|||
};
|
||||
|
||||
void SimplePlugin::onLoad(JNIEnv*) {
|
||||
std::cout << "SimplePLugin::onLoad();" << std::endl;
|
||||
log("SimplePLugin::onLoad();");
|
||||
}
|
||||
|
||||
void SimplePlugin::onEnable(JNIEnv*) {
|
||||
std::cout << "SimplePLugin::onEnable();" << std::endl;
|
||||
log("SimplePLugin::onEnable();");
|
||||
}
|
||||
|
||||
void SimplePlugin::onDisable(JNIEnv*) {
|
||||
std::cout << "SimplePLugin::onDisable();" << std::endl;
|
||||
log("SimplePLugin::onDisable();");
|
||||
}
|
||||
|
||||
CppPlugin* init() {
|
||||
|
|
|
@ -17,15 +17,15 @@ public:
|
|||
};
|
||||
|
||||
void EventsPlugin::onLoad(JNIEnv*) {
|
||||
std::cout << "Events::onLoad();" << std::endl;
|
||||
log("Events::onLoad();");
|
||||
}
|
||||
|
||||
void EventsPlugin::onEnable(JNIEnv*) {
|
||||
std::cout << "Events::onEnable();" << std::endl;
|
||||
log("Events::onEnable();");
|
||||
}
|
||||
|
||||
void EventsPlugin::onDisable(JNIEnv*) {
|
||||
std::cout << "Events::onDisable();" << std::endl;
|
||||
log("Events::onDisable();");
|
||||
}
|
||||
|
||||
CppPlugin* init() {
|
||||
|
@ -35,17 +35,17 @@ CppPlugin* init() {
|
|||
void eventJoin(JNIEnv* env, CppPlugin* pl, Event* e) {
|
||||
jstring msg = (jstring) e->getData(env, "joinMessage");
|
||||
std::string jmsg(env->GetStringUTFChars(msg, 0));
|
||||
std::cout << "join " << e->getName() << " old join message: " << jmsg << std::endl;
|
||||
pl->log("join " + e->getName() + " old join message: " + jmsg);
|
||||
jstring newmsg = env->NewStringUTF("Ein Spieler ist dem Spiel beigetreten!");
|
||||
e->setData("joinMessage", newmsg);
|
||||
}
|
||||
|
||||
void eventQuit(JNIEnv* env, CppPlugin* pl, Event* e) {
|
||||
std::cout << "quit " << e->getName() << std::endl;
|
||||
pl->log("quit " + e->getName());
|
||||
}
|
||||
|
||||
std::map<std::string, eventfptr> EventsPlugin::getEvents() {
|
||||
std::cout << "EventsPlugin::getEvents" << std::endl;
|
||||
log("EventsPlugin::getEvents");
|
||||
std::map<std::string, eventfptr> out;
|
||||
out.insert({"PlayerJoinEvent", eventJoin});
|
||||
out.insert({"PlayerQuitEvent", eventQuit});
|
||||
|
|
|
@ -10,7 +10,7 @@ using namespace Plugin;
|
|||
extern "C" {
|
||||
|
||||
bool hicmd(JNIEnv* env, CppPlugin* plugin, jobject sender, jobject cmd, std::string& label, std::vector<std::string>& args) {
|
||||
std::cout << "HICMD::hi " << label << " argsize: " << args.size() << std::endl;
|
||||
plugin->log("HICMD::hi " + label + " argsize: " + std::to_string(args.size()));
|
||||
for(std::string& arg : args) {
|
||||
std::cout << arg << std::endl;
|
||||
}
|
||||
|
@ -28,16 +28,16 @@ public:
|
|||
};
|
||||
|
||||
void CmdPlugin::onLoad(JNIEnv*) {
|
||||
std::cout << "CmdPlugin::onLoad();" << std::endl;
|
||||
log("CmdPlugin::onLoad();");
|
||||
}
|
||||
|
||||
void CmdPlugin::onEnable(JNIEnv* env) {
|
||||
std::cout << "CmdPlugin::onEnable();" << std::endl;
|
||||
log("CmdPlugin::onEnable();");
|
||||
registerCmd(env, this, "hi", hicmd);
|
||||
}
|
||||
|
||||
void CmdPlugin::onDisable(JNIEnv*) {
|
||||
std::cout << "CmdPlugin::onDisable();" << std::endl;
|
||||
log("CmdPlugin::onDisable();");
|
||||
}
|
||||
|
||||
CppPlugin* init() {
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
|
||||
CFLAGS = -Wall -pedantic-errors -std=c++17 -g
|
||||
SRCDIR = src/
|
||||
INCDIR = ../../src/main/cpp/inc
|
||||
LDFLAGS = -I/usr/lib/jvm/java-8-openjdk-amd64/include/ -I/usr/lib/jvm/java-8-openjdk-amd64/include/linux/ -I$(INCDIR)
|
||||
NAME = crashtest.so
|
||||
|
||||
all:
|
||||
g++ -fPIC -shared $(CFLAGS) $(SRCDIR)*.cpp $(LDFLAGS) -o $(NAME) -lplugin
|
||||
|
||||
#/home/yannis/git/Cppplugin/testserver/plugins/CppPlugins/libplugin.so
|
|
@ -0,0 +1,41 @@
|
|||
#include "plugin.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
using namespace Plugin;
|
||||
|
||||
extern "C" {
|
||||
|
||||
void sub(CppPlugin* plugin) {
|
||||
plugin->printStacktrace();
|
||||
plugin->log("asdf");
|
||||
throw std::string("Nope");
|
||||
}
|
||||
|
||||
bool throwcmd(JNIEnv* env, CppPlugin* plugin, jobject sender, jobject cmd, std::string& label, std::vector<std::string>& args) {
|
||||
sub(plugin);
|
||||
return true;
|
||||
}
|
||||
|
||||
class CmdPlugin : public CppPlugin {
|
||||
public:
|
||||
//virtual void onLoad(JNIEnv*);
|
||||
virtual void onEnable(JNIEnv*);
|
||||
//virtual void onDisable(JNIEnv*);
|
||||
//virtual std::map<std::string, eventfptr> getEvents();
|
||||
};
|
||||
|
||||
void CmdPlugin::onEnable(JNIEnv* env) {
|
||||
log("CmdPlugin::onEnable();");
|
||||
registerCmd(env, this, "throw", throwcmd);
|
||||
}
|
||||
|
||||
|
||||
CppPlugin* init() {
|
||||
return new CmdPlugin();
|
||||
}
|
||||
|
||||
}//extern "C"
|
Loading…
Reference in New Issue