Pet Nicknames IPC implementation (#74)
This commit is contained in:
157
MareSynchronos/Interop/Ipc/IpcCallerPetNames.cs
Normal file
157
MareSynchronos/Interop/Ipc/IpcCallerPetNames.cs
Normal file
@@ -0,0 +1,157 @@
|
||||
using Dalamud.Game.ClientState.Objects.SubKinds;
|
||||
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 IpcCallerPetNames : IIpcCaller
|
||||
{
|
||||
private readonly ILogger<IpcCallerPetNames> _logger;
|
||||
private readonly DalamudUtilService _dalamudUtil;
|
||||
private readonly MareMediator _mareMediator;
|
||||
|
||||
private readonly ICallGateSubscriber<object> _petnamesReady;
|
||||
private readonly ICallGateSubscriber<object> _petnamesDisposing;
|
||||
private readonly ICallGateSubscriber<(uint, uint)> _apiVersion;
|
||||
private readonly ICallGateSubscriber<bool> _enabled;
|
||||
|
||||
private readonly ICallGateSubscriber<string, object> _playerDataChanged;
|
||||
private readonly ICallGateSubscriber<string> _getPlayerData;
|
||||
private readonly ICallGateSubscriber<string, object> _setPlayerData;
|
||||
private readonly ICallGateSubscriber<ushort, object> _clearPlayerData;
|
||||
|
||||
public IpcCallerPetNames(ILogger<IpcCallerPetNames> logger, IDalamudPluginInterface pi, DalamudUtilService dalamudUtil,
|
||||
MareMediator mareMediator)
|
||||
{
|
||||
_logger = logger;
|
||||
_dalamudUtil = dalamudUtil;
|
||||
_mareMediator = mareMediator;
|
||||
|
||||
_petnamesReady = pi.GetIpcSubscriber<object>("PetRenamer.Ready");
|
||||
_petnamesDisposing = pi.GetIpcSubscriber<object>("PetRenamer.Disposing");
|
||||
_apiVersion = pi.GetIpcSubscriber<(uint, uint)>("PetRenamer.ApiVersion");
|
||||
_enabled = pi.GetIpcSubscriber<bool>("PetRenamer.Enabled");
|
||||
|
||||
_playerDataChanged = pi.GetIpcSubscriber<string, object>("PetRenamer.PlayerDataChanged");
|
||||
_getPlayerData = pi.GetIpcSubscriber<string>("PetRenamer.GetPlayerData");
|
||||
_setPlayerData = pi.GetIpcSubscriber<string, object>("PetRenamer.SetPlayerData");
|
||||
_clearPlayerData = pi.GetIpcSubscriber<ushort, object>("PetRenamer.ClearPlayerData");
|
||||
|
||||
_petnamesReady.Subscribe(OnPetNicknamesReady);
|
||||
_petnamesDisposing.Subscribe(OnPetNicknamesDispose);
|
||||
_playerDataChanged.Subscribe(OnLocalPetNicknamesDataChange);
|
||||
|
||||
CheckAPI();
|
||||
}
|
||||
|
||||
public bool APIAvailable { get; private set; } = false;
|
||||
|
||||
public void CheckAPI()
|
||||
{
|
||||
try
|
||||
{
|
||||
APIAvailable = _enabled?.InvokeFunc() ?? false;
|
||||
if (APIAvailable)
|
||||
{
|
||||
APIAvailable = _apiVersion?.InvokeFunc() is { Item1: 3, Item2: >= 1 };
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
APIAvailable = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnPetNicknamesReady()
|
||||
{
|
||||
CheckAPI();
|
||||
}
|
||||
|
||||
private void OnPetNicknamesDispose()
|
||||
{
|
||||
_mareMediator.Publish(new PetNamesMessage(string.Empty));
|
||||
}
|
||||
|
||||
public string GetLocalNames()
|
||||
{
|
||||
if (!APIAvailable) return string.Empty;
|
||||
|
||||
try
|
||||
{
|
||||
string localNameData = _getPlayerData.InvokeFunc();
|
||||
return string.IsNullOrEmpty(localNameData) ? string.Empty : localNameData;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogWarning(e, "Could not obtain Pet Nicknames data");
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
public async Task SetPlayerData(nint character, string playerData)
|
||||
{
|
||||
if (!APIAvailable) return;
|
||||
|
||||
_logger.LogTrace("Applying Pet Nicknames data to {chara}", character.ToString("X"));
|
||||
|
||||
try
|
||||
{
|
||||
await _dalamudUtil.RunOnFrameworkThread(() =>
|
||||
{
|
||||
if (string.IsNullOrEmpty(playerData))
|
||||
{
|
||||
var gameObj = _dalamudUtil.CreateGameObject(character);
|
||||
if (gameObj is IPlayerCharacter pc)
|
||||
{
|
||||
_clearPlayerData.InvokeAction(pc.ObjectIndex);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_setPlayerData.InvokeAction(playerData);
|
||||
}
|
||||
}).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogWarning(e, "Could not apply Pet Nicknames data");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task ClearPlayerData(nint characterPointer)
|
||||
{
|
||||
if (!APIAvailable) return;
|
||||
try
|
||||
{
|
||||
await _dalamudUtil.RunOnFrameworkThread(() =>
|
||||
{
|
||||
var gameObj = _dalamudUtil.CreateGameObject(characterPointer);
|
||||
if (gameObj is IPlayerCharacter pc)
|
||||
{
|
||||
_logger.LogTrace("Pet Nicknames removing for {addr}", pc.Address.ToString("X"));
|
||||
_clearPlayerData.InvokeAction(pc.ObjectIndex);
|
||||
}
|
||||
}).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogWarning(e, "Could not clear Pet Nicknames data");
|
||||
}
|
||||
}
|
||||
|
||||
private void OnLocalPetNicknamesDataChange(string data)
|
||||
{
|
||||
_mareMediator.Publish(new PetNamesMessage(data));
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_petnamesReady.Unsubscribe(OnPetNicknamesReady);
|
||||
_petnamesDisposing.Unsubscribe(OnPetNicknamesDispose);
|
||||
_playerDataChanged.Unsubscribe(OnLocalPetNicknamesDataChange);
|
||||
}
|
||||
}
|
||||
@@ -7,13 +7,14 @@ public sealed partial class IpcManager : DisposableMediatorSubscriberBase
|
||||
{
|
||||
public IpcManager(ILogger<IpcManager> logger, MareMediator mediator,
|
||||
IpcCallerPenumbra penumbraIpc, IpcCallerGlamourer glamourerIpc, IpcCallerCustomize customizeIpc, IpcCallerHeels heelsIpc,
|
||||
IpcCallerHonorific honorificIpc) : base(logger, mediator)
|
||||
IpcCallerHonorific honorificIpc, IpcCallerPetNames ipcCallerPetNames) : base(logger, mediator)
|
||||
{
|
||||
CustomizePlus = customizeIpc;
|
||||
Heels = heelsIpc;
|
||||
Glamourer = glamourerIpc;
|
||||
Penumbra = penumbraIpc;
|
||||
Honorific = honorificIpc;
|
||||
PetNames = ipcCallerPetNames;
|
||||
|
||||
if (Initialized)
|
||||
{
|
||||
@@ -39,6 +40,7 @@ public sealed partial class IpcManager : DisposableMediatorSubscriberBase
|
||||
public IpcCallerHeels Heels { get; init; }
|
||||
public IpcCallerGlamourer Glamourer { get; }
|
||||
public IpcCallerPenumbra Penumbra { get; }
|
||||
public IpcCallerPetNames PetNames { get; }
|
||||
|
||||
private void PeriodicApiStateCheck()
|
||||
{
|
||||
@@ -48,5 +50,6 @@ public sealed partial class IpcManager : DisposableMediatorSubscriberBase
|
||||
Heels.CheckAPI();
|
||||
CustomizePlus.CheckAPI();
|
||||
Honorific.CheckAPI();
|
||||
PetNames.CheckAPI();
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ public class CharacterData
|
||||
public string HeelsData { get; set; } = string.Empty;
|
||||
public string HonorificData { get; set; } = string.Empty;
|
||||
public string ManipulationString { get; set; } = string.Empty;
|
||||
public string PetNamesData { get; set; } = string.Empty;
|
||||
|
||||
public API.Data.CharacterData ToAPI()
|
||||
{
|
||||
@@ -44,7 +45,8 @@ public class CharacterData
|
||||
ManipulationData = ManipulationString,
|
||||
HeelsData = HeelsData,
|
||||
CustomizePlusData = CustomizePlusScale.ToDictionary(d => d.Key, d => d.Value),
|
||||
HonorificData = HonorificData
|
||||
HonorificData = HonorificData,
|
||||
PetNamesData = PetNamesData
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -9,4 +9,5 @@ public enum PlayerChanges
|
||||
Heels = 5,
|
||||
Honorific = 7,
|
||||
ForcedRedraw = 8,
|
||||
PetNames = 10,
|
||||
}
|
||||
@@ -204,6 +204,11 @@ public class PlayerDataFactory
|
||||
_logger.LogDebug("Honorific is now: {data}", previousData.HonorificData);
|
||||
previousData.HeelsData = await getHeelsOffset.ConfigureAwait(false);
|
||||
_logger.LogDebug("Heels is now: {heels}", previousData.HeelsData);
|
||||
if (objectKind == ObjectKind.Player)
|
||||
{
|
||||
previousData.PetNamesData = _ipcManager.PetNames.GetLocalNames();
|
||||
_logger.LogDebug("Pet Nicknames is now: {moodles}", previousData.PetNamesData);
|
||||
}
|
||||
|
||||
if (previousData.FileReplacements.TryGetValue(objectKind, out HashSet<FileReplacement>? fileReplacements))
|
||||
{
|
||||
|
||||
@@ -341,6 +341,10 @@ public sealed class PairHandler : DisposableMediatorSubscriberBase
|
||||
}
|
||||
break;
|
||||
|
||||
case PlayerChanges.PetNames:
|
||||
await _ipcManager.PetNames.SetPlayerData(handler.Address, charaData.PetNamesData).ConfigureAwait(false);
|
||||
break;
|
||||
|
||||
case PlayerChanges.ForcedRedraw:
|
||||
await _ipcManager.Penumbra.RedrawAsync(Logger, handler, applicationId, token).ConfigureAwait(false);
|
||||
break;
|
||||
@@ -570,6 +574,8 @@ public sealed class PairHandler : DisposableMediatorSubscriberBase
|
||||
tempHandler.CompareNameAndThrow(name);
|
||||
Logger.LogDebug("[{applicationId}] Restoring Honorific for {alias}/{name}", applicationId, OnlineUser.User.AliasOrUID, name);
|
||||
await _ipcManager.Honorific.ClearTitleAsync(address).ConfigureAwait(false);
|
||||
Logger.LogDebug("[{applicationId}] Restoring Pet Nicknames for {alias}/{name}", applicationId, OnlineUser.User.AliasOrUID, name);
|
||||
await _ipcManager.PetNames.ClearPlayerData(address).ConfigureAwait(false);
|
||||
}
|
||||
else if (objectKind == ObjectKind.MinionOrMount)
|
||||
{
|
||||
|
||||
@@ -5,4 +5,5 @@ public record OptionalPluginWarning
|
||||
public bool ShownHeelsWarning { get; set; } = false;
|
||||
public bool ShownCustomizePlusWarning { get; set; } = false;
|
||||
public bool ShownHonorificWarning { get; set; } = false;
|
||||
public bool ShowPetNicknamesWarning { get; set; } = false;
|
||||
}
|
||||
@@ -20,6 +20,7 @@ public sealed class CacheCreationService : DisposableMediatorSubscriberBase
|
||||
private readonly Dictionary<ObjectKind, GameObjectHandler> _playerRelatedObjects = [];
|
||||
private Task? _cacheCreationTask;
|
||||
private CancellationTokenSource _honorificCts = new();
|
||||
private CancellationTokenSource _petNicknamesCts = new();
|
||||
private bool _isZoning = false;
|
||||
private readonly Dictionary<ObjectKind, CancellationTokenSource> _glamourerCts = new();
|
||||
|
||||
@@ -108,6 +109,15 @@ public sealed class CacheCreationService : DisposableMediatorSubscriberBase
|
||||
HonorificChanged();
|
||||
}
|
||||
});
|
||||
Mediator.Subscribe<PetNamesMessage>(this, (msg) =>
|
||||
{
|
||||
if (_isZoning) return;
|
||||
if (!string.Equals(msg.PetNicknamesData, _playerData.PetNamesData, StringComparison.Ordinal))
|
||||
{
|
||||
Logger.LogDebug("Received Pet Nicknames change, updating player");
|
||||
PetNicknamesChanged();
|
||||
}
|
||||
});
|
||||
Mediator.Subscribe<PenumbraModSettingChangedMessage>(this, async (msg) =>
|
||||
{
|
||||
Logger.LogDebug("Received Penumbra Mod settings change, updating player");
|
||||
@@ -162,6 +172,21 @@ public sealed class CacheCreationService : DisposableMediatorSubscriberBase
|
||||
await AddPlayerCacheToCreate().ConfigureAwait(false);
|
||||
}, token);
|
||||
}
|
||||
|
||||
private void PetNicknamesChanged()
|
||||
{
|
||||
_petNicknamesCts?.Cancel();
|
||||
_petNicknamesCts?.Dispose();
|
||||
_petNicknamesCts = new();
|
||||
var token = _petNicknamesCts.Token;
|
||||
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromSeconds(3), token).ConfigureAwait(false);
|
||||
await AddPlayerCacheToCreate().ConfigureAwait(false);
|
||||
}, token);
|
||||
}
|
||||
|
||||
private void ProcessCacheCreation()
|
||||
{
|
||||
if (_isZoning) return;
|
||||
|
||||
@@ -111,9 +111,12 @@ public sealed class Plugin : IDalamudPlugin
|
||||
s.GetRequiredService<DalamudUtilService>(), s.GetRequiredService<MareMediator>()));
|
||||
collection.AddSingleton((s) => new IpcCallerHonorific(s.GetRequiredService<ILogger<IpcCallerHonorific>>(), pluginInterface,
|
||||
s.GetRequiredService<DalamudUtilService>(), s.GetRequiredService<MareMediator>()));
|
||||
collection.AddSingleton((s) => new IpcCallerPetNames(s.GetRequiredService<ILogger<IpcCallerPetNames>>(), pluginInterface,
|
||||
s.GetRequiredService<DalamudUtilService>(), s.GetRequiredService<MareMediator>()));
|
||||
collection.AddSingleton((s) => new IpcManager(s.GetRequiredService<ILogger<IpcManager>>(),
|
||||
s.GetRequiredService<MareMediator>(), s.GetRequiredService<IpcCallerPenumbra>(), s.GetRequiredService<IpcCallerGlamourer>(),
|
||||
s.GetRequiredService<IpcCallerCustomize>(), s.GetRequiredService<IpcCallerHeels>(), s.GetRequiredService<IpcCallerHonorific>()));
|
||||
s.GetRequiredService<IpcCallerCustomize>(), s.GetRequiredService<IpcCallerHeels>(), s.GetRequiredService<IpcCallerHonorific>(),
|
||||
s.GetRequiredService<IpcCallerPetNames>()));
|
||||
|
||||
collection.AddSingleton((s) => new MareConfigService(pluginInterface.ConfigDirectory.FullName));
|
||||
collection.AddSingleton((s) => new ServerConfigService(pluginInterface.ConfigDirectory.FullName));
|
||||
|
||||
@@ -40,6 +40,7 @@ public record HeelsOffsetMessage : MessageBase;
|
||||
public record PenumbraResourceLoadMessage(IntPtr GameObject, string GamePath, string FilePath) : SameThreadMessage;
|
||||
public record CustomizePlusMessage(string ProfileName) : MessageBase;
|
||||
public record HonorificMessage(string NewHonorificTitle) : MessageBase;
|
||||
public record PetNamesMessage(string PetNicknamesData) : MessageBase;
|
||||
public record HonorificReadyMessage : MessageBase;
|
||||
public record PlayerChangedMessage(CharacterData Data) : MessageBase;
|
||||
public record CharacterChangedMessage(GameObjectHandler GameObjectHandler) : MessageBase;
|
||||
|
||||
@@ -31,6 +31,7 @@ public class PluginWarningNotificationService
|
||||
ShownCustomizePlusWarning = _mareConfigService.Current.DisableOptionalPluginWarnings,
|
||||
ShownHeelsWarning = _mareConfigService.Current.DisableOptionalPluginWarnings,
|
||||
ShownHonorificWarning = _mareConfigService.Current.DisableOptionalPluginWarnings,
|
||||
ShowPetNicknamesWarning = _mareConfigService.Current.DisableOptionalPluginWarnings
|
||||
};
|
||||
}
|
||||
|
||||
@@ -52,6 +53,12 @@ public class PluginWarningNotificationService
|
||||
warning.ShownHonorificWarning = true;
|
||||
}
|
||||
|
||||
if (changes.Contains(PlayerChanges.PetNames) && !warning.ShowPetNicknamesWarning && !_ipcManager.PetNames.APIAvailable)
|
||||
{
|
||||
missingPluginsForData.Add("PetNicknames");
|
||||
warning.ShowPetNicknamesWarning = true;
|
||||
}
|
||||
|
||||
if (missingPluginsForData.Any())
|
||||
{
|
||||
_mediator.Publish(new NotificationMessage("Missing plugins for " + playerName,
|
||||
|
||||
Reference in New Issue
Block a user