-
Notifications
You must be signed in to change notification settings - Fork 927
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure behaviors are compatible with an object before pasting them
- Loading branch information
Showing
8 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* GDevelop Core | ||
* Copyright 2008-2025 Florian Rival ([email protected]). All rights | ||
* reserved. This project is released under the MIT License. | ||
*/ | ||
#include "ObjectTools.h" | ||
|
||
#include "GDCore/Extensions/Metadata/BehaviorMetadata.h" | ||
#include "GDCore/Extensions/Metadata/ObjectMetadata.h" | ||
#include "GDCore/Extensions/Metadata/MetadataProvider.h" | ||
#include "GDCore/Extensions/Platform.h" | ||
|
||
namespace gd { | ||
|
||
bool ObjectTools::IsBehaviorCompatibleWithObject( | ||
const gd::Platform &platform, const gd::String &objectType, | ||
const gd::String &behaviorType, | ||
std::unordered_set<gd::String> coveredBehaviorType) { | ||
bool isBehaviorTypeAlreadyCovered = | ||
!coveredBehaviorType.insert(behaviorType).second; | ||
if (isBehaviorTypeAlreadyCovered) { | ||
return true; | ||
} | ||
const gd::BehaviorMetadata &behaviorMetadata = | ||
MetadataProvider::GetBehaviorMetadata(platform, behaviorType); | ||
if (MetadataProvider::IsBadBehaviorMetadata(behaviorMetadata)) { | ||
// Should not happen because the behavior was added successfully (so its | ||
// metadata are valid) - but double check anyway and bail out if the | ||
// behavior metadata are invalid. | ||
return false; | ||
} | ||
if (!behaviorMetadata.GetObjectType().empty() && | ||
behaviorMetadata.GetObjectType() != objectType) { | ||
return false; | ||
} | ||
for (const gd::String &requiredBehaviorType : | ||
behaviorMetadata.GetRequiredBehaviorTypes()) { | ||
const gd::BehaviorMetadata &requiredBehaviorMetadata = | ||
gd::MetadataProvider::GetBehaviorMetadata(platform, requiredBehaviorType); | ||
if (requiredBehaviorMetadata.IsHidden()) { | ||
const gd::ObjectMetadata &objectMetadata = | ||
gd::MetadataProvider::GetObjectMetadata(platform, objectType); | ||
if (objectMetadata.GetDefaultBehaviors().find(requiredBehaviorType) == | ||
objectMetadata.GetDefaultBehaviors().end()) { | ||
// A capability is missing in the object. | ||
return false; | ||
} | ||
} | ||
if (!gd::ObjectTools::IsBehaviorCompatibleWithObject( | ||
platform, objectType, requiredBehaviorType, coveredBehaviorType)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
} // namespace gd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* GDevelop Core | ||
* Copyright 2008-2025 Florian Rival ([email protected]). All rights | ||
* reserved. This project is released under the MIT License. | ||
*/ | ||
#pragma once | ||
|
||
#include "GDCore/String.h" | ||
#include <unordered_set> | ||
|
||
namespace gd { | ||
class Platform; | ||
class Object; | ||
} // namespace gd | ||
|
||
namespace gd { | ||
|
||
class GD_CORE_API ObjectTools { | ||
public: | ||
static bool IsBehaviorCompatibleWithObject(const gd::Platform &platform, | ||
const gd::String &objectType, | ||
const gd::String &behaviorType) { | ||
std::unordered_set<gd::String> coveredBehaviorType; | ||
return IsBehaviorCompatibleWithObject(platform, objectType, behaviorType, | ||
coveredBehaviorType); | ||
} | ||
|
||
private: | ||
static bool IsBehaviorCompatibleWithObject( | ||
const gd::Platform &platform, const gd::String &objectType, | ||
const gd::String &behaviorType, | ||
std::unordered_set<gd::String> coveredBehaviorType); | ||
}; | ||
|
||
} // namespace gd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Automatically generated by GDevelop.js/scripts/generate-types.js | ||
declare class gdObjectTools { | ||
static isBehaviorCompatibleWithObject(platform: gdPlatform, objectType: string, behaviorType: string): boolean; | ||
delete(): void; | ||
ptr: number; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters