Skip to content

Commit bfdaa25

Browse files
committed
Made several adjustments to the npcNameToPtr stuff
Adjusted the replacement function to simply call the original function, and then check if the returned pointer is valid.This change also fixed an issue where the function did not return the same pointer that the vanilla one would, as the vanilla one ends up going out of range when it fails to find a proper NPC. Also changed the error text to display the number of times that the function returned an invalid pointer. Every time the number is incremented, the timer is reset to 5 seconds.
1 parent 95069ba commit bfdaa25

File tree

3 files changed

+17
-36
lines changed

3 files changed

+17
-36
lines changed

ttyd-tools/rel/include/global.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -811,8 +811,8 @@ struct ErrorHandlerStrings
811811

812812
struct NpcNameToPtrErrorStruct
813813
{
814-
uint32_t Timer;
815-
char Buffer[32];
814+
uint32_t Counter;
815+
uint16_t Timer;
816816
};
817817

818818
extern MenuVars MenuVar;

ttyd-tools/rel/source/draw.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4494,14 +4494,12 @@ void drawNpcNameToPtrError()
44944494
float Scale = 0.6;
44954495

44964496
// Draw the text
4497-
char *tempBuffer = NpcNameToPtrError.Buffer;
4498-
drawText(tempBuffer, PosX, PosY, Alpha, TextColor, Scale);
4497+
char *tempDisplayBuffer = DisplayBuffer;
4498+
sprintf(tempDisplayBuffer,
4499+
"npcNameToPtr error occured x%" PRIu32,
4500+
NpcNameToPtrError.Counter);
44994501

4500-
// Clear the buffer if the timer is at 0
4501-
if (NpcNameToPtrError.Timer == 0)
4502-
{
4503-
clearMemory(tempBuffer, sizeof(NpcNameToPtrError.Buffer));
4504-
}
4502+
drawText(tempDisplayBuffer, PosX, PosY, Alpha, TextColor, Scale);
45054503
}
45064504

45074505
void drawTitleScreenInfo()

ttyd-tools/rel/source/main.cpp

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -635,32 +635,16 @@ void checkHeaps()
635635

636636
ttyd::npcdrv::NpcEntry *Mod::npcNameToPtr_New(const char *name)
637637
{
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-
}
638+
// Call the original function right away
639+
ttyd::npcdrv::NpcEntry *NPC = mPFN_npcNameToPtr_New_trampoline(name);
656640

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)
641+
// Check if the returned pointer is valid
642+
ttyd::npcdrv::NpcWork *NpcWorkPointer = ttyd::npcdrv::npcGetWorkPtr();
643+
if (NPC == &NpcWorkPointer->entries[NpcWorkPointer->npcMaxCount])
660644
{
661-
const char *Text = "npcNameToPtr error occured";
662-
strcpy(NpcNameToPtrError.Buffer, Text);
645+
// Didn't find the correct NPC, so print error text
663646
NpcNameToPtrError.Timer = secondsToFrames(5);
647+
NpcNameToPtrError.Counter++;
664648
}
665649

666650
return NPC;
@@ -673,12 +657,11 @@ void displayNpcNameToPtrError()
673657
{
674658
Timer--;
675659
NpcNameToPtrError.Timer = Timer;
660+
drawFunctionOnDebugLayerWithOrder(drawNpcNameToPtrError, 100.f);
676661
}
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')
662+
else
680663
{
681-
drawFunctionOnDebugLayerWithOrder(drawNpcNameToPtrError, 100.f);
664+
NpcNameToPtrError.Counter = 0;
682665
}
683666
}
684667

0 commit comments

Comments
 (0)