Skip to content

Commit c601bdd

Browse files
committed
Applied wrappers to functions from the game that are called more than 3 times each
This ends up saving 760 bytes.
1 parent 5db53c0 commit c601bdd

File tree

14 files changed

+312
-111
lines changed

14 files changed

+312
-111
lines changed

ttyd-tools/rel/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ MACHDEP = -mno-sdata -mgcn -DGEKKO -mcpu=750 -meabi -mhard-float
5454
CFLAGS = -nostdlib -ffreestanding -ffunction-sections -fdata-sections -g -Os -Wall -Werror $(MACHDEP) $(INCLUDE)
5555
CXXFLAGS = -fno-exceptions -fno-rtti -std=gnu++17 $(CFLAGS)
5656

57-
LDFLAGS = -r -e _prolog -u _prolog -u _epilog -u _unresolved -Wl,--gc-sections -nostdlib -g $(MACHDEP) -Wl,-Map,$(notdir $@).map
57+
FUNCWRAPS = -Wl,--wrap,sprintf -Wl,--wrap,strcmp -Wl,--wrap,strncmp -Wl,--wrap,strcpy -Wl,--wrap,strncpy -Wl,--wrap,strcat -Wl,--wrap,strlen -Wl,--wrap,memcpy -Wl,--wrap,memset -Wl,--wrap,sysMsec2Frame -Wl,--wrap,keyGetButton -Wl,--wrap,keyGetButtonTrg -Wl,--wrap,seqGetSeq -Wl,--wrap,seqGetNextSeq -Wl,--wrap,swByteGet -Wl,--wrap,swByteSet -Wl,--wrap,swClear -Wl,--wrap,marioStGetSystemLevel -Wl,--wrap,marioStSystemLevel -Wl,--wrap,marioGetPartyId -Wl,--wrap,marioGetExtraPartyId -Wl,--wrap,marioGetPtr -Wl,--wrap,pouchGetPtr -Wl,--wrap,btlGetScreenPoint -Wl,--wrap,evtGetWork -Wl,--wrap,winGetPtr -Wl,--wrap,winOpenEnable -Wl,--wrap,CARDClose -Wl,--wrap,DCFlushRange -Wl,--wrap,ICInvalidateRange -Wl,--wrap,__udivdi3 -Wl,--wrap,__umoddi3
58+
59+
LDFLAGS = -r -e _prolog -u _prolog -u _epilog -u _unresolved -Wl,--gc-sections -nostdlib -g $(MACHDEP) -Wl,-Map,$(notdir $@).map $(FUNCWRAPS)
5860

5961
# Platform options
6062
ifeq ($(VERSION),us)

ttyd-tools/rel/include/commonfunctions.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <ttyd/party.h>
55

66
#include <cstdint>
7+
#include <cstring>
78

89
namespace mod {
910

@@ -24,18 +25,11 @@ uint32_t getSequencePosition();
2425
void setSequencePosition(uint32_t value);
2526
void setNextMap(const char *map);
2627
void setNextBero(const char *bero);
27-
char *copyString(char *destination, const char *source);
28-
void *copyMemory(void *destination, const void *source, uint32_t size);
2928
bool compareStrings(const char *str1, const char *str2);
30-
bool compareStringsSize(const char *str1, const char *str2, uint32_t size);
29+
bool compareStringsSize(const char *str1, const char *str2, size_t size);
3130
bool compareStringToNextMap(const char *str);
32-
uint32_t getStringSize(const char *str);
33-
uint32_t getSystemLevel();
34-
void setSystemLevel(uint32_t value);
3531
void setSeqMapChange(const char *map, const char *bero);
36-
void *clearMemory(void *destination, uint32_t size);
37-
int32_t getPartnerID();
38-
int32_t getFollowerID();
32+
void *clearMemory(void *destination, std::size_t size);
3933
void *getPartnerPointer();
4034
void *getFollowerPointer();
4135
void removePartnerFromOverworld();
@@ -48,7 +42,6 @@ void recheckJumpAndHammerLevels();
4842
uint32_t getCurrentPitFloor();
4943
uint32_t getCurrentFPS();
5044
void clearGSWFsRange(uint32_t lowerBound, uint32_t upperBound);
51-
void clear_DC_IC_Cache(void *ptr, uint32_t size);
5245
bool checkIfPointerIsValid(void *ptr);
5346
void *getLastPointerFromPath(void *address, int32_t *offset, uint32_t offsetAmount);
5447

ttyd-tools/rel/include/menufunctions.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#pragma once
22

33
#include <ttyd/party.h>
4-
#include <ttyd/evtmgr.h>
54

65
#include <cstdint>
76

@@ -63,7 +62,6 @@ void deleteItem();
6362
int32_t changeItem();
6463

6564
int32_t resolveFade(uint32_t index);
66-
ttyd::evtmgr::EvtWork *getCurrentEventWork();
6765
int32_t getGW(uint32_t gw);
6866
void setGW(uint32_t gw, uint32_t value);
6967
bool getGF(uint32_t gf);

ttyd-tools/rel/include/patch.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
#pragma once
22

3-
#include "commonfunctions.h"
4-
53
#include <cstdint>
64

75
namespace mod::patch {
86

7+
void clear_DC_IC_Cache(void *ptr, uint32_t size);
98
void writeBranch(void *ptr, void *destination);
109
void writeBranchLR(void *ptr, void *destination);
1110
void writeBranchMain(void *ptr, void *destination, uint32_t branch);

ttyd-tools/rel/source/codes.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "patch.h"
88

99
#include <ttyd/item_data.h>
10+
#include <ttyd/mariost.h>
1011
#include <ttyd/system.h>
1112
#include <ttyd/mario.h>
1213
#include <ttyd/evtmgr.h>
@@ -21,8 +22,8 @@
2122
#include <ttyd/win_main.h>
2223
#include <ttyd/itemdrv.h>
2324
#include <ttyd/battle_ac.h>
24-
#include <ttyd/mariost.h>
2525

26+
#include <cstring>
2627
#include <cstdio>
2728
#include <cinttypes>
2829
#include <cmath>
@@ -246,7 +247,7 @@ void loadMarioAndPartnerPositions()
246247

247248
void saveAnywhere()
248249
{
249-
uint32_t SystemLevel = getSystemLevel();
250+
uint32_t SystemLevel = ttyd::mariost::marioStGetSystemLevel();
250251
if (!SaveAnywhere.ScriptIsRunning)
251252
{
252253
if (!Cheat[SAVE_ANYWHERE].Active || MenuVar.ChangingCheatButtonCombo)
@@ -269,7 +270,7 @@ void saveAnywhere()
269270
#endif
270271

271272
// Take away control from the player and start the Save script
272-
setSystemLevel(SystemLevel + 1);
273+
ttyd::mariost::marioStSystemLevel(SystemLevel + 1);
273274

274275
uint32_t SaveScriptEvtEntry = reinterpret_cast<uint32_t>(ttyd::evtmgr::evtEntryType(SaveScript, 0, 0, 0));
275276
SaveAnywhere.ThreadID = *reinterpret_cast<uint32_t *>(SaveScriptEvtEntry + 0x15C);
@@ -298,7 +299,7 @@ void saveAnywhere()
298299
// Only lower the system level if it's not currently at 0
299300
if (SystemLevel > 0)
300301
{
301-
setSystemLevel(SystemLevel - 1);
302+
ttyd::mariost::marioStSystemLevel(SystemLevel - 1);
302303
}
303304
}
304305
}
@@ -428,10 +429,10 @@ void checkIfSystemLevelShouldBeLowered()
428429
if (!checkForSpecificSeq(ttyd::seqdrv::SeqIndex::kGame))
429430
{
430431
// Only lower the system level if it's not currently at 0
431-
uint32_t SystemLevel = getSystemLevel();
432+
uint32_t SystemLevel = ttyd::mariost::marioStGetSystemLevel();
432433
if (SystemLevel > 0)
433434
{
434-
setSystemLevel(0);
435+
ttyd::mariost::marioStSystemLevel(0);
435436
}
436437
}
437438
else
@@ -479,15 +480,15 @@ void reloadRoomMain()
479480
char *tempNewBero = ReloadRoom.NewBero;
480481
char *tempNewMap = ReloadRoom.NewMap;
481482

482-
copyString(tempNewBero, ttyd::seq_mapchange::NextBero);
483-
copyString(tempNewMap, ttyd::seq_mapchange::NextMap);
483+
strcpy(tempNewBero, ttyd::seq_mapchange::NextBero);
484+
strcpy(tempNewMap, ttyd::seq_mapchange::NextMap);
484485
setSeqMapChange(tempNewMap, tempNewBero);
485486

486487
// Reset the camera - mainly for the black bars at the top and bottom of the screen
487488
uint32_t CameraPointer = reinterpret_cast<uint32_t>(ttyd::camdrv::camGetPtr(8));
488489
*reinterpret_cast<uint16_t *>(CameraPointer) &= ~((1 << 8) | (1 << 9)); // Turn off the 8 and 9 bits
489490

490-
uint32_t SystemLevel = getSystemLevel();
491+
uint32_t SystemLevel = ttyd::mariost::marioStGetSystemLevel();
491492
if (SystemLevel == 0)
492493
{
493494
return;
@@ -503,7 +504,7 @@ void reloadRoomMain()
503504
// Enable sound effects, set the default camera id for Mario, and give back control to the player
504505
ttyd::pmario_sound::psndClearFlag(0x80);
505506
ttyd::mario_cam::marioSetCamId(4);
506-
setSystemLevel(0);
507+
ttyd::mariost::marioStSystemLevel(0);
507508
ReloadRoom.SystemLevelShouldBeLowered = true;
508509
}
509510

@@ -638,7 +639,7 @@ void spawnItem()
638639
bool tempInAdjustableValueMenu = SpawnItem.InAdjustableValueMenu;
639640
if (Cheat[SPAWN_ITEM].Active && !MenuVar.ChangingCheatButtonCombo)
640641
{
641-
uint32_t SystemLevel = getSystemLevel();
642+
uint32_t SystemLevel = ttyd::mariost::marioStGetSystemLevel();
642643
if (checkIfInGame() && (SystemLevel != 15))
643644
{
644645
// Currently not in a battle and the pause menu is not open
@@ -825,7 +826,7 @@ void getStickAngleString(char *stringOut)
825826
if (StickAngle == -1000)
826827
{
827828
// The stick is currently at the neutral position
828-
copyString(stringOut, "Neutral");
829+
strcpy(stringOut, "Neutral");
829830
return;
830831
}
831832

@@ -838,7 +839,7 @@ void getStickAngleString(char *stringOut)
838839

839840
void displaySequenceInPauseMenu()
840841
{
841-
uint32_t SystemLevel = getSystemLevel();
842+
uint32_t SystemLevel = ttyd::mariost::marioStGetSystemLevel();
842843
if (SystemLevel != 15)
843844
{
844845
return;
@@ -1048,7 +1049,7 @@ void displayPalaceSkipDetails()
10481049
return;
10491050
}
10501051

1051-
uint32_t SystemLevel = getSystemLevel();
1052+
uint32_t SystemLevel = ttyd::mariost::marioStGetSystemLevel();
10521053
if (SystemLevel == 15)
10531054
{
10541055
// Stop upon pausing
@@ -1097,7 +1098,7 @@ void displayBlimpTicketSkipDetails()
10971098
return;
10981099
}
10991100

1100-
uint32_t SystemLevel = getSystemLevel();
1101+
uint32_t SystemLevel = ttyd::mariost::marioStGetSystemLevel();
11011102
if (SystemLevel == 15)
11021103
{
11031104
// Stop upon pausing
@@ -1471,7 +1472,7 @@ uint32_t Mod::setIndexWarpEntrance(void *event, uint32_t waitMode)
14711472
reinterpret_cast<uint32_t>(
14721473
ttyd::mariost::globalWorkPointer) + 0x11C);
14731474

1474-
copyString(NextBeroGSW, ChosenEntranceName);
1475+
strcpy(NextBeroGSW, ChosenEntranceName);
14751476
setNextBero(ChosenEntranceName);
14761477
}
14771478

ttyd-tools/rel/source/commonfunctions.cpp

Lines changed: 8 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "commonfunctions.h"
22

3-
#include <gc/OSCache.h>
43
#include <ttyd/system.h>
54
#include <ttyd/seqdrv.h>
65
#include <ttyd/mariost.h>
@@ -174,31 +173,21 @@ void setSequencePosition(uint32_t value)
174173

175174
void setNextMap(const char *map)
176175
{
177-
copyString(ttyd::seq_mapchange::NextMap, map);
176+
strcpy(ttyd::seq_mapchange::NextMap, map);
178177
strncpy(ttyd::seq_mapchange::NextArea, map, 3);
179178
}
180179

181180
void setNextBero(const char *bero)
182181
{
183-
copyString(ttyd::seq_mapchange::NextBero, bero);
184-
}
185-
186-
char *copyString(char *destination, const char *source)
187-
{
188-
return strcpy(destination, source);
189-
}
190-
191-
void *copyMemory(void *destination, const void *source, uint32_t size)
192-
{
193-
return memcpy(destination, source, size);
182+
strcpy(ttyd::seq_mapchange::NextBero, bero);
194183
}
195184

196185
bool compareStrings(const char *str1, const char *str2)
197186
{
198187
return strcmp(str1, str2) == 0;
199188
}
200189

201-
bool compareStringsSize(const char *str1, const char *str2, uint32_t size)
190+
bool compareStringsSize(const char *str1, const char *str2, size_t size)
202191
{
203192
return strncmp(str1, str2, size) == 0;
204193
}
@@ -208,62 +197,37 @@ bool compareStringToNextMap(const char *str)
208197
return compareStrings(str, ttyd::seq_mapchange::NextMap);
209198
}
210199

211-
uint32_t getStringSize(const char *str)
212-
{
213-
return strlen(str);
214-
}
215-
216-
uint32_t getSystemLevel()
217-
{
218-
return ttyd::mariost::marioStGetSystemLevel();
219-
}
220-
221-
void setSystemLevel(uint32_t value)
222-
{
223-
ttyd::mariost::marioStSystemLevel(value);
224-
}
225-
226200
void setSeqMapChange(const char *map, const char *bero)
227201
{
228202
ttyd::seqdrv::seqSetSeq(ttyd::seqdrv::SeqIndex::kMapChange, map, bero);
229203
}
230204

231-
void *clearMemory(void *destination, uint32_t size)
205+
void *clearMemory(void *destination, std::size_t size)
232206
{
233207
return memset(destination, 0, size);
234208
}
235209

236-
int32_t getPartnerID()
237-
{
238-
return ttyd::mario_party::marioGetPartyId();
239-
}
240-
241-
int32_t getFollowerID()
242-
{
243-
return ttyd::mario_party::marioGetExtraPartyId();
244-
}
245-
246210
void *getPartnerPointer()
247211
{
248-
int32_t PartyID = getPartnerID();
212+
int32_t PartyID = ttyd::mario_party::marioGetPartyId();
249213
return ttyd::party::partyGetPtr(PartyID);
250214
}
251215

252216
void *getFollowerPointer()
253217
{
254-
int32_t FollowerID = getFollowerID();
218+
int32_t FollowerID = ttyd::mario_party::marioGetExtraPartyId();
255219
return ttyd::party::partyGetPtr(FollowerID);
256220
}
257221

258222
void removePartnerFromOverworld()
259223
{
260-
int32_t PartyID = getPartnerID();
224+
int32_t PartyID = ttyd::mario_party::marioGetPartyId();
261225
ttyd::party::partyKill2(PartyID);
262226
}
263227

264228
void removeFollowerFromOverworld()
265229
{
266-
int32_t FollowerID = getFollowerID();
230+
int32_t FollowerID = ttyd::mario_party::marioGetExtraPartyId();
267231
ttyd::party::partyKill2(FollowerID);
268232
}
269233

@@ -335,12 +299,6 @@ void clearGSWFsRange(uint32_t lowerBound, uint32_t upperBound)
335299
}
336300
}
337301

338-
void clear_DC_IC_Cache(void *ptr, uint32_t size)
339-
{
340-
gc::OSCache::DCFlushRange(ptr, size);
341-
gc::OSCache::ICInvalidateRange(ptr, size);
342-
}
343-
344302
bool checkIfPointerIsValid(void *ptr)
345303
{
346304
uint32_t ptrRaw = reinterpret_cast<uint32_t>(ptr);

ttyd-tools/rel/source/draw.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <ttyd/itemdrv.h>
2626
#include <ttyd/battle_ac.h>
2727

28+
#include <cstring>
2829
#include <cstdio>
2930
#include <cinttypes>
3031

@@ -382,9 +383,9 @@ void drawStringMultiline(float x, float y, const char *text, float scale)
382383
increment = static_cast<float>(tempIncrementValue);
383384

384385
// Copy the text to a temporary array, as it will be modified
385-
uint32_t textSize = getStringSize(text);
386+
uint32_t textSize = strlen(text);
386387
char tempText[textSize + 1];
387-
copyString(tempText, text);
388+
strcpy(tempText, text);
388389

389390
// Get the index for the next line
390391
uint32_t index = getNextLineIndex(tempText);

ttyd-tools/rel/source/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,8 +506,8 @@ void addTextToHeapArray(char *text)
506506
char *tempHeapBuffer = HeapBuffer;
507507

508508
// Make sure adding the new text will not result in an overflow
509-
uint32_t NewTextSize = getStringSize(text);
510-
uint32_t CurrentHeapSize = getStringSize(tempHeapBuffer);
509+
uint32_t NewTextSize = strlen(text);
510+
uint32_t CurrentHeapSize = strlen(tempHeapBuffer);
511511

512512
uint32_t NewHeapSize = CurrentHeapSize + NewTextSize + 1;
513513
uint32_t MaxHeapSize = sizeof(HeapBuffer);

0 commit comments

Comments
 (0)