From 3c560268fc67cc17a79ee9931f02664d15340b97 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 25 Jul 2021 01:09:33 +0200 Subject: [PATCH] Split CharEquipment. --- Penumbra/Game/ActorEquip.cs | 27 ++++++++++ Penumbra/Game/CharEquipment.cs | 72 ++++++++++----------------- Penumbra/Game/ObjectIdentification.cs | 5 +- Penumbra/Game/SetId.cs | 23 +++++++++ Penumbra/Game/StainId.cs | 29 +++++++++++ Penumbra/Game/WeaponType.cs | 29 +++++++++++ 6 files changed, 136 insertions(+), 49 deletions(-) create mode 100644 Penumbra/Game/ActorEquip.cs create mode 100644 Penumbra/Game/SetId.cs create mode 100644 Penumbra/Game/StainId.cs create mode 100644 Penumbra/Game/WeaponType.cs diff --git a/Penumbra/Game/ActorEquip.cs b/Penumbra/Game/ActorEquip.cs new file mode 100644 index 00000000..6a8645ba --- /dev/null +++ b/Penumbra/Game/ActorEquip.cs @@ -0,0 +1,27 @@ +using System.Runtime.InteropServices; + +namespace Penumbra.Game +{ + [StructLayout( LayoutKind.Sequential, Pack = 1 )] + public readonly struct ActorEquip + { + public readonly SetId Set; + public readonly byte Variant; + public readonly StainId Stain; + + public override string ToString() + => $"{Set},{Variant},{Stain}"; + } + + [StructLayout( LayoutKind.Sequential, Pack = 1 )] + public readonly struct ActorWeapon + { + public readonly SetId Set; + public readonly WeaponType Type; + public readonly ushort Variant; + public readonly StainId Stain; + + public override string ToString() + => $"{Set},{Type},{Variant},{Stain}"; + } +} diff --git a/Penumbra/Game/CharEquipment.cs b/Penumbra/Game/CharEquipment.cs index e863c3bd..c2c0abc5 100644 --- a/Penumbra/Game/CharEquipment.cs +++ b/Penumbra/Game/CharEquipment.cs @@ -9,48 +9,25 @@ namespace Penumbra.Game [StructLayout( LayoutKind.Sequential, Pack = 1 )] public class CharEquipment { - [StructLayout( LayoutKind.Sequential, Pack = 1 )] - internal readonly struct Weapon - { - public readonly ushort Set; - public readonly ushort Type; - public readonly ushort Variant; - public readonly byte Stain; - - public override string ToString() - => $"{Set},{Type},{Variant},{Stain}"; - } - - [StructLayout( LayoutKind.Sequential, Pack = 1 )] - internal readonly struct Equip - { - public readonly ushort Set; - public readonly byte Variant; - public readonly byte Stain; - - public override string ToString() - => $"{Set},{Variant},{Stain}"; - } - private const int MainWeaponOffset = 0x0F08; private const int OffWeaponOffset = 0x0F70; private const int EquipmentOffset = 0x1040; private const int EquipmentSlots = 10; private const int WeaponSlots = 2; - internal readonly Weapon Mainhand; - internal readonly Weapon Offhand; - internal readonly Equip Head; - internal readonly Equip Body; - internal readonly Equip Hands; - internal readonly Equip Legs; - internal readonly Equip Feet; - internal readonly Equip Ear; - internal readonly Equip Neck; - internal readonly Equip Wrist; - internal readonly Equip RFinger; - internal readonly Equip LFinger; - internal readonly ushort IsSet; // Also fills struct size to 56, a multiple of 8. + public readonly ActorWeapon Mainhand; + public readonly ActorWeapon Offhand; + public readonly ActorEquip Head; + public readonly ActorEquip Body; + public readonly ActorEquip Hands; + public readonly ActorEquip Legs; + public readonly ActorEquip Feet; + public readonly ActorEquip Ear; + public readonly ActorEquip Neck; + public readonly ActorEquip Wrist; + public readonly ActorEquip RFinger; + public readonly ActorEquip LFinger; + public readonly ushort IsSet; // Also fills struct size to 56, a multiple of 8. public CharEquipment() => Clear(); @@ -81,23 +58,24 @@ namespace Penumbra.Game { IsSet = 1; var actorPtr = ( byte* )actorAddress.ToPointer(); - fixed( Weapon* main = &Mainhand, off = &Offhand ) + fixed( ActorWeapon* main = &Mainhand, off = &Offhand ) { - Buffer.MemoryCopy( actorPtr + MainWeaponOffset, main, sizeof( Weapon ), sizeof( Weapon ) ); - Buffer.MemoryCopy( actorPtr + OffWeaponOffset, off, sizeof( Weapon ), sizeof( Weapon ) ); + Buffer.MemoryCopy( actorPtr + MainWeaponOffset, main, sizeof( ActorWeapon ), sizeof( ActorWeapon ) ); + Buffer.MemoryCopy( actorPtr + OffWeaponOffset, off, sizeof( ActorWeapon ), sizeof( ActorWeapon ) ); } - fixed( Equip* equipment = &Head ) + fixed( ActorEquip* equipment = &Head ) { - Buffer.MemoryCopy( actorPtr + EquipmentOffset, equipment, EquipmentSlots * sizeof( Equip ), EquipmentSlots * sizeof( Equip ) ); + Buffer.MemoryCopy( actorPtr + EquipmentOffset, equipment, EquipmentSlots * sizeof( ActorEquip ), + EquipmentSlots * sizeof( ActorEquip ) ); } } public unsafe void Clear() { - fixed( Weapon* main = &Mainhand ) + fixed( ActorWeapon* main = &Mainhand ) { - var structSizeEights = ( 2 + EquipmentSlots * sizeof( Equip ) + WeaponSlots * sizeof( Weapon ) ) / 8; + var structSizeEights = ( 2 + EquipmentSlots * sizeof( ActorEquip ) + WeaponSlots * sizeof( ActorWeapon ) ) / 8; for( ulong* ptr = ( ulong* )main, end = ptr + structSizeEights; ptr != end; ++ptr ) { *ptr = 0; @@ -107,9 +85,9 @@ namespace Penumbra.Game private unsafe bool CompareAndOverwrite( CharEquipment rhs ) { - var structSizeEights = ( 2 + EquipmentSlots * sizeof( Equip ) + WeaponSlots * sizeof( Weapon ) ) / 8; + var structSizeEights = ( 2 + EquipmentSlots * sizeof( ActorEquip ) + WeaponSlots * sizeof( ActorWeapon ) ) / 8; var ret = true; - fixed( Weapon* data1 = &Mainhand, data2 = &rhs.Mainhand ) + fixed( ActorWeapon* data1 = &Mainhand, data2 = &rhs.Mainhand ) { var ptr1 = ( ulong* )data1; var ptr2 = ( ulong* )data2; @@ -128,8 +106,8 @@ namespace Penumbra.Game private unsafe bool CompareData( CharEquipment rhs ) { - var structSizeEights = ( 2 + EquipmentSlots * sizeof( Equip ) + WeaponSlots * sizeof( Weapon ) ) / 8; - fixed( Weapon* data1 = &Mainhand, data2 = &rhs.Mainhand ) + var structSizeEights = ( 2 + EquipmentSlots * sizeof( ActorEquip ) + WeaponSlots * sizeof( ActorWeapon ) ) / 8; + fixed( ActorWeapon* data1 = &Mainhand, data2 = &rhs.Mainhand ) { var ptr1 = ( ulong* )data1; var ptr2 = ( ulong* )data2; diff --git a/Penumbra/Game/ObjectIdentification.cs b/Penumbra/Game/ObjectIdentification.cs index 6d21047a..7c3620e2 100644 --- a/Penumbra/Game/ObjectIdentification.cs +++ b/Penumbra/Game/ObjectIdentification.cs @@ -291,7 +291,7 @@ namespace Penumbra.Game } } - public Item? Identify( ushort setId, ushort weaponType, ushort variant, EquipSlot slot ) + public Item? Identify( SetId setId, WeaponType weaponType, ushort variant, EquipSlot slot ) { switch( slot ) { @@ -304,7 +304,8 @@ namespace Penumbra.Game } default: { - var (begin, _) = FindIndexRange( _equipment, ( ( ulong )setId << 32 ) | ( ( ulong )slot.ToSlot() << 16 ) | weaponType, + var (begin, _) = FindIndexRange( _equipment, + ( ( ulong )setId << 32 ) | ( ( ulong )slot.ToSlot() << 16 ) | ( ulong )weaponType, 0xFFFFFFFFFFFF ); return begin >= 0 ? _equipment[ begin ].Item2.FirstOrDefault() : null; } diff --git a/Penumbra/Game/SetId.cs b/Penumbra/Game/SetId.cs new file mode 100644 index 00000000..196a8f8b --- /dev/null +++ b/Penumbra/Game/SetId.cs @@ -0,0 +1,23 @@ +using System; + +namespace Penumbra.Game +{ + public readonly struct SetId : IComparable + { + public readonly ushort Value; + + public SetId( ushort value ) + => Value = value; + + public static implicit operator SetId( ushort id ) => new(id); + + public static explicit operator ushort( SetId id ) + => id.Value; + + public override string ToString() + => Value.ToString(); + + public int CompareTo( SetId other ) + => Value.CompareTo( other.Value ); + } +} diff --git a/Penumbra/Game/StainId.cs b/Penumbra/Game/StainId.cs new file mode 100644 index 00000000..a16af075 --- /dev/null +++ b/Penumbra/Game/StainId.cs @@ -0,0 +1,29 @@ +using System; + +namespace Penumbra.Game +{ + public readonly struct StainId : IEquatable + { + public readonly byte Value; + + public StainId( byte value ) + => Value = value; + + public static implicit operator StainId( byte id ) => new( id ); + + public static explicit operator byte( StainId id ) + => id.Value; + + public override string ToString() + => Value.ToString(); + + public bool Equals( StainId other ) + => Value == other.Value; + + public override bool Equals( object? obj ) + => obj is StainId other && Equals( other ); + + public override int GetHashCode() + => Value.GetHashCode(); + } +} diff --git a/Penumbra/Game/WeaponType.cs b/Penumbra/Game/WeaponType.cs new file mode 100644 index 00000000..2798ec97 --- /dev/null +++ b/Penumbra/Game/WeaponType.cs @@ -0,0 +1,29 @@ +using System; + +namespace Penumbra.Game +{ + public readonly struct WeaponType : IEquatable + { + public readonly ushort Value; + + public WeaponType( ushort value ) + => Value = value; + + public static implicit operator WeaponType( ushort id ) => new( id ); + + public static explicit operator ushort( WeaponType id ) + => id.Value; + + public override string ToString() + => Value.ToString(); + + public bool Equals( WeaponType other ) + => Value == other.Value; + + public override bool Equals( object? obj ) + => obj is WeaponType other && Equals( other ); + + public override int GetHashCode() + => Value.GetHashCode(); + } +}