Files
ArachnaeSwarm/Source/ArachnaeSwarm/HarmonyPatches/Patch_PlantPollutionNullCheck.cs

68 lines
2.6 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 Verse;
namespace ArachnaeSwarm
{
/// <summary>
/// 修复原版 Plant 类在被 Pawn 携带/吃掉时的空引用异常。
/// 当植物被 Pawn 吃掉时,它会被放入 Pawn 的 ThingOwner 中,
/// 此时 Map 为 null但 Plant.TickLong() 仍然会被调用,
/// 导致多处访问 Map 相关方法时抛出 NullReferenceException。
/// </summary>
[HarmonyPatch]
public static class Patch_PlantPollutionNullCheck
{
/// <summary>
/// Prefix for Plant.TickLong
/// 如果植物不在地图上,直接跳过整个 TickLong 方法
/// 这是最根本的修复,可以防止所有后续的空引用异常
/// </summary>
[HarmonyPatch(typeof(Plant), nameof(Plant.TickLong))]
[HarmonyPrefix]
public static bool TickLong_Prefix(Plant __instance)
{
// 如果植物未生成或不在地图上,跳过整个 TickLong
if (!__instance.Spawned || __instance.Map == null)
{
return false; // 跳过原方法
}
return true; // 继续执行原方法
}
/// <summary>
/// Prefix for Plant.DyingFromPollution getter
/// 如果植物不在地图上,直接返回 false跳过原方法
/// </summary>
[HarmonyPatch(typeof(Plant), nameof(Plant.DyingFromPollution), MethodType.Getter)]
[HarmonyPrefix]
public static bool DyingFromPollution_Prefix(Plant __instance, ref bool __result)
{
// 如果植物未生成或不在地图上,返回 false 并跳过原方法
if (!__instance.Spawned || __instance.Map == null)
{
__result = false;
return false; // 跳过原方法
}
return true; // 继续执行原方法
}
/// <summary>
/// Prefix for Plant.DyingFromNoPollution getter
/// 如果植物不在地图上,直接返回 false跳过原方法
/// </summary>
[HarmonyPatch(typeof(Plant), nameof(Plant.DyingFromNoPollution), MethodType.Getter)]
[HarmonyPrefix]
public static bool DyingFromNoPollution_Prefix(Plant __instance, ref bool __result)
{
// 如果植物未生成或不在地图上,返回 false 并跳过原方法
if (!__instance.Spawned || __instance.Map == null)
{
__result = false;
return false; // 跳过原方法
}
return true; // 继续执行原方法
}
}
}