static int read_persistent(lua_State *state, PersistentDataItem ref, bool create) { if (!ref.isValid()) { lua_pushnil(state); lua_pushstring(state, "entry not found"); return 2; } if (create) lua_createtable(state, 0, 4); lua_pushvalue(state, lua_upvalueindex(1)); lua_setmetatable(state, -2); lua_pushinteger(state, ref.entry_id()); lua_setfield(state, -2, "entry_id"); lua_pushstring(state, ref.key().c_str()); lua_setfield(state, -2, "key"); lua_pushstring(state, ref.val().c_str()); lua_setfield(state, -2, "value"); lua_createtable(state, PersistentDataItem::NumInts, 0); for (int i = 0; i < PersistentDataItem::NumInts; i++) { lua_pushinteger(state, ref.ival(i)); lua_rawseti(state, -2, i+1); } lua_setfield(state, -2, "ints"); return 1; }
bool World::DeletePersistentData(const PersistentDataItem &item) { int id = item.raw_id(); if (id > -100) return false; if (!BuildPersistentCache()) return false; stl::vector<df::historical_figure*> &hfvec = df::historical_figure::get_vector(); auto eqrange = persistent_index.equal_range(item.key()); for (auto it2 = eqrange.first; it2 != eqrange.second; ) { auto it = it2; ++it2; if (it->second != -id) continue; persistent_index.erase(it); int idx = binsearch_index(hfvec, id); if (idx >= 0) { delete hfvec[idx]; hfvec.erase(hfvec.begin()+idx); } return true; } return false; }
static PersistentDataItem persistent_by_struct(lua_State *state, int idx) { lua_getfield(state, idx, "entry_id"); int id = lua_tointeger(state, -1); lua_pop(state, 1); PersistentDataItem ref = Core::getInstance().getWorld()->GetPersistentData(id); if (ref.isValid()) { lua_getfield(state, idx, "key"); const char *str = lua_tostring(state, -1); if (!str || str != ref.key()) luaL_argerror(state, idx, "inconsistent id and key"); lua_pop(state, 1); } return ref; }