Rename IpcCallerMare, implement checking for Lightless/Snowcloak.
- Do not sync with pairs handled by Lightless/Snowcloak. - Easy to add extra clients into this list.
This commit is contained in:
@@ -1,44 +0,0 @@
|
||||
using Dalamud.Plugin;
|
||||
using Dalamud.Plugin.Ipc;
|
||||
using MareSynchronos.Services;
|
||||
using MareSynchronos.Services.Mediator;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace MareSynchronos.Interop.Ipc;
|
||||
|
||||
public sealed class IpcCallerMare : DisposableMediatorSubscriberBase
|
||||
{
|
||||
private readonly ICallGateSubscriber<List<nint>> _mareHandledGameAddresses;
|
||||
private readonly List<nint> _emptyList = [];
|
||||
|
||||
private bool _pluginLoaded;
|
||||
|
||||
public IpcCallerMare(ILogger<IpcCallerMare> logger, IDalamudPluginInterface pi, MareMediator mediator) : base(logger, mediator)
|
||||
{
|
||||
_mareHandledGameAddresses = pi.GetIpcSubscriber<List<nint>>("MareSynchronos.GetHandledAddresses");
|
||||
|
||||
_pluginLoaded = PluginWatcherService.GetInitialPluginState(pi, "MareSynchronos")?.IsLoaded ?? false;
|
||||
|
||||
Mediator.SubscribeKeyed<PluginChangeMessage>(this, "MareSynchronos", (msg) =>
|
||||
{
|
||||
_pluginLoaded = msg.IsLoaded;
|
||||
});
|
||||
}
|
||||
|
||||
public bool APIAvailable { get; private set; } = false;
|
||||
|
||||
// Must be called on framework thread
|
||||
public IReadOnlyList<nint> GetHandledGameAddresses()
|
||||
{
|
||||
if (!_pluginLoaded) return _emptyList;
|
||||
|
||||
try
|
||||
{
|
||||
return _mareHandledGameAddresses.InvokeFunc();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return _emptyList;
|
||||
}
|
||||
}
|
||||
}
|
||||
83
MareSynchronos/Interop/Ipc/IpcCallerOtherSync.cs
Normal file
83
MareSynchronos/Interop/Ipc/IpcCallerOtherSync.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
using Dalamud.Plugin;
|
||||
using Dalamud.Plugin.Ipc;
|
||||
using MareSynchronos.Services;
|
||||
using MareSynchronos.Services.Mediator;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace MareSynchronos.Interop.Ipc;
|
||||
|
||||
public sealed class IpcCallerOtherSync : DisposableMediatorSubscriberBase
|
||||
{
|
||||
private readonly ICallGateSubscriber<List<nint>> _lightlessHandledGameAddresses;
|
||||
private readonly ICallGateSubscriber<List<nint>> _snowcloakHandledGameAddresses;
|
||||
|
||||
private readonly List<nint> _emptyList = [];
|
||||
|
||||
private bool _lightlessLoaded;
|
||||
private bool _snowcloakLoaded;
|
||||
|
||||
public IpcCallerOtherSync(ILogger<IpcCallerOtherSync> logger, IDalamudPluginInterface pi, MareMediator mediator) : base(logger, mediator)
|
||||
{
|
||||
_lightlessHandledGameAddresses = pi.GetIpcSubscriber<List<nint>>("LightlessSync.GetHandledAddresses");
|
||||
_snowcloakHandledGameAddresses = pi.GetIpcSubscriber<List<nint>>("SnowcloakSync.GetHandledAddresses");
|
||||
|
||||
_lightlessLoaded = PluginWatcherService.GetInitialPluginState(pi, "LightlessSync")?.IsLoaded ?? false;
|
||||
|
||||
Mediator.SubscribeKeyed<PluginChangeMessage>(this, "LightlessSync", (msg) =>
|
||||
{
|
||||
_lightlessLoaded = msg.IsLoaded;
|
||||
});
|
||||
|
||||
_snowcloakLoaded = PluginWatcherService.GetInitialPluginState(pi, "SnowcloakSync")?.IsLoaded ?? false;
|
||||
|
||||
Mediator.SubscribeKeyed<PluginChangeMessage>(this, "SnowcloakSync", (msg) =>
|
||||
{
|
||||
_snowcloakLoaded = msg.IsLoaded;
|
||||
});
|
||||
}
|
||||
|
||||
public bool APIAvailable { get; private set; } = false;
|
||||
|
||||
// Must be called on framework thread
|
||||
public IReadOnlyList<nint> GetHandledGameAddresses()
|
||||
{
|
||||
if (!_lightlessLoaded && !_snowcloakLoaded) return _emptyList;
|
||||
|
||||
try
|
||||
{
|
||||
return GetLightlessHandledGameAddresses().Concat(GetSnowcloakHandledGameAddresses()).ToList();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return _emptyList;
|
||||
}
|
||||
}
|
||||
|
||||
private List<nint> GetLightlessHandledGameAddresses()
|
||||
{
|
||||
if (!_lightlessLoaded) return _emptyList;
|
||||
|
||||
try
|
||||
{
|
||||
return _lightlessHandledGameAddresses.InvokeFunc();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return _emptyList;
|
||||
}
|
||||
}
|
||||
|
||||
private List<nint> GetSnowcloakHandledGameAddresses()
|
||||
{
|
||||
if (!_snowcloakLoaded) return _emptyList;
|
||||
|
||||
try
|
||||
{
|
||||
return _snowcloakHandledGameAddresses.InvokeFunc();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return _emptyList;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,6 @@ using MareSynchronos.MareConfiguration;
|
||||
using MareSynchronos.PlayerData.Handlers;
|
||||
using MareSynchronos.Services;
|
||||
using MareSynchronos.Services.Mediator;
|
||||
using MareSynchronos.Utils;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
@@ -15,25 +14,12 @@ public class IpcProvider : IHostedService, IMediatorSubscriber
|
||||
{
|
||||
private readonly ILogger<IpcProvider> _logger;
|
||||
private readonly IDalamudPluginInterface _pi;
|
||||
private readonly MareConfigService _mareConfig;
|
||||
private readonly CharaDataManager _charaDataManager;
|
||||
private ICallGateProvider<string, IGameObject, bool>? _loadFileProvider;
|
||||
private ICallGateProvider<string, IGameObject, Task<bool>>? _loadFileAsyncProvider;
|
||||
private ICallGateProvider<List<nint>>? _handledGameAddresses;
|
||||
private readonly List<GameObjectHandler> _activeGameObjectHandlers = [];
|
||||
|
||||
private ICallGateProvider<string, IGameObject, bool>? _loadFileProviderMare;
|
||||
private ICallGateProvider<string, IGameObject, Task<bool>>? _loadFileAsyncProviderMare;
|
||||
private ICallGateProvider<List<nint>>? _handledGameAddressesMare;
|
||||
|
||||
private bool _marePluginEnabled = false;
|
||||
private bool _impersonating = false;
|
||||
private DateTime _unregisterTime = DateTime.UtcNow;
|
||||
private CancellationTokenSource _registerDelayCts = new();
|
||||
|
||||
public bool MarePluginEnabled => _marePluginEnabled;
|
||||
public bool ImpersonationActive => _impersonating;
|
||||
|
||||
public MareMediator Mediator { get; init; }
|
||||
|
||||
public IpcProvider(ILogger<IpcProvider> logger, IDalamudPluginInterface pi, MareConfigService mareConfig,
|
||||
@@ -41,7 +27,6 @@ public class IpcProvider : IHostedService, IMediatorSubscriber
|
||||
{
|
||||
_logger = logger;
|
||||
_pi = pi;
|
||||
_mareConfig = mareConfig;
|
||||
_charaDataManager = charaDataManager;
|
||||
Mediator = mareMediator;
|
||||
|
||||
@@ -55,15 +40,6 @@ public class IpcProvider : IHostedService, IMediatorSubscriber
|
||||
if (msg.OwnedObject) return;
|
||||
_activeGameObjectHandlers.Remove(msg.GameObjectHandler);
|
||||
});
|
||||
|
||||
/*
|
||||
_marePluginEnabled = PluginWatcherService.GetInitialPluginState(pi, "MareSynchronos")?.IsLoaded ?? false;
|
||||
Mediator.SubscribeKeyed<PluginChangeMessage>(this, "MareSynchronos", p => {
|
||||
_marePluginEnabled = p.IsLoaded;
|
||||
HandleMareImpersonation(automatic: true);
|
||||
});
|
||||
*/
|
||||
_marePluginEnabled = true;
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
@@ -76,72 +52,10 @@ public class IpcProvider : IHostedService, IMediatorSubscriber
|
||||
_handledGameAddresses = _pi.GetIpcProvider<List<nint>>("ClubPenguinSync.GetHandledAddresses");
|
||||
_handledGameAddresses.RegisterFunc(GetHandledAddresses);
|
||||
|
||||
_loadFileProviderMare = _pi.GetIpcProvider<string, IGameObject, bool>("MareSynchronos.LoadMcdf");
|
||||
_loadFileAsyncProviderMare = _pi.GetIpcProvider<string, IGameObject, Task<bool>>("MareSynchronos.LoadMcdfAsync");
|
||||
_handledGameAddressesMare = _pi.GetIpcProvider<List<nint>>("MareSynchronos.GetHandledAddresses");
|
||||
HandleMareImpersonation(automatic: true);
|
||||
|
||||
_logger.LogInformation("Started IpcProviderService");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public void HandleMareImpersonation(bool automatic = false)
|
||||
{
|
||||
if (_marePluginEnabled)
|
||||
{
|
||||
if (_impersonating)
|
||||
{
|
||||
_loadFileProviderMare?.UnregisterFunc();
|
||||
_loadFileAsyncProviderMare?.UnregisterFunc();
|
||||
_handledGameAddressesMare?.UnregisterFunc();
|
||||
_impersonating = false;
|
||||
_unregisterTime = DateTime.UtcNow;
|
||||
_logger.LogDebug("Unregistered MareSynchronos API");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_mareConfig.Current.MareAPI)
|
||||
{
|
||||
var cancelToken = _registerDelayCts.Token;
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
// Wait before registering to reduce the chance of a race condition
|
||||
if (automatic)
|
||||
await Task.Delay(5000).ConfigureAwait(false);
|
||||
|
||||
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(MareGetHandledAddresses);
|
||||
_impersonating = true;
|
||||
_logger.LogDebug("Registered MareSynchronos API");
|
||||
}, cancelToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
_registerDelayCts = _registerDelayCts.CancelRecreate();
|
||||
if (_impersonating)
|
||||
{
|
||||
_loadFileProviderMare?.UnregisterFunc();
|
||||
_loadFileAsyncProviderMare?.UnregisterFunc();
|
||||
_handledGameAddressesMare?.UnregisterFunc();
|
||||
_impersonating = false;
|
||||
_unregisterTime = DateTime.UtcNow;
|
||||
_logger.LogDebug("Unregistered MareSynchronos API");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogDebug("Stopping IpcProvider Service");
|
||||
@@ -149,14 +63,6 @@ public class IpcProvider : IHostedService, IMediatorSubscriber
|
||||
_loadFileAsyncProvider?.UnregisterFunc();
|
||||
_handledGameAddresses?.UnregisterFunc();
|
||||
|
||||
_registerDelayCts.Cancel();
|
||||
if (_impersonating)
|
||||
{
|
||||
_loadFileProviderMare?.UnregisterFunc();
|
||||
_loadFileAsyncProviderMare?.UnregisterFunc();
|
||||
_handledGameAddressesMare?.UnregisterFunc();
|
||||
}
|
||||
|
||||
Mediator.UnsubscribeAll(this);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
@@ -186,19 +92,4 @@ public class IpcProvider : IHostedService, IMediatorSubscriber
|
||||
{
|
||||
return _activeGameObjectHandlers.Where(g => g.Address != nint.Zero).Select(g => g.Address).Distinct().ToList();
|
||||
}
|
||||
|
||||
private List<nint> MareGetHandledAddresses()
|
||||
{
|
||||
if (!_impersonating)
|
||||
{
|
||||
if ((DateTime.UtcNow - _unregisterTime).TotalSeconds >= 1.0)
|
||||
{
|
||||
_logger.LogWarning("GetHandledAddresses called when it should not be registered");
|
||||
_handledGameAddressesMare?.UnregisterFunc();
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
return GetHandledAddresses();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user