66 lines
2.4 KiB
C#
66 lines
2.4 KiB
C#
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
using AIImages.Components;
|
|
using AIImages.Helpers;
|
|
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
|
|
)
|
|
{
|
|
DebugLogger.Log($"[AI Images] Adding portrait component to {pawn.Name}");
|
|
|
|
// Создаем компонент
|
|
var comp = new PawnPortraitComp { parent = pawn };
|
|
|
|
// Инициализируем компонент
|
|
comp.Initialize(null);
|
|
|
|
// Получаем список компонентов через рефлексию и добавляем наш
|
|
var compsList = allCompsField.GetValue(pawn) as List<ThingComp>;
|
|
if (compsList != null)
|
|
{
|
|
compsList.Add(comp);
|
|
DebugLogger.Log(
|
|
$"[AI Images] Successfully added portrait component to {pawn.Name}"
|
|
);
|
|
}
|
|
else
|
|
{
|
|
DebugLogger.Error($"[AI Images] Failed to get comps list for {pawn.Name}");
|
|
}
|
|
}
|
|
else if (__instance is Pawn pawn2)
|
|
{
|
|
if (pawn2.RaceProps?.Humanlike != true)
|
|
{
|
|
DebugLogger.Log($"[AI Images] Skipping non-humanlike pawn: {pawn2.Name}");
|
|
}
|
|
else if (pawn2.GetComp<PawnPortraitComp>() != null)
|
|
{
|
|
DebugLogger.Log(
|
|
$"[AI Images] Portrait component already exists for {pawn2.Name}"
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|