Cleanup branch work

This commit is contained in:
Michal Pikulski
2025-11-04 11:47:09 +01:00
committed by Michal Pikulski
parent f88bd0e2c9
commit 3e835ed3b8
8 changed files with 588 additions and 1877 deletions

View File

@@ -73,7 +73,6 @@ namespace Core.Lifecycle
#region Private Fields
private ManagedEventManager _eventManager;
private bool _isRegistered;
#endregion
@@ -86,8 +85,6 @@ namespace Core.Lifecycle
/// </summary>
protected virtual void Awake()
{
_eventManager = new ManagedEventManager();
if (LifecycleManager.Instance != null)
{
LifecycleManager.Instance.Register(this);
@@ -114,9 +111,6 @@ namespace Core.Lifecycle
LifecycleManager.Instance.Unregister(this);
}
// Auto-cleanup managed events
_eventManager?.UnregisterAllEvents();
// Auto-unregister from GameManager if auto-registered
if (AutoRegisterPausable && this is AppleHills.Core.Interfaces.IPausable pausable)
@@ -197,28 +191,6 @@ namespace Core.Lifecycle
}
#endregion
#region Helper Methods
/// <summary>
/// Register an event subscription for automatic cleanup on destroy.
/// Prevents memory leaks by ensuring the event is unsubscribed when this component is destroyed.
/// </summary>
/// <param name="target">The object that owns the event</param>
/// <param name="eventName">Name of the event (e.g., "SceneLoadCompleted")</param>
/// <param name="handler">The delegate/handler for the event</param>
protected void RegisterManagedEvent(object target, string eventName, Delegate handler)
{
if (_eventManager == null)
{
Debug.LogWarning($"[ManagedBehaviour] Event manager not initialized for {gameObject.name}");
return;
}
_eventManager.RegisterEvent(target, eventName, handler);
}
#endregion
}
}

View File

@@ -1,102 +0,0 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
namespace Core.Lifecycle
{
/// <summary>
/// Stores information about a single event subscription for automatic cleanup.
/// </summary>
internal class EventSubscriptionInfo
{
public object Target { get; set; }
public Delegate Handler { get; set; }
public string EventName { get; set; }
}
/// <summary>
/// Manages event subscriptions for a ManagedBehaviour with automatic cleanup on destroy.
/// Prevents memory leaks by ensuring all event subscriptions are properly unsubscribed.
/// </summary>
public class ManagedEventManager
{
private readonly List<EventSubscriptionInfo> _subscriptions = new List<EventSubscriptionInfo>();
/// <summary>
/// Register an event subscription for automatic cleanup.
/// </summary>
/// <param name="target">The object that owns the event</param>
/// <param name="eventName">Name of the event (e.g., "OnSomethingHappened")</param>
/// <param name="handler">The delegate/handler for the event</param>
public void RegisterEvent(object target, string eventName, Delegate handler)
{
if (target == null)
{
Debug.LogWarning("[ManagedEventManager] Cannot register event on null target");
return;
}
if (string.IsNullOrEmpty(eventName))
{
Debug.LogWarning("[ManagedEventManager] Event name cannot be null or empty");
return;
}
if (handler == null)
{
Debug.LogWarning("[ManagedEventManager] Handler cannot be null");
return;
}
_subscriptions.Add(new EventSubscriptionInfo
{
Target = target,
EventName = eventName,
Handler = handler
});
}
/// <summary>
/// Unregister all event subscriptions. Called automatically on ManagedBehaviour destruction.
/// </summary>
public void UnregisterAllEvents()
{
foreach (var subscription in _subscriptions)
{
try
{
if (subscription.Target == null)
continue;
// Use reflection to get the event and unsubscribe
var eventInfo = subscription.Target.GetType().GetEvent(
subscription.EventName,
BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static
);
if (eventInfo != null)
{
eventInfo.RemoveEventHandler(subscription.Target, subscription.Handler);
}
else
{
Debug.LogWarning($"[ManagedEventManager] Could not find event '{subscription.EventName}' on type '{subscription.Target.GetType().Name}'");
}
}
catch (Exception ex)
{
Debug.LogError($"[ManagedEventManager] Error unsubscribing from event '{subscription.EventName}': {ex.Message}");
}
}
_subscriptions.Clear();
}
/// <summary>
/// Get the count of registered event subscriptions (for debugging).
/// </summary>
public int SubscriptionCount => _subscriptions.Count;
}
}

View File

@@ -1,2 +0,0 @@
fileFormatVersion: 2
guid: 63e107279fdbf1542a9d93d57e60285c