-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameMemory.cs
180 lines (160 loc) · 5.3 KB
/
GameMemory.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MHP3rdController
{
static class GameMemory
{
public static Addresses Address;
private static ProcessMemoryReader Reader = new ProcessMemoryReader();
private static int tempReadSize;
/// <summary>
/// Camera angle horisontal
/// </summary>
public static ushort LookHorisontal
{
get
{
byte[] raw = Reader.ReadProcessMemory((IntPtr)Address.LookHor, (int)2, out tempReadSize);
return BitConverter.ToUInt16(raw, 0);
}
set
{
byte[] raw = BitConverter.GetBytes(value);
Reader.WriteProcessMemory((IntPtr)Address.LookHor, raw, out tempReadSize);
}
}
/// <summary>
/// Scope angle horisontal
/// </summary>
public static ushort ScopeHorisontal
{
get
{
byte[] raw = Reader.ReadProcessMemory((IntPtr)Address.ScopeHor, (int)2, out tempReadSize);
return (ushort)(BitConverter.ToUInt16(raw, 0) - 32767);
//return BitConverter.ToUInt16(raw, 0);
}
set
{
byte[] raw = BitConverter.GetBytes((ushort)(value + 32767));
//byte[] raw = BitConverter.GetBytes((value));
Reader.WriteProcessMemory((IntPtr)Address.ScopeHor, raw, out tempReadSize);
}
}
/// <summary>
/// Scope angle vertical (Y)
/// From -100 to 100
/// </summary>
public static int ScopeVertical
{
get
{
byte[] raw = Reader.ReadProcessMemory((IntPtr)Address.ScopeVer, (int)1, out tempReadSize);
int retval = (int)raw[0];
if (retval > 128) retval -= 256;
return retval;
}
set
{
int val = value;
if (val < -100) val = -100;
if (val > 100) val = 100;
if (val < 0) val += 256;
byte[] raw = new byte[] { (byte)val };
Reader.WriteProcessMemory((IntPtr)Address.ScopeVer, raw, out tempReadSize);
}
}
/// <summary>
/// Bullets curr amount
/// </summary>
public static byte AmmoCurAmount
{
get
{
byte[] raw = Reader.ReadProcessMemory((IntPtr)Address.AmmoCurAmount, (int)1, out tempReadSize);
return raw[0];
}
set
{
byte[] raw = new byte[] { value };
Reader.WriteProcessMemory((IntPtr)Address.AmmoCurAmount, raw, out tempReadSize);
}
}
/// <summary>
/// If scope active
/// </summary>
public static bool IsScopeActive
{
get
{
byte[] raw = Reader.ReadProcessMemory((IntPtr)Address.IsScopeActive, (int)1, out tempReadSize);
return raw[0] == 1;
}
}
/// <summary>
/// If weapon equipped
/// </summary>
public static bool IsWeaponEquipped
{
get
{
byte[] raw = Reader.ReadProcessMemory((IntPtr)Address.IsWeaponEquipped, (int)1, out tempReadSize);
return raw[0] == 0;
}
}
public static void LoadAddresses()
{
if(WaitForLoadAddresses())
{
Reader.ReadProcess = Emulator.EmulatorProcess;
Reader.OpenProcess();
}
else
{
Environment.Exit(1);
}
}
private static bool WaitForLoadAddresses()
{
int i = 0;
bool result = false;
while (!result && i < 50)
{
result = TryLoadAddresses();
if (!result)
System.Threading.Thread.Sleep(100);
}
return result;
}
private static bool TryLoadAddresses()
{
try
{
FileStream LogFileStream = new FileStream(Setting.PpssppLogPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader LogFileReader = new StreamReader(LogFileStream, System.Text.Encoding.UTF8);
string LogText = LogFileReader.ReadToEnd();
int LogAddressShift = LogText.LastIndexOf("Memory system initialized. RAM at ");
IntPtr BaseAddress = IntPtr.Zero;
if (IntPtr.Size == 8)
{
BaseAddress = ((IntPtr)UInt64.Parse(LogText.Substring(LogAddressShift + 34, 16), System.Globalization.NumberStyles.HexNumber) - 0x8000000);
}
else
{
BaseAddress = ((IntPtr)int.Parse(LogText.Substring(LogAddressShift + 43, 7), System.Globalization.NumberStyles.HexNumber) - 0x8000000);
}
if (BaseAddress != IntPtr.Zero)
Address = new Addresses(BaseAddress);
}
catch
{
return false;
}
return true;
}
}
}