Changeset 892
- Timestamp:
- 03/05/10 01:24:09 (5 months ago)
- Location:
- trunk/client/Game2
- Files:
-
- 2 modified
-
SysConsole.cpp (modified) (7 diffs)
-
SysConsole.h (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/client/Game2/SysConsole.cpp
r887 r892 39 39 using namespace HoverRace::Script; 40 40 41 namespace {42 const char ON_INIT_REG_KEY = 'A';43 }44 45 41 SysConsole::SysConsole() : 46 SUPER() 42 SUPER(), onInitRef(-1) 47 43 { 48 44 } … … 50 46 SysConsole::~SysConsole() 51 47 { 48 if (onInitRef >= 0) { 49 lua_State *state = GetScripting()->GetState(); 50 luaL_unref(state, LUA_REGISTRYINDEX, onInitRef); 51 } 52 } 53 54 void SysConsole::InitEnv(Script::Env *scripting) 55 { 56 SUPER::InitEnv(scripting); 57 onInitRef = luaL_ref(scripting->GetState(), LUA_REGISTRYINDEX); 52 58 } 53 59 … … 58 64 lua_State *state = scripting->GetState(); 59 65 60 lua_pushcclosure(state, SysConsole::LOnInit, 0); 66 lua_pushlightuserdata(state, this); 67 lua_pushcclosure(state, SysConsole::LOnInit, 1); 61 68 lua_setglobal(state, "on_init"); 62 69 63 lua_pushcclosure(state, SysConsole::LGetOnInit, 0); 70 lua_pushlightuserdata(state, this); 71 lua_pushcclosure(state, SysConsole::LGetOnInit, 1); 64 72 lua_setglobal(state, "get_on_init"); 65 73 66 74 // Initial arrays for callbacks. 67 lua_pushlightuserdata(state, (void*)&ON_INIT_REG_KEY);68 75 lua_newtable(state); 69 lua_ settable(state, LUA_REGISTRYINDEX);76 lua_rawseti(state, LUA_REGISTRYINDEX, onInitRef); 70 77 } 71 78 … … 116 123 lua_State *state = GetScripting()->GetState(); 117 124 118 lua_pushlightuserdata(state, (void*)&ON_INIT_REG_KEY); 119 lua_gettable(state, LUA_REGISTRYINDEX); // table 125 lua_rawgeti(state, LUA_REGISTRYINDEX, onInitRef); // table 120 126 121 127 lua_pushnil(state); // table nil … … 132 138 // function on_init(f) 133 139 // Register a callback function for when the game starts up. 140 SysConsole *self = static_cast<SysConsole*>(lua_touserdata(state, lua_upvalueindex(1))); 134 141 135 142 if (!lua_isfunction(state, 1)) { … … 138 145 } 139 146 140 lua_pushlightuserdata(state, (void*)&ON_INIT_REG_KEY); 141 lua_gettable(state, LUA_REGISTRYINDEX); // table 147 lua_rawgeti(state, LUA_REGISTRYINDEX, self->onInitRef); // table 142 148 143 149 lua_pushinteger(state, lua_objlen(state, -1) + 1); // table key … … 154 160 // function get_on_init() 155 161 // Returns the table of on_init callbacks (for debugging purposes). 162 SysConsole *self = static_cast<SysConsole*>(lua_touserdata(state, lua_upvalueindex(1))); 156 163 157 lua_pushlightuserdata(state, (void*)&ON_INIT_REG_KEY); 158 lua_gettable(state, LUA_REGISTRYINDEX); // table 164 lua_rawgeti(state, LUA_REGISTRYINDEX, self->onInitRef); // table 159 165 160 166 return 1; -
trunk/client/Game2/SysConsole.h
r880 r892 37 37 38 38 protected: 39 virtual void InitEnv(Script::Env *scripting); 39 40 virtual void InitGlobals(Script::Env *scripting); 40 41 … … 56 57 static int LOnInit(lua_State *state); 57 58 static int LGetOnInit(lua_State *state); 59 60 private: 61 int onInitRef; 58 62 }; 59 63
