-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathMapSeedReader.cs
223 lines (178 loc) · 9.01 KB
/
MapSeedReader.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
* Copyright (C) 2021 okaygo
*
* https://github.com/misterokaygo/D2RAssist/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
**/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace D2RAssist
{
class GameData
{
public UInt16 PlayerX;
public UInt16 PlayerY;
public UInt32 MapSeed;
public UInt16 Difficulty;
public UInt32 AreaId;
}
class MapSeedReader
{
const int PROCESS_WM_READ = 0x0010;
public static GameData GetMapSeed()
{
try
{
Process gameProcess = Process.GetProcessesByName("D2R")[0];
IntPtr processHandle = OpenProcess(PROCESS_WM_READ, false, gameProcess.Id);
IntPtr processAddress = gameProcess.MainModule.BaseAddress;
IntPtr pPlayerUnit = IntPtr.Add(processAddress, 0x205fe40);
byte[] addressBuffer = new byte[8];
byte[] dwordBuffer = new byte[4];
byte[] shortBuffer = new byte[2];
byte[] byteBuffer = new byte[1];
ReadProcessMemory(processHandle, pPlayerUnit, addressBuffer, addressBuffer.Length, out IntPtr bytesRead);
IntPtr playerUnit = (IntPtr)BitConverter.ToInt64(addressBuffer, 0);
IntPtr pPlayer = IntPtr.Add(playerUnit, 0x10);
IntPtr pAct = IntPtr.Add(playerUnit, 0x20);
ReadProcessMemory(processHandle, pPlayer, addressBuffer, addressBuffer.Length, out bytesRead);
IntPtr player = (IntPtr)BitConverter.ToInt64(addressBuffer, 0);
// This was for Quest struct not difficulty
// IntPtr aDifficulty = IntPtr.Add(player, 0x10);
byte[] playerNameBuffer = new byte[16];
ReadProcessMemory(processHandle, player, playerNameBuffer, playerNameBuffer.Length, out bytesRead);
string playerName = Encoding.ASCII.GetString(playerNameBuffer);
Console.WriteLine("Player name: " + playerName);
//ReadProcessMemory(processHandle, aDifficulty, buffer, buffer.Length, out bytesRead);
//ulong difficulty = BitConverter.ToUInt64(buffer, 0);
//Console.WriteLine("Difficulty: " + difficulty);
ReadProcessMemory(processHandle, pAct, addressBuffer, addressBuffer.Length, out bytesRead);
IntPtr aAct = (IntPtr)BitConverter.ToInt64(addressBuffer, 0);
IntPtr pActUnk1 = IntPtr.Add(aAct, 0x70);
ReadProcessMemory(processHandle, pActUnk1, addressBuffer, addressBuffer.Length, out bytesRead);
IntPtr aActUnk1 = (IntPtr)BitConverter.ToInt64(addressBuffer, 0);
IntPtr pGameDifficulty = IntPtr.Add(aActUnk1, 0x830);
ReadProcessMemory(processHandle, pGameDifficulty, byteBuffer, byteBuffer.Length, out bytesRead);
UInt16 aGameDifficulty = byteBuffer[0];
IntPtr aDwAct = IntPtr.Add(aAct, 0x20);
ReadProcessMemory(processHandle, aDwAct, dwordBuffer, dwordBuffer.Length, out bytesRead);
uint dwAct = BitConverter.ToUInt32(dwordBuffer, 0);
IntPtr aMapSeed = IntPtr.Add(aAct, 0x14);
IntPtr pPath = IntPtr.Add(playerUnit, 0x38);
ReadProcessMemory(processHandle, pPath, addressBuffer, addressBuffer.Length, out bytesRead);
IntPtr path = (IntPtr)BitConverter.ToInt64(addressBuffer, 0);
IntPtr pRoom1 = IntPtr.Add(path, 0x20);
ReadProcessMemory(processHandle, pRoom1, addressBuffer, addressBuffer.Length, out bytesRead);
IntPtr aRoom1 = (IntPtr)BitConverter.ToInt64(addressBuffer, 0);
IntPtr pRoom2 = IntPtr.Add(aRoom1, 0x18);
ReadProcessMemory(processHandle, pRoom2, addressBuffer, addressBuffer.Length, out bytesRead);
IntPtr aRoom2 = (IntPtr)BitConverter.ToInt64(addressBuffer, 0);
IntPtr pLevel = IntPtr.Add(aRoom2, 0x90);
ReadProcessMemory(processHandle, pLevel, addressBuffer, addressBuffer.Length, out bytesRead);
IntPtr aLevel = (IntPtr)BitConverter.ToInt64(addressBuffer, 0);
IntPtr aLevelId = IntPtr.Add(aLevel, 0x1F8);
ReadProcessMemory(processHandle, aLevelId, dwordBuffer, dwordBuffer.Length, out bytesRead);
uint dwLevelId = BitConverter.ToUInt32(dwordBuffer, 0);
/* IntPtr pRoom1 = IntPtr.Add(path, 0x1C);
ReadProcessMemory(processHandle, pRoom1, buffer, buffer.Length, out bytesRead);
IntPtr aRoom1 = (IntPtr)BitConverter.ToInt64(buffer, 0);
IntPtr pRoom2 = IntPtr.Add(aRoom1, 0x10);
ReadProcessMemory(processHandle, pRoom2, buffer, buffer.Length, out bytesRead);
IntPtr aRoom2 = (IntPtr)BitConverter.ToInt64(buffer, 0);
IntPtr pLevel = IntPtr.Add(aRoom2, 0x58);
ReadProcessMemory(processHandle, pLevel, buffer, buffer.Length, out bytesRead);
IntPtr aLevel = (IntPtr)BitConverter.ToInt64(buffer, 0);
IntPtr aLevelNo = IntPtr.Add(aLevel, 0x1D0);
byte[] dwordBuffer = new byte[4];
ReadProcessMemory(processHandle, aLevelNo, dwordBuffer, dwordBuffer.Length, out bytesRead);
uint dwLevelNo = BitConverter.ToUInt32(dwordBuffer, 0);
Console.WriteLine("dwLevelNo: " + dwLevelNo); */
IntPtr posXAddress = IntPtr.Add(path, 0x02);
IntPtr posYAddress = IntPtr.Add(path, 0x06);
ReadProcessMemory(processHandle, aMapSeed, dwordBuffer, dwordBuffer.Length, out bytesRead);
UInt32 mapSeed = BitConverter.ToUInt32(dwordBuffer, 0);
ReadProcessMemory(processHandle, posXAddress, addressBuffer, addressBuffer.Length, out bytesRead);
UInt16 playerX = BitConverter.ToUInt16(addressBuffer, 0);
ReadProcessMemory(processHandle, posYAddress, addressBuffer, addressBuffer.Length, out bytesRead);
UInt16 playerY = BitConverter.ToUInt16(addressBuffer, 0);
return new GameData()
{
MapSeed = mapSeed,
PlayerX = playerX,
PlayerY = playerY,
AreaId = dwLevelId,
Difficulty = aGameDifficulty
};
}
catch(Exception e)
{
return null;
}
}
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr OpenProcess(
uint processAccess,
bool bInheritHandle,
int processId
);
public static IntPtr OpenProcess(Process proc, ProcessAccessFlags flags)
{
return OpenProcess((uint)flags, false, proc.Id);
}
[Flags]
public enum ProcessAccessFlags : uint
{
All = 0x001F0FFF,
Terminate = 0x00000001,
CreateThread = 0x00000002,
VirtualMemoryOperation = 0x00000008,
VirtualMemoryRead = 0x00000010,
VirtualMemoryWrite = 0x00000020,
DuplicateHandle = 0x00000040,
CreateProcess = 0x000000080,
SetQuota = 0x00000100,
SetInformation = 0x00000200,
QueryInformation = 0x00000400,
QueryLimitedInformation = 0x00001000,
Synchronize = 0x00100000
}
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool ReadProcessMemory(
IntPtr hProcess,
IntPtr lpBaseAddress,
[Out] byte[] lpBuffer,
int dwSize,
out IntPtr lpNumberOfBytesRead);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool ReadProcessMemory(
IntPtr hProcess,
IntPtr lpBaseAddress,
[Out, MarshalAs(UnmanagedType.AsAny)] object lpBuffer,
int dwSize,
out IntPtr lpNumberOfBytesRead);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool ReadProcessMemory(
IntPtr hProcess,
IntPtr lpBaseAddress,
IntPtr lpBuffer,
int dwSize,
out IntPtr lpNumberOfBytesRead);
}
}