Run PluginWatcher in lobby and fix accidental Mare API hijacking

This commit is contained in:
Loporrit
2025-08-09 15:04:07 +00:00
parent ec17b32f74
commit e1d54aea43
4 changed files with 46 additions and 12 deletions

View File

@@ -27,6 +27,7 @@ public class IpcProvider : IHostedService, IMediatorSubscriber
private bool _marePluginEnabled = false; private bool _marePluginEnabled = false;
private bool _impersonating = false; private bool _impersonating = false;
private readonly CancellationTokenSource _registerDelayCts = new();
public bool MarePluginEnabled => _marePluginEnabled; public bool MarePluginEnabled => _marePluginEnabled;
public bool ImpersonationActive => _impersonating; public bool ImpersonationActive => _impersonating;
@@ -95,19 +96,39 @@ public class IpcProvider : IHostedService, IMediatorSubscriber
{ {
if (_mareConfig.Current.MareAPI) if (_mareConfig.Current.MareAPI)
{ {
_loadFileProviderMare?.RegisterFunc(LoadMcdf); var cancelToken = _registerDelayCts.Token;
_loadFileAsyncProviderMare?.RegisterFunc(LoadMcdfAsync); Task.Run(async () =>
_handledGameAddressesMare?.RegisterFunc(GetHandledAddresses); {
_impersonating = true; // Wait before registering to reduce the chance of a race condition
_logger.LogDebug("Registered MareSynchronos API"); 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 else
{ {
_loadFileProviderMare?.UnregisterFunc(); _registerDelayCts.Cancel();
_loadFileAsyncProviderMare?.UnregisterFunc(); if (_impersonating)
_handledGameAddressesMare?.UnregisterFunc(); {
_impersonating = false; _loadFileProviderMare?.UnregisterFunc();
_logger.LogDebug("Unregistered MareSynchronos API"); _loadFileAsyncProviderMare?.UnregisterFunc();
_handledGameAddressesMare?.UnregisterFunc();
_impersonating = false;
_logger.LogDebug("Unregistered MareSynchronos API");
}
} }
} }
} }
@@ -119,6 +140,7 @@ public class IpcProvider : IHostedService, IMediatorSubscriber
_loadFileAsyncProvider?.UnregisterFunc(); _loadFileAsyncProvider?.UnregisterFunc();
_handledGameAddresses?.UnregisterFunc(); _handledGameAddresses?.UnregisterFunc();
_registerDelayCts.Cancel();
if (_impersonating) if (_impersonating)
{ {
_loadFileProviderMare?.UnregisterFunc(); _loadFileProviderMare?.UnregisterFunc();

View File

@@ -153,7 +153,6 @@ public class MarePlugin : MediatorSubscriberBase, IHostedService
_runtimeServiceScope.ServiceProvider.GetRequiredService<NotificationService>(); _runtimeServiceScope.ServiceProvider.GetRequiredService<NotificationService>();
_runtimeServiceScope.ServiceProvider.GetRequiredService<ChatService>(); _runtimeServiceScope.ServiceProvider.GetRequiredService<ChatService>();
_runtimeServiceScope.ServiceProvider.GetRequiredService<GuiHookService>(); _runtimeServiceScope.ServiceProvider.GetRequiredService<GuiHookService>();
_runtimeServiceScope.ServiceProvider.GetRequiredService<PluginWatcherService>();
#if !DEBUG #if !DEBUG
if (_mareConfigService.Current.LogLevel != LogLevel.Information) if (_mareConfigService.Current.LogLevel != LogLevel.Information)

View File

@@ -197,6 +197,7 @@ public sealed class Plugin : IDalamudPlugin
collection.AddScoped<ChatService>(); collection.AddScoped<ChatService>();
collection.AddScoped<GuiHookService>(); collection.AddScoped<GuiHookService>();
collection.AddHostedService(p => p.GetRequiredService<PluginWatcherService>());
collection.AddHostedService(p => p.GetRequiredService<ConfigurationSaveService>()); collection.AddHostedService(p => p.GetRequiredService<ConfigurationSaveService>());
collection.AddHostedService(p => p.GetRequiredService<MareMediator>()); collection.AddHostedService(p => p.GetRequiredService<MareMediator>());
collection.AddHostedService(p => p.GetRequiredService<NotificationService>()); collection.AddHostedService(p => p.GetRequiredService<NotificationService>());

View File

@@ -1,5 +1,6 @@
using Dalamud.Plugin; using Dalamud.Plugin;
using MareSynchronos.Services.Mediator; using MareSynchronos.Services.Mediator;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using CapturedPluginState = (string InternalName, System.Version Version, bool IsLoaded); 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; private readonly IDalamudPluginInterface _pluginInterface;
@@ -88,6 +89,17 @@ public class PluginWatcherService : MediatorSubscriberBase
Update(publish: false); 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) public static PluginChangeMessage? GetInitialPluginState(IDalamudPluginInterface pi, string internalName)
{ {
try try