Files
WulaFallenEmpireRW/Source/WulaFallenEmpire/HarmonyPatches/ScenPart_PlayerPawnsArriveMethod_Patch.cs
Tourswen 6a5c5bd9aa 1
2025-11-30 14:39:22 +08:00

94 lines
3.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using HarmonyLib;
using RimWorld;
using System.Collections.Generic;
using System.Reflection;
using Verse;
namespace WulaFallenEmpire
{
[HarmonyPatch(typeof(ScenPart_PlayerPawnsArriveMethod))]
[HarmonyPatch("DoDropPods")]
public static class ScenPart_PlayerPawnsArriveMethod_ReflectionPatch
{
/// <summary>
/// 使用反射来直接调用正确的方法
/// </summary>
[HarmonyPrefix]
public static bool Prefix(ScenPart_PlayerPawnsArriveMethod __instance, Map map, List<Thing> startingItems)
{
try
{
// 获取私有字段 "method"
FieldInfo methodField = typeof(ScenPart_PlayerPawnsArriveMethod).GetField("method", BindingFlags.NonPublic | BindingFlags.Instance);
PlayerPawnsArriveMethod method = (PlayerPawnsArriveMethod)methodField.GetValue(__instance);
// 重新创建物品分组逻辑
List<List<Thing>> thingGroups = new List<List<Thing>>();
foreach (Pawn startingPawn in Find.GameInitData.startingAndOptionalPawns)
{
List<Thing> pawnGroup = new List<Thing>();
pawnGroup.Add(startingPawn);
thingGroups.Add(pawnGroup);
}
int itemIndex = 0;
foreach (Thing startingItem in startingItems)
{
if (startingItem.def.CanHaveFaction)
{
startingItem.SetFactionDirect(Faction.OfPlayer);
}
thingGroups[itemIndex].Add(startingItem);
itemIndex++;
if (itemIndex >= thingGroups.Count)
{
itemIndex = 0;
}
}
// 使用反射调用正确的 DropThingGroupsNear 方法11个参数版本
MethodInfo dropMethod = typeof(DropPodUtility).GetMethod("DropThingGroupsNear", new System.Type[]
{
typeof(IntVec3),
typeof(Map),
typeof(List<List<Thing>>),
typeof(int),
typeof(bool),
typeof(bool),
typeof(bool),
typeof(bool),
typeof(bool),
typeof(bool),
typeof(Faction)
});
if (dropMethod != null)
{
dropMethod.Invoke(null, new object[]
{
MapGenerator.PlayerStartSpot,
map,
thingGroups,
110,
Find.GameInitData.QuickStarted || method != PlayerPawnsArriveMethod.DropPods,
true, // leaveSlag
true, // canRoofPunch
true, // forbid
false, // allowFogged
false, // canTransfer
Faction.OfPlayer // faction
});
Log.Message("[WULA] Successfully called DropThingGroupsNear with faction parameter via reflection");
}
else
{
Log.Error("[WULA] Could not find 11-parameter DropThingGroupsNear method");
}
// 返回 false 来跳过原方法的执行
return false;
}
catch (System.Exception ex)
{
Log.Error($"[WULA] Error in DoDropPods prefix: {ex}");
// 如果出错,让原方法继续执行
return true;
}
}
}
}