主工程 VirtualNes.Core核心低版本C#兼容

This commit is contained in:
sin365 2024-12-20 18:44:55 +08:00
parent b303607a29
commit 0ad8768d67
11 changed files with 344 additions and 317 deletions

View File

@ -86,7 +86,7 @@ public class AxiProjectTools : EditorWindow
static void LoopPrefabNode(string rootPath, GameObject trans, int depth) static void LoopPrefabNode(string rootPath, GameObject trans, int depth)
{ {
string nodename = $"{rootPath}>{trans.name}"; string nodename = $"{rootPath}>{trans.name}";
#if UNITY_5_4_OR_NEWER && !UNITY_2018_4_OR_NEWER
GameObject prefabRoot = trans.gameObject; GameObject prefabRoot = trans.gameObject;
int comCount = prefabRoot.GetComponentCount(); int comCount = prefabRoot.GetComponentCount();
for (int i = 0; i < comCount; i++) for (int i = 0; i < comCount; i++)
@ -130,6 +130,9 @@ public class AxiProjectTools : EditorWindow
//遍历 //遍历
foreach (Transform child in trans.transform) foreach (Transform child in trans.transform)
LoopPrefabNode(nodename, child.gameObject, depth + 1); LoopPrefabNode(nodename, child.gameObject, depth + 1);
#else
Debug.Log("低版本不要执行本函数");
#endif
} }
[MenuItem("Axibug移植工具/[2]")] [MenuItem("Axibug移植工具/[2]")]

View File

@ -2,7 +2,7 @@
{ {
public struct BLOCKHDR : IStateBufferObject public struct BLOCKHDR : IStateBufferObject
{ {
public readonly bool Valid => !string.IsNullOrEmpty(ID); public bool Valid => !string.IsNullOrEmpty(ID);
/// <summary> 总是8个字节 </summary> /// <summary> 总是8个字节 </summary>
public string ID; public string ID;
public ushort Reserved; public ushort Reserved;
@ -11,12 +11,12 @@
public readonly uint GetSize() public uint GetSize()
{ {
return (uint)(8 + sizeof(ushort) + sizeof(ushort) + sizeof(uint)); return (uint)(8 + sizeof(ushort) + sizeof(ushort) + sizeof(uint));
} }
public readonly void SaveState(StateBuffer buffer) public void SaveState(StateBuffer buffer)
{ {
if (Valid) if (Valid)
{ {

View File

@ -8,12 +8,12 @@
public uint pad4bit; public uint pad4bit;
public byte strobe; public byte strobe;
public readonly uint GetSize() public uint GetSize()
{ {
return sizeof(uint) * 4 + sizeof(byte); return sizeof(uint) * 4 + sizeof(byte);
} }
public readonly void SaveState(StateBuffer buffer) public void SaveState(StateBuffer buffer)
{ {
buffer.Write(pad1bit); buffer.Write(pad1bit);
buffer.Write(pad2bit); buffer.Write(pad2bit);

View File

@ -4,12 +4,12 @@
{ {
public uint data; public uint data;
public readonly uint GetSize() public uint GetSize()
{ {
return sizeof(uint); return sizeof(uint);
} }
public readonly void SaveState(StateBuffer buffer) public void SaveState(StateBuffer buffer)
{ {
buffer.Write(data); buffer.Write(data);
} }

View File

@ -14,12 +14,12 @@
public readonly uint GetSize() public uint GetSize()
{ {
return (uint)(ID.Length + sizeof(ushort) + sizeof(uint) + sizeof(ushort) + sizeof(ushort)); return (uint)(ID.Length + sizeof(ushort) + sizeof(uint) + sizeof(ushort) + sizeof(ushort));
} }
public readonly void SaveState(StateBuffer buffer) public void SaveState(StateBuffer buffer)
{ {
buffer.Write(ID); buffer.Write(ID);
buffer.Write(BlockVersion); buffer.Write(BlockVersion);

View File

@ -9,12 +9,12 @@
return new MMCSTAT() { mmcdata = new byte[256] }; return new MMCSTAT() { mmcdata = new byte[256] };
} }
public readonly uint GetSize() public uint GetSize()
{ {
return 256; return 256;
} }
public readonly void SaveState(StateBuffer buffer) public void SaveState(StateBuffer buffer)
{ {
buffer.Write(mmcdata); buffer.Write(mmcdata);
} }

View File

@ -22,12 +22,12 @@ namespace VirtualNes.Core
return res; return res;
} }
public readonly uint GetSize() public uint GetSize()
{ {
return (uint)(RAM.Length + BGPAL.Length + SPPAL.Length + SPRAM.Length); return (uint)(RAM.Length + BGPAL.Length + SPPAL.Length + SPRAM.Length);
} }
public readonly void SaveState(StateBuffer buffer) public void SaveState(StateBuffer buffer)
{ {
buffer.Write(RAM); buffer.Write(RAM);
buffer.Write(BGPAL); buffer.Write(BGPAL);

View File

@ -7,12 +7,12 @@
public readonly uint GetSize() public uint GetSize()
{ {
return cpureg.GetSize() + ppureg.GetSize(); return cpureg.GetSize() + ppureg.GetSize();
} }
public readonly void SaveState(StateBuffer buffer) public void SaveState(StateBuffer buffer)
{ {
cpureg.SaveState(buffer); cpureg.SaveState(buffer);
ppureg.SaveState(buffer); ppureg.SaveState(buffer);
@ -45,12 +45,12 @@
public long emul_cycles; public long emul_cycles;
public long base_cycles; public long base_cycles;
public readonly uint GetSize() public uint GetSize()
{ {
return 32; return 32;
} }
public readonly void SaveState(StateBuffer buffer) public void SaveState(StateBuffer buffer)
{ {
buffer.Write(PC); buffer.Write(PC);
buffer.Write(A); buffer.Write(A);
@ -102,12 +102,12 @@
public ushort loopy_v; public ushort loopy_v;
public ushort loopy_x; public ushort loopy_x;
public readonly uint GetSize() public uint GetSize()
{ {
return 12; return 12;
} }
public readonly void SaveState(StateBuffer buffer) public void SaveState(StateBuffer buffer)
{ {
buffer.Write(reg0); buffer.Write(reg0);
buffer.Write(reg1); buffer.Write(reg1);

View File

@ -9,12 +9,12 @@
return new SNDSTAT() { snddata = new byte[0x800] }; return new SNDSTAT() { snddata = new byte[0x800] };
} }
public readonly uint GetSize() public uint GetSize()
{ {
return (uint)snddata.Length; return (uint)snddata.Length;
} }
public readonly void SaveState(StateBuffer buffer) public void SaveState(StateBuffer buffer)
{ {
buffer.Write(snddata); buffer.Write(snddata);
} }

View File

@ -36,7 +36,7 @@ namespace VirtualNes.Core
public BLOCKHDR exctrBLOCK; public BLOCKHDR exctrBLOCK;
public EXCTRSTAT exctr; public EXCTRSTAT exctr;
public readonly byte[] ToBytes() public byte[] ToBytes()
{ {
StateBuffer buffer = new StateBuffer(); StateBuffer buffer = new StateBuffer();

View File

@ -22,14 +22,18 @@ namespace VirtualNes.Core
public bool HasButton(int player, EnumButtonType button) public bool HasButton(int player, EnumButtonType button)
{ {
uint raw = player switch uint raw;
switch (player)
{ {
0 => raw0, case 0: raw = raw0; break;
1 => raw1, case 1: raw = raw1; break;
2 => raw2, case 2: raw = raw2; break;
3 => raw3, case 3: raw = raw3; break;
_ => 0 default:
}; raw = 0;
break;
}
return (raw & (uint)button) == (uint)button; return (raw & (uint)button) == (uint)button;
} }
@ -51,7 +55,27 @@ namespace VirtualNes.Core
public override int GetHashCode() public override int GetHashCode()
{ {
return HashCode.Combine(raw0, raw1, raw2, raw3, valid); //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) public static bool operator ==(ControllerState left, ControllerState right)