mirror of
https://github.com/kwsch/NHSE.git
synced 2026-08-22 02:25:16 -05:00
Condense expression for EncryptedInt32
Bitwise operations were done manually; use intrinsics for clearer intent.
This commit is contained in:
@@ -1,32 +1,24 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Numerics;
|
||||
using static System.Buffers.Binary.BinaryPrimitives;
|
||||
|
||||
namespace NHSE.Core;
|
||||
|
||||
public sealed class EncryptedInt32
|
||||
/// <summary>
|
||||
/// Represents an encrypted 32-bit integer with associated encryption parameters.
|
||||
/// </summary>
|
||||
[DebuggerDisplay("{Value}")]
|
||||
public sealed record EncryptedInt32(uint OriginalEncrypted, ushort Adjust = 0, byte Shift = 0, byte Checksum = 0)
|
||||
{
|
||||
// Encryption constant used to encrypt the int.
|
||||
private const uint ENCRYPTION_CONSTANT = 0x80E32B11;
|
||||
// Base shift count used in the encryption.
|
||||
private const byte SHIFT_BASE = 3;
|
||||
|
||||
public readonly uint OriginalEncrypted;
|
||||
public readonly ushort Adjust;
|
||||
public readonly byte Shift;
|
||||
public readonly byte Checksum;
|
||||
public uint Value { get; set; } = Decrypt(OriginalEncrypted, Shift, Adjust);
|
||||
|
||||
public uint Value;
|
||||
|
||||
public override string ToString() => Value.ToString();
|
||||
|
||||
public EncryptedInt32(uint encryptedValue, ushort adjust = 0, byte shift = 0, byte checksum = 0)
|
||||
{
|
||||
OriginalEncrypted = encryptedValue;
|
||||
Adjust = adjust;
|
||||
Shift = shift;
|
||||
Checksum = checksum;
|
||||
Value = Decrypt(encryptedValue, shift, adjust);
|
||||
}
|
||||
public static implicit operator uint(EncryptedInt32 encryptedInt32) => encryptedInt32.Value;
|
||||
|
||||
public void Write(Span<byte> data) => Write(this, data);
|
||||
public void Write(byte[] data, int offset) => Write(data.AsSpan(offset));
|
||||
@@ -41,16 +33,14 @@ public static byte CalculateChecksum(uint value)
|
||||
|
||||
public static uint Decrypt(uint encrypted, byte shift, ushort adjust)
|
||||
{
|
||||
// Decrypt the encrypted int using the given params.
|
||||
ulong val = ((ulong) encrypted) << ((32 - SHIFT_BASE - shift) & 0x3F);
|
||||
val += val >> 32;
|
||||
return ENCRYPTION_CONSTANT - adjust + (uint)val;
|
||||
var rotated = BitOperations.RotateRight(encrypted, shift + SHIFT_BASE);
|
||||
return rotated + ENCRYPTION_CONSTANT - adjust;
|
||||
}
|
||||
|
||||
public static uint Encrypt(uint value, byte shift, ushort adjust)
|
||||
{
|
||||
ulong val = (ulong) (value + unchecked(adjust - ENCRYPTION_CONSTANT)) << (shift + SHIFT_BASE);
|
||||
return (uint) ((val >> 32) + val);
|
||||
var adjusted = value + adjust - ENCRYPTION_CONSTANT;
|
||||
return BitOperations.RotateLeft(adjusted, shift + SHIFT_BASE);
|
||||
}
|
||||
|
||||
public static EncryptedInt32 ReadVerify(ReadOnlySpan<byte> data, int offset)
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using static System.Buffers.Binary.BinaryPrimitives;
|
||||
|
||||
namespace NHSE.Core;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using static System.Buffers.Binary.BinaryPrimitives;
|
||||
|
||||
namespace NHSE.Core;
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
using NHSE.Sprites;
|
||||
using NHSE.WinForms.Properties;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
@@ -293,10 +292,10 @@ private void LoadPlayer(int index)
|
||||
var pers = player.Personal;
|
||||
TB_Name.Text = pers.PlayerName;
|
||||
TB_TownName.Text = pers.TownName;
|
||||
NUD_BankBells.Value = Math.Min(int.MaxValue, pers.Bank.Value);
|
||||
NUD_NookMiles.Value = Math.Min(int.MaxValue, pers.NookMiles.Value);
|
||||
NUD_TotalNookMiles.Value = Math.Min(int.MaxValue, pers.TotalNookMiles.Value);
|
||||
NUD_Wallet.Value = Math.Min(int.MaxValue, pers.Wallet.Value);
|
||||
NUD_BankBells.Value = Math.Min(int.MaxValue, pers.Bank);
|
||||
NUD_NookMiles.Value = Math.Min(int.MaxValue, pers.NookMiles);
|
||||
NUD_TotalNookMiles.Value = Math.Min(int.MaxValue, pers.TotalNookMiles);
|
||||
NUD_Wallet.Value = Math.Min(int.MaxValue, pers.Wallet);
|
||||
|
||||
// swapped on purpose -- first count is the first two rows of items
|
||||
NUD_PocketCount1.Value = Math.Min(int.MaxValue, pers.PocketCount);
|
||||
@@ -305,7 +304,7 @@ private void LoadPlayer(int index)
|
||||
|
||||
if (pers.Data30 is { IsInitialized30: true } addition)
|
||||
{
|
||||
NUD_HotelTickets.Value = Math.Min(int.MaxValue, addition.HotelTickets.Value);
|
||||
NUD_HotelTickets.Value = Math.Min(int.MaxValue, addition.HotelTickets);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -357,21 +356,10 @@ private void SavePlayer(int index)
|
||||
SAV.ChangeIdentity(orig, updated);
|
||||
}
|
||||
|
||||
var bank = pers.Bank;
|
||||
bank.Value = (uint)NUD_BankBells.Value;
|
||||
pers.Bank = bank;
|
||||
|
||||
var nook = pers.NookMiles;
|
||||
nook.Value = (uint)NUD_NookMiles.Value;
|
||||
pers.NookMiles = nook;
|
||||
|
||||
var tnook = pers.TotalNookMiles;
|
||||
tnook.Value = (uint)NUD_TotalNookMiles.Value;
|
||||
pers.TotalNookMiles = tnook;
|
||||
|
||||
var wallet = pers.Wallet;
|
||||
wallet.Value = (uint)NUD_Wallet.Value;
|
||||
pers.Wallet = wallet;
|
||||
pers.Bank = pers.Bank with { Value = (uint)NUD_BankBells.Value };
|
||||
pers.NookMiles = pers.NookMiles with { Value = (uint)NUD_NookMiles.Value };
|
||||
pers.TotalNookMiles = pers.TotalNookMiles with { Value = (uint)NUD_TotalNookMiles.Value };
|
||||
pers.Wallet = pers.Wallet with { Value = (uint)NUD_Wallet.Value };
|
||||
|
||||
// swapped on purpose -- first count is the first two rows of items
|
||||
pers.PocketCount = (uint)NUD_PocketCount1.Value;
|
||||
@@ -381,16 +369,12 @@ private void SavePlayer(int index)
|
||||
|
||||
if (player.Personal.Data30 is { IsInitialized30: true } addition)
|
||||
{
|
||||
var tickets = addition.HotelTickets;
|
||||
tickets.Value = (uint)NUD_HotelTickets.Value;
|
||||
addition.HotelTickets = tickets;
|
||||
addition.HotelTickets = addition.HotelTickets with { Value = (uint)NUD_HotelTickets.Value };
|
||||
}
|
||||
|
||||
if (player.WhereAreN is { } x)
|
||||
{
|
||||
var poki = x.Poki;
|
||||
poki.Value = (uint)NUD_Poki.Value;
|
||||
x.Poki = poki;
|
||||
x.Poki = x.Poki with { Value = (uint)NUD_Poki.Value };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user