Skip to content

Commit

Permalink
Add a button to export objects as assets
Browse files Browse the repository at this point in the history
  • Loading branch information
D8H committed Dec 1, 2023
1 parent a9cc911 commit 0a13447
Show file tree
Hide file tree
Showing 22 changed files with 603 additions and 47 deletions.
110 changes: 110 additions & 0 deletions Core/GDCore/IDE/ObjectAssetSerializer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* GDevelop Core
* Copyright 2008-2016 Florian Rival ([email protected]). All rights
* reserved. This project is released under the MIT License.
*/
#include "ObjectAssetSerializer.h"

#include "GDCore/Extensions/Metadata/BehaviorMetadata.h"
#include "GDCore/Extensions/Metadata/MetadataProvider.h"
#include "GDCore/Extensions/Platform.h"
#include "GDCore/Extensions/PlatformExtension.h"
#include "GDCore/IDE/Project/ResourcesInUseHelper.h"
#include "GDCore/Project/Behavior.h"
#include "GDCore/Project/CustomBehavior.h"
#include "GDCore/Project/EventsFunctionsExtension.h"
#include "GDCore/Project/Layout.h"
#include "GDCore/Project/Object.h"
#include "GDCore/Project/Project.h"
#include "GDCore/Project/PropertyDescriptor.h"
#include "GDCore/Serialization/SerializerElement.h"
#include "GDCore/Tools/Log.h"


namespace gd {

const std::vector<gd::String> ObjectAssetSerializer::resourceTypes = {
"image", "audio", "font", "json",
"tilemap", "tileset", "video", "bitmapFont"};

void ObjectAssetSerializer::SerializeTo(gd::Project &project,
const gd::Object &object,
SerializerElement &element) {
auto cleanObject = object.Clone();
cleanObject->GetVariables().Clear();
cleanObject->GetEffects().Clear();
for (auto &&behaviorName : cleanObject->GetAllBehaviorNames()) {
cleanObject->RemoveBehavior(behaviorName);
}

const gd::String &type = cleanObject->GetType();
const auto separatorIndex =
type.find(PlatformExtension::GetNamespaceSeparator());
gd::String extensionName =
separatorIndex != std::string::npos ? type.substr(0, separatorIndex) : "";

element.SetAttribute("id", "");
element.SetAttribute("name", "");
element.SetAttribute("license", "");
if (project.HasEventsFunctionsExtensionNamed(extensionName)) {
auto &extension = project.GetEventsFunctionsExtension(extensionName);
element.SetAttribute("description", extension.GetShortDescription());
}
element.SetAttribute("gdevelopVersion", "");
element.SetAttribute("version", "");
element.SetIntAttribute("animationsCount", 1);
element.SetIntAttribute("maxFramesCount", 1);
// TODO Find the right object dimensions.
element.SetIntAttribute("width", 0);
element.SetIntAttribute("height", 0);
SerializerElement &authorsElement = element.AddChild("authors");
authorsElement.ConsiderAsArrayOf("author");
SerializerElement &tagsElement = element.AddChild("tags");
tagsElement.ConsiderAsArrayOf("tag");

SerializerElement &objectAssetsElement = element.AddChild("objectAssets");
objectAssetsElement.ConsiderAsArrayOf("objectAsset");
SerializerElement &objectAssetElement =
objectAssetsElement.AddChild("objectAsset");

cleanObject->SerializeTo(objectAssetElement.AddChild("object"));

SerializerElement &resourcesElement =
objectAssetElement.AddChild("resources");
resourcesElement.ConsiderAsArrayOf("resource");
auto &resourcesManager = project.GetResourcesManager();
gd::ResourcesInUseHelper resourcesInUse(resourcesManager);
cleanObject->GetConfiguration().ExposeResources(resourcesInUse);
for (auto &&resourceType : resourceTypes) {
for (auto &&resourceName : resourcesInUse.GetAll(resourceType)) {
if (resourceName.length() == 0) {
continue;
}
auto &resource = resourcesManager.GetResource(resourceName);
SerializerElement &resourceElement =
resourcesElement.AddChild("resource");
resourceElement.SetAttribute("name", resourceName);
resourceElement.SetAttribute("file", resource.GetFile());
resourceElement.SetAttribute("kind", resource.GetKind());
resourceElement.SetBoolAttribute("alwaysLoaded", false);
resourceElement.SetAttribute("metadata", resource.GetMetadata());
}
}

SerializerElement &requiredExtensionsElement =
objectAssetElement.AddChild("requiredExtensions");
requiredExtensionsElement.ConsiderAsArrayOf("requiredExtension");
if (project.HasEventsFunctionsExtensionNamed(extensionName)) {
SerializerElement &requiredExtensionElement =
requiredExtensionsElement.AddChild("requiredExtension");
requiredExtensionElement.SetAttribute("extensionName", extensionName);
requiredExtensionElement.SetAttribute("extensionVersion", "1.0.0");
}

// TODO This can be removed when the asset script no longer require it.
SerializerElement &customizationElement =
objectAssetElement.AddChild("customization");
customizationElement.ConsiderAsArrayOf("empty");
}

} // namespace gd
46 changes: 46 additions & 0 deletions Core/GDCore/IDE/ObjectAssetSerializer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* GDevelop Core
* Copyright 2008-2016 Florian Rival ([email protected]). All rights
* reserved. This project is released under the MIT License.
*/
#pragma once
#include <vector>

#include "GDCore/String.h"

namespace gd {
class Object;
class ExtensionDependency;
class PropertyDescriptor;
class Project;
class Layout;
class ArbitraryResourceWorker;
class InitialInstance;
class SerializerElement;
class EffectsContainer;
} // namespace gd

namespace gd {

/**
* \brief Serialize objects into an asset for the store.
*
* \ingroup IDE
*/
class GD_CORE_API ObjectAssetSerializer {
public:
/**
* \brief Serialize the object into an asset.
*/
static void SerializeTo(gd::Project &project, const gd::Object &object,
SerializerElement &element);

~ObjectAssetSerializer(){};

private:
ObjectAssetSerializer(){};

static const std::vector<gd::String> resourceTypes;
};

} // namespace gd
56 changes: 56 additions & 0 deletions Core/GDCore/IDE/Project/AssetResourcesMergingHelper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* GDevelop Core
* Copyright 2008-2016 Florian Rival ([email protected]). All rights
* reserved. This project is released under the MIT License.
*/

#include "AssetResourcesMergingHelper.h"
#include "GDCore/Project/Project.h"
#include "GDCore/Project/ResourcesManager.h"
#include "GDCore/String.h"


namespace gd {

void AssetResourcesMergingHelper::ExposeImage(gd::String &imageName) {
ExposeResourceAsFile(imageName);
}

void AssetResourcesMergingHelper::ExposeAudio(gd::String &audioName) {
ExposeResourceAsFile(audioName);
}

void AssetResourcesMergingHelper::ExposeFont(gd::String &fontName) {
ExposeResourceAsFile(fontName);
}

void AssetResourcesMergingHelper::ExposeJson(gd::String &jsonName) {
ExposeResourceAsFile(jsonName);
}

void AssetResourcesMergingHelper::ExposeTilemap(gd::String &tilemapName) {
ExposeResourceAsFile(tilemapName);
}

void AssetResourcesMergingHelper::ExposeTileset(gd::String &tilesetName) {
ExposeResourceAsFile(tilesetName);
}

void AssetResourcesMergingHelper::ExposeVideo(gd::String &videoName) {
ExposeResourceAsFile(videoName);
}

void AssetResourcesMergingHelper::ExposeBitmapFont(gd::String &bitmapFontName) {
ExposeResourceAsFile(bitmapFontName);
}

void AssetResourcesMergingHelper::ExposeResourceAsFile(
gd::String &resourceName) {

auto &resource = project.GetResourcesManager().GetResource(resourceName);
gd::String file = resource.GetFile();
ExposeFile(file);
resourceName = file;
}

} // namespace gd
54 changes: 54 additions & 0 deletions Core/GDCore/IDE/Project/AssetResourcesMergingHelper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* GDevelop Core
* Copyright 2008-2016 Florian Rival ([email protected]). All rights
* reserved. This project is released under the MIT License.
*/
#pragma once

#include "GDCore/IDE/Project/ArbitraryResourceWorker.h"
#include "GDCore/IDE/Project/ResourcesMergingHelper.h"
#include "GDCore/String.h"
#include <map>
#include <memory>
#include <vector>

namespace gd {
class AbstractFileSystem;
class Project;
} // namespace gd

namespace gd {

/**
* \brief ResourcesMergingHelper is used (mainly during export)
* to list resources and generate new filenames, to allow them to be all copied
* in a single directory (potentially changing the filename to avoid conflicts,
* but preserving extensions).
*
* \see ArbitraryResourceWorker
*
* \ingroup IDE
*/
class GD_CORE_API AssetResourcesMergingHelper : public ResourcesMergingHelper {
public:
AssetResourcesMergingHelper(gd::Project &project_,
gd::AbstractFileSystem &fileSystem)
: ResourcesMergingHelper(project_.GetResourcesManager(), fileSystem), project(project_){};
virtual ~AssetResourcesMergingHelper(){};

void ExposeImage(gd::String &imageName) override;
void ExposeAudio(gd::String &audioName) override;
void ExposeFont(gd::String &fontName) override;
void ExposeJson(gd::String &jsonName) override;
void ExposeTilemap(gd::String &tilemapName) override;
void ExposeTileset(gd::String &tilesetName) override;
void ExposeVideo(gd::String &videoName) override;
void ExposeBitmapFont(gd::String &bitmapFontName) override;

protected:
void ExposeResourceAsFile(gd::String &resourceName);

gd::Project &project;
};

} // namespace gd
Loading

0 comments on commit 0a13447

Please sign in to comment.