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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user