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