Skip to content

Commit 690df5a

Browse files
committed
Added an error display for when npcNameToPtr fails to find a correct NPC
1 parent e3668c1 commit 690df5a

File tree

7 files changed

+98
-11
lines changed

7 files changed

+98
-11
lines changed

ttyd-tools/rel/include/draw.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ void drawActionCommandsTiming();
123123
void drawSettingsCurrentWork();
124124

125125
void drawHeapArrayErrors();
126+
void drawNpcNameToPtrError();
126127
void drawTitleScreenInfo();
127128
void drawFileSelectScreenInfo();
128129

ttyd-tools/rel/include/global.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,12 @@ struct ErrorHandlerStrings
809809
char InstructionAndAddress[128];
810810
};
811811

812+
struct NpcNameToPtrErrorStruct
813+
{
814+
uint32_t Timer;
815+
char Buffer[32];
816+
};
817+
812818
extern MenuVars MenuVar;
813819
extern Menus Menu[30];
814820
extern Cheats Cheat[24];
@@ -840,6 +846,7 @@ extern MemoryCardStruct MenuSettings;
840846
extern WarpByEventStruct WarpByEvent;
841847
extern WarpByIndexStruct WarpByIndex;
842848
extern MenuPrevMenuAndOption PrevMenuAndOption;
849+
extern NpcNameToPtrErrorStruct NpcNameToPtrError;
843850

844851
extern uint8_t CheatsOrder[];
845852
extern uint16_t StatsMarioIcons[];

ttyd-tools/rel/include/mod.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Mod
3333
void drawArtAttackHitboxes(ttyd::dispdrv::CameraId);
3434
int32_t displayActionCommandsTimingHook(void *, ttyd::battle_unit::AttackParams *);
3535
void errorHandler(uint16_t, gc::OSContext::OSContext *, uint32_t, uint32_t);
36+
ttyd::npcdrv::NpcEntry *npcNameToPtr_New(const char *);
3637

3738
private:
3839
void (*mPFN_marioStMain_trampoline)() = nullptr;
@@ -57,6 +58,8 @@ class Mod
5758

5859
void (*mPFN_systemErrorHandler_trampoline)(uint16_t,
5960
gc::OSContext::OSContext *, uint32_t, uint32_t) = nullptr;
61+
62+
ttyd::npcdrv::NpcEntry *(*mPFN_npcNameToPtr_New_trampoline)(const char *) = nullptr;
6063
};
6164

6265
}

ttyd-tools/rel/source/draw.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4474,7 +4474,7 @@ void drawHeapArrayErrors()
44744474
uint32_t TextColor = 0xFFFFFFFF;
44754475
uint8_t Alpha = 0xFF;
44764476
int32_t PosX = -232;
4477-
int32_t PosY = 120;
4477+
int32_t PosY = 100;
44784478
float Scale = 0.6;
44794479

44804480
// Draw the text
@@ -4485,6 +4485,25 @@ void drawHeapArrayErrors()
44854485
clearMemory(tempHeapBuffer, sizeof(HeapBuffer));
44864486
}
44874487

4488+
void drawNpcNameToPtrError()
4489+
{
4490+
uint32_t TextColor = 0xFFFFFFFF;
4491+
uint8_t Alpha = 0xFF;
4492+
int32_t PosX = -232;
4493+
int32_t PosY = 120;
4494+
float Scale = 0.6;
4495+
4496+
// Draw the text
4497+
char *tempBuffer = NpcNameToPtrError.Buffer;
4498+
drawText(tempBuffer, PosX, PosY, Alpha, TextColor, Scale);
4499+
4500+
// Clear the buffer if the timer is at 0
4501+
if (NpcNameToPtrError.Timer == 0)
4502+
{
4503+
clearMemory(tempBuffer, sizeof(NpcNameToPtrError.Buffer));
4504+
}
4505+
}
4506+
44884507
void drawTitleScreenInfo()
44894508
{
44904509
// Draw the window for the text

ttyd-tools/rel/source/global.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,6 +1851,7 @@ struct MemoryCardStruct MenuSettings;
18511851
struct WarpByEventStruct WarpByEvent;
18521852
struct WarpByIndexStruct WarpByIndex;
18531853
struct MenuPrevMenuAndOption PrevMenuAndOption;
1854+
struct NpcNameToPtrErrorStruct NpcNameToPtrError;
18541855

18551856
void initMenuVars()
18561857
{

ttyd-tools/rel/source/main.cpp

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -476,18 +476,18 @@ bool Mod::performRelPatches(gc::OSModule::OSModuleInfo *newModule, void *bss)
476476
}
477477
}
478478

479-
void addTextToHeapArray(char *text)
479+
void addTextToHeapBuffer(char *text)
480480
{
481481
char *tempHeapBuffer = HeapBuffer;
482482

483483
// Make sure adding the new text will not result in an overflow
484484
uint32_t NewTextSize = strlen(text);
485-
uint32_t CurrentHeapSize = strlen(tempHeapBuffer);
485+
uint32_t CurrentHeapBufferSize = strlen(tempHeapBuffer);
486486

487-
uint32_t NewHeapSize = CurrentHeapSize + NewTextSize + 1;
488-
uint32_t MaxHeapSize = sizeof(HeapBuffer);
487+
uint32_t NewHeapBufferSize = CurrentHeapBufferSize + NewTextSize + 1;
488+
uint32_t MaxHeapBufferSize = sizeof(HeapBuffer);
489489

490-
if (NewHeapSize > MaxHeapSize)
490+
if (NewHeapBufferSize > MaxHeapBufferSize)
491491
{
492492
// Adding the new text will result in an overflow, so don't add it
493493
return;
@@ -582,7 +582,7 @@ void checkHeaps()
582582
currentChunk);
583583

584584
// Add the text to the heap buffer
585-
addTextToHeapArray(tempDisplayBuffer);
585+
addTextToHeapBuffer(tempDisplayBuffer);
586586
}
587587

588588
// Check the free entries
@@ -595,7 +595,7 @@ void checkHeaps()
595595
currentChunk);
596596

597597
// Add the text to the heap buffer
598-
addTextToHeapArray(tempDisplayBuffer);
598+
addTextToHeapBuffer(tempDisplayBuffer);
599599
}
600600
}
601601

@@ -611,7 +611,7 @@ void checkHeaps()
611611
currentChunk);
612612

613613
// Add the text to the heap buffer
614-
addTextToHeapArray(tempDisplayBuffer);
614+
addTextToHeapBuffer(tempDisplayBuffer);
615615
}
616616

617617
// Check the free entries
@@ -623,7 +623,7 @@ void checkHeaps()
623623
currentChunk);
624624

625625
// Add the text to the heap buffer
626-
addTextToHeapArray(tempDisplayBuffer);
626+
addTextToHeapBuffer(tempDisplayBuffer);
627627
}
628628

629629
// Draw any errors that occured
@@ -633,6 +633,55 @@ void checkHeaps()
633633
}
634634
}
635635

636+
ttyd::npcdrv::NpcEntry *Mod::npcNameToPtr_New(const char *name)
637+
{
638+
// Get the work pointer
639+
ttyd::npcdrv::NpcWork *NpcWorkPointer = ttyd::npcdrv::npcGetWorkPtr();
640+
641+
// Loop through the NPCs to find the correct one
642+
ttyd::npcdrv::NpcEntry *NPC = nullptr;
643+
uint32_t MaxNpcCount = NpcWorkPointer->npcMaxCount;
644+
645+
for (uint32_t i = 0; i < MaxNpcCount; i++)
646+
{
647+
NPC = &NpcWorkPointer->entries[i];
648+
if (NPC->flags & (1 << 0)) // Check if 0 bit is active
649+
{
650+
if (compareStrings(NPC->wUnkAnimation, name))
651+
{
652+
return NPC;
653+
}
654+
}
655+
}
656+
657+
// Didn't find the correct NPC, so return the last one and print error text
658+
// Only print the error text if currently not printing any previous error text
659+
if (NpcNameToPtrError.Timer == 0)
660+
{
661+
const char *Text = "npcNameToPtr error occured";
662+
strcpy(NpcNameToPtrError.Buffer, Text);
663+
NpcNameToPtrError.Timer = secondsToFrames(5);
664+
}
665+
666+
return NPC;
667+
}
668+
669+
void displayNpcNameToPtrError()
670+
{
671+
uint32_t Timer = NpcNameToPtrError.Timer;
672+
if (Timer > 0)
673+
{
674+
Timer--;
675+
NpcNameToPtrError.Timer = Timer;
676+
}
677+
678+
// Call the drawing function regardless of the current time, as the drawing function is what clears the buffer
679+
if (NpcNameToPtrError.Buffer[0] != '\0')
680+
{
681+
drawFunctionOnDebugLayerWithOrder(drawNpcNameToPtrError, 100.f);
682+
}
683+
}
684+
636685
void initAddressOverwrites()
637686
{
638687
#ifdef TTYD_US
@@ -818,6 +867,7 @@ void Mod::run()
818867
displayMarioSpeedXZ();
819868
displayStickAngle();
820869
displayMemoryWatches();
870+
displayNpcNameToPtrError();
821871
}
822872

823873
// Only run button-based displays if currently not changing button combos

ttyd-tools/rel/source/mod.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,13 @@ void Mod::init()
139139
StartErrorHandlerEnableFPU();
140140
gMod->errorHandler(error, context, dsisr, dar);
141141
});
142-
142+
143+
mPFN_npcNameToPtr_New_trampoline = patch::hookFunction(
144+
ttyd::npcdrv::npcNameToPtr, [](const char *name)
145+
{
146+
return gMod->npcNameToPtr_New(name);
147+
});
148+
143149
// Initialize typesettings early
144150
// Only run if an ACE loader was not used
145151
uint32_t LoaderValue = *reinterpret_cast<uint32_t *>(0x80004148);

0 commit comments

Comments
 (0)