Files
ArachnaeSwarm/Source/ArachnaeSwarm/AnimalWorkSystemPatcher.cs
2025-09-02 21:43:35 +08:00

75 lines
2.6 KiB
C#

using System.Collections.Generic;
using System.Reflection;
using HarmonyLib;
using RimWorld;
using Verse;
namespace ArachnaeSwarm
{
[StaticConstructorOnStartup]
public static class AnimalWorkSystemPatcher
{
static AnimalWorkSystemPatcher()
{
var harmony = new Harmony("com.yourname.animalworksystem");
harmony.PatchAll();
}
}
[HarmonyPatch(typeof(Pawn_WorkSettings), "EnableAndInitialize")]
public static class Patch_Pawn_WorkSettings_EnableAndInitialize
{
public static void Postfix(Pawn_WorkSettings __instance, Pawn ___pawn)
{
// 检查是否是我们想要启用工作系统的动物,并且它不是机械体
// 因为原版的 EnableAndInitialize 已经处理了机械体的工作设置
if (___pawn.Faction != null && ___pawn.Faction.IsPlayer &&
!___pawn.RaceProps.IsMechanoid &&
ShouldEnableWorkSystem(___pawn))
{
// 获取 CompProperties_WorkForNonMechs
CompProperties_WorkForNonMechs compProps = null;
if (___pawn.def.comps != null)
{
foreach (var comp in ___pawn.def.comps)
{
if (comp is CompProperties_WorkForNonMechs props)
{
compProps = props;
break;
}
}
}
if (compProps != null && compProps.workTypes != null)
{
// 设置 CompProperties_WorkForNonMechs 中定义的工作类型优先级
foreach (var workType in compProps.workTypes)
{
if (!__instance.WorkIsActive(workType) && !___pawn.WorkTypeIsDisabled(workType))
{
__instance.SetPriority(workType, 3); // 默认优先级
}
}
}
}
}
private static bool ShouldEnableWorkSystem(Pawn pawn)
{
// 检查 ThingDef 中是否有 CompProperties_WorkForNonMechs 配置
if (pawn.def.comps != null)
{
foreach (var compProperties in pawn.def.comps)
{
if (compProperties is CompProperties_WorkForNonMechs)
{
return true;
}
}
}
return false;
}
}
}