Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort lua components during serialization #6070

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/lua/LuaObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,10 @@ bool LuaObjectBase::SerializeComponents(LuaWrappable *object, Json &out)
}

lua_pushnil(l);

// sort the components before serialization
std::vector<std::string> componentNames;

while (lua_next(l, -2) != 0) {
if (!lua_isstring(l, -2)) {
lua_pop(l, 1);
Expand All @@ -772,6 +776,14 @@ bool LuaObjectBase::SerializeComponents(LuaWrappable *object, Json &out)
continue;
}

componentNames.emplace_back(key);
lua_pop(l, 1);
}
std::sort(componentNames.begin(), componentNames.end(), std::less{});

for (const auto &key : componentNames) {
lua_pushstring(l, key.data());
lua_rawget(l, -2);
// Pickle the table to json
LuaSerializer::pickle_json(l, lua_gettop(l), out[key.data()], "BodyComponent");
lua_pop(l, 1);
Expand Down Expand Up @@ -809,7 +821,7 @@ bool LuaObjectBase::DeserializeComponents(LuaWrappable *object, const Json &obj)
return false;
}

// Deserialize all components
// Deserialize all components, in alphabetical order
for (const auto &pair : obj.items()) {
lua_pushstring(l, pair.key().c_str());
LuaSerializer::unpickle_json(l, pair.value());
Expand Down
Loading