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

Fix lua components corruption on save #5657

Merged
Merged
Show file tree
Hide file tree
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
22 changes: 20 additions & 2 deletions src/Space.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,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++)
for (Uint32 i = 0; i < bodyArray.size(); i++) {
if(bodyArray[i].count("is_not_in_space") > 0)
continue;
m_bodies.push_back(Body::FromJson(bodyArray[i], this));
}
} catch (Json::type_error &) {
throw SavedGameCorruptException();
}
Expand Down Expand Up @@ -348,8 +351,23 @@ 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 = 0; i < m_bodyIndex.size() - 1; i++) {
// First index of m_bodyIndex is reserved to
// nullptr or bad index
Body* b = m_bodyIndex[i + 1];
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.
// FIXME: This is done to save it's lua components which is otherwise won't save.
// This needs a better way to handle this case.
// Described in depth: https://github.com/pioneerspacesim/pioneer/pull/5657#issuecomment-1818188703
bodyArray.push_back(bodyArrayEl);
continue;
}
b->ToJson(bodyArrayEl, this);
bodyArray.push_back(bodyArrayEl); // Append body object to array.
}
Expand Down
14 changes: 9 additions & 5 deletions src/lua/LuaSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,14 +462,17 @@ 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];
// First index of m_bodyIndex is reserved to
// nullptr or bad index
Body *body = space->GetBodyByIndex(idx + 1);

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

if (!luaComponentsObj.empty()) {
bodies[idx]["lua_components"] = luaComponentsObj;
Expand All @@ -485,16 +488,17 @@ 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];

if (bodyObj.count("lua_components") != 0) {
const Json &luaComponents = bodyObj["lua_components"];
if (luaComponents.is_object() && !luaComponents.empty()) {
Body *body = space->GetBodies()[idx];
// First index of m_bodyIndex is reserved to
// nullptr or bad index
Body *body = space->GetBodyByIndex(idx + 1);

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