Enhance AIImages mod by adding debug logging functionality to various components, improving troubleshooting capabilities. Introduce a new setting to enable or disable debug logs in the UI, with localized strings in English and Russian. Update StableDiffusionNet.Core dependency to version 1.1.5. Update AIImages.dll to reflect these changes.

This commit is contained in:
Leonid Pershin
2025-10-28 19:01:38 +03:00
parent 0bdcd3036a
commit beb1e2b2fc
14 changed files with 385 additions and 52 deletions

View File

@@ -0,0 +1,43 @@
using Verse;
namespace AIImages.Helpers
{
/// <summary>
/// Вспомогательный класс для условного отладочного логирования
/// </summary>
public static class DebugLogger
{
/// <summary>
/// Логирует сообщение только если включены отладочные логи
/// </summary>
public static void Log(string message)
{
if (AIImagesMod.Settings?.enableDebugLogs == true)
{
Verse.Log.Message(message);
}
}
/// <summary>
/// Логирует предупреждение только если включены отладочные логи
/// </summary>
public static void Warning(string message)
{
if (AIImagesMod.Settings?.enableDebugLogs == true)
{
Verse.Log.Warning(message);
}
}
/// <summary>
/// Логирует ошибку только если включены отладочные логи
/// </summary>
public static void Error(string message)
{
if (AIImagesMod.Settings?.enableDebugLogs == true)
{
Verse.Log.Error(message);
}
}
}
}

View File

@@ -1,4 +1,5 @@
using System.IO;
using System.Linq;
using AIImages.Components;
using UnityEngine;
using Verse;
@@ -15,7 +16,17 @@ namespace AIImages.Helpers
/// </summary>
public static PawnPortraitComp GetPortraitComp(Pawn pawn)
{
return pawn?.TryGetComp<PawnPortraitComp>();
if (pawn == null)
{
DebugLogger.Warning("[AI Images] GetPortraitComp called with null pawn");
return null;
}
var comp = pawn.TryGetComp<PawnPortraitComp>();
DebugLogger.Log(
$"[AI Images] GetPortraitComp for {pawn.Name}: {(comp != null ? "Found" : "Not found")}"
);
return comp;
}
/// <summary>
@@ -23,11 +34,25 @@ namespace AIImages.Helpers
/// </summary>
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;
Log.Message($"[AI Images] Saved portrait path for {pawn.Name}: {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}");
}
}
@@ -36,8 +61,19 @@ namespace AIImages.Helpers
/// </summary>
public static string GetPortraitPath(Pawn pawn)
{
DebugLogger.Log($"[AI Images] GetPortraitPath called for {pawn?.Name}");
var comp = GetPortraitComp(pawn);
return comp?.PortraitPath;
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;
}
/// <summary>
@@ -45,8 +81,20 @@ namespace AIImages.Helpers
/// </summary>
public static bool HasPortrait(Pawn pawn)
{
DebugLogger.Log($"[AI Images] HasPortrait called for {pawn?.Name}");
var comp = GetPortraitComp(pawn);
return comp != null && comp.HasPortrait;
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;
}
/// <summary>
@@ -54,23 +102,46 @@ namespace AIImages.Helpers
/// </summary>
public static Texture2D LoadPortrait(Pawn pawn)
{
string path = GetPortraitPath(pawn);
DebugLogger.Log($"[AI Images] LoadPortrait called for {pawn?.Name}");
if (string.IsNullOrEmpty(path) || !File.Exists(path))
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)
{
Log.Warning($"[AI Images] Failed to load portrait for {pawn.Name}: {ex.Message}");
DebugLogger.Warning(
$"[AI Images] Failed to load portrait for {pawn?.Name}: {ex.Message}"
);
return null;
}
}
@@ -92,53 +163,62 @@ namespace AIImages.Helpers
/// </summary>
public static int ClearAllPortraits(string savePath)
{
int deletedCount = 0;
ClearPawnComponents();
return DeletePortraitFiles(savePath);
}
// Очищаем компоненты всех пешек
if (Current.Game != null && Current.Game.Maps != null)
private static void ClearPawnComponents()
{
if (Current.Game?.Maps == null)
return;
foreach (var map in Current.Game.Maps)
{
foreach (var map in Current.Game.Maps)
foreach (var pawn in map.mapPawns.AllPawns)
{
foreach (var pawn in map.mapPawns.AllPawns)
var comp = GetPortraitComp(pawn);
if (comp != null && comp.HasPortrait)
{
var comp = GetPortraitComp(pawn);
if (comp != null && comp.HasPortrait)
{
comp.PortraitPath = null;
}
comp.PortraitPath = null;
}
}
}
}
// Удаляем файлы из директории
private static int DeletePortraitFiles(string savePath)
{
int deletedCount = 0;
string fullPath = Path.Combine(GenFilePaths.SaveDataFolderPath, savePath);
if (Directory.Exists(fullPath))
if (!Directory.Exists(fullPath))
return deletedCount;
try
{
try
{
var files = Directory.GetFiles(fullPath, "*.png");
foreach (var file in files)
{
try
{
File.Delete(file);
deletedCount++;
}
catch (System.Exception ex)
{
Log.Warning($"[AI Images] Failed to delete file {file}: {ex.Message}");
}
}
Log.Message($"[AI Images] Deleted {deletedCount} portrait files");
}
catch (System.Exception ex)
{
Log.Error($"[AI Images] Error cleaning portraits directory: {ex.Message}");
}
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;
}
}
}
}