From fd4f83ba401ce2274d2da4324f57c443ccf01de8 Mon Sep 17 00:00:00 2001 From: Kurt Date: Fri, 16 Jan 2026 15:43:41 -0600 Subject: [PATCH] Condense expression for EncryptedInt32 Bitwise operations were done manually; use intrinsics for clearer intent. --- NHSE.Core/Encryption/EncryptedInt32.cs | 36 +++++++----------- NHSE.Core/Structures/Designs/DesignPattern.cs | 1 - .../Structures/Designs/DesignPatternPRO.cs | 1 - NHSE.WinForms/Editor.cs | 38 ++++++------------- 4 files changed, 24 insertions(+), 52 deletions(-) diff --git a/NHSE.Core/Encryption/EncryptedInt32.cs b/NHSE.Core/Encryption/EncryptedInt32.cs index eb5ff75..f221056 100644 --- a/NHSE.Core/Encryption/EncryptedInt32.cs +++ b/NHSE.Core/Encryption/EncryptedInt32.cs @@ -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 +/// +/// Represents an encrypted 32-bit integer with associated encryption parameters. +/// +[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 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 data, int offset) diff --git a/NHSE.Core/Structures/Designs/DesignPattern.cs b/NHSE.Core/Structures/Designs/DesignPattern.cs index 262f116..89bd3ee 100644 --- a/NHSE.Core/Structures/Designs/DesignPattern.cs +++ b/NHSE.Core/Structures/Designs/DesignPattern.cs @@ -1,5 +1,4 @@ using System; -using System.Runtime.InteropServices; using static System.Buffers.Binary.BinaryPrimitives; namespace NHSE.Core; diff --git a/NHSE.Core/Structures/Designs/DesignPatternPRO.cs b/NHSE.Core/Structures/Designs/DesignPatternPRO.cs index faa2188..329d060 100644 --- a/NHSE.Core/Structures/Designs/DesignPatternPRO.cs +++ b/NHSE.Core/Structures/Designs/DesignPatternPRO.cs @@ -1,5 +1,4 @@ using System; -using System.Runtime.InteropServices; using static System.Buffers.Binary.BinaryPrimitives; namespace NHSE.Core; diff --git a/NHSE.WinForms/Editor.cs b/NHSE.WinForms/Editor.cs index 13a18b3..18e4092 100644 --- a/NHSE.WinForms/Editor.cs +++ b/NHSE.WinForms/Editor.cs @@ -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 }; } }