mirror of
https://github.com/goatcorp/Dalamud.git
synced 2026-02-15 12:27:43 +01:00
Convert Fate to readonly struct
This commit is contained in:
parent
d1bed3ebc5
commit
a48eead85e
2 changed files with 59 additions and 93 deletions
|
|
@ -1,3 +1,4 @@
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
|
|
||||||
using Dalamud.Data;
|
using Dalamud.Data;
|
||||||
|
|
@ -6,10 +7,12 @@ using Dalamud.Memory;
|
||||||
|
|
||||||
using Lumina.Excel;
|
using Lumina.Excel;
|
||||||
|
|
||||||
|
using CSFateContext = FFXIVClientStructs.FFXIV.Client.Game.Fate.FateContext;
|
||||||
|
|
||||||
namespace Dalamud.Game.ClientState.Fates;
|
namespace Dalamud.Game.ClientState.Fates;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Interface representing an fate entry that can be seen in the current area.
|
/// Interface representing a fate entry that can be seen in the current area.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IFate : IEquatable<IFate>
|
public interface IFate : IEquatable<IFate>
|
||||||
{
|
{
|
||||||
|
|
@ -111,133 +114,96 @@ public interface IFate : IEquatable<IFate>
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the address of this Fate in memory.
|
/// Gets the address of this Fate in memory.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
IntPtr Address { get; }
|
nint Address { get; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// This class represents an FFXIV Fate.
|
/// This struct represents a Fate.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal unsafe partial class Fate
|
/// <param name="ptr">A pointer to the FateContext.</param>
|
||||||
|
internal readonly unsafe struct Fate(CSFateContext* ptr) : IFate
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of the <see cref="Fate"/> class.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="address">The address of this fate in memory.</param>
|
|
||||||
internal Fate(IntPtr address)
|
|
||||||
{
|
|
||||||
this.Address = address;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public IntPtr Address { get; }
|
public nint Address => (nint)ptr;
|
||||||
|
|
||||||
private FFXIVClientStructs.FFXIV.Client.Game.Fate.FateContext* Struct => (FFXIVClientStructs.FFXIV.Client.Game.Fate.FateContext*)this.Address;
|
|
||||||
|
|
||||||
public static bool operator ==(Fate fate1, Fate fate2)
|
|
||||||
{
|
|
||||||
if (fate1 is null || fate2 is null)
|
|
||||||
return Equals(fate1, fate2);
|
|
||||||
|
|
||||||
return fate1.Equals(fate2);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool operator !=(Fate fate1, Fate fate2) => !(fate1 == fate2);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets a value indicating whether this Fate is still valid in memory.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="fate">The fate to check.</param>
|
|
||||||
/// <returns>True or false.</returns>
|
|
||||||
public static bool IsValid(Fate fate)
|
|
||||||
{
|
|
||||||
var clientState = Service<ClientState>.GetNullable();
|
|
||||||
|
|
||||||
if (fate == null || clientState == null)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (clientState.LocalContentId == 0)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets a value indicating whether this actor is still valid in memory.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>True or false.</returns>
|
|
||||||
public bool IsValid() => IsValid(this);
|
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
bool IEquatable<IFate>.Equals(IFate other) => this.FateId == other?.FateId;
|
public ushort FateId => ptr->FateId;
|
||||||
|
|
||||||
/// <inheritdoc/>
|
|
||||||
public override bool Equals(object obj) => ((IEquatable<IFate>)this).Equals(obj as IFate);
|
|
||||||
|
|
||||||
/// <inheritdoc/>
|
|
||||||
public override int GetHashCode() => this.FateId.GetHashCode();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// This class represents an FFXIV Fate.
|
|
||||||
/// </summary>
|
|
||||||
internal unsafe partial class Fate : IFate
|
|
||||||
{
|
|
||||||
/// <inheritdoc/>
|
|
||||||
public ushort FateId => this.Struct->FateId;
|
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public RowRef<Lumina.Excel.Sheets.Fate> GameData => LuminaUtils.CreateRef<Lumina.Excel.Sheets.Fate>(this.FateId);
|
public RowRef<Lumina.Excel.Sheets.Fate> GameData => LuminaUtils.CreateRef<Lumina.Excel.Sheets.Fate>(this.FateId);
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public int StartTimeEpoch => this.Struct->StartTimeEpoch;
|
public int StartTimeEpoch => ptr->StartTimeEpoch;
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public short Duration => this.Struct->Duration;
|
public short Duration => ptr->Duration;
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public long TimeRemaining => this.StartTimeEpoch + this.Duration - DateTimeOffset.Now.ToUnixTimeSeconds();
|
public long TimeRemaining => this.StartTimeEpoch + this.Duration - DateTimeOffset.Now.ToUnixTimeSeconds();
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public SeString Name => MemoryHelper.ReadSeString(&this.Struct->Name);
|
public SeString Name => MemoryHelper.ReadSeString(&ptr->Name);
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public SeString Description => MemoryHelper.ReadSeString(&this.Struct->Description);
|
public SeString Description => MemoryHelper.ReadSeString(&ptr->Description);
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public SeString Objective => MemoryHelper.ReadSeString(&this.Struct->Objective);
|
public SeString Objective => MemoryHelper.ReadSeString(&ptr->Objective);
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public FateState State => (FateState)this.Struct->State;
|
public FateState State => (FateState)ptr->State;
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public byte HandInCount => this.Struct->HandInCount;
|
public byte HandInCount => ptr->HandInCount;
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public byte Progress => this.Struct->Progress;
|
public byte Progress => ptr->Progress;
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public bool HasBonus => this.Struct->IsBonus;
|
public bool HasBonus => ptr->IsBonus;
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public uint IconId => this.Struct->IconId;
|
public uint IconId => ptr->IconId;
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public byte Level => this.Struct->Level;
|
public byte Level => ptr->Level;
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public byte MaxLevel => this.Struct->MaxLevel;
|
public byte MaxLevel => ptr->MaxLevel;
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public Vector3 Position => this.Struct->Location;
|
public Vector3 Position => ptr->Location;
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public float Radius => this.Struct->Radius;
|
public float Radius => ptr->Radius;
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public uint MapIconId => this.Struct->MapIconId;
|
public uint MapIconId => ptr->MapIconId;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the territory this <see cref="Fate"/> is located in.
|
/// Gets the territory this <see cref="Fate"/> is located in.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public RowRef<Lumina.Excel.Sheets.TerritoryType> TerritoryType => LuminaUtils.CreateRef<Lumina.Excel.Sheets.TerritoryType>(this.Struct->MapMarkers[0].MapMarkerData.TerritoryTypeId);
|
public RowRef<Lumina.Excel.Sheets.TerritoryType> TerritoryType => LuminaUtils.CreateRef<Lumina.Excel.Sheets.TerritoryType>(ptr->MapMarkers[0].MapMarkerData.TerritoryTypeId);
|
||||||
|
|
||||||
|
public static bool operator ==(Fate x, Fate y) => x.Equals(y);
|
||||||
|
|
||||||
|
public static bool operator !=(Fate x, Fate y) => !(x == y);
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public bool Equals(IFate? other)
|
||||||
|
{
|
||||||
|
return this.FateId == other.FateId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public override bool Equals([NotNullWhen(true)] object? obj)
|
||||||
|
{
|
||||||
|
return obj is Fate fate && this.Equals(fate);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public override int GetHashCode()
|
||||||
|
{
|
||||||
|
return this.FateId.GetHashCode();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ using Dalamud.IoC;
|
||||||
using Dalamud.IoC.Internal;
|
using Dalamud.IoC.Internal;
|
||||||
using Dalamud.Plugin.Services;
|
using Dalamud.Plugin.Services;
|
||||||
|
|
||||||
|
using CSFateContext = FFXIVClientStructs.FFXIV.Client.Game.Fate.FateContext;
|
||||||
using CSFateManager = FFXIVClientStructs.FFXIV.Client.Game.Fate.FateManager;
|
using CSFateManager = FFXIVClientStructs.FFXIV.Client.Game.Fate.FateManager;
|
||||||
|
|
||||||
namespace Dalamud.Game.ClientState.Fates;
|
namespace Dalamud.Game.ClientState.Fates;
|
||||||
|
|
@ -25,7 +26,7 @@ internal sealed partial class FateTable : IServiceType, IFateTable
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public unsafe IntPtr Address => (nint)CSFateManager.Instance();
|
public unsafe nint Address => (nint)CSFateManager.Instance();
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public unsafe int Length
|
public unsafe int Length
|
||||||
|
|
@ -72,30 +73,29 @@ internal sealed partial class FateTable : IServiceType, IFateTable
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public unsafe IntPtr GetFateAddress(int index)
|
public unsafe nint GetFateAddress(int index)
|
||||||
{
|
{
|
||||||
if (index >= this.Length)
|
if (index >= this.Length)
|
||||||
return IntPtr.Zero;
|
return 0;
|
||||||
|
|
||||||
var fateManager = CSFateManager.Instance();
|
var fateManager = CSFateManager.Instance();
|
||||||
if (fateManager == null)
|
if (fateManager == null)
|
||||||
return IntPtr.Zero;
|
return 0;
|
||||||
|
|
||||||
return (IntPtr)fateManager->Fates[index].Value;
|
return (nint)fateManager->Fates[index].Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public IFate? CreateFateReference(IntPtr offset)
|
public unsafe IFate? CreateFateReference(IntPtr address)
|
||||||
{
|
{
|
||||||
var clientState = Service<ClientState>.Get();
|
if (address == 0)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
var clientState = Service<ClientState>.Get();
|
||||||
if (clientState.LocalContentId == 0)
|
if (clientState.LocalContentId == 0)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
if (offset == IntPtr.Zero)
|
return new Fate((CSFateContext*)address);
|
||||||
return null;
|
|
||||||
|
|
||||||
return new Fate(offset);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue