NSMBW-Decomp
A decompilation of New Super Mario Bros. Wii
Loading...
Searching...
No Matches
d_res.hpp
1#pragma once
2#include <types.h>
3#include <lib/rvl/arc/ARC.h>
4#include <lib/egg/core/eggFrmHeap.h>
5#include <lib/egg/core/eggArchive.h>
6#include <game/mLib/m_dvd.hpp>
7
8/// @brief An archive resource management class.
9/// @ingroup bases
10class dRes_c {
11public:
12
13 /// @brief A callback class for processing resources.
14 /// @details The calls to ::execute occur during the initial loading of an archive.
15 class callback_c {
16 public:
17 /// @brief Initializes the callback with the resource name.
18 virtual void init(const char *name) = 0;
19
20 /**
21 * @brief Executes the callback.
22 * @param data The resource data.
23 * @param folderSig The first 4 characters of the folder name.
24 * @param path The path to the current resource.
25 * @return The resource data.
26 */
27 virtual void *execute(void *data, u32 folderSig, const char* path) = 0;
28 };
29
30 /// @brief A callback class for EGG::Archive::searchInside.
31 /// @unofficial
33 public:
34 /// @brief Constructs a new searchCallback_c.
35 searchCallback_c(callback_c *callback, u8 **files, int numFiles, int fileIdx, u32 folderSig) :
37 mpFiles(files),
38 mNumFiles(numFiles),
39 mFileIdx(fileIdx),
40 mFolderSig(folderSig) {
41 }
42
43 /// @brief The callback function.
44 static void callback(void *cbInfo, void *file, const ARCDirEntry *dirEntry, const char *path);
45
46 private:
47 /// @brief The internal callback function.
48 void callback(void *file, const ARCDirEntry *dirEntry, const char *path);
49
50 callback_c *mpCallback; ///< The file processing callback.
51 u8 **mpFiles; ///< An array of pointers to the data of each loaded resource.
52 int mNumFiles; ///< The number of files in ::mpFiles.
53 unsigned int mFileIdx; ///< The index of the current file in ::mpFiles.
54 u32 mFolderSig; ///< The first 4 characters of the current folder.
55 };
56
57 /// @brief An archive holder.
58 class info_c {
59 public:
60
61 /// @brief The loading status of the archive.
63 LOAD_ERROR = -1, ///< An error occurred while loading the archive.
64 LOAD_SUCCESS = 0, ///< The archive was loaded successfully.
65 LOAD_IN_PROGRESS = 1, ///< The archive is currently being loaded.
66 };
67
68 info_c(); ///< Constructs a new archive holder.
69 ~info_c(); ///< Destroys the archive holder.
70
71 /// @brief Unmounts the archive and frees the holder for use.
72 /// @return Whether the operation was successful.
73 bool cleanup();
74
75 /**
76 * @brief Sets information about the archive to be loaded.
77 *
78 * @param arcName The name of the archive.
79 * @param containingFolder The path to the folder which the archive is in.
80 * @param allocDirection The allocation direction. See ::MEMExpHeapAllocDir.
81 * @param heap The heap to load the resources of the archive into, or @p nullptr to use the default heap.
82 * @return Whether the archive was prepared successfully and will be loaded.
83 */
84 bool set(const char *arcName, const char *containingFolder, u8 allocDirection, EGG::Heap *heap);
85
86 /**
87 * @brief Attempts to load the archive into memory and load the resources with a callback.
88 *
89 * @param callback The resource loaded callback, or @p nullptr .
90 * @return The result of the operation.
91 */
93
94 const char *getName() const { return mName; }
95 int getRefCount() const { return mRefCount; }
96
97 /// @brief Gets the file loading command.
98 /// @return The file loading command, or @p nullptr if the archive has already been loaded
99 /// or the holder is empty.
101
102 /// @brief Gets the archive accessor.
103 /// @return The archive accessor, or @p nullptr if the archive has not yet been loaded
104 /// or the holder is empty.
105 EGG::Archive *getArchive() const { return mpArchive; }
106
107 void incRefCount() { mRefCount++; } ///< Increments the reference count.
108 void decRefCount() { mRefCount--; } ///< Decrements the reference count.
109
110 private:
111
112 /**
113 * @brief Builds the resource list and executes a callback on each file and directory.
114 * @param callback The resource loaded callback, or @p nullptr .
115 * @return The result of the operation.
116 */
118
119 char mName[0x20]; ///< The name of the archive.
120 u16 mRefCount; ///< The number of references to this archive.
121 mDvd_mountMemArchive_c *mpDvdCmd; ///< The DVD command for mounting the archive.
122 EGG::Archive *mpArchive; ///< The accessor for this archive.
123 EGG::Heap *mpMountHeap; ///< The heap used for mounting the archive.
124 void *mpArcBinary; ///< The raw archive data.
125 int mArchiveSize; ///< The archive size.
126
127 /// @brief The heap used for loading the resources of the archive.
128 /// @decompnote{No p because of the string "dRes_c::info_c::mDataHeap"}
130
131 u8 **mpFiles; ///< An array of pointers to the data of each loaded resource.
132 };
133
134 dRes_c(); ///< Constructs a new manager.
135 ~dRes_c(); ///< Destroys the manager.
136
137 /**
138 * @brief Initializes the manager by allocating the archive holders and setting the callback.
139 *
140 * @param maxCount The number of archive holders to allocate.
141 * @param callback The resource loaded callback.
142 * @return Whether the initialization was successful.
143 */
144 bool init(u16 maxCount, callback_c *callback);
145
146 /**
147 * @brief Schedules an archive for loading.
148 * @param arcName The name of the archive to load. See the [path notes](#path-notes).
149 * @param containingFolder The path to the folder the archive is in. See the [path notes](#path-notes).
150 * @param allocDir The allocation direction. See ::MEMExpHeapAllocDir.
151 * @param heap The heap to load the archive into, or @p nullptr to use the default archive heap.
152 * @return Whether the operation was successful.
153 */
154 bool setRes(const char *arcName, const char *containingFolder, u8 allocDir, EGG::Heap *heap);
155
156 /**
157 * @brief Marks an archive as no longer needed.
158 *
159 * @param arcName The name of the archive to unload. See the [path notes](#path-notes).
160 * @return Whether the operation was successful.
161 */
162 bool deleteRes(const char *arcName);
163
164 /**
165 * @brief Gets a resource.
166 *
167 * @param arcName The name of the archive which contains the resource. See the [path notes](#path-notes).
168 * @param resPath The path to the resource within the archive. See the [path notes](#path-notes).
169 * @return A pointer to the contents of the resource.
170 */
171 void *getRes(const char *arcName, const char *resPath) const;
172
173 /**
174 * @brief Gets a resource.
175 *
176 * @param arcName The name of the archive which contains the resource. See the [path notes](#path-notes).
177 * @param resPath The path to the resource within the archive. See the [path notes](#path-notes).
178 * @param size A pointer where the size of the resource will be written to.
179 * @return A pointer to the contents of the resource.
180 */
181 void *getRes(const char *arcName, const char *resPath, unsigned long *size) const;
182
183 /**
184 * @brief Gets a resource which may optionally be compressed.
185 * @details If the resource is available in both compressed and uncompressed form, the latter is
186 * prioritized. Only LZ77 compression is supported (the resource must be named @p <resPath>\.LZ
187 * for it to be found).
188 *
189 * @param arcName The name of the archive which contains the resource. See the [path notes](#path-notes).
190 * @param resPath The path to the resource within the archive. See the [path notes](#path-notes).
191 * @param size A pointer where the uncompressed size of the resource will be written to, or @p nullptr .
192 * @param compressionType A pointer where the compression type of the resource will be written to, or
193 * @p nullptr . See ::CXCompressionType.
194 * @return A pointer to the compressed contents of the resource.
195 */
196 void *getRes(const char *arcName, const char *resPath, unsigned long *size, int *compressionType) const;
197
198 /// @brief Gets a resource without logging a message if the resource is not found.
199 /// @see ::getRes(const char*, const char*) const
200 void *getResSilently(const char *arcName, const char *resPath) const;
201
202 /// @brief Gets a resource without logging a message if the resource is not found.
203 /// @see ::getRes(const char*, const char*, unsigned long*) const
204 void *getResSilently(const char *arcName, const char *resPath, unsigned long *size) const;
205
206 /// @brief Attempts to load the resources of an archive that has finished loading since the last call to this function. The callback is executed on all files and folders.
207 /// @return Whether such an archive was found.
208 bool syncAllRes();
209
210private:
211 /**
212 * @brief Gets the holder for an archive.
213 * @param arcName The name of the archive.
214 * @return The holder for the archive, or @p nullptr if the holder doesn't exist.
215 */
216 info_c *getResInfo(const char *arcName) const;
217
218 /**
219 * @brief Gets the holder for a loaded archive.
220 * @param arcName The name of the archive.
221 * @return The holder for the archive, or @p nullptr if the holder doesn't exist or the archive
222 * hasn't been loaded into memory yet.
223 */
224 info_c *getResInfoLoaded(const char *arcName) const;
225
226 /// @brief Gets a free archive holder that can be used to store an archive reference.
227 /// @return The free archive holder, or @p nullptr if none is available.
229
230public:
231 /// @brief Copies an uncompressed resource.
232 static void copyRes(const void *from, void *to, int size);
233
234 /// @brief Copies an optionally compressed resource.
235 /// @param size The size of the data. Only used for uncompressed resources.
236 /// @param compressionType The compression type. See ::CXCompressionType for the possible values.
237 static void copyRes(const void *from, void *to, int size, int compressionType);
238
239private:
240 info_c *mpArcInfos; ///< An array of archive holders.
241 u16 mNumArcs; ///< The number of archive holders.
242 callback_c *mpCallback; ///< The callback for when a resource is loaded.
243
244 /// @brief The callback for when an archive is scheduled for loading.
245 /// @unused
246 static void (*mSetCallback)(const char *arcName, EGG::Heap *heap);
247};
A callback class for processing resources.
Definition d_res.hpp:15
virtual void init(const char *name)=0
Initializes the callback with the resource name.
virtual void * execute(void *data, u32 folderSig, const char *path)=0
Executes the callback.
An archive holder.
Definition d_res.hpp:58
EGG::Archive * mpArchive
The accessor for this archive.
Definition d_res.hpp:122
bool set(const char *arcName, const char *containingFolder, u8 allocDirection, EGG::Heap *heap)
Sets information about the archive to be loaded.
mDvd_mountMemArchive_c * getDvdCmd() const
Gets the file loading command.
Definition d_res.hpp:100
~info_c()
Destroys the archive holder.
LOAD_STATUS_e
The loading status of the archive.
Definition d_res.hpp:62
@ LOAD_IN_PROGRESS
The archive is currently being loaded.
Definition d_res.hpp:65
@ LOAD_SUCCESS
The archive was loaded successfully.
Definition d_res.hpp:64
@ LOAD_ERROR
An error occurred while loading the archive.
Definition d_res.hpp:63
info_c()
Constructs a new archive holder.
Definition d_res_info.cpp:7
EGG::Heap * mpMountHeap
The heap used for mounting the archive.
Definition d_res.hpp:123
mDvd_mountMemArchive_c * mpDvdCmd
The DVD command for mounting the archive.
Definition d_res.hpp:121
void decRefCount()
Decrements the reference count.
Definition d_res.hpp:108
char mName[0x20]
The name of the archive.
Definition d_res.hpp:119
void * mpArcBinary
The raw archive data.
Definition d_res.hpp:124
u8 ** mpFiles
An array of pointers to the data of each loaded resource.
Definition d_res.hpp:131
int mArchiveSize
The archive size.
Definition d_res.hpp:125
void incRefCount()
Increments the reference count.
Definition d_res.hpp:107
LOAD_STATUS_e loadRes(callback_c *callback)
Builds the resource list and executes a callback on each file and directory.
EGG::FrmHeap * mDataHeap
The heap used for loading the resources of the archive.
Definition d_res.hpp:129
u16 mRefCount
The number of references to this archive.
Definition d_res.hpp:120
EGG::Archive * getArchive() const
Gets the archive accessor.
Definition d_res.hpp:105
bool cleanup()
Unmounts the archive and frees the holder for use.
LOAD_STATUS_e setRes(callback_c *callback)
Attempts to load the archive into memory and load the resources with a callback.
searchCallback_c(callback_c *callback, u8 **files, int numFiles, int fileIdx, u32 folderSig)
Constructs a new searchCallback_c.
Definition d_res.hpp:35
unsigned int mFileIdx
The index of the current file in mpFiles.
Definition d_res.hpp:53
callback_c * mpCallback
The file processing callback.
Definition d_res.hpp:50
u32 mFolderSig
The first 4 characters of the current folder.
Definition d_res.hpp:54
u8 ** mpFiles
An array of pointers to the data of each loaded resource.
Definition d_res.hpp:51
static void callback(void *cbInfo, void *file, const ARCDirEntry *dirEntry, const char *path)
The callback function.
int mNumFiles
The number of files in mpFiles.
Definition d_res.hpp:52
~dRes_c()
Destroys the manager.
Definition d_res.cpp:13
info_c * newResInfo()
Gets a free archive holder that can be used to store an archive reference.
Definition d_res.cpp:206
u16 mNumArcs
The number of archive holders.
Definition d_res.hpp:241
dRes_c()
Constructs a new manager.
Definition d_res.cpp:7
static void copyRes(const void *from, void *to, int size)
Copies an uncompressed resource.
Definition d_res.cpp:130
info_c * getResInfoLoaded(const char *arcName) const
Gets the holder for a loaded archive.
Definition d_res.cpp:218
static void(* mSetCallback)(const char *arcName, EGG::Heap *heap)
The callback for when an archive is scheduled for loading.
Definition d_res.hpp:246
callback_c * mpCallback
The callback for when a resource is loaded.
Definition d_res.hpp:242
bool syncAllRes()
Attempts to load the resources of an archive that has finished loading since the last call to this fu...
Definition d_res.cpp:181
bool deleteRes(const char *arcName)
Marks an archive as no longer needed.
Definition d_res.cpp:50
void * getRes(const char *arcName, const char *resPath) const
Gets a resource.
Definition d_res.cpp:64
info_c * mpArcInfos
An array of archive holders.
Definition d_res.hpp:240
void * getResSilently(const char *arcName, const char *resPath) const
Gets a resource without logging a message if the resource is not found.
Definition d_res.cpp:142
bool setRes(const char *arcName, const char *containingFolder, u8 allocDir, EGG::Heap *heap)
Schedules an archive for loading.
Definition d_res.cpp:32
bool init(u16 maxCount, callback_c *callback)
Initializes the manager by allocating the archive holders and setting the callback.
Definition d_res.cpp:21
info_c * getResInfo(const char *arcName) const
Gets the holder for an archive.
Definition d_res.cpp:194