Code cleanup and refactoring.
This commit is contained in:
@@ -105,12 +105,12 @@ void AnimStepEditor::print_debug_string(eng::ostringstream &oss) {
|
||||
oss << ":";
|
||||
}
|
||||
switch (value.type) {
|
||||
case SimpleDynamicTag::UNINITIALIZED: oss << "UNINITIALIZED"; break;
|
||||
case SimpleDynamicTag::STRING: oss << value.s; break;
|
||||
case SimpleDynamicTag::TOKEN: oss << "[" << value.s << "]"; break;
|
||||
case SimpleDynamicTag::NUMBER: oss << value.x; break;
|
||||
case SimpleDynamicTag::BOOLEAN: oss << ((value.x == 1.0) ? "true":"false"); break;
|
||||
case SimpleDynamicTag::VECTOR: oss << value.x << "," << value.y << "," << value.z; break;
|
||||
case LuaValueType::UNINITIALIZED: oss << "UNINITIALIZED"; break;
|
||||
case LuaValueType::STRING: oss << value.s; break;
|
||||
case LuaValueType::TOKEN: oss << "[" << value.s << "]"; break;
|
||||
case LuaValueType::NUMBER: oss << value.x; break;
|
||||
case LuaValueType::BOOLEAN: oss << ((value.x == 1.0) ? "true":"false"); break;
|
||||
case LuaValueType::VECTOR: oss << value.x << "," << value.y << "," << value.z; break;
|
||||
default: assert(false);
|
||||
}
|
||||
first = false;
|
||||
@@ -166,7 +166,7 @@ void AnimStepEditor::decode_persistent(std::string_view s) {
|
||||
eng::string AnimStepEditor::add_default(const eng::string &name, const AnimValue &def, const AnimStepEditor *other) {
|
||||
AnimValue &value = map_[name];
|
||||
value.persistent = true;
|
||||
if (value.type == SimpleDynamicTag::UNINITIALIZED) {
|
||||
if (value.type == LuaValueType::UNINITIALIZED) {
|
||||
if (other != nullptr) {
|
||||
auto otheriter = other->map_.find(name);
|
||||
if (otheriter != other->map_.end()) {
|
||||
@@ -228,7 +228,7 @@ eng::string AnimStepEditor::from_lua(LuaCoreStack &LS0, LuaSlot tab, bool persis
|
||||
return "in animation key-value pairs, key must be a valid lua identifier.";
|
||||
}
|
||||
AnimValue parsedvalue = parse_anim_value(LS, val);
|
||||
if (parsedvalue.type == SimpleDynamicTag::UNINITIALIZED) {
|
||||
if (parsedvalue.type == LuaValueType::UNINITIALIZED) {
|
||||
return "in animation key-value pairs, value must be string, token, number, boolean, or xyz";
|
||||
}
|
||||
if (parsedvalue.is_token("auto") && !allowauto) {
|
||||
@@ -253,15 +253,15 @@ eng::string AnimStepEditor::merge(const AnimStepEditor &previous, const AnimStep
|
||||
// Handle autocalculation rules.
|
||||
if (src.is_token("auto")) {
|
||||
if (name == "facing") {
|
||||
if (!dst.persistent || dst.type != SimpleDynamicTag::NUMBER) {
|
||||
if (!dst.persistent || dst.type != LuaValueType::NUMBER) {
|
||||
return "Cannot auto-calculate facing because facing has not been specified as a persistent number";
|
||||
}
|
||||
const auto xyz_previous = previous.map_.find("xyz");
|
||||
const auto xyz_update = update.map_.find("xyz");
|
||||
if ((xyz_previous == previous.map_.end()) ||
|
||||
(xyz_update == update.map_.end()) ||
|
||||
(xyz_previous->second.type != SimpleDynamicTag::VECTOR) ||
|
||||
(xyz_update->second.type != SimpleDynamicTag::VECTOR)) {
|
||||
(xyz_previous->second.type != LuaValueType::VECTOR) ||
|
||||
(xyz_update->second.type != LuaValueType::VECTOR)) {
|
||||
return "Cannot auto-calculate facing because before/after xyz coordinates are not present";
|
||||
}
|
||||
double dx = xyz_update->second.x - xyz_previous->second.x;
|
||||
@@ -298,15 +298,15 @@ void AnimStepEditor::to_lua(LuaCoreStack &LS0, LuaSlot tab, bool transient, bool
|
||||
}
|
||||
LS.set(name, pair.first);
|
||||
const AnimValue &value = pair.second;
|
||||
if (value.type == SimpleDynamicTag::BOOLEAN) {
|
||||
if (value.type == LuaValueType::BOOLEAN) {
|
||||
LS.set(val, (value.x == 1.0));
|
||||
} else if (value.type == SimpleDynamicTag::NUMBER) {
|
||||
} else if (value.type == LuaValueType::NUMBER) {
|
||||
LS.set(val, value.x);
|
||||
} else if (value.type == SimpleDynamicTag::STRING) {
|
||||
} else if (value.type == LuaValueType::STRING) {
|
||||
LS.set(val, std::string_view(value.s));
|
||||
} else if (value.type == SimpleDynamicTag::TOKEN) {
|
||||
} else if (value.type == LuaValueType::TOKEN) {
|
||||
LS.set(val, LuaToken(value.s));
|
||||
} else if (value.type == SimpleDynamicTag::VECTOR) {
|
||||
} else if (value.type == LuaValueType::VECTOR) {
|
||||
LS.newtable(val);
|
||||
LS.rawset(val, 1, value.x);
|
||||
LS.rawset(val, 2, value.y);
|
||||
@@ -351,8 +351,8 @@ void AnimCoreState::decode(std::string_view s) {
|
||||
bool persistent = sb.read_bool();
|
||||
sb.read_simple_dynamic(&value);
|
||||
if (persistent) {
|
||||
if ((name == "xyz") && (value.type == SimpleDynamicTag::VECTOR)) xyz = util::DXYZ(value.x, value.y, value.z);
|
||||
if ((name == "plane") && (value.type == SimpleDynamicTag::STRING)) plane = value.s;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,23 +124,23 @@
|
||||
#include <cassert>
|
||||
#include <ostream>
|
||||
|
||||
struct AnimValue : public SimpleDynamicValue {
|
||||
struct AnimValue : public LuaValueHolderValue {
|
||||
bool persistent;
|
||||
|
||||
AnimValue() { persistent = false; }
|
||||
|
||||
void set_token(LuaToken token) {
|
||||
type = SimpleDynamicTag::TOKEN;
|
||||
type = LuaValueType::TOKEN;
|
||||
s=token.str(); x=y=z=0;
|
||||
}
|
||||
|
||||
void set_dxyz(const util::DXYZ &xyz) {
|
||||
type = SimpleDynamicTag::VECTOR;
|
||||
type = LuaValueType::VECTOR;
|
||||
s.clear(); x = xyz.x; y = xyz.y; z = xyz.z;
|
||||
}
|
||||
|
||||
bool is_token(const char *t) const {
|
||||
return (type == SimpleDynamicTag::TOKEN) && (s == t);
|
||||
return (type == LuaValueType::TOKEN) && (s == t);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -226,7 +226,7 @@ public:
|
||||
//
|
||||
// If 'allowauto' is true, then the lua table may contain a key-value
|
||||
// pair of the form (key, math.auto). These keys will be stored in the
|
||||
// AnimStepEditor map with mapentry.type == SimpleDynamicTag::AUTO.
|
||||
// AnimStepEditor map with mapentry.type == LuaValueType::AUTO.
|
||||
// This is done to express an intent that the value should be
|
||||
// automatically computer later.
|
||||
//
|
||||
|
||||
@@ -243,7 +243,7 @@ public:
|
||||
virtual char const *what() const { return "Stream Corruption"; }
|
||||
};
|
||||
|
||||
using SimpleDynamicValue = SimpleDynamic<eng::string>;
|
||||
using LuaValueHolderValue = LuaValueHolder<eng::string>;
|
||||
|
||||
class StreamBufferCore {
|
||||
protected:
|
||||
|
||||
@@ -8,17 +8,17 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
// Read a SimpleDynamic value from the streambuffer and push
|
||||
// Read a LuaValueHolder value from the streambuffer and push
|
||||
// it onto the lua stack.
|
||||
void push_simple_dynamic(lua_State *L, StreamBuffer *sb) {
|
||||
SimpleDynamicTag type = sb->read_simple_dynamic_tag();
|
||||
LuaValueType type = sb->read_simple_dynamic_tag();
|
||||
switch (type) {
|
||||
case SimpleDynamicTag::STRING: {
|
||||
case LuaValueType::STRING: {
|
||||
std::string_view s = sb->read_string_view();
|
||||
lua_pushlstring(L, s.data(), s.size());
|
||||
break;
|
||||
}
|
||||
case SimpleDynamicTag::TOKEN: {
|
||||
case LuaValueType::TOKEN: {
|
||||
std::string_view toktext = sb->read_string_view();
|
||||
LuaToken token(toktext);
|
||||
if (token.empty() && !toktext.empty()) {
|
||||
@@ -27,15 +27,15 @@ void push_simple_dynamic(lua_State *L, StreamBuffer *sb) {
|
||||
lua_pushlightuserdata(L, token.voidvalue());
|
||||
break;
|
||||
}
|
||||
case SimpleDynamicTag::NUMBER: {
|
||||
case LuaValueType::NUMBER: {
|
||||
lua_pushnumber(L, sb->read_double());
|
||||
break;
|
||||
}
|
||||
case SimpleDynamicTag::BOOLEAN: {
|
||||
case LuaValueType::BOOLEAN: {
|
||||
lua_pushboolean(L, sb->read_bool() ? 1:0);
|
||||
break;
|
||||
}
|
||||
case SimpleDynamicTag::VECTOR: {
|
||||
case LuaValueType::VECTOR: {
|
||||
double x = sb->read_double();
|
||||
double y = sb->read_double();
|
||||
double z = sb->read_double();
|
||||
@@ -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(SimpleDynamicTag::STRING);
|
||||
sb->write_simple_dynamic_tag(LuaValueType::STRING);
|
||||
sb->write_string(LS.ckstringview(slot));
|
||||
return true;
|
||||
case LUA_TLIGHTUSERDATA:
|
||||
sb->write_simple_dynamic_tag(SimpleDynamicTag::TOKEN);
|
||||
sb->write_simple_dynamic_tag(LuaValueType::TOKEN);
|
||||
sb->write_string(LS.cktoken(slot).str());
|
||||
return true;
|
||||
case LUA_TNUMBER:
|
||||
sb->write_simple_dynamic_tag(SimpleDynamicTag::NUMBER);
|
||||
sb->write_simple_dynamic_tag(LuaValueType::NUMBER);
|
||||
sb->write_double(LS.cknumber(slot));
|
||||
return true;
|
||||
case LUA_TBOOLEAN:
|
||||
sb->write_simple_dynamic_tag(SimpleDynamicTag::BOOLEAN);
|
||||
sb->write_simple_dynamic_tag(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(SimpleDynamicTag::VECTOR);
|
||||
sb->write_simple_dynamic_tag(LuaValueType::VECTOR);
|
||||
sb->write_dxyz(xyz.value());
|
||||
return true;
|
||||
}
|
||||
@@ -492,7 +492,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(SimpleDynamicTag::STRING);
|
||||
retvals->write_simple_dynamic_tag(LuaValueType::STRING);
|
||||
retvals->write_string(msg);
|
||||
|
||||
if (msg.empty()) {
|
||||
@@ -521,7 +521,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(SimpleDynamicTag::STRING);
|
||||
retvals->write_simple_dynamic_tag(LuaValueType::STRING);
|
||||
retvals->write_string(msg);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,21 +17,22 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////
|
||||
//
|
||||
// SimpleDynamic
|
||||
// LuaValueHolder
|
||||
//
|
||||
// A struct that holds a dynamically typed value.
|
||||
// This can hold a string, number, vector, or boolean.
|
||||
// This can hold a string, token, number, vector, or boolean.
|
||||
//
|
||||
// The type is stored in the 'type' field.
|
||||
//
|
||||
// If it's a STRING, the value is in the field s
|
||||
// If it's a TOKEN, the value is stored in the field s
|
||||
// If it's a NUMBER, the value is in the field x
|
||||
// If it's a BOOLEAN, it's true if (x==1.0)
|
||||
// If it's a VECTOR, the value is in x,y,z
|
||||
//
|
||||
///////////////////////////////////////////////////////////////
|
||||
|
||||
enum class SimpleDynamicTag {
|
||||
enum class LuaValueType {
|
||||
UNINITIALIZED,
|
||||
STRING,
|
||||
TOKEN,
|
||||
@@ -41,25 +42,25 @@ enum class SimpleDynamicTag {
|
||||
};
|
||||
|
||||
template<class STRING>
|
||||
struct SimpleDynamic {
|
||||
struct LuaValueHolder {
|
||||
using string = STRING;
|
||||
SimpleDynamicTag type;
|
||||
LuaValueType type;
|
||||
double x, y, z;
|
||||
string s;
|
||||
|
||||
SimpleDynamic() {
|
||||
type = SimpleDynamicTag::UNINITIALIZED;
|
||||
LuaValueHolder() {
|
||||
type = LuaValueType::UNINITIALIZED;
|
||||
x=y=z=0;
|
||||
}
|
||||
|
||||
static const char *type_name_of(SimpleDynamicTag t) {
|
||||
static const char *type_name_of(LuaValueType t) {
|
||||
switch (t) {
|
||||
case SimpleDynamicTag::UNINITIALIZED: return "uninitialized";
|
||||
case SimpleDynamicTag::STRING: return "string";
|
||||
case SimpleDynamicTag::TOKEN: return "token";
|
||||
case SimpleDynamicTag::BOOLEAN: return "boolean";
|
||||
case SimpleDynamicTag::NUMBER: return "number";
|
||||
case SimpleDynamicTag::VECTOR: return "vector";
|
||||
case LuaValueType::UNINITIALIZED: return "uninitialized";
|
||||
case LuaValueType::STRING: return "string";
|
||||
case LuaValueType::TOKEN: return "token";
|
||||
case LuaValueType::BOOLEAN: return "boolean";
|
||||
case LuaValueType::NUMBER: return "number";
|
||||
case LuaValueType::VECTOR: return "vector";
|
||||
default: return "unknown";
|
||||
}
|
||||
}
|
||||
@@ -69,30 +70,30 @@ struct SimpleDynamic {
|
||||
}
|
||||
|
||||
void set_uninitialized() {
|
||||
type=SimpleDynamicTag::UNINITIALIZED; s.clear(); x=y=z=0;
|
||||
type=LuaValueType::UNINITIALIZED; s.clear(); x=y=z=0;
|
||||
}
|
||||
|
||||
void set_string(std::string_view is) {
|
||||
type=SimpleDynamicTag::STRING; s=is; x=y=z=0;
|
||||
type=LuaValueType::STRING; s=is; x=y=z=0;
|
||||
}
|
||||
|
||||
void set_token(std::string_view is) {
|
||||
type=SimpleDynamicTag::TOKEN; s=is; x=y=z=0;
|
||||
type=LuaValueType::TOKEN; s=is; x=y=z=0;
|
||||
}
|
||||
|
||||
void set_number(double n) {
|
||||
type = SimpleDynamicTag::NUMBER; s.clear(); x=n; y=z=0;
|
||||
type = LuaValueType::NUMBER; s.clear(); x=n; y=z=0;
|
||||
}
|
||||
|
||||
void set_boolean(bool b) {
|
||||
type = SimpleDynamicTag::BOOLEAN; s.clear(); x=(b?1:0); y=z=0;
|
||||
type = LuaValueType::BOOLEAN; s.clear(); x=(b?1:0); y=z=0;
|
||||
}
|
||||
|
||||
void set_vector(double ix, double iy, double iz) {
|
||||
type = SimpleDynamicTag::VECTOR; s.clear(); x=ix; y=iy; z=iz;
|
||||
type = LuaValueType::VECTOR; s.clear(); x=ix; y=iy; z=iz;
|
||||
}
|
||||
|
||||
void copy_value(const SimpleDynamic &other) {
|
||||
void copy_value(const LuaValueHolder &other) {
|
||||
type = other.type;
|
||||
s=other.s; x=other.x; y=other.y; z=other.z;
|
||||
}
|
||||
@@ -492,26 +493,26 @@ public:
|
||||
write_bytes(s);
|
||||
}
|
||||
|
||||
// Write a SimpleDynamicTag.
|
||||
// Write a LuaValueType.
|
||||
//
|
||||
void write_simple_dynamic_tag(SimpleDynamicTag tag) {
|
||||
void write_simple_dynamic_tag(LuaValueType tag) {
|
||||
write_uint8(uint8_t(tag));
|
||||
}
|
||||
|
||||
// Write a SimpleDynamic value.
|
||||
// Write a LuaValueHolder value.
|
||||
//
|
||||
// This works regardless of what kind of data is present in the
|
||||
// SimpleDynamic.
|
||||
// LuaValueHolder.
|
||||
//
|
||||
template<class STRING>
|
||||
void write_simple_dynamic(const SimpleDynamic<STRING> &sd) {
|
||||
void write_simple_dynamic(const LuaValueHolder<STRING> &sd) {
|
||||
write_simple_dynamic_tag(sd.type);
|
||||
switch(sd.type) {
|
||||
case SimpleDynamicTag::STRING: write_string(sd.s); break;
|
||||
case SimpleDynamicTag::TOKEN: write_string(sd.s); break;
|
||||
case SimpleDynamicTag::NUMBER: write_double(sd.x); break;
|
||||
case SimpleDynamicTag::BOOLEAN: write_bool(sd.x == 1.0); break;
|
||||
case SimpleDynamicTag::VECTOR: write_double(sd.x); write_double(sd.y); write_double(sd.z); break;
|
||||
case LuaValueType::STRING: write_string(sd.s); break;
|
||||
case LuaValueType::TOKEN: write_string(sd.s); break;
|
||||
case LuaValueType::NUMBER: write_double(sd.x); break;
|
||||
case LuaValueType::BOOLEAN: write_bool(sd.x == 1.0); break;
|
||||
case LuaValueType::VECTOR: write_double(sd.x); write_double(sd.y); write_double(sd.z); break;
|
||||
default: assert(false);
|
||||
}
|
||||
}
|
||||
@@ -616,23 +617,23 @@ public:
|
||||
return read_string_limit(0x1000000);
|
||||
}
|
||||
|
||||
// Read a SimpleDynamicTag
|
||||
// Read a LuaValueType
|
||||
//
|
||||
SimpleDynamicTag read_simple_dynamic_tag() {
|
||||
return SimpleDynamicTag(read_uint8());
|
||||
LuaValueType read_simple_dynamic_tag() {
|
||||
return LuaValueType(read_uint8());
|
||||
}
|
||||
|
||||
// Read a SimpleDynamic
|
||||
// Read a LuaValueHolder
|
||||
//
|
||||
template<class STRING>
|
||||
void read_simple_dynamic(SimpleDynamic<STRING> *result) {
|
||||
SimpleDynamicTag type = read_simple_dynamic_tag();
|
||||
void read_simple_dynamic(LuaValueHolder<STRING> *result) {
|
||||
LuaValueType type = read_simple_dynamic_tag();
|
||||
switch (type) {
|
||||
case SimpleDynamicTag::STRING: result->set_string(read_string()); break;
|
||||
case SimpleDynamicTag::TOKEN: result->set_token(read_string()); break;
|
||||
case SimpleDynamicTag::NUMBER: result->set_number(read_double()); break;
|
||||
case SimpleDynamicTag::BOOLEAN: result->set_boolean(read_bool()); break;
|
||||
case SimpleDynamicTag::VECTOR: {
|
||||
case LuaValueType::STRING: result->set_string(read_string()); break;
|
||||
case LuaValueType::TOKEN: result->set_token(read_string()); break;
|
||||
case LuaValueType::NUMBER: result->set_number(read_double()); break;
|
||||
case LuaValueType::BOOLEAN: result->set_boolean(read_bool()); break;
|
||||
case LuaValueType::VECTOR: {
|
||||
double x=read_double();
|
||||
double y=read_double();
|
||||
double z=read_double();
|
||||
|
||||
Reference in New Issue
Block a user