From 8d5da55594127f25a5d06803289bdc4c6392ad6b Mon Sep 17 00:00:00 2001 From: friflo Date: Thu, 2 Jan 2025 09:57:19 +0100 Subject: [PATCH] Systems: Made default OnAddStore() / OnRemoveStore() methods a NOP operation --- src/ECS/Systems/BaseSystem.cs | 11 +++-- src/ECS/Systems/QueryBaseSystem.cs | 6 ++- src/ECS/Systems/SystemGroup.cs | 6 ++- src/ECS/Systems/SystemRoot.cs | 4 +- src/Tests/ECS/Examples/HelloSystem.cs | 1 - src/Tests/ECS/Systems/Systems.cs | 51 +++++++++++++++++++---- src/Tests/ECS/Systems/Test_SystemGroup.cs | 23 +++++++--- 7 files changed, 79 insertions(+), 23 deletions(-) diff --git a/src/ECS/Systems/BaseSystem.cs b/src/ECS/Systems/BaseSystem.cs index 52af80f82..b8ec46bcc 100644 --- a/src/ECS/Systems/BaseSystem.cs +++ b/src/ECS/Systems/BaseSystem.cs @@ -122,8 +122,11 @@ internal static void CastSystemRemoved(BaseSystem system, SystemRoot oldRoot, Sy #endregion #region virtual - store: add / remove - protected internal virtual void OnRemoveStore(EntityStore store) { } - protected internal virtual void OnAddStore (EntityStore store) { } + internal virtual void AddStoreInternal (EntityStore store) => OnAddStore(store); + internal virtual void RemoveStoreInternal(EntityStore store) => OnRemoveStore(store); + + protected virtual void OnAddStore (EntityStore store) { } + protected virtual void OnRemoveStore(EntityStore store) { } #endregion #region virtual - system: update @@ -207,7 +210,7 @@ internal void SetParentAndRoot(SystemGroup group) system.systemRoot = newRoot; newRoot.AddSystemToRoot(system); foreach (var store in newRoot.stores) { - system.OnAddStore(store); + system.AddStoreInternal(store); } } } @@ -225,7 +228,7 @@ internal void ClearParentAndRoot() system.systemRoot = null; currentRoot.RemoveSystemFromRoot(system); foreach (var store in currentRoot.stores) { - system.OnRemoveStore(store); + system.RemoveStoreInternal(store); } } } diff --git a/src/ECS/Systems/QueryBaseSystem.cs b/src/ECS/Systems/QueryBaseSystem.cs index 33775f117..ff347e719 100644 --- a/src/ECS/Systems/QueryBaseSystem.cs +++ b/src/ECS/Systems/QueryBaseSystem.cs @@ -68,14 +68,16 @@ internal QuerySystemBase(in ComponentTypes componentTypes) { #endregion #region store: add / remove - protected internal override void OnAddStore(EntityStore entityStore) + internal override void AddStoreInternal(EntityStore entityStore) { var query = CreateQuery(entityStore); queries.Add(query); + OnAddStore(entityStore); } - protected internal override void OnRemoveStore(EntityStore entityStore) + internal override void RemoveStoreInternal(EntityStore entityStore) { + OnRemoveStore(entityStore); foreach (var query in queries) { if (query.Store != entityStore) { continue; diff --git a/src/ECS/Systems/SystemGroup.cs b/src/ECS/Systems/SystemGroup.cs index 9d2b75fdd..46a375026 100644 --- a/src/ECS/Systems/SystemGroup.cs +++ b/src/ECS/Systems/SystemGroup.cs @@ -230,15 +230,17 @@ public void SetName(string name) { #endregion #region store: add / remove - protected internal override void OnAddStore(EntityStore entityStore) + internal override void AddStoreInternal(EntityStore entityStore) { var commandBuffer = entityStore.GetCommandBuffer(); commandBuffer.ReuseBuffer = true; commandBuffers.Add(commandBuffer); + OnAddStore(entityStore); } - protected internal override void OnRemoveStore(EntityStore entityStore) + internal override void RemoveStoreInternal(EntityStore entityStore) { + OnRemoveStore(entityStore); foreach (var commandBuffer in commandBuffers) { if (commandBuffer.EntityStore != entityStore) { continue; diff --git a/src/ECS/Systems/SystemRoot.cs b/src/ECS/Systems/SystemRoot.cs index 9b8c23596..b7d025796 100644 --- a/src/ECS/Systems/SystemRoot.cs +++ b/src/ECS/Systems/SystemRoot.cs @@ -67,7 +67,7 @@ public void AddStore(EntityStore entityStore) stores.Add(entityStore); var rootSystems = GetSubSystems(ref systemBuffer); foreach (var system in rootSystems) { - system.OnAddStore(entityStore); + system.AddStoreInternal(entityStore); } } @@ -80,7 +80,7 @@ public void RemoveStore(EntityStore entityStore) stores.Remove(entityStore); var rootSystems = GetSubSystems(ref systemBuffer); foreach (var system in rootSystems) { - system.OnRemoveStore(entityStore); + system.RemoveStoreInternal(entityStore); } } #endregion diff --git a/src/Tests/ECS/Examples/HelloSystem.cs b/src/Tests/ECS/Examples/HelloSystem.cs index 5e897d8b2..3ee352cc7 100644 --- a/src/Tests/ECS/Examples/HelloSystem.cs +++ b/src/Tests/ECS/Examples/HelloSystem.cs @@ -79,7 +79,6 @@ class CustomQuerySystem : QuerySystem protected override void OnAddStore(EntityStore store) { customQuery = store.Query(); - base.OnAddStore(store); // must be called to ensure execution of OnUpdate() } /// Executes the customQuery instead of the base class Query. diff --git a/src/Tests/ECS/Systems/Systems.cs b/src/Tests/ECS/Systems/Systems.cs index 63cb26374..fe6257fe4 100644 --- a/src/Tests/ECS/Systems/Systems.cs +++ b/src/Tests/ECS/Systems/Systems.cs @@ -81,23 +81,60 @@ public class MySystem1 : BaseSystem { public string value; // used to test serialization } - // A custom System class with all possible overrides + // A custom BaseSystem class with all possible overrides public class MySystem2 : BaseSystem { - public bool storeAdded; - public bool storeRemoved; + public int storeAdded; + public int storeRemoved; public override string Name => "MySystem2 - custom name"; protected override void OnAddStore (EntityStore store) { - base.OnAddStore(store); - storeAdded = true; + storeAdded++; } protected override void OnRemoveStore(EntityStore store) { - base.OnRemoveStore(store); - storeRemoved = true; + storeRemoved++; } + protected override void OnUpdateGroupBegin() { } + protected override void OnUpdateGroupEnd() { } + protected override void OnUpdateGroup() { } + } + + // QuerySystem class with all possible overrides + public class MyQuerySystem : QuerySystem { + public int storeAdded; + public int storeRemoved; + + protected override void OnUpdate() { } + + protected override void OnAddStore (EntityStore store) { + storeAdded++; + } + + protected override void OnRemoveStore(EntityStore store) { + storeRemoved++; + } + + protected override void OnUpdateGroupBegin() { } + protected override void OnUpdateGroupEnd() { } + protected override void OnUpdateGroup() { } + } + + // SystemGroup class with all possible overrides + public class MyGroup : SystemGroup { + public int storeAdded; + public int storeRemoved; + + public MyGroup() : base("MyGroup") { } + + protected override void OnAddStore (EntityStore store) { + storeAdded++; + } + + protected override void OnRemoveStore(EntityStore store) { + storeRemoved++; + } protected override void OnUpdateGroupBegin() { } protected override void OnUpdateGroupEnd() { } diff --git a/src/Tests/ECS/Systems/Test_SystemGroup.cs b/src/Tests/ECS/Systems/Test_SystemGroup.cs index 64aad2fa6..a5aae14f7 100644 --- a/src/Tests/ECS/Systems/Test_SystemGroup.cs +++ b/src/Tests/ECS/Systems/Test_SystemGroup.cs @@ -349,16 +349,29 @@ public static void Test_SystemGroup_FindSystem() [Test] public static void Test_SystemGroup_OnAddRemoveStore() { - var root = new SystemRoot("Systems"); var store = new EntityStore(); var mySystem = new MySystem2(); - root.Add(mySystem); + var myQuery = new MyQuerySystem(); + var myGroup = new MyGroup(); + var root = new SystemRoot(store) { + mySystem, + myQuery, + myGroup + }; + AreEqual(1, mySystem.storeAdded); + AreEqual(1, myQuery.storeAdded); + AreEqual(1, myGroup.storeAdded); - root.AddStore(store); - IsTrue(mySystem.storeAdded); + var store2 = new EntityStore(); + root.AddStore(store2); + AreEqual(2, mySystem.storeAdded); + AreEqual(2, myQuery.storeAdded); + AreEqual(2, myGroup.storeAdded); root.RemoveStore(store); - IsTrue(mySystem.storeRemoved); + AreEqual(1, mySystem.storeRemoved); + AreEqual(1, myQuery.storeRemoved); + AreEqual(1, myGroup.storeRemoved); } [Test]