diff --git a/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/BLOCKHDR.cs b/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/BLOCKHDR.cs index 027cb5c..f3aafa6 100644 --- a/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/BLOCKHDR.cs +++ b/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/BLOCKHDR.cs @@ -2,7 +2,7 @@ { public struct BLOCKHDR : IStateBufferObject { - public bool Valid => !string.IsNullOrEmpty(ID); + public readonly bool Valid => !string.IsNullOrEmpty(ID); /// 总是8个字节 public string ID; public ushort Reserved; @@ -11,12 +11,12 @@ - public uint GetSize() + public readonly uint GetSize() { return (uint)(8 + sizeof(ushort) + sizeof(ushort) + sizeof(uint)); } - public void SaveState(StateBuffer buffer) + public readonly void SaveState(StateBuffer buffer) { if (Valid) { diff --git a/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/CTRSTAT.cs b/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/CTRSTAT.cs index ad7d1ec..0f9e29d 100644 --- a/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/CTRSTAT.cs +++ b/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/CTRSTAT.cs @@ -8,12 +8,12 @@ public uint pad4bit; public byte strobe; - public uint GetSize() + public readonly uint GetSize() { return sizeof(uint) * 4 + sizeof(byte); } - public void SaveState(StateBuffer buffer) + public readonly void SaveState(StateBuffer buffer) { buffer.Write(pad1bit); buffer.Write(pad2bit); diff --git a/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/EXCTRSTAT.cs b/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/EXCTRSTAT.cs index 8484be4..802a463 100644 --- a/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/EXCTRSTAT.cs +++ b/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/EXCTRSTAT.cs @@ -4,12 +4,12 @@ { public uint data; - public uint GetSize() + public readonly uint GetSize() { return sizeof(uint); } - public void SaveState(StateBuffer buffer) + public readonly void SaveState(StateBuffer buffer) { buffer.Write(data); } diff --git a/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/FILEHDR2.cs b/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/FILEHDR2.cs index 4406ccb..cf10d3e 100644 --- a/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/FILEHDR2.cs +++ b/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/FILEHDR2.cs @@ -14,12 +14,12 @@ - public uint GetSize() + public readonly uint GetSize() { return (uint)(ID.Length + sizeof(ushort) + sizeof(uint) + sizeof(ushort) + sizeof(ushort)); } - public void SaveState(StateBuffer buffer) + public readonly void SaveState(StateBuffer buffer) { buffer.Write(ID); buffer.Write(BlockVersion); diff --git a/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/MMCSTAT.cs b/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/MMCSTAT.cs index f761abe..e23ed91 100644 --- a/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/MMCSTAT.cs +++ b/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/MMCSTAT.cs @@ -9,12 +9,12 @@ return new MMCSTAT() { mmcdata = new byte[256] }; } - public uint GetSize() + public readonly uint GetSize() { return 256; } - public void SaveState(StateBuffer buffer) + public readonly void SaveState(StateBuffer buffer) { buffer.Write(mmcdata); } diff --git a/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/RAMSTAT.cs b/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/RAMSTAT.cs index f4d4888..5d3929b 100644 --- a/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/RAMSTAT.cs +++ b/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/RAMSTAT.cs @@ -22,12 +22,12 @@ namespace VirtualNes.Core return res; } - public uint GetSize() + public readonly uint GetSize() { return (uint)(RAM.Length + BGPAL.Length + SPPAL.Length + SPRAM.Length); } - public void SaveState(StateBuffer buffer) + public readonly void SaveState(StateBuffer buffer) { buffer.Write(RAM); buffer.Write(BGPAL); diff --git a/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/REGSTAT.cs b/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/REGSTAT.cs index ecc6ec6..13e0e8d 100644 --- a/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/REGSTAT.cs +++ b/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/REGSTAT.cs @@ -7,12 +7,12 @@ - public uint GetSize() + public readonly uint GetSize() { return cpureg.GetSize() + ppureg.GetSize(); } - public void SaveState(StateBuffer buffer) + public readonly void SaveState(StateBuffer buffer) { cpureg.SaveState(buffer); ppureg.SaveState(buffer); @@ -45,12 +45,12 @@ public long emul_cycles; public long base_cycles; - public uint GetSize() + public readonly uint GetSize() { return 32; } - public void SaveState(StateBuffer buffer) + public readonly void SaveState(StateBuffer buffer) { buffer.Write(PC); buffer.Write(A); @@ -102,12 +102,12 @@ public ushort loopy_v; public ushort loopy_x; - public uint GetSize() + public readonly uint GetSize() { return 12; } - public void SaveState(StateBuffer buffer) + public readonly void SaveState(StateBuffer buffer) { buffer.Write(reg0); buffer.Write(reg1); diff --git a/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/SNDSTAT.cs b/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/SNDSTAT.cs index 1cdef6e..f80a114 100644 --- a/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/SNDSTAT.cs +++ b/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/SNDSTAT.cs @@ -9,12 +9,12 @@ return new SNDSTAT() { snddata = new byte[0x800] }; } - public uint GetSize() + public readonly uint GetSize() { return (uint)snddata.Length; } - public void SaveState(StateBuffer buffer) + public readonly void SaveState(StateBuffer buffer) { buffer.Write(snddata); } diff --git a/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/State.cs b/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/State.cs index 9ff9d6e..4038f60 100644 --- a/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/State.cs +++ b/AxibugEmuOnline.Client/Assets/VirtualNes.Core/State/State.cs @@ -36,7 +36,7 @@ namespace VirtualNes.Core public BLOCKHDR exctrBLOCK; public EXCTRSTAT exctr; - public byte[] ToBytes() + public readonly byte[] ToBytes() { StateBuffer buffer = new StateBuffer(); diff --git a/AxibugEmuOnline.Client/Assets/VirtualNes.Core/Supporter/ControllerState.cs b/AxibugEmuOnline.Client/Assets/VirtualNes.Core/Supporter/ControllerState.cs index 6f9182a..22b7bcc 100644 --- a/AxibugEmuOnline.Client/Assets/VirtualNes.Core/Supporter/ControllerState.cs +++ b/AxibugEmuOnline.Client/Assets/VirtualNes.Core/Supporter/ControllerState.cs @@ -22,18 +22,14 @@ namespace VirtualNes.Core public bool HasButton(int player, EnumButtonType button) { - uint raw; - - switch (player) + uint raw = player switch { - case 0: raw = raw0; break; - case 1: raw = raw1; break; - case 2: raw = raw2; break; - case 3: raw = raw3; break; - default: - raw = 0; - break; - } + 0 => raw0, + 1 => raw1, + 2 => raw2, + 3 => raw3, + _ => 0 + }; return (raw & (uint)button) == (uint)button; } @@ -55,30 +51,10 @@ namespace VirtualNes.Core public override int GetHashCode() { - //return HashCode.Combine(raw0, raw1, raw2, raw3, valid); - return ComputeHashCode(raw0, raw1, raw2, raw3, valid); - } - - public static int ComputeHashCode(uint raw0, uint raw1, uint raw2, uint raw3, bool valid) - { - unchecked // 允许溢出,使得哈希码计算更加合理 - { - int hash = 17; // 选择一个非零的初始值 - - // 将每个 uint 类型的值转换为 int 并合并到哈希码中 - hash = hash * 31 + (int)raw0; - hash = hash * 31 + (int)raw1; - hash = hash * 31 + (int)raw2; - hash = hash * 31 + (int)raw3; - - // 将 bool 类型的值转换为 int 并合并到哈希码中 - hash = hash * 31 + (valid ? 1 : 0); - - return hash; - } - } - - public static bool operator ==(ControllerState left, ControllerState right) + return HashCode.Combine(raw0, raw1, raw2, raw3, valid); + } + + public static bool operator ==(ControllerState left, ControllerState right) { return left.raw0 == right.raw0 &&