forked from codestation/gclite
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gcread.c
333 lines (298 loc) Β· 11.6 KB
/
gcread.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* this file is part of Game Categories Lite
*
* Copyright (C) 2011 Codestation
*
* 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 <pspsdk.h>
#include <pspkernel.h>
#include <psprtc.h>
#include <string.h>
#include "categories_lite.h"
#include "psppaf.h"
#include "gcpatches.h"
#include "pspdefs.h"
#include "vshitem.h"
#include "config.h"
#include "filter.h"
#include "language.h"
#include "logger.h"
#define GAME_FOLDER "/PSP/GAME"
//extern variables
extern Category *folder_list[2];
// global vars
char category[52];
// unit vars
static SceUID game_dfd = -1;
static SceUID fakefd = -1;
static SceUID realfd = -1;
static SceUID openfd = -1;
static int uncategorized;
static char user_buffer[256];
static char mod_base[128];
static char opened_path[128];
#ifdef BENCHMARK
u64 start_mtime;
int display_flag;
#endif
inline void trim(char *str) {
int i = sce_paf_private_strlen(str);
while(str[i-1] == ' ') {
--i;
}
if(str[i] == ' ') {
str[i] = '\0';
}
}
int is_category_folder(SceIoDirent *dir) {
kprintf("checking %s\n", dir->d_name);
if(FIO_S_ISDIR(dir->d_stat.st_mode)) {
if(!*category) {
if(config.mode == MODE_FOLDER) {
kprintf("base: %s\n", opened_path);
if(!config.prefix && (*opened_path && !is_game_folder(opened_path, dir->d_name)) && !FindCategory(folder_list, dir->d_name, global_pos)) {
return 1;
}
} else {
if(!config.prefix && FindCategory(cat_list, dir->d_name, global_pos)) {
return 1;
}
}
if(config.prefix && sce_paf_private_strncmp(dir->d_name, "CAT_", 4) == 0) {
return 1;
}
}
if(!config.prefix && sce_paf_private_strcmp(dir->d_name, category) == 0) {
return 1;
}
if(config.prefix && sce_paf_private_strcmp(dir->d_name + 4, category) == 0) {
return 1;
}
}
return 0;
}
SceUID sceIoDopenPatched(const char *path) {
SceUID fd = sceIoDopen(path);
// only make a backup of the opened path if the game folder is opened in uncategorized mode
if (!*category && sce_paf_private_strcmp(path + 4, GAME_FOLDER) == 0) {
sce_paf_private_strcpy(opened_path, path);
} else {
*opened_path = '\0';
}
if(config.mode == MODE_FOLDER && sce_paf_private_strcmp(path + 4, GAME_FOLDER) == 0) {
#ifdef BENCHMARK
display_flag = 0;
sceRtcGetCurrentTick(&start_mtime);
#endif
sce_paf_private_strcpy(opened_path, path);
ClearCategories(folder_list, global_pos);
uncategorized = 0;
game_dfd = fd;
}
kprintf("opened dir, path: [%s], fd: %08X\n", path, fd);
// we are receiving a kernel mode file descriptor
if(fd > 0xFFFF) {
realfd = fd;
sce_paf_private_strncpy(user_buffer, path, 13);
user_buffer[13]= '\0';
// lets return a dummy fd instead
fakefd = sceIoDopen(user_buffer);
kprintf("Opening fake dir: [%s], fd: %08X\n", user_buffer, fakefd);
fd = fakefd;
}
return fd;
}
int sceIoDreadPatchedFolder(SceUID fd, SceIoDirent *dir) {
int res;
if (fd == game_dfd) {
while (1) {
if (openfd >= 0) {
res = sceIoDread(openfd, dir);
if (res > 0) {
if (dir->d_name[0] != '.' && !check_filter(dir->d_name)) {
sce_paf_private_strcpy(dir->d_name + 128, dir->d_name);
sce_paf_private_snprintf(dir->d_name, 128, "%s/%s", user_buffer + 14, dir->d_name + 128);
if (dir->d_private) {
sce_paf_private_strcpy((char *)dir->d_private + 13, dir->d_name);
}
kprintf("B) exit, dir: [%s]\n", dir->d_name);
return res;
} else {
kprintf("C) ignoring [%s]\n", dir->d_name);
continue;
}
} else {
sceIoDclose(openfd);
openfd = -1;
}
}
res = sceIoDread(fd, dir);
if (res > 0) {
kprintf("checking %s\n", dir->d_name);
if (dir->d_name[0] != '.') {
if(is_category_folder(dir) && has_directories(opened_path, dir->d_name)) {
u64 mtime;
kprintf("category match: %s\n", dir->d_name);
sceRtcGetTick((pspTime *) &dir->d_stat.st_mtime, &mtime);
kprintf("Adding %s\n", dir->d_name);
if(AddCategory(folder_list, dir->d_name, mtime, global_pos)) {
sce_paf_private_snprintf(user_buffer, 128, "%s/%s", opened_path, dir->d_name);
openfd = sceIoDopen(user_buffer);
}
continue;
} else {
if (!global_pos && (config.uncategorized & ONLY_MS)) {
uncategorized = 1;
} else if (global_pos && (config.uncategorized & ONLY_IE)) {
uncategorized = 1;
} else {
kprintf("A) ignoring [%s]\n", dir->d_name);
continue; // ignore this Dread
}
if(dir->d_name[0] == '.' || check_filter(dir->d_name) || (*opened_path && !is_game_folder(opened_path, dir->d_name))) { // ignore non game folders
kprintf("B) ignoring [%s]\n", dir->d_name);
continue;
}
}
}
}
if(res > 0) {
kprintf("A) exit, dir: [%s]\n", dir->d_name);
} else {
kprintf("exit, end of directory\n");
}
return res;
}
}
return sceIoDread(fd, dir);
}
int sceIoDreadPatched(SceUID fd, SceIoDirent *dir) {
SceUID ret;
//if our fake fd is being used then replace it with the kernel one
if(fd == fakefd) {
kprintf("Replacing fakefd: %08X with realfd: %08X\n", fakefd, realfd);
fd = realfd;
}
while((ret = sceIoDread(fd, dir)) > 0) {
kprintf("read dir: [%s]\n", dir->d_name);
if(!check_filter(dir->d_name) && (*category || !*opened_path || dir->d_name[0] == '.' || is_game_folder(opened_path, dir->d_name))) {
break;
}
}
if(ret <= 0) {
kprintf("end of directory reached\n");
}
return ret;
}
int gcGetStatIso(SceIoStat *stat) {
if(config.prefix) {
sce_paf_private_strcpy(user_buffer, "xxx:/ISO/CAT_");
sce_paf_private_strcpy(user_buffer + 13, category);
} else {
sce_paf_private_strcpy(user_buffer, "xxx:/ISO/");
sce_paf_private_strcpy(user_buffer + 9, category);
}
SET_DEVICENAME(user_buffer, global_pos);
// trim away the ME marker (5 spaces at the end of the category)
trim(user_buffer);
sce_paf_private_memset(stat, 0, sizeof(SceIoStat));
kprintf("opening [%s]\n", user_buffer);
return sceIoGetstat(user_buffer, stat);
}
int sceIoGetstatPatched(char *file, SceIoStat *stat) {
int ret;
kprintf("checking [%s]\n", file);
ret = sceIoGetstat(file, stat);
if(ret < 0 && *category) {
// lets verify if it was trying to open a ISO category
sce_paf_private_strcpy(user_buffer, GAME_FOLDER);
user_buffer[9] = '/';
if(config.prefix) {
sce_paf_private_strcpy(user_buffer + 10, "CAT_");
sce_paf_private_strcpy(user_buffer + 14, category);
} else {
sce_paf_private_strcpy(user_buffer + 10, category);
}
kprintf("comparing to [%s]\n", user_buffer);
if(sce_paf_private_strcmp(user_buffer, file + 4) == 0) {
kprintf("tried to open a iso category, retry\n");
// check if the category exists in /ISO
ret = gcGetStatIso(stat);
kprintf("gcGetStatIso result: %08X\n", ret);
}
}
return ret;
}
char *ReturnBasePathPatched(char *base) {
kprintf("orig base: [%s]\n", base);
// only do the patch if a category is being accessed
if(*category && base && sce_paf_private_strcmp(base + 4, GAME_FOLDER) == 0) {
sce_paf_private_strcpy(mod_base, base);
//append the category dir if is available and the original path is /PSP/GAME
if(config.prefix) {
sce_paf_private_strcpy(mod_base + 13, "/CAT_");
sce_paf_private_strcpy(mod_base + 18, category);
} else {
mod_base[13] = '/';
sce_paf_private_strcpy(mod_base + 14, category);
}
// force the device name
SET_DEVICENAME(mod_base, global_pos);
kprintf("modified base: [%s]\n", mod_base);
base = mod_base;
}
return base;
}
int sceIoDclosePatched(SceUID fd) {
kprintf("closing dir, fd: %08X\n", fd);
if(config.mode == MODE_FOLDER && fd == game_dfd) {
// add the uncategorized content in folder mode
if(uncategorized) {
AddCategory(folder_list, lang_container.msg_uncategorized, 1, 0);
}
game_dfd = -1;
}
// close the kernel descriptor along the fake one
if(fd == fakefd) {
kprintf("closing realfd: %08X\n", realfd);
sceIoDclose(realfd);
fakefd = -1;
}
return sceIoDclose(fd);
}
int sce_paf_private_snprintf_patched(char *a0, int a1, const char *a2, void *a3, void *t0) {
sce_paf_private_strcpy((char *)a1, (char *)t0);
return sce_paf_private_snprintf(a0, 291, a2, a3, t0);
}
void PatchGamePluginForGCread(u32 text_addr) {
// hook some sceIo funcs
MAKE_STUB(text_addr + patches.io_dread_stub[patch_index], config.mode == MODE_FOLDER ? sceIoDreadPatchedFolder : sceIoDreadPatched);
MAKE_STUB(text_addr + patches.io_dopen_stub[patch_index], sceIoDopenPatched);
MAKE_STUB(text_addr + patches.io_dclose_stub[patch_index], sceIoDclosePatched);
MAKE_STUB(text_addr + patches.io_getstat_stub[patch_index], sceIoGetstatPatched);
// hook the base path creation
MAKE_JUMP(text_addr + patches.base_path[patch_index], ReturnBasePathPatched);
_sw(0x00602021, text_addr + patches.base_path_arg[patch_index]); // move $a0, $v1
/* SCE renames folders before removal, but it doesn't handle
categories in doing so. It will try to move things out of
the category with the rename function... just get rid of
renaming to fix this lame bug. */
// #1
MAKE_CALL(text_addr+patches.snprintf_call_arg_1[patch_index][0], sce_paf_private_snprintf_patched);
_sw(0x02402821, text_addr+patches.snprintf_call_arg_1[patch_index][1]); // li $a1, 291 -> move $a1, $s2
// #2
MAKE_CALL(text_addr+patches.snprintf_call_arg_2[patch_index][0], sce_paf_private_snprintf_patched);
_sw(0x02002821, text_addr+patches.snprintf_call_arg_2[patch_index][1]); // li $a1, 291 -> move $a1, $s0
}