Repository navigation
Expand file tree
/
Copy pathgame_hooks.cpp
More file actions
290 lines (224 loc) · 7.71 KB
/
Copy pathgame_hooks.cpp
File metadata and controls
290 lines (224 loc) · 7.71 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include "patches.h"
#include "game_hooks.h"
#include "spelunky.h"
#include <Windows.h>
#include <string>
#ifdef DEBUG_MODE
#define DISCOVERY_FUNC(FUNC) if(!FUNC()) {std::cout << "GameHooks::" << #FUNC << " failed, hooks invalidated." << std::endl; is_valid = false;}
#else
#define DISCOVERY_FUNC(FUNC) if(!FUNC()) {is_valid = false;}
#endif
GameHooks::GameHooks(std::shared_ptr<Spelunky> spel, std::shared_ptr<DerandomizePatch> dp) :
is_valid(true),
spel(spel),
dp(dp),
have_steamid(false)
{
g_CurrentGamePtr = dp->game_ptr();
if(g_CurrentGamePtr == 0x0) {
is_valid = false;
return;
}
DISCOVERY_FUNC(discover_game_state);
DISCOVERY_FUNC(discover_timers);
DISCOVERY_FUNC(discover_gold_count);
DISCOVERY_FUNC(discover_steamid);
DISCOVERY_FUNC(discover_player_data);
}
BYTE game_state_find[] = {0xBB, 0x0F, 0x00, 0x00, 0x00, 0x3B, 0xC3, 0x75, 0xFF, 0x8B, 0x7E, 0xFF, 0xC7, 0x46, 0xFF, 0x1B, 0x00, 0x00, 0x00, 0x89, 0x5E, 0xFF, 0xE8};
std::string game_state_mask = "xxxxxxxx.xx.xx.xxxxxx.x";
bool GameHooks::discover_game_state() {
game_state_offset = spel->get_stored_hook("game_state_offset");
if(game_state_offset == 0x0)
{
Address game_state_ptr = spel->find_mem(game_state_find, game_state_mask);
if(game_state_ptr == 0x0) {
return false;
}
game_state_ptr += 0x15;
BYTE byte_state_offset;
spel->read_mem(game_state_ptr, &byte_state_offset, sizeof(BYTE));
game_state_offset = (Address)byte_state_offset;
spel->store_hook("game_state_offset", game_state_offset);
#ifdef DEBUG_MODE
std::cout << "game_state_offset -> " << std::setbase(16) << (Address)game_state_offset << std::endl;
#endif
}
return true;
}
BYTE game_timer_offset_find[] = {0xD8, 0xC2, 0xDD, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xD8, 0xD9, 0xDF, 0xE0, 0x84, 0xFF, 0x75, 0xFF, 0xDD, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01};
std::string game_timer_offset_mask = "xxx.....xxxxx.x.x.....x";
bool GameHooks::discover_timers() {
game_timer_offset = spel->get_stored_hook("game_timer_offset");
if(game_timer_offset == 0x0)
{
Address game_timer_offset_ptr = spel->find_mem(game_timer_offset_find, game_timer_offset_mask);
if(game_timer_offset_ptr == 0x0) {
return false;
}
game_timer_offset_ptr += 4;
spel->read_mem(game_timer_offset_ptr, &game_timer_offset, sizeof(Address));
game_timer_offset -= 8; //this points to milliseconds when read, we want to start from minutes
#ifdef DEBUG_MODE
std::cout << "game_timer_offset -> " << std::setbase(16) << game_timer_offset << std::endl;
#endif
spel->store_hook("game_timer_offset", game_timer_offset);
}
if(game_timer_offset != 0x0) {
level_timer_offset = game_timer_offset + 16;
}
return true;
}
BYTE game_goldcount_offset_find[] = {0x8B, 0x97, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x50, 0xFF, 0x8B, 0x87, 0xFF, 0xFF, 0xFF, 0xFF, 0x8B, 0x0D, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x81, 0xFF, 0xFF, 0xFF, 0xFF, 0x8B, 0x83, 0x00, 0x00, 0x00, 0x00};
std::string game_goldcount_offset_mask = "xx....xx.xx....xx....xx....xx....";
bool GameHooks::discover_gold_count() {
game_goldcount_offset = spel->get_stored_hook("game_goldcount_offset");
if(game_goldcount_offset == 0x0)
{
Address game_goldcount_offset_ptr = spel->find_mem(game_goldcount_offset_find, game_goldcount_offset_mask);
if(game_goldcount_offset_ptr == 0x0) {
return false;
}
game_goldcount_offset_ptr += 0x17;
spel->read_mem(game_goldcount_offset_ptr, &game_goldcount_offset, sizeof(Address));
#ifdef DEBUG_MODE
std::cout << "gold_count_offset -> " << std::setbase(16) << game_goldcount_offset << std::endl;
#endif
spel->store_hook("game_goldcount_offset", game_goldcount_offset);
}
return true;
}
#define STEAMID_MEM_LENGTH 18
BYTE steamid_find[] = "STEAMID=";
std::string steamid_mask = "xxxxxxxx";
#ifdef DEBUG_MODE
bool debugged_steamid = false;
#endif
bool valid_steamid(char id[STEAMID_MEM_LENGTH]) {
for(int i = 0; i < STEAMID_MEM_LENGTH-1; i++) {
char ch = id[i];
if(ch < '0' || ch > '9') {
return false;
}
}
id[STEAMID_MEM_LENGTH] = 0x0;
return true;
}
bool GameHooks::discover_steamid()
{
char id[STEAMID_MEM_LENGTH];
Address start = 0x0;
while(true) {
start = spel->find_mem(steamid_find, steamid_mask, start);
if(start == 0x0) {
have_steamid = false;
return false;
}
spel->read_mem(start+sizeof(steamid_find)-1, &id, sizeof(id));
if(valid_steamid(id)) {
break;
}
start++;
}
this->steamid = std::string(id);
have_steamid = true;
#ifdef DEBUG_MODE
std::cout << "Discovered steamid " << this->steamid << " at " << start << std::endl;
#endif
return true;
}
BYTE playerdata_find[] = {0x33, 0xCC, 0x8B, 0xCC, 0x69, 0xCC, 0xAA, 0xAA, 0xAA, 0xAA, 0xCC, 0x04,
0x00, 0x00, 0x00, 0x89, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0x89, 0xCC, 0xCC, 0xAA, 0xAA, 0xAA, 0xAA, 0x89};
std::string playerdata_mask = "x.x.x......xxxxx......x......x";
#ifdef DEBUG_MODE
bool debugged_player_data = false;
#endif
bool GameHooks::discover_player_data()
{
Address playerdata_addr = spel->get_stored_hook("pdat");
if(playerdata_addr == 0x0) {
playerdata_addr = spel->find_mem(playerdata_find, playerdata_mask);
if(playerdata_addr == 0x0) {
return false;
}
spel->store_hook("pdat", playerdata_addr);
}
spel->read_mem(playerdata_addr+6, &player_struct_size, sizeof(unsigned));
spel->read_mem(playerdata_addr+32, &player_health_offs, sizeof(Address));
spel->read_mem(playerdata_addr+39, &player_bomb_offs, sizeof(Address));
spel->read_mem(playerdata_addr+46, &player_rope_offs, sizeof(Address));
#ifdef DEBUG_MODE
if(!debugged_player_data) {
debugged_player_data = true;
std::cout << "Size of a player structure = " << std::setbase(16) << player_struct_size << std::endl;
std::cout << "Game health offset = " << std::setbase(16) << player_health_offs << std::endl;
std::cout << "Game bomb offset = " << std::setbase(16) << player_bomb_offs << std::endl;
std::cout << "Game rope offset = " << std::setbase(16) << player_rope_offs << std::endl;
}
#endif
return true;
}
int GameHooks::bombs(int player_id) {
Address game;
spel->read_mem(dp->game_ptr(), &game, sizeof(Address));
int bomb_count = 0;
spel->read_mem(game + player_bomb_offs + player_struct_size*player_id, &bomb_count, sizeof(int));
return bomb_count;
}
int GameHooks::ropes(int player_id) {
Address game;
spel->read_mem(dp->game_ptr(), &game, sizeof(Address));
int ropes_count = 0;
spel->read_mem(game + player_rope_offs + player_struct_size*player_id, &ropes_count, sizeof(int));
return ropes_count;
}
int GameHooks::health(int player_id) {
Address game;
spel->read_mem(dp->game_ptr(), &game, sizeof(Address));
int health_count = 0;
spel->read_mem(game + player_health_offs + player_struct_size*player_id, &health_count, sizeof(int));
return health_count;
}
int GameHooks::game_state() {
Address game;
spel->read_mem(g_CurrentGamePtr, &game, sizeof(Address));
int state;
spel->read_mem(game+game_state_offset, &state, sizeof(unsigned));
return state;
}
unsigned GameHooks::gold_count() {
Address game;
spel->read_mem(g_CurrentGamePtr, &game, sizeof(Address));
unsigned gold_count;
spel->read_mem(game+game_goldcount_offset, &gold_count, sizeof(unsigned));
return gold_count;
}
TimeInfo GameHooks::game_timer() {
Address game;
spel->read_mem(g_CurrentGamePtr, &game, sizeof(Address));
TimeInfo ti;
spel->read_mem(game+game_timer_offset, &ti, sizeof(TimeInfo));
return ti;
}
TimeInfo GameHooks::level_timer() {
Address game;
spel->read_mem(g_CurrentGamePtr, &game, sizeof(Address));
TimeInfo ti;
spel->read_mem(game+level_timer_offset, &ti, sizeof(TimeInfo));
return ti;
}
int GameHooks::current_level() {
return dp->current_level();
}
bool GameHooks::valid() {
return is_valid;
}
std::string GameHooks::steam_id() {
if(have_steamid) {
//prevent steam id from being overwritten
discover_steamid();
return steamid;
}
else
return "";
}