World serialization done (not well-tested yet)

This commit is contained in:
2021-03-16 12:19:37 -04:00
parent 7c9fd1e46b
commit a64e339b6b
8 changed files with 111 additions and 52 deletions

View File

@@ -46,14 +46,11 @@ void Tangible::update_plane_item() {
}
void Tangible::serialize(StreamBuffer *sb) {
sb->write_int64(id());
anim_queue_.serialize(sb);
id_player_pool_.serialize(sb);
}
void Tangible::deserialize(StreamBuffer *sb) {
int64_t id = sb->read_int64();
plane_item_.set_id(id);
anim_queue_.deserialize(sb);
id_player_pool_.deserialize(sb);
update_plane_item();
@@ -365,6 +362,60 @@ void World::run_scheduled_threads(int64_t clk) {
LS.result();
}
void World::serialize(StreamBuffer *sb) {
int64_t wc0 = sb->write_count();
lua_snap_.serialize(sb);
id_global_pool_.serialize(sb);
thread_sched_.serialize(sb);
sb->write_size(tangibles_.size());
for (const auto &p : tangibles_) {
sb->write_int64(p.first);
p.second->serialize(sb);
}
int64_t wc1 = sb->write_count();
std::cerr << "World serialized to " << wc1-wc0 << " bytes." << std::endl;
}
void World::deserialize(StreamBuffer *sb) {
lua_snap_.deserialize(sb);
id_global_pool_.deserialize(sb);
thread_sched_.deserialize(sb);
// Mark all tangibles for deletion by setting ID to zero.
for (const auto &p : tangibles_) {
p.second->plane_item_.set_id(0);
}
// Deserialize tangibles.
size_t ntan = sb->read_size();
for (size_t i = 0; i < ntan; i++) {
int64_t id = sb->read_int64();
std::unique_ptr<Tangible> &t = tangibles_[id];
if (t == nullptr) {
t.reset(new Tangible(this, id));
} else {
t->plane_item_.set_id(id);
}
t->deserialize(sb);
}
// Delete tangibles that didn't get deserialized.
for (auto iter = tangibles_.begin(); iter != tangibles_.end(); ) {
if (iter->second->plane_item_.id() == 0) {
tangibles_.erase(iter++);
} else {
++iter;
}
}
}
void World::snapshot() {
snapshot_.clear();
serialize(&snapshot_);
}
void World::rollback() {
assert(!snapshot_.at_eof());
deserialize(&snapshot_);
}
LuaDefine(tangible_xyz, "c") {
LuaArg tanobj;
LuaRet X, Y, Z;