| using Microsoft.Extensions.DependencyInjection; |
| using Microsoft.Extensions.Logging; |
| using SilkroadBot.Core.Configuration; |
| using SilkroadBot.Core.Events; |
| using SilkroadBot.Core.Plugins; |
| using SilkroadBot.Core.State; |
| using SilkroadBot.Plugins.SDK.Interfaces; |
|
|
| namespace SilkroadBot.Core.Services; |
|
|
| |
| |
| |
| |
| public class BotEngine : IDisposable |
| { |
| private readonly IServiceProvider _services; |
| private readonly ILogger<BotEngine> _logger; |
| private readonly Dictionary<string, BotContext> _activeBots = new(); |
| private readonly ProtocolAdapterRegistry _adapterRegistry; |
| private readonly PluginManager _pluginManager; |
| |
| public IReadOnlyDictionary<string, BotContext> ActiveBots => _activeBots; |
| public ProtocolAdapterRegistry AdapterRegistry => _adapterRegistry; |
| public PluginManager PluginManager => _pluginManager; |
| public IEventDispatcher EventDispatcher { get; } |
|
|
| public BotEngine(IServiceProvider services) |
| { |
| _services = services; |
| _logger = services.GetRequiredService<ILogger<BotEngine>>(); |
| _adapterRegistry = services.GetRequiredService<ProtocolAdapterRegistry>(); |
| _pluginManager = services.GetRequiredService<PluginManager>(); |
| EventDispatcher = services.GetRequiredService<IEventDispatcher>(); |
| } |
|
|
| |
| |
| |
| public BotContext CreateBot(string profileId, Domain.Enums.GameClientType clientType) |
| { |
| var adapter = _adapterRegistry.GetAdapter(clientType); |
| var eventDispatcher = new EventDispatcher(); |
| var logger = _services.GetRequiredService<ILoggerFactory>().CreateLogger<BotContext>(); |
| |
| var bot = new BotContext(profileId, adapter, eventDispatcher, logger); |
| _activeBots[profileId] = bot; |
| |
| _logger.LogInformation("Created bot for profile {ProfileId} with {ClientType}", profileId, clientType); |
| return bot; |
| } |
|
|
| |
| |
| |
| public async Task RemoveBotAsync(string profileId) |
| { |
| if (_activeBots.TryGetValue(profileId, out var bot)) |
| { |
| await bot.DisconnectAsync(); |
| bot.Dispose(); |
| _activeBots.Remove(profileId); |
| _logger.LogInformation("Removed bot for profile {ProfileId}", profileId); |
| } |
| } |
|
|
| |
| |
| |
| public async Task InitializePluginsAsync(IPluginContext context) |
| { |
| await _pluginManager.DiscoverAndLoadAsync(context); |
| } |
|
|
| |
| |
| |
| public static IServiceCollection ConfigureServices(IServiceCollection services, string pluginDir) |
| { |
| services.AddSingleton<ProtocolAdapterRegistry>(); |
| services.AddSingleton<IEventDispatcher, EventDispatcher>(); |
| services.AddSingleton(sp => new PluginManager( |
| pluginDir, |
| sp, |
| sp.GetRequiredService<ILogger<PluginManager>>() |
| )); |
| services.AddSingleton<BotEngine>(); |
| services.AddLogging(builder => builder.AddConsole().SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Debug)); |
| |
| return services; |
| } |
|
|
| public void Dispose() |
| { |
| foreach (var bot in _activeBots.Values) |
| bot.Dispose(); |
| _activeBots.Clear(); |
| _pluginManager.Dispose(); |
| } |
| } |
|
|