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 _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();

View File

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

View File

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

View File

@@ -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