Fix regression in extensions loading (#7242)

This commit is contained in:
Florian Rival
2024-12-18 12:26:56 +01:00
committed by GitHub
parent e2281dfd82
commit deab962081
3 changed files with 358 additions and 18 deletions

View File

@@ -936,6 +936,9 @@ void Project::UnserializeAndInsertExtensionsFrom(
const gd::SerializerElement &eventsFunctionsExtensionsElement) {
eventsFunctionsExtensionsElement.ConsiderAsArrayOf(
"eventsFunctionsExtension");
std::map<gd::String, size_t> extensionNameToElementIndex;
// First, only unserialize behaviors and objects names.
// As event based objects can contains custom behaviors and custom objects,
// this allows them to reference EventBasedBehavior and EventBasedObject
@@ -946,6 +949,7 @@ void Project::UnserializeAndInsertExtensionsFrom(
const SerializerElement& eventsFunctionsExtensionElement =
eventsFunctionsExtensionsElement.GetChild(i);
const gd::String& name = eventsFunctionsExtensionElement.GetStringAttribute("name");
extensionNameToElementIndex[name] = i;
gd::EventsFunctionsExtension& eventsFunctionsExtension =
HasEventsFunctionsExtensionNamed(name)
@@ -959,11 +963,25 @@ void Project::UnserializeAndInsertExtensionsFrom(
// Then unserialize functions, behaviors and objects content.
for (gd::String &extensionName :
GetUnserializingOrderExtensionNames(eventsFunctionsExtensionsElement)) {
size_t extensionIndex = GetEventsFunctionsExtensionPosition(extensionName);
const SerializerElement &eventsFunctionsExtensionElement =
eventsFunctionsExtensionsElement.GetChild(extensionIndex);
eventsFunctionsExtensions.at(extensionIndex)
size_t extensionIndex = GetEventsFunctionsExtensionPosition(extensionName);
if (extensionIndex == gd::String::npos) {
// Should never happen because the extension was added in the first pass.
gd::LogError("Can't find extension " + extensionName + " in the list of extensions in second pass of unserialization.");
continue;
}
auto& partiallyLoadedExtension = eventsFunctionsExtensions.at(extensionIndex);
if (extensionNameToElementIndex.find(extensionName) == extensionNameToElementIndex.end()) {
// Should never happen because the extension element is present.
gd::LogError("Can't find extension element to unserialize for " + extensionName + " in second pass of unserialization.");
continue;
}
size_t elementIndex = extensionNameToElementIndex[extensionName];
const SerializerElement &eventsFunctionsExtensionElement =
eventsFunctionsExtensionsElement.GetChild(elementIndex);
partiallyLoadedExtension
->UnserializeExtensionImplementationFrom(
*this, eventsFunctionsExtensionElement);
}
@@ -971,16 +989,24 @@ void Project::UnserializeAndInsertExtensionsFrom(
std::vector<gd::String> Project::GetUnserializingOrderExtensionNames(
const gd::SerializerElement &eventsFunctionsExtensionsElement) {
eventsFunctionsExtensionsElement.ConsiderAsArrayOf(
"eventsFunctionsExtension");
// Some extension have custom objects, which have child objects coming from other extension.
// These child objects must be loaded completely before the parent custom obejct can be unserialized.
// This implies: an order on the extension unserialization (and no cycles).
// At the beginning, everything is yet to be loaded.
std::map<gd::String, size_t> extensionNameToElementIndex;
std::vector<gd::String> remainingExtensionNames(
eventsFunctionsExtensions.size());
for (std::size_t i = 0; i < eventsFunctionsExtensions.size(); ++i) {
remainingExtensionNames[i] = eventsFunctionsExtensions.at(i)->GetName();
eventsFunctionsExtensionsElement.GetChildrenCount());
for (std::size_t i = 0; i < eventsFunctionsExtensionsElement.GetChildrenCount(); ++i) {
const SerializerElement& eventsFunctionsExtensionElement =
eventsFunctionsExtensionsElement.GetChild(i);
const gd::String& name = eventsFunctionsExtensionElement.GetStringAttribute("name");
remainingExtensionNames[i] = name;
extensionNameToElementIndex[name] = i;
}
// Helper allowing to find if an extension has an object that depends on
@@ -1029,10 +1055,10 @@ std::vector<gd::String> Project::GetUnserializingOrderExtensionNames(
foundAnyExtension = false;
for (std::size_t i = 0; i < remainingExtensionNames.size(); ++i) {
auto extensionName = remainingExtensionNames[i];
size_t extensionIndex =
GetEventsFunctionsExtensionPosition(extensionName);
size_t elementIndex = extensionNameToElementIndex[extensionName];
const SerializerElement &eventsFunctionsExtensionElement =
eventsFunctionsExtensionsElement.GetChild(extensionIndex);
eventsFunctionsExtensionsElement.GetChild(elementIndex);
if (!isDependentFromRemainingExtensions(
eventsFunctionsExtensionElement)) {

View File

@@ -1073,23 +1073,25 @@ class GD_CORE_API Project {
return wholeProjectDiagnosticReport;
}
private:
/**
* Initialize from another game. Used by copy-ctor and assign-op.
* Don't forget to update me if members were changed!
*/
void Init(const gd::Project& project);
/**
* @brief Get the project extensions names in the order they have to be
* unserialized.
*
* Child-objects need the event-based objects they use to be loaded completely
* before they are unserialized.
*
* \warning This is only public to allow testing - don't use it in the editor.
*/
std::vector<gd::String> GetUnserializingOrderExtensionNames(
static std::vector<gd::String> GetUnserializingOrderExtensionNames(
const gd::SerializerElement& eventsFunctionsExtensionsElement);
private:
/**
* Initialize from another game. Used by copy-ctor and assign-op.
* Don't forget to update me if members were changed!
*/
void Init(const gd::Project& project);
/**
* Create an object configuration of the given type.
*

View File

@@ -0,0 +1,312 @@
/*
* GDevelop Core
* Copyright 2008-2016 Florian Rival (Florian.Rival@gmail.com). All rights
* reserved. This project is released under the MIT License.
*/
/**
* @file Tests covering common features of GDevelop Core.
*/
#include <algorithm>
#include "DummyPlatform.h"
#include "GDCore/Extensions/Metadata/MetadataProvider.h"
#include "GDCore/Extensions/Platform.h"
#include "GDCore/Extensions/PlatformExtension.h"
#include "GDCore/Project/Object.h"
#include "GDCore/Project/Project.h"
#include "GDCore/String.h"
#include "catch.hpp"
TEST_CASE("Project::GetUnserializingOrderExtensionNames", "[common]") {
SECTION("Unserialization order is correct when nothing to load") {
gd::SerializerElement emptyElement;
std::vector<gd::String> orderedNames =
gd::Project::GetUnserializingOrderExtensionNames(emptyElement);
REQUIRE(orderedNames.size() == 0);
}
SECTION("One extension with no dependencies") {
gd::SerializerElement extensionsElement = gd::Serializer::FromJSON(
R"([
{
"author": "",
"category": "Input",
"extensionNamespace": "",
"fullName": "3D character keyboard mapper",
"helpPath": "",
"iconUrl": "fake-icon-url",
"name": "Extension1",
"previewIconUrl": "fake-preview-icon-url",
"shortDescription": "3D platformer and 3D shooter keyboard controls.",
"version": "1.0.0",
"description": "3D platformer and 3D shooter keyboard controls.",
"tags": [],
"authorIds": [],
"dependencies": [],
"globalVariables": [],
"sceneVariables": [],
"eventsFunctions": [],
"eventsBasedBehaviors": [],
"eventsBasedObjects": []
}
])");
std::vector<gd::String> orderedNames =
gd::Project::GetUnserializingOrderExtensionNames(extensionsElement);
REQUIRE(orderedNames.size() == 1);
REQUIRE(orderedNames[0] == "Extension1");
}
SECTION("One extension with a dependency outside the loaded extensions") {
gd::SerializerElement extensionsElement = gd::Serializer::FromJSON(
R"([
{
"author": "",
"category": "Input",
"extensionNamespace": "",
"fullName": "3D character keyboard mapper",
"helpPath": "",
"iconUrl": "fake-icon-url",
"name": "Extension1DependsOtherExtension",
"previewIconUrl": "fake-preview-icon-url",
"shortDescription": "3D platformer and 3D shooter keyboard controls.",
"version": "1.0.0",
"description": "3D platformer and 3D shooter keyboard controls.",
"tags": [],
"authorIds": [],
"dependencies": [],
"globalVariables": [],
"sceneVariables": [],
"eventsFunctions": [],
"eventsBasedBehaviors": [],
"eventsBasedObjects": [
{
"areaMaxX": 64,
"areaMaxY": 64,
"areaMaxZ": 64,
"areaMinX": 0,
"areaMinY": 0,
"areaMinZ": 0,
"defaultName": "Joystick",
"description": "Joystick for touchscreens.",
"fullName": "Multitouch Joystick",
"name": "SpriteMultitouchJoystick",
"eventsFunctions": [],
"propertyDescriptors": [],
"objects": [
{
"name": "Thumb",
"type": "OtherExtension::Whatever"
}
],
"objectsFolderStructure": {
"folderName": "__ROOT",
"children": []
},
"objectsGroups": [],
"layers": [],
"instances": []
}
]
}
])");
std::vector<gd::String> orderedNames =
gd::Project::GetUnserializingOrderExtensionNames(extensionsElement);
REQUIRE(orderedNames.size() == 1);
REQUIRE(orderedNames[0] == "Extension1DependsOtherExtension");
}
SECTION("4 extensions with dependencies on each others") {
gd::SerializerElement extensionsElement = gd::Serializer::FromJSON(
R"([
{
"author": "",
"category": "Input",
"extensionNamespace": "",
"fullName": "3D character keyboard mapper",
"helpPath": "",
"iconUrl": "fake-icon-url",
"name": "Extension4DependsOn1And3",
"previewIconUrl": "fake-preview-icon-url",
"shortDescription": "3D platformer and 3D shooter keyboard controls.",
"version": "1.0.0",
"description": "3D platformer and 3D shooter keyboard controls.",
"tags": [],
"authorIds": [],
"dependencies": [],
"globalVariables": [],
"sceneVariables": [],
"eventsFunctions": [],
"eventsBasedBehaviors": [],
"eventsBasedObjects": [
{
"areaMaxX": 64,
"areaMaxY": 64,
"areaMaxZ": 64,
"areaMinX": 0,
"areaMinY": 0,
"areaMinZ": 0,
"defaultName": "Joystick",
"description": "Joystick for touchscreens.",
"fullName": "Multitouch Joystick",
"name": "SpriteMultitouchJoystick",
"eventsFunctions": [],
"propertyDescriptors": [],
"objects": [
{
"name": "Thumb",
"type": "OtherExtension::Whatever"
},
{
"name": "Thumb2",
"type": "Extension1DependsNothing::Whatever"
}
],
"objectsFolderStructure": {
"folderName": "__ROOT",
"children": []
},
"objectsGroups": [],
"layers": [],
"instances": []
},
{
"areaMaxX": 64,
"areaMaxY": 64,
"areaMaxZ": 64,
"areaMinX": 0,
"areaMinY": 0,
"areaMinZ": 0,
"defaultName": "Joystick",
"description": "Joystick for touchscreens.",
"fullName": "Multitouch Joystick",
"name": "SpriteMultitouchJoystick",
"eventsFunctions": [],
"propertyDescriptors": [],
"objects": [
{
"name": "Thumb",
"type": "OtherExtension::Whatever"
},
{
"name": "Thumb2",
"type": "Extension3DependingOn2::Whatever"
}
],
"objectsFolderStructure": {
"folderName": "__ROOT",
"children": []
},
"objectsGroups": [],
"layers": [],
"instances": []
}
]
},
{
"author": "",
"category": "Input",
"extensionNamespace": "",
"fullName": "3D character keyboard mapper",
"helpPath": "",
"iconUrl": "fake-icon-url",
"name": "Extension3DependingOn2",
"previewIconUrl": "fake-preview-icon-url",
"shortDescription": "3D platformer and 3D shooter keyboard controls.",
"version": "1.0.0",
"description": "3D platformer and 3D shooter keyboard controls.",
"tags": [],
"authorIds": [],
"dependencies": [],
"globalVariables": [],
"sceneVariables": [],
"eventsFunctions": [],
"eventsBasedBehaviors": [],
"eventsBasedObjects": [
{
"areaMaxX": 64,
"areaMaxY": 64,
"areaMaxZ": 64,
"areaMinX": 0,
"areaMinY": 0,
"areaMinZ": 0,
"defaultName": "Joystick",
"description": "Joystick for touchscreens.",
"fullName": "Multitouch Joystick",
"name": "SpriteMultitouchJoystick",
"eventsFunctions": [],
"propertyDescriptors": [],
"objects": [
{
"name": "Thumb",
"type": "OtherExtension::Whatever"
},
{
"name": "Thumb2",
"type": "Extension2DependsNothing::Whatever"
}
],
"objectsFolderStructure": {
"folderName": "__ROOT",
"children": []
},
"objectsGroups": [],
"layers": [],
"instances": []
}
]
},
{
"author": "",
"category": "Input",
"extensionNamespace": "",
"fullName": "3D character keyboard mapper",
"helpPath": "",
"iconUrl": "fake-icon-url",
"name": "Extension2DependsNothing",
"previewIconUrl": "fake-preview-icon-url",
"shortDescription": "3D platformer and 3D shooter keyboard controls.",
"version": "1.0.0",
"description": "3D platformer and 3D shooter keyboard controls.",
"tags": [],
"authorIds": [],
"dependencies": [],
"globalVariables": [],
"sceneVariables": [],
"eventsFunctions": [],
"eventsBasedBehaviors": [],
"eventsBasedObjects": []
},
{
"author": "",
"category": "Input",
"extensionNamespace": "",
"fullName": "3D character keyboard mapper",
"helpPath": "",
"iconUrl": "fake-icon-url",
"name": "Extension1DependsNothing",
"previewIconUrl": "fake-preview-icon-url",
"shortDescription": "3D platformer and 3D shooter keyboard controls.",
"version": "1.0.0",
"description": "3D platformer and 3D shooter keyboard controls.",
"tags": [],
"authorIds": [],
"dependencies": [],
"globalVariables": [],
"sceneVariables": [],
"eventsFunctions": [],
"eventsBasedBehaviors": [],
"eventsBasedObjects": []
}
])");
std::vector<gd::String> orderedNames =
gd::Project::GetUnserializingOrderExtensionNames(extensionsElement);
REQUIRE(orderedNames.size() == 4);
REQUIRE(orderedNames[0] == "Extension2DependsNothing");
REQUIRE(orderedNames[1] == "Extension1DependsNothing");
REQUIRE(orderedNames[2] == "Extension3DependingOn2");
REQUIRE(orderedNames[3] == "Extension4DependsOn1And3");
}
}