using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using HarmonyLib;
using RimWorld;
using Verse;
namespace BlaxZones
{
///
/// Package Caravan. A caller-scoped transpiler on Pawn_TraderTracker.GiveSoldThingToPlayer replaces its
/// one GenPlace.TryPlaceThing call (Pawn_TraderTracker.cs:194) with DeliveryRouter.PlaceFromCaravan, which
/// has the same signature. Hospitality guests sell through Pawn.GiveSoldThingToPlayer, which calls this
/// method (Verse/Pawn.cs:4711-4714), so their goods route too. GenPlace itself is never patched.
///
[HarmonyPatch(typeof(Pawn_TraderTracker), nameof(Pawn_TraderTracker.GiveSoldThingToPlayer))]
public static class Patch_Pawn_TraderTracker_GiveSoldThingToPlayer
{
private static readonly Type[] PlaceArgs =
{
typeof(Thing), typeof(IntVec3), typeof(Map), typeof(ThingPlaceMode), typeof(Action), typeof(Predicate),
typeof(Rot4?), typeof(int),
};
public static readonly MethodInfo TryPlaceThing = AccessTools.Method(typeof(GenPlace), nameof(GenPlace.TryPlaceThing), PlaceArgs);
public static readonly MethodInfo PlaceFromCaravan = AccessTools.Method(typeof(DeliveryRouter), nameof(DeliveryRouter.PlaceFromCaravan), PlaceArgs);
/// Calls of TryPlaceThing the last transpiler run found. The test checks it is exactly 1.
public static int LastCallCount = -1;
///
/// Exactly one TryPlaceThing call: replace it. Any other count: log an error ("unexpected call
/// count") and return the instructions unchanged, so caravan goods land as in vanilla.
///
public static IEnumerable Transpiler(IEnumerable instructions)
{
List list = instructions.ToList();
try
{
if (TryPlaceThing == null || PlaceFromCaravan == null)
{
LastCallCount = -1;
Log.Error("[Blax Zones] Caravan purchase routing is off: GenPlace.TryPlaceThing or DeliveryRouter.PlaceFromCaravan not found. Caravan goods land next to the pack animals.");
return list;
}
int count = 0;
int at = -1;
for (int i = 0; i < list.Count; i++)
{
if (list[i].Calls(TryPlaceThing))
{
count++;
at = i;
}
}
LastCallCount = count;
if (count != 1)
{
Log.Error("[Blax Zones] Caravan purchase routing is off: unexpected call count " + count + " of GenPlace.TryPlaceThing in Pawn_TraderTracker.GiveSoldThingToPlayer, expected 1. Caravan goods land next to the pack animals.");
return list;
}
// Same signature, static: only the target changes. Labels and exception blocks stay on the instruction.
list[at].opcode = OpCodes.Call;
list[at].operand = PlaceFromCaravan;
return list;
}
catch (Exception e)
{
Log.Error("[Blax Zones] Caravan purchase routing is off: the Pawn_TraderTracker.GiveSoldThingToPlayer transpiler failed: " + e);
return instructions;
}
}
}
///
/// Package Caravan. Opens a CaravanDeliveries batch for one trade (TradeDeal.TryExecute, TradeDeal.cs:150),
/// so every stack bought in it makes one message. Prefix, postfix and finalizer only, so the trade mods
/// that also touch TryExecute compose. The finalizer sends what was collected if the trade threw and
/// restores the previous batch.
///
[HarmonyPatch(typeof(TradeDeal), nameof(TradeDeal.TryExecute))]
public static class Patch_TradeDeal_TryExecute
{
public static void Prefix(out CaravanDeliveries.Batch __state)
{
__state = null;
try
{
__state = CaravanDeliveries.Begin();
}
catch (Exception e)
{
ZonesLog.Once("Caravan trade message", e);
}
}
public static void Postfix(CaravanDeliveries.Batch __state)
{
try
{
CaravanDeliveries.End(__state);
}
catch (Exception e)
{
ZonesLog.Once("Caravan trade message", e);
}
}
/// Returns void, so Harmony rethrows a trade exception with its stack trace intact.
public static void Finalizer(CaravanDeliveries.Batch __state)
{
try
{
CaravanDeliveries.End(__state);
}
catch (Exception e)
{
ZonesLog.Once("Caravan trade message", e);
}
}
}
}