Final refactor of basebuffer

This commit is contained in:
2026-02-22 23:56:48 -05:00
parent cbbe475f65
commit 8889a36ba3
8 changed files with 162 additions and 128 deletions

View File

@@ -130,7 +130,7 @@ eng::string AnimStepEditor::encode() const {
const AnimValue &value = pair.second;
sb.write_string(name);
sb.write_bool(value.persistent);
sb.write_simple_dynamic(value);
sb.write_lua_value(value);
}
return eng::string(sb.view());
}
@@ -142,7 +142,7 @@ void AnimStepEditor::decode(std::string_view s) {
eng::string name = sb.read_string();
AnimValue &value = map_[name];
value.persistent = sb.read_bool();
sb.read_simple_dynamic(&value);
sb.read_lua_value(&value);
}
}
@@ -156,9 +156,9 @@ void AnimStepEditor::decode_persistent(std::string_view s) {
if (persistent) {
AnimValue &value = map_[name];
value.persistent = persistent;
sb.read_simple_dynamic(&value);
sb.read_lua_value(&value);
} else {
sb.read_simple_dynamic(&dummy);
sb.read_lua_value(&dummy);
}
}
}
@@ -349,7 +349,7 @@ void AnimCoreState::decode(std::string_view s) {
while (!sb.empty()) {
eng::string name = sb.read_string();
bool persistent = sb.read_bool();
sb.read_simple_dynamic(&value);
sb.read_lua_value(&value);
if (persistent) {
if ((name == "xyz") && (value.type == LuaValueType::VECTOR)) xyz = util::DXYZ(value.x, value.y, value.z);
if ((name == "plane") && (value.type == LuaValueType::STRING)) plane = value.s;

View File

@@ -79,7 +79,7 @@
// for all key-value pairs do:
// write_string(key)
// write_bool(persistent)
// write_simple_dynamic(value)
// write_lua_value(value)
//
// The encoded string produced by the loop above is called an "encstep".
// That's short for "encoded animation step". The encstep has a hash

View File

@@ -255,6 +255,7 @@ class ReplayLogfile : public std::ifstream {
using std::ifstream::ifstream;
using DD = DataDeserializer<ReplayLogfile>;
public:
using string_type = std::string;
void read_bytes_into(char *n, size_t size) {
read(n, size);
if (!good()) {

View File

@@ -248,6 +248,9 @@ using LuaValue = BaseLuaValue<eng::string>;
class StreamBufferConfig {
public:
using string_type = eng::string;
using fvector_type = util::XYZ;
using dvector_type = util::DXYZ;
using luavalue_type = LuaValue;
void *basebuffer_malloc(size_t size) { return eng::malloc(size); }
void basebuffer_free(void *p) { eng::free(p); }
void clear_error_flags() { }
@@ -260,50 +263,16 @@ class StreamBuffer : public eng::nevernew, public BaseBuffer<StreamBufferConfig>
public:
using BaseBuffer::BaseBuffer;
void write_xyz(const util::XYZ &xyz) {
write_float(xyz.x);
write_float(xyz.y);
write_float(xyz.z);
}
void write_dxyz(const util::DXYZ &xyz) {
write_double(xyz.x);
write_double(xyz.y);
write_double(xyz.z);
}
void write_hashvalue(const util::HashValue &h) {
write_uint64(h.first);
write_uint64(h.second);
}
util::XYZ read_xyz() {
float x = read_float();
float y = read_float();
float z = read_float();
return util::XYZ(x, y, z);
}
util::DXYZ read_dxyz() {
double x = read_double();
double y = read_double();
double z = read_double();
return util::DXYZ(x, y, z);
}
util::HashValue read_hashvalue() {
uint64_t f = read_uint64();
uint64_t s = read_uint64();
return util::HashValue(f, s);
}
bool contents_equal(const StreamBuffer *sb) {
return view() == sb->view();
}
void copy_into(StreamBuffer *sb) {
sb->write_bytes(view());
}
};
// Use a streambuffer as a lua_writer.

View File

@@ -11,7 +11,7 @@
// Read a serialized LuaValue value from the
// streambuffer and push it onto the lua stack.
void push_simple_dynamic(lua_State *L, StreamBuffer *sb) {
LuaValueType type = sb->read_simple_dynamic_tag();
LuaValueType type = sb->read_lua_value_type();
switch (type) {
case LuaValueType::STRING: {
std::string_view s = sb->read_string_view();
@@ -56,25 +56,25 @@ void push_simple_dynamic(lua_State *L, StreamBuffer *sb) {
bool encode_simple_dynamic(LuaCoreStack &LS, LuaSlot &slot, StreamBuffer *sb) {
switch (LS.type(slot)) {
case LUA_TSTRING:
sb->write_simple_dynamic_tag(LuaValueType::STRING);
sb->write_lua_value_type(LuaValueType::STRING);
sb->write_string(LS.ckstringview(slot));
return true;
case LUA_TLIGHTUSERDATA:
sb->write_simple_dynamic_tag(LuaValueType::TOKEN);
sb->write_lua_value_type(LuaValueType::TOKEN);
sb->write_string(LS.cktoken(slot).str());
return true;
case LUA_TNUMBER:
sb->write_simple_dynamic_tag(LuaValueType::NUMBER);
sb->write_lua_value_type(LuaValueType::NUMBER);
sb->write_double(LS.cknumber(slot));
return true;
case LUA_TBOOLEAN:
sb->write_simple_dynamic_tag(LuaValueType::BOOLEAN);
sb->write_lua_value_type(LuaValueType::BOOLEAN);
sb->write_bool(LS.ckboolean(slot));
return true;
case LUA_TTABLE: {
std::optional<util::DXYZ> xyz = LS.tryxyz(slot);
if (!xyz.has_value()) return false;
sb->write_simple_dynamic_tag(LuaValueType::VECTOR);
sb->write_lua_value_type(LuaValueType::VECTOR);
sb->write_dxyz(xyz.value());
return true;
}
@@ -480,7 +480,7 @@ void World::probe_lua_call(int64_t actor_id, int64_t place_id, std::string_view
// return value of the function.
//
int64_t rv_base = retvals->total_writes();
retvals->write_simple_dynamic_tag(LuaValueType::STRING);
retvals->write_lua_value_type(LuaValueType::STRING);
retvals->write_string(msg);
if (msg.empty()) {
@@ -509,7 +509,7 @@ void World::probe_lua_call(int64_t actor_id, int64_t place_id, std::string_view
if (!ok) {
msg = util::ss("Lua function ",classname,".",funcname," returned a non-serializable value");
retvals->unwrite_to(rv_base);
retvals->write_simple_dynamic_tag(LuaValueType::STRING);
retvals->write_lua_value_type(LuaValueType::STRING);
retvals->write_string(msg);
}
}