43 lines
1.5 KiB
C#
43 lines
1.5 KiB
C#
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
using AIImages.Components;
|
|
using HarmonyLib;
|
|
using Verse;
|
|
|
|
namespace AIImages.Patches
|
|
{
|
|
/// <summary>
|
|
/// Патч для добавления PawnPortraitComp ко всем пешкам
|
|
/// </summary>
|
|
[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<PawnPortraitComp>() == null
|
|
)
|
|
{
|
|
// Создаем компонент
|
|
var comp = new PawnPortraitComp { parent = pawn };
|
|
|
|
// Инициализируем компонент
|
|
comp.Initialize(null);
|
|
|
|
// Получаем список компонентов через рефлексию и добавляем наш
|
|
var compsList = allCompsField.GetValue(pawn) as List<ThingComp>;
|
|
if (compsList != null)
|
|
{
|
|
compsList.Add(comp);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|