mirror of
https://github.com/soarqin/DSP_Mods.git
synced 2026-08-05 19:00:12 +08:00
refactor(UXAssist): safe GameEvent wrapper and XML docs
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using BepInEx.Logging;
|
||||
|
||||
namespace UXAssist.Common;
|
||||
|
||||
/// <summary>
|
||||
/// Provides safe invocation helpers for game events.
|
||||
/// </summary>
|
||||
public static class GameEvent
|
||||
{
|
||||
/// <summary>
|
||||
/// Invokes each handler in the action's invocation list individually,
|
||||
/// logging a warning if any handler throws an exception.
|
||||
/// </summary>
|
||||
/// <param name="action">The action to invoke.</param>
|
||||
/// <param name="logger">The logger to use for warnings.</param>
|
||||
/// <param name="name">The name of the event for diagnostic messages.</param>
|
||||
public static void InvokeSafe(this Action action, ManualLogSource logger, string name)
|
||||
{
|
||||
if (action == null) return;
|
||||
foreach (var d in action.GetInvocationList())
|
||||
{
|
||||
try { d.DynamicInvoke(); }
|
||||
catch (Exception ex) { logger?.LogWarning($"GameEvent '{name}' handler failed: {ex}"); }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,49 +1,58 @@
|
||||
using System;
|
||||
using System;
|
||||
using HarmonyLib;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UXAssist.Common;
|
||||
|
||||
/// <summary>
|
||||
/// Provides game lifecycle events for UXAssist and dependent mods.
|
||||
/// </summary>
|
||||
public class GameLogic : PatchImpl<GameLogic>
|
||||
{
|
||||
/// <summary>
|
||||
/// Raised after game data has finished loading.
|
||||
/// </summary>
|
||||
public static Action OnDataLoaded;
|
||||
|
||||
/// <summary>
|
||||
/// Raised when a game session begins.
|
||||
/// </summary>
|
||||
public static Action OnGameBegin;
|
||||
|
||||
/// <summary>
|
||||
/// Raised when a game session ends.
|
||||
/// </summary>
|
||||
public static Action OnGameEnd;
|
||||
|
||||
private static void InvokeSafe(Action action)
|
||||
{
|
||||
if (action == null) return;
|
||||
foreach (var handler in action.GetInvocationList())
|
||||
{
|
||||
try
|
||||
{
|
||||
((Action)handler)();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Harmony postfix for <see cref="VFPreload.InvokeOnLoadWorkEnded"/>.
|
||||
/// Raises <see cref="OnDataLoaded"/>.
|
||||
/// </summary>
|
||||
[HarmonyPostfix]
|
||||
[HarmonyPatch(typeof(VFPreload), nameof(VFPreload.InvokeOnLoadWorkEnded))]
|
||||
public static void VFPreload_InvokeOnLoadWorkEnded_Postfix()
|
||||
{
|
||||
InvokeSafe(OnDataLoaded);
|
||||
OnDataLoaded.InvokeSafe(UXAssist.Logger, nameof(OnDataLoaded));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Harmony postfix for <see cref="GameMain.Begin"/>.
|
||||
/// Raises <see cref="OnGameBegin"/>.
|
||||
/// </summary>
|
||||
[HarmonyPostfix, HarmonyPriority(Priority.First)]
|
||||
[HarmonyPatch(typeof(GameMain), nameof(GameMain.Begin))]
|
||||
public static void GameMain_Begin_Postfix()
|
||||
{
|
||||
InvokeSafe(OnGameBegin);
|
||||
OnGameBegin.InvokeSafe(UXAssist.Logger, nameof(OnGameBegin));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Harmony postfix for <see cref="GameMain.End"/>.
|
||||
/// Raises <see cref="OnGameEnd"/>.
|
||||
/// </summary>
|
||||
[HarmonyPostfix, HarmonyPriority(Priority.Last)]
|
||||
[HarmonyPatch(typeof(GameMain), nameof(GameMain.End))]
|
||||
public static void GameMain_End_Postfix()
|
||||
{
|
||||
InvokeSafe(OnGameEnd);
|
||||
OnGameEnd.InvokeSafe(UXAssist.Logger, nameof(OnGameEnd));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user