Skip to content

Commit 19f274f

Browse files
committed
Memory management improvements
1 parent 29b98f2 commit 19f274f

File tree

7 files changed

+90
-2
lines changed

7 files changed

+90
-2
lines changed

include/SetlistHooks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "rb3/BandLabel.h"
88
#include "rb3/SortNode.h"
99
#include "rb3/Rnd/RndMat.h"
10+
#include "rb3/Mem.h"
1011

1112
void SetSongAndArtistNameHook(BandLabel *label, SortNode *sortNode);
1213
void SetSongNameFromNodeHook(BandLabel *label, SortNode *sortNode);

include/ports.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,10 @@
146146
#define PORT_ADDMULTIGEM 0x827930d8 // GameGemDB::AddMultiGem
147147
#define PORT_SONGSORTMGRGETSORT 0x82595ff8 // SongSortMgr::GetSort
148148
#define PORT_DYNAMICTEX_CT 0x825f2318 // DynamicTex::__ct
149+
#define PORT_DYNAMICTEX_DT 0x825f2210 // DynamicTex::__dt
149150
#define PORT_RNDMATSETDIFFUSETEX 0x8238b130 // RndMat::SetDiffuseTex
151+
#define PORT_MUSICLIBRARYONENTER 0x82542238 // MusicLibrary::OnEnter
152+
#define PORT_MUSICLIBRARYONUNLOAD 0x82540450 // MusicLibrary::OnExit
150153
// instance addresses
151154
#define PORT_MODIFIERMGR_POINTER 0x82dfec08 // pointer to ModifierManager
152155
#define PORT_ROCKCENTRALGATEWAY 0x82cc8f60 // address of RockCentralGateway
@@ -289,6 +292,9 @@ void DbgPrint(const char *s, ...);
289292
#define PORT_SONGSORTMGRGETSORT 0x80281b20 // SongSortMgr::GetSort
290293
#define PORT_RNDMATSETDIFFUSETEX 0x8025ab90 // RndMat::SetDiffuseTex
291294
#define PORT_DYNAMICTEX_CT 0x80292a70 // DynamicTex::__ct
295+
#define PORT_DYNAMICTEX_DT 0x80292bcc // DynamicTex::__dt
296+
#define PORT_MUSICLIBRARYONENTER 0x8022dd24 // MusicLibrary::OnEnter
297+
#define PORT_MUSICLIBRARYONUNLOAD 0x8022e87c // MusicLibrary::OnExit
292298
// instance addresses
293299
#define PORT_MODIFIERMGR_POINTER 0x808fda68 // pointer to ModifierManager
294300
#define PORT_ROCKCENTRALGATEWAY 0x80900870 // address of RockCentralGateway

include/rb3/MusicLibrary.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,9 @@ typedef struct _MusicLibrary
1717
void MusicLibrarySelect(int theMusicLibrary, Symbol entryName, int sortType, int unk_r6);
1818
int *MusicLibraryConstructor(int *thisMusicLibrary, int *songPreview);
1919
int *MusicLibraryConstructorHook(MusicLibrary *thisMusicLibrary, int *songPreview);
20+
void MusicLibraryOnEnter(void* thisMusicLibrary);
21+
void MusicLibraryOnUnload(void* thisMusicLibrary);
22+
void MusicLibraryOnEnterHook(void* thisMusicLibrary);
23+
void MusicLibraryOnUnloadHook(void* thisMusicLibrary);
2024

2125
#endif // _MUSICLIBRARY_H_

include/rb3/Rnd/DynamicTex.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ typedef struct _DynamicTex
1414
char unk; // 0x1C
1515
} DynamicTex;
1616

17-
void DynamicTexConstructor(void* thisDynamicTex, const char * path, const char* matName, char createNewMat, char enableZBuffer);
17+
void DynamicTexConstructor(DynamicTex* thisDynamicTex, const char * path, const char* matName, char createNewMat, char enableZBuffer);
18+
void DynamicTexDestructor(DynamicTex* thisDynamicTex, int unk);
1819

1920

2021

source/SetlistHooks.c

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ void CreateMaterial(GameOriginInfo *info)
3434
// it will free the dynamic tex itself, the material it created, and the texture too, so it nicely wraps it all up for you
3535
// this way there is not a chunk of memory permanently dedicated to game origin icons (even if it is not a large amount)
3636
tex = MemAlloc(0x20, 0);
37+
if (tex == NULL) {
38+
RB3E_DEBUG("MemAlloc failed for dynamic tex '%s'", info->gameOrigin);
39+
return;
40+
}
3741

3842
// build the ark path (so dont include /gen/ or the _platform extension etc.)
3943
RB3E_DEBUG("Creating dynamic tex for game origin '%s'", info->gameOrigin);
@@ -43,6 +47,13 @@ void CreateMaterial(GameOriginInfo *info)
4347

4448
// create and pray
4549
DynamicTexConstructor(tex, path, info->gameOrigin, 1, 0);
50+
if (tex->mMat == NULL || tex->mTex == NULL) {
51+
RB3E_DEBUG("DynamicTex::__ct failed for dynamic tex '%s'", path);
52+
53+
// we should prob do cleanup here but that is a
54+
// TODO
55+
return;
56+
}
4657

4758
textures[info->num] = tex;
4859

@@ -60,6 +71,8 @@ void CreateMaterial(GameOriginInfo *info)
6071
// print the material name
6172
RB3E_DEBUG("Dynamic tex created at %p with material '%s'", textures[info->num], textures[info->num]->mMatName.buf);
6273

74+
RB3E_DEBUG("Dynamic tex created at %p with mat %p tex %p fileloader %p", tex, tex->mMat, tex->mTex, tex->mFileLoader);
75+
6376

6477
}
6578

@@ -123,6 +136,64 @@ RndMat *MusicLibraryMatHook(MusicLibrary *thisMusicLibrary, int data, int idx, U
123136
return MusicLibraryMat(thisMusicLibrary, data, idx, listSlot);
124137
}
125138

139+
// called when entering the music library
140+
// allocate the memory and load the textures here
141+
void MusicLibraryOnEnterHook(void *thisMusicLibrary)
142+
{
143+
int i;
144+
145+
// allocate and create the materials for any game origins we found while loading songs
146+
for (i = 0; i < numGameOrigins; i++) {
147+
int slot = originInfo[i].num;
148+
if (slot >= 0 && slot < 100) {
149+
if (textures[slot] == NULL) {
150+
CreateMaterial(&originInfo[i]);
151+
}
152+
}
153+
}
154+
155+
// do the original logic
156+
MusicLibraryOnEnter(thisMusicLibrary);
157+
}
158+
159+
// called when the music library panel is unloaded
160+
void MusicLibraryOnUnloadHook(void *thisMusicLibrary)
161+
{
162+
int i;
163+
164+
MusicLibraryOnUnload(thisMusicLibrary);
165+
166+
// call the dynamictex destructor for any textures we created
167+
// lets just sweep all 100 to be safe
168+
for (i = 0; i < 100; i++) {
169+
if (textures[i] != NULL) {
170+
// we can't call the DynamicTexDestructor because the FileLoader it used has already been freed by this point, so we must destruct everything but the FileLoader
171+
// extremely nasty but it seems to work
172+
if(textures[i]->mMat != NULL) {
173+
((Object *)textures[i]->mMat)->table->destructor((Object *)textures[i]->mMat);
174+
textures[i]->mMat = NULL;
175+
}
176+
if(textures[i]->mTex != NULL) {
177+
((Object *)textures[i]->mTex)->table->destructor((Object *)textures[i]->mTex);
178+
textures[i]->mTex = NULL;
179+
}
180+
// TODO: set up a proper vtable for String instead of doign this shit
181+
if(textures[i]->mMatName.length != 0) {
182+
if(textures[i]->mMatName.vtable != NULL) {
183+
#ifdef RB3E_WII
184+
((void (*)(String *))textures[i]->mMatName.vtable[2])(&textures[i]->mMatName);
185+
#else
186+
((void (*)(String *))textures[i]->mMatName.vtable[0])(&textures[i]->mMatName);
187+
#endif
188+
}
189+
}
190+
textures[i] = NULL;
191+
}
192+
}
193+
194+
RB3E_DEBUG("Freed game origin icon textures", NULL);
195+
}
196+
126197
int numGameOrigins;
127198
GameOriginInfo originInfo[100] = {0};
128199

@@ -150,7 +221,6 @@ void AddGameOriginToIconList(char *gameOrigin)
150221

151222
originInfo[numGameOrigins].gameOrigin = gameOrigin;
152223
originInfo[numGameOrigins].num = numGameOrigins;
153-
CreateMaterial(&originInfo[numGameOrigins]);
154224
numGameOrigins++;
155225
RB3E_DEBUG("Adding game origin '%s' to icon list, total is now %i", gameOrigin, numGameOrigins);
156226
}

source/_functions.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ RB3E_STUB(RndTexSetBitmap)
9898
RB3E_STUB(RndTexSetBitmap2)
9999
RB3E_STUB(FilePathConstructor)
100100
RB3E_STUB(MusicLibraryMat)
101+
RB3E_STUB(MusicLibraryOnEnter)
102+
RB3E_STUB(MusicLibraryOnUnload)
101103
RB3E_STUB(RndCreateDefaultTexture)
102104
RB3E_STUB(MusicLibraryConstructor)
103105
RB3E_STUB(MusicLibraryGetNodeByIndex)
@@ -112,5 +114,6 @@ RB3E_STUB(SongMetadataLoad)
112114
RB3E_STUB(SongSortMgrGetSort)
113115
RB3E_STUB(NodeSortGetNode)
114116
RB3E_STUB(DynamicTexConstructor)
117+
RB3E_STUB(DynamicTexDestructor)
115118
RB3E_STUB(RndMatSetDiffuseTex)
116119
RB3E_STUB(RndTexSetBitmap3)

source/rb3enhanced.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ void InitialiseFunctions()
334334
POKE_B(&GameGemDBConstructor, PORT_GAMEGEMDB_CT);
335335
POKE_B(&SongSortMgrGetSort, PORT_SONGSORTMGRGETSORT);
336336
POKE_B(&DynamicTexConstructor, PORT_DYNAMICTEX_CT);
337+
POKE_B(&DynamicTexDestructor, PORT_DYNAMICTEX_DT);
337338
POKE_B(&RndMatSetDiffuseTex, PORT_RNDMATSETDIFFUSETEX);
338339
POKE_B(&RndTexSetBitmap3, PORT_RNDTEXSETBITMAP3);
339340
RB3E_MSG("Functions initialized!", NULL);
@@ -371,6 +372,8 @@ void ApplyHooks()
371372
HookFunction(PORT_UPDATEPRESENCE, &UpdatePresence, &UpdatePresenceHook);
372373
HookFunction(PORT_MUSICLIBRARY_CT, &MusicLibraryConstructor, &MusicLibraryConstructorHook);
373374
HookFunction(PORT_MUSICLIBRARYMAT, &MusicLibraryMat, &MusicLibraryMatHook);
375+
HookFunction(PORT_MUSICLIBRARYONENTER, &MusicLibraryOnEnter, &MusicLibraryOnEnterHook);
376+
HookFunction(PORT_MUSICLIBRARYONUNLOAD, &MusicLibraryOnUnload, &MusicLibraryOnUnloadHook);
374377
#ifdef RB3E_WII // wii exclusive hooks
375378
HookFunction(PORT_USBWIIGETTYPE, &UsbWiiGetType, &UsbWiiGetTypeHook);
376379
HookFunction(PORT_WIINETINIT_DNSLOOKUP, &StartDNSLookup, &StartDNSLookupHook);

0 commit comments

Comments
 (0)