This repository has been archived by the owner on Jan 28, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGenesis.cs
153 lines (121 loc) · 4.35 KB
/
Genesis.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.Runtime.InteropServices;
using System.Text;
using LiveSplit.ComponentUtil;
using LiveSplit.EMUHELP;
using LiveSplit.EMUHELP.Genesis;
public class Megadrive : Genesis { }
public partial class Genesis : HelperBase
{
public Genesis(bool generateCode) : base(generateCode)
{
var ProcessNames = new string[]
{
"retroarch",
"SEGAGameRoom",
"SEGAGenesisClassics",
"Fusion",
"gens",
"blastem",
};
GameProcess = new ProcessHook(ProcessNames);
Debugs.Info(" => Genesis Helper started");
}
public Genesis()
: this(true) { }
protected override void InitActions()
{
Emu = Game.ProcessName switch
{
"retroarch" => new Retroarch(this),
"SEGAGameRoom" or "SEGAGenesisClassics" => new SegaClassics(this),
"Fusion" => new Fusion(this),
"gens" => new Gens(this),
"blastem" => new BlastEm(this),
_ => throw new NotImplementedException(),
};
}
internal override bool IsAddressInBounds<T>(ulong address)
{
var defOffset = address;
if ((defOffset > 0xFFFF && defOffset < 0xFF0000) || defOffset > 0xFFFFFF)
return false;
else if (defOffset >= 0xFF0000 && defOffset <= 0xFFFFFF)
defOffset -= 0xFF0000;
return defOffset + (ulong)Marshal.SizeOf<T>() <= 0x10000;
}
internal override bool IsStringAddressInBounds(ulong address, int stringLength)
{
var defOffset = address;
if ((defOffset > 0xFFFF && defOffset < 0xFF0000) || defOffset > 0xFFFFFF)
return false;
else if (defOffset >= 0xFF0000 && defOffset <= 0xFFFFFF)
defOffset -= 0xFF0000;
return defOffset + (ulong)stringLength <= 0x10000;
}
public override bool TryGetAddress(ulong address, out IntPtr realAddress)
{
realAddress = default;
if (Emu.GetMemoryAddress(0) == null)
return false;
var defOffset = address;
if ((defOffset > 0xFFFF && defOffset < 0xFF0000) || defOffset > 0xFFFFFF)
return false;
else if (defOffset >= 0xFF0000 && defOffset <= 0xFFFFFF)
defOffset -= 0xFF0000;
realAddress = (IntPtr)((ulong)Emu.GetMemoryAddress(0) + defOffset);
return true;
}
public override bool TryReadValue<T>(ulong address, out T value, params uint[] offsets)
{
value = default;
if (Emu.GetMemoryAddress(0) == null || !IsAddressInBounds<T>(address))
return false;
var alignedOffset = (int)address & ~1;
if (!TryGetAddress((ulong)alignedOffset, out IntPtr realAddress))
return false;
int size = Marshal.SizeOf<T>();
int misalignment = (int)(address & 1);
var f_size = size + misalignment;
if ((f_size & 1) != 0)
f_size++;
if (Game.ReadBytes(realAddress, f_size, out var buf))
{
if (Emu.Endian == Endianness.Endian.Little)
{
for (int i = 0; i < buf.Length; i += 2)
(buf[i + 1], buf[i]) = (buf[i], buf[i + 1]);
}
if (buf.TryConvertTo<T>(misalignment, out T tempValue))
{
value = tempValue.FromEndian(Emu.Endian);
return true;
}
}
return default;
}
public override bool TryReadString(ulong address, int length, out string value, params uint[] offsets)
{
value = default;
if (Emu.GetMemoryAddress(0) == null || !IsStringAddressInBounds(address, length))
return false;
var alignedOffset = (int)address & ~1;
if (!TryGetAddress((ulong)alignedOffset, out IntPtr realAddress))
return false;
int misalignment = (int)(address & 1);
var f_size = length + misalignment;
if ((f_size & 1) != 0)
f_size++;
if (Game.ReadBytes(realAddress, f_size, out var buf))
{
if (Emu.Endian == Endianness.Endian.Little)
{
for (int i = 0; i < buf.Length; i += 2)
(buf[i + 1], buf[i]) = (buf[i], buf[i + 1]);
}
value = Encoding.UTF8.GetString(buf, 0, length);
return true;
}
return default;
}
}