Files
integration/luprex/core/cpp/luasnap.cpp

73 lines
1.9 KiB
C++
Raw Normal View History

2021-02-09 17:15:54 -05:00
#include "luasnap.hpp"
#include "luastack.hpp"
2021-02-09 17:15:54 -05:00
#include <iostream>
#include <cassert>
#include <sstream>
2021-02-09 17:15:54 -05:00
LuaSnap::LuaSnap() {
state_ = luaL_newstate();
LuaStack LS(state_);
LS.rawset(LuaRegistry, "persist", LuaNewTable);
LS.rawset(LuaRegistry, "unpersist", LuaNewTable);
2021-02-09 17:15:54 -05:00
}
LuaSnap::~LuaSnap() {
std::cerr << "LuaSnap destructor not implemented yet" << std::endl;
2021-02-09 17:15:54 -05:00
}
bool LuaSnap::have_snapshot() const {
return !snapshot_.empty();
2021-02-09 17:15:54 -05:00
}
static void oss_writer(lua_State *L, const void *p, size_t sz, void *ud) {
std::ostringstream *oss = (std::ostringstream *)ud;
oss->write((const char *)p, sz);
2021-02-09 17:15:54 -05:00
}
void LuaSnap::snapshot() {
// Snapshot and the lua stack should both be empty.
assert(snapshot_.empty());
assert(lua_gettop(state_) == 0);
// lua variables that we'll need.
LuaVar key, value;
LuaRet permstable, regcopy;
LuaStack LS(state_, permstable, regcopy, key, value);
// Construct a copy of the registry table.
LS.set(regcopy, LuaNewTable);
LS.set(key, LuaNil);
while (LS.next(LuaRegistry, key, value) != 0) {
LS.rawset(regcopy, key, value);
2021-02-09 17:15:54 -05:00
}
// Remove certain things from the copy.
LS.rawset(regcopy, LUA_RIDX_MAINTHREAD, LuaNil);
LS.rawset(regcopy, "persist", LuaNil);
LS.rawset(regcopy, "unpersist", LuaNil);
LS.rawset(regcopy, "world", LuaNil);
LS.rawset(regcopy, "gui", LuaNil);
LS.result();
2021-02-09 17:15:54 -05:00
// Get the permanents table from the registry.
LS.rawget(permstable, LuaRegistry, "persist");
2021-02-09 17:15:54 -05:00
// When we call 'LS.result', this should leave the
// permstable and the regcopy on the stack.
LS.result();
assert(lua_gettop(state_) == 2);
2021-02-09 17:15:54 -05:00
// Call eris to dump the state to an ostringstream,
// then save the result.
std::ostringstream oss;
eris_dump(state_, oss_writer, (void *)&oss);
snapshot_ = oss.str();
2021-02-09 17:15:54 -05:00
}
void LuaSnap::rollback() {
assert(!snapshot_.empty());
2021-02-09 17:15:54 -05:00
}