From b442c72efea6f84052fd584723ababa00f3f159a Mon Sep 17 00:00:00 2001 From: David Higueros Date: Tue, 26 Nov 2024 12:39:22 -0700 Subject: [PATCH 01/43] Added AddEntityToTracker and AddComponentToTracker methods --- Celeste.Mod.mm/Patches/Monocle/Tracker.cs | 47 +++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs index c3165c558..018a33cfd 100644 --- a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs +++ b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs @@ -123,5 +123,52 @@ private static List GetSubclasses(Type type) { // don't hold references to all the types anymore _temporaryAllTypes = null; } + + public static void AddEntityToTracker(Type type, params Type[] subTypes) { + StoredEntityTypes.Add(type); + if (!TrackedEntityTypes.TryGetValue(type, out List value)) { + TrackedEntityTypes[type] = new List { type }; + } else if (!value.Contains(type)) { + value.Add(type); + } + foreach (Type subType in subTypes) { + if (!TrackedEntityTypes.TryGetValue(subType, out List subvalue)) { + TrackedEntityTypes[subType] = new List { type }; + } else if (!subvalue.Contains(type)) { + subvalue.Add(type); + } + } + Dictionary> entities = Engine.Scene?.Tracker.Entities; + if (entities != null && !entities.ContainsKey(type)) { + entities[type] = Engine.Scene.Entities.Where((Entity e) => e.GetType() == type).ToList(); + } + } + + public static void AddComponentToTracker(Type type, params Type[] subTypes) { + StoredComponentTypes.Add(type); + if (!TrackedComponentTypes.TryGetValue(type, out List value)) { + TrackedComponentTypes[type] = new List { type }; + } else if (!value.Contains(type)) { + value.Add(type); + } + foreach (Type subType in subTypes) { + if (!TrackedComponentTypes.TryGetValue(subType, out List subvalue)) { + TrackedComponentTypes[subType] = new List { type }; + } else if (!subvalue.Contains(type)) { + subvalue.Add(type); + } + } + Dictionary> components = Engine.Scene?.Tracker.Components; + if (components != null && !components.ContainsKey(type)) { + List list = new List(); + foreach (Entity entity in Engine.Scene.Entities) { + Component component = entity.Components.FirstOrDefault((c) => c.GetType() == type); + if (component != null) { + list.Add(component); + } + } + components[type] = list; + } + } } } From fec47216c109dac8ad0d2f6b881960c74bcbaa7a Mon Sep 17 00:00:00 2001 From: David Higueros Date: Tue, 26 Nov 2024 12:48:56 -0700 Subject: [PATCH 02/43] Simplified Lambda, accounted for multiple Components in the same entity --- Celeste.Mod.mm/Patches/Monocle/Tracker.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs index 018a33cfd..cf62ea79d 100644 --- a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs +++ b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs @@ -140,7 +140,7 @@ public static void AddEntityToTracker(Type type, params Type[] subTypes) { } Dictionary> entities = Engine.Scene?.Tracker.Entities; if (entities != null && !entities.ContainsKey(type)) { - entities[type] = Engine.Scene.Entities.Where((Entity e) => e.GetType() == type).ToList(); + entities[type] = Engine.Scene.Entities.Where((e) => e.GetType() == type).ToList(); } } @@ -162,8 +162,7 @@ public static void AddComponentToTracker(Type type, params Type[] subTypes) { if (components != null && !components.ContainsKey(type)) { List list = new List(); foreach (Entity entity in Engine.Scene.Entities) { - Component component = entity.Components.FirstOrDefault((c) => c.GetType() == type); - if (component != null) { + foreach (Component component in entity.Components.Where((c) => c.GetType() == type)) { list.Add(component); } } From 036e3d37c96d7dbf4ec648536e3d0fd37f298c4a Mon Sep 17 00:00:00 2001 From: David Higueros Date: Tue, 26 Nov 2024 12:49:50 -0700 Subject: [PATCH 03/43] Simplified new statement --- Celeste.Mod.mm/Patches/Monocle/Tracker.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs index cf62ea79d..8d7a6e35f 100644 --- a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs +++ b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs @@ -160,7 +160,7 @@ public static void AddComponentToTracker(Type type, params Type[] subTypes) { } Dictionary> components = Engine.Scene?.Tracker.Components; if (components != null && !components.ContainsKey(type)) { - List list = new List(); + List list = new(); foreach (Entity entity in Engine.Scene.Entities) { foreach (Component component in entity.Components.Where((c) => c.GetType() == type)) { list.Add(component); From a6d3b62f07c66412485c310f10f74d79df84bb1e Mon Sep 17 00:00:00 2001 From: David Higueros Date: Tue, 26 Nov 2024 12:52:36 -0700 Subject: [PATCH 04/43] Added EntityCollider and EntityColliderByComponent --- Celeste.Mod.mm/Mod/Entities/EntityCollider.cs | 51 +++++++++++++++++++ .../Mod/Entities/EntityColliderByComponent.cs | 51 +++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 Celeste.Mod.mm/Mod/Entities/EntityCollider.cs create mode 100644 Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs diff --git a/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs b/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs new file mode 100644 index 000000000..0f6b60dc9 --- /dev/null +++ b/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs @@ -0,0 +1,51 @@ +using Monocle; +using System; +using Microsoft.Xna.Framework; + +namespace Celeste.Mod.Entities { + [Tracked(false)] + public class EntityCollider : Component where T : Entity { + public readonly string entityType = typeof(T).Name; + + public Action OnEntityAction; + + public Collider Collider; + + public EntityCollider(Action onEntityAction, Collider collider) + : base(active: true, visible: true) { + OnEntityAction = onEntityAction; + Collider = collider; + } + + public override void Added(Entity entity) { + if (!Engine.Scene.Tracker.IsEntityTracked()) { + patch_Tracker.AddEntityToTracker(typeof(T)); + } + base.Added(entity); + } + + public override void Update() { + if (OnEntityAction == null) { + return; + } + + Collider collider = Entity.Collider; + if (Collider != null) { + Entity.Collider = Collider; + } + + Entity.CollideDo(OnEntityAction); + + Entity.Collider = collider; + } + + public override void DebugRender(Camera camera) { + if (Collider != null) { + Collider collider = Entity.Collider; + Entity.Collider = Collider; + Collider.Render(camera, Color.HotPink); + Entity.Collider = collider; + } + } + } +} diff --git a/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs b/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs new file mode 100644 index 000000000..9948bff47 --- /dev/null +++ b/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs @@ -0,0 +1,51 @@ +using Monocle; +using System; +using Microsoft.Xna.Framework; + +namespace Celeste.Mod.Entities { + [Tracked(false)] + public class EntityColliderByComponent : Component where T : Component { + public readonly string componentType = typeof(T).Name; + + public Action OnComponentAction; + + public Collider Collider; + + public EntityColliderByComponent(Action onComponentAction, Collider collider = null) + : base(active: true, visible: true) { + OnComponentAction = onComponentAction; + Collider = collider; + } + + public override void Added(Entity entity) { + if (!Engine.Scene.Tracker.IsComponentTracked()) { + patch_Tracker.AddComponentToTracker(typeof(T)); + } + base.Added(entity); + } + + public override void Update() { + if (OnComponentAction == null) { + return; + } + + Collider collider = Entity.Collider; + if (Collider != null) { + Entity.Collider = Collider; + } + + Entity.CollideDoByComponent(OnComponentAction); + + Entity.Collider = collider; + } + + public override void DebugRender(Camera camera) { + if (Collider != null) { + Collider collider = Entity.Collider; + Entity.Collider = Collider; + Collider.Render(camera, Color.HotPink); + Entity.Collider = collider; + } + } + } +} From 48db15a75cd00280ee079d10227b991095600fde Mon Sep 17 00:00:00 2001 From: David Higueros Date: Sat, 30 Nov 2024 00:23:46 -0700 Subject: [PATCH 05/43] Simplified AddComponentToTracker by using AddRange instead of foreach and Add --- Celeste.Mod.mm/Patches/Monocle/Tracker.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs index 8d7a6e35f..c1a48049e 100644 --- a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs +++ b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs @@ -162,9 +162,7 @@ public static void AddComponentToTracker(Type type, params Type[] subTypes) { if (components != null && !components.ContainsKey(type)) { List list = new(); foreach (Entity entity in Engine.Scene.Entities) { - foreach (Component component in entity.Components.Where((c) => c.GetType() == type)) { - list.Add(component); - } + list.AddRange(entity.Components.Where((c) => c.GetType() == type)); } components[type] = list; } From 5c0e16a561b472e71622215d4a2901c1cfad299d Mon Sep 17 00:00:00 2001 From: David Higueros Date: Sat, 30 Nov 2024 14:33:48 -0700 Subject: [PATCH 06/43] Made method instance instead of static so it can access its own dictionaries --- Celeste.Mod.mm/Patches/Monocle/Tracker.cs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs index c1a48049e..cb3c0c6df 100644 --- a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs +++ b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs @@ -124,7 +124,7 @@ private static List GetSubclasses(Type type) { _temporaryAllTypes = null; } - public static void AddEntityToTracker(Type type, params Type[] subTypes) { + public void AddEntityToTracker(Type type, params Type[] subTypes) { StoredEntityTypes.Add(type); if (!TrackedEntityTypes.TryGetValue(type, out List value)) { TrackedEntityTypes[type] = new List { type }; @@ -138,13 +138,12 @@ public static void AddEntityToTracker(Type type, params Type[] subTypes) { subvalue.Add(type); } } - Dictionary> entities = Engine.Scene?.Tracker.Entities; - if (entities != null && !entities.ContainsKey(type)) { - entities[type] = Engine.Scene.Entities.Where((e) => e.GetType() == type).ToList(); + if (!Entities.ContainsKey(type)) { + Entities[type] = Engine.Scene.Entities.Where((e) => e.GetType() == type).ToList(); } } - public static void AddComponentToTracker(Type type, params Type[] subTypes) { + public void AddComponentToTracker(Type type, params Type[] subTypes) { StoredComponentTypes.Add(type); if (!TrackedComponentTypes.TryGetValue(type, out List value)) { TrackedComponentTypes[type] = new List { type }; @@ -158,13 +157,12 @@ public static void AddComponentToTracker(Type type, params Type[] subTypes) { subvalue.Add(type); } } - Dictionary> components = Engine.Scene?.Tracker.Components; - if (components != null && !components.ContainsKey(type)) { + if (!Components.ContainsKey(type)) { List list = new(); foreach (Entity entity in Engine.Scene.Entities) { list.AddRange(entity.Components.Where((c) => c.GetType() == type)); } - components[type] = list; + Components[type] = list; } } } From 68d4be098c489f5cfd941d86b0203a53c96f891a Mon Sep 17 00:00:00 2001 From: David Higueros Date: Sat, 30 Nov 2024 14:36:16 -0700 Subject: [PATCH 07/43] Changed Added for EntityAdded to access scene for Tracker --- Celeste.Mod.mm/Mod/Entities/EntityCollider.cs | 8 ++++---- Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs b/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs index 0f6b60dc9..91763b60d 100644 --- a/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs +++ b/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs @@ -17,11 +17,11 @@ public EntityCollider(Action onEntityAction, Collider collider) Collider = collider; } - public override void Added(Entity entity) { - if (!Engine.Scene.Tracker.IsEntityTracked()) { - patch_Tracker.AddEntityToTracker(typeof(T)); + public override void EntityAdded(Scene scene) { + if (scene.Tracker.IsEntityTracked()) { + (scene.Tracker as patch_Tracker).AddEntityToTracker(typeof(T)); } - base.Added(entity); + base.EntityAdded(scene); } public override void Update() { diff --git a/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs b/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs index 9948bff47..53c9f0410 100644 --- a/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs +++ b/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs @@ -17,11 +17,11 @@ public EntityColliderByComponent(Action onComponentAction, Collider collider Collider = collider; } - public override void Added(Entity entity) { - if (!Engine.Scene.Tracker.IsComponentTracked()) { - patch_Tracker.AddComponentToTracker(typeof(T)); + public override void EntityAdded(Scene scene) { + if (scene.Tracker.IsComponentTracked()) { + (scene.Tracker as patch_Tracker).AddComponentToTracker(typeof(T)); } - base.Added(entity); + base.EntityAdded(scene); } public override void Update() { From 04ead2dbea1b800533cca2d488da551897b8fa51 Mon Sep 17 00:00:00 2001 From: David Higueros Date: Sat, 30 Nov 2024 14:36:35 -0700 Subject: [PATCH 08/43] Added default null collider --- Celeste.Mod.mm/Mod/Entities/EntityCollider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs b/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs index 91763b60d..b43884f66 100644 --- a/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs +++ b/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs @@ -11,7 +11,7 @@ public class EntityCollider : Component where T : Entity { public Collider Collider; - public EntityCollider(Action onEntityAction, Collider collider) + public EntityCollider(Action onEntityAction, Collider collider = null) : base(active: true, visible: true) { OnEntityAction = onEntityAction; Collider = collider; From 795db3c3ca8e25f79e095d882bf525d0d50b2a9e Mon Sep 17 00:00:00 2001 From: David Higueros Date: Sat, 30 Nov 2024 14:52:28 -0700 Subject: [PATCH 09/43] Added Summary documentation to Entity Collider Components --- Celeste.Mod.mm/Mod/Entities/EntityCollider.cs | 11 +++++++++++ .../Mod/Entities/EntityColliderByComponent.cs | 12 ++++++++++++ 2 files changed, 23 insertions(+) diff --git a/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs b/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs index b43884f66..442b24253 100644 --- a/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs +++ b/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs @@ -3,10 +3,21 @@ using Microsoft.Xna.Framework; namespace Celeste.Mod.Entities { + /// + /// Allows for Collision with any type of entity in the game, similar to a PlayerCollider or PufferCollider. + /// Performs the Action provided on collision. + /// + /// The specific type of Entity this component should try to collide with [Tracked(false)] public class EntityCollider : Component where T : Entity { + /// + /// Provides a simple way to know the Entity type of the specific Collider without Reflection + /// public readonly string entityType = typeof(T).Name; + /// + /// The Action invoked on Collision, with the Component collided with passed as a parameter + /// public Action OnEntityAction; public Collider Collider; diff --git a/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs b/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs index 53c9f0410..e77bcb9c3 100644 --- a/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs +++ b/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs @@ -3,10 +3,22 @@ using Microsoft.Xna.Framework; namespace Celeste.Mod.Entities { + /// + /// Allows for Collision with any type of entity in the game, similar to a PlayerCollider or PufferCollider. + /// Collision is done by component, as in, it will get all the components of the type and try to collide with their entities. + /// Performs the Action provided on collision. + /// + /// The specific type of Component this component should try to collide with [Tracked(false)] public class EntityColliderByComponent : Component where T : Component { + /// + /// Provides a simple way to know the Component type of the specific Collider without Reflection + /// public readonly string componentType = typeof(T).Name; + /// + /// The Action invoked on Collision, with the Component collided with passed as a parameter + /// public Action OnComponentAction; public Collider Collider; From c20217c32918c09ee1a868b66ee985d8c4c68f7e Mon Sep 17 00:00:00 2001 From: David Higueros Date: Sat, 30 Nov 2024 14:57:29 -0700 Subject: [PATCH 10/43] Fixed boolean logic of EntityAdded --- Celeste.Mod.mm/Mod/Entities/EntityCollider.cs | 2 +- Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs b/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs index 442b24253..0742e5dcd 100644 --- a/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs +++ b/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs @@ -29,7 +29,7 @@ public EntityCollider(Action onEntityAction, Collider collider = null) } public override void EntityAdded(Scene scene) { - if (scene.Tracker.IsEntityTracked()) { + if (!scene.Tracker.IsEntityTracked()) { (scene.Tracker as patch_Tracker).AddEntityToTracker(typeof(T)); } base.EntityAdded(scene); diff --git a/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs b/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs index e77bcb9c3..5e3d36aa0 100644 --- a/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs +++ b/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs @@ -30,7 +30,7 @@ public EntityColliderByComponent(Action onComponentAction, Collider collider } public override void EntityAdded(Scene scene) { - if (scene.Tracker.IsComponentTracked()) { + if (!scene.Tracker.IsComponentTracked()) { (scene.Tracker as patch_Tracker).AddComponentToTracker(typeof(T)); } base.EntityAdded(scene); From aa7cd7b17950de46525b97aec27e2fc4e5ef9041 Mon Sep 17 00:00:00 2001 From: David Higueros Date: Sat, 30 Nov 2024 16:12:11 -0700 Subject: [PATCH 11/43] Changed to general AddTypeToTracker --- Celeste.Mod.mm/Mod/Entities/EntityCollider.cs | 2 +- Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs b/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs index 0742e5dcd..f1b6069e1 100644 --- a/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs +++ b/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs @@ -30,7 +30,7 @@ public EntityCollider(Action onEntityAction, Collider collider = null) public override void EntityAdded(Scene scene) { if (!scene.Tracker.IsEntityTracked()) { - (scene.Tracker as patch_Tracker).AddEntityToTracker(typeof(T)); + patch_Tracker.AddTypeToTracker(typeof(T)); } base.EntityAdded(scene); } diff --git a/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs b/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs index 5e3d36aa0..11f0334cb 100644 --- a/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs +++ b/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs @@ -31,7 +31,7 @@ public EntityColliderByComponent(Action onComponentAction, Collider collider public override void EntityAdded(Scene scene) { if (!scene.Tracker.IsComponentTracked()) { - (scene.Tracker as patch_Tracker).AddComponentToTracker(typeof(T)); + patch_Tracker.AddTypeToTracker(typeof(T)); } base.EntityAdded(scene); } From 3cc657a6502aff34668adba56365ebc86acf9071 Mon Sep 17 00:00:00 2001 From: David Higueros Date: Sat, 30 Nov 2024 16:41:31 -0700 Subject: [PATCH 12/43] Added static RefreshTracker which will make sure the tracker is up to date, especially if a Scene change happens --- Celeste.Mod.mm/Patches/Monocle/Tracker.cs | 36 +++++++++++++++++------ 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs index cb3c0c6df..43b15ddf1 100644 --- a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs +++ b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs @@ -150,19 +150,37 @@ public void AddComponentToTracker(Type type, params Type[] subTypes) { } else if (!value.Contains(type)) { value.Add(type); } - foreach (Type subType in subTypes) { - if (!TrackedComponentTypes.TryGetValue(subType, out List subvalue)) { - TrackedComponentTypes[subType] = new List { type }; - } else if (!subvalue.Contains(type)) { - subvalue.Add(type); + + public static void RefreshTracker() { + foreach (Type entityType in StoredEntityTypes) { + if (!Engine.Scene.Tracker.Entities.ContainsKey(entityType)) { + Engine.Scene.Tracker.Entities.Add(entityType, new List()); } } - if (!Components.ContainsKey(type)) { - List list = new(); + List components = new List(); foreach (Entity entity in Engine.Scene.Entities) { - list.AddRange(entity.Components.Where((c) => c.GetType() == type)); + components.AddRange(entity.Components); + Type entityType = entity.GetType(); + if (!TrackedEntityTypes.TryGetValue(entityType, out List value)) { + continue; + } + foreach (Type item in value) { + Engine.Scene.Tracker.Entities[item].Add(entity); + } + } + foreach (Type componentType in StoredComponentTypes) { + if (!Engine.Scene.Tracker.Components.ContainsKey(componentType)) { + Engine.Scene.Tracker.Components.Add(componentType, new List()); + } + } + foreach (Component component in components) { + Type componentType = component.GetType(); + if (!TrackedComponentTypes.TryGetValue(componentType, out List value)) { + continue; + } + foreach (Type item in value) { + Engine.Scene.Tracker.Components[item].Add(component); } - Components[type] = list; } } } From 780d16a50396e79eb329182f3098339bb8fc0150 Mon Sep 17 00:00:00 2001 From: David Higueros Date: Sat, 30 Nov 2024 16:42:20 -0700 Subject: [PATCH 13/43] Changed Entity and Component methods to AddTypeToTracker in the same way the Tracker's Initializer works, plus method to include all or no subtypes --- Celeste.Mod.mm/Patches/Monocle/Tracker.cs | 68 +++++++++++++++-------- 1 file changed, 46 insertions(+), 22 deletions(-) diff --git a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs index 43b15ddf1..f6de14e4c 100644 --- a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs +++ b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs @@ -124,32 +124,56 @@ private static List GetSubclasses(Type type) { _temporaryAllTypes = null; } - public void AddEntityToTracker(Type type, params Type[] subTypes) { - StoredEntityTypes.Add(type); - if (!TrackedEntityTypes.TryGetValue(type, out List value)) { - TrackedEntityTypes[type] = new List { type }; - } else if (!value.Contains(type)) { - value.Add(type); + public static void AddTypeToTracker(Type type, bool inheritAll) { + if (inheritAll) { + AddTypeToTracker(type, GetSubclasses(type).ToArray()); } - foreach (Type subType in subTypes) { - if (!TrackedEntityTypes.TryGetValue(subType, out List subvalue)) { - TrackedEntityTypes[subType] = new List { type }; - } else if (!subvalue.Contains(type)) { - subvalue.Add(type); - } - } - if (!Entities.ContainsKey(type)) { - Entities[type] = Engine.Scene.Entities.Where((e) => e.GetType() == type).ToList(); + else { + AddTypeToTracker(type); } } - public void AddComponentToTracker(Type type, params Type[] subTypes) { - StoredComponentTypes.Add(type); - if (!TrackedComponentTypes.TryGetValue(type, out List value)) { - TrackedComponentTypes[type] = new List { type }; - } else if (!value.Contains(type)) { - value.Add(type); + public static void AddTypeToTracker(Type type, params Type[] subtypes) { + if (typeof(Entity).IsAssignableFrom(type)) { + StoredEntityTypes.Add(type); + if (!type.IsAbstract) { + if (!TrackedEntityTypes.ContainsKey(type)) { + TrackedEntityTypes.Add(type, new List()); + } + TrackedEntityTypes[type].AddRange(TrackedEntityTypes.TryGetValue(type, out List list) ? list : new List()); + TrackedEntityTypes[type] = TrackedEntityTypes[type].Distinct().ToList(); + } + foreach (Type subtype in subtypes) { + if (!subtype.IsAbstract) { + if (!TrackedEntityTypes.ContainsKey(subtype)) + TrackedEntityTypes.Add(subtype, new List()); + TrackedEntityTypes[subtype].AddRange(TrackedEntityTypes.TryGetValue(type, out List list) ? list : new List()); + TrackedEntityTypes[subtype] = TrackedEntityTypes[subtype].Distinct().ToList(); + } + } } + else if (typeof(Component).IsAssignableFrom(type)) { + StoredComponentTypes.Add(type); + if (!type.IsAbstract) { + if (!TrackedComponentTypes.ContainsKey(type)) { + TrackedComponentTypes.Add(type, new List()); + } + TrackedComponentTypes[type].AddRange(TrackedComponentTypes.TryGetValue(type, out List list) ? list : new List()); + TrackedComponentTypes[type] = TrackedComponentTypes[type].Distinct().ToList(); + } + foreach(Type subtype in subtypes) { + if (!subtype.IsAbstract) { + if (!TrackedComponentTypes.ContainsKey(subtype)) + TrackedComponentTypes.Add(subtype, new List()); + TrackedComponentTypes[subtype].AddRange(TrackedComponentTypes.TryGetValue(type, out List list) ? list : new List()); + TrackedComponentTypes[subtype] = TrackedComponentTypes[subtype].Distinct().ToList(); + } + } + } + else { + throw new Exception("Type '" + type.Name + "' cannot be TrackedAs because it does not derive from Entity or Component"); + } + } public static void RefreshTracker() { foreach (Type entityType in StoredEntityTypes) { @@ -158,7 +182,7 @@ public static void RefreshTracker() { } } List components = new List(); - foreach (Entity entity in Engine.Scene.Entities) { + foreach (Entity entity in Engine.Scene.Entities) { components.AddRange(entity.Components); Type entityType = entity.GetType(); if (!TrackedEntityTypes.TryGetValue(entityType, out List value)) { From 6bc5a2e78d91f06e982cbf44de936e93f4da409e Mon Sep 17 00:00:00 2001 From: David Higueros Date: Sat, 30 Nov 2024 17:09:35 -0700 Subject: [PATCH 14/43] Added RefreshTracker and RefreshTrackerLists to update the tracker manually --- Celeste.Mod.mm/Patches/Monocle/Tracker.cs | 44 +++++++++++++++-------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs index f6de14e4c..e65b914c6 100644 --- a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs +++ b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs @@ -175,35 +175,49 @@ public static void AddTypeToTracker(Type type, params Type[] subtypes) { } } + public static void RefreshTracker(Type type) { + if (typeof(Entity).IsAssignableFrom(type) && !Engine.Scene.Tracker.Entities.ContainsKey(type)) { + Engine.Scene.Tracker.Entities.Add(type, new List()); + } + else if (typeof(Component).IsAssignableFrom(type) && !Engine.Scene.Tracker.Components.ContainsKey(type)) { + Engine.Scene.Tracker.Components.Add(type, new List()); + } else { + throw new Exception("Type '" + type.Name + "' cannot be TrackedAs because it does not derive from Entity or Component"); + } + RefreshTrackerLists(); + } + public static void RefreshTracker() { foreach (Type entityType in StoredEntityTypes) { if (!Engine.Scene.Tracker.Entities.ContainsKey(entityType)) { Engine.Scene.Tracker.Entities.Add(entityType, new List()); } } - List components = new List(); - foreach (Entity entity in Engine.Scene.Entities) { - components.AddRange(entity.Components); - Type entityType = entity.GetType(); - if (!TrackedEntityTypes.TryGetValue(entityType, out List value)) { - continue; - } - foreach (Type item in value) { - Engine.Scene.Tracker.Entities[item].Add(entity); - } - } foreach (Type componentType in StoredComponentTypes) { if (!Engine.Scene.Tracker.Components.ContainsKey(componentType)) { Engine.Scene.Tracker.Components.Add(componentType, new List()); } } - foreach (Component component in components) { + RefreshTrackerLists(); + } + + private static void RefreshTrackerLists() { + foreach (Entity entity in Engine.Scene.Entities) { + foreach (Component component in entity.Components) { Type componentType = component.GetType(); - if (!TrackedComponentTypes.TryGetValue(componentType, out List value)) { + if (!TrackedComponentTypes.TryGetValue(componentType, out List componentTypes)) { + continue; + } + foreach (Type trackedType in componentTypes) { + Engine.Scene.Tracker.Components[trackedType].Add(component); + } + } + Type entityType = entity.GetType(); + if (!TrackedEntityTypes.TryGetValue(entityType, out List entityTypes)) { continue; } - foreach (Type item in value) { - Engine.Scene.Tracker.Components[item].Add(component); + foreach (Type trackedType in entityTypes) { + Engine.Scene.Tracker.Entities[trackedType].Add(entity); } } } From 6a2f3cf9d022a924cda62f5b5ab0fad2a8757298 Mon Sep 17 00:00:00 2001 From: David Higueros Date: Sat, 30 Nov 2024 17:10:13 -0700 Subject: [PATCH 15/43] TrackedTypes Dictionaries access changed, added RefreshTracker call to end --- Celeste.Mod.mm/Patches/Monocle/Tracker.cs | 43 +++++++++++++---------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs index e65b914c6..e5371f67c 100644 --- a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs +++ b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs @@ -137,42 +137,49 @@ public static void AddTypeToTracker(Type type, params Type[] subtypes) { if (typeof(Entity).IsAssignableFrom(type)) { StoredEntityTypes.Add(type); if (!type.IsAbstract) { - if (!TrackedEntityTypes.ContainsKey(type)) { - TrackedEntityTypes.Add(type, new List()); + if (!TrackedEntityTypes.TryGetValue(type, out List value)) { + value = new List(); + TrackedEntityTypes.Add(type, value); } - TrackedEntityTypes[type].AddRange(TrackedEntityTypes.TryGetValue(type, out List list) ? list : new List()); - TrackedEntityTypes[type] = TrackedEntityTypes[type].Distinct().ToList(); + value.AddRange(TrackedEntityTypes.TryGetValue(type, out List list) ? list : new List()); + TrackedEntityTypes[type] = value.Distinct().ToList(); } foreach (Type subtype in subtypes) { if (!subtype.IsAbstract) { - if (!TrackedEntityTypes.ContainsKey(subtype)) - TrackedEntityTypes.Add(subtype, new List()); - TrackedEntityTypes[subtype].AddRange(TrackedEntityTypes.TryGetValue(type, out List list) ? list : new List()); - TrackedEntityTypes[subtype] = TrackedEntityTypes[subtype].Distinct().ToList(); + if (!TrackedEntityTypes.TryGetValue(subtype, out List value)) { + value = new List(); + TrackedEntityTypes.Add(subtype, value); + } + value.AddRange(TrackedEntityTypes.TryGetValue(type, out List list) ? list : new List()); + TrackedEntityTypes[subtype] = value.Distinct().ToList(); } } } else if (typeof(Component).IsAssignableFrom(type)) { StoredComponentTypes.Add(type); if (!type.IsAbstract) { - if (!TrackedComponentTypes.ContainsKey(type)) { - TrackedComponentTypes.Add(type, new List()); + if (!TrackedComponentTypes.TryGetValue(type, out List value)) { + value = new List(); + TrackedComponentTypes.Add(type, value); } - TrackedComponentTypes[type].AddRange(TrackedComponentTypes.TryGetValue(type, out List list) ? list : new List()); - TrackedComponentTypes[type] = TrackedComponentTypes[type].Distinct().ToList(); + value.AddRange(TrackedComponentTypes.TryGetValue(type, out List list) ? list : new List()); + TrackedComponentTypes[type] = value.Distinct().ToList(); } foreach(Type subtype in subtypes) { if (!subtype.IsAbstract) { - if (!TrackedComponentTypes.ContainsKey(subtype)) - TrackedComponentTypes.Add(subtype, new List()); - TrackedComponentTypes[subtype].AddRange(TrackedComponentTypes.TryGetValue(type, out List list) ? list : new List()); - TrackedComponentTypes[subtype] = TrackedComponentTypes[subtype].Distinct().ToList(); + if (!TrackedComponentTypes.TryGetValue(subtype, out List value)) { + value = new List(); + TrackedComponentTypes.Add(subtype, value); + } + value.AddRange(TrackedComponentTypes.TryGetValue(type, out List list) ? list : new List()); + TrackedComponentTypes[subtype] = value.Distinct().ToList(); } } } else { throw new Exception("Type '" + type.Name + "' cannot be TrackedAs because it does not derive from Entity or Component"); } + RefreshTracker(type); } public static void RefreshTracker(Type type) { @@ -199,12 +206,12 @@ public static void RefreshTracker() { } } RefreshTrackerLists(); - } + } private static void RefreshTrackerLists() { foreach (Entity entity in Engine.Scene.Entities) { foreach (Component component in entity.Components) { - Type componentType = component.GetType(); + Type componentType = component.GetType(); if (!TrackedComponentTypes.TryGetValue(componentType, out List componentTypes)) { continue; } From d6c68cfa484e2c35aeb2e3467c9a97afb27f7254 Mon Sep 17 00:00:00 2001 From: David Higueros Date: Sat, 30 Nov 2024 17:22:35 -0700 Subject: [PATCH 16/43] Added TrackedAs parameter to AddTypeToTracker --- Celeste.Mod.mm/Patches/Monocle/Tracker.cs | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs index e5371f67c..1cfd233ac 100644 --- a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs +++ b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs @@ -124,16 +124,11 @@ private static List GetSubclasses(Type type) { _temporaryAllTypes = null; } - public static void AddTypeToTracker(Type type, bool inheritAll) { - if (inheritAll) { - AddTypeToTracker(type, GetSubclasses(type).ToArray()); - } - else { - AddTypeToTracker(type); - } + public static void AddTypeToTracker(Type type, Type trackedAs = null, bool inheritAll = false) { + AddTypeToTracker(type, trackedAs, inheritAll ? GetSubclasses(type).ToArray() : Array.Empty()); } - public static void AddTypeToTracker(Type type, params Type[] subtypes) { + public static void AddTypeToTracker(Type type, Type trackedAs = null, params Type[] subtypes) { if (typeof(Entity).IsAssignableFrom(type)) { StoredEntityTypes.Add(type); if (!type.IsAbstract) { @@ -141,7 +136,8 @@ public static void AddTypeToTracker(Type type, params Type[] subtypes) { value = new List(); TrackedEntityTypes.Add(type, value); } - value.AddRange(TrackedEntityTypes.TryGetValue(type, out List list) ? list : new List()); + value.AddRange(TrackedEntityTypes.TryGetValue(trackedAs != null && trackedAs.IsAssignableFrom(type) + ? trackedAs : type, out List list) ? list : new List()); TrackedEntityTypes[type] = value.Distinct().ToList(); } foreach (Type subtype in subtypes) { @@ -150,7 +146,8 @@ public static void AddTypeToTracker(Type type, params Type[] subtypes) { value = new List(); TrackedEntityTypes.Add(subtype, value); } - value.AddRange(TrackedEntityTypes.TryGetValue(type, out List list) ? list : new List()); + value.AddRange(TrackedEntityTypes.TryGetValue(trackedAs != null && trackedAs.IsAssignableFrom(type) + ? trackedAs : type, out List list) ? list : new List()); TrackedEntityTypes[subtype] = value.Distinct().ToList(); } } @@ -162,7 +159,8 @@ public static void AddTypeToTracker(Type type, params Type[] subtypes) { value = new List(); TrackedComponentTypes.Add(type, value); } - value.AddRange(TrackedComponentTypes.TryGetValue(type, out List list) ? list : new List()); + value.AddRange(TrackedComponentTypes.TryGetValue(trackedAs != null && trackedAs.IsAssignableFrom(type) + ? trackedAs : type, out List list) ? list : new List()); TrackedComponentTypes[type] = value.Distinct().ToList(); } foreach(Type subtype in subtypes) { @@ -171,7 +169,8 @@ public static void AddTypeToTracker(Type type, params Type[] subtypes) { value = new List(); TrackedComponentTypes.Add(subtype, value); } - value.AddRange(TrackedComponentTypes.TryGetValue(type, out List list) ? list : new List()); + value.AddRange(TrackedComponentTypes.TryGetValue(trackedAs != null && trackedAs.IsAssignableFrom(type) + ? trackedAs : type, out List list) ? list : new List()); TrackedComponentTypes[subtype] = value.Distinct().ToList(); } } From 4dd4556069097fe967b128f8e0edb7b1bca84de3 Mon Sep 17 00:00:00 2001 From: David Higueros Date: Sat, 30 Nov 2024 17:24:02 -0700 Subject: [PATCH 17/43] Changed type field to be Type instead of string --- Celeste.Mod.mm/Mod/Entities/EntityCollider.cs | 2 +- Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs b/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs index f1b6069e1..a9a8af97e 100644 --- a/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs +++ b/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs @@ -13,7 +13,7 @@ public class EntityCollider : Component where T : Entity { /// /// Provides a simple way to know the Entity type of the specific Collider without Reflection /// - public readonly string entityType = typeof(T).Name; + public readonly Type entityType = typeof(T); /// /// The Action invoked on Collision, with the Component collided with passed as a parameter diff --git a/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs b/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs index 11f0334cb..7806053b7 100644 --- a/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs +++ b/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs @@ -14,7 +14,7 @@ public class EntityColliderByComponent : Component where T : Component { /// /// Provides a simple way to know the Component type of the specific Collider without Reflection /// - public readonly string componentType = typeof(T).Name; + public readonly Type componentType = typeof(T); /// /// The Action invoked on Collision, with the Component collided with passed as a parameter From e6a0da1a51f7ff7615ab2b3e9142f363a4e65dbc Mon Sep 17 00:00:00 2001 From: David Higueros <111460331+TheDavSmasher@users.noreply.github.com> Date: Sun, 1 Dec 2024 14:54:05 -0700 Subject: [PATCH 18/43] Made PascalCase and changed to property --- Celeste.Mod.mm/Mod/Entities/EntityCollider.cs | 4 ++-- Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs b/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs index a9a8af97e..112b70ed0 100644 --- a/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs +++ b/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs @@ -11,9 +11,9 @@ namespace Celeste.Mod.Entities { [Tracked(false)] public class EntityCollider : Component where T : Entity { /// - /// Provides a simple way to know the Entity type of the specific Collider without Reflection + /// Provides a simple way to know the Entity type of the specific Collider /// - public readonly Type entityType = typeof(T); + public Type EntityType => typeof(T); /// /// The Action invoked on Collision, with the Component collided with passed as a parameter diff --git a/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs b/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs index 7806053b7..b272d448a 100644 --- a/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs +++ b/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs @@ -12,9 +12,9 @@ namespace Celeste.Mod.Entities { [Tracked(false)] public class EntityColliderByComponent : Component where T : Component { /// - /// Provides a simple way to know the Component type of the specific Collider without Reflection + /// Provides a simple way to know the Component type of the specific Collider /// - public readonly Type componentType = typeof(T); + public Type ComponentType => typeof(T); /// /// The Action invoked on Collision, with the Component collided with passed as a parameter From d4cdbf718d75103a42712705164364f5e32d1c93 Mon Sep 17 00:00:00 2001 From: David Higueros <111460331+TheDavSmasher@users.noreply.github.com> Date: Sun, 1 Dec 2024 15:01:30 -0700 Subject: [PATCH 19/43] Changed exception message --- Celeste.Mod.mm/Patches/Monocle/Tracker.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs index 1cfd233ac..4bffa8b10 100644 --- a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs +++ b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs @@ -188,7 +188,7 @@ public static void RefreshTracker(Type type) { else if (typeof(Component).IsAssignableFrom(type) && !Engine.Scene.Tracker.Components.ContainsKey(type)) { Engine.Scene.Tracker.Components.Add(type, new List()); } else { - throw new Exception("Type '" + type.Name + "' cannot be TrackedAs because it does not derive from Entity or Component"); + throw new Exception("Type '" + type.Name + "' does not derive from Entity or Component"); } RefreshTrackerLists(); } From 5b6b2e086eea6c014240a936ea1877571c6429ea Mon Sep 17 00:00:00 2001 From: David Higueros <111460331+TheDavSmasher@users.noreply.github.com> Date: Sun, 1 Dec 2024 15:01:53 -0700 Subject: [PATCH 20/43] Changed Initialize to use AddTypeToTracker to avoid duplicate code --- Celeste.Mod.mm/Patches/Monocle/Tracker.cs | 67 ++++------------------- 1 file changed, 12 insertions(+), 55 deletions(-) diff --git a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs index 4bffa8b10..70f5c3088 100644 --- a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs +++ b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs @@ -71,52 +71,7 @@ private static List GetSubclasses(Type type) { object[] customAttributes = type.GetCustomAttributes(typeof(TrackedAsAttribute), inherit: false); foreach (object customAttribute in customAttributes) { TrackedAsAttribute trackedAs = customAttribute as TrackedAsAttribute; - Type trackedAsType = trackedAs.TrackedAsType; - bool inherited = trackedAs.Inherited; - if (typeof(Entity).IsAssignableFrom(type)) { - if (!type.IsAbstract) { - // this is an entity. copy the registered types for the target entity - if (!TrackedEntityTypes.ContainsKey(type)) { - TrackedEntityTypes.Add(type, new List()); - } - TrackedEntityTypes[type].AddRange(TrackedEntityTypes.TryGetValue(trackedAsType, out List list) ? list : new List()); - TrackedEntityTypes[type] = TrackedEntityTypes[type].Distinct().ToList(); - } - if (inherited) { - // do the same for subclasses - foreach (Type subclass in GetSubclasses(type)) { - if (!subclass.IsAbstract) { - if (!TrackedEntityTypes.ContainsKey(subclass)) - TrackedEntityTypes.Add(subclass, new List()); - TrackedEntityTypes[subclass].AddRange(TrackedEntityTypes.TryGetValue(trackedAsType, out List list) ? list : new List()); - TrackedEntityTypes[subclass] = TrackedEntityTypes[subclass].Distinct().ToList(); - } - } - } - } else if (typeof(Component).IsAssignableFrom(type)) { - if (!type.IsAbstract) { - // this is an component. copy the registered types for the target component - if (!TrackedComponentTypes.ContainsKey(type)) { - TrackedComponentTypes.Add(type, new List()); - } - TrackedComponentTypes[type].AddRange(TrackedComponentTypes.TryGetValue(trackedAsType, out List list) ? list : new List()); - TrackedComponentTypes[type] = TrackedComponentTypes[type].Distinct().ToList(); - } - if (inherited) { - // do the same for subclasses - foreach (Type subclass in GetSubclasses(type)) { - if (!subclass.IsAbstract) { - if (!TrackedComponentTypes.ContainsKey(subclass)) - TrackedComponentTypes.Add(subclass, new List()); - TrackedComponentTypes[subclass].AddRange(TrackedComponentTypes.TryGetValue(trackedAsType, out List list) ? list : new List()); - TrackedComponentTypes[subclass] = TrackedComponentTypes[subclass].Distinct().ToList(); - } - } - } - } else { - // this is neither an entity nor a component. Help! - throw new Exception("Type '" + type.Name + "' cannot be TrackedAs because it does not derive from Entity or Component"); - } + AddTypeToTracker(type, trackedAs.TrackedAsType, trackedAs.Inherited); } } @@ -129,53 +84,55 @@ public static void AddTypeToTracker(Type type, Type trackedAs = null, bool inher } public static void AddTypeToTracker(Type type, Type trackedAs = null, params Type[] subtypes) { + Type trackedAsType = trackedAs != null && trackedAs.IsAssignableFrom(type) ? trackedAs : type; if (typeof(Entity).IsAssignableFrom(type)) { + // this is an entity. copy the registered types for the target entity StoredEntityTypes.Add(type); if (!type.IsAbstract) { if (!TrackedEntityTypes.TryGetValue(type, out List value)) { value = new List(); TrackedEntityTypes.Add(type, value); } - value.AddRange(TrackedEntityTypes.TryGetValue(trackedAs != null && trackedAs.IsAssignableFrom(type) - ? trackedAs : type, out List list) ? list : new List()); + value.AddRange(TrackedEntityTypes.TryGetValue(trackedAsType, out List list) ? list : new List()); TrackedEntityTypes[type] = value.Distinct().ToList(); } + // do the same for subclasses foreach (Type subtype in subtypes) { if (!subtype.IsAbstract) { if (!TrackedEntityTypes.TryGetValue(subtype, out List value)) { value = new List(); TrackedEntityTypes.Add(subtype, value); } - value.AddRange(TrackedEntityTypes.TryGetValue(trackedAs != null && trackedAs.IsAssignableFrom(type) - ? trackedAs : type, out List list) ? list : new List()); + value.AddRange(TrackedEntityTypes.TryGetValue(trackedAsType, out List list) ? list : new List()); TrackedEntityTypes[subtype] = value.Distinct().ToList(); } } } else if (typeof(Component).IsAssignableFrom(type)) { + // this is an component. copy the registered types for the target component StoredComponentTypes.Add(type); if (!type.IsAbstract) { if (!TrackedComponentTypes.TryGetValue(type, out List value)) { value = new List(); TrackedComponentTypes.Add(type, value); } - value.AddRange(TrackedComponentTypes.TryGetValue(trackedAs != null && trackedAs.IsAssignableFrom(type) - ? trackedAs : type, out List list) ? list : new List()); + value.AddRange(TrackedComponentTypes.TryGetValue(trackedAsType, out List list) ? list : new List()); TrackedComponentTypes[type] = value.Distinct().ToList(); } - foreach(Type subtype in subtypes) { + // do the same for subclasses + foreach (Type subtype in subtypes) { if (!subtype.IsAbstract) { if (!TrackedComponentTypes.TryGetValue(subtype, out List value)) { value = new List(); TrackedComponentTypes.Add(subtype, value); } - value.AddRange(TrackedComponentTypes.TryGetValue(trackedAs != null && trackedAs.IsAssignableFrom(type) - ? trackedAs : type, out List list) ? list : new List()); + value.AddRange(TrackedComponentTypes.TryGetValue(trackedAsType, out List list) ? list : new List()); TrackedComponentTypes[subtype] = value.Distinct().ToList(); } } } else { + // this is neither an entity nor a component. Help! throw new Exception("Type '" + type.Name + "' cannot be TrackedAs because it does not derive from Entity or Component"); } RefreshTracker(type); From 8d65602292187992ae549fdae90441ee74d77278 Mon Sep 17 00:00:00 2001 From: David Higueros <111460331+TheDavSmasher@users.noreply.github.com> Date: Sun, 1 Dec 2024 15:02:42 -0700 Subject: [PATCH 21/43] Removed parameterized RefreshTracker --- Celeste.Mod.mm/Patches/Monocle/Tracker.cs | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs index 70f5c3088..b48622dfe 100644 --- a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs +++ b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs @@ -135,19 +135,6 @@ public static void AddTypeToTracker(Type type, Type trackedAs = null, params Typ // this is neither an entity nor a component. Help! throw new Exception("Type '" + type.Name + "' cannot be TrackedAs because it does not derive from Entity or Component"); } - RefreshTracker(type); - } - - public static void RefreshTracker(Type type) { - if (typeof(Entity).IsAssignableFrom(type) && !Engine.Scene.Tracker.Entities.ContainsKey(type)) { - Engine.Scene.Tracker.Entities.Add(type, new List()); - } - else if (typeof(Component).IsAssignableFrom(type) && !Engine.Scene.Tracker.Components.ContainsKey(type)) { - Engine.Scene.Tracker.Components.Add(type, new List()); - } else { - throw new Exception("Type '" + type.Name + "' does not derive from Entity or Component"); - } - RefreshTrackerLists(); } public static void RefreshTracker() { From 610ffb3ab87fd092f080fb79acf593a3e0a8c173 Mon Sep 17 00:00:00 2001 From: David Higueros <111460331+TheDavSmasher@users.noreply.github.com> Date: Sun, 1 Dec 2024 15:15:36 -0700 Subject: [PATCH 22/43] Made refresh instance method --- Celeste.Mod.mm/Patches/Monocle/Tracker.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs index b48622dfe..7a3d322b8 100644 --- a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs +++ b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs @@ -137,15 +137,19 @@ public static void AddTypeToTracker(Type type, Type trackedAs = null, params Typ } } - public static void RefreshTracker() { + /// + /// Ensures the current scene's tracker contains all entities of all tracked Types. + /// Must be called if a type is added to the tracker manually and if the active scene changes. + /// + public void Refresh() { foreach (Type entityType in StoredEntityTypes) { - if (!Engine.Scene.Tracker.Entities.ContainsKey(entityType)) { - Engine.Scene.Tracker.Entities.Add(entityType, new List()); + if (!Entities.ContainsKey(entityType)) { + Entities.Add(entityType, new List()); } } foreach (Type componentType in StoredComponentTypes) { - if (!Engine.Scene.Tracker.Components.ContainsKey(componentType)) { - Engine.Scene.Tracker.Components.Add(componentType, new List()); + if (!Components.ContainsKey(componentType)) { + Components.Add(componentType, new List()); } } RefreshTrackerLists(); From a35664d34f59c84f164d3b17d4d3e62cc794c8e6 Mon Sep 17 00:00:00 2001 From: David Higueros <111460331+TheDavSmasher@users.noreply.github.com> Date: Sun, 1 Dec 2024 15:29:58 -0700 Subject: [PATCH 23/43] Made non-static and added check if already inside tracker to prevent duplicates --- Celeste.Mod.mm/Patches/Monocle/Tracker.cs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs index 7a3d322b8..9531b1595 100644 --- a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs +++ b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs @@ -155,23 +155,25 @@ public void Refresh() { RefreshTrackerLists(); } - private static void RefreshTrackerLists() { + private void RefreshTrackerLists() { foreach (Entity entity in Engine.Scene.Entities) { foreach (Component component in entity.Components) { Type componentType = component.GetType(); - if (!TrackedComponentTypes.TryGetValue(componentType, out List componentTypes)) { + if (Components[componentType].Contains(component) + || !TrackedComponentTypes.TryGetValue(componentType, out List componentTypes)) { continue; } foreach (Type trackedType in componentTypes) { - Engine.Scene.Tracker.Components[trackedType].Add(component); + Components[trackedType].Add(component); } } Type entityType = entity.GetType(); - if (!TrackedEntityTypes.TryGetValue(entityType, out List entityTypes)) { + if (Entities[entityType].Contains(entity) + || !TrackedEntityTypes.TryGetValue(entityType, out List entityTypes)) { continue; } foreach (Type trackedType in entityTypes) { - Engine.Scene.Tracker.Entities[trackedType].Add(entity); + Entities[trackedType].Add(entity); } } } From 024a821bf144a6011da2e22bfce1d7e6d74ebe2b Mon Sep 17 00:00:00 2001 From: David Higueros <111460331+TheDavSmasher@users.noreply.github.com> Date: Sun, 1 Dec 2024 16:04:30 -0700 Subject: [PATCH 24/43] Added instance field Unrefreshed to prevent Refresh being executed again without type being added to Tracker first --- Celeste.Mod.mm/Patches/Monocle/Tracker.cs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs index 9531b1595..15af70a9c 100644 --- a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs +++ b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs @@ -39,6 +39,8 @@ class patch_Tracker : Tracker { private static Type[] _temporaryAllTypes; private static Type[] GetAllTypesUncached() => FakeAssembly.GetFakeEntryAssembly().GetTypesSafe(); + + private bool Unrefreshed; [MonoModReplace] private static List GetSubclasses(Type type) { @@ -74,6 +76,7 @@ private static List GetSubclasses(Type type) { AddTypeToTracker(type, trackedAs.TrackedAsType, trackedAs.Inherited); } } + (Engine.Scene.Tracker as patch_Tracker).Unrefreshed = false; // don't hold references to all the types anymore _temporaryAllTypes = null; @@ -84,6 +87,7 @@ public static void AddTypeToTracker(Type type, Type trackedAs = null, bool inher } public static void AddTypeToTracker(Type type, Type trackedAs = null, params Type[] subtypes) { + (Engine.Scene.Tracker as patch_Tracker).Unrefreshed = true; Type trackedAsType = trackedAs != null && trackedAs.IsAssignableFrom(type) ? trackedAs : type; if (typeof(Entity).IsAssignableFrom(type)) { // this is an entity. copy the registered types for the target entity @@ -133,15 +137,21 @@ public static void AddTypeToTracker(Type type, Type trackedAs = null, params Typ } else { // this is neither an entity nor a component. Help! - throw new Exception("Type '" + type.Name + "' cannot be TrackedAs because it does not derive from Entity or Component"); + throw new Exception("Type '" + type.Name + "' cannot be Tracked because it does not derive from Entity or Component"); } } /// /// Ensures the current scene's tracker contains all entities of all tracked Types. /// Must be called if a type is added to the tracker manually and if the active scene changes. + /// If called back to back without a type added to the Tracker, it won't go through again, for performance. + /// will make ensure the Refresh happens, even if run back to back. /// - public void Refresh() { + public void Refresh(bool force = false) { + if (!Unrefreshed && !force) { + return; + } + Unrefreshed = false; foreach (Type entityType in StoredEntityTypes) { if (!Entities.ContainsKey(entityType)) { Entities.Add(entityType, new List()); @@ -152,10 +162,6 @@ public void Refresh() { Components.Add(componentType, new List()); } } - RefreshTrackerLists(); - } - - private void RefreshTrackerLists() { foreach (Entity entity in Engine.Scene.Entities) { foreach (Component component in entity.Components) { Type componentType = component.GetType(); From 64d8f9c085e455380069eecce32f2f7193c97296 Mon Sep 17 00:00:00 2001 From: David Higueros <111460331+TheDavSmasher@users.noreply.github.com> Date: Sun, 1 Dec 2024 16:13:55 -0700 Subject: [PATCH 25/43] Added note to summary for Refresh --- Celeste.Mod.mm/Patches/Monocle/Tracker.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs index 15af70a9c..04859570c 100644 --- a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs +++ b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs @@ -146,6 +146,7 @@ public static void AddTypeToTracker(Type type, Type trackedAs = null, params Typ /// Must be called if a type is added to the tracker manually and if the active scene changes. /// If called back to back without a type added to the Tracker, it won't go through again, for performance. /// will make ensure the Refresh happens, even if run back to back. + /// Due to only the active Scene's Tracker's refreshed state is changed, must be true if a different scene becomes active. /// public void Refresh(bool force = false) { if (!Unrefreshed && !force) { From 56b05493e8b96b92a5f43fc4ab089a582a22f60d Mon Sep 17 00:00:00 2001 From: David Higueros <111460331+TheDavSmasher@users.noreply.github.com> Date: Sun, 1 Dec 2024 16:15:49 -0700 Subject: [PATCH 26/43] Overrode Added and EntityAwake methods to Refresh the Tracker: Added if added mid-scene and EntityAwake if added pre-scene --- Celeste.Mod.mm/Mod/Entities/EntityCollider.cs | 9 +++++++++ Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs b/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs index 112b70ed0..77c35a9a2 100644 --- a/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs +++ b/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs @@ -28,6 +28,11 @@ public EntityCollider(Action onEntityAction, Collider collider = null) Collider = collider; } + public override void Added(Entity entity) { + base.Added(entity); + (Scene?.Tracker as patch_Tracker).Refresh(); + } + public override void EntityAdded(Scene scene) { if (!scene.Tracker.IsEntityTracked()) { patch_Tracker.AddTypeToTracker(typeof(T)); @@ -35,6 +40,10 @@ public override void EntityAdded(Scene scene) { base.EntityAdded(scene); } + public override void EntityAwake() { + (Scene.Tracker as patch_Tracker).Refresh(); + } + public override void Update() { if (OnEntityAction == null) { return; diff --git a/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs b/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs index b272d448a..a1baffc79 100644 --- a/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs +++ b/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs @@ -29,6 +29,11 @@ public EntityColliderByComponent(Action onComponentAction, Collider collider Collider = collider; } + public override void Added(Entity entity) { + base.Added(entity); + (Scene?.Tracker as patch_Tracker).Refresh(); + } + public override void EntityAdded(Scene scene) { if (!scene.Tracker.IsComponentTracked()) { patch_Tracker.AddTypeToTracker(typeof(T)); @@ -36,6 +41,10 @@ public override void EntityAdded(Scene scene) { base.EntityAdded(scene); } + public override void EntityAwake() { + (Scene.Tracker as patch_Tracker).Refresh(); + } + public override void Update() { if (OnComponentAction == null) { return; From e9312ac57cf98da20412cc43a64e589db46e8863 Mon Sep 17 00:00:00 2001 From: David Higueros <111460331+TheDavSmasher@users.noreply.github.com> Date: Mon, 2 Dec 2024 10:12:56 -0700 Subject: [PATCH 27/43] Do Dictionary check before List indexing --- Celeste.Mod.mm/Patches/Monocle/Tracker.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs index 04859570c..65f4be439 100644 --- a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs +++ b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs @@ -166,8 +166,8 @@ public void Refresh(bool force = false) { foreach (Entity entity in Engine.Scene.Entities) { foreach (Component component in entity.Components) { Type componentType = component.GetType(); - if (Components[componentType].Contains(component) - || !TrackedComponentTypes.TryGetValue(componentType, out List componentTypes)) { + if (!TrackedComponentTypes.TryGetValue(componentType, out List componentTypes) + || Components[componentType].Contains(component)) { continue; } foreach (Type trackedType in componentTypes) { @@ -175,8 +175,8 @@ public void Refresh(bool force = false) { } } Type entityType = entity.GetType(); - if (Entities[entityType].Contains(entity) - || !TrackedEntityTypes.TryGetValue(entityType, out List entityTypes)) { + if (!TrackedEntityTypes.TryGetValue(entityType, out List entityTypes) + || Entities[entityType].Contains(entity)) { continue; } foreach (Type trackedType in entityTypes) { From 33913484256a45a9158c158a7442d23ad2c81c26 Mon Sep 17 00:00:00 2001 From: David Higueros <111460331+TheDavSmasher@users.noreply.github.com> Date: Mon, 2 Dec 2024 10:18:29 -0700 Subject: [PATCH 28/43] Fixed summary error --- Celeste.Mod.mm/Mod/Entities/EntityCollider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs b/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs index 77c35a9a2..cf9d59fa4 100644 --- a/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs +++ b/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs @@ -16,7 +16,7 @@ public class EntityCollider : Component where T : Entity { public Type EntityType => typeof(T); /// - /// The Action invoked on Collision, with the Component collided with passed as a parameter + /// The Action invoked on Collision, with the Entity collided with passed as a parameter /// public Action OnEntityAction; From 4b1e9acb64796489f4a2b640a7f6f122c3587eea Mon Sep 17 00:00:00 2001 From: David Higueros <111460331+TheDavSmasher@users.noreply.github.com> Date: Mon, 2 Dec 2024 10:29:46 -0700 Subject: [PATCH 29/43] Added AddTypeToTracker to Added before refresh, both only if Entity is already on scene --- Celeste.Mod.mm/Mod/Entities/EntityCollider.cs | 9 ++++++++- Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs b/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs index cf9d59fa4..cc1505ff5 100644 --- a/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs +++ b/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs @@ -1,6 +1,7 @@ using Monocle; using System; using Microsoft.Xna.Framework; +using static System.Formats.Asn1.AsnWriter; namespace Celeste.Mod.Entities { /// @@ -30,7 +31,13 @@ public EntityCollider(Action onEntityAction, Collider collider = null) public override void Added(Entity entity) { base.Added(entity); - (Scene?.Tracker as patch_Tracker).Refresh(); + //Only called if Component is added post Scene Begin and Entity Adding and Awake time. + if (Scene != null) { + if (!Scene.Tracker.IsEntityTracked()) { + patch_Tracker.AddTypeToTracker(typeof(T)); + } + (Scene.Tracker as patch_Tracker).Refresh(); + } } public override void EntityAdded(Scene scene) { diff --git a/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs b/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs index a1baffc79..180447b94 100644 --- a/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs +++ b/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs @@ -1,6 +1,7 @@ using Monocle; using System; using Microsoft.Xna.Framework; +using static System.Formats.Asn1.AsnWriter; namespace Celeste.Mod.Entities { /// @@ -31,7 +32,13 @@ public EntityColliderByComponent(Action onComponentAction, Collider collider public override void Added(Entity entity) { base.Added(entity); - (Scene?.Tracker as patch_Tracker).Refresh(); + //Only called if Component is added post Scene Begin and Entity Adding and Awake time. + if (Scene != null) { + if (!Scene.Tracker.IsComponentTracked()) { + patch_Tracker.AddTypeToTracker(typeof(T)); + } + (Scene.Tracker as patch_Tracker).Refresh(); + } } public override void EntityAdded(Scene scene) { From df2f875fff25078474226d099aeca854985575c3 Mon Sep 17 00:00:00 2001 From: David Higueros <111460331+TheDavSmasher@users.noreply.github.com> Date: Mon, 2 Dec 2024 10:38:20 -0700 Subject: [PATCH 30/43] Removed unneeded using statement --- Celeste.Mod.mm/Mod/Entities/EntityCollider.cs | 1 - Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs | 1 - 2 files changed, 2 deletions(-) diff --git a/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs b/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs index cc1505ff5..c58abb83e 100644 --- a/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs +++ b/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs @@ -1,7 +1,6 @@ using Monocle; using System; using Microsoft.Xna.Framework; -using static System.Formats.Asn1.AsnWriter; namespace Celeste.Mod.Entities { /// diff --git a/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs b/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs index 180447b94..6aed9b8b0 100644 --- a/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs +++ b/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs @@ -1,7 +1,6 @@ using Monocle; using System; using Microsoft.Xna.Framework; -using static System.Formats.Asn1.AsnWriter; namespace Celeste.Mod.Entities { /// From 1bd3b0be6710bcf05a21c44a4bbce46374a9a8bc Mon Sep 17 00:00:00 2001 From: David Higueros <111460331+TheDavSmasher@users.noreply.github.com> Date: Mon, 2 Dec 2024 12:15:16 -0700 Subject: [PATCH 31/43] Attempt to reduce duplicate code between adding an Entity Type and Component Type to Tracker --- Celeste.Mod.mm/Patches/Monocle/Tracker.cs | 65 ++++++++--------------- 1 file changed, 21 insertions(+), 44 deletions(-) diff --git a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs index 65f4be439..5d69be12e 100644 --- a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs +++ b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs @@ -89,56 +89,33 @@ public static void AddTypeToTracker(Type type, Type trackedAs = null, bool inher public static void AddTypeToTracker(Type type, Type trackedAs = null, params Type[] subtypes) { (Engine.Scene.Tracker as patch_Tracker).Unrefreshed = true; Type trackedAsType = trackedAs != null && trackedAs.IsAssignableFrom(type) ? trackedAs : type; - if (typeof(Entity).IsAssignableFrom(type)) { - // this is an entity. copy the registered types for the target entity - StoredEntityTypes.Add(type); - if (!type.IsAbstract) { - if (!TrackedEntityTypes.TryGetValue(type, out List value)) { - value = new List(); - TrackedEntityTypes.Add(type, value); - } - value.AddRange(TrackedEntityTypes.TryGetValue(trackedAsType, out List list) ? list : new List()); - TrackedEntityTypes[type] = value.Distinct().ToList(); - } - // do the same for subclasses - foreach (Type subtype in subtypes) { - if (!subtype.IsAbstract) { - if (!TrackedEntityTypes.TryGetValue(subtype, out List value)) { - value = new List(); - TrackedEntityTypes.Add(subtype, value); - } - value.AddRange(TrackedEntityTypes.TryGetValue(trackedAsType, out List list) ? list : new List()); - TrackedEntityTypes[subtype] = value.Distinct().ToList(); - } + bool? trackedEntity = typeof(Entity).IsAssignableFrom(type) ? true : typeof(Component).IsAssignableFrom(type) ? false : null; + if (trackedEntity == null) { + // this is neither an entity nor a component. Help! + throw new Exception("Type '" + type.Name + "' cannot be Tracked because it does not derive from Entity or Component"); + } + // copy the registered types for the target type + ((bool)trackedEntity ? StoredEntityTypes : StoredComponentTypes).Add(type); + Dictionary> tracked = (bool)trackedEntity ? TrackedEntityTypes : TrackedComponentTypes; + if (!type.IsAbstract) { + if (!tracked.TryGetValue(type, out List value)) { + value = new List(); + tracked.Add(type, value); } + value.AddRange(tracked.TryGetValue(trackedAsType, out List list) ? list : new List()); + tracked[type] = value.Distinct().ToList(); } - else if (typeof(Component).IsAssignableFrom(type)) { - // this is an component. copy the registered types for the target component - StoredComponentTypes.Add(type); - if (!type.IsAbstract) { - if (!TrackedComponentTypes.TryGetValue(type, out List value)) { + // do the same for subclasses + foreach (Type subtype in subtypes) { + if (trackedAsType.IsAssignableFrom(subtype) && !subtype.IsAbstract) { + if (!tracked.TryGetValue(subtype, out List value)) { value = new List(); - TrackedComponentTypes.Add(type, value); - } - value.AddRange(TrackedComponentTypes.TryGetValue(trackedAsType, out List list) ? list : new List()); - TrackedComponentTypes[type] = value.Distinct().ToList(); - } - // do the same for subclasses - foreach (Type subtype in subtypes) { - if (!subtype.IsAbstract) { - if (!TrackedComponentTypes.TryGetValue(subtype, out List value)) { - value = new List(); - TrackedComponentTypes.Add(subtype, value); - } - value.AddRange(TrackedComponentTypes.TryGetValue(trackedAsType, out List list) ? list : new List()); - TrackedComponentTypes[subtype] = value.Distinct().ToList(); + tracked.Add(subtype, value); } + value.AddRange(tracked.TryGetValue(trackedAsType, out List list) ? list : new List()); + tracked[subtype] = value.Distinct().ToList(); } } - else { - // this is neither an entity nor a component. Help! - throw new Exception("Type '" + type.Name + "' cannot be Tracked because it does not derive from Entity or Component"); - } } /// From df175d3a2c77a59efa1d3fd0c7a2d2d572974917 Mon Sep 17 00:00:00 2001 From: David Higueros <111460331+TheDavSmasher@users.noreply.github.com> Date: Mon, 2 Dec 2024 12:19:39 -0700 Subject: [PATCH 32/43] Changed Exception Message to account for Tracked vs TrackedAs additions --- Celeste.Mod.mm/Patches/Monocle/Tracker.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs index 5d69be12e..59ba4ed5d 100644 --- a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs +++ b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs @@ -92,7 +92,7 @@ public static void AddTypeToTracker(Type type, Type trackedAs = null, params Typ bool? trackedEntity = typeof(Entity).IsAssignableFrom(type) ? true : typeof(Component).IsAssignableFrom(type) ? false : null; if (trackedEntity == null) { // this is neither an entity nor a component. Help! - throw new Exception("Type '" + type.Name + "' cannot be Tracked because it does not derive from Entity or Component"); + throw new Exception("Type '" + type.Name + "' cannot be Tracked" + (trackedAsType != type ? "As" : "") + " because it does not derive from Entity or Component"); } // copy the registered types for the target type ((bool)trackedEntity ? StoredEntityTypes : StoredComponentTypes).Add(type); From 7d6ad9f34e896e3299d55fd3da3dcb924ce1d9a6 Mon Sep 17 00:00:00 2001 From: David Higueros <111460331+TheDavSmasher@users.noreply.github.com> Date: Thu, 5 Dec 2024 17:51:43 -0700 Subject: [PATCH 33/43] Small refactors --- Celeste.Mod.mm/Patches/Monocle/Tracker.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs index 59ba4ed5d..c01219ecd 100644 --- a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs +++ b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs @@ -48,8 +48,7 @@ private static List GetSubclasses(Type type) { _temporaryAllTypes ??= GetAllTypesUncached(); List subclasses = new(); - foreach (Type otherType in _temporaryAllTypes) - { + foreach (Type otherType in _temporaryAllTypes) { if (type != otherType && type.IsAssignableFrom(otherType)) subclasses.Add(otherType); } @@ -95,8 +94,8 @@ public static void AddTypeToTracker(Type type, Type trackedAs = null, params Typ throw new Exception("Type '" + type.Name + "' cannot be Tracked" + (trackedAsType != type ? "As" : "") + " because it does not derive from Entity or Component"); } // copy the registered types for the target type - ((bool)trackedEntity ? StoredEntityTypes : StoredComponentTypes).Add(type); - Dictionary> tracked = (bool)trackedEntity ? TrackedEntityTypes : TrackedComponentTypes; + ((bool) trackedEntity ? StoredEntityTypes : StoredComponentTypes).Add(type); + Dictionary> tracked = (bool) trackedEntity ? TrackedEntityTypes : TrackedComponentTypes; if (!type.IsAbstract) { if (!tracked.TryGetValue(type, out List value)) { value = new List(); From c1a7050f10b21f31f39b95e428e331061f03380a Mon Sep 17 00:00:00 2001 From: David Higueros <111460331+TheDavSmasher@users.noreply.github.com> Date: Thu, 5 Dec 2024 17:52:18 -0700 Subject: [PATCH 34/43] Made untracked and removed Type property --- Celeste.Mod.mm/Mod/Entities/EntityCollider.cs | 6 ------ Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs | 6 ------ 2 files changed, 12 deletions(-) diff --git a/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs b/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs index c58abb83e..f5a51f9c0 100644 --- a/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs +++ b/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs @@ -8,13 +8,7 @@ namespace Celeste.Mod.Entities { /// Performs the Action provided on collision. /// /// The specific type of Entity this component should try to collide with - [Tracked(false)] public class EntityCollider : Component where T : Entity { - /// - /// Provides a simple way to know the Entity type of the specific Collider - /// - public Type EntityType => typeof(T); - /// /// The Action invoked on Collision, with the Entity collided with passed as a parameter /// diff --git a/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs b/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs index 6aed9b8b0..af875dce4 100644 --- a/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs +++ b/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs @@ -9,13 +9,7 @@ namespace Celeste.Mod.Entities { /// Performs the Action provided on collision. /// /// The specific type of Component this component should try to collide with - [Tracked(false)] public class EntityColliderByComponent : Component where T : Component { - /// - /// Provides a simple way to know the Component type of the specific Collider - /// - public Type ComponentType => typeof(T); - /// /// The Action invoked on Collision, with the Component collided with passed as a parameter /// From 42fac47b4b26509b1b8a9310b8edcad6eb29be3b Mon Sep 17 00:00:00 2001 From: wuke32767 <2446214230@qq.com> Date: Tue, 3 Dec 2024 14:03:33 +0800 Subject: [PATCH 35/43] use version to determine if tracker is outdated --- Celeste.Mod.mm/Patches/Monocle/Tracker.cs | 46 +++++++++++++++++------ 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs index c01219ecd..d59515543 100644 --- a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs +++ b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs @@ -40,13 +40,24 @@ class patch_Tracker : Tracker { private static Type[] GetAllTypesUncached() => FakeAssembly.GetFakeEntryAssembly().GetTypesSafe(); - private bool Unrefreshed; - + //can it overflow? + public static int TrackedTypeVersion; + + private int currentVersion; + + public extern void orig_ctor(); + + [MonoModConstructor] + public void ctor() { + orig_ctor(); + currentVersion = TrackedTypeVersion; + } + [MonoModReplace] private static List GetSubclasses(Type type) { bool shouldNullOutCache = _temporaryAllTypes is null; _temporaryAllTypes ??= GetAllTypesUncached(); - + List subclasses = new(); foreach (Type otherType in _temporaryAllTypes) { if (type != otherType && type.IsAssignableFrom(otherType)) @@ -57,14 +68,14 @@ private static List GetSubclasses(Type type) { // Let's do that now instead. if (shouldNullOutCache) _temporaryAllTypes = null; - + return subclasses; } public static extern void orig_Initialize(); public new static void Initialize() { _temporaryAllTypes = GetAllTypesUncached(); - + orig_Initialize(); // search for entities with [TrackedAs] @@ -75,8 +86,6 @@ private static List GetSubclasses(Type type) { AddTypeToTracker(type, trackedAs.TrackedAsType, trackedAs.Inherited); } } - (Engine.Scene.Tracker as patch_Tracker).Unrefreshed = false; - // don't hold references to all the types anymore _temporaryAllTypes = null; } @@ -86,13 +95,13 @@ public static void AddTypeToTracker(Type type, Type trackedAs = null, bool inher } public static void AddTypeToTracker(Type type, Type trackedAs = null, params Type[] subtypes) { - (Engine.Scene.Tracker as patch_Tracker).Unrefreshed = true; Type trackedAsType = trackedAs != null && trackedAs.IsAssignableFrom(type) ? trackedAs : type; bool? trackedEntity = typeof(Entity).IsAssignableFrom(type) ? true : typeof(Component).IsAssignableFrom(type) ? false : null; if (trackedEntity == null) { // this is neither an entity nor a component. Help! throw new Exception("Type '" + type.Name + "' cannot be Tracked" + (trackedAsType != type ? "As" : "") + " because it does not derive from Entity or Component"); } + bool updated = false; // copy the registered types for the target type ((bool) trackedEntity ? StoredEntityTypes : StoredComponentTypes).Add(type); Dictionary> tracked = (bool) trackedEntity ? TrackedEntityTypes : TrackedComponentTypes; @@ -101,8 +110,13 @@ public static void AddTypeToTracker(Type type, Type trackedAs = null, params Typ value = new List(); tracked.Add(type, value); } + int cnt = value.Count; value.AddRange(tracked.TryGetValue(trackedAsType, out List list) ? list : new List()); - tracked[type] = value.Distinct().ToList(); + var result = value.Distinct().ToList(); + tracked[type] = result; + if (cnt != result.Count) { + updated = true; + } } // do the same for subclasses foreach (Type subtype in subtypes) { @@ -111,10 +125,18 @@ public static void AddTypeToTracker(Type type, Type trackedAs = null, params Typ value = new List(); tracked.Add(subtype, value); } + int cnt = value.Count; value.AddRange(tracked.TryGetValue(trackedAsType, out List list) ? list : new List()); - tracked[subtype] = value.Distinct().ToList(); + var result = value.Distinct().ToList(); + tracked[subtype] = result; + if (cnt != result.Count) { + updated = true; + } } } + if (updated) { + TrackedTypeVersion++; + } } /// @@ -125,10 +147,10 @@ public static void AddTypeToTracker(Type type, Type trackedAs = null, params Typ /// Due to only the active Scene's Tracker's refreshed state is changed, must be true if a different scene becomes active. /// public void Refresh(bool force = false) { - if (!Unrefreshed && !force) { + if (currentVersion == TrackedTypeVersion && !force) { return; } - Unrefreshed = false; + currentVersion = TrackedTypeVersion; foreach (Type entityType in StoredEntityTypes) { if (!Entities.ContainsKey(entityType)) { Entities.Add(entityType, new List()); From 5c4d28543ec659fb9bc5d2518934c9f71cac84a3 Mon Sep 17 00:00:00 2001 From: David Higueros <111460331+TheDavSmasher@users.noreply.github.com> Date: Thu, 5 Dec 2024 18:10:13 -0700 Subject: [PATCH 36/43] Simplified assignment and boolean expressions --- Celeste.Mod.mm/Patches/Monocle/Tracker.cs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs index d59515543..4595b650b 100644 --- a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs +++ b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs @@ -112,11 +112,8 @@ public static void AddTypeToTracker(Type type, Type trackedAs = null, params Typ } int cnt = value.Count; value.AddRange(tracked.TryGetValue(trackedAsType, out List list) ? list : new List()); - var result = value.Distinct().ToList(); - tracked[type] = result; - if (cnt != result.Count) { - updated = true; - } + List result = tracked[type] = value.Distinct().ToList(); + updated = cnt != result.Count; } // do the same for subclasses foreach (Type subtype in subtypes) { @@ -127,11 +124,8 @@ public static void AddTypeToTracker(Type type, Type trackedAs = null, params Typ } int cnt = value.Count; value.AddRange(tracked.TryGetValue(trackedAsType, out List list) ? list : new List()); - var result = value.Distinct().ToList(); - tracked[subtype] = result; - if (cnt != result.Count) { - updated = true; - } + List result = tracked[subtype] = value.Distinct().ToList(); + updated = cnt != result.Count; } } if (updated) { @@ -147,7 +141,7 @@ public static void AddTypeToTracker(Type type, Type trackedAs = null, params Typ /// Due to only the active Scene's Tracker's refreshed state is changed, must be true if a different scene becomes active. /// public void Refresh(bool force = false) { - if (currentVersion == TrackedTypeVersion && !force) { + if (currentVersion >= TrackedTypeVersion && !force) { return; } currentVersion = TrackedTypeVersion; From 2170b466fc53a18527ba52e69701abef021b34a0 Mon Sep 17 00:00:00 2001 From: David Higueros <111460331+TheDavSmasher@users.noreply.github.com> Date: Thu, 5 Dec 2024 18:14:23 -0700 Subject: [PATCH 37/43] Added Summary and set Tracker Version to default after initialization to prevent refreshes immediately after creation --- Celeste.Mod.mm/Patches/Monocle/Tracker.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs index 4595b650b..64dcba9ec 100644 --- a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs +++ b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs @@ -40,9 +40,14 @@ class patch_Tracker : Tracker { private static Type[] GetAllTypesUncached() => FakeAssembly.GetFakeEntryAssembly().GetTypesSafe(); - //can it overflow? + /// + /// Represents the amount of Types added by external callers to the Tracked Types + /// public static int TrackedTypeVersion; + /// + /// Represents the amount of Types currently added to the tracker from Types added externally + /// private int currentVersion; public extern void orig_ctor(); @@ -79,6 +84,7 @@ private static List GetSubclasses(Type type) { orig_Initialize(); // search for entities with [TrackedAs] + int oldVersion = TrackedTypeVersion; foreach (Type type in _temporaryAllTypes) { object[] customAttributes = type.GetCustomAttributes(typeof(TrackedAsAttribute), inherit: false); foreach (object customAttribute in customAttributes) { @@ -86,6 +92,7 @@ private static List GetSubclasses(Type type) { AddTypeToTracker(type, trackedAs.TrackedAsType, trackedAs.Inherited); } } + TrackedTypeVersion = oldVersion; // don't hold references to all the types anymore _temporaryAllTypes = null; } From 4da146f6fedf033ed643a90834d6bea7de14f1ff Mon Sep 17 00:00:00 2001 From: David Higueros <111460331+TheDavSmasher@users.noreply.github.com> Date: Thu, 5 Dec 2024 18:24:24 -0700 Subject: [PATCH 38/43] Made TrackedTypeVersion private and removed Summary --- Celeste.Mod.mm/Patches/Monocle/Tracker.cs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs index 64dcba9ec..1d37fe656 100644 --- a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs +++ b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs @@ -40,14 +40,8 @@ class patch_Tracker : Tracker { private static Type[] GetAllTypesUncached() => FakeAssembly.GetFakeEntryAssembly().GetTypesSafe(); - /// - /// Represents the amount of Types added by external callers to the Tracked Types - /// - public static int TrackedTypeVersion; + private static int TrackedTypeVersion; - /// - /// Represents the amount of Types currently added to the tracker from Types added externally - /// private int currentVersion; public extern void orig_ctor(); From d21dcfd226f304dfe4a850b3095e9129a97d6088 Mon Sep 17 00:00:00 2001 From: David Higueros <111460331+TheDavSmasher@users.noreply.github.com> Date: Mon, 9 Dec 2024 13:55:51 -0700 Subject: [PATCH 39/43] Made static and requires a scene parameter, which defaults to Engine.Scene if not passed --- Celeste.Mod.mm/Patches/Monocle/Tracker.cs | 31 ++++++++++++----------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs index 1d37fe656..c1e76ae96 100644 --- a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs +++ b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs @@ -135,45 +135,46 @@ public static void AddTypeToTracker(Type type, Type trackedAs = null, params Typ } /// - /// Ensures the current scene's tracker contains all entities of all tracked Types. - /// Must be called if a type is added to the tracker manually and if the active scene changes. + /// Ensures the 's tracker contains all entities of all tracked Types from the . + /// Must be called if a type is added to the tracker manually and if the 's Tracker isn't refreshed. /// If called back to back without a type added to the Tracker, it won't go through again, for performance. /// will make ensure the Refresh happens, even if run back to back. - /// Due to only the active Scene's Tracker's refreshed state is changed, must be true if a different scene becomes active. + /// Only the 's Tracker's refreshed state is changed. /// - public void Refresh(bool force = false) { - if (currentVersion >= TrackedTypeVersion && !force) { + public static void Refresh(Scene scene = null, bool force = false) { + scene ??= Engine.Scene; + if ((scene.Tracker as patch_Tracker).currentVersion >= TrackedTypeVersion && !force) { return; } - currentVersion = TrackedTypeVersion; + (scene.Tracker as patch_Tracker).currentVersion = TrackedTypeVersion; foreach (Type entityType in StoredEntityTypes) { - if (!Entities.ContainsKey(entityType)) { - Entities.Add(entityType, new List()); + if (!scene.Tracker.Entities.ContainsKey(entityType)) { + scene.Tracker.Entities.Add(entityType, new List()); } } foreach (Type componentType in StoredComponentTypes) { - if (!Components.ContainsKey(componentType)) { - Components.Add(componentType, new List()); + if (!scene.Tracker.Components.ContainsKey(componentType)) { + scene.Tracker.Components.Add(componentType, new List()); } } - foreach (Entity entity in Engine.Scene.Entities) { + foreach (Entity entity in scene.Entities) { foreach (Component component in entity.Components) { Type componentType = component.GetType(); if (!TrackedComponentTypes.TryGetValue(componentType, out List componentTypes) - || Components[componentType].Contains(component)) { + || scene.Tracker.Components[componentType].Contains(component)) { continue; } foreach (Type trackedType in componentTypes) { - Components[trackedType].Add(component); + scene.Tracker.Components[trackedType].Add(component); } } Type entityType = entity.GetType(); if (!TrackedEntityTypes.TryGetValue(entityType, out List entityTypes) - || Entities[entityType].Contains(entity)) { + || scene.Tracker.Entities[entityType].Contains(entity)) { continue; } foreach (Type trackedType in entityTypes) { - Entities[trackedType].Add(entity); + scene.Tracker.Entities[trackedType].Add(entity); } } } From 79ad2ff0f1de1b38b7248456cff39ccedf0e65d6 Mon Sep 17 00:00:00 2001 From: David Higueros <111460331+TheDavSmasher@users.noreply.github.com> Date: Mon, 9 Dec 2024 14:05:46 -0700 Subject: [PATCH 40/43] Changed parameter name and null check --- Celeste.Mod.mm/Patches/Monocle/Tracker.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs index c1e76ae96..2846f04c3 100644 --- a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs +++ b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs @@ -135,14 +135,14 @@ public static void AddTypeToTracker(Type type, Type trackedAs = null, params Typ } /// - /// Ensures the 's tracker contains all entities of all tracked Types from the . - /// Must be called if a type is added to the tracker manually and if the 's Tracker isn't refreshed. + /// Ensures the 's tracker contains all entities of all tracked Types from the . + /// Must be called if a type is added to the tracker manually and if the 's Tracker isn't refreshed. /// If called back to back without a type added to the Tracker, it won't go through again, for performance. /// will make ensure the Refresh happens, even if run back to back. - /// Only the 's Tracker's refreshed state is changed. + /// Only the 's Tracker's refreshed state is changed. /// - public static void Refresh(Scene scene = null, bool force = false) { - scene ??= Engine.Scene; + public static void Refresh(Scene toUpdate = null, bool force = false) { + Scene scene = toUpdate ?? Engine.Scene; if ((scene.Tracker as patch_Tracker).currentVersion >= TrackedTypeVersion && !force) { return; } From 797ca532e515f3c2ce4a91b2a4691da19be11452 Mon Sep 17 00:00:00 2001 From: David Higueros <111460331+TheDavSmasher@users.noreply.github.com> Date: Mon, 9 Dec 2024 14:14:54 -0700 Subject: [PATCH 41/43] Added summary note of default value of scene parameter --- Celeste.Mod.mm/Patches/Monocle/Tracker.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs index 2846f04c3..bcb7b028e 100644 --- a/Celeste.Mod.mm/Patches/Monocle/Tracker.cs +++ b/Celeste.Mod.mm/Patches/Monocle/Tracker.cs @@ -140,6 +140,7 @@ public static void AddTypeToTracker(Type type, Type trackedAs = null, params Typ /// If called back to back without a type added to the Tracker, it won't go through again, for performance. /// will make ensure the Refresh happens, even if run back to back. /// Only the 's Tracker's refreshed state is changed. + /// If is null, it will default to Engine.Scene. /// public static void Refresh(Scene toUpdate = null, bool force = false) { Scene scene = toUpdate ?? Engine.Scene; From 5385f3eee86616be669e3efb3e1a28695dc1af8d Mon Sep 17 00:00:00 2001 From: David Higueros <111460331+TheDavSmasher@users.noreply.github.com> Date: Mon, 9 Dec 2024 14:17:37 -0700 Subject: [PATCH 42/43] Changed method call to static call --- Celeste.Mod.mm/Mod/Entities/EntityCollider.cs | 4 ++-- Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs b/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs index f5a51f9c0..fc33d2a8e 100644 --- a/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs +++ b/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs @@ -29,7 +29,7 @@ public override void Added(Entity entity) { if (!Scene.Tracker.IsEntityTracked()) { patch_Tracker.AddTypeToTracker(typeof(T)); } - (Scene.Tracker as patch_Tracker).Refresh(); + patch_Tracker.Refresh(); } } @@ -41,7 +41,7 @@ public override void EntityAdded(Scene scene) { } public override void EntityAwake() { - (Scene.Tracker as patch_Tracker).Refresh(); + patch_Tracker.Refresh(); } public override void Update() { diff --git a/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs b/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs index af875dce4..c7e717277 100644 --- a/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs +++ b/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs @@ -30,7 +30,7 @@ public override void Added(Entity entity) { if (!Scene.Tracker.IsComponentTracked()) { patch_Tracker.AddTypeToTracker(typeof(T)); } - (Scene.Tracker as patch_Tracker).Refresh(); + patch_Tracker.Refresh(); } } @@ -42,7 +42,7 @@ public override void EntityAdded(Scene scene) { } public override void EntityAwake() { - (Scene.Tracker as patch_Tracker).Refresh(); + patch_Tracker.Refresh(); } public override void Update() { From 9ef4ab53d30ea1daeae004199750db1c61fc984f Mon Sep 17 00:00:00 2001 From: David Higueros <111460331+TheDavSmasher@users.noreply.github.com> Date: Mon, 9 Dec 2024 14:23:24 -0700 Subject: [PATCH 43/43] Called Refresh with the scene of the Entity --- Celeste.Mod.mm/Mod/Entities/EntityCollider.cs | 4 ++-- Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs b/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs index fc33d2a8e..63a21513e 100644 --- a/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs +++ b/Celeste.Mod.mm/Mod/Entities/EntityCollider.cs @@ -29,7 +29,7 @@ public override void Added(Entity entity) { if (!Scene.Tracker.IsEntityTracked()) { patch_Tracker.AddTypeToTracker(typeof(T)); } - patch_Tracker.Refresh(); + patch_Tracker.Refresh(Scene); } } @@ -41,7 +41,7 @@ public override void EntityAdded(Scene scene) { } public override void EntityAwake() { - patch_Tracker.Refresh(); + patch_Tracker.Refresh(Scene); } public override void Update() { diff --git a/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs b/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs index c7e717277..c8e9a98d4 100644 --- a/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs +++ b/Celeste.Mod.mm/Mod/Entities/EntityColliderByComponent.cs @@ -30,7 +30,7 @@ public override void Added(Entity entity) { if (!Scene.Tracker.IsComponentTracked()) { patch_Tracker.AddTypeToTracker(typeof(T)); } - patch_Tracker.Refresh(); + patch_Tracker.Refresh(Scene); } } @@ -42,7 +42,7 @@ public override void EntityAdded(Scene scene) { } public override void EntityAwake() { - patch_Tracker.Refresh(); + patch_Tracker.Refresh(Scene); } public override void Update() {