Files
ai-images/Source/AIImages/Services/ServiceContainer.cs

84 lines
3.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using AIImages.Settings;
using Verse;
namespace AIImages.Services
{
/// <summary>
/// Простой DI контейнер для управления зависимостями мода
/// Используется вместо статического Service Locator паттерна
/// </summary>
public class ServiceContainer : IDisposable
{
private bool _disposed;
// Сервисы
public IPawnDescriptionService PawnDescriptionService { get; }
public IPromptGeneratorService PromptGeneratorService { get; }
public IStableDiffusionApiService ApiService { get; private set; }
// Настройки
public AIImagesModSettings Settings { get; }
public ServiceContainer(AIImagesModSettings settings)
{
if (settings == null)
{
throw new ArgumentNullException(nameof(settings));
}
Settings = settings;
// Инициализируем сервисы с внедрением зависимостей
PawnDescriptionService = new PawnDescriptionService();
PromptGeneratorService = new AdvancedPromptGenerator();
// Создаем API сервис с текущими настройками
RecreateApiService();
Log.Message("[AI Images] ServiceContainer initialized successfully");
}
/// <summary>
/// Пересоздает API сервис с новыми настройками (например, когда изменился endpoint)
/// </summary>
public void RecreateApiService()
{
// Освобождаем старый сервис, если он был
if (ApiService is IDisposable disposable)
{
disposable.Dispose();
}
// Создаем новый с актуальными настройками
ApiService = new StableDiffusionNetAdapter(Settings.apiEndpoint, Settings.savePath);
Log.Message($"[AI Images] API service recreated with endpoint: {Settings.apiEndpoint}");
}
/// <summary>
/// Проверяет, нужно ли пересоздать API сервис (например, если изменился endpoint)
/// </summary>
public bool ShouldRecreateApiService(string newEndpoint)
{
// Можно добавить более сложную логику проверки
return Settings.apiEndpoint != newEndpoint;
}
public void Dispose()
{
if (_disposed)
return;
// Освобождаем ресурсы API сервиса
if (ApiService is IDisposable disposable)
{
disposable.Dispose();
}
_disposed = true;
Log.Message("[AI Images] ServiceContainer disposed");
}
}
}