| 33 | } |
| 34 | |
| 35 | int LUAPersistentTable::Len(lua_State* pState) { |
| 36 | SCRIPT_CALLBACK(LUAPersistentTable,table) |
| 37 | SCRIPT_WRITE_NUMBER(table.count) |
| 38 | SCRIPT_CALLBACK_RETURN |
| 39 | } |
| 40 | |
| 41 | int LUAPersistentTable::Get(lua_State *pState) { |
| 42 | SCRIPT_CALLBACK(LUAPersistentTable, table) |
| 43 | const char* name = SCRIPT_READ_STRING(NULL); |
| 44 | if (name) { |
| 45 | lua_getmetatable(pState, 1); |
| 46 | lua_getfield(pState, -1, "|items"); |
| 47 | lua_replace(pState, -2); |
| 48 | lua_getfield(pState, -1, name); |
| 49 | lua_replace(pState, -2); |
| 50 | if (lua_isnil(pState,-1) && strcmp(name, "count") == 0) |
| 51 | SCRIPT_WRITE_NUMBER(table.count) |
| 52 | } |
| 53 | SCRIPT_CALLBACK_RETURN |
| 54 | } |
| 55 | |
| 56 | int LUAPersistentTable::Set(lua_State *pState) { |
| 57 | SCRIPT_CALLBACK(LUAPersistentTable, table); |
| 58 | const char* name = SCRIPT_READ_STRING(NULL); |
| 59 | if (name) { |
| 60 | Int8 removing = SCRIPT_NEXT_TYPE==LUA_TNIL ? 1 : 0; |
| 61 | string path; |
| 62 | FileSystem::MakeFolder(String::Format(path, table.path, '/', name)); |
| 63 | |
| 64 | lua_getmetatable(pState, 1); |
| 65 | lua_getfield(pState, -1, "|items"); |
| 66 | lua_getfield(pState, -1, name); // old value |
| 67 | |
| 68 | if (!removing) { |
| 69 | if (lua_istable(pState, -1)) { |
| 70 | SCRIPT_ERROR("Complex type exists already on ", path, ", for secure reasons delete explicitly this entry with a nil assignation before override") |
| 71 | removing = -1; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | if (removing>=0) { |
| 76 | if (SCRIPT_NEXT_TYPE==LUA_TTABLE) { |
| 77 | |
| 78 | lua_pushvalue(pState, 2); // name |
| 79 | Script::NewObject<LUAPersistentTable,LUAPersistentTable>(pState,*new LUAPersistentTable(table.persistentData, path)); |
| 80 | |
| 81 | // table iteration |
| 82 | lua_pushnil(pState); // first key |
| 83 | while (Script::Next(pState, 3) != 0) { |
| 84 | // uses 'key' (at index -2) and 'value' (at index -1) |
| 85 | lua_pushvalue(pState, -2); // duplicate key |
| 86 | lua_pushvalue(pState, -2); // duplicate value |
| 87 | lua_settable(pState, -5); |
| 88 | lua_pop(pState, 1); |
| 89 | } |
| 90 | |
| 91 | // memory |
| 92 | lua_rawset(pState, -4); |
nothing calls this directly
no outgoing calls
no test coverage detected