Enhance AIImages mod by adding cancellation support for image generation, improving user experience with localized strings for cancellation actions in English and Russian. Refactor service integration for better dependency management and update AIImages.dll to reflect these changes.
This commit is contained in:
83
Source/AIImages/Services/ServiceContainer.cs
Normal file
83
Source/AIImages/Services/ServiceContainer.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user