fix(ItemPayload): correctly handle EventItem, Collectible (closes #166)

This commit is contained in:
goaaats 2022-01-19 00:41:36 +01:00
parent f9da69b47c
commit 7e34f8c00c
No known key found for this signature in database
GPG key ID: F18F057873895461

View file

@ -13,13 +13,13 @@ namespace Dalamud.Game.Text.SeStringHandling.Payloads
/// </summary> /// </summary>
public class ItemPayload : Payload public class ItemPayload : Payload
{ {
private Item item; private Item? item;
// mainly to allow overriding the name (for things like owo) // mainly to allow overriding the name (for things like owo)
// TODO: even though this is present in some item links, it may not really have a use at all // TODO: even though this is present in some item links, it may not really have a use at all
// For things like owo, changing the text payload is probably correct, whereas changing the // For things like owo, changing the text payload is probably correct, whereas changing the
// actual embedded name might not work properly. // actual embedded name might not work properly.
private string? displayName = null; private string? displayName;
[JsonProperty] [JsonProperty]
private uint itemId; private uint itemId;
@ -36,7 +36,23 @@ namespace Dalamud.Game.Text.SeStringHandling.Payloads
public ItemPayload(uint itemId, bool isHq, string? displayNameOverride = null) public ItemPayload(uint itemId, bool isHq, string? displayNameOverride = null)
{ {
this.itemId = itemId; this.itemId = itemId;
this.IsHQ = isHq; this.Kind = isHq ? ItemKind.Hq : ItemKind.Normal;
this.displayName = displayNameOverride;
}
/// <summary>
/// Initializes a new instance of the <see cref="ItemPayload"/> class.
/// Creates a payload representing an interactable item link for the specified item.
/// </summary>
/// <param name="itemId">The id of the item.</param>
/// <param name="kind">Kind of item to encode.</param>
/// <param name="displayNameOverride">An optional name to include in the item link. Typically this should
/// be left as null, or set to the normal item name. Actual overrides are better done with the subsequent
/// TextPayload that is a part of a full item link in chat.</param>
public ItemPayload(uint itemId, ItemKind kind = ItemKind.Normal, string? displayNameOverride = null)
{
this.itemId = itemId;
this.Kind = kind;
this.displayName = displayNameOverride; this.displayName = displayNameOverride;
} }
@ -48,6 +64,32 @@ namespace Dalamud.Game.Text.SeStringHandling.Payloads
{ {
} }
/// <summary>
/// Kinds of items that can be fetched from this payload.
/// </summary>
public enum ItemKind : uint
{
/// <summary>
/// Normal items.
/// </summary>
Normal,
/// <summary>
/// Collectible Items.
/// </summary>
Collectible = 500_000,
/// <summary>
/// High-Quality items.
/// </summary>
Hq = 1_000_000,
/// <summary>
/// Event/Key items.
/// </summary>
EventItem = 2_000_000,
}
/// <inheritdoc/> /// <inheritdoc/>
public override PayloadType Type => PayloadType.Item; public override PayloadType Type => PayloadType.Item;
@ -55,7 +97,7 @@ namespace Dalamud.Game.Text.SeStringHandling.Payloads
/// Gets or sets the displayed name for this item link. Note that incoming links only sometimes have names embedded, /// Gets or sets the displayed name for this item link. Note that incoming links only sometimes have names embedded,
/// often the name is only present in a following text payload. /// often the name is only present in a following text payload.
/// </summary> /// </summary>
public string DisplayName public string? DisplayName
{ {
get get
{ {
@ -82,24 +124,45 @@ namespace Dalamud.Game.Text.SeStringHandling.Payloads
/// The value is evaluated lazily and cached. /// The value is evaluated lazily and cached.
/// </remarks> /// </remarks>
[JsonIgnore] [JsonIgnore]
public Item Item => this.item ??= this.DataResolver.GetExcelSheet<Item>().GetRow(this.itemId); public Item? Item => this.item ??= this.DataResolver.GetExcelSheet<Item>()!.GetRow(this.itemId);
/// <summary> /// <summary>
/// Gets a value indicating whether or not this item link is for a high-quality version of the item. /// Gets a value indicating whether or not this item link is for a high-quality version of the item.
/// </summary> /// </summary>
[JsonProperty] [JsonProperty]
public bool IsHQ { get; private set; } = false; public bool IsHQ => this.Kind == ItemKind.Hq;
/// <summary>
/// Gets or sets the kind of item represented by this payload.
/// </summary>
public ItemKind Kind { get; set; } = ItemKind.Normal;
/// <summary>
/// Initializes a new instance of the <see cref="ItemPayload"/> class.
/// Creates a payload representing an interactable item link for the specified item.
/// </summary>
/// <param name="rawItemId">The raw, unadjusted id of the item.</param>
/// <param name="displayNameOverride">An optional name to include in the item link. Typically this should
/// be left as null, or set to the normal item name. Actual overrides are better done with the subsequent
/// TextPayload that is a part of a full item link in chat.</param>
/// <returns>The created item payload.</returns>
public static ItemPayload FromRaw(uint rawItemId, string? displayNameOverride = null)
{
var (id, kind) = GetAdjustedId(rawItemId);
return new ItemPayload(id, kind, displayNameOverride);
}
/// <inheritdoc/> /// <inheritdoc/>
public override string ToString() public override string ToString()
{ {
return $"{this.Type} - ItemId: {this.itemId}, IsHQ: {this.IsHQ}, Name: {this.displayName ?? this.Item.Name}"; return $"{this.Type} - ItemId: {this.itemId}, Kind: {this.Kind}, Name: {this.displayName ?? this.Item?.Name}";
} }
/// <inheritdoc/> /// <inheritdoc/>
protected override byte[] EncodeImpl() protected override byte[] EncodeImpl()
{ {
var actualItemId = this.IsHQ ? this.itemId + 1000000 : this.itemId; var actualItemId = this.itemId - (uint)this.Kind;
var idBytes = MakeInteger(actualItemId); var idBytes = MakeInteger(actualItemId);
var hasName = !string.IsNullOrEmpty(this.displayName); var hasName = !string.IsNullOrEmpty(this.displayName);
@ -154,13 +217,10 @@ namespace Dalamud.Game.Text.SeStringHandling.Payloads
/// <inheritdoc/> /// <inheritdoc/>
protected override void DecodeImpl(BinaryReader reader, long endOfStream) protected override void DecodeImpl(BinaryReader reader, long endOfStream)
{ {
this.itemId = GetInteger(reader); var (id, kind) = GetAdjustedId(GetInteger(reader));
if (this.itemId > 1000000) this.itemId = id;
{ this.Kind = kind;
this.itemId -= 1000000;
this.IsHQ = true;
}
if (reader.BaseStream.Position + 3 < endOfStream) if (reader.BaseStream.Position + 3 < endOfStream)
{ {
@ -185,5 +245,16 @@ namespace Dalamud.Game.Text.SeStringHandling.Payloads
this.displayName = Encoding.UTF8.GetString(itemNameBytes); this.displayName = Encoding.UTF8.GetString(itemNameBytes);
} }
} }
private static (uint ItemId, ItemKind Kind) GetAdjustedId(uint rawItemId)
{
return rawItemId switch
{
> 500_000 and < 1_000_000 => (rawItemId - 500_000, ItemKind.Collectible),
> 1_000_000 and < 2_000_000 => (rawItemId - 1_000_000, ItemKind.Hq),
> 2_000_000 => (rawItemId - 2_000_000, ItemKind.EventItem),
_ => (rawItemId, ItemKind.Normal),
};
}
} }
} }