Merge pull request #375 from daemitus/FixActorName

The ultimate fix (for actor names)
This commit is contained in:
goaaats 2021-06-16 14:50:36 +02:00 committed by GitHub
commit 249e490c1e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 4 deletions

View file

@ -1,6 +1,7 @@
using System;
using System.Text;
using Dalamud.Game.ClientState.Structs;
using Serilog;
namespace Dalamud.Game.ClientState.Actors.Types
{
@ -12,6 +13,8 @@ namespace Dalamud.Game.ClientState.Actors.Types
private readonly Structs.Actor actorStruct;
private readonly Dalamud dalamud;
private string name;
/// <summary>
/// Initializes a new instance of the <see cref="Actor"/> class.
/// This represents a basic FFXIV actor.
@ -40,7 +43,7 @@ namespace Dalamud.Game.ClientState.Actors.Types
/// <summary>
/// Gets the displayname of this <see cref="Actor" />.
/// </summary>
public string Name => this.ActorStruct.Name;
public string Name => this.name ??= Util.GetUTF8String(this.actorStruct.Name);
/// <summary>
/// Gets the actor ID of this <see cref="Actor" />.

View file

@ -14,8 +14,8 @@ namespace Dalamud.Game.ClientState.Structs
/// The actor name.
/// </summary>
[FieldOffset(ActorOffsets.Name)]
[MarshalAs(UnmanagedType.LPUTF8Str, SizeConst = 30)]
public string Name;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]
public byte[] Name;
/// <summary>
/// The actor's internal id.

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++; }
}
}