Skip to content

Commit

Permalink
Fix resource used in behavior properties not being exported (#7084)
Browse files Browse the repository at this point in the history
Only show in developer changelog
  • Loading branch information
D8H authored Oct 18, 2024
1 parent 228479c commit d66ea06
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Core/GDCore/IDE/Project/ArbitraryResourceWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ void ResourceWorkerInObjectsWorker::DoVisitObject(gd::Object &object) {
};

void ResourceWorkerInObjectsWorker::DoVisitBehavior(gd::Behavior &behavior){
// TODO Allow behaviors to expose resources
behavior.ExposeResources(worker);
};

gd::ResourceWorkerInObjectsWorker
Expand Down
45 changes: 45 additions & 0 deletions Core/GDCore/Project/BehaviorConfigurationContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "GDCore/Project/BehaviorConfigurationContainer.h"
#include <iostream>
#include "GDCore/Project/PropertyDescriptor.h"
#include "GDCore/IDE/Project/ArbitraryResourceWorker.h"

namespace gd {

Expand All @@ -22,4 +23,48 @@ std::map<gd::String, gd::PropertyDescriptor> BehaviorConfigurationContainer::Get
return nothing;
}

void BehaviorConfigurationContainer::ExposeResources(gd::ArbitraryResourceWorker& worker) {
std::map<gd::String, gd::PropertyDescriptor> properties = GetProperties();

for (auto& property : properties) {
const String& propertyName = property.first;
const gd::PropertyDescriptor& propertyDescriptor = property.second;

if (propertyDescriptor.GetType().LowerCase() == "resource") {
auto& extraInfo = propertyDescriptor.GetExtraInfo();
const gd::String& resourceType = extraInfo.empty() ? "" : extraInfo[0];
const gd::String& oldPropertyValue = propertyDescriptor.GetValue();

gd::String newPropertyValue = oldPropertyValue;
if (resourceType == "image") {
worker.ExposeImage(newPropertyValue);
} else if (resourceType == "audio") {
worker.ExposeAudio(newPropertyValue);
} else if (resourceType == "font") {
worker.ExposeFont(newPropertyValue);
} else if (resourceType == "video") {
worker.ExposeVideo(newPropertyValue);
} else if (resourceType == "json") {
worker.ExposeJson(newPropertyValue);
} else if (resourceType == "tilemap") {
worker.ExposeTilemap(newPropertyValue);
} else if (resourceType == "tileset") {
worker.ExposeTileset(newPropertyValue);
} else if (resourceType == "bitmapFont") {
worker.ExposeBitmapFont(newPropertyValue);
} else if (resourceType == "model3D") {
worker.ExposeModel3D(newPropertyValue);
} else if (resourceType == "atlas") {
worker.ExposeAtlas(newPropertyValue);
} else if (resourceType == "spine") {
worker.ExposeSpine(newPropertyValue);
}

if (newPropertyValue != oldPropertyValue) {
UpdateProperty(propertyName, newPropertyValue);
}
}
}
}

} // namespace gd
18 changes: 14 additions & 4 deletions Core/GDCore/Project/BehaviorConfigurationContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
* Copyright 2008-2016 Florian Rival ([email protected]). All rights
* reserved. This project is released under the MIT License.
*/
#ifndef GDCORE_BEHAVIORCONFIGURATIONCONTAINER_H
#define GDCORE_BEHAVIORCONFIGURATIONCONTAINER_H
#pragma once

#include <map>
#include <memory>
Expand All @@ -19,6 +18,7 @@ class PropertyDescriptor;
class SerializerElement;
class Project;
class Layout;
class ArbitraryResourceWorker;
} // namespace gd

namespace gd {
Expand Down Expand Up @@ -158,6 +158,18 @@ class GD_CORE_API BehaviorConfigurationContainer {
return propertiesQuickCustomizationVisibilities;
}

/**
* \brief Called ( e.g. during compilation ) so as to inventory internal
* resources and sometimes update their filename. Implementation example:
* \code
* worker.ExposeImage(myImage);
* worker.ExposeFile(myResourceFile);
* \endcode
*
* \see ArbitraryResourceWorker
*/
void ExposeResources(gd::ArbitraryResourceWorker& worker);

protected:
/**
* \brief Called when the IDE wants to know about the custom properties of the
Expand Down Expand Up @@ -209,5 +221,3 @@ class GD_CORE_API BehaviorConfigurationContainer {
};

} // namespace gd

#endif // GDCORE_BEHAVIORCONFIGURATIONCONTAINER_H
4 changes: 2 additions & 2 deletions Core/GDCore/Project/CustomObjectConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ void CustomObjectConfiguration::ExposeResources(gd::ArbitraryResourceWorker& wor

for (auto& property : properties) {
const String& propertyName = property.first;
const gd::PropertyDescriptor& propertyDescriptor = property.second;
if (propertyDescriptor.GetType() == "resource") {
const gd::PropertyDescriptor &propertyDescriptor = property.second;
if (propertyDescriptor.GetType().LowerCase() == "resource") {
auto& extraInfo = propertyDescriptor.GetExtraInfo();
const gd::String& resourceType = extraInfo.empty() ? "" : extraInfo[0];
const gd::String& oldPropertyValue = propertyDescriptor.GetValue();
Expand Down
31 changes: 31 additions & 0 deletions Core/tests/ArbitraryResourceWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,37 @@ TEST_CASE("ArbitraryResourceWorker", "[common][resources]") {
REQUIRE(worker.images[0] == "res1");
}

SECTION("Can find resource usages in behavior configurations") {
gd::Platform platform;
gd::Project project;
SetupProjectWithDummyPlatform(project, platform);

project.GetResourcesManager().AddResource(
"res1", "path/to/file1.png", "image");
project.GetResourcesManager().AddResource(
"res2", "path/to/file2.png", "image");
project.GetResourcesManager().AddResource(
"res3", "path/to/file3.png", "image");
project.GetResourcesManager().AddResource(
"res4", "path/to/file4.png", "audio");
ArbitraryResourceWorkerTest worker(project.GetResourcesManager());

auto &layout = project.InsertNewLayout("Scene", 0);
auto &object = layout.GetObjects().InsertNewObject(
project, "MyExtension::Sprite", "MyObject", 0);
auto *behavior = object.AddNewBehavior(
project, "MyExtension::BehaviorWithRequiredBehaviorProperty",
"BehaviorWithResource");
behavior->UpdateProperty("resourceProperty", "res1");

worker.files.clear();
worker.images.clear();
gd::ResourceExposer::ExposeWholeProjectResources(project, worker);
REQUIRE(worker.files.size() == 4);
REQUIRE(worker.images.size() == 1);
REQUIRE(worker.images[0] == "res1");
}

SECTION("Can find resource usages in layout events") {
gd::Platform platform;
gd::Project project;
Expand Down
13 changes: 12 additions & 1 deletion Core/tests/DummyPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,31 @@ class BehaviorWithRequiredBehaviorProperty : public gd::Behavior {
behaviorContent.GetStringAttribute("requiredBehaviorProperty"))
.SetType("Behavior")
.AddExtraInfo("MyExtension::MyBehavior");
properties["resourceProperty"]
.SetLabel("A resource")
.SetValue(
behaviorContent.GetStringAttribute("resourceProperty"))
.SetType("Resource")
.AddExtraInfo("image");
return properties;
}
virtual bool UpdateProperty(gd::SerializerElement& behaviorContent,
const gd::String& name,
const gd::String& value) override {
if (name == _("requiredBehaviorProperty")) {
if (name == "requiredBehaviorProperty") {
behaviorContent.SetAttribute("requiredBehaviorProperty", value);
return true;
}
if (name == "resourceProperty") {
behaviorContent.SetAttribute("resourceProperty", value);
return true;
}
return false;
}
virtual void InitializeContent(
gd::SerializerElement& behaviorContent) override {
behaviorContent.SetAttribute("requiredBehaviorProperty", "");
behaviorContent.SetAttribute("resourceProperty", "");
}
};

Expand Down

0 comments on commit d66ea06

Please sign in to comment.