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
|
#include "global.h"
struct File
{
char *name;
u8 *data;
};
struct OpenedFile
{
struct File *file;
u8 *data;
};
struct FileArchive
{
char magic[8];
s32 count;
struct File *entries;
};
struct SiroArchive
{
u32 magic;
u8 *data;
};
extern struct OpenedFile gFileCache[64];
extern u32 gFileCacheCursorPosition;
extern u32 gUnknown_202D2A4;
extern int sprintf(char *, const char *, ...);
u8 *GetSiroPtr(struct OpenedFile *);
void NDS_DecompressRLE(void *);
void InitFileSystem(void)
{
s32 i;
for (i = 0; i < 64; i++)
{
gFileCache[i].file = NULL;
gFileCache[i].data = NULL;
}
gFileCacheCursorPosition = 0;
gUnknown_202D2A4 = 1;
}
u32 sub_800A8F8(u32 value)
{
u32 oldValue = gUnknown_202D2A4;
gUnknown_202D2A4 = value;
return oldValue;
}
struct OpenedFile *OpenFile(char *filename, struct FileArchive *arc)
{
char buffer[0x12C];
s32 left, right;
s32 cursor;
s32 i;
u32 magic = 0;
bool32 magicFound;
struct File *entries;
struct File *file;
magic = strcmp(arc->magic, "pksdir0") != 0;
magicFound = 0;
if (!(bool8)magic)
magicFound = 1;
if (!magicFound)
return NULL;
entries = arc->entries;
left = 0;
right = arc->count - 1;
while (left < right)
{
s32 mid = (left + right) / 2;
int result = strcmp(entries[mid].name, filename);
if (result == 0)
{
left = mid;
break;
}
else if (result < 0)
{
left = mid + 1;
}
else
{
right = mid;
}
}
file = &entries[left];
if (strcmp(file->name, filename))
{
sprintf(buffer, "not find file [%s]\n", filename);
return NULL;
}
cursor = gFileCacheCursorPosition;
for (i = 0; i < 64; i++)
{
cursor++;
if (cursor > 63)
cursor = 0;
if (!gFileCache[cursor].file)
{
gFileCache[cursor].file = file;
gFileCache[cursor].data = NULL;
return &gFileCache[cursor];
}
}
return NULL;
}
static u8 *_GetFileDataPtr(struct OpenedFile *openedFile)
{
openedFile->data = openedFile->file->data;
return openedFile->data;
}
u8 *GetFileDataPtr(struct OpenedFile *openedFile, int unused)
{
_GetFileDataPtr(openedFile);
return GetSiroPtr(openedFile);
}
struct OpenedFile *OpenFileAndGetFileDataPtr(char *filename, struct FileArchive *arc)
{
struct OpenedFile *openedFile = OpenFile(filename, arc);
if (openedFile)
GetFileDataPtr(openedFile, 0);
return openedFile;
}
struct OpenedFile *Call_OpenFileAndGetFileDataPtr(char *filename, struct FileArchive *arc)
{
return OpenFileAndGetFileDataPtr(filename, arc);
}
void CloseFile(struct OpenedFile *openedFile)
{
s32 i;
for (i = 0; i < 64; i++)
{
if (&gFileCache[i] == openedFile)
{
gFileCache[i].file = NULL;
gFileCache[i].data = NULL;
gFileCacheCursorPosition = i;
return;
}
}
}
u8 *GetSiroPtr(struct OpenedFile *openedFile)
{
struct SiroArchive *siro = (struct SiroArchive *)openedFile->data;
if (siro->magic == 0x30524953)
{
NDS_DecompressRLE(openedFile->data);
}
else if (siro->magic != 0x4F524953)
{
return openedFile->data;
}
openedFile->data = siro->data;
return openedFile->data;
}
void *UnusedGetSir0Ptr(struct SiroArchive *siro)
{
if (siro->magic != 0x30524953)
return siro;
NDS_DecompressRLE(siro);
return siro->data;
}
void NDS_DecompressRLE(void *unused)
{
}
|