Skip to content

Commit 72a6779

Browse files
committed
Add templates for several writeBranch functions
1 parent 09e799c commit 72a6779

File tree

3 files changed

+44
-30
lines changed

3 files changed

+44
-30
lines changed

ttyd-tools/rel/include/patch.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,34 @@
55
namespace mod::patch {
66

77
void clear_DC_IC_Cache(void *ptr, uint32_t size);
8-
void writeStandardBranches(void *address, void functionStart(), void functionBranchBack());
98
void writeBranch(void *ptr, void *destination);
109
void writeBranchBL(void *ptr, void *destination);
1110
void writeBranchMain(void *ptr, void *destination, uint32_t branch);
1211

12+
template<typename Func>
13+
void writeBranch_Template(void *ptr, Func destination)
14+
{
15+
uint32_t branch = 0x48000000; // b
16+
writeBranchMain(ptr, reinterpret_cast<void *>(destination), branch);
17+
}
18+
19+
template<typename Func>
20+
void writeBranchBL_Template(void *ptr, Func destination)
21+
{
22+
uint32_t branch = 0x48000001; // bl
23+
writeBranchMain(ptr, reinterpret_cast<void *>(destination), branch);
24+
}
25+
26+
template<typename FuncStart, typename FuncBranchBack>
27+
void writeStandardBranches(void *address, FuncStart funcStart, FuncBranchBack funcBranchBack)
28+
{
29+
void *BranchBackAddress = reinterpret_cast<void *>(
30+
reinterpret_cast<uint32_t>(address) + 0x4);
31+
32+
writeBranch(address, reinterpret_cast<void *>(funcStart));
33+
writeBranch(reinterpret_cast<void *>(funcBranchBack), BranchBackAddress);
34+
}
35+
1336
template<typename Func, typename Dest>
1437
Func hookFunction(Func function, Dest destination)
1538
{

ttyd-tools/rel/source/main.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -913,43 +913,43 @@ void initAddressOverwrites()
913913
void *JumpOnWaterAddress = reinterpret_cast<void *>(0x80093CF0);
914914
#endif
915915

916-
patch::writeBranchBL(AllowRunningFromBattlesAddress, reinterpret_cast<void *>(StartAllowRunningFromBattles));
916+
patch::writeBranchBL_Template(AllowRunningFromBattlesAddress, StartAllowRunningFromBattles);
917917

918-
patch::writeBranchBL(ForceNPCItemDropAddress, reinterpret_cast<void *>(StartForceNPCItemDrop));
918+
patch::writeBranchBL_Template(ForceNPCItemDropAddress, StartForceNPCItemDrop);
919919

920-
patch::writeBranchBL(ReplaceJumpAnimAddress, reinterpret_cast<void *>(StartReplaceJumpFallAnim));
920+
patch::writeBranchBL_Template(ReplaceJumpAnimAddress, StartReplaceJumpFallAnim);
921921

922-
patch::writeBranchBL(DisplayBattleMenuJumpAddress, reinterpret_cast<void *>(StartDisplayMegaJumpBadgeBattleMenu));
922+
patch::writeBranchBL_Template(DisplayBattleMenuJumpAddress, StartDisplayMegaJumpBadgeBattleMenu);
923923

924-
patch::writeBranchBL(DisplayBattleMenuHammerAddress, reinterpret_cast<void *>(StartDisplayMegaHammerBadgesBattleMenu));
924+
patch::writeBranchBL_Template(DisplayBattleMenuHammerAddress, StartDisplayMegaHammerBadgesBattleMenu);
925925

926-
patch::writeBranchBL(FixBlooperCrash1Address, reinterpret_cast<void *>(StartFixBlooperCrash1));
926+
patch::writeBranchBL_Template(FixBlooperCrash1Address, StartFixBlooperCrash1);
927927

928-
patch::writeBranchBL(FixBlooperCrash2Address, reinterpret_cast<void *>(StartFixBlooperCrash2));
928+
patch::writeBranchBL_Template(FixBlooperCrash2Address, StartFixBlooperCrash2);
929929

930-
patch::writeBranchBL(PreventTextboxSelectionAddress, reinterpret_cast<void *>(StartPreventTextboxSelection));
930+
patch::writeBranchBL_Template(PreventTextboxSelectionAddress, StartPreventTextboxSelection);
931931

932-
patch::writeBranchBL(DisableDPadOptionsDisplayAddress, reinterpret_cast<void *>(StartDisableDPadOptionsDisplay));
932+
patch::writeBranchBL_Template(DisableDPadOptionsDisplayAddress, StartDisableDPadOptionsDisplay);
933933

934-
patch::writeBranchBL(FixEvtMapBlendSetFlagPartnerCrashAddress, reinterpret_cast<void *>(StartFixEvtMapBlendSetFlagPartnerCrash));
934+
patch::writeBranchBL_Template(FixEvtMapBlendSetFlagPartnerCrashAddress, StartFixEvtMapBlendSetFlagPartnerCrash);
935935

936-
patch::writeBranchBL(FixEvtMapBlendSetFlagFollowerCrashAddress, reinterpret_cast<void *>(StartFixEvtMapBlendSetFlagFollowerCrash));
936+
patch::writeBranchBL_Template(FixEvtMapBlendSetFlagFollowerCrashAddress, StartFixEvtMapBlendSetFlagFollowerCrash);
937937

938-
patch::writeBranchBL(PreventPouchInitMemoryLeakAddress, reinterpret_cast<void *>(preventPouchInitMemoryLeak));
938+
patch::writeBranchBL_Template(PreventPouchInitMemoryLeakAddress, preventPouchInitMemoryLeak);
939939

940-
patch::writeBranchBL(InitStageEventsAddress, reinterpret_cast<void *>(initStageEvents));
940+
patch::writeBranchBL_Template(InitStageEventsAddress, initStageEvents);
941941

942-
patch::writeBranchBL(FallThroughMostObjectsStandAddress, reinterpret_cast<void *>(StartFallThroughMostObjectsStandard));
943-
patch::writeBranchBL(FallThroughMostObjectsTubeAddress, reinterpret_cast<void *>(StartFallThroughMostObjectsStandard));
942+
patch::writeBranchBL_Template(FallThroughMostObjectsStandAddress, StartFallThroughMostObjectsStandard);
943+
patch::writeBranchBL_Template(FallThroughMostObjectsTubeAddress, StartFallThroughMostObjectsStandard);
944944

945-
patch::writeBranchBL(FallThroughMostObjectsBowserAddress, reinterpret_cast<void *>(StartFallThroughMostObjectsBowser));
945+
patch::writeBranchBL_Template(FallThroughMostObjectsBowserAddress, StartFallThroughMostObjectsBowser);
946946

947-
patch::writeBranchBL(AutoMashThroughText1Address, reinterpret_cast<void *>(autoMashText));
948-
patch::writeBranchBL(AutoMashThroughText2Address, reinterpret_cast<void *>(autoMashText));
949-
patch::writeBranchBL(AutoMashThroughText3Address, reinterpret_cast<void *>(autoMashText));
947+
patch::writeBranchBL_Template(AutoMashThroughText1Address, autoMashText);
948+
patch::writeBranchBL_Template(AutoMashThroughText2Address, autoMashText);
949+
patch::writeBranchBL_Template(AutoMashThroughText3Address, autoMashText);
950950

951951
#ifdef TTYD_EU
952-
patch::writeBranchBL(JumpOnWaterAddress, reinterpret_cast<void *>(StartJumpOnWater));
952+
patch::writeBranchBL_Template(JumpOnWaterAddress, StartJumpOnWater);
953953
#endif
954954

955955
*reinterpret_cast<uint32_t *>(DebugModeInitialzeAddress) = 0x3800FFFF; // li r0,-1

ttyd-tools/rel/source/patch.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,6 @@ void clear_DC_IC_Cache(void *ptr, uint32_t size)
1212
gc::OSCache::ICInvalidateRange(ptr, size);
1313
}
1414

15-
void writeStandardBranches(void *address, void functionStart(), void functionBranchBack())
16-
{
17-
void *BranchBackAddress = reinterpret_cast<void *>(
18-
reinterpret_cast<uint32_t>(address) + 0x4);
19-
20-
writeBranch(address, reinterpret_cast<void *>(functionStart));
21-
writeBranch(reinterpret_cast<void *>(functionBranchBack), BranchBackAddress);
22-
}
23-
2415
void writeBranch(void *ptr, void *destination)
2516
{
2617
uint32_t branch = 0x48000000; // b

0 commit comments

Comments
 (0)