EVENTMGR: refactored lua bindings
parent
5bb0995bf7
commit
cfcbf3ebec
|
@ -1,3 +1,3 @@
|
|||
function init ()
|
||||
event.create("GENERIC", "GENERIC")
|
||||
eventmgr.create("GENERIC", "GENERIC")
|
||||
end
|
||||
|
|
|
@ -5,7 +5,7 @@ set(SRCS
|
|||
EventConfigurationData.h
|
||||
EventId.h
|
||||
EventType.h
|
||||
LUAFunctions.h
|
||||
LUAEventMgr.h LUAEventMgr.cpp
|
||||
)
|
||||
set(LIB eventmgr)
|
||||
engine_add_module(TARGET ${LIB} SRCS ${SRCS} DEPENDENCIES commonlua network persistence)
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
#include "core/Common.h"
|
||||
#include "core/Trace.h"
|
||||
#include "io/Filesystem.h"
|
||||
#include "LUAFunctions.h"
|
||||
#include "LUAEventMgr.h"
|
||||
#include "EventMgrModels.h"
|
||||
#include "persistence/Timestamp.h"
|
||||
|
||||
|
@ -23,17 +23,7 @@ bool EventMgr::init(const core::String& luaScript) {
|
|||
return false;
|
||||
}
|
||||
|
||||
luaL_Reg create = { "create", luaCreateEventConfigurationData };
|
||||
luaL_Reg eof = { nullptr, nullptr };
|
||||
luaL_Reg funcs[] = { create, eof };
|
||||
|
||||
lua::LUAType luaEvent = _lua.registerType("EventConfigurationData");
|
||||
luaEvent.addFunction("type", luaEventConfigurationDataGetType);
|
||||
luaEvent.addFunction("name", luaEventConfigurationDataGetName);
|
||||
luaEvent.addFunction("__gc", luaEventConfigurationDataGC);
|
||||
luaEvent.addFunction("__tostring", luaEventConfigurationDataToString);
|
||||
|
||||
_lua.reg("event", funcs);
|
||||
luaeventmgr_setup(_lua, this);
|
||||
|
||||
if (!_lua.load(luaScript)) {
|
||||
Log::error("%s", _lua.error().c_str());
|
||||
|
@ -41,7 +31,6 @@ bool EventMgr::init(const core::String& luaScript) {
|
|||
}
|
||||
|
||||
// loads all the event configurations
|
||||
_lua.newGlobalData<EventMgr>("EventMgr", this);
|
||||
if (!_lua.execute("init")) {
|
||||
Log::error("%s", _lua.error().c_str());
|
||||
return false;
|
||||
|
|
|
@ -0,0 +1,91 @@
|
|||
/**
|
||||
* @file
|
||||
*/
|
||||
|
||||
#include "LUAEventMgr.h"
|
||||
#include "EventConfigurationData.h"
|
||||
#include "EventMgr.h"
|
||||
#include "commonlua/LUA.h"
|
||||
#include "commonlua/LUAFunctions.h"
|
||||
|
||||
namespace eventmgr {
|
||||
|
||||
static const char* luaeventmgr_eventmgr() {
|
||||
return "__global_eventmgr";
|
||||
}
|
||||
|
||||
static const char* luaeventmgr_metaevent() {
|
||||
return "__meta_event";
|
||||
}
|
||||
|
||||
static EventMgr* luaeventmgr_geteventmgr(lua_State *s) {
|
||||
lua_getglobal(s, luaeventmgr_eventmgr());
|
||||
EventMgr *mgr = (EventMgr *)lua_touserdata(s, -1);
|
||||
lua_pop(s, 1);
|
||||
return mgr;
|
||||
}
|
||||
|
||||
static EventConfigurationData* luaeventmgr_toevent(lua_State* s, int n) {
|
||||
return *(EventConfigurationData**)clua_getudata<EventConfigurationData*>(s, n, luaeventmgr_metaevent());
|
||||
}
|
||||
|
||||
static int luaeventmgr_create_event(lua_State * l) {
|
||||
EventMgr *ctx = luaeventmgr_geteventmgr(l);
|
||||
const char* nameId = luaL_checkstring(l, 1);
|
||||
const char *typeStr = luaL_checkstring(l, 2);
|
||||
const Type type = getType(typeStr);
|
||||
const EventConfigurationDataPtr& eventConfig = ctx->createEventConfig(nameId, type);
|
||||
if (!eventConfig) {
|
||||
return clua_error(l, "Could not create event config for id '%s'", nameId);
|
||||
}
|
||||
lua::LUA::newUserdata(l, "EventConfigurationData", eventConfig.get());
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int luaeventmgr_event_gc(lua_State * l) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int luaeventmgr_event_tostring(lua_State * l) {
|
||||
const EventConfigurationData *ctx = luaeventmgr_toevent(l, 1);
|
||||
lua_pushfstring(l, "eventconfig: %s (type: %s)", ctx->eventNameId.c_str(), network::EnumNameEventType(ctx->type));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int luaeventmgr_event_gettype(lua_State * l) {
|
||||
const EventConfigurationData *ctx = luaeventmgr_toevent(l, 1);
|
||||
lua_pushinteger(l, core::enumVal(ctx->type));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int luaeventmgr_event_getname(lua_State * l) {
|
||||
const EventConfigurationData *ctx = luaeventmgr_toevent(l, 1);
|
||||
lua_pushfstring(l, "%s", ctx->eventNameId.c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void luaeventmgr_pusheventmgr(lua_State* s, EventMgr* mgr) {
|
||||
lua_pushlightuserdata(s, mgr);
|
||||
lua_setglobal(s, luaeventmgr_eventmgr());
|
||||
}
|
||||
|
||||
void luaeventmgr_setup(lua_State* s, EventMgr* mgr) {
|
||||
static const luaL_Reg eventFuncs[] = {
|
||||
{"type", luaeventmgr_event_gettype},
|
||||
{"name", luaeventmgr_event_getname},
|
||||
{"__gc", luaeventmgr_event_gc},
|
||||
{"__tostring", luaeventmgr_event_tostring},
|
||||
{nullptr, nullptr}
|
||||
};
|
||||
clua_registerfuncs(s, eventFuncs, luaeventmgr_metaevent());
|
||||
|
||||
static const luaL_Reg attribFuncs[] = {
|
||||
{"create", luaeventmgr_create_event},
|
||||
{nullptr, nullptr}
|
||||
};
|
||||
clua_registerfuncsglobal(s, attribFuncs, luaeventmgr_eventmgr(), "eventmgr");
|
||||
|
||||
luaeventmgr_pusheventmgr(s, mgr);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
|
||||
#include "commonlua/LUA.h"
|
||||
|
||||
namespace eventmgr {
|
||||
|
||||
class EventMgr;
|
||||
|
||||
extern void luaeventmgr_setup(lua_State* s, EventMgr* mgr);
|
||||
|
||||
}
|
|
@ -1,56 +0,0 @@
|
|||
/**
|
||||
* @file
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "EventConfigurationData.h"
|
||||
#include "commonlua/LUA.h"
|
||||
#include "commonlua/LUAFunctions.h"
|
||||
|
||||
namespace eventmgr {
|
||||
|
||||
static EventMgr* luaGetContext(lua_State * l) {
|
||||
return lua::LUA::globalData<EventMgr>(l, "EventMgr");
|
||||
}
|
||||
|
||||
static EventConfigurationData* luaGetEventConfigurationDataContext(lua_State * l, int n) {
|
||||
return lua::LUA::userData<EventConfigurationData>(l, n, "EventConfigurationData");
|
||||
}
|
||||
|
||||
static int luaCreateEventConfigurationData(lua_State * l) {
|
||||
EventMgr *ctx = luaGetContext(l);
|
||||
const char* nameId = luaL_checkstring(l, 1);
|
||||
const char *typeStr = luaL_checkstring(l, 2);
|
||||
const Type type = getType(typeStr);
|
||||
const EventConfigurationDataPtr& eventConfig = ctx->createEventConfig(nameId, type);
|
||||
if (!eventConfig) {
|
||||
return clua_error(l, "Could not create event config for id '%s'", nameId);
|
||||
}
|
||||
lua::LUA::newUserdata(l, "EventConfigurationData", eventConfig.get());
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int luaEventConfigurationDataGC(lua_State * l) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int luaEventConfigurationDataToString(lua_State * l) {
|
||||
const EventConfigurationData *ctx = luaGetEventConfigurationDataContext(l, 1);
|
||||
lua_pushfstring(l, "eventconfig: %s (type: %s)", ctx->eventNameId.c_str(), network::EnumNameEventType(ctx->type));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int luaEventConfigurationDataGetType(lua_State * l) {
|
||||
const EventConfigurationData *ctx = luaGetEventConfigurationDataContext(l, 1);
|
||||
lua_pushinteger(l, core::enumVal(ctx->type));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int luaEventConfigurationDataGetName(lua_State * l) {
|
||||
const EventConfigurationData *ctx = luaGetEventConfigurationDataContext(l, 1);
|
||||
lua_pushfstring(l, "%s", ctx->eventNameId.c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,5 +1,9 @@
|
|||
function generic ()
|
||||
local event = event.create("GENERIC", "GENERIC")
|
||||
local module = {}
|
||||
|
||||
function module.register()
|
||||
local event = eventmgr.create("GENERIC", "GENERIC")
|
||||
--print(event:type())
|
||||
--print(event:name())
|
||||
end
|
||||
|
||||
return module
|
||||
|
|
|
@ -1,5 +1,10 @@
|
|||
require "event.generic"
|
||||
|
||||
function init ()
|
||||
generic()
|
||||
function init()
|
||||
local events = {
|
||||
"generic"
|
||||
}
|
||||
for key, value in ipairs(events) do
|
||||
local name = "event." .. value
|
||||
local mod = require(name)
|
||||
mod.register()
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue