static lua_State *init(){ lua_State *L = luaL_newstate(); luaL_openlibs(L); //注册C函数,常量到lua reg_common_c_function(L); //注册group特有的函数 reg_group_c_function(L); if (luaL_dofile(L,"script/handler.lua")) { const char * error = lua_tostring(L, -1); lua_pop(L,1); LOG_GROUP(LOG_INFO,"error on handler.lua:%s\n",error); printf("error on handler.lua:%s\n",error); lua_close(L); return NULL; } //注册lua消息处理器 if(CALL_LUA_FUNC(L,"reghandler",1)){ const char * error = lua_tostring(L, -1); lua_pop(L,1); LOG_GROUP(LOG_INFO,"error on reghandler:%s\n",error); printf("error on reghandler:%s\n",error); lua_close(L); } if(!lua_toboolean(L,1)){ LOG_GROUP(LOG_ERROR,"reghandler failed\n"); printf("reghandler failed\n"); return NULL; } return L; }
battleservice_t new_battleservice() { lua_State *L = luaL_newstate(); luaL_openlibs(L); if (luaL_dofile(L,"battlemgr.lua")) { const char * error = lua_tostring(L, -1); lua_pop(L,1); printf("%s\n",error); exit(0); } register_battle_cfunction(L); battleservice_t bservice = calloc(1,sizeof(*bservice)); if(0 != CALL_LUA_FUNC(L,"CreateBattleMgr",1)) { const char * error = lua_tostring(L, -1); lua_pop(L,1); printf("%s\n",error); exit(0); } bservice->battlemgr = create_luaObj(L,-1); bservice->msgdisp = new_msgdisp(NULL,NULL,NULL, NULL,battle_processpacket,NULL); bservice->thd = create_thread(THREAD_JOINABLE); thread_start_run(bservice->thd,service_main,(void*)bservice); return bservice; }