131 lines
5.6 KiB
C#
131 lines
5.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using HarmonyLib;
|
|
using RimWorld;
|
|
using Verse;
|
|
|
|
namespace BlaxZones
|
|
{
|
|
/// <summary>
|
|
/// Package Rewards. Sets the quest reward context for QuestPart_DropPods.Notify_QuestSignalReceived
|
|
/// (QuestPart_DropPods.cs:124), only when useTradeDropSpot is true and dropSpot is invalid (:165,
|
|
/// 298-303): exactly the drops vanilla sends to the trade spot, the ones Reward_Items creates
|
|
/// (Reward_Items.cs:180). Any other part runs with no context, also when an outer one is set. The
|
|
/// finalizer restores the previous value, even on an exception.
|
|
///
|
|
/// Every finalizer here returns void: Harmony then rethrows an exception from the quest part with its
|
|
/// stack trace intact. A finalizer that returns Exception makes Harmony throw it anew, the trace then
|
|
/// starts at the patched method, and the log would blame Blax Zones for another mod's bug.
|
|
/// </summary>
|
|
[HarmonyPatch(typeof(QuestPart_DropPods), nameof(QuestPart_DropPods.Notify_QuestSignalReceived))]
|
|
public static class Patch_QuestPart_DropPods_Notify_QuestSignalReceived
|
|
{
|
|
public static void Prefix(QuestPart_DropPods __instance, out DeliveryContext __state)
|
|
{
|
|
__state = DeliveryContext.Current;
|
|
DeliveryContext next = null;
|
|
try
|
|
{
|
|
if (__instance != null && __instance.useTradeDropSpot && !__instance.dropSpot.IsValid)
|
|
next = DeliveryContext.QuestRewards;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
ZonesLog.Once("Quest reward context", e);
|
|
}
|
|
DeliveryContext.Current = next;
|
|
}
|
|
|
|
public static void Finalizer(DeliveryContext __state)
|
|
{
|
|
DeliveryContext.Current = __state;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Package Rewards. QuestPart_GiveNearPawn.Notify_QuestSignalReceived (QuestPart_GiveNearPawn.cs:107)
|
|
/// always drops at the trade spot (:187-188), so it always runs in the quest reward context. Gifts to
|
|
/// a pawn in a world caravan go into the caravan's inventory and never reach a drop.
|
|
/// </summary>
|
|
[HarmonyPatch(typeof(QuestPart_GiveNearPawn), nameof(QuestPart_GiveNearPawn.Notify_QuestSignalReceived))]
|
|
public static class Patch_QuestPart_GiveNearPawn_Notify_QuestSignalReceived
|
|
{
|
|
public static void Prefix(out DeliveryContext __state)
|
|
{
|
|
__state = DeliveryContext.Current;
|
|
DeliveryContext.Current = DeliveryContext.QuestRewards;
|
|
}
|
|
|
|
public static void Finalizer(DeliveryContext __state)
|
|
{
|
|
DeliveryContext.Current = __state;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Package Rewards. Signals are delivered inside SendSignal (SignalManager.cs:38-60), so a quest part
|
|
/// chained through outSignalResult (QuestPart_DropPods.cs:239-251, QuestPart_GiveNearPawn.cs:224-238)
|
|
/// would run inside the sender's context. The prefix saves and clears it; the finalizer restores it.
|
|
/// </summary>
|
|
[HarmonyPatch(typeof(SignalManager), nameof(SignalManager.SendSignal))]
|
|
public static class Patch_SignalManager_SendSignal
|
|
{
|
|
public static void Prefix(out DeliveryContext __state)
|
|
{
|
|
__state = DeliveryContext.Current;
|
|
DeliveryContext.Current = null;
|
|
}
|
|
|
|
public static void Finalizer(DeliveryContext __state)
|
|
{
|
|
DeliveryContext.Current = __state;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Package Rewards. DropPodUtility.DropThingGroupsNear (DropPodUtility.cs:40) inside a DeliveryContext:
|
|
/// plans every group first (DeliveryRouter.PlanGroups), drops the qualifying ones as their own pods in
|
|
/// delivery zones (DeliveryRouter.DropPlannedGroups), then assigns a new list holding the rest, so
|
|
/// vanilla drops only those and the caller's list is never changed. DropThingsNear passes its shared
|
|
/// static list here and clears it afterwards (DropPodUtility.cs:10, 27-38); Zones never calls
|
|
/// DropThingsNear. Without a context (raids, ransom, lent colonists, edge and center drops) nothing
|
|
/// changes. A failed plan leaves the caller's list to vanilla.
|
|
/// </summary>
|
|
[HarmonyPatch(typeof(DropPodUtility), nameof(DropPodUtility.DropThingGroupsNear))]
|
|
public static class Patch_DropPodUtility_DropThingGroupsNear
|
|
{
|
|
public static void Prefix(Map map, ref List<List<Thing>> thingsGroups, int openDelay, bool instaDrop, bool leaveSlag, bool forbid, Faction faction)
|
|
{
|
|
DeliveryContext context = DeliveryContext.Current;
|
|
if (context == null)
|
|
return;
|
|
List<DeliveryRouter.GroupPlan> plans;
|
|
try
|
|
{
|
|
if (!context.Enabled)
|
|
return;
|
|
plans = DeliveryRouter.PlanGroups(map, thingsGroups, context.Source);
|
|
if (plans.Count == 0)
|
|
return;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
ZonesLog.Once(context.LogKey, e);
|
|
return;
|
|
}
|
|
try
|
|
{
|
|
List<List<Thing>> remaining = DeliveryRouter.DropPlannedGroups(map, thingsGroups, plans, openDelay, instaDrop, leaveSlag,
|
|
forbid, faction, context.LogKey);
|
|
if (remaining != null)
|
|
thingsGroups = remaining;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
// DropPlannedGroups catches per group; this only guards its list handling.
|
|
ZonesLog.Once(context.LogKey, e);
|
|
}
|
|
}
|
|
}
|
|
}
|