Skip to content

Commit c773aa3

Browse files
author
djevangelia
committed
Object_LoadExtra add size check to vanilla function
1 parent f52ed99 commit c773aa3

5 files changed

Lines changed: 42 additions & 8 deletions

File tree

ASM/c/object.c

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,33 @@ int16_t Object_LoadExtra(z64_game_t* play, int16_t objectId, uint8_t syncDma) {
4040
}
4141
}
4242

43-
if(slot != -1) {
43+
if (slot != -1) {
4444
// Take the found slot
4545
z64_mem_obj_t* newEntry = &play->obj_ctxt.objects[slot]; // The new object
4646
z64_mem_obj_t* lastEntry = &play->obj_ctxt.objects[play->obj_ctxt.n_objects-1]; // Previous last added object
4747
RomFile* lastObjectFile = &gObjectTable[ABS(lastEntry->id)]; // Get previous object start pointer and size
4848
uint32_t lastSize = lastObjectFile->vromEnd - lastObjectFile->vromStart;
4949
newEntry->data = (void*)ALIGN16((uintptr_t)lastEntry->data + lastSize); // Gives pointer to start for the new object data
5050

51-
func_800982FC(&play->obj_ctxt, slot, objectId); // Set up data for adding the object to the slot on next update
52-
play->obj_ctxt.n_objects++;
51+
// Set up data for adding the object to the slot on next update or now
52+
if (func_800982FC(&play->obj_ctxt, slot, objectId) != NULL) {
53+
play->obj_ctxt.n_objects++;
5354

54-
if (syncDma) {
55-
play->obj_ctxt.objects[slot].id = objectId; // Uninvert the id because it will get loaded now
56-
RomFile* objectFile = &gObjectTable[ABS(objectId)]; // Get object start pointer and size
57-
uint32_t size = objectFile->vromEnd - objectFile->vromStart;
58-
DmaMgr_RequestSync(newEntry->data, objectFile->vromStart, size);
55+
if (syncDma) {
56+
play->obj_ctxt.objects[slot].id = objectId; // Uninvert the id because it will get loaded now
57+
RomFile* objectFile = &gObjectTable[objectId]; // Get object start pointer and size
58+
uint32_t size = objectFile->vromEnd - objectFile->vromStart;
59+
DmaMgr_RequestSync(newEntry->data, objectFile->vromStart, size);
60+
}
61+
62+
} else { // Loading new object would exceed object space
63+
#ifdef DEBUG_MODE
64+
char msg[256];
65+
sprintf(msg, "Obj %d slot %d", objectId, slot);
66+
Fault_AddHungupAndCrashImpl("Object_LoadExtra: No memory", msg);
67+
#endif
68+
69+
return -1; // Don't crash non-debug mode for now but this should not happen!
5970
}
6071
}
6172

ASM/src/build.asm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ RANDO_CONTEXT:
135135
.include "big_poe.asm"
136136
.include "player_ladder_cutscene.asm"
137137
.include "enbox_callloadobject.asm"
138+
.include "object_checkmaxobjectspace.asm"
138139

139140
.align 0x10
140141
.importobj "../build/bundle.o"

ASM/src/hacks.asm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4163,3 +4163,4 @@ DemoEffect_DrawJewel_AfterHook:
41634163
.include "hacks/sound.asm"
41644164
.include "hacks/z_player.asm"
41654165
.include "hacks/en_box.asm"
4166+
.include "hacks/z_scene.asm"

ASM/src/hacks/z_scene.asm

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.headersize (0x80114dd0 - 0x00b8ad30)
2+
3+
; Object load: Add check to ensure that new object to be loaded
4+
; fits within object context space, else return NULL
5+
; Replaces addu t1,t4,t0
6+
; addiu t1,t1,15
7+
; and v0,t1,at
8+
.org 0x8008178c ; in func_800982FC
9+
jal Object_CheckMaxObjectSpace
10+
addu t1,t4,t0 ; displaced
11+
lw ra,12(sp) ; ra is already saved by another hack, but not saved in vanilla func_800982FC
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Object_CheckMaxObjectSpace:
2+
addiu t1,t1,15 ; displaced
3+
and v0,t1,at ; displaced
4+
lw t1,4(a0) ; objectCtx space end
5+
sltu at,v0,t1 ; next object start pointer < space end?
6+
beqzl at,@@Return ; if yes, return pointer (v0)
7+
move v0,zero ; if not - memory full, return NULL, let caller deal with it
8+
@@Return:
9+
jr ra
10+
nop

0 commit comments

Comments
 (0)