Less serialization and deserialization of Lua Source, also, Invocation is now simpler

This commit is contained in:
2023-10-19 19:42:33 -04:00
parent 049b0b893a
commit 7104a523b5
16 changed files with 134 additions and 150 deletions

View File

@@ -6,57 +6,24 @@
#include "invocation.hpp"
const eng::string &InvocationData::get(const eng::string &key) const {
static eng::string blank_;
auto iter = find(key);
if (iter == end()) {
return blank_;
} else {
return iter->second;
}
}
void InvocationData::serialize(StreamBuffer *sb) const {
assert(int(size()) < 65536);
sb->write_uint16(size());
for (const auto &pair : *this) {
sb->write_string(pair.first);
sb->write_string(pair.second);
}
}
void InvocationData::deserialize(StreamBuffer *sb) {
clear();
int size = sb->read_uint16();
for (int i = 0; i < size; i++) {
eng::string key = sb->read_string();
eng::string val = sb->read_string();
(*this)[key] = val;
}
}
Invocation::Invocation() : kind_(KIND_INVALID), actor_(0), place_(0) {}
Invocation::Invocation(Kind kind, int64_t actor, int64_t place, const eng::string &action, const InvocationData &data)
: kind_(kind), actor_(actor), place_(place), action_(action), data_(data) {}
Invocation::Invocation(Kind kind, int64_t actor, int64_t place, const eng::string &action)
: kind_(kind), actor_(actor), place_(place), action_(action) {}
Invocation::Invocation(Kind kind, int64_t actor, int64_t place, std::string_view datapack)
: kind_(kind), actor_(actor), place_(place), datapack_(datapack) {}
void Invocation::serialize(StreamBuffer *sb) const {
sb->write_uint8(kind_);
sb->write_int64(actor_);
sb->write_int64(place_);
sb->write_string(action_);
data_.serialize(sb);
sb->write_string(datapack_);
}
void Invocation::deserialize(StreamBuffer *sb) {
kind_ = Kind(sb->read_uint8());
actor_ = sb->read_int64();
place_ = sb->read_int64();
action_ = sb->read_string();
data_.deserialize(sb);
datapack_ = sb->read_string();
}
eng::string Invocation::debug_string() const {
@@ -73,12 +40,7 @@ eng::string Invocation::debug_string() const {
}
oss << " a=" << actor_;
oss << " p=" << place_;
if (kind_ != KIND_LUA_SOURCE) {
oss << " " << action_;
}
for (const auto &pair : data_) {
oss << " " << pair.first << "=" << pair.second;
}
oss << " dp=" << datapack_.size() << " bytes";
oss << "]";
return oss.str();
}