- bugfixes for loading XML and not dropping newlines

- started work on trying to save lua state (global variables only)
This commit is contained in:
Mark Vejvoda
2012-03-15 20:48:27 +00:00
parent 5cd0a788d2
commit 510ee7dfff
4 changed files with 131 additions and 11 deletions

View File

@@ -61,9 +61,103 @@ LuaScript::LuaScript() {
argumentCount= -1;
}
void LuaScript::DumpGlobals()
{
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());
lua_pop(L, 1);
}
}
LuaScript::~LuaScript() {
Lua_STREFLOP_Wrapper streflopWrapper;
// LuaInterface.LuaTable luatab;
//
// luatab = lua.GetTable("_G");
//
//
// foreach (DictionaryEntry d in luatab)
//
// {
//
// Console.WriteLine("{0} -> {1}", d.Key, d.Value);
//
// }
//DumpGlobals();
lua_close(luaState);
}
@@ -73,7 +167,12 @@ void LuaScript::loadCode(const string &code, const string &name){
//printf("Code [%s]\nName [%s]\n",code.c_str(),name.c_str());
int errorCode= luaL_loadbuffer(luaState, code.c_str(), code.length(), name.c_str());
if(errorCode !=0 ) {
if(errorCode != 0 ) {
printf("=========================================================\n");
printf("Error loading lua code: %s\n",errorToString(errorCode).c_str());
printf("Function name [%s]\ncode:\n%s\n",name.c_str(),code.c_str());
printf("=========================================================\n");
throw runtime_error("Error loading lua code: " + errorToString(errorCode));
}
@@ -82,6 +181,10 @@ void LuaScript::loadCode(const string &code, const string &name){
//run code
errorCode= lua_pcall(luaState, 0, 0, 0);
if(errorCode !=0 ) {
printf("=========================================================\n");
printf("Error calling lua pcall: %s\n",errorToString(errorCode).c_str());
printf("=========================================================\n");
throw runtime_error("Error initializing lua: " + errorToString(errorCode));
}