Skip to content

Commit

Permalink
Fix wrong custom objects duplication when updating an extension (#7086)
Browse files Browse the repository at this point in the history
  • Loading branch information
D8H authored Oct 20, 2024
1 parent f4e3f24 commit f09a1dd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Core/GDCore/Project/EventsFunctionsExtension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,15 @@ void EventsFunctionsExtension::UnserializeExtensionDeclarationFrom(
// As event based objects can contains objects using CustomBehavior and/or
// CustomObject, this allows them to reference EventBasedBehavior and
// EventBasedObject respectively.
eventsBasedBehaviors.Clear();
auto &behaviorsElement = element.GetChild("eventsBasedBehaviors");
behaviorsElement.ConsiderAsArrayOf("eventsBasedBehavior");
for (std::size_t i = 0; i < behaviorsElement.GetChildrenCount(); ++i) {
const gd::String &behaviorName =
behaviorsElement.GetChild(i).GetStringAttribute("name");
eventsBasedBehaviors.InsertNew(behaviorName, eventsBasedBehaviors.GetCount());
}
eventsBasedObjects.Clear();
auto &objectsElement = element.GetChild("eventsBasedObjects");
objectsElement.ConsiderAsArrayOf("eventsBasedObject");
for (std::size_t i = 0; i < objectsElement.GetChildrenCount(); ++i) {
Expand Down
23 changes: 21 additions & 2 deletions Core/tests/ObjectSerialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,7 @@ TEST_CASE("ObjectSerialization", "[common]") {
REQUIRE(readProject.GetEventsFunctionsExtensionsCount() == 1);
auto &eventsExtension =
readProject.GetEventsFunctionsExtension(0);
REQUIRE(eventsExtension.GetEventsBasedObjects().GetCount() ==
2);
REQUIRE(eventsExtension.GetEventsBasedObjects().GetCount() == 2);
auto &eventsBasedObject =
eventsExtension.GetEventsBasedObjects().Get(0);
REQUIRE(eventsBasedObject.GetObjects().GetObjectsCount() == 1);
Expand All @@ -392,4 +391,24 @@ TEST_CASE("ObjectSerialization", "[common]") {
customObjectConfiguration->GetChildObjectConfiguration("MyChildSprite");
CheckSpriteConfiguration(spriteConfiguration);
}

SECTION("Can unserialize over an existing extension without duplicating its event-based objects") {
gd::Platform platform;
gd::Project project;
SetupProjectWithDummyPlatform(project, platform);

auto &eventsExtension =
project.InsertNewEventsFunctionsExtension("MyEventsExtension", 0);
{
auto &eventsBasedObject =
eventsExtension.GetEventsBasedObjects().InsertNew(
"MyEventsBasedObject", 0);
}

SerializerElement extensionElement;
eventsExtension.SerializeTo(extensionElement);
eventsExtension.UnserializeFrom(project, extensionElement);

REQUIRE(eventsExtension.GetEventsBasedObjects().GetCount() == 1);
}
}

0 comments on commit f09a1dd

Please sign in to comment.