Split CharEquipment.

This commit is contained in:
Ottermandias 2021-07-25 01:09:33 +02:00
parent a4ae44f310
commit 3c560268fc
6 changed files with 136 additions and 49 deletions

View file

@ -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}";
}
}

View file

@ -9,48 +9,25 @@ namespace Penumbra.Game
[StructLayout( LayoutKind.Sequential, Pack = 1 )] [StructLayout( LayoutKind.Sequential, Pack = 1 )]
public class CharEquipment 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 MainWeaponOffset = 0x0F08;
private const int OffWeaponOffset = 0x0F70; private const int OffWeaponOffset = 0x0F70;
private const int EquipmentOffset = 0x1040; private const int EquipmentOffset = 0x1040;
private const int EquipmentSlots = 10; private const int EquipmentSlots = 10;
private const int WeaponSlots = 2; private const int WeaponSlots = 2;
internal readonly Weapon Mainhand; public readonly ActorWeapon Mainhand;
internal readonly Weapon Offhand; public readonly ActorWeapon Offhand;
internal readonly Equip Head; public readonly ActorEquip Head;
internal readonly Equip Body; public readonly ActorEquip Body;
internal readonly Equip Hands; public readonly ActorEquip Hands;
internal readonly Equip Legs; public readonly ActorEquip Legs;
internal readonly Equip Feet; public readonly ActorEquip Feet;
internal readonly Equip Ear; public readonly ActorEquip Ear;
internal readonly Equip Neck; public readonly ActorEquip Neck;
internal readonly Equip Wrist; public readonly ActorEquip Wrist;
internal readonly Equip RFinger; public readonly ActorEquip RFinger;
internal readonly Equip LFinger; public readonly ActorEquip LFinger;
internal readonly ushort IsSet; // Also fills struct size to 56, a multiple of 8. public readonly ushort IsSet; // Also fills struct size to 56, a multiple of 8.
public CharEquipment() public CharEquipment()
=> Clear(); => Clear();
@ -81,23 +58,24 @@ namespace Penumbra.Game
{ {
IsSet = 1; IsSet = 1;
var actorPtr = ( byte* )actorAddress.ToPointer(); 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 + MainWeaponOffset, main, sizeof( ActorWeapon ), sizeof( ActorWeapon ) );
Buffer.MemoryCopy( actorPtr + OffWeaponOffset, off, sizeof( Weapon ), sizeof( Weapon ) ); 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() 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 ) for( ulong* ptr = ( ulong* )main, end = ptr + structSizeEights; ptr != end; ++ptr )
{ {
*ptr = 0; *ptr = 0;
@ -107,9 +85,9 @@ namespace Penumbra.Game
private unsafe bool CompareAndOverwrite( CharEquipment rhs ) 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; var ret = true;
fixed( Weapon* data1 = &Mainhand, data2 = &rhs.Mainhand ) fixed( ActorWeapon* data1 = &Mainhand, data2 = &rhs.Mainhand )
{ {
var ptr1 = ( ulong* )data1; var ptr1 = ( ulong* )data1;
var ptr2 = ( ulong* )data2; var ptr2 = ( ulong* )data2;
@ -128,8 +106,8 @@ namespace Penumbra.Game
private unsafe bool CompareData( CharEquipment rhs ) private unsafe bool CompareData( CharEquipment rhs )
{ {
var structSizeEights = ( 2 + EquipmentSlots * sizeof( Equip ) + WeaponSlots * sizeof( Weapon ) ) / 8; var structSizeEights = ( 2 + EquipmentSlots * sizeof( ActorEquip ) + WeaponSlots * sizeof( ActorWeapon ) ) / 8;
fixed( Weapon* data1 = &Mainhand, data2 = &rhs.Mainhand ) fixed( ActorWeapon* data1 = &Mainhand, data2 = &rhs.Mainhand )
{ {
var ptr1 = ( ulong* )data1; var ptr1 = ( ulong* )data1;
var ptr2 = ( ulong* )data2; var ptr2 = ( ulong* )data2;

View file

@ -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 ) switch( slot )
{ {
@ -304,7 +304,8 @@ namespace Penumbra.Game
} }
default: 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 ); 0xFFFFFFFFFFFF );
return begin >= 0 ? _equipment[ begin ].Item2.FirstOrDefault() : null; return begin >= 0 ? _equipment[ begin ].Item2.FirstOrDefault() : null;
} }

23
Penumbra/Game/SetId.cs Normal file
View file

@ -0,0 +1,23 @@
using System;
namespace Penumbra.Game
{
public readonly struct SetId : IComparable<SetId>
{
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 );
}
}

29
Penumbra/Game/StainId.cs Normal file
View file

@ -0,0 +1,29 @@
using System;
namespace Penumbra.Game
{
public readonly struct StainId : IEquatable<StainId>
{
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();
}
}

View file

@ -0,0 +1,29 @@
using System;
namespace Penumbra.Game
{
public readonly struct WeaponType : IEquatable<WeaponType>
{
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();
}
}