This repository has been archived by the owner on Jun 8, 2018. It is now read-only.
/
main.c
261 lines (245 loc) · 9.52 KB
/
main.c
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
/*
Copyright 2010-2011 Seth VanHeulen
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 <http://www.gnu.org/licenses/>.
*/
#include <pspkernel.h>
#include <pspctrl.h>
#include <string.h>
PSP_MODULE_INFO("MonsterHunterSaveConverter", PSP_MODULE_KERNEL, 0, 3);
PSP_NO_CREATE_MAIN_THREAD();
typedef struct {
// pointer to the character data
char* character;
// length of the character data in memory
unsigned int size;
// module id of the main module of the game
SceUID module_id;
// name of the main module of the game
char game_name[28];
// name of the file to save/load character data to/from
char file_name[50];
// indicates if the file given in file_name exists
int file_exists;
} game_info_t;
int control_module_threads(SceUID module_id, int pause) {
// get an array of all threads
int thread_count;
if (sceKernelGetThreadmanIdList(SCE_KERNEL_TMID_Thread, NULL, 0, &thread_count) < 0)
return 0;
if (thread_count < 1)
return 0;
SceUID thread_id[thread_count];
if (sceKernelGetThreadmanIdList(SCE_KERNEL_TMID_Thread, thread_id, thread_count, NULL) < 0)
return 0;
int i;
SceModule* module;
SceKernelThreadInfo thread_info;
thread_info.size = sizeof(SceKernelThreadInfo);
for (i = 0; i < thread_count; i++) {
// determine if a thread is from the game
if (sceKernelReferThreadStatus(thread_id[i], &thread_info))
continue;
module = sceKernelFindModuleByAddress((unsigned int) thread_info.entry);
if (module && module->modid == module_id) {
// pause or resume the thread
if (pause)
sceKernelSuspendThread(thread_id[i]);
else
sceKernelResumeThread(thread_id[i]);
}
}
return 1;
}
int check_game(game_info_t* game_info) {
// get the game's module info
SceModule* module = NULL;
while (module == NULL) {
sceKernelDelayThread(1000000);
module = sceKernelFindModuleByAddress(0x08804000);
}
// set the module id and game name
game_info->module_id = module->modid;
strcpy(game_info->game_name, module->modname);
// get the game's global pointer
// NOTE: the SceModule structure is incorrect, text_addr is actually the gp_value
unsigned int module_gp = module->text_addr;
// add the offset to the global pointer, add the offset to the character pointer,
// set the size and file name based on the game name
if (strcmp("MonsterHunterPSP", game_info->game_name) == 0) {
module_gp -= 0x6e60;
game_info->character = (char *) 648;
game_info->size = 0x46a0;
strcat(game_info->file_name, "mhp.bin");
} else if (strcmp("MonsterHunterPortable2nd", game_info->game_name) == 0) {
module_gp -= 0x7e5c;
game_info->character = (char *) 1060;
game_info->size = 0x13ef4;
strcat(game_info->file_name, "mhp2.bin");
} else if (strcmp("MonsterHunterPortable2ndG", game_info->game_name) == 0) {
module_gp -= 0x7648;
game_info->character = (char *) 1184;
game_info->size = 0x6a938;
strcat(game_info->file_name, "mhp2g.bin");
} else if (strcmp("MonsterHunterPortable3rd", game_info->game_name) == 0) {
module_gp += 0x88fc0;
game_info->character = (char *) 2140;
game_info->size = 0x5f378;
strcat(game_info->file_name, "mhp3.bin");
} else {
return 0;
}
// wait for the global pointer to be filled
while (*((unsigned int *) module_gp) == 0)
sceKernelDelayThread(1000000);
// set the character pointer
game_info->character += *((unsigned int *) module_gp);
// check if a save file already exists
SceIoStat stat;
if (sceIoGetstat(game_info->file_name, &stat) < 0)
game_info->file_exists = 0;
else
game_info->file_exists = 1;
return 1;
}
int save(game_info_t* game_info) {
// open or create the save file
SceUID file = sceIoOpen(game_info->file_name, PSP_O_WRONLY | PSP_O_CREAT | PSP_O_TRUNC, 0777);
if (file < 0)
return 0;
// read character data from memory and write it into the save file
if (sceIoWrite(file, game_info->character, game_info->size) < game_info->size) {
sceIoClose(file);
return 0;
}
sceIoClose(file);
// mark the save file as existing
game_info->file_exists = 1;
return 1;
}
int load(game_info_t* game_info) {
// open an existing save file
SceUID file = sceIoOpen(game_info->file_name, PSP_O_RDONLY, 0777);
if (file < 0)
return 0;
// read character data from the save file and write it to memory
if (sceIoRead(file, game_info->character, game_info->size) < game_info->size) {
sceIoClose(file);
return 0;
}
sceIoClose(file);
return 1;
}
int delete(game_info_t* game_info) {
// delete an existing save file
if (sceIoRemove(game_info->file_name) < 0)
return 0;
// mark the save file as not existing
game_info->file_exists = 0;
return 1;
}
void display_message(game_info_t* game_info, const char* message, unsigned int color) {
// print some info and controls to the screen
pspDebugScreenSetTextColor(0xffffff);
pspDebugScreenSetXY(0, 0);
pspDebugScreenKprintf("game: ");
pspDebugScreenKprintf(game_info->game_name);
pspDebugScreenSetXY(0, 1);
pspDebugScreenKprintf("save file: ");
pspDebugScreenKprintf(game_info->file_name);
pspDebugScreenSetXY(0, 3);
pspDebugScreenKprintf("o = exit, square = save");
// print load and delete controls only if the save file exists
if (game_info->file_exists)
pspDebugScreenKprintf(", x = load, triangle = delete");
else
pspDebugScreenKprintf(" ");
pspDebugScreenSetXY(0, 5);
pspDebugScreenSetTextColor(color);
pspDebugScreenKprintf(message);
}
int main_thread(SceSize argc, void* argp) {
game_info_t game_info;
// get the path that the plugin was loaded from
int path_length = strrchr((char*) argp, '/') - (char*) argp + 1;
strncpy(game_info.file_name, argp, path_length);
game_info.file_name[path_length] = 0;
// check that the loaded game is supported
if (!check_game(&game_info)) {
sceKernelExitDeleteThread(0);
return 0;
}
// setup the controller
sceCtrlSetSamplingCycle(0);
sceCtrlSetSamplingMode(PSP_CTRL_MODE_DIGITAL);
SceCtrlData pad;
// wait for input
int paused = 0;
while (1) {
sceCtrlPeekBufferPositive(&pad, 1);
if (pad.Buttons != 0) {
if (paused) {
// exiting to xmb while the game is paused will cause the psp to hang
// so resuming when the home button is pressed works around that
if (pad.Buttons & (PSP_CTRL_CIRCLE | PSP_CTRL_HOME)) {
// resume the game
if (control_module_threads(game_info.module_id, 0))
paused = 0;
else
display_message(&game_info, "resume failed. ", 0xff);
}
if (pad.Buttons & PSP_CTRL_SQUARE) {
// save character
display_message(&game_info, "saving... ", 0xff0000);
if (save(&game_info))
display_message(&game_info, "save complete. ", 0xff00);
else
display_message(&game_info, "save failed. ", 0xff);
}
if (pad.Buttons & PSP_CTRL_CROSS && game_info.file_exists) {
// load character
display_message(&game_info, "loading... ", 0xff0000);
if (load(&game_info))
display_message(&game_info, "load complete. ", 0xff00);
else
display_message(&game_info, "load failed. ", 0xff);
}
if (pad.Buttons & PSP_CTRL_TRIANGLE && game_info.file_exists) {
// delete save file
display_message(&game_info, "deleting... ", 0xff0000);
if (delete(&game_info))
display_message(&game_info, "delete complete.", 0xff00);
else
display_message(&game_info, "delete failed. ", 0xff);
}
} else {
if (pad.Buttons & PSP_CTRL_NOTE) {
// pause the game
if (control_module_threads(game_info.module_id, 1)) {
paused = 1;
pspDebugScreenInit();
display_message(&game_info, "", 0);
}
}
}
}
sceKernelDelayThread(100000);
}
sceKernelExitDeleteThread(0);
return 0;
}
int module_start(SceSize argc, void* argp) {
// create plugin main thread
SceUID thid = sceKernelCreateThread("mhsc_main", main_thread, 0x30, 0x10000, 0, NULL);
if (thid >= 0)
sceKernelStartThread(thid, argc, argp);
return 0;
}