using System.IO;
using System.Linq;
using AIImages.Components;
using UnityEngine;
using Verse;
namespace AIImages.Helpers
{
///
/// Вспомогательный класс для работы с портретами персонажей
///
public static class PawnPortraitHelper
{
///
/// Получить компонент портрета пешки
///
public static PawnPortraitComp GetPortraitComp(Pawn pawn)
{
if (pawn == null)
{
DebugLogger.Warning("[AI Images] GetPortraitComp called with null pawn");
return null;
}
var comp = pawn.TryGetComp();
DebugLogger.Log(
$"[AI Images] GetPortraitComp for {pawn.Name}: {(comp != null ? "Found" : "Not found")}"
);
return comp;
}
///
/// Сохранить путь к портрету на пешке
///
public static void SavePortraitPath(Pawn pawn, string path)
{
DebugLogger.Log(
$"[AI Images] SavePortraitPath called for {pawn?.Name} with path: {path}"
);
var comp = GetPortraitComp(pawn);
if (comp != null)
{
DebugLogger.Log(
$"[AI Images] Found portrait component for {pawn.Name}, setting path from '{comp.PortraitPath}' to '{path}'"
);
comp.PortraitPath = path;
DebugLogger.Log(
$"[AI Images] Successfully saved portrait path for {pawn.Name}: {path}"
);
DebugLogger.Log($"[AI Images] Component now has portrait: {comp.HasPortrait}");
}
else
{
DebugLogger.Error($"[AI Images] Failed to get portrait component for {pawn?.Name}");
}
}
///
/// Получить путь к портрету пешки
///
public static string GetPortraitPath(Pawn pawn)
{
DebugLogger.Log($"[AI Images] GetPortraitPath called for {pawn?.Name}");
var comp = GetPortraitComp(pawn);
if (comp == null)
{
DebugLogger.Warning($"[AI Images] No portrait component found for {pawn?.Name}");
return null;
}
DebugLogger.Log(
$"[AI Images] Portrait component found for {pawn?.Name}, path: '{comp.PortraitPath}'"
);
return comp.PortraitPath;
}
///
/// Есть ли у пешки сохраненный портрет
///
public static bool HasPortrait(Pawn pawn)
{
DebugLogger.Log($"[AI Images] HasPortrait called for {pawn?.Name}");
var comp = GetPortraitComp(pawn);
if (comp == null)
{
DebugLogger.Log($"[AI Images] No portrait component found for {pawn?.Name}");
return false;
}
bool hasPortrait = comp.HasPortrait;
DebugLogger.Log(
$"[AI Images] Portrait component for {pawn?.Name} has portrait: {hasPortrait} (path: '{comp.PortraitPath}')"
);
return hasPortrait;
}
///
/// Загрузить портрет пешки как текстуру
///
public static Texture2D LoadPortrait(Pawn pawn)
{
DebugLogger.Log($"[AI Images] LoadPortrait called for {pawn?.Name}");
string path = GetPortraitPath(pawn);
DebugLogger.Log($"[AI Images] Retrieved path for {pawn?.Name}: '{path}'");
if (string.IsNullOrEmpty(path))
{
DebugLogger.Log($"[AI Images] No portrait path found for {pawn?.Name}");
return null;
}
if (!File.Exists(path))
{
DebugLogger.Warning(
$"[AI Images] Portrait file does not exist for {pawn?.Name}: {path}"
);
return null;
}
DebugLogger.Log($"[AI Images] Portrait file exists for {pawn?.Name}: {path}");
try
{
byte[] imageData = File.ReadAllBytes(path);
DebugLogger.Log(
$"[AI Images] Successfully read {imageData.Length} bytes from {path}"
);
Texture2D texture = new Texture2D(2, 2);
texture.LoadImage(imageData);
DebugLogger.Log(
$"[AI Images] Successfully loaded texture for {pawn?.Name}, size: {texture.width}x{texture.height}"
);
return texture;
}
catch (System.Exception ex)
{
DebugLogger.Warning(
$"[AI Images] Failed to load portrait for {pawn?.Name}: {ex.Message}"
);
return null;
}
}
///
/// Очистить портрет пешки
///
public static void ClearPortrait(Pawn pawn)
{
var comp = GetPortraitComp(pawn);
if (comp != null)
{
comp.PortraitPath = null;
}
}
///
/// Очистить все сгенерированные портреты
///
public static int ClearAllPortraits(string savePath)
{
ClearPawnComponents();
return DeletePortraitFiles(savePath);
}
private static void ClearPawnComponents()
{
if (Current.Game?.Maps == null)
return;
foreach (var map in Current.Game.Maps)
{
foreach (var pawn in map.mapPawns.AllPawns)
{
var comp = GetPortraitComp(pawn);
if (comp != null && comp.HasPortrait)
{
comp.PortraitPath = null;
}
}
}
}
private static int DeletePortraitFiles(string savePath)
{
int deletedCount = 0;
string fullPath = Path.Combine(GenFilePaths.SaveDataFolderPath, savePath);
if (!Directory.Exists(fullPath))
return deletedCount;
try
{
var files = Directory.GetFiles(fullPath, "*.png");
deletedCount = files.Count(TryDeleteFile);
Log.Message($"[AI Images] Deleted {deletedCount} portrait files");
}
catch (System.Exception ex)
{
Log.Error($"[AI Images] Error cleaning portraits directory: {ex.Message}");
}
return deletedCount;
}
private static bool TryDeleteFile(string file)
{
try
{
File.Delete(file);
return true;
}
catch (System.Exception ex)
{
Log.Warning($"[AI Images] Failed to delete file {file}: {ex.Message}");
return false;
}
}
}
}