Improve handling of quote parts when no text part is provided

This commit is contained in:
nebel 2024-07-10 21:50:43 +09:00
parent b2e30f7cc1
commit 9a1a32c03e
No known key found for this signature in database
3 changed files with 61 additions and 9 deletions

View file

@ -94,6 +94,38 @@ internal sealed class NamePlateGui : IInternalDisposableService, INamePlateGui
this.addonLifecycle.UnregisterListener(this.preRequestedUpdateListener);
}
/// <summary>
/// Strips the surrounding quotes from a free company tag. If the quotes are not present in the expected location,
/// no modifications will be made.
/// </summary>
/// <param name="text">A quoted free company tag.</param>
/// <returns>A span containing the free company tag without its surrounding quote characters.</returns>
internal static ReadOnlySpan<byte> StripFreeCompanyTagQuotes(ReadOnlySpan<byte> text)
{
if (text.Length > 4 && text[..3].SequenceEqual(" «"u8) && text[^2..].SequenceEqual("»"u8))
{
return text[3..^2];
}
return text;
}
/// <summary>
/// Strips the surrounding quotes from a title. If the quotes are not present in the expected location, no
/// modifications will be made.
/// </summary>
/// <param name="text">A quoted title.</param>
/// <returns>A span containing the title without its surrounding quote characters.</returns>
internal static ReadOnlySpan<byte> StripTitleQuotes(ReadOnlySpan<byte> text)
{
if (text.Length > 5 && text[..3].SequenceEqual("《"u8) && text[^3..].SequenceEqual("》"u8))
{
return text[3..^3];
}
return text;
}
private static nint CreateEmptyStringPointer()
{
var pointer = Marshal.AllocHGlobal(1);

View file

@ -16,21 +16,28 @@ public interface INamePlateInfoView
SeString Name { get; }
/// <summary>
/// Gets the displayed free company tag for this nameplate according to the nameplate info object.
/// Gets the displayed free company tag for this nameplate according to the nameplate info object. For this field,
/// the quote characters which appear on either side of the title are NOT included.
/// </summary>
SeString FreeCompanyTag { get; }
/// <summary>
/// Gets the displayed title for this nameplate according to the nameplate info object. In this field, the quote
/// Gets the displayed free company tag for this nameplate according to the nameplate info object. For this field,
/// the quote characters which appear on either side of the title ARE included.
/// </summary>
SeString QuotedFreeCompanyTag { get; }
/// <summary>
/// Gets the displayed title for this nameplate according to the nameplate info object. For this field, the quote
/// characters which appear on either side of the title are NOT included.
/// </summary>
SeString Title { get; }
/// <summary>
/// Gets the displayed title for this nameplate according to the nameplate info object. In this field, the quote
/// Gets the displayed title for this nameplate according to the nameplate info object. For this field, the quote
/// characters which appear on either side of the title ARE included.
/// </summary>
SeString DisplayTitle { get; }
SeString QuotedTitle { get; }
/// <summary>
/// Gets the displayed level text for this nameplate according to the nameplate info object.
@ -63,21 +70,26 @@ internal unsafe class NamePlateInfoView(RaptureAtkModule.NamePlateInfo* info) :
{
private SeString? name;
private SeString? freeCompanyTag;
private SeString? quotedFreeCompanyTag;
private SeString? title;
private SeString? displayTitle;
private SeString? quotedTitle;
private SeString? levelText;
/// <inheritdoc/>
public SeString Name => this.name ??= SeString.Parse(info->Name);
/// <inheritdoc/>
public SeString FreeCompanyTag => this.freeCompanyTag ??= SeString.Parse(info->FcName);
public SeString FreeCompanyTag => this.freeCompanyTag ??=
SeString.Parse(NamePlateGui.StripFreeCompanyTagQuotes(info->FcName));
/// <inheritdoc/>
public SeString QuotedFreeCompanyTag => this.quotedFreeCompanyTag ??= SeString.Parse(info->FcName);
/// <inheritdoc/>
public SeString Title => this.title ??= SeString.Parse(info->Title);
/// <inheritdoc/>
public SeString DisplayTitle => this.displayTitle ??= SeString.Parse(info->DisplayTitle);
public SeString QuotedTitle => this.quotedTitle ??= SeString.Parse(info->DisplayTitle);
/// <inheritdoc/>
public SeString LevelText => this.levelText ??= SeString.Parse(info->LevelText);

View file

@ -51,12 +51,12 @@ public class NamePlateQuotedParts(NamePlateStringField field, bool isFreeCompany
if (this.TextWrap is { Item1: var left, Item2: var right })
{
sb.Append(left);
sb.Append(this.Text ?? handler.GetFieldAsSeString(field));
sb.Append(this.Text ?? this.GetStrippedField(handler));
sb.Append(right);
}
else
{
sb.Append(this.Text ?? handler.GetFieldAsSeString(field));
sb.Append(this.Text ?? this.GetStrippedField(handler));
}
if (this.RightQuote is not null)
@ -70,4 +70,12 @@ public class NamePlateQuotedParts(NamePlateStringField field, bool isFreeCompany
handler.SetField(field, sb.Build());
}
private SeString GetStrippedField(NamePlateUpdateHandler handler)
{
return SeString.Parse(
isFreeCompany
? NamePlateGui.StripFreeCompanyTagQuotes(handler.GetFieldAsSpan(field))
: NamePlateGui.StripTitleQuotes(handler.GetFieldAsSpan(field)));
}
}