This repository has been archived by the owner on Nov 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathautosave.cc
300 lines (276 loc) · 8.6 KB
/
autosave.cc
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
291
292
293
294
295
296
297
298
299
#include "jaakkos.h"
unsigned char ignore_yesno111[] = {0x33, 0xc0, 0x40, 0x90,
0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90};
unsigned char ignore_yesno12018[] = {0x33, 0xC0, 0x40, 0x90,
0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90};
unsigned char press_q12018[] = {0xC7, 0x05, 0x70, 0x9C,
0x2A, 0x08, 0x02, 0x00,
0x00, 0x00, 0xC3};
static int autosave(int patch_really_save) {
// child
if(!try_fork()) {
uint32_t REALLY_SAVE_ADDR = 0, SAVE_ADDR = 0, SPACEBAR_ADDR = 0, TEMP_FILES_ADDR = 0, QUIT_ADDR = 0, RETURN_ADDR = 0;
int adom_version = get_version();
if (adom_version == 111) {
REALLY_SAVE_ADDR = 0x0809072A;
SAVE_ADDR = 0x082b665c;
SPACEBAR_ADDR = 0x0804A690;
TEMP_FILES_ADDR = 0x08125c20;
}
else if (adom_version == 12018) {
REALLY_SAVE_ADDR = 0x0808AE98;
SAVE_ADDR = 0x082E20CC;
SPACEBAR_ADDR = 0x0804B320;
TEMP_FILES_ADDR = 0x0812C400;
QUIT_ADDR = 0x0807D790;
RETURN_ADDR = 0x709c2a08;
}
else if (adom_version == 12020) {
REALLY_SAVE_ADDR = 0x0808af18;
SAVE_ADDR = 0x082e2bf0;
SPACEBAR_ADDR = 0x0804b320;
TEMP_FILES_ADDR = 0x0812c720;
QUIT_ADDR = 0x0807d810;
RETURN_ADDR = 0x90a72a08;
}
else if (adom_version == 12021) {
REALLY_SAVE_ADDR = 0x0808e9a5;
SAVE_ADDR = 0x08309668;
SPACEBAR_ADDR = 0x0804b650;
TEMP_FILES_ADDR = 0x08136350;
QUIT_ADDR = 0x0807fe90;
RETURN_ADDR = 0x70ed2c08;
}
else if (adom_version == 12022) {
REALLY_SAVE_ADDR = 0x0808f20b;
SAVE_ADDR = 0x0830b690;
SPACEBAR_ADDR = 0x0804b650;
TEMP_FILES_ADDR = 0x081378e0;
QUIT_ADDR = 0x08080320;
RETURN_ADDR = 0x900d2d08;
}
else if (adom_version == 12023) {
REALLY_SAVE_ADDR = 0x0808f4db;
SAVE_ADDR = 0x0830c6f0;
SPACEBAR_ADDR = 0x0804b650;
TEMP_FILES_ADDR = 0x08138030;
QUIT_ADDR = 0x080804c0;
RETURN_ADDR = 0xd01d2d08;
}
else if (adom_version == 12048) {
REALLY_SAVE_ADDR = 0x08093bd4;
SAVE_ADDR = 0x083ae408;
SPACEBAR_ADDR = 0x0804b7b0;
TEMP_FILES_ADDR = 0x08143fb0;
QUIT_ADDR = 0x08082ad0;
RETURN_ADDR = 0xecc53008;
}
else if (adom_version == 12049) {
REALLY_SAVE_ADDR = 0x08093d54;
SAVE_ADDR = 0x083af9c8;
SPACEBAR_ADDR = 0x0804b7c0;
TEMP_FILES_ADDR = 0x08144a00;
QUIT_ADDR = 0x08082be0;
RETURN_ADDR = 0xacdb3008;
}
else if (adom_version == 12051) {
REALLY_SAVE_ADDR = 0x0809CD80;
SAVE_ADDR = 0x084d6e40;
SPACEBAR_ADDR = 0x0804b9f0;
TEMP_FILES_ADDR = 0x08150b40;
QUIT_ADDR = 0x0808A710;
RETURN_ADDR = 0xd0174308;
}
else {
printf("Don't know where to inject autosaver. Unknown ADOM version %i ?\n", adom_version);
return 2;
}
if(mprotect(PAGEBOUND(REALLY_SAVE_ADDR), getpagesize(), RWX_PROT) ||
mprotect(PAGEBOUND(SPACEBAR_ADDR), getpagesize(), RWX_PROT) ||
mprotect(PAGEBOUND(TEMP_FILES_ADDR), getpagesize(), RWX_PROT) ) {
perror("mprotect()");
exit(1);
}
close(STDOUT_FILENO);
close(STDIN_FILENO);
if(patch_really_save) {
if (adom_version == 111) {
memcpy((void*)REALLY_SAVE_ADDR, ignore_yesno111, sizeof(ignore_yesno111)); // no "really save"
}
else if ((adom_version == 12018) || (adom_version >= 12020)) {
memcpy((void*)REALLY_SAVE_ADDR, ignore_yesno12018, sizeof(ignore_yesno12018));
}
}
*((int*)SAVE_ADDR) = 60; // command = save
*((unsigned char*)SPACEBAR_ADDR) = 0xC3; // no hit spacebar to continue
*((unsigned int*)TEMP_FILES_ADDR) = 0x00c3c031; // don't remove temp files
if ((adom_version == 12018) || (adom_version >= 12020)) {
if(mprotect(PAGEBOUND(QUIT_ADDR), getpagesize(), RWX_PROT)) {
perror("mprotect()");
exit(1);
}
memcpy((void*)QUIT_ADDR, press_q12018, sizeof(press_q12018)); //pre18 quits to main menu after save
*((unsigned int*)(QUIT_ADDR + 2)) = RETURN_ADDR;
}
return 0;
}
// parent
else {
uint32_t SVG_ADDR = 0, MSG_ADDR = 0;
int adom_version = get_version();
if (adom_version == 111) {
SVG_ADDR = 0x080D6050;
MSG_ADDR = 0x8049fc0;
}
else if (adom_version == 12018) {
SVG_ADDR = 0x080CDBC0;
MSG_ADDR = 0x0804B8A0;
}
else if (adom_version == 12020) {
SVG_ADDR = 0x080cdd30;
MSG_ADDR = 0x0804b8a0;
}
else if (adom_version == 12021) {
SVG_ADDR = 0x080d4890;
MSG_ADDR = 0x0804c640;
}
else if (adom_version == 12022) {
SVG_ADDR = 0x080d5560;
MSG_ADDR = 0x0804c640;
}
else if (adom_version == 12023) {
SVG_ADDR = 0x080d5a20;
MSG_ADDR = 0x0804c720;
}
else if (adom_version == 12048) {
SVG_ADDR = 0x080dd4b0;
MSG_ADDR = 0x0804c330;
}
else if (adom_version == 12049) {
SVG_ADDR = 0x080dd970;
MSG_ADDR = 0x0804c3b0;
}
else if (adom_version == 12051) {
SVG_ADDR = 0x080e8ae0;
MSG_ADDR = 0x0804c550;
}
else {
printf("Don't know where to inject autosaver. Unknown ADOM version %i ?\n", adom_version);
return 2;
}
int (*adom_msg)(const char *format, ...) = (int(*)(const char*,...))MSG_ADDR;
wait(NULL);
char* (*get_svg_name)() = (char*(*)())SVG_ADDR;
// Back up save game to backup folder. Try to create backup directory.
char homedir[1024];
char backupdir[1043];
char svg_path[2048];
char new_path[2048];
snprintf(homedir, 1024, "%s", getpwuid(getuid())->pw_dir);
snprintf(backupdir, 1043, "%s/.adom.data/backup", homedir);
mkdir(backupdir,S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
snprintf(svg_path, 2048, "%s/.adom.data/savedg/%s", homedir, get_svg_name());
snprintf(new_path, 2048, "%s/%s", backupdir, get_svg_name());
if(!rename(svg_path, new_path)) {
adom_msg("NOTICE: backup .svg created!");
}
else {
adom_msg("WARNING: Automatic backup failed to move the .svg (%s)", sys_errlist[errno]);
}
return 1;
}
}
void command_hook() {
static unsigned int last_save_turn = 0;
uint32_t REFRESH_ADDR = 0, TURN_ADDR = 0;
int adom_version = get_version();
if (adom_version == 111) {
REFRESH_ADDR = 0x0804a500;
TURN_ADDR = 0x082b16e0;
}
else if (adom_version == 12018) {
REFRESH_ADDR = 0x0804B440;
TURN_ADDR = 0x082DBA18;
}
else if (adom_version == 12020) {
REFRESH_ADDR = 0x0804b440;
TURN_ADDR = 0x082dc538;
}
else if (adom_version == 12021) {
REFRESH_ADDR = 0x0804b800;
TURN_ADDR = 0x08302c38;
}
else if (adom_version == 12022) {
REFRESH_ADDR = 0x0804b800;
TURN_ADDR = 0x08304c58;
}
else if (adom_version == 12023) {
REFRESH_ADDR = 0x0804b800;
TURN_ADDR = 0x08305cb8;
}
else if (adom_version == 12048) {
REFRESH_ADDR = 0x0804b980;
TURN_ADDR = 0x08342584;
}
else if (adom_version == 12049) {
REFRESH_ADDR = 0x0804b990;
TURN_ADDR = 0x08343b44;
}
else if (adom_version == 12051) {
REFRESH_ADDR = 0x08074680;
TURN_ADDR = 0x0846af24;
}
else {
printf("Don't know where to inject autosaver. Unknown ADOM version %i ?\n", adom_version);
return;
}
// time for autobackup?
if((*((unsigned int*)TURN_ADDR)) >= last_save_turn + 1000)
if(autosave(1))
last_save_turn = (*((unsigned int*)TURN_ADDR)); // in parent
((void(*)())REFRESH_ADDR)();
}
int save_hook(char *msg, int a, char b) {
uint32_t PASK_ADDR = 0;
int adom_version = get_version();
if (adom_version == 111) {
PASK_ADDR = 0x0804B800;
}
else if (adom_version == 12018) {
PASK_ADDR = 0x0804C020;
}
else if (adom_version == 12020) {
PASK_ADDR = 0x0804c020;
}
else if (adom_version == 12021) {
PASK_ADDR = 0x0804c970;
}
else if (adom_version == 12022) {
PASK_ADDR = 0x0804c970;
}
else if (adom_version == 12023) {
PASK_ADDR = 0x0804ca50;
}
else if (adom_version == 12048) {
PASK_ADDR = 0x0804c660;
}
else if (adom_version == 12049) {
PASK_ADDR = 0x0804c6e0;
}
else if (adom_version == 12051) {
PASK_ADDR = 0x0804c880;
}
else {
printf("Don't know where to inject autosaver. Unknown ADOM version %i ?\n", adom_version);
return 2;
}
if(!autosave(0))
return 1; // child - answer 'y'
// parent - ask
return ((int(*)(char*,int,char))PASK_ADDR)(msg,a,b);
}