1#ifndef SEAD_RESOURCEMGR_H_
2#define SEAD_RESOURCEMGR_H_
3
4#include <basis/seadTypes.h>
5#include <container/seadTList.h>
6#include <filedevice/seadFileDevice.h>
7#include <heap/seadDisposer.h>
8#include <heap/seadHeap.h>
9#include <prim/seadSafeString.h>
10
11namespace sead
12{
13class Resource;
14class DirectResource;
15class ResourceFactory;
16class Decompressor;
17
18template <typename T>
19class DirectResourceFactory;
20
21class ResourceMgr
22{
23 SEAD_SINGLETON_DISPOSER(ResourceMgr)
24
25public:
26 struct CreateArg
27 {
28 u8* buffer = nullptr;
29 u32 file_size = 0;
30 u32 buffer_size = 0;
31 bool need_unload = false;
32 ResourceFactory* factory = nullptr;
33 SafeString ext;
34 Heap* heap = nullptr;
35 s32 alignment = 0x20;
36 };
37#ifdef NNSDK
38 static_assert(sizeof(CreateArg) == 0x40);
39#endif
40
41 struct LoadArg
42 {
43 SafeString path;
44 Heap* instance_heap = nullptr;
45 Heap* load_data_heap = nullptr;
46 s32 instance_alignment = 0x20;
47 s32 load_data_alignment = 0;
48 u8* load_data_buffer = nullptr;
49 u32 load_data_buffer_size = 0;
50 s32 load_data_buffer_alignment = 0;
51 ResourceFactory* factory = nullptr;
52 FileDevice* device = nullptr;
53 // Read chunk size.
54 u32 div_size = 0;
55 bool assert_on_alloc_fail = true;
56 bool* has_tried_create_with_decomp = nullptr;
57 };
58#ifdef NNSDK
59 static_assert(sizeof(LoadArg) == 0x58);
60#endif
61
62public:
63 ResourceMgr();
64 ~ResourceMgr();
65
66 Resource* create(const CreateArg& arg);
67
68 void registerFactory(ResourceFactory* factory, const SafeString& name);
69 void unregisterFactory(ResourceFactory* factory);
70 ResourceFactory* getDefaultFactory() const { return mDefaultResourceFactory; }
71 /// Set the specified factory as the default factory. Its name is set to "".
72 /// @param factory If null, a dummy resource factory is set as the default factory.
73 /// @returns the previous default factory
74 ResourceFactory* setDefaultFactory(ResourceFactory* factory);
75 ResourceFactory* findFactory(const SafeString& name);
76
77 void registerDecompressor(Decompressor* decompressor, const SafeString& name);
78 void unregisterDecompressor(Decompressor* decompressor);
79 Decompressor* findDecompressor(const SafeString& name);
80
81 Resource* tryLoad(const LoadArg& arg, const SafeString& factory_name,
82 Decompressor* decompressor);
83 Resource* tryLoadWithoutDecomp(const LoadArg& arg);
84 void unload(Resource* res);
85
86private:
87 typedef TList<ResourceFactory*> FactoryList;
88 typedef TList<Decompressor*> DecompressorList;
89
90 FactoryList mFactoryList;
91 DecompressorList mDecompList;
92 ResourceFactory* mNullResourceFactory = nullptr;
93 ResourceFactory* mDefaultResourceFactory = nullptr;
94};
95#ifdef NNSDK
96static_assert(sizeof(ResourceMgr) == 0x60);
97#endif
98
99} // namespace sead
100
101#endif // SEAD_RESOURCEMGR_H_
102