mirror of
https://github.com/glest/glest-source.git
synced 2025-08-17 21:51:17 +02:00
- attempt to save and load scenario info in saved games
This commit is contained in:
@@ -140,6 +140,117 @@ void LuaScript::DumpGlobals()
|
||||
}
|
||||
}
|
||||
|
||||
void LuaScript::saveGame(XmlNode *rootNode) {
|
||||
std::map<string,string> mapTagReplacements;
|
||||
|
||||
LuaHandle *L = luaState;
|
||||
// push the first key (nil = beginning of table)
|
||||
lua_pushnil(L);
|
||||
|
||||
// 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
|
||||
while (lua_next(L, LUA_GLOBALSINDEX) != 0) {
|
||||
// get type of key and value
|
||||
int key_type = lua_type(L, -2);
|
||||
int value_type = lua_type(L, -1);
|
||||
|
||||
// support only string keys
|
||||
// globals aren't likely to have a non-string key, but just to be certain ...
|
||||
if (key_type != LUA_TSTRING) {
|
||||
lua_pop(L, 1); // pop the value so that the top contains the key for the next iteration
|
||||
continue;
|
||||
}
|
||||
|
||||
// support only number, boolean and string values
|
||||
if (value_type != LUA_TNUMBER &&
|
||||
value_type != LUA_TBOOLEAN &&
|
||||
value_type != LUA_TSTRING) {
|
||||
lua_pop(L, 1); // again, pop the value before going to the next loop iteration
|
||||
continue;
|
||||
}
|
||||
|
||||
// get the key as a string
|
||||
string key_string = lua_tostring(L, -2); // no copy required - we already know this is a string
|
||||
|
||||
// do not support variables that start with '_'
|
||||
// lua has some predefined values like _VERSION. They all start with underscore
|
||||
|
||||
if (!key_string.size()) { // this again is highly unlikely, but still ...
|
||||
lua_pop(L, 1);
|
||||
continue;
|
||||
}
|
||||
if (key_string[0] == '_') {
|
||||
lua_pop(L, 1);
|
||||
continue;
|
||||
}
|
||||
|
||||
string value_string;
|
||||
|
||||
// convert the value to a string. This depends on its type
|
||||
switch (value_type) {
|
||||
case LUA_TSTRING:
|
||||
case LUA_TNUMBER:
|
||||
// numbers can be converted to strings
|
||||
|
||||
// get the value as a string (this requires a copy because traversing tables
|
||||
// uses the top of the stack as an index. If conversion from a number to string
|
||||
// happens, the top of the stack will be altered and the table index will become invalid)
|
||||
lua_pushvalue(L, -1);
|
||||
value_string = lua_tostring(L, -1);
|
||||
lua_pop(L, 1);
|
||||
break;
|
||||
case LUA_TBOOLEAN:
|
||||
value_string = lua_toboolean(L, -1) == 0 ? "false" : "true";
|
||||
break;
|
||||
}
|
||||
|
||||
// enclose the value in "" if it is a string
|
||||
if (value_type == LUA_TSTRING) {
|
||||
value_string = "\"" + value_string + "\"";
|
||||
}
|
||||
|
||||
// resulting line. Somehow save this and when you need to restore it, just
|
||||
// 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());
|
||||
XmlNode *luaScriptNode = rootNode->addChild("LuaScript");
|
||||
luaScriptNode->addAttribute("variable",key_string, mapTagReplacements);
|
||||
luaScriptNode->addAttribute("value",value_string, mapTagReplacements);
|
||||
luaScriptNode->addAttribute("value_type",intToStr(value_type), mapTagReplacements);
|
||||
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
}
|
||||
|
||||
void LuaScript::loadGame(const XmlNode *rootNode) {
|
||||
const XmlNode *luaScriptNode = rootNode;
|
||||
|
||||
vector<XmlNode *> luaScriptNodeList = rootNode->getChildList("LuaScript");
|
||||
for(unsigned int i = 0; i < luaScriptNodeList.size(); ++i) {
|
||||
XmlNode *node = luaScriptNodeList[i];
|
||||
|
||||
string variable = node->getAttribute("variable")->getValue();
|
||||
int value_type = node->getAttribute("value_type")->getIntValue();
|
||||
|
||||
switch (value_type) {
|
||||
case LUA_TSTRING:
|
||||
lua_pushstring( luaState, node->getAttribute("value")->getValue().c_str() );
|
||||
break;
|
||||
case LUA_TNUMBER:
|
||||
lua_pushnumber( luaState, node->getAttribute("value")->getIntValue() );
|
||||
break;
|
||||
case LUA_TBOOLEAN:
|
||||
lua_pushboolean( luaState, node->getAttribute("value")->getIntValue() );
|
||||
break;
|
||||
}
|
||||
|
||||
lua_setglobal( luaState, variable.c_str() );
|
||||
}
|
||||
}
|
||||
|
||||
LuaScript::~LuaScript() {
|
||||
Lua_STREFLOP_Wrapper streflopWrapper;
|
||||
|
||||
|
Reference in New Issue
Block a user