SMG-Decomp
A decompilation of Super Mario Galaxy 1
Loading...
Searching...
No Matches
FileUtil.cpp
1#include "Game/System/FileLoader.hpp"
2#include "Game/Util.hpp"
3#include "Game/SingletonHolder.hpp"
4
5#include <cstdio>
6#include "revolution.h"
7
8namespace MR {
9 extern const char* getCurrentLanguagePrefix();
10
11 bool isFileExist(const char *pFile, bool considerLanguage) {
12 s32 entryNum;
13
14 if (considerLanguage) {
15 entryNum = MR::convertPathToEntrynumConsideringLanguage(pFile);
16 }
17 else {
18 entryNum = DVDConvertPathToEntrynum(pFile);
19 }
20
21 return entryNum >= 0;
22 }
23
24 u32 getFileSize(const char *pFile, bool considerLanguage) {
25 s32 entryNum;
26
27 if (considerLanguage) {
28 entryNum = MR::convertPathToEntrynumConsideringLanguage(pFile);
29 }
30 else {
31 entryNum = DVDConvertPathToEntrynum(pFile);
32 }
33
34 DVDFileInfo fileInfo;
35 DVDFastOpen(entryNum, &fileInfo);
36 u32 size = fileInfo.length;
37 DVDClose(&fileInfo);
38
39 return size;
40 }
41
42 s32 convertPathToEntrynumConsideringLanguage(const char *pFile) {
43 const char* langPrefix = getCurrentLanguagePrefix();
44
45 char buf[0x100];
46 MR::addFilePrefix(buf, 0x100, pFile, langPrefix);
47
48 if (!isFileExist(buf, false)) {
49 snprintf(buf, 0x100, "%s", pFile);
50 }
51
52 return DVDConvertPathToEntrynum(buf);
53 }
54
55 void* loadToMainRAM(const char *pFile, u8 *pData, JKRHeap *pHeap, JKRDvdRipper::EAllocDirection allocDir) {
56 MR::loadAsyncToMainRAM(pFile, pData, pHeap, allocDir);
57 return MR::receiveFile(pFile);
58 }
59
60 void loadAsyncToMainRAM(const char *pFile, u8 *pData, JKRHeap *pHeap, JKRDvdRipper::EAllocDirection allocDir) {
61 const char* langPrefix = getCurrentLanguagePrefix();
62
63 char buf[0x100];
64 MR::addFilePrefix(buf, 0x100, pFile, langPrefix);
65
66 if (!isFileExist(buf, false)) {
67 snprintf(buf, 0x100, "%s", pFile);
68 }
69
70 SingletonHolder<FileLoader>::sInstance->requestLoadToMainRAM(buf, pData, pHeap, allocDir, false);
71 }
72
73 void* mountArchive(const char *pFile, JKRHeap *pHeap) {
74 MR::mountAsyncArchive(pFile, pHeap);
75 return MR::receiveArchive(pFile);
76 }
77
78 void mountAsyncArchive(const char *pFile, JKRHeap *pHeap) {
79 const char* langPrefix = getCurrentLanguagePrefix();
80
81 char buf[0x100];
82 MR::addFilePrefix(buf, 0x100, pFile, langPrefix);
83
84 if (!isFileExist(buf, false)) {
85 snprintf(buf, 0x100, "%s", pFile);
86 }
87
88 SingletonHolder<FileLoader>::sInstance->requestMountArchive(buf, pHeap, false);
89 }
90
91 void mountAsyncArchiveByObjectOrLayoutName(const char *pFile, JKRHeap *pHeap) {
92 JKRHeap* heap;
93
94 if (pHeap) {
95 heap = pHeap;
96 }
97 else {
98 heap = MR::getAproposHeapForSceneArchive(0.029999999f);
99 }
100
101 char objArch[0x100];
102 bool objArchiveName = MR::makeObjectArchiveFileNameFromPrefix(objArch, 0x100, pFile, false);
103
104 char layArch[0x100];
105 bool layoutArchiveName = MR::makeLayoutArchiveFileNameFromPrefix(layArch, 0x100, pFile, false);
106
107 if (objArchiveName) {
108 MR::mountAsyncArchive(objArch, heap);
109 }
110 else if (layoutArchiveName) {
111 MR::mountAsyncArchive(layArch, heap);
112 }
113 }
114
115 void* receiveFile(const char *pFile) {
116 const char* langPrefix = getCurrentLanguagePrefix();
117
118 char buf[0x100];
119 MR::addFilePrefix(buf, 0x100, pFile, langPrefix);
120
121 if (!isFileExist(buf, false)) {
122 snprintf(buf, 0x100, "%s", pFile);
123 }
124
125 return SingletonHolder<FileLoader>::sInstance->receiveFile(buf);
126 }
127
128 void* receiveArchive(const char *pFile) {
129 const char* langPrefix = getCurrentLanguagePrefix();
130
131 char buf[0x100];
132 MR::addFilePrefix(buf, 0x100, pFile, langPrefix);
133
134 if (!isFileExist(buf, false)) {
135 snprintf(buf, 0x100, "%s", pFile);
136 }
137
138 return SingletonHolder<FileLoader>::sInstance->receiveArchive(buf);
139 }
140
141 void receiveAllRequestedFile() {
142 SingletonHolder<FileLoader>::sInstance->receiveAllRequestedFile();
143 }
144
145 void* createAndAddArchive(void *pData, JKRHeap *pHeap, const char *pFile) {
146 return SingletonHolder<FileLoader>::sInstance->createAndAddArchive(pData, pHeap, pFile);
147 }
148
149 void getMountedArchiveAndHeap(const char *pFile, JKRArchive **pArchive, JKRHeap **pHeap) {
150 const char* langPrefix = getCurrentLanguagePrefix();
151
152 char buf[0x100];
153 MR::addFilePrefix(buf, 0x100, pFile, langPrefix);
154
155 if (!isFileExist(buf, false)) {
156 snprintf(buf, 0x100, "%s", pFile);
157 }
158
159 SingletonHolder<FileLoader>::sInstance->getMountedArchiveAndHeap(pFile, pArchive, pHeap);
160 }
161
162 void removeFileConsideringLanguage(const char *pFile) {
163 const char* langPrefix = getCurrentLanguagePrefix();
164
165 char buf[0x100];
166 MR::addFilePrefix(buf, 0x100, pFile, langPrefix);
167
168 if (!isFileExist(buf, false)) {
169 snprintf(buf, 0x100, "%s", pFile);
170 }
171
173 }
174
175 // MR::removeResourceAndFileHolderIfIsEqualHeap
176 // MR::decompressFileFromArchive
177
178 bool isLoadedFile(const char *pFile) {
179 const char* langPrefix = getCurrentLanguagePrefix();
180
181 char buf[0x100];
182 MR::addFilePrefix(buf, 0x100, pFile, langPrefix);
183
184 if (!isFileExist(buf, false)) {
185 snprintf(buf, 0x100, "%s", pFile);
186 }
187
188 return SingletonHolder<FileLoader>::sInstance->isLoaded(pFile);
189 }
190
191 bool isMountedArchive(const char *pFile) {
192 const char* langPrefix = getCurrentLanguagePrefix();
193
194 char buf[0x100];
195 MR::addFilePrefix(buf, 0x100, pFile, langPrefix);
196
197 if (!isFileExist(buf, false)) {
198 snprintf(buf, 0x100, "%s", pFile);
199 }
200
201 return SingletonHolder<FileLoader>::sInstance->isMountedArchive(pFile);
202 }
203
204 bool isLoadedObjectOrLayoutArchive(const char *pFile) {
205 char objFile[0x100];
206 bool obj_arch = MR::makeObjectArchiveFileNameFromPrefix(objFile, 0x100, pFile, false);
207
208 char archFile[0x100];
209 bool lay_arch = MR::makeLayoutArchiveFileNameFromPrefix(archFile, 0x100, pFile, false);
210
211 if (obj_arch) {
212 return MR::isLoadedFile(objFile);
213 }
214 else {
215 return MR::isLoadedFile(archFile);
216 }
217 }
218
219 void makeFileNameConsideringLanguage(char *pName, u32 length, const char *pFile) {
220 const char* langPrefix = getCurrentLanguagePrefix();
221
222 MR::addFilePrefix(pName, length, pFile, langPrefix);
223
224 if (!isFileExist(pName, false)) {
225 snprintf(pName, length, "%s", pFile);
226 }
227 }
228
229 bool makeObjectArchiveFileName(char *pName, u32 length, const char *pFile) {
230 snprintf(pName, length, "/ObjectData/%s", pFile);
231
232 if (MR::isFileExist(pName, true)) {
233 return true;
234 }
235
236 snprintf(pName, length, "/MapPartsData/%s", pFile);
237
238 if (MR::isFileExist(pName, false)) {
239 return true;
240 }
241
242 snprintf(pName, length, "%s", pFile);
243 return MR::isFileExist(pName, true);
244 }
245
246 bool makeObjectArchiveFileNameFromPrefix(char *pName, u32 length, const char *pFile, bool /*unused*/) {
247 char buf[0x100];
248 snprintf(buf, 0x100, "%s.arc", pFile);
249 return MR::makeObjectArchiveFileName(pName, length, buf);
250 }
251
252 bool makeLayoutArchiveFileName(char *pName, u32 length, const char *pFile) {
253 snprintf(pName, length, "/Region/LayoutData/%s", pFile);
254
255 if (MR::isFileExist(pName, false)) {
256 return true;
257 }
258
259 snprintf(pName, length, "/LayoutData/%s", pFile);
260
261 if (MR::isFileExist(pName, true)) {
262 return true;
263 }
264
265 snprintf(pName, length, "%s", pFile);
266 return MR::isFileExist(pName, false);
267 }
268
269 // MR::makeLayoutArchiveFileNameFromPrefix
270
271 void makeScenarioArchiveFileName(char *pName, u32 length, const char *pFile) {
272 snprintf(pName, length, "/StageData/%s/%sScenario.arc", pFile, pFile);
273 }
274};