- fixed lua 5.2 code for save / load games

- cmake now prefers lua 5.2 over 5.1
This commit is contained in:
Mark Vejvoda 2013-02-16 21:20:09 +00:00
parent 85b1365e7e
commit 87a4eb49e4
2 changed files with 22 additions and 11 deletions

View File

@ -21,6 +21,7 @@ ENDIF(LUA_INCLUDE_DIR AND LUA_LIBRARIES)
FIND_PATH(LUA_INCLUDE_DIR NAMES lua.hpp
PATHS /usr/include
/usr/include/lua
/usr/include/lua5.2
/usr/include/lua5.1
IF(FreeBSD)
SET(PATHS "/usr/local/include/lua51")
@ -29,7 +30,7 @@ FIND_PATH(LUA_INCLUDE_DIR NAMES lua.hpp
)
IF (LUA_STATIC AND NOT LUA_LIBRARIES)
FIND_LIBRARY(LUA_LIBRARIES NAMES liblua5.1.a liblua.a lua5.1 lua
FIND_LIBRARY(LUA_LIBRARIES NAMES liblua5.2.a liblua5.1.a liblua.a lua5.1 lua
PATHS
IF(FreeBSD)
SET(PATHS "/usr/local/lib/lua51")
@ -37,7 +38,7 @@ IF (LUA_STATIC AND NOT LUA_LIBRARIES)
$ENV{LUA_HOME})
ELSE()
FIND_LIBRARY(LUA_LIBRARIES NAMES lua5.1 lua
FIND_LIBRARY(LUA_LIBRARIES NAMES lua5.2 lua5.1 lua
PATHS
IF(FreeBSD)
SET(PATHS "/usr/local/lib/lua51")

View File

@ -121,16 +121,18 @@ void LuaScript::DumpGlobals()
{
LuaHandle *L = luaState;
// push the first key (nil = beginning of table)
#if LUA_VERSION_NUM <= 501
lua_pushnil(L);
#endif
// lua_next will:
// 1 - pop the key
// 2 - push the next key
// 3 - push the value at that key
// ... so the key will be at index -2 and the value at index -1
#if LUA_VERSION_NUM > 501
lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS);
for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) {
lua_pushglobaltable(L);
lua_pushnil(L);
while (lua_next(L, -2) != 0) {
#else
while (lua_next(L, LUA_GLOBALSINDEX) != 0) {
#endif
@ -197,10 +199,13 @@ void LuaScript::DumpGlobals()
// call luaL_dostring with that line.
//SaveLine(key_string + " = " + value_string); // Pop the value so the index remains on top of the stack for the next iteration
printf("Found global LUA var: %s = %s\n",key_string.c_str(),value_string.c_str());
#if LUA_VERSION_NUM <= 501
lua_pop(L, 1);
}
#if LUA_VERSION_NUM > 501
lua_pop(L, 1);
#endif
}
}
void LuaScript::saveGame(XmlNode *rootNode) {
@ -210,7 +215,9 @@ void LuaScript::saveGame(XmlNode *rootNode) {
//try{
LuaHandle *L = luaState;
// push the first key (nil = beginning of table)
#if LUA_VERSION_NUM <= 501
lua_pushnil(L);
#endif
// lua_next will:
// 1 - pop the key
@ -219,8 +226,9 @@ void LuaScript::saveGame(XmlNode *rootNode) {
// ... so the key will be at index -2 and the value at index -1
#if LUA_VERSION_NUM > 501
lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS);
for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) {
lua_pushglobaltable(L);
lua_pushnil(L);
while (lua_next(L, -2) != 0) {
#else
while (lua_next(L, LUA_GLOBALSINDEX) != 0) {
#endif
@ -387,10 +395,12 @@ void LuaScript::saveGame(XmlNode *rootNode) {
luaScriptNode->addAttribute("value_type",intToStr(value_type), mapTagReplacements);
}
}
#if LUA_VERSION_NUM <= 501
lua_pop(L, 1);
}
#if LUA_VERSION_NUM > 501
lua_pop(L, 1);
#endif
}
//}
//catch(const exception &ex) {