Files
AppleHillsProduction/Assets/Scripts/Bootstrap/CustomBoot.cs
tschesky 0aa2270e1a Lifecycle System Refactor & Logging Centralization (#56)
## ManagedBehaviour System Refactor

- **Sealed `Awake()`** to prevent override mistakes that break singleton registration
- **Added `OnManagedAwake()`** for early initialization (fires during registration)
- **Renamed lifecycle hook:** `OnManagedAwake()` → `OnManagedStart()` (fires after boot, mirrors Unity's Awake→Start)
- **40 files migrated** to new pattern (2 core, 38 components)
- Eliminated all fragile `private new void Awake()` patterns
- Zero breaking changes - backward compatible

## Centralized Logging System

- **Automatic tagging** via `CallerMemberName` and `CallerFilePath` - logs auto-tagged as `[ClassName][MethodName] message`
- **Unified API:** Single `Logging.Debug/Info/Warning/Error()` replaces custom `LogDebugMessage()` implementations
- **~90 logging call sites** migrated across 10 files
- **10 redundant helper methods** removed
- All logs broadcast via `Logging.OnLogEntryAdded` event for real-time monitoring

## Custom Log Console (Editor Window)

- **Persistent filter popups** for multi-selection (classes, methods, log levels) - windows stay open during selection
- **Search** across class names, methods, and message content
- **Time range filter** with MinMaxSlider
- **Export** filtered logs to timestamped `.txt` files
- **Right-click context menu** for quick filtering and copy actions
- **Visual improvements:** White text, alternating row backgrounds, color-coded log levels
- **Multiple instances** supported for simultaneous system monitoring
- Open via `AppleHills > Custom Log Console`

Co-authored-by: Michal Pikulski <michal@foolhardyhorizons.com>
Co-authored-by: Michal Pikulski <michal.a.pikulski@gmail.com>
Reviewed-on: #56
2025-11-11 08:48:29 +00:00

243 lines
8.0 KiB
C#

using System;
using System.Threading.Tasks;
using Core;
using Core.Lifecycle;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
namespace Bootstrap
{
/// <summary>
/// Entrypoint for the Custom Boot initialisation
/// </summary>
public static class CustomBoot
{
/// <summary>
/// Current initialisation status
/// </summary>
public static bool Initialised { get; private set; }
/// <summary>
/// Event triggered when boot progress changes
/// </summary>
public static event Action<float> OnBootProgressChanged;
/// <summary>
/// Event triggered when boot process completes
/// </summary>
public static event Action OnBootCompleted;
/// <summary>
/// Current progress of the boot process (0-1)
/// </summary>
public static float CurrentProgress { get; private set; }
/// <summary>
/// Called as soon as the game begins
/// </summary>
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSplashScreen)]
private static void Initialise()
{
// Create LifecycleManager FIRST - before any bootstrap logic
// This ensures it exists when boot completes
LifecycleManager.CreateInstance();
//We should always clean up after Addressables, so let's take care of that immediately
Application.quitting += ApplicationOnUnloading;
PerformInitialisation();
}
/// <summary>
/// Initialise the bootstrapper
/// </summary>
public static void PerformInitialisation()
{
//Reset progress
CurrentProgress = 0f;
//In editor, perform initialisation synchronously
if (Application.isEditor)
{
InitialiseBootSettingsSync();
}
else
{
//In builds, just run things asynchronously, since we can add any checks we need early on
_ = InitialiseBootSettings();
}
}
/// <summary>
/// Called as the game is quitting, allowing for cleanup
/// </summary>
private static void ApplicationOnUnloading()
{
Application.quitting -= ApplicationOnUnloading;
PerformDeInitialisation();
}
/// <summary>
/// De-Initialise the bootstrapper
/// </summary>
public static void PerformDeInitialisation()
{
Cleanup(runtimeBootSettingsHandle);
Cleanup(editorBootSettingsHandle);
Initialised = false;
}
/// <summary>
/// Initialise the boot settings asynchronously
/// </summary>
private static async Task InitialiseBootSettings()
{
await LoadCustomBootSettings();
Initialised = true;
CurrentProgress = 1f;
OnBootProgressChanged?.Invoke(1f);
OnBootCompleted?.Invoke();
// Notify the LifecycleManager that boot is complete
if (Application.isPlaying)
{
Logging.Debug("Calling LifecycleManager.OnBootCompletionTriggered()");
if (LifecycleManager.Instance != null)
{
LifecycleManager.Instance.OnBootCompletionTriggered();
}
}
}
/// <summary>
/// Initialise the boot settings synchronously
/// </summary>
private static void InitialiseBootSettingsSync()
{
LoadCustomBootSettingsSync();
Initialised = true;
CurrentProgress = 1f;
OnBootProgressChanged?.Invoke(1f);
OnBootCompleted?.Invoke();
// Notify the LifecycleManager that boot is complete
if (Application.isPlaying)
{
Logging.Debug("Calling LifecycleManager.OnBootCompletionTriggered()");
if (LifecycleManager.Instance != null)
{
LifecycleManager.Instance.OnBootCompletionTriggered();
}
}
}
/// <summary>
/// Clean up the boot settings
/// </summary>
/// <param name="handle"></param>
private static void Cleanup(AsyncOperationHandle<CustomBootSettings> handle)
{
if (handle.IsValid())
{
handle.Result.Cleanup();
Addressables.Release(handle);
}
}
/// <summary>
/// Async handle for the runtime custom boot settings scriptable object
/// </summary>
private static AsyncOperationHandle<CustomBootSettings> runtimeBootSettingsHandle;
/// <summary>
/// Async handle for the editor custom boot settings object
/// </summary>
private static AsyncOperationHandle<CustomBootSettings> editorBootSettingsHandle;
/// <summary>
/// Runtime addressable key
/// </summary>
private static string RuntimeAsset = $"{nameof(CustomBootSettings)}_Runtime";
/// <summary>
/// Editor addressable key
/// </summary>
private static string EditorAsset = $"{nameof(CustomBootSettings)}_Editor";
/// <summary>
/// Load the custom boot settings asynchronously and run the initialisation method
/// </summary>
private static async Task LoadCustomBootSettings()
{
if (Application.isEditor)
{
editorBootSettingsHandle = await InitialiseBootSettingsAsset(EditorAsset);
}
runtimeBootSettingsHandle = await InitialiseBootSettingsAsset(RuntimeAsset);
}
/// <summary>
/// Load the custom boot settings synchronously and run the initialisation method
/// </summary>
private static void LoadCustomBootSettingsSync()
{
if (Application.isEditor)
{
editorBootSettingsHandle = InitialiseBootSettingsAssetSync(EditorAsset);
}
runtimeBootSettingsHandle = InitialiseBootSettingsAssetSync(RuntimeAsset);
}
/// <summary>
/// Initialise the boot settings asset with the given key
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
private static async Task<AsyncOperationHandle<CustomBootSettings>> InitialiseBootSettingsAsset(string key)
{
var handle = Addressables.LoadAssetAsync<CustomBootSettings>(key);
await handle.Task;
switch (handle.Status)
{
case AsyncOperationStatus.Failed:
Debug.LogError(handle.OperationException);
break;
case AsyncOperationStatus.Succeeded:
await handle.Result.Initialise();
break;
}
return handle;
}
/// <summary>
/// Initialise the boot settings asset with the given key synchronously
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
private static AsyncOperationHandle<CustomBootSettings> InitialiseBootSettingsAssetSync(string key)
{
var handle = Addressables.LoadAssetAsync<CustomBootSettings>(key);
var result = handle.WaitForCompletion();
result.InitialiseSync();
return handle;
}
/// <summary>
/// Updates the current progress value and triggers the progress event
/// </summary>
/// <param name="progress">Progress value between 0-1</param>
internal static void UpdateProgress(float progress)
{
CurrentProgress = Mathf.Clamp01(progress);
OnBootProgressChanged?.Invoke(CurrentProgress);
Logging.Debug($"Progress: {CurrentProgress:P0}");
}
}
}