Such utility, so many wows

This commit is contained in:
Raymond Lynch 2021-06-16 08:42:42 -04:00
parent ba8780e047
commit 98781f0bcd
2 changed files with 32 additions and 27 deletions

View file

@ -43,33 +43,7 @@ namespace Dalamud.Game.ClientState.Actors.Types
/// <summary>
/// Gets the displayname of this <see cref="Actor" />.
/// </summary>
public string Name
{
get
{
if (this.name == null)
{
var i = 0;
for (; i < this.actorStruct.Name.Length; i++)
{
if (this.actorStruct.Name[i] == 0)
break;
}
if (i == this.actorStruct.Name.Length)
{
this.name = Encoding.UTF8.GetString(this.actorStruct.Name);
Log.Warning($"Warning: Actor name exceeds underlying struct array length (name={this.name})");
}
else
{
this.name = Encoding.UTF8.GetString(this.actorStruct.Name, 0, i);
}
}
return this.name;
}
}
public string Name => this.name ??= Util.GetUTF8String(this.actorStruct.Name);
/// <summary>
/// Gets the actor ID of this <see cref="Actor" />.

View file

@ -165,5 +165,36 @@ namespace Dalamud
NativeFunctions.MessageBox(Process.GetCurrentProcess().MainWindowHandle, message, caption, flags);
Environment.Exit(-1);
}
/// <summary>
/// Retrieve a UTF8 string from a null terminated byte array.
/// </summary>
/// <param name="array">A null terminated UTF8 byte array.</param>
/// <returns>A UTF8 encoded string.</returns>
public static string GetUTF8String(byte[] array)
{
var count = 0;
for (; count < array.Length; count++)
{
if (array[count] == 0)
break;
}
string text;
if (count == array.Length)
{
text = Encoding.UTF8.GetString(array);
Log.Warning($"Warning: text exceeds underlying array length ({text})");
}
else
{
text = Encoding.UTF8.GetString(array, 0, count);
}
return text;
}
// TODO: Someone implement GetUTF8String with some IntPtr overloads.
// while(Marshal.ReadByte(0, sz) != 0) { sz++; }
}
}