NSMBW-Decomp
A decompilation of New Super Mario Bros. Wii
Loading...
Searching...
No Matches
d_dylink.cpp
Go to the documentation of this file.
1#include <game/bases/d_dylink.hpp>
2#include <game/mLib/m_heap.hpp>
3#include <constants/sjis_constants.h>
4/// @file
5
6/// @brief The module handler for the d_profile module.
7DynamicModuleControl s_ProfileDMC("d_profile", nullptr);
8
9/// @brief The profile to module name table.
10/// @hideinitializer
12 { fProfile::INVALID, nullptr }
13};
14
15const sDynNameTableEntry *pDynamicNameTable; ///< A pointer to @ref sDynNameTableEntry "the profile to module name table".
16int nDynamicNameTable; ///< The amount of entries in #pDynamicNameTable .
17
20EGG::FrmHeap *dDyl::cCc_frmHeap;
23
24bool dDyl::Init(int profileCount, const sDynNameTableEntry *pNameTable, int nNameTable, EGG::Heap *heap) {
25 // [Memory waste: profileCount * 4 would have been enough space to allocate pDMC]
26 cCc_frmHeap = mHeap::createFrmHeap(profileCount * 16 + nNameTable * sizeof(DynamicModuleControl),
27 heap, DYL_FRM_HEAP_NAME, 0x20, mHeap::OPT_NONE);
28 EGG::Heap *prevHeap = mHeap::setCurrentHeap(cCc_frmHeap);
29
30 nDMC = profileCount;
31 pDMC = new DynamicModuleControlBase *[profileCount];
32 memset(pDMC, 0, nDMC * sizeof(DynamicModuleControlBase *));
33
34 pDynamicNameTable = pNameTable;
35 nDynamicNameTable = nNameTable;
36 for (int i = 0; i < nDynamicNameTable; i++) {
37 const sDynNameTableEntry *curr = &pDynamicNameTable[i];
38 if (curr->mModuleName == nullptr) {
39 continue;
40 }
41 for (int j = 0; j < nDMC; j++) {
42 if (pDMC[j] == nullptr) {
43 continue;
44 }
45
46 // If the module has already been linked for another profile, use it
47 if (strcmp(curr->mModuleName, pDMC[j]->getModuleName()) == 0) {
48 pDMC[curr->mProf] = pDMC[j];
49 break;
50 }
51 }
52 if (pDMC[curr->mProf] == nullptr) {
53 pDMC[curr->mProf] = new DynamicModuleControl(curr->mModuleName, nullptr);
54 }
55 }
56
57 cCc_frmHeap->adjust();
58 mHeap::setCurrentHeap(prevHeap);
59 return true;
60}
61
63 if (false) {
64 if (pDMC[profile] == nullptr) {
65 return true;
66 }
67 return pDMC[profile]->unlink();
68 }
69
70 return true;
71}
72
74 if (false) {
75 if (pDMC[profile] == nullptr) {
76 return true;
77 }
78 return pDMC[profile]->link();
79 }
80
81 return Initialized ? 1 : 0;
82}
83
87
88 // Load string table file
89 if (DynamicModuleControl::sDvdFile->openFilename("/WIIMJ2DNP.str")) {
90 size_t fileSize = DynamicModuleControl::sDvdFile->getFileSize();
91 if (fileSize != 0) {
92 void *stringTableBuffer = DynamicModuleControl::sDylinkHeap->alloc(ROUND_UP(fileSize, 0x20), 0x20);
93 DynamicModuleControl::sDvdFile->readData(stringTableBuffer, ROUND_UP(fileSize, 0x20), 0);
94 OSSetStringTable(stringTableBuffer);
95 }
97 }
98
99 bool res = s_ProfileDMC.link();
100 while (!res); // Infinite loop if the profile module fails to link
101
102 dDyl::Initialized = true;
103 return (void *) true;
104}
105
110
112 if (DVD == nullptr) {
113 return true;
114 }
115 if (DVD->mDone) {
116 DVD->destroy();
117 DVD = nullptr;
118 return true;
119 }
120 return false;
121}
122
124
125dDynamicModuleControl::dDynamicModuleControl(const char *name, EGG::ExpHeap *heap) : DynamicModuleControl(name, heap) {}
126
Base class for managing a relocatable module.
Definition c_dylink.hpp:46
Full implementation of a dynamic module handler.
Definition c_dylink.hpp:112
virtual bool do_link() override
Module-specific implementation for linking the module, making it ready for use.
Definition c_dylink.cpp:247
static EGG::DvdFile * sDvdFile
The DVD file handle used for loading the string table file.
Definition c_dylink.hpp:168
static const char * sModulesDir
The directory on the disk where the modules are located.
Definition c_dylink.hpp:172
static void initialize(EGG::ExpHeap *heap)
Initializes the global dynamic linking system.
Definition c_dylink.cpp:106
DynamicModuleControl(const char *name, EGG::ExpHeap *heap)
Constructs a new DynamicModuleControl.
Definition c_dylink.cpp:86
static EGG::ExpHeap * sDylinkHeap
The heap used for dynamic module loading and BSS allocation.
Definition c_dylink.hpp:167
dDynamicModuleControl(const char *name, EGG::ExpHeap *heap)
Constructs a new DynamicModuleControl.
Definition d_dylink.cpp:125
virtual bool do_link() override
Module-specific implementation for linking the module, making it ready for use.
Definition d_dylink.cpp:127
virtual ~dDynamicModuleControl()
Destroys the DynamicModuleControl.
Definition d_dylink.cpp:123
void * InitCallback(void *self)
Second initialization step of the dynamic module system.
Definition d_dylink.cpp:84
u16 ProfileName
The name of a profile. Value is a fProfile::PROFILE_NAME_e.
Definition f_profile.hpp:32
@ PROFILE_COUNT
The total number of profiles.
bool Init(int profileCount, const sDynNameTableEntry *pNameTable, int nNameTable, EGG::Heap *heap)
First initialization step of the dynamic module system.
Definition d_dylink.cpp:24
DynamicModuleControlBase ** pDMC
A table that keeps track of which module is associated with each profile.
Definition d_dylink.cpp:18
EGG::FrmHeap * cCc_frmHeap
The heap to be used for allocating the module handlers and the profile to module table.
Definition d_dylink.cpp:20
void InitAsync()
Initializes the dynamic module system.
Definition d_dylink.cpp:106
int LinkASync(ProfileName profile)
Links the module associated with the given profile.
Definition d_dylink.cpp:73
bool InitAsyncIsDone()
Checks whether initialization has completed.
Definition d_dylink.cpp:111
int nDMC
The number of entries in pDMC.
Definition d_dylink.cpp:19
mDvd_callback_c * DVD
The asynchronous DVD task used during the dynamic module system initialization.
Definition d_dylink.cpp:22
bool Unlink(ProfileName profile)
Unlinks the module associated with the given profile.
Definition d_dylink.cpp:62
BOOL Initialized
Whether the dynamic module system has finished initializing.
Definition d_dylink.cpp:21
EGG::FrmHeap * createFrmHeap(size_t size, EGG::Heap *parent, const char *name, ulong align, AllocOptBit_t opt)
Creates a frame heap.
Definition m_heap.cpp:75
EGG::ExpHeap * g_dylinkHeap
The REL linking heap.
Definition m_heap.cpp:16
@ OPT_NONE
No special allocation options.
Definition m_heap.hpp:29
EGG::Heap * setCurrentHeap(EGG::Heap *heap)
Sets the specified heap as the current heap.
Definition m_heap.cpp:37
An entry in the profile to module name table.
Definition d_dylink.hpp:36
const char * mModuleName
The name of the module to load for the profile, or nullptr if the profile is statically linked.
Definition d_dylink.hpp:38
ProfileName mProf
The profile name of this entry.
Definition d_dylink.hpp:37