From 2dd43cd1a03936c2e4a0697bba08dfc5d07a1fa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E6=B0=B4?= <53346444+ArchiDog1998@users.noreply.github.com> Date: Tue, 23 Jul 2024 22:13:40 +0800 Subject: [PATCH] fix: better nameplate. but not perfect. --- FakeName/Hooker.cs | 71 ++++++++++++++++++++----- FakeName/Localization/Localization.json | 13 +++-- FakeName/Service.cs | 3 ++ XIVConfigUI | 2 +- 4 files changed, 68 insertions(+), 21 deletions(-) diff --git a/FakeName/Hooker.cs b/FakeName/Hooker.cs index 52de638..8184d61 100644 --- a/FakeName/Hooker.cs +++ b/FakeName/Hooker.cs @@ -1,5 +1,6 @@ using Dalamud.Game.ClientState.Conditions; using Dalamud.Game.ClientState.Objects.SubKinds; +using Dalamud.Game.Gui.NamePlate; using Dalamud.Game.Text.SeStringHandling; using Dalamud.Game.Text.SeStringHandling.Payloads; using Dalamud.Hooking; @@ -7,10 +8,12 @@ using Dalamud.Utility.Signatures; using FFXIVClientStructs.FFXIV.Client.UI; using FFXIVClientStructs.FFXIV.Client.UI.Info; +using Svg; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; +using System.Reflection.Metadata; using System.Runtime.InteropServices; using System.Text; @@ -26,15 +29,6 @@ public class Hooker [Signature("E8 ?? ?? ?? ?? 8D 4E 32", DetourName = nameof(AtkTextNodeSetTextDetour))] private Hook? AtkTextNodeSetTextHook { get; init; } - private delegate void SetNamePlateDelegate(IntPtr addon, bool isPrefixTitle, - bool displayTitle, IntPtr titlePtr, IntPtr namePtr, IntPtr fcNamePtr, IntPtr prefix, int iconId); - - /// - /// https://github.com/shdwp/xivPartyIcons/blob/main/PartyIcons/Api/PluginAddressResolver.cs#L40 - /// - [Signature("E8 ?? ?? ?? ?? E9 ?? ?? ?? ?? 48 8B 5C 24 ?? 45 38 BE", DetourName = nameof(SetNamePlateDetour))] - private Hook? SetNamePlateHook { get; init; } - public static List<(string[], string)> Replacement { get; private set; } = []; internal unsafe Hooker() @@ -42,16 +36,67 @@ internal unsafe Hooker() Service.Hook.InitializeFromAttributes(this); AtkTextNodeSetTextHook?.Enable(); - SetNamePlateHook?.Enable(); + + Service.NamePlate.OnNamePlateUpdate += NamePlate_OnNamePlateUpdate; Service.Framework.Update += Framework_Update; Service.ClientState.Login += ClientState_Login; } + private void NamePlate_OnNamePlateUpdate(INamePlateUpdateContext context, IReadOnlyList handlers) + { + foreach (var handler in handlers) + { + switch (handler.NamePlateKind) + { + case NamePlateKind.PlayerCharacter: + var str = handler.Name?.TextValue; + + if (!string.IsNullOrEmpty(str)) + { + handler.Name = ReplaceNameplate(str); + } + + var nameSe = handler.FreeCompanyTag.TextValue; + foreach ((var key, var value) in Service.Config.FCNameDict) + { + if (key != nameSe) continue; + handler.FreeCompanyTag = value; + break; + } + break; + + case NamePlateKind.EventNpcCompanion: + str = handler.Title.ToString(); + var start = str[0]; + var end = str[^1]; + handler.Title = start + ReplaceNameplate(str[1..^1]) + end; + break; + } + } + } + + private static string ReplaceNameplate(string str) + { + if (Service.ClientState.LocalPlayer != null && GetNamesFull(Service.ClientState.LocalPlayer.Name.TextValue).Contains(str)) + { + return Service.Config.FakeNameText; + } + else if (Service.Config.AllPlayerReplace) + { + return GetChangedName(str); + } + else + { + return str; + } + } + public unsafe void Dispose() { AtkTextNodeSetTextHook?.Dispose(); - SetNamePlateHook?.Dispose(); + Service.NamePlate.OnNamePlateUpdate -= NamePlate_OnNamePlateUpdate; + Service.Framework.Update -= Framework_Update; Service.ClientState.Login -= ClientState_Login; } @@ -202,8 +247,8 @@ private unsafe void SetNamePlateDetour(IntPtr namePlateObjectPtr, bool isPrefixT Service.Log.Error(ex, "Failed to change name plate"); } - SetNamePlateHook?.Original(namePlateObjectPtr, isPrefixTitle, displayTitle, - titlePtr, namePtr, fcNamePtr, prefix, iconId); + //SetNamePlateHook?.Original(namePlateObjectPtr, isPrefixTitle, displayTitle, + // titlePtr, namePtr, fcNamePtr, prefix, iconId); } private static string[] GetNamesFull(string name) diff --git a/FakeName/Localization/Localization.json b/FakeName/Localization/Localization.json index 3b3c442..0b4b05f 100644 --- a/FakeName/Localization/Localization.json +++ b/FakeName/Localization/Localization.json @@ -18,17 +18,16 @@ "XIVConfigUI.LocalString.CopyCommand": "Copy \"{0}\"", "XIVConfigUI.LocalString.Localization": "Click to open the crowdin for modifying localization!", "XIVConfigUI.LocalString.SourceCode": "Click to see the source code!", - "XIVConfigUI.Attributes.ConfigUnitType.None": "None", - "XIVConfigUI.Attributes.ConfigUnitType.Seconds": "Time Unit, in seconds.", - "XIVConfigUI.Attributes.ConfigUnitType.Degree": "Angle Unit, in degrees.", - "XIVConfigUI.Attributes.ConfigUnitType.Yalms": "Distance Unit, in yalms.", - "XIVConfigUI.Attributes.ConfigUnitType.Percent": "Ratio Unit, as percentage.", - "XIVConfigUI.Attributes.ConfigUnitType.Pixels": "Display Unit, in pixels.", "XIVConfigUI.LocalString.Remove": "Remove", "XIVConfigUI.LocalString.MoveUp": "Move Up", "XIVConfigUI.LocalString.MoveDown": "Move Down", "XIVConfigUI.LocalString.CopyToClipboard": "Copy to Clipboard", "XIVConfigUI.LocalString.FromClipboard": "From Clipboard", "XIVConfigUI.LocalString.List": "List", - "XIVConfigUI.LocalString.Nothing": "No items in Combo" + "XIVConfigUI.LocalString.Nothing": "No items in Combo", + "XIVConfigUI.Attributes.ConfigUnitType.Seconds": "Time Unit, in seconds.", + "XIVConfigUI.Attributes.ConfigUnitType.Degree": "Angle Unit, in degrees.", + "XIVConfigUI.Attributes.ConfigUnitType.Yalms": "Distance Unit, in yalms.", + "XIVConfigUI.Attributes.ConfigUnitType.Percent": "Ratio Unit, as percentage.", + "XIVConfigUI.Attributes.ConfigUnitType.Pixels": "Display Unit, in pixels." } \ No newline at end of file diff --git a/FakeName/Service.cs b/FakeName/Service.cs index bf70743..0262091 100644 --- a/FakeName/Service.cs +++ b/FakeName/Service.cs @@ -31,4 +31,7 @@ internal class Service [PluginService] public static IPluginLog Log { get; private set; } = null!; + + [PluginService] + public static INamePlateGui NamePlate { get; private set; } = null!; } diff --git a/XIVConfigUI b/XIVConfigUI index 81eff75..aa82808 160000 --- a/XIVConfigUI +++ b/XIVConfigUI @@ -1 +1 @@ -Subproject commit 81eff75dc6256d497646d903cda02b912b5be58d +Subproject commit aa82808b06bdbc16517f8ae8166d3b6769aaac74