diff --git a/MareSynchronos/Interop/Ipc/IpcProvider.cs b/MareSynchronos/Interop/Ipc/IpcProvider.cs index 697e842..74bf5dd 100644 --- a/MareSynchronos/Interop/Ipc/IpcProvider.cs +++ b/MareSynchronos/Interop/Ipc/IpcProvider.cs @@ -27,6 +27,7 @@ public class IpcProvider : IHostedService, IMediatorSubscriber private bool _marePluginEnabled = false; private bool _impersonating = false; + private readonly CancellationTokenSource _registerDelayCts = new(); public bool MarePluginEnabled => _marePluginEnabled; public bool ImpersonationActive => _impersonating; @@ -95,19 +96,39 @@ public class IpcProvider : IHostedService, IMediatorSubscriber { if (_mareConfig.Current.MareAPI) { - _loadFileProviderMare?.RegisterFunc(LoadMcdf); - _loadFileAsyncProviderMare?.RegisterFunc(LoadMcdfAsync); - _handledGameAddressesMare?.RegisterFunc(GetHandledAddresses); - _impersonating = true; - _logger.LogDebug("Registered MareSynchronos API"); + var cancelToken = _registerDelayCts.Token; + Task.Run(async () => + { + // Wait before registering to reduce the chance of a race condition + await Task.Delay(500); + + if (cancelToken.IsCancellationRequested) + return; + + if (_marePluginEnabled) + { + _logger.LogDebug("Not registering MareSynchronos API: Mare plugin is loaded"); + return; + } + + _loadFileProviderMare?.RegisterFunc(LoadMcdf); + _loadFileAsyncProviderMare?.RegisterFunc(LoadMcdfAsync); + _handledGameAddressesMare?.RegisterFunc(GetHandledAddresses); + _impersonating = true; + _logger.LogDebug("Registered MareSynchronos API"); + }, cancelToken); } else { - _loadFileProviderMare?.UnregisterFunc(); - _loadFileAsyncProviderMare?.UnregisterFunc(); - _handledGameAddressesMare?.UnregisterFunc(); - _impersonating = false; - _logger.LogDebug("Unregistered MareSynchronos API"); + _registerDelayCts.Cancel(); + if (_impersonating) + { + _loadFileProviderMare?.UnregisterFunc(); + _loadFileAsyncProviderMare?.UnregisterFunc(); + _handledGameAddressesMare?.UnregisterFunc(); + _impersonating = false; + _logger.LogDebug("Unregistered MareSynchronos API"); + } } } } @@ -119,6 +140,7 @@ public class IpcProvider : IHostedService, IMediatorSubscriber _loadFileAsyncProvider?.UnregisterFunc(); _handledGameAddresses?.UnregisterFunc(); + _registerDelayCts.Cancel(); if (_impersonating) { _loadFileProviderMare?.UnregisterFunc(); diff --git a/MareSynchronos/MarePlugin.cs b/MareSynchronos/MarePlugin.cs index 243da73..bf9a153 100644 --- a/MareSynchronos/MarePlugin.cs +++ b/MareSynchronos/MarePlugin.cs @@ -153,7 +153,6 @@ public class MarePlugin : MediatorSubscriberBase, IHostedService _runtimeServiceScope.ServiceProvider.GetRequiredService(); _runtimeServiceScope.ServiceProvider.GetRequiredService(); _runtimeServiceScope.ServiceProvider.GetRequiredService(); - _runtimeServiceScope.ServiceProvider.GetRequiredService(); #if !DEBUG if (_mareConfigService.Current.LogLevel != LogLevel.Information) diff --git a/MareSynchronos/Plugin.cs b/MareSynchronos/Plugin.cs index 30a4438..794eb05 100644 --- a/MareSynchronos/Plugin.cs +++ b/MareSynchronos/Plugin.cs @@ -197,6 +197,7 @@ public sealed class Plugin : IDalamudPlugin collection.AddScoped(); collection.AddScoped(); + collection.AddHostedService(p => p.GetRequiredService()); collection.AddHostedService(p => p.GetRequiredService()); collection.AddHostedService(p => p.GetRequiredService()); collection.AddHostedService(p => p.GetRequiredService()); diff --git a/MareSynchronos/Services/PluginWatcherService.cs b/MareSynchronos/Services/PluginWatcherService.cs index 4062352..73d8630 100644 --- a/MareSynchronos/Services/PluginWatcherService.cs +++ b/MareSynchronos/Services/PluginWatcherService.cs @@ -1,5 +1,6 @@ using Dalamud.Plugin; using MareSynchronos.Services.Mediator; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using CapturedPluginState = (string InternalName, System.Version Version, bool IsLoaded); @@ -31,7 +32,7 @@ SOFTWARE. */ -public class PluginWatcherService : MediatorSubscriberBase +public class PluginWatcherService : MediatorSubscriberBase, IHostedService { private readonly IDalamudPluginInterface _pluginInterface; @@ -88,6 +89,17 @@ public class PluginWatcherService : MediatorSubscriberBase Update(publish: false); } + public Task StartAsync(CancellationToken cancellationToken) + { + return Task.CompletedTask; + } + + public Task StopAsync(CancellationToken cancellationToken) + { + Mediator.UnsubscribeAll(this); + return Task.CompletedTask; + } + public static PluginChangeMessage? GetInitialPluginState(IDalamudPluginInterface pi, string internalName) { try