Skip to content

Commit

Permalink
Fix lua components corruption on save
Browse files Browse the repository at this point in the history
Fix for the issue when objects which are not in space were corrupted on save, because their lua components wasn't saved.
For this, m_bodyIndex vector is used instead of m_bodies to not miss any bodies that are currently exists in the game.
  • Loading branch information
Max5377 committed Nov 18, 2023
1 parent 9ee3f29 commit f65ca13
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
18 changes: 17 additions & 1 deletion src/Space.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,11 @@ Space::Space(Game *game, RefCountedPtr<Galaxy> galaxy, const Json &jsonObj, doub
try {
Json bodyArray = spaceObj["bodies"].get<Json::array_t>();
for (Uint32 i = 0; i < bodyArray.size(); i++)
{
if(bodyArray[i]["is_not_in_space"].is_boolean())
continue;
m_bodies.push_back(Body::FromJson(bodyArray[i], this));
}
} catch (Json::type_error &) {
throw SavedGameCorruptException();
}
Expand Down Expand Up @@ -348,8 +352,20 @@ void Space::ToJson(Json &jsonObj)
spaceObj["frame"] = frameObj;

Json bodyArray = Json::array(); // Create JSON array to contain body data.
for (Body *b : m_bodies) {
for (size_t i = 1; i < m_bodyIndex.size(); i++)
{
Body* b = m_bodyIndex[i];
Json bodyArrayEl({}); // Create JSON object to contain body.
if (!b->IsInSpace())
{
bodyArrayEl["is_not_in_space"] = true;
// Append empty body object to array.
// The only working example right now is ship in hyperspace
// which is loaded through HyperspaceCloud class, so
// there is no need to load it a second time.
bodyArray.push_back(bodyArrayEl);
continue;
}
b->ToJson(bodyArrayEl, this);
bodyArray.push_back(bodyArrayEl); // Append body object to array.
}
Expand Down
16 changes: 8 additions & 8 deletions src/lua/LuaSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,17 +462,18 @@ void LuaSerializer::SaveComponents(Json &jsonObj, Space *space)

// Note: this loop relies on the ordering and contents of Space::m_bodies not changing
// between when bodies were serialized and when this function is called.
// Space should not do update, because it invalidates m_bodyIndex vector.

for (size_t idx = 0; idx < bodies.size(); idx++) {
Body *body = space->GetBodies()[idx];
for (size_t idx = 1; idx < bodies.size(); idx++) {
Body *body = space->GetBodyByIndex(idx);

// Serialize lua components
Json luaComponentsObj = Json::object();
if (!LuaObjectBase::SerializeComponents(body, luaComponentsObj))
break;

if (!luaComponentsObj.empty()) {
bodies[idx]["lua_components"] = luaComponentsObj;
bodies[idx - 1]["lua_components"] = luaComponentsObj;
}
}
}
Expand All @@ -485,16 +486,15 @@ void LuaSerializer::LoadComponents(const Json &jsonObj, Space *space)

// Note: this loop relies on the ordering and contents of Space::m_bodies not changing
// between when bodies were deserialized and when this function is called.
// Space::GetBodyByIndex cannot be used to lookup bodies as it can be different from the
// index into the JSON bodies array when loading.
// Space should not do update, because it invalidates m_bodyIndex vector.

for (size_t idx = 0; idx < bodies.size(); idx++) {
const Json &bodyObj = bodies[idx];
for (size_t idx = 1; idx < bodies.size(); idx++) {
const Json &bodyObj = bodies[idx - 1];

if (bodyObj.count("lua_components") != 0) {
const Json &luaComponents = bodyObj["lua_components"];
if (luaComponents.is_object() && !luaComponents.empty()) {
Body *body = space->GetBodies()[idx];
Body *body = space->GetBodyByIndex(idx);

// Ensure we've registered the body object in Lua
LuaObject<Body>::PushToLua(body);
Expand Down

0 comments on commit f65ca13

Please sign in to comment.