Rework of base interactables and managed behaviors

This commit is contained in:
Michal Pikulski
2025-11-04 11:11:27 +01:00
parent 0dc3f3e803
commit c57e3aa7e0
62 changed files with 11193 additions and 1376 deletions

View File

@@ -0,0 +1,48 @@
namespace Core.Lifecycle
{
/// <summary>
/// Defines the different lifecycle phases that can be broadcast by the LifecycleManager.
/// All ManagedBehaviours participate in all lifecycle phases by default.
/// </summary>
public enum LifecyclePhase
{
/// <summary>
/// Called once per component after bootstrap completes.
/// Guaranteed to be called after all bootstrap resources are loaded.
/// For late-registered components, called immediately upon registration.
/// </summary>
ManagedAwake,
/// <summary>
/// Called before a scene is unloaded.
/// Only called for components in the scene being unloaded.
/// </summary>
SceneUnloading,
/// <summary>
/// Called after a scene has finished loading.
/// Only called for components in the scene being loaded.
/// </summary>
SceneReady,
/// <summary>
/// Called before scene unloads to save data via SaveLoadManager.
/// Integrates with existing SaveLoadManager save system.
/// </summary>
SaveRequested,
/// <summary>
/// Called after scene loads to restore data via SaveLoadManager.
/// Integrates with existing SaveLoadManager restore system.
/// </summary>
RestoreRequested,
/// <summary>
/// Called during OnDestroy before component is destroyed.
/// Use for custom cleanup logic.
/// Most cleanup is automatic (managed events, auto-registrations).
/// </summary>
ManagedDestroy
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 5f5f0f19f08240d4d9863b6be6a3cf03

View File

@@ -0,0 +1,420 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Core.Lifecycle
{
/// <summary>
/// Central orchestrator for ManagedBehaviour lifecycle events.
/// Singleton that broadcasts lifecycle events in priority-ordered manner.
/// </summary>
public class LifecycleManager : MonoBehaviour
{
#region Singleton
private static LifecycleManager _instance;
/// <summary>
/// Singleton instance of the LifecycleManager.
/// Created by CustomBoot.Initialise() before bootstrap begins.
/// </summary>
public static LifecycleManager Instance => _instance;
/// <summary>
/// Create LifecycleManager instance. Called by CustomBoot.Initialise() before bootstrap begins.
/// </summary>
public static void CreateInstance()
{
if (_instance != null)
{
Debug.LogWarning("[LifecycleManager] Instance already exists");
return;
}
var go = new GameObject("LifecycleManager");
_instance = go.AddComponent<LifecycleManager>();
DontDestroyOnLoad(go);
Debug.Log("[LifecycleManager] Instance created");
}
#endregion
#region Lifecycle Lists
private List<ManagedBehaviour> managedAwakeList = new List<ManagedBehaviour>();
private List<ManagedBehaviour> sceneUnloadingList = new List<ManagedBehaviour>();
private List<ManagedBehaviour> sceneReadyList = new List<ManagedBehaviour>();
private List<ManagedBehaviour> saveRequestedList = new List<ManagedBehaviour>();
private List<ManagedBehaviour> restoreRequestedList = new List<ManagedBehaviour>();
private List<ManagedBehaviour> destroyList = new List<ManagedBehaviour>();
#endregion
#region Tracking Dictionaries
private Dictionary<ManagedBehaviour, string> componentScenes = new Dictionary<ManagedBehaviour, string>();
#endregion
#region State Flags
private bool isBootComplete = false;
private string currentSceneReady = "";
[SerializeField] private bool enableDebugLogging = true;
#endregion
#region Unity Lifecycle
void Awake()
{
// Instance should already be set by CreateInstance() called from CustomBoot
// This Awake is backup in case LifecycleManager was manually added to a scene
if (_instance == null)
{
_instance = this;
DontDestroyOnLoad(gameObject);
LogDebug("LifecycleManager initialized via Awake (fallback)");
}
else if (_instance != this)
{
Debug.LogWarning("[LifecycleManager] Duplicate instance detected. Destroying.");
Destroy(gameObject);
}
}
void OnDestroy()
{
if (_instance == this)
{
_instance = null;
}
}
#endregion
#region Registration
/// <summary>
/// Register a ManagedBehaviour with the lifecycle system.
/// Called automatically from ManagedBehaviour.Awake().
/// All components participate in all lifecycle hooks.
/// </summary>
public void Register(ManagedBehaviour component)
{
if (component == null)
{
Debug.LogWarning("[LifecycleManager] Attempted to register null component");
return;
}
var sceneName = component.gameObject.scene.name;
// Track which scene this component belongs to
componentScenes[component] = sceneName;
// Register for ManagedAwake
if (isBootComplete)
{
// Boot already complete - call OnManagedAwake immediately
LogDebug($"Late registration: Calling OnManagedAwake immediately for {component.gameObject.name}");
try
{
component.InvokeManagedAwake();
HandleAutoRegistrations(component);
}
catch (Exception ex)
{
Debug.LogError($"[LifecycleManager] Error in OnManagedAwake for {component.gameObject.name}: {ex}");
}
}
else
{
// Boot not complete yet - add to list for broadcast
InsertSorted(managedAwakeList, component, component.ManagedAwakePriority);
}
// Register for all scene lifecycle hooks
InsertSorted(sceneUnloadingList, component, component.SceneUnloadingPriority);
InsertSorted(sceneReadyList, component, component.SceneReadyPriority);
InsertSorted(saveRequestedList, component, component.SavePriority);
InsertSorted(restoreRequestedList, component, component.RestorePriority);
InsertSorted(destroyList, component, component.DestroyPriority);
// If this scene is already ready, call OnSceneReady immediately
// Check both currentSceneReady AND if the Unity scene is actually loaded
// (during scene loading, components Awake before BroadcastSceneReady is called)
bool sceneIsReady = currentSceneReady == sceneName;
// Also check if this is happening during boot and the scene is the active scene
// This handles components that register during initial scene load
if (!sceneIsReady && isBootComplete && sceneName != "DontDestroyOnLoad")
{
var scene = UnityEngine.SceneManagement.SceneManager.GetSceneByName(sceneName);
sceneIsReady = scene.isLoaded;
}
if (sceneIsReady)
{
LogDebug($"Late registration: Calling OnSceneReady immediately for {component.gameObject.name}");
try
{
component.InvokeSceneReady();
}
catch (Exception ex)
{
Debug.LogError($"[LifecycleManager] Error in OnSceneReady for {component.gameObject.name}: {ex}");
}
}
LogDebug($"Registered {component.gameObject.name} (Scene: {sceneName})");
}
/// <summary>
/// Unregister a ManagedBehaviour from the lifecycle system.
/// Called automatically from ManagedBehaviour.OnDestroy().
/// </summary>
public void Unregister(ManagedBehaviour component)
{
if (component == null)
return;
managedAwakeList.Remove(component);
sceneUnloadingList.Remove(component);
sceneReadyList.Remove(component);
saveRequestedList.Remove(component);
restoreRequestedList.Remove(component);
destroyList.Remove(component);
componentScenes.Remove(component);
LogDebug($"Unregistered {component.gameObject.name}");
}
#endregion
#region Broadcast Methods
/// <summary>
/// Called by CustomBoot when boot completes.
/// Broadcasts ManagedAwake to all registered components.
/// </summary>
public void OnBootCompletionTriggered()
{
if (isBootComplete)
return;
LogDebug("=== Boot Completion Triggered ===");
BroadcastManagedAwake();
isBootComplete = true;
}
/// <summary>
/// Broadcast OnManagedAwake to all registered components (priority ordered).
/// </summary>
private void BroadcastManagedAwake()
{
LogDebug($"Broadcasting ManagedAwake to {managedAwakeList.Count} components");
foreach (var component in managedAwakeList)
{
if (component == null) continue;
try
{
component.InvokeManagedAwake();
HandleAutoRegistrations(component);
}
catch (Exception ex)
{
Debug.LogError($"[LifecycleManager] Error in OnManagedAwake for {component.gameObject.name}: {ex}");
}
}
// Clear the list - components already initialized
managedAwakeList.Clear();
}
/// <summary>
/// Broadcast OnSceneUnloading to components in the specified scene (reverse priority order).
/// </summary>
public void BroadcastSceneUnloading(string sceneName)
{
LogDebug($"Broadcasting SceneUnloading for scene: {sceneName}");
// Iterate backwards (high priority → low priority)
for (int i = sceneUnloadingList.Count - 1; i >= 0; i--)
{
var component = sceneUnloadingList[i];
if (component == null) continue;
if (componentScenes.TryGetValue(component, out string compScene) && compScene == sceneName)
{
try
{
component.InvokeSceneUnloading();
}
catch (Exception ex)
{
Debug.LogError($"[LifecycleManager] Error in OnSceneUnloading for {component.gameObject.name}: {ex}");
}
}
}
}
/// <summary>
/// Broadcast OnSaveRequested to components in the specified scene (reverse priority order).
/// </summary>
public void BroadcastSaveRequested(string sceneName)
{
LogDebug($"Broadcasting SaveRequested for scene: {sceneName}");
// Iterate backwards (high priority → low priority)
for (int i = saveRequestedList.Count - 1; i >= 0; i--)
{
var component = saveRequestedList[i];
if (component == null) continue;
if (componentScenes.TryGetValue(component, out string compScene) && compScene == sceneName)
{
try
{
component.InvokeSaveRequested();
}
catch (Exception ex)
{
Debug.LogError($"[LifecycleManager] Error in OnSaveRequested for {component.gameObject.name}: {ex}");
}
}
}
}
/// <summary>
/// Broadcast OnSceneReady to components in the specified scene (priority order).
/// </summary>
public void BroadcastSceneReady(string sceneName)
{
LogDebug($"Broadcasting SceneReady for scene: {sceneName}");
currentSceneReady = sceneName;
foreach (var component in sceneReadyList)
{
if (component == null) continue;
if (componentScenes.TryGetValue(component, out string compScene) && compScene == sceneName)
{
try
{
component.InvokeSceneReady();
}
catch (Exception ex)
{
Debug.LogError($"[LifecycleManager] Error in OnSceneReady for {component.gameObject.name}: {ex}");
}
}
}
}
/// <summary>
/// Broadcast OnRestoreRequested to components in the specified scene (priority order).
/// </summary>
public void BroadcastRestoreRequested(string sceneName)
{
LogDebug($"Broadcasting RestoreRequested for scene: {sceneName}");
foreach (var component in restoreRequestedList)
{
if (component == null) continue;
if (componentScenes.TryGetValue(component, out string compScene) && compScene == sceneName)
{
try
{
component.InvokeRestoreRequested();
}
catch (Exception ex)
{
Debug.LogError($"[LifecycleManager] Error in OnRestoreRequested for {component.gameObject.name}: {ex}");
}
}
}
}
#endregion
#region Auto-Registration
/// <summary>
/// Handle automatic registration with GameManager.
/// </summary>
private void HandleAutoRegistrations(ManagedBehaviour component)
{
// Auto-register IPausable
if (component.AutoRegisterPausable && component is AppleHills.Core.Interfaces.IPausable pausable)
{
if (GameManager.Instance != null)
{
GameManager.Instance.RegisterPausableComponent(pausable);
LogDebug($"Auto-registered IPausable: {component.gameObject.name}");
}
}
}
#endregion
#region Helper Methods
/// <summary>
/// Insert component into list maintaining sorted order by priority.
/// Uses binary search for efficient insertion.
/// </summary>
private void InsertSorted(List<ManagedBehaviour> list, ManagedBehaviour component, int priority)
{
// Simple linear insertion for now (can optimize with binary search later if needed)
int index = 0;
for (int i = 0; i < list.Count; i++)
{
int existingPriority = GetPriorityForList(list[i], list);
if (priority < existingPriority)
{
index = i;
break;
}
index = i + 1;
}
list.Insert(index, component);
}
/// <summary>
/// Get the priority value for a component based on which list it's in.
/// </summary>
private int GetPriorityForList(ManagedBehaviour component, List<ManagedBehaviour> list)
{
if (list == managedAwakeList) return component.ManagedAwakePriority;
if (list == sceneUnloadingList) return component.SceneUnloadingPriority;
if (list == sceneReadyList) return component.SceneReadyPriority;
if (list == saveRequestedList) return component.SavePriority;
if (list == restoreRequestedList) return component.RestorePriority;
if (list == destroyList) return component.DestroyPriority;
return 100;
}
/// <summary>
/// Log debug message if debug logging is enabled.
/// </summary>
private void LogDebug(string message)
{
if (enableDebugLogging)
{
Debug.Log($"[LifecycleManager] {message}");
}
}
#endregion
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: db6d4743867a3a44381d511cea39218d

View File

@@ -0,0 +1,224 @@
using System;
using UnityEngine;
namespace Core.Lifecycle
{
/// <summary>
/// Base class for all managed behaviours with deterministic lifecycle hooks.
/// Automatically registers with LifecycleManager and provides ordered lifecycle callbacks.
/// </summary>
public abstract class ManagedBehaviour : MonoBehaviour
{
#region Priority Properties
/// <summary>
/// Priority for OnManagedAwake (lower values execute first).
/// Default: 100
/// </summary>
public virtual int ManagedAwakePriority => 100;
/// <summary>
/// Priority for OnSceneUnloading (executed in reverse: higher values execute first).
/// Default: 100
/// </summary>
public virtual int SceneUnloadingPriority => 100;
/// <summary>
/// Priority for OnSceneReady (lower values execute first).
/// Default: 100
/// </summary>
public virtual int SceneReadyPriority => 100;
/// <summary>
/// Priority for OnSaveRequested (executed in reverse: higher values execute first).
/// Default: 100
/// </summary>
public virtual int SavePriority => 100;
/// <summary>
/// Priority for OnRestoreRequested (lower values execute first).
/// Default: 100
/// </summary>
public virtual int RestorePriority => 100;
/// <summary>
/// Priority for OnManagedDestroy (executed in reverse: higher values execute first).
/// Default: 100
/// </summary>
public virtual int DestroyPriority => 100;
#endregion
#region Configuration Properties
/// <summary>
/// If true and component implements IPausable, automatically registers with GameManager.
/// Default: false
/// </summary>
public virtual bool AutoRegisterPausable => false;
#endregion
#region Public Accessors (for LifecycleManager)
// Public wrappers to invoke protected lifecycle methods
public void InvokeManagedAwake() => OnManagedAwake();
public void InvokeSceneUnloading() => OnSceneUnloading();
public void InvokeSceneReady() => OnSceneReady();
public void InvokeSaveRequested() => OnSaveRequested();
public void InvokeRestoreRequested() => OnRestoreRequested();
public void InvokeManagedDestroy() => OnManagedDestroy();
#endregion
#region Private Fields
private ManagedEventManager _eventManager;
private bool _isRegistered;
#endregion
#region Unity Lifecycle
/// <summary>
/// Unity Awake - automatically registers with LifecycleManager.
/// IMPORTANT: Derived classes that override Awake MUST call base.Awake()
/// </summary>
protected virtual void Awake()
{
_eventManager = new ManagedEventManager();
if (LifecycleManager.Instance != null)
{
LifecycleManager.Instance.Register(this);
_isRegistered = true;
}
else
{
Debug.LogWarning($"[ManagedBehaviour] LifecycleManager not found for {gameObject.name}. Component will not receive lifecycle callbacks.");
}
}
/// <summary>
/// Unity OnDestroy - automatically unregisters and cleans up.
/// IMPORTANT: Derived classes that override OnDestroy MUST call base.OnDestroy()
/// </summary>
protected virtual void OnDestroy()
{
if (!_isRegistered)
return;
// Unregister from LifecycleManager
if (LifecycleManager.Instance != null)
{
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)
{
GameManager.Instance?.UnregisterPausableComponent(pausable);
}
_isRegistered = false;
}
#endregion
#region Managed Lifecycle Hooks
/// <summary>
/// Called once per component after bootstrap completes.
/// GUARANTEE: Bootstrap resources are available, all managers are initialized.
/// For boot-time components: Called during LifecycleManager.BroadcastManagedAwake (priority ordered).
/// For late-registered components: Called immediately upon registration (bootstrap already complete).
/// Replaces the old Awake + InitializePostBoot pattern.
/// </summary>
protected virtual void OnManagedAwake()
{
// Override in derived classes
}
/// <summary>
/// Called before the scene this component belongs to is unloaded.
/// Called in REVERSE priority order (higher values execute first).
/// Use for scene-specific cleanup.
/// </summary>
protected virtual void OnSceneUnloading()
{
// Override in derived classes
}
/// <summary>
/// Called after the scene this component belongs to has finished loading.
/// Called in priority order (lower values execute first).
/// Use for scene-specific initialization.
/// </summary>
protected virtual void OnSceneReady()
{
// Override in derived classes
}
/// <summary>
/// Called before scene unloads to save data via SaveLoadManager.
/// Called in REVERSE priority order (higher values execute first).
/// Integrates with existing SaveLoadManager save system.
/// Return serialized state string (e.g., JsonUtility.ToJson(myData)).
/// </summary>
protected virtual void OnSaveRequested()
{
// Override in derived classes
}
/// <summary>
/// Called after scene loads to restore data via SaveLoadManager.
/// Called in priority order (lower values execute first).
/// Integrates with existing SaveLoadManager restore system.
/// Receives serialized state string to restore from.
/// </summary>
protected virtual void OnRestoreRequested()
{
// Override in derived classes
}
/// <summary>
/// Called during OnDestroy before component is destroyed.
/// Called in REVERSE priority order (higher values execute first).
/// NOTE: Most cleanup is automatic (managed events, auto-registrations).
/// Only override if you need custom cleanup logic.
/// </summary>
protected virtual void OnManagedDestroy()
{
// Override in derived classes
}
#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

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: af776ef1493d6e543aa3cbe2601f4ef2

View File

@@ -0,0 +1,102 @@
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

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