128 lines
3.6 KiB
C#
128 lines
3.6 KiB
C#
using HarmonyLib;
|
|
|
|
namespace IdleSpectator;
|
|
|
|
/// <summary>Plot lifecycle patches from mutation discovery.</summary>
|
|
[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");
|
|
}
|
|
|
|
[HarmonyPatch(typeof(Plot), nameof(Plot.finishPlot))]
|
|
[HarmonyPostfix]
|
|
public static void PostfixFinishPlot(Plot __instance, PlotState pState, Actor pActor)
|
|
{
|
|
if (__instance == null || pState != PlotState.Finished)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// Prefer finishing actor; fall back to plot author inside EmitFromPlot.
|
|
if (pActor != null && pActor.isAlive())
|
|
{
|
|
string assetId = ReadPlotAssetId(__instance);
|
|
if (string.IsNullOrEmpty(assetId))
|
|
{
|
|
PlotInterestFeed.EmitFromPlot(__instance, "complete");
|
|
return;
|
|
}
|
|
|
|
PlotInterestFeed.Emit(assetId, pActor, "complete");
|
|
return;
|
|
}
|
|
|
|
PlotInterestFeed.EmitFromPlot(__instance, "complete");
|
|
}
|
|
|
|
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 "";
|
|
}
|
|
}
|