Tighten editor scene mutation boundary

This commit is contained in:
2026-04-28 17:53:36 +08:00
parent 357dc136fe
commit b67af931de
12 changed files with 290 additions and 62 deletions

View File

@@ -177,7 +177,7 @@ GameObject* FindGameObjectByItemId(
SceneManager& sceneManager,
std::string_view itemId) {
Scene* scene = ResolvePrimaryScene(sceneManager);
const std::optional<GameObject::ID> gameObjectId =
const std::optional<EditorSceneObjectId> gameObjectId =
ParseEditorGameObjectItemId(itemId);
if (scene == nullptr || !gameObjectId.has_value()) {
return nullptr;
@@ -186,6 +186,27 @@ GameObject* FindGameObjectByItemId(
return scene->FindByID(gameObjectId.value());
}
EditorSceneHierarchyNode BuildHierarchySnapshotNodeRecursive(
const GameObject& gameObject) {
EditorSceneHierarchyNode node = {};
node.itemId = MakeEditorGameObjectItemId(gameObject.GetID());
node.displayName = gameObject.GetName().empty()
? std::string("GameObject")
: gameObject.GetName();
node.children.reserve(gameObject.GetChildCount());
for (std::size_t childIndex = 0u;
childIndex < gameObject.GetChildCount();
++childIndex) {
const GameObject* child = gameObject.GetChild(childIndex);
if (child == nullptr) {
continue;
}
node.children.push_back(BuildHierarchySnapshotNodeRecursive(*child));
}
return node;
}
bool MoveGameObjectRelativeToTarget(
SceneManager& sceneManager,
std::string_view itemId,
@@ -313,6 +334,25 @@ public:
return ResolvePrimaryScene(m_sceneManager);
}
EditorSceneHierarchySnapshot BuildHierarchySnapshot() const override {
EditorSceneHierarchySnapshot snapshot = {};
Scene* scene = ResolvePrimaryScene(m_sceneManager);
if (scene == nullptr) {
return snapshot;
}
const std::vector<GameObject*> roots = scene->GetRootGameObjects();
snapshot.roots.reserve(roots.size());
for (const GameObject* root : roots) {
if (root == nullptr) {
continue;
}
snapshot.roots.push_back(BuildHierarchySnapshotNodeRecursive(*root));
}
return snapshot;
}
bool OpenSceneAsset(const std::filesystem::path& scenePath) override {
if (scenePath.empty()) {
return false;
@@ -344,6 +384,25 @@ public:
return FindGameObjectByItemId(m_sceneManager, itemId);
}
bool AddComponent(
std::string_view itemId,
std::string_view componentTypeName) override {
if (componentTypeName.empty()) {
return false;
}
GameObject* gameObject = FindGameObject(itemId);
if (gameObject == nullptr) {
return false;
}
Component* addedComponent =
ComponentFactoryRegistry::Get().CreateComponent(
gameObject,
std::string(componentTypeName));
return addedComponent != nullptr;
}
bool RenameGameObject(
std::string_view itemId,
std::string_view newName) override {