Prepare script lifecycle and data layer

This commit is contained in:
2026-03-26 20:14:58 +08:00
parent 5ca5ca1f19
commit 0921f2a459
20 changed files with 1367 additions and 26 deletions

View File

@@ -39,6 +39,8 @@ add_subdirectory(threading)
add_subdirectory(debug)
add_subdirectory(components)
add_subdirectory(scene)
add_subdirectory(scripting)
add_subdirectory(Rendering)
add_subdirectory(rhi)
add_subdirectory(resources)
add_subdirectory(input)

View File

@@ -21,12 +21,14 @@ public:
bool m_onEnableCalled = false;
bool m_onDisableCalled = false;
bool m_onDestroyCalled = false;
int m_onEnableCount = 0;
int m_onDisableCount = 0;
void Awake() override { m_awakeCalled = true; }
void Start() override { m_startCalled = true; }
void Update(float deltaTime) override { m_updateCalled = true; }
void OnEnable() override { m_onEnableCalled = true; }
void OnDisable() override { m_onDisableCalled = true; }
void OnEnable() override { m_onEnableCalled = true; ++m_onEnableCount; }
void OnDisable() override { m_onDisableCalled = true; ++m_onDisableCount; }
void OnDestroy() override { m_onDestroyCalled = true; }
private:
@@ -107,4 +109,24 @@ TEST(Component_Test, SetEnabled_NoCallback_WhenStateUnchanged) {
EXPECT_FALSE(comp.m_onDisableCalled);
}
} // namespace
TEST(Component_Test, SetEnabled_AttachedInactiveGameObject_DelaysOnEnableUntilActivation) {
GameObject go;
TestComponent* comp = go.AddComponent<TestComponent>();
go.SetActive(false);
comp->m_onEnableCalled = false;
comp->m_onDisableCalled = false;
comp->m_onEnableCount = 0;
comp->m_onDisableCount = 0;
comp->SetEnabled(false);
EXPECT_EQ(comp->m_onDisableCount, 0);
comp->SetEnabled(true);
EXPECT_EQ(comp->m_onEnableCount, 0);
go.SetActive(true);
EXPECT_EQ(comp->m_onEnableCount, 1);
}
} // namespace

View File

@@ -21,10 +21,21 @@ public:
bool m_awakeCalled = false;
bool m_startCalled = false;
bool m_updateCalled = false;
bool m_onEnableCalled = false;
bool m_onDisableCalled = false;
bool m_onDestroyCalled = false;
int m_startCount = 0;
int m_updateCount = 0;
int m_onEnableCount = 0;
int m_onDisableCount = 0;
int m_onDestroyCount = 0;
void Awake() override { m_awakeCalled = true; }
void Start() override { m_startCalled = true; }
void Update(float deltaTime) override { m_updateCalled = true; }
void Start() override { m_startCalled = true; ++m_startCount; }
void Update(float deltaTime) override { m_updateCalled = true; ++m_updateCount; }
void OnEnable() override { m_onEnableCalled = true; ++m_onEnableCount; }
void OnDisable() override { m_onDisableCalled = true; ++m_onDisableCount; }
void OnDestroy() override { m_onDestroyCalled = true; ++m_onDestroyCount; }
private:
std::string m_customName;
@@ -45,7 +56,6 @@ TEST(GameObject_Test, DefaultConstructor_DefaultValues) {
EXPECT_EQ(go.GetName(), "GameObject");
EXPECT_TRUE(go.IsActive());
EXPECT_NE(go.GetID(), GameObject::INVALID_ID);
EXPECT_EQ(go.GetID(), 1u);
}
TEST(GameObject_Test, NamedConstructor) {
@@ -203,6 +213,34 @@ TEST(GameObject_Test, SetActive_False) {
EXPECT_FALSE(go.IsActive());
}
TEST(GameObject_Test, SetActive_PropagatesEnableDisableToChildren) {
GameObject parent("Parent");
GameObject child("Child");
TestComponent* comp = child.AddComponent<TestComponent>();
child.SetParent(&parent);
parent.SetActive(false);
EXPECT_EQ(comp->m_onDisableCount, 1);
parent.SetActive(true);
EXPECT_EQ(comp->m_onEnableCount, 1);
}
TEST(GameObject_Test, SetParent_PropagatesActiveHierarchyChanges) {
GameObject activeParent("ActiveParent");
GameObject inactiveParent("InactiveParent");
GameObject child("Child");
TestComponent* comp = child.AddComponent<TestComponent>();
inactiveParent.SetActive(false);
child.SetParent(&inactiveParent);
EXPECT_EQ(comp->m_onDisableCount, 1);
child.SetParent(&activeParent);
EXPECT_EQ(comp->m_onEnableCount, 1);
}
TEST(GameObject_Test, IsActiveInHierarchy_True) {
GameObject parent("Parent");
GameObject child("Child");
@@ -238,6 +276,17 @@ TEST(GameObject_Test, Lifecycle_Start) {
go.Start();
EXPECT_TRUE(comp->m_startCalled);
EXPECT_EQ(comp->m_startCount, 1);
}
TEST(GameObject_Test, Lifecycle_Start_IsOnlyCalledOnce) {
GameObject go;
TestComponent* comp = go.AddComponent<TestComponent>();
go.Start();
go.Start();
EXPECT_EQ(comp->m_startCount, 1);
}
TEST(GameObject_Test, Lifecycle_Update) {
@@ -247,6 +296,17 @@ TEST(GameObject_Test, Lifecycle_Update) {
go.Update(0.016f);
EXPECT_TRUE(comp->m_updateCalled);
EXPECT_EQ(comp->m_updateCount, 1);
}
TEST(GameObject_Test, Destroy_CallsOnDestroyOnce_WhenStandalone) {
GameObject go;
TestComponent* comp = go.AddComponent<TestComponent>();
go.Destroy();
EXPECT_TRUE(comp->m_onDestroyCalled);
EXPECT_EQ(comp->m_onDestroyCount, 1);
}
TEST_F(GameObjectTest, Find_Exists) {

View File

@@ -27,9 +27,22 @@ public:
bool m_awakeCalled = false;
bool m_updateCalled = false;
bool m_onDestroyCalled = false;
int m_startCount = 0;
int m_updateCount = 0;
int m_onDestroyCount = 0;
int* m_externalOnDestroyCount = nullptr;
void Awake() override { m_awakeCalled = true; }
void Update(float deltaTime) override { m_updateCalled = true; }
void Start() override { ++m_startCount; }
void Update(float deltaTime) override { m_updateCalled = true; ++m_updateCount; }
void OnDestroy() override {
m_onDestroyCalled = true;
++m_onDestroyCount;
if (m_externalOnDestroyCount) {
++(*m_externalOnDestroyCount);
}
}
private:
std::string m_customName;
@@ -111,6 +124,17 @@ TEST_F(SceneTest, DestroyGameObject_WithChildren) {
EXPECT_EQ(testScene->Find("Child"), nullptr);
}
TEST_F(SceneTest, DestroyGameObject_CallsOnDestroyOnce) {
GameObject* go = testScene->CreateGameObject("DestroyMe");
TestComponent* comp = go->AddComponent<TestComponent>();
int destroyCount = 0;
comp->m_externalOnDestroyCount = &destroyCount;
testScene->DestroyGameObject(go);
EXPECT_EQ(destroyCount, 1);
}
TEST_F(SceneTest, Find_Exists) {
testScene->CreateGameObject("FindMe");
@@ -163,6 +187,28 @@ TEST_F(SceneTest, Update_SkipsInactiveObjects) {
EXPECT_FALSE(comp->m_updateCalled);
}
TEST_F(SceneTest, Update_RecursivelyUpdatesChildObjects) {
GameObject* parent = testScene->CreateGameObject("Parent");
GameObject* child = testScene->CreateGameObject("Child", parent);
TestComponent* comp = child->AddComponent<TestComponent>();
testScene->Update(0.016f);
EXPECT_TRUE(comp->m_updateCalled);
EXPECT_EQ(comp->m_updateCount, 1);
}
TEST_F(SceneTest, Update_CallsStartOnlyOnceBeforeUpdate) {
GameObject* go = testScene->CreateGameObject("TestObject");
TestComponent* comp = go->AddComponent<TestComponent>();
testScene->Update(0.016f);
testScene->Update(0.016f);
EXPECT_EQ(comp->m_startCount, 1);
EXPECT_EQ(comp->m_updateCount, 2);
}
TEST(Scene_Test, IsActive_DefaultTrue) {
Scene s;
@@ -415,6 +461,27 @@ TEST_F(SceneTest, SerializeToString_And_DeserializeFromString_PreservesHierarchy
EXPECT_TRUE(loadedCamera->IsPrimary());
}
TEST_F(SceneTest, SerializeToString_And_DeserializeFromString_PreservesUUIDs) {
GameObject* parent = testScene->CreateGameObject("Parent");
GameObject* child = testScene->CreateGameObject("Child", parent);
const uint64_t parentUUID = parent->GetUUID();
const uint64_t childUUID = child->GetUUID();
const std::string serialized = testScene->SerializeToString();
Scene loadedScene;
loadedScene.DeserializeFromString(serialized);
GameObject* loadedParent = loadedScene.Find("Parent");
GameObject* loadedChild = loadedScene.Find("Child");
ASSERT_NE(loadedParent, nullptr);
ASSERT_NE(loadedChild, nullptr);
EXPECT_EQ(loadedParent->GetUUID(), parentUUID);
EXPECT_EQ(loadedChild->GetUUID(), childUUID);
}
TEST_F(SceneTest, Save_ContainsHierarchyAndComponentEntries) {
GameObject* parent = testScene->CreateGameObject("Parent");
GameObject* child = testScene->CreateGameObject("Child", parent);

View File

@@ -0,0 +1,29 @@
cmake_minimum_required(VERSION 3.15)
project(XCEngine_ScriptingTests)
set(SCRIPTING_TEST_SOURCES
test_script_field_storage.cpp
test_script_component.cpp
)
add_executable(scripting_tests ${SCRIPTING_TEST_SOURCES})
if(MSVC)
set_target_properties(scripting_tests PROPERTIES
LINK_FLAGS "/NODEFAULTLIB:libcpmt.lib /NODEFAULTLIB:libcmt.lib"
)
endif()
target_link_libraries(scripting_tests PRIVATE
XCEngine
GTest::gtest
GTest::gtest_main
)
target_include_directories(scripting_tests PRIVATE
${CMAKE_SOURCE_DIR}/engine/include
)
include(GoogleTest)
gtest_discover_tests(scripting_tests)

View File

@@ -0,0 +1,143 @@
#include <gtest/gtest.h>
#include <XCEngine/Components/ComponentFactoryRegistry.h>
#include <XCEngine/Components/GameObject.h>
#include <XCEngine/Scene/Scene.h>
#include <XCEngine/Scripting/ScriptComponent.h>
#include <sstream>
using namespace XCEngine::Components;
using namespace XCEngine::Scripting;
namespace {
TEST(ScriptComponent_Test, DefaultsAreInitializedForNativeDataLayer) {
ScriptComponent component;
EXPECT_EQ(component.GetName(), "ScriptComponent");
EXPECT_NE(component.GetScriptComponentUUID(), 0u);
EXPECT_EQ(component.GetAssemblyName(), "GameScripts");
EXPECT_TRUE(component.GetNamespaceName().empty());
EXPECT_TRUE(component.GetClassName().empty());
EXPECT_FALSE(component.HasScriptClass());
EXPECT_TRUE(component.GetFullClassName().empty());
EXPECT_EQ(component.GetFieldStorage().GetFieldCount(), 0u);
}
TEST(ScriptComponent_Test, FullClassNameComposesNamespaceAndClassName) {
ScriptComponent component;
component.SetScriptClass("Gameplay.Characters", "PlayerController");
EXPECT_EQ(component.GetFullClassName(), "Gameplay.Characters.PlayerController");
component.SetScriptClass("", "Mover");
EXPECT_EQ(component.GetFullClassName(), "Mover");
}
TEST(ScriptComponent_Test, SerializeRoundTripPreservesMetadataAndFields) {
ScriptComponent original;
original.SetScriptClass("GameScripts.Runtime", "Gameplay.Characters", "PlayerController");
ASSERT_TRUE(original.GetFieldStorage().SetFieldValue("DisplayName", std::string("Hero=One;Ready|100%")));
ASSERT_TRUE(original.GetFieldStorage().SetFieldValue("MoveSpeed", 6.25f));
ASSERT_TRUE(original.GetFieldStorage().SetFieldValue("Target", GameObjectReference{9988}));
std::ostringstream stream;
original.Serialize(stream);
ScriptComponent restored;
std::istringstream input(stream.str());
restored.Deserialize(input);
std::string displayName;
float moveSpeed = 0.0f;
GameObjectReference target;
EXPECT_EQ(restored.GetScriptComponentUUID(), original.GetScriptComponentUUID());
EXPECT_EQ(restored.GetAssemblyName(), "GameScripts.Runtime");
EXPECT_EQ(restored.GetNamespaceName(), "Gameplay.Characters");
EXPECT_EQ(restored.GetClassName(), "PlayerController");
EXPECT_EQ(restored.GetFullClassName(), "Gameplay.Characters.PlayerController");
EXPECT_TRUE(restored.GetFieldStorage().TryGetFieldValue("DisplayName", displayName));
EXPECT_TRUE(restored.GetFieldStorage().TryGetFieldValue("MoveSpeed", moveSpeed));
EXPECT_TRUE(restored.GetFieldStorage().TryGetFieldValue("Target", target));
EXPECT_EQ(displayName, "Hero=One;Ready|100%");
EXPECT_FLOAT_EQ(moveSpeed, 6.25f);
EXPECT_EQ(target, GameObjectReference{9988});
}
TEST(ScriptComponent_Test, ComponentFactoryRegistryCreatesScriptComponents) {
GameObject gameObject("ScriptHost");
auto& registry = ComponentFactoryRegistry::Get();
EXPECT_TRUE(registry.IsRegistered("ScriptComponent"));
ScriptComponent* created = dynamic_cast<ScriptComponent*>(registry.CreateComponent(&gameObject, "ScriptComponent"));
ASSERT_NE(created, nullptr);
EXPECT_EQ(gameObject.GetComponents<ScriptComponent>().size(), 1u);
}
TEST(ScriptComponent_Test, GameObjectCanOwnMultipleScriptComponents) {
GameObject gameObject("ScriptHost");
ScriptComponent* first = gameObject.AddComponent<ScriptComponent>();
ScriptComponent* second = gameObject.AddComponent<ScriptComponent>();
ASSERT_NE(first, nullptr);
ASSERT_NE(second, nullptr);
EXPECT_NE(first, second);
EXPECT_EQ(gameObject.GetComponents<ScriptComponent>().size(), 2u);
}
TEST(ScriptComponent_Test, SceneRoundTripPreservesMultipleScriptComponentsAndFields) {
Scene scene("Script Scene");
GameObject* host = scene.CreateGameObject("Host");
GameObject* targetObject = scene.CreateGameObject("Target");
ScriptComponent* movement = host->AddComponent<ScriptComponent>();
movement->SetScriptClass("GameScripts", "Gameplay", "MovementController");
ASSERT_TRUE(movement->GetFieldStorage().SetFieldValue("Speed", 4.25f));
ASSERT_TRUE(movement->GetFieldStorage().SetFieldValue("Target", GameObjectReference{targetObject->GetUUID()}));
ScriptComponent* inventory = host->AddComponent<ScriptComponent>();
inventory->SetScriptClass("GameScripts", "Gameplay.Inventory", "InventoryWatcher");
ASSERT_TRUE(inventory->GetFieldStorage().SetFieldValue("Label", std::string("Player|One;Ready=1")));
ASSERT_TRUE(inventory->GetFieldStorage().SetFieldValue("Slots", int32_t(24)));
const uint64_t movementUUID = movement->GetScriptComponentUUID();
const uint64_t inventoryUUID = inventory->GetScriptComponentUUID();
Scene loadedScene;
loadedScene.DeserializeFromString(scene.SerializeToString());
GameObject* loadedHost = loadedScene.Find("Host");
GameObject* loadedTarget = loadedScene.Find("Target");
ASSERT_NE(loadedHost, nullptr);
ASSERT_NE(loadedTarget, nullptr);
std::vector<ScriptComponent*> loadedScripts = loadedHost->GetComponents<ScriptComponent>();
ASSERT_EQ(loadedScripts.size(), 2u);
float speed = 0.0f;
GameObjectReference loadedReference;
std::string label;
int32_t slots = 0;
EXPECT_EQ(loadedScripts[0]->GetScriptComponentUUID(), movementUUID);
EXPECT_EQ(loadedScripts[0]->GetAssemblyName(), "GameScripts");
EXPECT_EQ(loadedScripts[0]->GetFullClassName(), "Gameplay.MovementController");
EXPECT_TRUE(loadedScripts[0]->GetFieldStorage().TryGetFieldValue("Speed", speed));
EXPECT_TRUE(loadedScripts[0]->GetFieldStorage().TryGetFieldValue("Target", loadedReference));
EXPECT_FLOAT_EQ(speed, 4.25f);
EXPECT_EQ(loadedReference, GameObjectReference{loadedTarget->GetUUID()});
EXPECT_EQ(loadedScripts[1]->GetScriptComponentUUID(), inventoryUUID);
EXPECT_EQ(loadedScripts[1]->GetAssemblyName(), "GameScripts");
EXPECT_EQ(loadedScripts[1]->GetFullClassName(), "Gameplay.Inventory.InventoryWatcher");
EXPECT_TRUE(loadedScripts[1]->GetFieldStorage().TryGetFieldValue("Label", label));
EXPECT_TRUE(loadedScripts[1]->GetFieldStorage().TryGetFieldValue("Slots", slots));
EXPECT_EQ(label, "Player|One;Ready=1");
EXPECT_EQ(slots, 24);
}
} // namespace

View File

@@ -0,0 +1,92 @@
#include <gtest/gtest.h>
#include <XCEngine/Scripting/ScriptFieldStorage.h>
using namespace XCEngine::Math;
using namespace XCEngine::Scripting;
namespace {
TEST(ScriptFieldStorage_Test, StoresRetrievesAndRemovesSupportedValues) {
ScriptFieldStorage storage;
EXPECT_TRUE(storage.SetFieldValue("Speed", 3.5f));
EXPECT_TRUE(storage.SetFieldValue("Accuracy", 0.875));
EXPECT_TRUE(storage.SetFieldValue("IsAlive", true));
EXPECT_TRUE(storage.SetFieldValue("Health", int32_t(125)));
EXPECT_TRUE(storage.SetFieldValue("Score", uint64_t(9001)));
EXPECT_TRUE(storage.SetFieldValue("DisplayName", std::string("Player One")));
EXPECT_TRUE(storage.SetFieldValue("Offset2D", Vector2(1.0f, 2.0f)));
EXPECT_TRUE(storage.SetFieldValue("Velocity", Vector3(3.0f, 4.0f, 5.0f)));
EXPECT_TRUE(storage.SetFieldValue("Tint", Vector4(0.1f, 0.2f, 0.3f, 1.0f)));
EXPECT_TRUE(storage.SetFieldValue("Target", GameObjectReference{42}));
float speed = 0.0f;
double accuracy = 0.0;
bool isAlive = false;
int32_t health = 0;
uint64_t score = 0;
std::string displayName;
Vector2 offset2D;
Vector3 velocity;
Vector4 tint;
GameObjectReference target;
EXPECT_EQ(storage.GetFieldCount(), 10u);
EXPECT_TRUE(storage.Contains("Velocity"));
EXPECT_TRUE(storage.TryGetFieldValue("Speed", speed));
EXPECT_TRUE(storage.TryGetFieldValue("Accuracy", accuracy));
EXPECT_TRUE(storage.TryGetFieldValue("IsAlive", isAlive));
EXPECT_TRUE(storage.TryGetFieldValue("Health", health));
EXPECT_TRUE(storage.TryGetFieldValue("Score", score));
EXPECT_TRUE(storage.TryGetFieldValue("DisplayName", displayName));
EXPECT_TRUE(storage.TryGetFieldValue("Offset2D", offset2D));
EXPECT_TRUE(storage.TryGetFieldValue("Velocity", velocity));
EXPECT_TRUE(storage.TryGetFieldValue("Tint", tint));
EXPECT_TRUE(storage.TryGetFieldValue("Target", target));
EXPECT_FLOAT_EQ(speed, 3.5f);
EXPECT_DOUBLE_EQ(accuracy, 0.875);
EXPECT_TRUE(isAlive);
EXPECT_EQ(health, 125);
EXPECT_EQ(score, 9001u);
EXPECT_EQ(displayName, "Player One");
EXPECT_EQ(offset2D, Vector2(1.0f, 2.0f));
EXPECT_EQ(velocity, Vector3(3.0f, 4.0f, 5.0f));
EXPECT_EQ(tint, Vector4(0.1f, 0.2f, 0.3f, 1.0f));
EXPECT_EQ(target, GameObjectReference{42});
EXPECT_TRUE(storage.Remove("IsAlive"));
EXPECT_FALSE(storage.Contains("IsAlive"));
EXPECT_EQ(storage.GetFieldCount(), 9u);
}
TEST(ScriptFieldStorage_Test, SerializeRoundTripPreservesFieldValues) {
ScriptFieldStorage original;
ASSERT_TRUE(original.SetFieldValue("Message", std::string("Ready;Set=Go|100%\nLine2")));
ASSERT_TRUE(original.SetFieldValue("Scale", Vector3(2.0f, 3.0f, 4.0f)));
ASSERT_TRUE(original.SetFieldValue("Owner", GameObjectReference{778899}));
ASSERT_TRUE(original.SetFieldValue("Counter", int32_t(-17)));
const std::string serialized = original.SerializeToString();
ScriptFieldStorage restored;
restored.DeserializeFromString(serialized);
std::string message;
Vector3 scale;
GameObjectReference owner;
int32_t counter = 0;
EXPECT_TRUE(restored.TryGetFieldValue("Message", message));
EXPECT_TRUE(restored.TryGetFieldValue("Scale", scale));
EXPECT_TRUE(restored.TryGetFieldValue("Owner", owner));
EXPECT_TRUE(restored.TryGetFieldValue("Counter", counter));
EXPECT_EQ(message, "Ready;Set=Go|100%\nLine2");
EXPECT_EQ(scale, Vector3(2.0f, 3.0f, 4.0f));
EXPECT_EQ(owner, GameObjectReference{778899});
EXPECT_EQ(counter, -17);
}
} // namespace