From 98781f0bcd252f9835b87023ee276769851de51a Mon Sep 17 00:00:00 2001 From: Raymond Lynch Date: Wed, 16 Jun 2021 08:42:42 -0400 Subject: [PATCH] Such utility, so many wows --- .../Game/ClientState/Actors/Types/Actor.cs | 28 +---------------- Dalamud/Util.cs | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/Dalamud/Game/ClientState/Actors/Types/Actor.cs b/Dalamud/Game/ClientState/Actors/Types/Actor.cs index b1476d9b7..9707f6674 100644 --- a/Dalamud/Game/ClientState/Actors/Types/Actor.cs +++ b/Dalamud/Game/ClientState/Actors/Types/Actor.cs @@ -43,33 +43,7 @@ namespace Dalamud.Game.ClientState.Actors.Types /// /// Gets the displayname of this . /// - 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); /// /// Gets the actor ID of this . diff --git a/Dalamud/Util.cs b/Dalamud/Util.cs index e851f87a0..456c93689 100644 --- a/Dalamud/Util.cs +++ b/Dalamud/Util.cs @@ -165,5 +165,36 @@ namespace Dalamud NativeFunctions.MessageBox(Process.GetCurrentProcess().MainWindowHandle, message, caption, flags); Environment.Exit(-1); } + + /// + /// Retrieve a UTF8 string from a null terminated byte array. + /// + /// A null terminated UTF8 byte array. + /// A UTF8 encoded string. + 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++; } } }