Merge branch 'petnames'

This commit is contained in:
Stanley Dimant
2024-09-10 09:15:46 +02:00
9 changed files with 205 additions and 3 deletions

View 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);
}
}

View File

@@ -7,7 +7,7 @@ public sealed partial class IpcManager : DisposableMediatorSubscriberBase
{
public IpcManager(ILogger<IpcManager> logger, MareMediator mediator,
IpcCallerPenumbra penumbraIpc, IpcCallerGlamourer glamourerIpc, IpcCallerCustomize customizeIpc, IpcCallerHeels heelsIpc,
IpcCallerHonorific honorificIpc, IpcCallerMoodles moodlesIpc) : base(logger, mediator)
IpcCallerHonorific honorificIpc, IpcCallerMoodles moodlesIpc, IpcCallerPetNames ipcCallerPetNames) : base(logger, mediator)
{
CustomizePlus = customizeIpc;
Heels = heelsIpc;
@@ -15,6 +15,7 @@ public sealed partial class IpcManager : DisposableMediatorSubscriberBase
Penumbra = penumbraIpc;
Honorific = honorificIpc;
Moodles = moodlesIpc;
PetNames = ipcCallerPetNames;
if (Initialized)
{
@@ -41,6 +42,7 @@ public sealed partial class IpcManager : DisposableMediatorSubscriberBase
public IpcCallerGlamourer Glamourer { get; }
public IpcCallerPenumbra Penumbra { get; }
public IpcCallerMoodles Moodles { get; }
public IpcCallerPetNames PetNames { get; }
private void PeriodicApiStateCheck()
{
@@ -51,5 +53,6 @@ public sealed partial class IpcManager : DisposableMediatorSubscriberBase
CustomizePlus.CheckAPI();
Honorific.CheckAPI();
Moodles.CheckAPI();
PetNames.CheckAPI();
}
}

View File

@@ -217,6 +217,9 @@ public class PlayerDataFactory
{
previousData.MoodlesData = await _ipcManager.Moodles.GetStatusAsync(playerRelatedObject.Address).ConfigureAwait(false) ?? string.Empty;
_logger.LogDebug("Moodles is now: {moodles}", previousData.MoodlesData);
previousData.MoodlesData = _ipcManager.PetNames.GetLocalNames();
_logger.LogDebug("Pet Nicknames is now: {moodles}", previousData.MoodlesData);
}
if (previousData.FileReplacements.TryGetValue(objectKind, out HashSet<FileReplacement>? fileReplacements))

View File

@@ -344,6 +344,8 @@ public sealed class PairHandler : DisposableMediatorSubscriberBase
case PlayerChanges.Moodles:
await _ipcManager.Moodles.SetStatusAsync(handler.Address, charaData.MoodlesData).ConfigureAwait(false);
await _ipcManager.PetNames.SetPlayerData(handler.Address, charaData.MoodlesData).ConfigureAwait(false);
break;
case PlayerChanges.ForcedRedraw:
@@ -593,6 +595,8 @@ public sealed class PairHandler : DisposableMediatorSubscriberBase
await _ipcManager.Honorific.ClearTitleAsync(address).ConfigureAwait(false);
Logger.LogDebug("[{applicationId}] Restoring Moodles for {alias}/{name}", applicationId, Pair.UserData.AliasOrUID, name);
await _ipcManager.Moodles.RevertStatusAsync(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)
{

View File

@@ -6,4 +6,5 @@ public record OptionalPluginWarning
public bool ShownCustomizePlusWarning { get; set; } = false;
public bool ShownHonorificWarning { get; set; } = false;
public bool ShownMoodlesWarning { get; set; } = false;
public bool ShowPetNicknamesWarning { get; set; } = false;
}

View File

@@ -21,6 +21,7 @@ public sealed class CacheCreationService : DisposableMediatorSubscriberBase
private Task? _cacheCreationTask;
private CancellationTokenSource _honorificCts = new();
private CancellationTokenSource _moodlesCts = new();
private CancellationTokenSource _petNicknamesCts = new();
private bool _isZoning = false;
private readonly Dictionary<ObjectKind, CancellationTokenSource> _glamourerCts = new();
@@ -123,6 +124,15 @@ public sealed class CacheCreationService : DisposableMediatorSubscriberBase
MoodlesChanged();
}
});
Mediator.Subscribe<PetNamesMessage>(this, (msg) =>
{
if (_isZoning) return;
if (!string.Equals(msg.PetNicknamesData, _playerData.MoodlesData, StringComparison.Ordinal))
{
Logger.LogDebug("Received Pet Nicknames change, updating player");
PetNicknamesChanged();
}
});
Mediator.Subscribe<PenumbraModSettingChangedMessage>(this, (msg) =>
{
Logger.LogDebug("Received Penumbra Mod settings change, updating player");
@@ -192,6 +202,20 @@ public sealed class CacheCreationService : DisposableMediatorSubscriberBase
}, 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;

View File

@@ -138,10 +138,12 @@ public sealed class Plugin : IDalamudPlugin
s.GetRequiredService<DalamudUtilService>(), s.GetRequiredService<MareMediator>()));
collection.AddSingleton((s) => new IpcCallerMoodles(s.GetRequiredService<ILogger<IpcCallerMoodles>>(), 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<IpcCallerMoodles>()));
s.GetRequiredService<IpcCallerMoodles>(), s.GetRequiredService<IpcCallerPetNames>()));
collection.AddSingleton((s) => new NotificationService(s.GetRequiredService<ILogger<NotificationService>>(),
s.GetRequiredService<MareMediator>(), s.GetRequiredService<DalamudUtilService>(),
notificationManager, chatGui, s.GetRequiredService<MareConfigService>()));

View File

@@ -41,6 +41,7 @@ public record PenumbraResourceLoadMessage(IntPtr GameObject, string GamePath, st
public record CustomizePlusMessage(nint? Address) : MessageBase;
public record HonorificMessage(string NewHonorificTitle) : MessageBase;
public record MoodlesMessage(IntPtr Address) : MessageBase;
public record PetNamesMessage(string PetNicknamesData) : MessageBase;
public record HonorificReadyMessage : MessageBase;
public record PlayerChangedMessage(CharacterData Data) : MessageBase;
public record CharacterChangedMessage(GameObjectHandler GameObjectHandler) : MessageBase;

View File

@@ -31,7 +31,8 @@ public class PluginWarningNotificationService
ShownCustomizePlusWarning = _mareConfigService.Current.DisableOptionalPluginWarnings,
ShownHeelsWarning = _mareConfigService.Current.DisableOptionalPluginWarnings,
ShownHonorificWarning = _mareConfigService.Current.DisableOptionalPluginWarnings,
ShownMoodlesWarning = _mareConfigService.Current.DisableOptionalPluginWarnings
ShownMoodlesWarning = _mareConfigService.Current.DisableOptionalPluginWarnings,
ShowPetNicknamesWarning = _mareConfigService.Current.DisableOptionalPluginWarnings
};
}
@@ -59,6 +60,12 @@ public class PluginWarningNotificationService
warning.ShownMoodlesWarning = true;
}
if (changes.Contains(PlayerChanges.Moodles) && !warning.ShowPetNicknamesWarning && !_ipcManager.PetNames.APIAvailable)
{
missingPluginsForData.Add("PetNicknames");
warning.ShowPetNicknamesWarning = true;
}
if (missingPluginsForData.Any())
{
_mediator.Publish(new NotificationMessage("Missing plugins for " + playerName,