mirror of
https://github.com/Ottermandias/Glamourer.git
synced 2025-12-12 10:17:23 +01:00
Simple changes for 6.1
This commit is contained in:
parent
b141da40b0
commit
e6e033bb8b
8 changed files with 539 additions and 529 deletions
|
|
@ -4,14 +4,17 @@ namespace Glamourer;
|
|||
|
||||
public static class CharacterExtensions
|
||||
{
|
||||
public const int WetnessOffset = 0x19E4;
|
||||
public const byte WetnessFlag = 0x08;
|
||||
public const int StateFlagsOffset = 0xDF6;
|
||||
public const byte HatHiddenFlag = 0x01;
|
||||
public const byte VisorToggledFlag = 0x10;
|
||||
public const int AlphaOffset = 0x18B8;
|
||||
public const int WeaponHiddenOffset = 0xCD4;
|
||||
public const byte WeaponHiddenFlag = 0x02;
|
||||
public const int WetnessOffset = 0x1ADA;
|
||||
public const byte WetnessFlag = 0x80;
|
||||
public const int HatVisibleOffset = 0x84E;
|
||||
public const int VisorToggledOffset = 0x84F;
|
||||
public const byte HatHiddenFlag = 0x01;
|
||||
public const byte VisorToggledFlag = 0x08;
|
||||
public const int AlphaOffset = 0x19E0;
|
||||
public const int WeaponHiddenOffset1 = 0x84F;
|
||||
public const int WeaponHiddenOffset2 = 0x72C; // maybe
|
||||
public const byte WeaponHiddenFlag1 = 0x01;
|
||||
public const byte WeaponHiddenFlag2 = 0x02;
|
||||
|
||||
public static unsafe bool IsWet(this Character a)
|
||||
=> (*((byte*)a.Address + WetnessOffset) & WetnessFlag) != 0;
|
||||
|
|
@ -29,49 +32,62 @@ public static class CharacterExtensions
|
|||
return true;
|
||||
}
|
||||
|
||||
public static unsafe ref byte StateFlags(this Character a)
|
||||
=> ref *((byte*)a.Address + StateFlagsOffset);
|
||||
public static unsafe bool IsHatVisible(this Character a)
|
||||
=> (*((byte*)a.Address + HatVisibleOffset) & HatHiddenFlag) == 0;
|
||||
|
||||
public static bool SetStateFlag(this Character a, bool value, byte flag)
|
||||
public static unsafe bool SetHatVisible(this Character a, bool visible)
|
||||
{
|
||||
var current = a.StateFlags();
|
||||
var previousValue = (current & flag) != 0;
|
||||
if (previousValue == value)
|
||||
var current = IsHatVisible(a);
|
||||
if (current == visible)
|
||||
return false;
|
||||
|
||||
if (value)
|
||||
a.StateFlags() = (byte)(current | flag);
|
||||
if (visible)
|
||||
*((byte*)a.Address + HatVisibleOffset) = (byte)(*((byte*)a.Address + HatVisibleOffset) & ~HatHiddenFlag);
|
||||
else
|
||||
a.StateFlags() = (byte)(current & ~flag);
|
||||
*((byte*)a.Address + HatVisibleOffset) = (byte)(*((byte*)a.Address + HatVisibleOffset) | HatHiddenFlag);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool IsHatHidden(this Character a)
|
||||
=> (a.StateFlags() & HatHiddenFlag) != 0;
|
||||
public static unsafe bool IsVisorToggled(this Character a)
|
||||
=> (*((byte*)a.Address + VisorToggledOffset) & VisorToggledFlag) == VisorToggledFlag;
|
||||
|
||||
public static unsafe bool SetVisorToggled(this Character a, bool toggled)
|
||||
{
|
||||
var current = IsVisorToggled(a);
|
||||
if (current == toggled)
|
||||
return false;
|
||||
|
||||
if (toggled)
|
||||
*((byte*)a.Address + VisorToggledOffset) = (byte)(*((byte*)a.Address + VisorToggledOffset) | VisorToggledFlag);
|
||||
else
|
||||
*((byte*)a.Address + VisorToggledOffset) = (byte)(*((byte*)a.Address + VisorToggledOffset) & ~VisorToggledFlag);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static unsafe bool IsWeaponHidden(this Character a)
|
||||
=> (a.StateFlags() & WeaponHiddenFlag) != 0
|
||||
&& (*((byte*)a.Address + WeaponHiddenOffset) & WeaponHiddenFlag) != 0;
|
||||
|
||||
public static bool IsVisorToggled(this Character a)
|
||||
=> (a.StateFlags() & VisorToggledFlag) != 0;
|
||||
|
||||
public static bool SetHatHidden(this Character a, bool value)
|
||||
=> SetStateFlag(a, value, HatHiddenFlag);
|
||||
=> (*((byte*)a.Address + WeaponHiddenOffset1) & WeaponHiddenFlag1) == WeaponHiddenFlag1
|
||||
&& (*((byte*)a.Address + WeaponHiddenOffset2) & WeaponHiddenFlag2) == WeaponHiddenFlag2;
|
||||
|
||||
public static unsafe bool SetWeaponHidden(this Character a, bool value)
|
||||
{
|
||||
var ret = SetStateFlag(a, value, WeaponHiddenFlag);
|
||||
var val = *((byte*)a.Address + WeaponHiddenOffset);
|
||||
if (value)
|
||||
*((byte*)a.Address + WeaponHiddenOffset) = (byte)(val | WeaponHiddenFlag);
|
||||
else
|
||||
*((byte*)a.Address + WeaponHiddenOffset) = (byte)(val & ~WeaponHiddenFlag);
|
||||
return ret || (val & WeaponHiddenFlag) != 0 != value;
|
||||
}
|
||||
var hidden = IsWeaponHidden(a);
|
||||
if (hidden == value)
|
||||
return false;
|
||||
|
||||
public static bool SetVisorToggled(this Character a, bool value)
|
||||
=> SetStateFlag(a, value, VisorToggledFlag);
|
||||
var val1 = *((byte*)a.Address + WeaponHiddenOffset1);
|
||||
var val2 = *((byte*)a.Address + WeaponHiddenOffset2);
|
||||
if (value)
|
||||
{
|
||||
*((byte*)a.Address + WeaponHiddenOffset1) = (byte)(val1 | WeaponHiddenFlag1);
|
||||
*((byte*)a.Address + WeaponHiddenOffset2) = (byte)(val2 | WeaponHiddenFlag2);
|
||||
}
|
||||
else
|
||||
{
|
||||
*((byte*)a.Address + WeaponHiddenOffset1) = (byte)(val1 & ~WeaponHiddenFlag1);
|
||||
*((byte*)a.Address + WeaponHiddenOffset2) = (byte)(val2 & ~WeaponHiddenFlag2);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static unsafe ref float Alpha(this Character a)
|
||||
=> ref *(float*)((byte*)a.Address + AlphaOffset);
|
||||
|
|
|
|||
|
|
@ -8,394 +8,386 @@ using Newtonsoft.Json.Linq;
|
|||
using Penumbra.GameData.Enums;
|
||||
using Penumbra.GameData.Structs;
|
||||
|
||||
namespace Glamourer
|
||||
namespace Glamourer;
|
||||
|
||||
public class CharacterSaveConverter : JsonConverter
|
||||
{
|
||||
public class CharacterSaveConverter : JsonConverter
|
||||
public override bool CanConvert(Type objectType)
|
||||
=> objectType == typeof(CharacterSave);
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
|
||||
{
|
||||
public override bool CanConvert(Type objectType)
|
||||
=> objectType == typeof(CharacterSave);
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
|
||||
{
|
||||
var token = JToken.Load(reader);
|
||||
var s = token.ToObject<string>();
|
||||
return CharacterSave.FromString(s!);
|
||||
}
|
||||
|
||||
public override bool CanWrite
|
||||
=> true;
|
||||
|
||||
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
|
||||
{
|
||||
if (value != null)
|
||||
{
|
||||
var s = ((CharacterSave) value).ToBase64();
|
||||
serializer.Serialize(writer, s);
|
||||
}
|
||||
}
|
||||
var token = JToken.Load(reader);
|
||||
var s = token.ToObject<string>();
|
||||
return CharacterSave.FromString(s!);
|
||||
}
|
||||
|
||||
[JsonConverter(typeof(CharacterSaveConverter))]
|
||||
public class CharacterSave
|
||||
public override bool CanWrite
|
||||
=> true;
|
||||
|
||||
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
|
||||
{
|
||||
public const byte CurrentVersion = 2;
|
||||
public const byte TotalSizeVersion1 = 1 + 1 + 2 + 56 + CharacterCustomization.CustomizationBytes;
|
||||
public const byte TotalSizeVersion2 = 1 + 1 + 2 + 56 + CharacterCustomization.CustomizationBytes + 4 + 1;
|
||||
|
||||
public const byte TotalSize = TotalSizeVersion2;
|
||||
|
||||
private readonly byte[] _bytes = new byte[TotalSize];
|
||||
|
||||
public CharacterSave()
|
||||
if (value != null)
|
||||
{
|
||||
_bytes[0] = CurrentVersion;
|
||||
Alpha = 1.0f;
|
||||
}
|
||||
|
||||
public CharacterSave Copy()
|
||||
{
|
||||
var ret = new CharacterSave();
|
||||
_bytes.CopyTo((Span<byte>) ret._bytes);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public byte Version
|
||||
=> _bytes[0];
|
||||
|
||||
public bool WriteCustomizations
|
||||
{
|
||||
get => (_bytes[1] & 0x01) != 0;
|
||||
set => _bytes[1] = (byte) (value ? _bytes[1] | 0x01 : _bytes[1] & ~0x01);
|
||||
}
|
||||
|
||||
public bool IsWet
|
||||
{
|
||||
get => (_bytes[1] & 0x02) != 0;
|
||||
set => _bytes[1] = (byte) (value ? _bytes[1] | 0x02 : _bytes[1] & ~0x02);
|
||||
}
|
||||
|
||||
public bool SetHatState
|
||||
{
|
||||
get => (_bytes[1] & 0x04) != 0;
|
||||
set => _bytes[1] = (byte) (value ? _bytes[1] | 0x04 : _bytes[1] & ~0x04);
|
||||
}
|
||||
|
||||
public bool SetWeaponState
|
||||
{
|
||||
get => (_bytes[1] & 0x08) != 0;
|
||||
set => _bytes[1] = (byte) (value ? _bytes[1] | 0x08 : _bytes[1] & ~0x08);
|
||||
}
|
||||
|
||||
public bool SetVisorState
|
||||
{
|
||||
get => (_bytes[1] & 0x10) != 0;
|
||||
set => _bytes[1] = (byte) (value ? _bytes[1] | 0x10 : _bytes[1] & ~0x10);
|
||||
}
|
||||
|
||||
public bool WriteProtected
|
||||
{
|
||||
get => (_bytes[1] & 0x20) != 0;
|
||||
set => _bytes[1] = (byte) (value ? _bytes[1] | 0x20 : _bytes[1] & ~0x20);
|
||||
}
|
||||
|
||||
public byte StateFlags
|
||||
{
|
||||
get => _bytes[64 + CharacterCustomization.CustomizationBytes];
|
||||
set => _bytes[64 + CharacterCustomization.CustomizationBytes] = value;
|
||||
}
|
||||
|
||||
public bool HatState
|
||||
{
|
||||
get => (StateFlags & 0x01) == 0;
|
||||
set => StateFlags = (byte) (value ? StateFlags & ~0x01 : StateFlags | 0x01);
|
||||
}
|
||||
|
||||
public bool VisorState
|
||||
{
|
||||
get => (StateFlags & 0x10) != 0;
|
||||
set => StateFlags = (byte) (value ? StateFlags | 0x10 : StateFlags & ~0x10);
|
||||
}
|
||||
|
||||
public bool WeaponState
|
||||
{
|
||||
get => (StateFlags & 0x02) == 0;
|
||||
set => StateFlags = (byte) (value ? StateFlags & ~0x02 : StateFlags | 0x02);
|
||||
}
|
||||
|
||||
public CharacterEquipMask WriteEquipment
|
||||
{
|
||||
get => (CharacterEquipMask) (_bytes[2] | (_bytes[3] << 8));
|
||||
set
|
||||
{
|
||||
_bytes[2] = (byte) ((ushort) value & 0xFF);
|
||||
_bytes[3] = (byte) ((ushort) value >> 8);
|
||||
}
|
||||
}
|
||||
|
||||
private static Dictionary<EquipSlot, (int, int, bool)> Offsets()
|
||||
{
|
||||
var stainOffsetWeapon = (int) Marshal.OffsetOf<CharacterWeapon>("Stain");
|
||||
var stainOffsetEquip = (int) Marshal.OffsetOf<CharacterArmor>("Stain");
|
||||
|
||||
(int, int, bool) ToOffsets(IntPtr offset, bool weapon)
|
||||
{
|
||||
var off = 4 + CharacterCustomization.CustomizationBytes + (int) offset;
|
||||
return weapon ? (off, off + stainOffsetWeapon, weapon) : (off, off + stainOffsetEquip, weapon);
|
||||
}
|
||||
|
||||
return new Dictionary<EquipSlot, (int, int, bool)>(12)
|
||||
{
|
||||
[EquipSlot.MainHand] = ToOffsets(Marshal.OffsetOf<CharacterEquipment>("MainHand"), true),
|
||||
[EquipSlot.OffHand] = ToOffsets(Marshal.OffsetOf<CharacterEquipment>("OffHand"), true),
|
||||
[EquipSlot.Head] = ToOffsets(Marshal.OffsetOf<CharacterEquipment>("Head"), false),
|
||||
[EquipSlot.Body] = ToOffsets(Marshal.OffsetOf<CharacterEquipment>("Body"), false),
|
||||
[EquipSlot.Hands] = ToOffsets(Marshal.OffsetOf<CharacterEquipment>("Hands"), false),
|
||||
[EquipSlot.Legs] = ToOffsets(Marshal.OffsetOf<CharacterEquipment>("Legs"), false),
|
||||
[EquipSlot.Feet] = ToOffsets(Marshal.OffsetOf<CharacterEquipment>("Feet"), false),
|
||||
[EquipSlot.Ears] = ToOffsets(Marshal.OffsetOf<CharacterEquipment>("Ears"), false),
|
||||
[EquipSlot.Neck] = ToOffsets(Marshal.OffsetOf<CharacterEquipment>("Neck"), false),
|
||||
[EquipSlot.Wrists] = ToOffsets(Marshal.OffsetOf<CharacterEquipment>("Wrists"), false),
|
||||
[EquipSlot.RFinger] = ToOffsets(Marshal.OffsetOf<CharacterEquipment>("RFinger"), false),
|
||||
[EquipSlot.LFinger] = ToOffsets(Marshal.OffsetOf<CharacterEquipment>("LFinger"), false),
|
||||
};
|
||||
}
|
||||
|
||||
private static readonly IReadOnlyDictionary<EquipSlot, (int, int, bool)> FieldOffsets = Offsets();
|
||||
|
||||
public bool WriteStain(EquipSlot slot, StainId stainId)
|
||||
{
|
||||
if (WriteProtected)
|
||||
return false;
|
||||
|
||||
var (_, stainOffset, _) = FieldOffsets[slot];
|
||||
if (_bytes[stainOffset] == (byte) stainId)
|
||||
return false;
|
||||
|
||||
_bytes[stainOffset] = stainId.Value;
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool WriteItem(int offset, SetId id, WeaponType type, ushort variant, bool weapon)
|
||||
{
|
||||
var idBytes = BitConverter.GetBytes(id.Value);
|
||||
|
||||
static bool WriteIfDifferent(ref byte x, byte y)
|
||||
{
|
||||
if (x == y)
|
||||
return false;
|
||||
|
||||
x = y;
|
||||
return true;
|
||||
}
|
||||
|
||||
var ret = WriteIfDifferent(ref _bytes[offset], idBytes[0]);
|
||||
ret |= WriteIfDifferent(ref _bytes[offset + 1], idBytes[1]);
|
||||
if (weapon)
|
||||
{
|
||||
var typeBytes = BitConverter.GetBytes(type.Value);
|
||||
var variantBytes = BitConverter.GetBytes(variant);
|
||||
ret |= WriteIfDifferent(ref _bytes[offset + 2], typeBytes[0]);
|
||||
ret |= WriteIfDifferent(ref _bytes[offset + 3], typeBytes[1]);
|
||||
ret |= WriteIfDifferent(ref _bytes[offset + 4], variantBytes[0]);
|
||||
ret |= WriteIfDifferent(ref _bytes[offset + 5], variantBytes[1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret |= WriteIfDifferent(ref _bytes[offset + 2], (byte) variant);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public bool WriteItem(Item item)
|
||||
{
|
||||
if (WriteProtected)
|
||||
return false;
|
||||
|
||||
var (itemOffset, _, isWeapon) = FieldOffsets[item.EquippableTo];
|
||||
var (id, type, variant) = item.MainModel;
|
||||
var ret = WriteItem(itemOffset, id, type, variant, isWeapon);
|
||||
if (item.EquippableTo == EquipSlot.MainHand && item.HasSubModel)
|
||||
{
|
||||
var (subOffset, _, _) = FieldOffsets[EquipSlot.OffHand];
|
||||
var (subId, subType, subVariant) = item.SubModel;
|
||||
ret |= WriteItem(subOffset, subId, subType, subVariant, true);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public unsafe float Alpha
|
||||
{
|
||||
get
|
||||
{
|
||||
fixed (byte* ptr = &_bytes[60 + CharacterCustomization.CustomizationBytes])
|
||||
{
|
||||
return *(float*) ptr;
|
||||
}
|
||||
}
|
||||
set
|
||||
{
|
||||
fixed (byte* ptr = _bytes)
|
||||
{
|
||||
*(ptr + 60 + CharacterCustomization.CustomizationBytes + 0) = *((byte*) &value + 0);
|
||||
*(ptr + 60 + CharacterCustomization.CustomizationBytes + 1) = *((byte*) &value + 1);
|
||||
*(ptr + 60 + CharacterCustomization.CustomizationBytes + 2) = *((byte*) &value + 2);
|
||||
*(ptr + 60 + CharacterCustomization.CustomizationBytes + 3) = *((byte*) &value + 3);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Load(CharacterCustomization customization)
|
||||
{
|
||||
WriteCustomizations = true;
|
||||
customization.WriteBytes(_bytes, 4);
|
||||
}
|
||||
|
||||
public void Load(CharacterEquipment equipment, CharacterEquipMask mask = CharacterEquipMask.All)
|
||||
{
|
||||
WriteEquipment = mask;
|
||||
equipment.WriteBytes(_bytes, 4 + CharacterCustomization.CustomizationBytes);
|
||||
}
|
||||
|
||||
public string ToBase64()
|
||||
=> Convert.ToBase64String(_bytes);
|
||||
|
||||
private static void CheckSize(int length, int requiredLength)
|
||||
{
|
||||
if (length != requiredLength)
|
||||
throw new Exception(
|
||||
$"Can not parse Base64 string into CharacterSave:\n\tInvalid size {length} instead of {requiredLength}.");
|
||||
}
|
||||
|
||||
private static void CheckRange(int idx, byte value, byte min, byte max)
|
||||
{
|
||||
if (value < min || value > max)
|
||||
throw new Exception(
|
||||
$"Can not parse Base64 string into CharacterSave:\n\tInvalid value {value} in byte {idx}, should be in [{min},{max}].");
|
||||
}
|
||||
|
||||
private static void CheckCharacterMask(byte val1, byte val2)
|
||||
{
|
||||
var mask = (CharacterEquipMask) (val1 | (val2 << 8));
|
||||
if (mask > CharacterEquipMask.All)
|
||||
throw new Exception($"Can not parse Base64 string into CharacterSave:\n\tInvalid value {mask} in byte 3 and 4.");
|
||||
}
|
||||
|
||||
public void LoadCharacter(Character a)
|
||||
{
|
||||
WriteCustomizations = true;
|
||||
Load(new CharacterCustomization(a));
|
||||
|
||||
Load(new CharacterEquipment(a));
|
||||
|
||||
SetHatState = true;
|
||||
SetVisorState = true;
|
||||
SetWeaponState = true;
|
||||
StateFlags = a.StateFlags();
|
||||
|
||||
IsWet = a.IsWet();
|
||||
Alpha = a.Alpha();
|
||||
}
|
||||
|
||||
|
||||
public void Apply(Character a)
|
||||
{
|
||||
Glamourer.RevertableDesigns.Add(a);
|
||||
|
||||
if (WriteCustomizations)
|
||||
Customizations.Write(a.Address);
|
||||
if (WriteEquipment != CharacterEquipMask.None)
|
||||
Equipment.Write(a.Address, WriteEquipment, WriteEquipment);
|
||||
a.SetWetness(IsWet);
|
||||
a.Alpha() = Alpha;
|
||||
if ((_bytes[1] & 0b11100) == 0b11100)
|
||||
{
|
||||
a.StateFlags() = StateFlags;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (SetHatState)
|
||||
a.SetHatHidden(HatState);
|
||||
if (SetVisorState)
|
||||
a.SetVisorToggled(VisorState);
|
||||
if (SetWeaponState)
|
||||
a.SetWeaponHidden(WeaponState);
|
||||
}
|
||||
}
|
||||
|
||||
public void ApplyOnlyEquipment(Character a)
|
||||
{
|
||||
var oldState = _bytes[1];
|
||||
WriteCustomizations = false;
|
||||
SetHatState = false;
|
||||
SetVisorState = false;
|
||||
SetWeaponState = false;
|
||||
Apply(a);
|
||||
_bytes[1] = oldState;
|
||||
}
|
||||
|
||||
public void ApplyOnlyCustomizations(Character a)
|
||||
{
|
||||
var oldState = _bytes[1];
|
||||
SetHatState = false;
|
||||
SetVisorState = false;
|
||||
SetWeaponState = false;
|
||||
var oldEquip = WriteEquipment;
|
||||
WriteEquipment = CharacterEquipMask.None;
|
||||
Apply(a);
|
||||
_bytes[1] = oldState;
|
||||
WriteEquipment = oldEquip;
|
||||
}
|
||||
|
||||
public void Load(string base64)
|
||||
{
|
||||
var bytes = Convert.FromBase64String(base64);
|
||||
switch (bytes[0])
|
||||
{
|
||||
case 1:
|
||||
CheckSize(bytes.Length, TotalSizeVersion1);
|
||||
CheckRange(2, bytes[1], 0, 1);
|
||||
Alpha = 1.0f;
|
||||
bytes[0] = CurrentVersion;
|
||||
break;
|
||||
case 2:
|
||||
CheckSize(bytes.Length, TotalSizeVersion2);
|
||||
CheckRange(2, bytes[1], 0, 0x3F);
|
||||
break;
|
||||
default: throw new Exception($"Can not parse Base64 string into CharacterSave:\n\tInvalid Version {bytes[0]}.");
|
||||
}
|
||||
|
||||
CheckCharacterMask(bytes[2], bytes[3]);
|
||||
bytes.CopyTo(_bytes, 0);
|
||||
}
|
||||
|
||||
public static CharacterSave FromString(string base64)
|
||||
{
|
||||
var ret = new CharacterSave();
|
||||
ret.Load(base64);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public unsafe ref CharacterCustomization Customizations
|
||||
{
|
||||
get
|
||||
{
|
||||
fixed (byte* ptr = _bytes)
|
||||
{
|
||||
return ref *(CharacterCustomization*) (ptr + 4);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public CharacterEquipment Equipment
|
||||
{
|
||||
get
|
||||
{
|
||||
var ret = new CharacterEquipment();
|
||||
ret.FromBytes(_bytes, 4 + CharacterCustomization.CustomizationBytes);
|
||||
return ret;
|
||||
}
|
||||
var s = ((CharacterSave)value).ToBase64();
|
||||
serializer.Serialize(writer, s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[JsonConverter(typeof(CharacterSaveConverter))]
|
||||
public class CharacterSave
|
||||
{
|
||||
public const byte CurrentVersion = 2;
|
||||
public const byte TotalSizeVersion1 = 1 + 1 + 2 + 56 + CharacterCustomization.CustomizationBytes;
|
||||
public const byte TotalSizeVersion2 = 1 + 1 + 2 + 56 + CharacterCustomization.CustomizationBytes + 4 + 1;
|
||||
|
||||
public const byte TotalSize = TotalSizeVersion2;
|
||||
|
||||
private readonly byte[] _bytes = new byte[TotalSize];
|
||||
|
||||
public CharacterSave()
|
||||
{
|
||||
_bytes[0] = CurrentVersion;
|
||||
Alpha = 1.0f;
|
||||
}
|
||||
|
||||
public CharacterSave Copy()
|
||||
{
|
||||
var ret = new CharacterSave();
|
||||
_bytes.CopyTo((Span<byte>)ret._bytes);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public byte Version
|
||||
=> _bytes[0];
|
||||
|
||||
public bool WriteCustomizations
|
||||
{
|
||||
get => (_bytes[1] & 0x01) != 0;
|
||||
set => _bytes[1] = (byte)(value ? _bytes[1] | 0x01 : _bytes[1] & ~0x01);
|
||||
}
|
||||
|
||||
public bool IsWet
|
||||
{
|
||||
get => (_bytes[1] & 0x02) != 0;
|
||||
set => _bytes[1] = (byte)(value ? _bytes[1] | 0x02 : _bytes[1] & ~0x02);
|
||||
}
|
||||
|
||||
public bool SetHatState
|
||||
{
|
||||
get => (_bytes[1] & 0x04) != 0;
|
||||
set => _bytes[1] = (byte)(value ? _bytes[1] | 0x04 : _bytes[1] & ~0x04);
|
||||
}
|
||||
|
||||
public bool SetWeaponState
|
||||
{
|
||||
get => (_bytes[1] & 0x08) != 0;
|
||||
set => _bytes[1] = (byte)(value ? _bytes[1] | 0x08 : _bytes[1] & ~0x08);
|
||||
}
|
||||
|
||||
public bool SetVisorState
|
||||
{
|
||||
get => (_bytes[1] & 0x10) != 0;
|
||||
set => _bytes[1] = (byte)(value ? _bytes[1] | 0x10 : _bytes[1] & ~0x10);
|
||||
}
|
||||
|
||||
public bool WriteProtected
|
||||
{
|
||||
get => (_bytes[1] & 0x20) != 0;
|
||||
set => _bytes[1] = (byte)(value ? _bytes[1] | 0x20 : _bytes[1] & ~0x20);
|
||||
}
|
||||
|
||||
public byte StateFlags
|
||||
{
|
||||
get => _bytes[64 + CharacterCustomization.CustomizationBytes];
|
||||
set => _bytes[64 + CharacterCustomization.CustomizationBytes] = value;
|
||||
}
|
||||
|
||||
public bool HatState
|
||||
{
|
||||
get => (StateFlags & 0x01) == 0;
|
||||
set => StateFlags = (byte)(value ? StateFlags & ~0x01 : StateFlags | 0x01);
|
||||
}
|
||||
|
||||
public bool VisorState
|
||||
{
|
||||
get => (StateFlags & 0x10) != 0;
|
||||
set => StateFlags = (byte)(value ? StateFlags | 0x10 : StateFlags & ~0x10);
|
||||
}
|
||||
|
||||
public bool WeaponState
|
||||
{
|
||||
get => (StateFlags & 0x02) == 0;
|
||||
set => StateFlags = (byte)(value ? StateFlags & ~0x02 : StateFlags | 0x02);
|
||||
}
|
||||
|
||||
public CharacterEquipMask WriteEquipment
|
||||
{
|
||||
get => (CharacterEquipMask)(_bytes[2] | (_bytes[3] << 8));
|
||||
set
|
||||
{
|
||||
_bytes[2] = (byte)((ushort)value & 0xFF);
|
||||
_bytes[3] = (byte)((ushort)value >> 8);
|
||||
}
|
||||
}
|
||||
|
||||
private static Dictionary<EquipSlot, (int, int, bool)> Offsets()
|
||||
{
|
||||
var stainOffsetWeapon = (int)Marshal.OffsetOf<CharacterWeapon>("Stain");
|
||||
var stainOffsetEquip = (int)Marshal.OffsetOf<CharacterArmor>("Stain");
|
||||
|
||||
(int, int, bool) ToOffsets(IntPtr offset, bool weapon)
|
||||
{
|
||||
var off = 4 + CharacterCustomization.CustomizationBytes + (int)offset;
|
||||
return weapon ? (off, off + stainOffsetWeapon, weapon) : (off, off + stainOffsetEquip, weapon);
|
||||
}
|
||||
|
||||
return new Dictionary<EquipSlot, (int, int, bool)>(12)
|
||||
{
|
||||
[EquipSlot.MainHand] = ToOffsets(Marshal.OffsetOf<CharacterEquipment>("MainHand"), true),
|
||||
[EquipSlot.OffHand] = ToOffsets(Marshal.OffsetOf<CharacterEquipment>("OffHand"), true),
|
||||
[EquipSlot.Head] = ToOffsets(Marshal.OffsetOf<CharacterEquipment>("Head"), false),
|
||||
[EquipSlot.Body] = ToOffsets(Marshal.OffsetOf<CharacterEquipment>("Body"), false),
|
||||
[EquipSlot.Hands] = ToOffsets(Marshal.OffsetOf<CharacterEquipment>("Hands"), false),
|
||||
[EquipSlot.Legs] = ToOffsets(Marshal.OffsetOf<CharacterEquipment>("Legs"), false),
|
||||
[EquipSlot.Feet] = ToOffsets(Marshal.OffsetOf<CharacterEquipment>("Feet"), false),
|
||||
[EquipSlot.Ears] = ToOffsets(Marshal.OffsetOf<CharacterEquipment>("Ears"), false),
|
||||
[EquipSlot.Neck] = ToOffsets(Marshal.OffsetOf<CharacterEquipment>("Neck"), false),
|
||||
[EquipSlot.Wrists] = ToOffsets(Marshal.OffsetOf<CharacterEquipment>("Wrists"), false),
|
||||
[EquipSlot.RFinger] = ToOffsets(Marshal.OffsetOf<CharacterEquipment>("RFinger"), false),
|
||||
[EquipSlot.LFinger] = ToOffsets(Marshal.OffsetOf<CharacterEquipment>("LFinger"), false),
|
||||
};
|
||||
}
|
||||
|
||||
private static readonly IReadOnlyDictionary<EquipSlot, (int, int, bool)> FieldOffsets = Offsets();
|
||||
|
||||
public bool WriteStain(EquipSlot slot, StainId stainId)
|
||||
{
|
||||
if (WriteProtected)
|
||||
return false;
|
||||
|
||||
var (_, stainOffset, _) = FieldOffsets[slot];
|
||||
if (_bytes[stainOffset] == (byte)stainId)
|
||||
return false;
|
||||
|
||||
_bytes[stainOffset] = stainId.Value;
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool WriteItem(int offset, SetId id, WeaponType type, ushort variant, bool weapon)
|
||||
{
|
||||
var idBytes = BitConverter.GetBytes(id.Value);
|
||||
|
||||
static bool WriteIfDifferent(ref byte x, byte y)
|
||||
{
|
||||
if (x == y)
|
||||
return false;
|
||||
|
||||
x = y;
|
||||
return true;
|
||||
}
|
||||
|
||||
var ret = WriteIfDifferent(ref _bytes[offset], idBytes[0]);
|
||||
ret |= WriteIfDifferent(ref _bytes[offset + 1], idBytes[1]);
|
||||
if (weapon)
|
||||
{
|
||||
var typeBytes = BitConverter.GetBytes(type.Value);
|
||||
var variantBytes = BitConverter.GetBytes(variant);
|
||||
ret |= WriteIfDifferent(ref _bytes[offset + 2], typeBytes[0]);
|
||||
ret |= WriteIfDifferent(ref _bytes[offset + 3], typeBytes[1]);
|
||||
ret |= WriteIfDifferent(ref _bytes[offset + 4], variantBytes[0]);
|
||||
ret |= WriteIfDifferent(ref _bytes[offset + 5], variantBytes[1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret |= WriteIfDifferent(ref _bytes[offset + 2], (byte)variant);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public bool WriteItem(Item item)
|
||||
{
|
||||
if (WriteProtected)
|
||||
return false;
|
||||
|
||||
var (itemOffset, _, isWeapon) = FieldOffsets[item.EquippableTo];
|
||||
var (id, type, variant) = item.MainModel;
|
||||
var ret = WriteItem(itemOffset, id, type, variant, isWeapon);
|
||||
if (item.EquippableTo == EquipSlot.MainHand && item.HasSubModel)
|
||||
{
|
||||
var (subOffset, _, _) = FieldOffsets[EquipSlot.OffHand];
|
||||
var (subId, subType, subVariant) = item.SubModel;
|
||||
ret |= WriteItem(subOffset, subId, subType, subVariant, true);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public unsafe float Alpha
|
||||
{
|
||||
get
|
||||
{
|
||||
fixed (byte* ptr = &_bytes[60 + CharacterCustomization.CustomizationBytes])
|
||||
{
|
||||
return *(float*)ptr;
|
||||
}
|
||||
}
|
||||
set
|
||||
{
|
||||
fixed (byte* ptr = _bytes)
|
||||
{
|
||||
*(ptr + 60 + CharacterCustomization.CustomizationBytes + 0) = *((byte*)&value + 0);
|
||||
*(ptr + 60 + CharacterCustomization.CustomizationBytes + 1) = *((byte*)&value + 1);
|
||||
*(ptr + 60 + CharacterCustomization.CustomizationBytes + 2) = *((byte*)&value + 2);
|
||||
*(ptr + 60 + CharacterCustomization.CustomizationBytes + 3) = *((byte*)&value + 3);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Load(CharacterCustomization customization)
|
||||
{
|
||||
WriteCustomizations = true;
|
||||
customization.WriteBytes(_bytes, 4);
|
||||
}
|
||||
|
||||
public void Load(CharacterEquipment equipment, CharacterEquipMask mask = CharacterEquipMask.All)
|
||||
{
|
||||
WriteEquipment = mask;
|
||||
equipment.WriteBytes(_bytes, 4 + CharacterCustomization.CustomizationBytes);
|
||||
}
|
||||
|
||||
public string ToBase64()
|
||||
=> Convert.ToBase64String(_bytes);
|
||||
|
||||
private static void CheckSize(int length, int requiredLength)
|
||||
{
|
||||
if (length != requiredLength)
|
||||
throw new Exception(
|
||||
$"Can not parse Base64 string into CharacterSave:\n\tInvalid size {length} instead of {requiredLength}.");
|
||||
}
|
||||
|
||||
private static void CheckRange(int idx, byte value, byte min, byte max)
|
||||
{
|
||||
if (value < min || value > max)
|
||||
throw new Exception(
|
||||
$"Can not parse Base64 string into CharacterSave:\n\tInvalid value {value} in byte {idx}, should be in [{min},{max}].");
|
||||
}
|
||||
|
||||
private static void CheckCharacterMask(byte val1, byte val2)
|
||||
{
|
||||
var mask = (CharacterEquipMask)(val1 | (val2 << 8));
|
||||
if (mask > CharacterEquipMask.All)
|
||||
throw new Exception($"Can not parse Base64 string into CharacterSave:\n\tInvalid value {mask} in byte 3 and 4.");
|
||||
}
|
||||
|
||||
public void LoadCharacter(Character a)
|
||||
{
|
||||
WriteCustomizations = true;
|
||||
Load(new CharacterCustomization(a));
|
||||
|
||||
Load(new CharacterEquipment(a));
|
||||
|
||||
SetHatState = true;
|
||||
SetVisorState = true;
|
||||
SetWeaponState = true;
|
||||
StateFlags = (byte)((a.IsHatVisible() ? 0x00 : 0x01) | (a.IsVisorToggled() ? 0x10 : 0x00) | (a.IsWeaponHidden() ? 0x02 : 0x00));
|
||||
|
||||
IsWet = a.IsWet();
|
||||
Alpha = a.Alpha();
|
||||
}
|
||||
|
||||
|
||||
public void Apply(Character a)
|
||||
{
|
||||
Glamourer.RevertableDesigns.Add(a);
|
||||
|
||||
if (WriteCustomizations)
|
||||
Customizations.Write(a.Address);
|
||||
if (WriteEquipment != CharacterEquipMask.None)
|
||||
Equipment.Write(a.Address, WriteEquipment, WriteEquipment);
|
||||
a.SetWetness(IsWet);
|
||||
a.Alpha() = Alpha;
|
||||
if (SetHatState)
|
||||
a.SetHatVisible(!HatState);
|
||||
if (SetVisorState)
|
||||
a.SetVisorToggled(VisorState);
|
||||
if (SetWeaponState)
|
||||
a.SetWeaponHidden(WeaponState);
|
||||
}
|
||||
|
||||
public void ApplyOnlyEquipment(Character a)
|
||||
{
|
||||
var oldState = _bytes[1];
|
||||
WriteCustomizations = false;
|
||||
SetHatState = false;
|
||||
SetVisorState = false;
|
||||
SetWeaponState = false;
|
||||
Apply(a);
|
||||
_bytes[1] = oldState;
|
||||
}
|
||||
|
||||
public void ApplyOnlyCustomizations(Character a)
|
||||
{
|
||||
var oldState = _bytes[1];
|
||||
SetHatState = false;
|
||||
SetVisorState = false;
|
||||
SetWeaponState = false;
|
||||
var oldEquip = WriteEquipment;
|
||||
WriteEquipment = CharacterEquipMask.None;
|
||||
Apply(a);
|
||||
_bytes[1] = oldState;
|
||||
WriteEquipment = oldEquip;
|
||||
}
|
||||
|
||||
public void Load(string base64)
|
||||
{
|
||||
var bytes = Convert.FromBase64String(base64);
|
||||
switch (bytes[0])
|
||||
{
|
||||
case 1:
|
||||
CheckSize(bytes.Length, TotalSizeVersion1);
|
||||
CheckRange(2, bytes[1], 0, 1);
|
||||
Alpha = 1.0f;
|
||||
bytes[0] = CurrentVersion;
|
||||
break;
|
||||
case 2:
|
||||
CheckSize(bytes.Length, TotalSizeVersion2);
|
||||
CheckRange(2, bytes[1], 0, 0x3F);
|
||||
break;
|
||||
default: throw new Exception($"Can not parse Base64 string into CharacterSave:\n\tInvalid Version {bytes[0]}.");
|
||||
}
|
||||
|
||||
CheckCharacterMask(bytes[2], bytes[3]);
|
||||
bytes.CopyTo(_bytes, 0);
|
||||
}
|
||||
|
||||
public static CharacterSave FromString(string base64)
|
||||
{
|
||||
var ret = new CharacterSave();
|
||||
ret.Load(base64);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public unsafe ref CharacterCustomization Customizations
|
||||
{
|
||||
get
|
||||
{
|
||||
fixed (byte* ptr = _bytes)
|
||||
{
|
||||
return ref *(CharacterCustomization*)(ptr + 4);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public CharacterEquipment Equipment
|
||||
{
|
||||
get
|
||||
{
|
||||
var ret = new CharacterEquipment();
|
||||
ret.FromBytes(_bytes, 4 + CharacterCustomization.CustomizationBytes);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -140,7 +140,7 @@ namespace Glamourer.Gui
|
|||
ImGui.SameLine();
|
||||
if (InputInt($"##text_{id}", ref current, 1, count))
|
||||
{
|
||||
customization[id] = set.Data(id, current).Value;
|
||||
customization[id] = (byte) current;
|
||||
ret = true;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -277,8 +277,9 @@ namespace Glamourer.Gui
|
|||
var label = $"##fsPopup{child.FullName()}";
|
||||
if (ImGui.BeginPopup(label))
|
||||
{
|
||||
if (ImGui.MenuItem("Delete"))
|
||||
if (ImGui.MenuItem("Delete") && ImGui.GetIO().KeyCtrl && ImGui.GetIO().KeyShift)
|
||||
_designs.DeleteAllChildren(child, false);
|
||||
ImGuiCustom.HoverTooltip("Hold Control and Shift to delete.");
|
||||
|
||||
RenameChildInput(child);
|
||||
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ namespace Glamourer.Gui
|
|||
ret |= DrawCheckMark("Hat Visible", save.HatState, v =>
|
||||
{
|
||||
save.HatState = v;
|
||||
player?.SetHatHidden(!v);
|
||||
player?.SetHatVisible(v);
|
||||
});
|
||||
|
||||
ret |= DrawCheckMark("Weapon Visible", save.WeaponState, v =>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue