using System.Collections.Generic;
using System.Reflection;
using AIImages.Components;
using HarmonyLib;
using Verse;
namespace AIImages.Patches
{
///
/// Патч для добавления PawnPortraitComp ко всем пешкам
///
[HarmonyPatch(typeof(ThingWithComps), nameof(ThingWithComps.InitializeComps))]
public static class PawnPortraitCompPatch
{
private static FieldInfo allCompsField = AccessTools.Field(typeof(ThingWithComps), "comps");
[HarmonyPostfix]
public static void AddPortraitComp(ThingWithComps __instance)
{
// Проверяем, является ли объект пешкой-гуманоидом и нет ли уже компонента
if (
__instance is Pawn pawn
&& pawn.RaceProps?.Humanlike == true
&& pawn.GetComp() == null
)
{
// Создаем компонент
var comp = new PawnPortraitComp { parent = pawn };
// Инициализируем компонент
comp.Initialize(null);
// Получаем список компонентов через рефлексию и добавляем наш
var compsList = allCompsField.GetValue(pawn) as List;
if (compsList != null)
{
compsList.Add(comp);
}
}
}
}
}