feat(context): use SeString for names

BREAKING CHANGE
This commit is contained in:
Anna 2021-06-04 16:25:35 -04:00
parent 86471547f1
commit 2c5c15d6e2
Signed by: anna
GPG Key ID: 0B391D8F06FCD9E0
9 changed files with 37 additions and 28 deletions

View File

@ -1,4 +1,5 @@
using System;
using Dalamud.Game.Text.SeStringHandling;
namespace XivCommon.Functions.ContextMenu {
/// <summary>
@ -33,14 +34,14 @@ namespace XivCommon.Functions.ContextMenu {
/// <summary>
/// The text related to this context menu, usually an actor name.
/// </summary>
public string? Text { get; }
public SeString? Text { get; }
/// <summary>
/// The world of the actor this context menu is for, if any.
/// </summary>
public ushort ActorWorld { get; }
internal BaseContextMenuArgs(IntPtr addon, IntPtr agent, string? parentAddonName, uint actorId, uint contentIdLower, string? text, ushort actorWorld) {
internal BaseContextMenuArgs(IntPtr addon, IntPtr agent, string? parentAddonName, uint actorId, uint contentIdLower, SeString? text, ushort actorWorld) {
this.Addon = addon;
this.Agent = agent;
this.ParentAddonName = parentAddonName;

View File

@ -6,7 +6,7 @@ using System.Runtime.InteropServices;
using System.Text;
using Dalamud;
using Dalamud.Game;
using Dalamud.Game.Internal.Gui;
using Dalamud.Game.Text.SeStringHandling;
using Dalamud.Hooking;
using FFXIVClientStructs.FFXIV.Component.GUI;
using XivCommon.Functions.ContextMenu.Inventory;
@ -135,15 +135,15 @@ namespace XivCommon.Functions.ContextMenu {
private GameFunctions Functions { get; }
private ClientLanguage Language { get; }
private GameGui Gui { get; }
private SeStringManager SeStringManager { get; }
private IntPtr Agent { get; set; } = IntPtr.Zero;
private List<BaseContextMenuItem> Items { get; } = new();
private int NormalSize { get; set; }
internal ContextMenu(GameFunctions functions, SigScanner scanner, GameGui gui, ClientLanguage language, Hooks hooks) {
internal ContextMenu(GameFunctions functions, SigScanner scanner, SeStringManager manager, ClientLanguage language, Hooks hooks) {
this.Functions = functions;
this.Language = language;
this.Gui = gui;
this.SeStringManager = manager;
if (!hooks.HasFlag(Hooks.ContextMenu)) {
return;
@ -244,11 +244,11 @@ namespace XivCommon.Functions.ContextMenu {
return Encoding.UTF8.GetString(Util.ReadTerminated(parentAddon + 8));
}
private static unsafe (uint actorId, uint contentIdLower, string? text, ushort actorWorld) GetAgentInfo(IntPtr agent) {
private unsafe (uint actorId, uint contentIdLower, SeString? text, ushort actorWorld) GetAgentInfo(IntPtr agent) {
var actorId = *(uint*) (agent + ActorIdOffset);
var contentIdLower = *(uint*) (agent + ContentIdLowerOffset);
var textBytes = Util.ReadTerminated(Marshal.ReadIntPtr(agent + TextPointerOffset));
var text = textBytes.Length == 0 ? null : Encoding.UTF8.GetString(textBytes);
var text = textBytes.Length == 0 ? null : this.SeStringManager.Parse(textBytes);
var actorWorld = *(ushort*) (agent + WorldOffset);
return (actorId, contentIdLower, text, actorWorld);
}
@ -301,8 +301,7 @@ namespace XivCommon.Functions.ContextMenu {
for (var i = 0; i < this.NormalSize; i++) {
var atkItem = &atkValueArgs[offset + i];
var nameBytes = Util.ReadTerminated((IntPtr) atkItem->String);
var name = Encoding.UTF8.GetString(nameBytes);
var name = Util.ReadSeString((IntPtr) atkItem->String, this.SeStringManager);
var enabled = true;
if (hasGameDisabled) {
@ -347,7 +346,7 @@ namespace XivCommon.Functions.ContextMenu {
this.Items.AddRange(args.Items);
} else {
var info = GetAgentInfo(agent);
var info = this.GetAgentInfo(agent);
var args = new ContextMenuOpenArgs(
addon,
@ -426,7 +425,7 @@ namespace XivCommon.Functions.ContextMenu {
NativeContextMenuItem native => native.Name,
_ => "Invalid context menu item",
};
var nameBytes = Encoding.UTF8.GetBytes(name).Terminate();
var nameBytes = name.Encode().Terminate();
fixed (byte* nameBytesPtr = nameBytes) {
this._atkValueSetString(newItem, nameBytesPtr);
}
@ -452,7 +451,7 @@ namespace XivCommon.Functions.ContextMenu {
// a custom item is being clicked
case NormalContextMenuItem custom: {
var addonName = this.GetParentAddonName(addon);
var info = GetAgentInfo(custom.Agent);
var info = this.GetAgentInfo(custom.Agent);
var args = new ContextMenuItemSelectedArgs(
addon,

View File

@ -1,11 +1,12 @@
using System;
using Dalamud.Game.Text.SeStringHandling;
namespace XivCommon.Functions.ContextMenu {
/// <summary>
/// Arguments for the context menu item selected delegate.
/// </summary>
public class ContextMenuItemSelectedArgs : BaseContextMenuArgs {
internal ContextMenuItemSelectedArgs(IntPtr addon, IntPtr agent, string? parentAddonName, uint actorId, uint contentIdLower, string? text, ushort actorWorld) : base(addon, agent, parentAddonName, actorId, contentIdLower, text, actorWorld) {
internal ContextMenuItemSelectedArgs(IntPtr addon, IntPtr agent, string? parentAddonName, uint actorId, uint contentIdLower, SeString? text, ushort actorWorld) : base(addon, agent, parentAddonName, actorId, contentIdLower, text, actorWorld) {
}
}
}

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Dalamud.Game.Text.SeStringHandling;
namespace XivCommon.Functions.ContextMenu {
/// <summary>
@ -11,7 +12,7 @@ namespace XivCommon.Functions.ContextMenu {
/// </summary>
public List<BaseContextMenuItem> Items { get; } = new();
internal ContextMenuOpenArgs(IntPtr addon, IntPtr agent, string? parentAddonName, uint actorId, uint contentIdLower, string? text, ushort actorWorld) : base(addon, agent, parentAddonName, actorId, contentIdLower, text, actorWorld) {
internal ContextMenuOpenArgs(IntPtr addon, IntPtr agent, string? parentAddonName, uint actorId, uint contentIdLower, SeString? text, ushort actorWorld) : base(addon, agent, parentAddonName, actorId, contentIdLower, text, actorWorld) {
}
}
}

View File

@ -1,4 +1,5 @@
using System;
using Dalamud.Game.Text.SeStringHandling;
namespace XivCommon.Functions.ContextMenu {
/// <summary>
@ -11,22 +12,22 @@ namespace XivCommon.Functions.ContextMenu {
/// <summary>
/// The name of the context item to be shown for English clients.
/// </summary>
public string NameEnglish { get; set; }
public SeString NameEnglish { get; set; }
/// <summary>
/// The name of the context item to be shown for Japanese clients.
/// </summary>
public string NameJapanese { get; set; }
public SeString NameJapanese { get; set; }
/// <summary>
/// The name of the context item to be shown for French clients.
/// </summary>
public string NameFrench { get; set; }
public SeString NameFrench { get; set; }
/// <summary>
/// The name of the context item to be shown for German clients.
/// </summary>
public string NameGerman { get; set; }
public SeString NameGerman { get; set; }
/// <summary>
/// The action to perform when this item is clicked.
@ -38,7 +39,7 @@ namespace XivCommon.Functions.ContextMenu {
/// </summary>
/// <param name="name">the English name of the item, copied to other languages</param>
/// <param name="action">the action to perform on click</param>
internal CustomContextMenuItem(string name, T action) {
internal CustomContextMenuItem(SeString name, T action) {
this.NameEnglish = name;
this.NameJapanese = name;
this.NameFrench = name;

View File

@ -1,4 +1,6 @@
namespace XivCommon.Functions.ContextMenu.Inventory {
using Dalamud.Game.Text.SeStringHandling;
namespace XivCommon.Functions.ContextMenu.Inventory {
/// <summary>
/// A custom context menu item for inventory items.
/// </summary>
@ -8,7 +10,7 @@
/// </summary>
/// <param name="name">the English name of the item, copied to other languages</param>
/// <param name="action">the action to perform on click</param>
public InventoryContextMenuItem(string name, ContextMenu.InventoryContextMenuItemSelectedDelegate action) : base(name, action) {
public InventoryContextMenuItem(SeString name, ContextMenu.InventoryContextMenuItemSelectedDelegate action) : base(name, action) {
}
}
}

View File

@ -1,4 +1,6 @@
namespace XivCommon.Functions.ContextMenu {
using Dalamud.Game.Text.SeStringHandling;
namespace XivCommon.Functions.ContextMenu {
/// <summary>
/// A native context menu item
/// </summary>
@ -11,9 +13,9 @@
/// <summary>
/// The name of the context item.
/// </summary>
public string Name { get; set; }
public SeString Name { get; set; }
internal NativeContextMenuItem(byte action, string name, bool enabled) {
internal NativeContextMenuItem(byte action, SeString name, bool enabled) {
this.Name = name;
this.InternalAction = action;
this.Enabled = enabled;

View File

@ -1,4 +1,6 @@
namespace XivCommon.Functions.ContextMenu {
using Dalamud.Game.Text.SeStringHandling;
namespace XivCommon.Functions.ContextMenu {
/// <summary>
/// A custom normal context menu item
/// </summary>
@ -11,7 +13,7 @@
/// </summary>
/// <param name="name">the English name of the item, copied to other languages</param>
/// <param name="action">the action to perform on click</param>
public NormalContextMenuItem(string name, ContextMenu.ContextMenuItemSelectedDelegate action) : base(name, action) {
public NormalContextMenuItem(SeString name, ContextMenu.ContextMenuItemSelectedDelegate action) : base(name, action) {
}
}
}

View File

@ -84,7 +84,7 @@ namespace XivCommon {
this.Examine = new Examine(this, scanner);
this.Talk = new Talk(scanner, seStringManager, hooks.HasFlag(Hooks.Talk));
this.ChatBubbles = new ChatBubbles(dalamud, scanner, seStringManager, hooks.HasFlag(Hooks.ChatBubbles));
this.ContextMenu = new ContextMenu(this, scanner, @interface.Framework.Gui, @interface.ClientState.ClientLanguage, hooks);
this.ContextMenu = new ContextMenu(this, scanner, seStringManager, @interface.ClientState.ClientLanguage, hooks);
this.Tooltips = new Tooltips(scanner, @interface.Framework, @interface.Framework.Gui, seStringManager, hooks.HasFlag(Hooks.Tooltips));
if (scanner.TryScanText(Signatures.GetAgentByInternalId, out var byInternalIdPtr, "GetAgentByInternalId")) {