Convert PartyMember to readonly struct

This commit is contained in:
Haselnussbomber 2025-11-13 18:53:03 +01:00
parent d1dc81318a
commit 53b94caeb7
No known key found for this signature in database
GPG key ID: BB905BB49E7295D1
2 changed files with 62 additions and 48 deletions

View file

@ -8,6 +8,7 @@ using Dalamud.IoC.Internal;
using Dalamud.Plugin.Services;
using CSGroupManager = FFXIVClientStructs.FFXIV.Client.Game.Group.GroupManager;
using CSPartyMember = FFXIVClientStructs.FFXIV.Client.Game.Group.PartyMember;
namespace Dalamud.Game.ClientState.Party;
@ -42,20 +43,20 @@ internal sealed unsafe partial class PartyList : IServiceType, IPartyList
public bool IsAlliance => this.GroupManagerStruct->MainGroup.AllianceFlags > 0;
/// <inheritdoc/>
public unsafe IntPtr GroupManagerAddress => (nint)CSGroupManager.Instance();
public unsafe nint GroupManagerAddress => (nint)CSGroupManager.Instance();
/// <inheritdoc/>
public IntPtr GroupListAddress => (IntPtr)Unsafe.AsPointer(ref GroupManagerStruct->MainGroup.PartyMembers[0]);
public nint GroupListAddress => (nint)Unsafe.AsPointer(ref GroupManagerStruct->MainGroup.PartyMembers[0]);
/// <inheritdoc/>
public IntPtr AllianceListAddress => (IntPtr)Unsafe.AsPointer(ref this.GroupManagerStruct->MainGroup.AllianceMembers[0]);
public nint AllianceListAddress => (nint)Unsafe.AsPointer(ref this.GroupManagerStruct->MainGroup.AllianceMembers[0]);
/// <inheritdoc/>
public long PartyId => this.GroupManagerStruct->MainGroup.PartyId;
private static int PartyMemberSize { get; } = Marshal.SizeOf<FFXIVClientStructs.FFXIV.Client.Game.Group.PartyMember>();
private static int PartyMemberSize { get; } = Marshal.SizeOf<CSPartyMember>();
private FFXIVClientStructs.FFXIV.Client.Game.Group.GroupManager* GroupManagerStruct => (FFXIVClientStructs.FFXIV.Client.Game.Group.GroupManager*)this.GroupManagerAddress;
private CSGroupManager* GroupManagerStruct => (CSGroupManager*)this.GroupManagerAddress;
/// <inheritdoc/>
public IPartyMember? this[int index]
@ -80,45 +81,45 @@ internal sealed unsafe partial class PartyList : IServiceType, IPartyList
}
/// <inheritdoc/>
public IntPtr GetPartyMemberAddress(int index)
public nint GetPartyMemberAddress(int index)
{
if (index < 0 || index >= GroupLength)
return IntPtr.Zero;
return 0;
return this.GroupListAddress + (index * PartyMemberSize);
}
/// <inheritdoc/>
public IPartyMember? CreatePartyMemberReference(IntPtr address)
public IPartyMember? CreatePartyMemberReference(nint address)
{
if (this.clientState.LocalContentId == 0)
return null;
if (address == IntPtr.Zero)
if (address == 0)
return null;
return new PartyMember(address);
return new PartyMember((CSPartyMember*)address);
}
/// <inheritdoc/>
public IntPtr GetAllianceMemberAddress(int index)
public nint GetAllianceMemberAddress(int index)
{
if (index < 0 || index >= AllianceLength)
return IntPtr.Zero;
return 0;
return this.AllianceListAddress + (index * PartyMemberSize);
}
/// <inheritdoc/>
public IPartyMember? CreateAllianceMemberReference(IntPtr address)
public IPartyMember? CreateAllianceMemberReference(nint address)
{
if (this.clientState.LocalContentId == 0)
return null;
if (address == IntPtr.Zero)
if (address == 0)
return null;
return new PartyMember(address);
return new PartyMember((CSPartyMember*)address);
}
}

View file

@ -1,26 +1,27 @@
using System.Diagnostics.CodeAnalysis;
using System.Numerics;
using System.Runtime.CompilerServices;
using Dalamud.Data;
using Dalamud.Game.ClientState.Objects;
using Dalamud.Game.ClientState.Objects.Types;
using Dalamud.Game.ClientState.Statuses;
using Dalamud.Game.Text.SeStringHandling;
using Dalamud.Memory;
using Lumina.Excel;
using CSPartyMember = FFXIVClientStructs.FFXIV.Client.Game.Group.PartyMember;
namespace Dalamud.Game.ClientState.Party;
/// <summary>
/// Interface representing a party member.
/// </summary>
public interface IPartyMember
public interface IPartyMember : IEquatable<IPartyMember>
{
/// <summary>
/// Gets the address of this party member in memory.
/// </summary>
IntPtr Address { get; }
nint Address { get; }
/// <summary>
/// Gets a list of buffs or debuffs applied to this party member.
@ -108,69 +109,81 @@ public interface IPartyMember
}
/// <summary>
/// This class represents a party member in the group manager.
/// This struct represents a party member in the group manager.
/// </summary>
internal unsafe class PartyMember : IPartyMember
/// <param name="ptr">A pointer to the PartyMember.</param>
internal unsafe readonly struct PartyMember(CSPartyMember* ptr) : IPartyMember
{
/// <summary>
/// Initializes a new instance of the <see cref="PartyMember"/> class.
/// </summary>
/// <param name="address">Address of the party member.</param>
internal PartyMember(IntPtr address)
{
this.Address = address;
}
/// <inheritdoc/>
public nint Address => (nint)ptr;
/// <inheritdoc/>
public IntPtr Address { get; }
public StatusList Statuses => new(&ptr->StatusManager);
/// <inheritdoc/>
public StatusList Statuses => new(&this.Struct->StatusManager);
public Vector3 Position => ptr->Position;
/// <inheritdoc/>
public Vector3 Position => this.Struct->Position;
public long ContentId => (long)ptr->ContentId;
/// <inheritdoc/>
public long ContentId => (long)this.Struct->ContentId;
public uint ObjectId => ptr->EntityId;
/// <inheritdoc/>
public uint ObjectId => this.Struct->EntityId;
/// <inheritdoc/>
public uint EntityId => this.Struct->EntityId;
public uint EntityId => ptr->EntityId;
/// <inheritdoc/>
public IGameObject? GameObject => Service<ObjectTable>.Get().SearchById(this.EntityId);
/// <inheritdoc/>
public uint CurrentHP => this.Struct->CurrentHP;
public uint CurrentHP => ptr->CurrentHP;
/// <inheritdoc/>
public uint MaxHP => this.Struct->MaxHP;
public uint MaxHP => ptr->MaxHP;
/// <inheritdoc/>
public ushort CurrentMP => this.Struct->CurrentMP;
public ushort CurrentMP => ptr->CurrentMP;
/// <inheritdoc/>
public ushort MaxMP => this.Struct->MaxMP;
public ushort MaxMP => ptr->MaxMP;
/// <inheritdoc/>
public RowRef<Lumina.Excel.Sheets.TerritoryType> Territory => LuminaUtils.CreateRef<Lumina.Excel.Sheets.TerritoryType>(this.Struct->TerritoryType);
public RowRef<Lumina.Excel.Sheets.TerritoryType> Territory => LuminaUtils.CreateRef<Lumina.Excel.Sheets.TerritoryType>(ptr->TerritoryType);
/// <inheritdoc/>
public RowRef<Lumina.Excel.Sheets.World> World => LuminaUtils.CreateRef<Lumina.Excel.Sheets.World>(this.Struct->HomeWorld);
public RowRef<Lumina.Excel.Sheets.World> World => LuminaUtils.CreateRef<Lumina.Excel.Sheets.World>(ptr->HomeWorld);
/// <inheritdoc/>
public SeString Name => SeString.Parse(this.Struct->Name);
public SeString Name => SeString.Parse(ptr->Name);
/// <inheritdoc/>
public byte Sex => this.Struct->Sex;
public byte Sex => ptr->Sex;
/// <inheritdoc/>
public RowRef<Lumina.Excel.Sheets.ClassJob> ClassJob => LuminaUtils.CreateRef<Lumina.Excel.Sheets.ClassJob>(this.Struct->ClassJob);
public RowRef<Lumina.Excel.Sheets.ClassJob> ClassJob => LuminaUtils.CreateRef<Lumina.Excel.Sheets.ClassJob>(ptr->ClassJob);
/// <inheritdoc/>
public byte Level => this.Struct->Level;
public byte Level => ptr->Level;
private FFXIVClientStructs.FFXIV.Client.Game.Group.PartyMember* Struct => (FFXIVClientStructs.FFXIV.Client.Game.Group.PartyMember*)this.Address;
public static bool operator ==(PartyMember x, PartyMember y) => x.Equals(y);
public static bool operator !=(PartyMember x, PartyMember y) => !(x == y);
/// <inheritdoc/>
public bool Equals(IPartyMember? other)
{
return this.EntityId == other.EntityId;
}
/// <inheritdoc/>
public override bool Equals([NotNullWhen(true)] object? obj)
{
return obj is PartyMember fate && this.Equals(fate);
}
/// <inheritdoc/>
public override int GetHashCode()
{
return this.EntityId.GetHashCode();
}
}