[Api13] Add native wrapper structs (#2330)

This commit is contained in:
Haselnussbomber 2025-08-04 02:43:52 +02:00 committed by GitHub
parent b425ee0a2a
commit 57c6089fc1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 682 additions and 149 deletions

View file

@ -0,0 +1,96 @@
using System.Runtime.InteropServices;
using FFXIVClientStructs.FFXIV.Client.UI;
using FFXIVClientStructs.FFXIV.Client.UI.Agent;
namespace Dalamud.Game.NativeWrapper;
/// <summary>
/// A readonly wrapper for AgentInterface.
/// </summary>
/// <param name="address">The address to the AgentInterface.</param>
[StructLayout(LayoutKind.Explicit, Size = 0x08)]
public readonly unsafe struct AgentInterfacePtr(nint address) : IEquatable<AgentInterfacePtr>
{
/// <summary>
/// The address to the AgentInterface.
/// </summary>
[FieldOffset(0x00)]
public readonly nint Address = address;
/// <summary>
/// Gets a value indicating whether the underlying pointer is a nullptr.
/// </summary>
public readonly bool IsNull => this.Address == 0;
/// <summary>
/// Gets a value indicating whether the agents addon is visible.
/// </summary>
public readonly AtkUnitBasePtr Addon
{
get
{
if (this.IsNull)
return 0;
var raptureAtkUnitManager = RaptureAtkUnitManager.Instance();
if (raptureAtkUnitManager == null)
return 0;
return (nint)raptureAtkUnitManager->GetAddonById(this.AddonId);
}
}
/// <summary>
/// Gets a value indicating whether the agent is active.
/// </summary>
public readonly ushort AddonId => (ushort)(this.IsNull ? 0 : this.Struct->GetAddonId());
/// <summary>
/// Gets a value indicating whether the agent is active.
/// </summary>
public readonly bool IsAgentActive => !this.IsNull && this.Struct->IsAgentActive();
/// <summary>
/// Gets a value indicating whether the agents addon is ready.
/// </summary>
public readonly bool IsAddonReady => !this.IsNull && this.Struct->IsAddonReady();
/// <summary>
/// Gets a value indicating whether the agents addon is visible.
/// </summary>
public readonly bool IsAddonShown => !this.IsNull && this.Struct->IsAddonShown();
/// <summary>
/// Gets the AgentInterface*.
/// </summary>
/// <remarks> Internal use only. </remarks>
internal readonly AgentInterface* Struct => (AgentInterface*)this.Address;
public static implicit operator nint(AgentInterfacePtr wrapper) => wrapper.Address;
public static implicit operator AgentInterfacePtr(nint address) => new(address);
public static implicit operator AgentInterfacePtr(void* ptr) => new((nint)ptr);
public static bool operator ==(AgentInterfacePtr left, AgentInterfacePtr right) => left.Address == right.Address;
public static bool operator !=(AgentInterfacePtr left, AgentInterfacePtr right) => left.Address != right.Address;
/// <summary>
/// Focuses the AtkUnitBase.
/// </summary>
/// <returns> <c>true</c> when the addon was focused, <c>false</c> otherwise. </returns>
public readonly bool FocusAddon() => this.IsNull && this.Struct->FocusAddon();
/// <summary>Determines whether the specified AgentInterfacePtr is equal to the current AgentInterfacePtr.</summary>
/// <param name="other">The AgentInterfacePtr to compare with the current AgentInterfacePtr.</param>
/// <returns><c>true</c> if the specified AgentInterfacePtr is equal to the current AgentInterfacePtr; otherwise, <c>false</c>.</returns>
public readonly bool Equals(AgentInterfacePtr other) => this.Address == other.Address;
/// <inheritdoc cref="object.Equals(object?)"/>
public override readonly bool Equals(object obj) => obj is AgentInterfacePtr wrapper && this.Equals(wrapper);
/// <inheritdoc cref="object.GetHashCode()"/>
public override readonly int GetHashCode() => ((nuint)this.Address).GetHashCode();
}

View file

@ -0,0 +1,171 @@
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.InteropServices;
using FFXIVClientStructs.FFXIV.Component.GUI;
using FFXIVClientStructs.Interop;
namespace Dalamud.Game.NativeWrapper;
/// <summary>
/// A readonly wrapper for AtkUnitBase.
/// </summary>
/// <param name="address">The address to the AtkUnitBase.</param>
[StructLayout(LayoutKind.Explicit, Size = 0x08)]
public readonly unsafe struct AtkUnitBasePtr(nint address) : IEquatable<AtkUnitBasePtr>
{
/// <summary>
/// The address to the AtkUnitBase.
/// </summary>
[FieldOffset(0x00)]
public readonly nint Address = address;
/// <summary>
/// Gets a value indicating whether the underlying pointer is a nullptr.
/// </summary>
public readonly bool IsNull => this.Address == 0;
/// <summary>
/// Gets a value indicating whether the OnSetup function has been called.
/// </summary>
public readonly bool IsReady => !this.IsNull && this.Struct->IsReady;
/// <summary>
/// Gets a value indicating whether the AtkUnitBase is visible.
/// </summary>
public readonly bool IsVisible => !this.IsNull && this.Struct->IsVisible;
/// <summary>
/// Gets the name.
/// </summary>
public readonly string Name => this.IsNull ? string.Empty : this.Struct->NameString;
/// <summary>
/// Gets the id.
/// </summary>
public readonly ushort Id => this.IsNull ? (ushort)0 : this.Struct->Id;
/// <summary>
/// Gets the parent id.
/// </summary>
public readonly ushort ParentId => this.IsNull ? (ushort)0 : this.Struct->ParentId;
/// <summary>
/// Gets the host id.
/// </summary>
public readonly ushort HostId => this.IsNull ? (ushort)0 : this.Struct->HostId;
/// <summary>
/// Gets the scale.
/// </summary>
public readonly float Scale => this.IsNull ? 0f : this.Struct->Scale;
/// <summary>
/// Gets the x-position.
/// </summary>
public readonly short X => this.IsNull ? (short)0 : this.Struct->X;
/// <summary>
/// Gets the y-position.
/// </summary>
public readonly short Y => this.IsNull ? (short)0 : this.Struct->Y;
/// <summary>
/// Gets the width.
/// </summary>
public readonly float Width => this.IsNull ? 0f : this.Struct->GetScaledWidth(false);
/// <summary>
/// Gets the height.
/// </summary>
public readonly float Height => this.IsNull ? 0f : this.Struct->GetScaledHeight(false);
/// <summary>
/// Gets the scaled width.
/// </summary>
public readonly float ScaledWidth => this.IsNull ? 0f : this.Struct->GetScaledWidth(true);
/// <summary>
/// Gets the scaled height.
/// </summary>
public readonly float ScaledHeight => this.IsNull ? 0f : this.Struct->GetScaledHeight(true);
/// <summary>
/// Gets the position.
/// </summary>
public readonly Vector2 Position => new(this.X, this.Y);
/// <summary>
/// Gets the size.
/// </summary>
public readonly Vector2 Size => new(this.Width, this.Height);
/// <summary>
/// Gets the scaled size.
/// </summary>
public readonly Vector2 ScaledSize => new(this.ScaledWidth, this.ScaledHeight);
/// <summary>
/// Gets the number of <see cref="AtkValue"/> entries.
/// </summary>
public readonly int AtkValuesCount => this.Struct->AtkValuesCount;
/// <summary>
/// Gets an enumerable collection of <see cref="AtkValuePtr"/> of the addons current AtkValues.
/// </summary>
/// <returns>
/// An <see cref="IEnumerable{T}"/> of <see cref="AtkValuePtr"/> corresponding to the addons AtkValues.
/// </returns>
public IEnumerable<AtkValuePtr> AtkValues
{
get
{
for (var i = 0; i < this.AtkValuesCount; i++)
{
AtkValuePtr ptr;
unsafe
{
ptr = new AtkValuePtr((nint)this.Struct->AtkValuesSpan.GetPointer(i));
}
yield return ptr;
}
}
}
/// <summary>
/// Gets the AtkUnitBase*.
/// </summary>
/// <remarks> Internal use only. </remarks>
internal readonly AtkUnitBase* Struct => (AtkUnitBase*)this.Address;
public static implicit operator nint(AtkUnitBasePtr wrapper) => wrapper.Address;
public static implicit operator AtkUnitBasePtr(nint address) => new(address);
public static implicit operator AtkUnitBasePtr(void* ptr) => new((nint)ptr);
public static bool operator ==(AtkUnitBasePtr left, AtkUnitBasePtr right) => left.Address == right.Address;
public static bool operator !=(AtkUnitBasePtr left, AtkUnitBasePtr right) => left.Address != right.Address;
/// <summary>
/// Focuses the AtkUnitBase.
/// </summary>
public readonly void Focus()
{
if (!this.IsNull)
this.Struct->Focus();
}
/// <summary>Determines whether the specified AtkUnitBasePtr is equal to the current AtkUnitBasePtr.</summary>
/// <param name="other">The AtkUnitBasePtr to compare with the current AtkUnitBasePtr.</param>
/// <returns><c>true</c> if the specified AtkUnitBasePtr is equal to the current AtkUnitBasePtr; otherwise, <c>false</c>.</returns>
public readonly bool Equals(AtkUnitBasePtr other) => this.Address == other.Address;
/// <inheritdoc cref="object.Equals(object?)"/>
public override readonly bool Equals(object obj) => obj is AtkUnitBasePtr wrapper && this.Equals(wrapper);
/// <inheritdoc cref="object.GetHashCode()"/>
public override readonly int GetHashCode() => ((nuint)this.Address).GetHashCode();
}

View file

@ -0,0 +1,113 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using Dalamud.Utility;
using FFXIVClientStructs.FFXIV.Component.GUI;
namespace Dalamud.Game.NativeWrapper;
/// <summary>
/// A readonly wrapper for AtkValue.
/// </summary>
/// <param name="address">The address to the AtkValue.</param>
[StructLayout(LayoutKind.Explicit, Size = 0x08)]
public readonly unsafe struct AtkValuePtr(nint address) : IEquatable<AtkValuePtr>
{
/// <summary>
/// The address to the AtkValue.
/// </summary>
[FieldOffset(0x00)]
public readonly nint Address = address;
/// <summary>
/// Gets a value indicating whether the underlying pointer is a nullptr.
/// </summary>
public readonly bool IsNull => this.Address == 0;
/// <summary>
/// Gets the value type.
/// </summary>
public readonly AtkValueType ValueType => (AtkValueType)this.Struct->Type;
/// <summary>
/// Gets the AtkValue*.
/// </summary>
/// <remarks> Internal use only. </remarks>
internal readonly AtkValue* Struct => (AtkValue*)this.Address;
public static implicit operator nint(AtkValuePtr wrapper) => wrapper.Address;
public static implicit operator AtkValuePtr(nint address) => new(address);
public static implicit operator AtkValuePtr(void* ptr) => new((nint)ptr);
public static bool operator ==(AtkValuePtr left, AtkValuePtr right) => left.Address == right.Address;
public static bool operator !=(AtkValuePtr left, AtkValuePtr right) => left.Address != right.Address;
/// <summary>
/// Gets the value of the underlying <see cref="AtkValue"/> as a boxed object, based on its <see cref="AtkValueType"/>.
/// </summary>
/// <returns>
/// The boxed value represented by this <see cref="AtkValuePtr"/>, or <c>null</c> if the value is null or undefined.
/// </returns>
/// <exception cref="NotImplementedException">
/// Thrown if the value type is not currently handled by this implementation.
/// </exception>
public unsafe object? GetValue()
{
if (this.Struct == null)
return null;
return this.ValueType switch
{
AtkValueType.Undefined or AtkValueType.Null => null,
AtkValueType.Bool => this.Struct->Bool,
AtkValueType.Int => this.Struct->Int,
AtkValueType.Int64 => this.Struct->Int64,
AtkValueType.UInt => this.Struct->UInt,
AtkValueType.UInt64 => this.Struct->UInt64,
AtkValueType.Float => this.Struct->Float,
AtkValueType.String or AtkValueType.String8 or AtkValueType.ManagedString => this.Struct->String == null ? default : this.Struct->String.AsReadOnlySeString(),
AtkValueType.WideString => this.Struct->WideString == null ? string.Empty : new string(this.Struct->WideString),
AtkValueType.Pointer => (nint)this.Struct->Pointer,
_ => throw new NotImplementedException($"AtkValueType {this.ValueType} is currently not supported"),
};
}
/// <summary>
/// Attempts to retrieve the value as a strongly-typed object if the underlying type matches.
/// </summary>
/// <typeparam name="T">The expected value type to extract.</typeparam>
/// <param name="result">
/// When this method returns <c>true</c>, contains the extracted value of type <typeparamref name="T"/>.
/// Otherwise, contains the default value of type <typeparamref name="T"/>.
/// </param>
/// <returns>
/// <c>true</c> if the value was successfully extracted and matched <typeparamref name="T"/>; otherwise, <c>false</c>.
/// </returns>
public unsafe bool TryGet<T>([NotNullWhen(true)] out T? result) where T : struct
{
object? value = this.GetValue();
if (value is T typed)
{
result = typed;
return true;
}
result = default;
return false;
}
/// <summary>Determines whether the specified AtkValuePtr is equal to the current AtkValuePtr.</summary>
/// <param name="other">The AtkValuePtr to compare with the current AtkValuePtr.</param>
/// <returns><c>true</c> if the specified AtkValuePtr is equal to the current AtkValuePtr; otherwise, <c>false</c>.</returns>
public readonly bool Equals(AtkValuePtr other) => this.Address == other.Address || this.Struct->EqualTo(other.Struct);
/// <inheritdoc cref="object.Equals(object?)"/>
public override readonly bool Equals(object obj) => obj is AtkValuePtr wrapper && this.Equals(wrapper);
/// <inheritdoc cref="object.GetHashCode()"/>
public override readonly int GetHashCode() => ((nuint)this.Address).GetHashCode();
}

View file

@ -0,0 +1,87 @@
namespace Dalamud.Game.NativeWrapper;
/// <summary>
/// Represents the data type of the AtkValue.
/// </summary>
public enum AtkValueType
{
/// <summary>
/// The value is undefined or invalid.
/// </summary>
Undefined = 0,
/// <summary>
/// The value is null.
/// </summary>
Null = 0x1,
/// <summary>
/// The value is a boolean.
/// </summary>
Bool = 0x2,
/// <summary>
/// The value is a 32-bit signed integer.
/// </summary>
Int = 0x3,
/// <summary>
/// The value is a 64-bit signed integer.
/// </summary>
Int64 = 0x4,
/// <summary>
/// The value is a 32-bit unsigned integer.
/// </summary>
UInt = 0x5,
/// <summary>
/// The value is a 64-bit unsigned integer.
/// </summary>
UInt64 = 0x6,
/// <summary>
/// The value is a 32-bit floating-point number.
/// </summary>
Float = 0x7,
/// <summary>
/// The value points to a null-terminated 8-bit character string (ASCII or UTF-8).
/// </summary>
String = 0x8,
/// <summary>
/// The value points to a null-terminated 16-bit character string (UTF-16 / wide string).
/// </summary>
WideString = 0x9,
/// <summary>
/// The value points to a constant null-terminated 8-bit character string (const char*).
/// </summary>
String8 = 0xA,
/// <summary>
/// The value is a vector.
/// </summary>
Vector = 0xB,
/// <summary>
/// The value is a pointer.
/// </summary>
Pointer = 0xC,
/// <summary>
/// The value is pointing to an array of AtkValue entries.
/// </summary>
AtkValues = 0xD,
/// <summary>
/// The value is a managed string. See <see cref="String"/>.
/// </summary>
ManagedString = 0x28,
/// <summary>
/// The value is a managed vector. See <see cref="Vector"/>.
/// </summary>
ManagedVector = 0x2B,
}

View file

@ -0,0 +1,51 @@
using System.Runtime.InteropServices;
using FFXIVClientStructs.FFXIV.Client.UI;
namespace Dalamud.Game.NativeWrapper;
/// <summary>
/// A readonly wrapper for UIModule.
/// </summary>
/// <param name="address">The address to the UIModule.</param>
[StructLayout(LayoutKind.Explicit, Size = 0x08)]
public readonly unsafe struct UIModulePtr(nint address) : IEquatable<UIModulePtr>
{
/// <summary>
/// The address to the UIModule.
/// </summary>
[FieldOffset(0x00)]
public readonly nint Address = address;
/// <summary>
/// Gets a value indicating whether the underlying pointer is a nullptr.
/// </summary>
public readonly bool IsNull => this.Address == 0;
/// <summary>
/// Gets the UIModule*.
/// </summary>
/// <remarks> Internal use only. </remarks>
internal readonly UIModule* Struct => (UIModule*)this.Address;
public static implicit operator nint(UIModulePtr wrapper) => wrapper.Address;
public static implicit operator UIModulePtr(nint address) => new(address);
public static implicit operator UIModulePtr(void* ptr) => new((nint)ptr);
public static bool operator ==(UIModulePtr left, UIModulePtr right) => left.Address == right.Address;
public static bool operator !=(UIModulePtr left, UIModulePtr right) => left.Address != right.Address;
/// <summary>Determines whether the specified UIModulePtr is equal to the current UIModulePtr.</summary>
/// <param name="other">The UIModulePtr to compare with the current UIModulePtr.</param>
/// <returns><c>true</c> if the specified UIModulePtr is equal to the current UIModulePtr; otherwise, <c>false</c>.</returns>
public readonly bool Equals(UIModulePtr other) => this.Address == other.Address;
/// <inheritdoc cref="object.Equals(object?)"/>
public override readonly bool Equals(object obj) => obj is UIModulePtr wrapper && this.Equals(wrapper);
/// <inheritdoc cref="object.GetHashCode()"/>
public override readonly int GetHashCode() => ((nuint)this.Address).GetHashCode();
}