diff options
author | Thorbjørn Lindeijer <thorbjorn@lindeijer.nl> | 2012-03-05 20:53:18 +0100 |
---|---|---|
committer | Thorbjørn Lindeijer <thorbjorn@lindeijer.nl> | 2012-03-07 22:10:00 +0100 |
commit | 99227026607ecfaca1ff07705930a83d41c16042 (patch) | |
tree | 49153afd9f05d65c2102ff974f763342eb59684e /src/scripting/luautil.cpp | |
parent | b0225ccd7997e900d4276eacc1541ec80fadbb48 (diff) | |
download | manaserv-99227026607ecfaca1ff07705930a83d41c16042.tar.gz manaserv-99227026607ecfaca1ff07705930a83d41c16042.tar.bz2 manaserv-99227026607ecfaca1ff07705930a83d41c16042.tar.xz manaserv-99227026607ecfaca1ff07705930a83d41c16042.zip |
Added a function that returns the current map or raises an error
The new function 'checkCurrentMap' will raise an error when no current map has
been set, eliminating the need to do custom error handling all over the place.
This also fixes several functions that would otherwise have simply crashed
when there was no current map.
Also cleaned up some "empty string parameter" checks.
Reviewed-by: Erik Schilling
Diffstat (limited to 'src/scripting/luautil.cpp')
-rw-r--r-- | src/scripting/luautil.cpp | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/scripting/luautil.cpp b/src/scripting/luautil.cpp index 217fc60c..0a49e834 100644 --- a/src/scripting/luautil.cpp +++ b/src/scripting/luautil.cpp @@ -29,6 +29,8 @@ #include "utils/logger.h" +#include "scripting/luascript.h" + void raiseScriptError(lua_State *s, const char *format, ...) { @@ -112,6 +114,16 @@ void UserDataCache::insert(lua_State *s, void *object) } +Script *getScript(lua_State *s) +{ + lua_pushlightuserdata(s, (void *)&LuaScript::registryKey); + lua_gettable(s, LUA_REGISTRYINDEX); + Script *script = static_cast<Script *>(lua_touserdata(s, -1)); + lua_pop(s, 1); + return script; +} + + /* Functions below are unsafe, as they assume the script has passed pointers to objects which have not yet been destroyed. If the script never keeps pointers around, there will be no problem. In order to be safe, the engine @@ -241,6 +253,19 @@ NPC *checkNPC(lua_State *s, int p) } +MapComposite *checkCurrentMap(lua_State *s, Script *script /* = 0 */) +{ + if (!script) + script = getScript(s); + + MapComposite *mapComposite = script->getMap(); + if (!mapComposite) + luaL_error(s, "no current map"); + + return mapComposite; +} + + void push(lua_State *s, int val) { lua_pushinteger(s, val); |