Skip to content

Instantly share code, notes, and snippets.

@FurryHead
Created June 28, 2011 21:54
Show Gist options
  • Save FurryHead/1052333 to your computer and use it in GitHub Desktop.
Save FurryHead/1052333 to your computer and use it in GitHub Desktop.
void PluginManager::fire(string event, int numargs, ...) {
lua_getglobal(L, "plugins");
int table_index = lua_gettop(L);
if(!lua_istable(L, table_index)) {
lua_pop(L, 1);
lua_newtable(L);
lua_setglobal(L, "plugins");
return;
}
lua_pushnil(L);
while (lua_next(L, table_index) != 0) {
/* key is at -2, value at -1 */
int value = lua_gettop(L), key = value-1;
if (lua_isstring(L, -2)) {
lua_getfield(L, value, ("handle_"+event).c_str());
if (lua_isfunction(L, -1)) {
va_list args;
va_start(args, numargs);
int size;
string *str;
for (int i=0;i<numargs;i++) {
size = va_arg(args, int);
if (size > 0) {
str = va_arg(args, string*);
lua_newtable(L);
int table_index = lua_gettop(L);
cout << "Making table..." << endl;
for (int j = 0; j < size; j++) {
cout << "1Debug: " << str[j] << endl;
lua_pushstring(L,str[j].c_str());
lua_rawseti(L,table_index,j+1);
}
} else {
str = va_arg(args, string*);
cout << "Debug: " << *str << endl;
lua_pushstring(L, str->c_str());
}
}
int code = lua_pcall(L, numargs, 0, 0);
if (code != 0) {
c_print(string("Plugin ")+lua_tostring(L, key)+string(" handle_"+event+" error: ")+lua_tostring(L, -1));
lua_pop(L, 1);
}
va_end(args);
}
}
lua_pop(L, 1);
}
lua_pop(L, 1);
}void PluginManager::fire(string event, int numargs, ...) {
lua_getglobal(L, "plugins");
int table_index = lua_gettop(L);
if(!lua_istable(L, table_index)) {
lua_pop(L, 1);
lua_newtable(L);
lua_setglobal(L, "plugins");
return;
}
lua_pushnil(L);
while (lua_next(L, table_index) != 0) {
/* key is at -2, value at -1 */
int value = lua_gettop(L), key = value-1;
if (lua_isstring(L, -2)) {
lua_getfield(L, value, ("handle_"+event).c_str());
if (lua_isfunction(L, -1)) {
va_list args;
va_start(args, numargs);
int size;
string *str;
for (int i=0;i<numargs;i++) {
size = va_arg(args, int);
if (size > 0) {
str = va_arg(args, string*);
lua_newtable(L);
int table_index = lua_gettop(L);
cout << "Making table..." << endl;
for (int j = 0; j < size; j++) {
cout << "1Debug: " << str[j] << endl;
lua_pushstring(L,str[j].c_str());
lua_rawseti(L,table_index,j+1);
}
} else {
str = va_arg(args, string*);
cout << "Debug: " << *str << endl;
lua_pushstring(L, str->c_str());
}
}
int code = lua_pcall(L, numargs, 0, 0);
if (code != 0) {
c_print(string("Plugin ")+lua_tostring(L, key)+string(" handle_"+event+" error: ")+lua_tostring(L, -1));
lua_pop(L, 1);
}
va_end(args);
}
}
lua_pop(L, 1);
}
lua_pop(L, 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment