using HarmonyLib; namespace IdleSpectator; /// Plot lifecycle patches from mutation discovery. [HarmonyPatch] public static class PlotEventPatches { [HarmonyPatch(typeof(PlotManager), nameof(PlotManager.newPlot))] [HarmonyPostfix] public static void PostfixNewPlot(Plot __result) { if (__result == null) { return; } PlotInterestFeed.EmitFromPlot(__result, "new"); } [HarmonyPatch(typeof(PlotManager), nameof(PlotManager.cancelPlot))] [HarmonyPostfix] public static void PostfixCancelPlot(Plot pPlot) { if (pPlot == null) { return; } PlotInterestFeed.EmitFromPlot(pPlot, "cancel"); } [HarmonyPatch(typeof(Actor), nameof(Actor.setPlot))] [HarmonyPostfix] public static void PostfixSetPlot(Actor __instance, Plot pObject) { if (__instance == null || !__instance.isAlive() || pObject == null) { return; } PlotInterestFeed.EmitFromPlot(pObject, "join"); } [HarmonyPatch(typeof(Actor), nameof(Actor.leavePlot))] [HarmonyPrefix] public static void PrefixLeavePlot(Actor __instance, out string __state) { __state = ""; try { object plot = __instance?.GetType().GetField("plot")?.GetValue(__instance) ?? __instance?.GetType().GetProperty("plot")?.GetValue(__instance, null); if (plot != null) { __state = ReadPlotAssetId(plot); } } catch { __state = ""; } } [HarmonyPatch(typeof(Actor), nameof(Actor.leavePlot))] [HarmonyPostfix] public static void PostfixLeavePlot(Actor __instance, string __state) { if (__instance == null || !__instance.isAlive() || string.IsNullOrEmpty(__state)) { return; } PlotInterestFeed.Emit(__state, __instance, "leave"); } private static string ReadPlotAssetId(object plot) { try { object asset = plot.GetType().GetField("asset")?.GetValue(plot) ?? plot.GetType().GetProperty("asset")?.GetValue(plot, null) ?? plot.GetType().GetMethod("getAsset")?.Invoke(plot, null) ?? plot.GetType().GetField("plot_asset")?.GetValue(plot); if (asset != null) { object id = asset.GetType().GetField("id")?.GetValue(asset) ?? asset.GetType().GetProperty("id")?.GetValue(asset, null); if (id != null) { return id.ToString(); } } } catch { // ignore } return ""; } }