From 53935efc64b9945e49dc932917dc179bdbe58631 Mon Sep 17 00:00:00 2001 From: YamaArashi Date: Sat, 14 Jan 2017 15:17:51 -0800 Subject: [PATCH] mostly finish string_util.c --- asm/string_util.s | 368 ------------------------------------------ data/string_util.s | 70 -------- include/global.h | 16 ++ include/string_util.h | 16 +- ld_script.txt | 3 +- src/string_util.c | 326 +++++++++++++++++++++++++++++++++---- sym_ewram.txt | 3 +- 7 files changed, 322 insertions(+), 480 deletions(-) delete mode 100644 asm/string_util.s delete mode 100644 data/string_util.s diff --git a/asm/string_util.s b/asm/string_util.s deleted file mode 100644 index 8de4972c0..000000000 --- a/asm/string_util.s +++ /dev/null @@ -1,368 +0,0 @@ - .include "asm/macros.inc" - .include "constants/constants.inc" - - .syntax unified - - .text - - thumb_func_start StringCopyN_Multibyte -@ u8 *CopyString_LimitN_Multibyte(u8 *dest, u8 *src, u32 n) -StringCopyN_Multibyte: @ 800918C - push {r4,r5,lr} - adds r4, r0, 0 - adds r3, r1, 0 - subs r2, 0x1 - movs r5, 0x1 - negs r5, r5 - b _080091B2 -_0800919A: - strb r0, [r4] - adds r3, 0x1 - adds r4, 0x1 - subs r0, r3, 0x1 - ldrb r0, [r0] - cmp r0, 0xF9 - bne _080091B0 - ldrb r0, [r3] - strb r0, [r4] - adds r3, 0x1 - adds r4, 0x1 -_080091B0: - subs r2, 0x1 -_080091B2: - cmp r2, r5 - beq _080091BE - ldrb r0, [r3] - adds r1, r0, 0 - cmp r1, 0xFF - bne _0800919A -_080091BE: - movs r0, 0xFF - strb r0, [r4] - adds r0, r4, 0 - pop {r4,r5} - pop {r1} - bx r1 - thumb_func_end StringCopyN_Multibyte - - thumb_func_start StringLength_Multibyte -@ u32 GetStringLength_Multibyte(u8 *s) -StringLength_Multibyte: @ 80091CC - push {lr} - adds r2, r0, 0 - movs r3, 0 - b _080091DE -_080091D4: - cmp r1, 0xF9 - bne _080091DA - adds r2, 0x1 -_080091DA: - adds r2, 0x1 - adds r3, 0x1 -_080091DE: - ldrb r1, [r2] - adds r0, r1, 0 - cmp r0, 0xFF - bne _080091D4 - adds r0, r3, 0 - pop {r1} - bx r1 - thumb_func_end StringLength_Multibyte - - thumb_func_start WriteColorChangeControlCode -@ u8 *WriteColorChangeControlCode(u8 *dest, u8 colorType, u8 color) -WriteColorChangeControlCode: @ 80091EC - push {lr} - adds r3, r0, 0 - lsls r2, 24 - lsrs r2, 24 - movs r0, 0xFC - strb r0, [r3] - adds r3, 0x1 - cmp r1, 0x1 - beq _0800920E - cmp r1, 0x1 - bcc _08009208 - cmp r1, 0x2 - beq _08009214 - b _08009218 -_08009208: - movs r0, 0x1 - strb r0, [r3] - b _08009216 -_0800920E: - movs r0, 0x3 - strb r0, [r3] - b _08009216 -_08009214: - strb r1, [r3] -_08009216: - adds r3, 0x1 -_08009218: - strb r2, [r3] - adds r3, 0x1 - movs r0, 0xFF - strb r0, [r3] - adds r0, r3, 0 - pop {r1} - bx r1 - thumb_func_end WriteColorChangeControlCode - - thumb_func_start sub_8009228 -@ bool8 sub_8009228(u8 *s) -sub_8009228: @ 8009228 - push {lr} - adds r2, r0, 0 - b _0800923E -_0800922E: - adds r0, r1, 0 - cmp r0, 0xA0 - bhi _0800923C - cmp r0, 0 - beq _0800923C - movs r0, 0x1 - b _08009248 -_0800923C: - adds r2, 0x1 -_0800923E: - ldrb r1, [r2] - adds r0, r1, 0 - cmp r0, 0xFF - bne _0800922E - movs r0, 0 -_08009248: - pop {r1} - bx r1 - thumb_func_end sub_8009228 - - thumb_func_start sub_800924C -@ bool8 sub_800924C(u8 *s, u32 n) -sub_800924C: @ 800924C - push {r4,lr} - adds r3, r0, 0 - adds r4, r1, 0 - movs r1, 0 - b _08009268 -_08009256: - adds r0, r2, 0 - cmp r0, 0xA0 - bhi _08009264 - cmp r0, 0 - beq _08009264 - movs r0, 0x1 - b _08009276 -_08009264: - adds r3, 0x1 - adds r1, 0x1 -_08009268: - ldrb r2, [r3] - adds r0, r2, 0 - cmp r0, 0xFF - beq _08009274 - cmp r1, r4 - blt _08009256 -_08009274: - movs r0, 0 -_08009276: - pop {r4} - pop {r1} - bx r1 - thumb_func_end sub_800924C - - thumb_func_start GetExtCtrlCodeLength -@ u8 GetExtendedControlCodeLength(u8 code) -GetExtCtrlCodeLength: @ 800927C - push {lr} - lsls r0, 24 - lsrs r1, r0, 24 - movs r0, 0 - cmp r1, 0x18 - bhi _0800928E - ldr r0, =gExtendedControlCodeLengths - adds r0, r1, r0 - ldrb r0, [r0] -_0800928E: - pop {r1} - bx r1 - .pool - thumb_func_end GetExtCtrlCodeLength - - thumb_func_start SkipExtCtrlCode -@ u8 *SkipExtendedControlCode(u8 *s) -SkipExtCtrlCode: @ 8009298 - push {r4,lr} - adds r4, r0, 0 - b _080092AC -_0800929E: - adds r4, 0x1 - ldrb r0, [r4] - bl GetExtCtrlCodeLength - lsls r0, 24 - lsrs r0, 24 - adds r4, r0 -_080092AC: - ldrb r0, [r4] - cmp r0, 0xFC - beq _0800929E - adds r0, r4, 0 - pop {r4} - pop {r1} - bx r1 - thumb_func_end SkipExtCtrlCode - - thumb_func_start StringCompareWithoutExtCtrlCodes -@ u8 CompareStringWithoutExtendedControlCodes(u8 *s1, u8 *s2) -StringCompareWithoutExtCtrlCodes: @ 80092BC - push {r4-r6,lr} - adds r4, r0, 0 - adds r5, r1, 0 - movs r6, 0 - b _080092DE -_080092C6: - cmp r1, r0 - bcs _080092D4 - movs r6, 0x1 - negs r6, r6 - cmp r0, 0xFF - bne _080092D4 - movs r6, 0x1 -_080092D4: - ldrb r0, [r4] - cmp r0, 0xFF - beq _080092FE - adds r4, 0x1 - adds r5, 0x1 -_080092DE: - adds r0, r4, 0 - bl SkipExtCtrlCode - adds r4, r0, 0 - adds r0, r5, 0 - bl SkipExtCtrlCode - adds r5, r0, 0 - ldrb r1, [r4] - ldrb r0, [r5] - cmp r1, r0 - bls _080092C6 - movs r6, 0x1 - cmp r1, 0xFF - bne _080092FE - subs r6, 0x2 -_080092FE: - adds r0, r6, 0 - pop {r4-r6} - pop {r1} - bx r1 - thumb_func_end StringCompareWithoutExtCtrlCodes - - thumb_func_start ConvertInternationalString -@ void StopMusicWhileStringIsPrinted(u8 *s, bool8 stopMusic) -ConvertInternationalString: @ 8009308 - push {r4,lr} - adds r4, r0, 0 - lsls r1, 24 - lsrs r1, 24 - cmp r1, 0x1 - bne _08009368 - bl StripExtCtrlCodes - adds r0, r4, 0 - bl StringLength - lsls r0, 24 - lsrs r2, r0, 24 - adds r1, r2, 0 - adds r0, r1, 0x1 - lsls r0, 24 - lsrs r2, r0, 24 - adds r3, r2, 0 - adds r1, r4, r1 - movs r0, 0xFC - strb r0, [r1] - adds r0, r3, 0x1 - lsls r0, 24 - lsrs r2, r0, 24 - adds r3, r4, r3 - movs r0, 0x16 - strb r0, [r3] - adds r0, r2, 0x1 - lsls r0, 24 - adds r2, r4, r2 - movs r1, 0xFF - strb r1, [r2] - movs r1, 0xFF - lsls r1, 24 - adds r0, r1 - b _0800935A -_08009350: - adds r1, r2, r4 - ldrb r0, [r1] - strb r0, [r1, 0x2] - subs r0, r2, 0x1 - lsls r0, 24 -_0800935A: - lsrs r2, r0, 24 - cmp r2, 0xFF - bne _08009350 - movs r0, 0xFC - strb r0, [r4] - movs r0, 0x15 - strb r0, [r4, 0x1] -_08009368: - pop {r4} - pop {r0} - bx r0 - thumb_func_end ConvertInternationalString - - thumb_func_start StripExtCtrlCodes -@ void StripExtendedControlCodes(u8 *s) -StripExtCtrlCodes: @ 8009370 - push {r4-r6,lr} - adds r5, r0, 0 - movs r4, 0 - movs r6, 0 - ldrb r0, [r5] - cmp r0, 0xFF - beq _080093C0 -_0800937E: - adds r0, r5, r4 - ldrb r0, [r0] - cmp r0, 0xFC - bne _080093A0 - adds r0, r4, 0x1 - lsls r0, 16 - lsrs r4, r0, 16 - adds r0, r5, r4 - ldrb r0, [r0] - bl GetExtCtrlCodeLength - lsls r0, 24 - lsrs r0, 24 - adds r0, r4, r0 - lsls r0, 16 - lsrs r4, r0, 16 - b _080093B8 -_080093A0: - adds r2, r6, 0 - adds r0, r2, 0x1 - lsls r0, 16 - lsrs r6, r0, 16 - adds r2, r5, r2 - adds r1, r4, 0 - adds r0, r1, 0x1 - lsls r0, 16 - lsrs r4, r0, 16 - adds r1, r5, r1 - ldrb r0, [r1] - strb r0, [r2] -_080093B8: - adds r0, r5, r4 - ldrb r0, [r0] - cmp r0, 0xFF - bne _0800937E -_080093C0: - adds r1, r5, r6 - movs r0, 0xFF - strb r0, [r1] - pop {r4-r6} - pop {r0} - bx r0 - thumb_func_end StripExtCtrlCodes - - .align 2, 0 @ Don't pad with nop. diff --git a/data/string_util.s b/data/string_util.s deleted file mode 100644 index 69c4c19bf..000000000 --- a/data/string_util.s +++ /dev/null @@ -1,70 +0,0 @@ - .include "asm/macros.inc" - .include "constants/constants.inc" - - .section .rodata - -sDigits:: @ 82EC7D4 - .string "0123456789ABCDEF" - - .align 2 -sPowersOfTen:: @ 82EC7E4 - .4byte 1 - .4byte 10 - .4byte 100 - .4byte 1000 - .4byte 10000 - .4byte 100000 - .4byte 1000000 - .4byte 10000000 - .4byte 100000000 - .4byte 1000000000 - -sSetBrailleFont:: @ 82EC80C - .incbin "baserom.gba", 0x2ec80c, 0x4 - -sGotoLine2:: @ 82EC810 - .incbin "baserom.gba", 0x2ec810, 0x8 - - .align 2 -sExpandPlaceholderFuncs:: @ 82EC818 - .4byte ExpandPlaceholder_UnknownStringVar - .4byte ExpandPlaceholder_PlayerName - .4byte ExpandPlaceholder_StringVar1 - .4byte ExpandPlaceholder_StringVar2 - .4byte ExpandPlaceholder_StringVar3 - .4byte ExpandPlaceholder_KunChan - .4byte ExpandPlaceholder_RivalName - .4byte ExpandPlaceholder_Version - .4byte ExpandPlaceholder_Aqua - .4byte ExpandPlaceholder_Magma - .4byte ExpandPlaceholder_Archie - .4byte ExpandPlaceholder_Maxie - .4byte ExpandPlaceholder_Kyogre - .4byte ExpandPlaceholder_Groudon - -gExtendedControlCodeLengths:: @ 82EC850 - .byte 1 - .byte 2 - .byte 2 - .byte 2 - .byte 4 - .byte 2 - .byte 2 - .byte 1 - .byte 2 - .byte 1 - .byte 1 - .byte 3 - .byte 2 - .byte 2 - .byte 2 - .byte 1 - .byte 3 - .byte 2 - .byte 2 - .byte 2 - .byte 2 - .byte 1 - .byte 1 - .byte 1 - .byte 1 diff --git a/include/global.h b/include/global.h index 1f7df2ca6..b7038025b 100644 --- a/include/global.h +++ b/include/global.h @@ -3,6 +3,22 @@ #include "gba/gba.h" +// Prevent cross-jump optimization. +#define BLOCK_CROSS_JUMP asm(""); + +// to help in decompiling +#define asm_comment(x) asm volatile("@ -- " x " -- ") + +#define ARRAY_COUNT(array) (sizeof(array) / sizeof((array)[0])) + +#define POKEMON_NAME_LENGTH 10 +#define OT_NAME_LENGTH 7 + +extern u8 gStringVar1[]; +extern u8 gStringVar2[]; +extern u8 gStringVar3[]; +extern u8 gStringVar4[]; + enum { VERSION_SAPPHIRE = 1, diff --git a/include/string_util.h b/include/string_util.h index c20965380..382f52251 100644 --- a/include/string_util.h +++ b/include/string_util.h @@ -10,7 +10,7 @@ enum StringConvertMode u8 *StringCopy10(u8 *dest, const u8 *src); u8 *StringGetEnd10(u8 *str); -u8 *StringCopy8(u8 *dest, const u8 *src); +u8 *StringCopy7(u8 *dest, const u8 *src); u8 *StringCopy(u8 *dest, const u8 *src); u8 *StringAppend(u8 *dest, const u8 *src); u8 *StringCopyN(u8 *dest, const u8 *src, u8 n); @@ -18,15 +18,25 @@ u8 *StringAppendN(u8 *dest, const u8 *src, u8 n); u16 StringLength(const u8 *str); s32 StringCompare(const u8 *str1, const u8 *str2); s32 StringCompareN(const u8 *str1, const u8 *str2, u32 n); +bool8 IsStringLengthAtLeast(const u8 *str, s32 n); u8 *ConvertIntToDecimalStringN(u8 *dest, s32 value, enum StringConvertMode mode, u8 n); -u8 *ConvertIntToDecimalStringN_DigitWidth6(u8 *dest, s32 value, enum StringConvertMode mode, u8 n); +u8 *ConvertUIntToDecimalStringN(u8 *dest, u32 value, enum StringConvertMode mode, u8 n); u8 *ConvertIntToHexStringN(u8 *dest, s32 value, enum StringConvertMode mode, u8 n); -u8 *ConvertIntToDecimalString(u8 *dest, s32 value); u8 *StringExpandPlaceholders(u8 *dest, const u8 *src); u8 *StringBraille(u8 *dest, const u8 *src); u8 *GetExpandedPlaceholder(u32 id); u8 *StringFill(u8 *dest, u8 c, u16 n); u8 *StringCopyPadded(u8 *dest, const u8 *src, u8 c, u16 n); u8 *StringFillWithTerminator(u8 *dest, u16 n); +u8 *StringCopyN_Multibyte(u8 *dest, u8 *src, u32 n); +u32 StringLength_Multibyte(u8 *str); +u8 *WriteColorChangeControlCode(u8 *dest, u32 colorType, u8 color); +bool32 sub_8009228(u8 *str); +bool32 sub_800924C(u8 *str, s32 n); +u8 GetExtCtrlCodeLength(u8 code); +static const u8 *SkipExtCtrlCode(const u8 *s); +s32 StringCompareWithoutExtCtrlCodes(const u8 *str1, const u8 *str2); +void ConvertInternationalString(u8 *s, u8 language); +void StripExtCtrlCodes(u8 *str); #endif // GUARD_STRING_UTIL_H diff --git a/ld_script.txt b/ld_script.txt index 83c6bc49d..6fb2fbeb8 100644 --- a/ld_script.txt +++ b/ld_script.txt @@ -49,7 +49,6 @@ SECTIONS { asm/text.o(.text); asm/sprite.o(.text); src/string_util.o(.text); - asm/string_util.o(.text); asm/link.o(.text); asm/rtc.o(.text); asm/main_menu.o(.text); @@ -245,7 +244,7 @@ SECTIONS { data/window.o(.rodata); data/text.o(.rodata); data/sprite.o(.rodata); - data/string_util.o(.rodata); + src/string_util.o(.rodata); data/data2.o(.rodata); data/diploma.o(.rodata); data/strings.o(.rodata); diff --git a/src/string_util.c b/src/string_util.c index 35b48ceae..a00f261bd 100644 --- a/src/string_util.c +++ b/src/string_util.c @@ -2,22 +2,23 @@ #include "string_util.h" #include "text.h" -#define MAX_PLACEHOLDER_ID 0xD +EWRAM_DATA u8 gUnknownStringVar[16] = {0}; -extern const u8 sDigits[]; -extern const s32 sPowersOfTen[]; -extern const u8 sSetBrailleFont[]; -extern const u8 sGotoLine2[]; +static const u8 sDigits[] = @"0123456789ABCDEF"; -typedef u8 *(*ExpandPlaceholderFunc)(void); - -extern const ExpandPlaceholderFunc sExpandPlaceholderFuncs[]; - -extern u8 gUnknownStringVar[]; -extern u8 gStringVar1[]; -extern u8 gStringVar2[]; -extern u8 gStringVar3[]; -extern u8 gStringVar4[]; +static const s32 sPowersOfTen[] = +{ + 1, + 10, + 100, + 1000, + 10000, + 100000, + 1000000, + 10000000, + 100000000, + 1000000000, +}; extern u8 gExpandedPlaceholder_Empty[]; extern u8 gExpandedPlaceholder_Kun[]; @@ -393,11 +394,8 @@ u8 *StringExpandPlaceholders(u8 *dest, const u8 *src) u8 *StringBraille(u8 *dest, const u8 *src) { - u8 setBrailleFont[4]; - u8 gotoLine2[5]; - - memcpy(setBrailleFont, sSetBrailleFont, 4); - memcpy(gotoLine2, sGotoLine2, 5); + u8 setBrailleFont[] = { 0xFC, 0x06, 0x06, 0xFF }; + u8 gotoLine2[5] = { 0xFE, 0xFC, 0x0E, 0x02, 0xFF }; dest = StringCopy(dest, setBrailleFont); @@ -421,32 +419,32 @@ u8 *StringBraille(u8 *dest, const u8 *src) } } -u8 *ExpandPlaceholder_UnknownStringVar(void) +static u8 *ExpandPlaceholder_UnknownStringVar(void) { return gUnknownStringVar; } -u8 *ExpandPlaceholder_PlayerName(void) +static u8 *ExpandPlaceholder_PlayerName(void) { return gSaveBlock2Ptr->playerName; } -u8 *ExpandPlaceholder_StringVar1(void) +static u8 *ExpandPlaceholder_StringVar1(void) { return gStringVar1; } -u8 *ExpandPlaceholder_StringVar2(void) +static u8 *ExpandPlaceholder_StringVar2(void) { return gStringVar2; } -u8 *ExpandPlaceholder_StringVar3(void) +static u8 *ExpandPlaceholder_StringVar3(void) { return gStringVar3; } -u8 *ExpandPlaceholder_KunChan(void) +static u8 *ExpandPlaceholder_KunChan(void) { if (gSaveBlock2Ptr->playerGender == MALE) return gExpandedPlaceholder_Kun; @@ -454,7 +452,7 @@ u8 *ExpandPlaceholder_KunChan(void) return gExpandedPlaceholder_Chan; } -u8 *ExpandPlaceholder_RivalName(void) +static u8 *ExpandPlaceholder_RivalName(void) { if (gSaveBlock2Ptr->playerGender == MALE) return gExpandedPlaceholder_May; @@ -462,47 +460,67 @@ u8 *ExpandPlaceholder_RivalName(void) return gExpandedPlaceholder_Brendan; } -u8 *ExpandPlaceholder_Version(void) +static u8 *ExpandPlaceholder_Version(void) { return gExpandedPlaceholder_Emerald; } -u8 *ExpandPlaceholder_Aqua(void) +static u8 *ExpandPlaceholder_Aqua(void) { return gExpandedPlaceholder_Aqua; } -u8 *ExpandPlaceholder_Magma(void) +static u8 *ExpandPlaceholder_Magma(void) { return gExpandedPlaceholder_Magma; } -u8 *ExpandPlaceholder_Archie(void) +static u8 *ExpandPlaceholder_Archie(void) { return gExpandedPlaceholder_Archie; } -u8 *ExpandPlaceholder_Maxie(void) +static u8 *ExpandPlaceholder_Maxie(void) { return gExpandedPlaceholder_Maxie; } -u8 *ExpandPlaceholder_Kyogre(void) +static u8 *ExpandPlaceholder_Kyogre(void) { return gExpandedPlaceholder_Kyogre; } -u8 *ExpandPlaceholder_Groudon(void) +static u8 *ExpandPlaceholder_Groudon(void) { return gExpandedPlaceholder_Groudon; } u8 *GetExpandedPlaceholder(u32 id) { - if (id > MAX_PLACEHOLDER_ID) + typedef u8 *(*ExpandPlaceholderFunc)(void); + + static const ExpandPlaceholderFunc funcs[] = + { + ExpandPlaceholder_UnknownStringVar, + ExpandPlaceholder_PlayerName, + ExpandPlaceholder_StringVar1, + ExpandPlaceholder_StringVar2, + ExpandPlaceholder_StringVar3, + ExpandPlaceholder_KunChan, + ExpandPlaceholder_RivalName, + ExpandPlaceholder_Version, + ExpandPlaceholder_Aqua, + ExpandPlaceholder_Magma, + ExpandPlaceholder_Archie, + ExpandPlaceholder_Maxie, + ExpandPlaceholder_Kyogre, + ExpandPlaceholder_Groudon, + }; + + if (id >= ARRAY_COUNT(funcs)) return gExpandedPlaceholder_Empty; else - return sExpandPlaceholderFuncs[id](); + return funcs[id](); } u8 *StringFill(u8 *dest, u8 c, u16 n) @@ -542,3 +560,241 @@ u8 *StringFillWithTerminator(u8 *dest, u16 n) { return StringFill(dest, EOS, n); } + +__attribute__((naked)) +u8 *StringCopyN_Multibyte(u8 *dest, u8 *src, u32 n) +{ + asm(".syntax unified\n\ + push {r4,r5,lr}\n\ + adds r4, r0, 0\n\ + adds r3, r1, 0\n\ + subs r2, 0x1\n\ + movs r5, 0x1\n\ + negs r5, r5\n\ + b _080091B2\n\ +_0800919A:\n\ + strb r0, [r4]\n\ + adds r3, 0x1\n\ + adds r4, 0x1\n\ + subs r0, r3, 0x1\n\ + ldrb r0, [r0]\n\ + cmp r0, 0xF9\n\ + bne _080091B0\n\ + ldrb r0, [r3]\n\ + strb r0, [r4]\n\ + adds r3, 0x1\n\ + adds r4, 0x1\n\ +_080091B0:\n\ + subs r2, 0x1\n\ +_080091B2:\n\ + cmp r2, r5\n\ + beq _080091BE\n\ + ldrb r0, [r3]\n\ + adds r1, r0, 0\n\ + cmp r1, 0xFF\n\ + bne _0800919A\n\ +_080091BE:\n\ + movs r0, 0xFF\n\ + strb r0, [r4]\n\ + adds r0, r4, 0\n\ + pop {r4,r5}\n\ + pop {r1}\n\ + bx r1\n\ + .syntax divided"); +} + +u32 StringLength_Multibyte(u8 *str) +{ + u32 length = 0; + + while (*str != EOS) + { + if (*str == 0xF9) + str++; + str++; + length++; + } + + return length; +} + +u8 *WriteColorChangeControlCode(u8 *dest, u32 colorType, u8 color) +{ + *dest = 0xFC; + dest++; + + switch (colorType) + { + case 0: + *dest = 1; + dest++; + break; + case 1: + *dest = 3; + dest++; + break; + case 2: + *dest = 2; + dest++; + break; + } + + *dest = color; + dest++; + *dest = EOS; + return dest; +} + +bool32 sub_8009228(u8 *str) +{ + while (*str != EOS) + { + if (*str <= 0xA0) + if (*str != 0) + return TRUE; + str++; + } + + return FALSE; +} + +bool32 sub_800924C(u8 *str, s32 n) +{ + s32 i; + + for (i = 0; *str != EOS && i < n; i++) + { + if (*str <= 0xA0) + if (*str != 0) + return TRUE; + str++; + } + + return FALSE; +} + +u8 GetExtCtrlCodeLength(u8 code) +{ + static const u8 lengths[] = + { + 1, + 2, + 2, + 2, + 4, + 2, + 2, + 1, + 2, + 1, + 1, + 3, + 2, + 2, + 2, + 1, + 3, + 2, + 2, + 2, + 2, + 1, + 1, + 1, + 1, + }; + + u8 length = 0; + if (code < ARRAY_COUNT(lengths)) + length = lengths[code]; + return length; +} + +static const u8 *SkipExtCtrlCode(const u8 *s) +{ + while (*s == 0xFC) + { + s++; + s += GetExtCtrlCodeLength(*s); + } + + return s; +} + +s32 StringCompareWithoutExtCtrlCodes(const u8 *str1, const u8 *str2) +{ + s32 retVal = 0; + + while (1) + { + str1 = SkipExtCtrlCode(str1); + str2 = SkipExtCtrlCode(str2); + + if (*str1 > *str2) + break; + + if (*str1 < *str2) + { + retVal = -1; + if (*str2 == 0xFF) + retVal = 1; + } + + if (*str1 == 0xFF) + return retVal; + + str1++; + str2++; + } + + retVal = 1; + + if (*str1 == 0xFF) + retVal = -1; + + return retVal; +} + +void ConvertInternationalString(u8 *s, u8 language) +{ + if (language == LANGUAGE_JAPANESE) + { + u8 i; + + StripExtCtrlCodes(s); + i = StringLength(s); + s[i++] = 0xFC; + s[i++] = 22; + s[i++] = 0xFF; + + i--; + + while (i != (u8)-1) + { + s[i + 2] = s[i]; + i--; + } + + s[0] = 0xFC; + s[1] = 21; + } +} + +void StripExtCtrlCodes(u8 *str) +{ + u16 srcIndex = 0; + u16 destIndex = 0; + while (str[srcIndex] != 0xFF) + { + if (str[srcIndex] == 0xFC) + { + srcIndex++; + srcIndex += GetExtCtrlCodeLength(str[srcIndex]); + } + else + { + str[destIndex++] = str[srcIndex++]; + } + } + str[destIndex] = 0xFF; +} diff --git a/sym_ewram.txt b/sym_ewram.txt index fe852547a..763b7df8f 100644 --- a/sym_ewram.txt +++ b/sym_ewram.txt @@ -76,8 +76,7 @@ gStringVar3: @ 2021EC4 gStringVar4: @ 2021FC4 .space 0x3E8 -gUnknownStringVar: @ 20223AC - .space 0x10 + .include "src/string_util.o" gUnknown_020223BC: @ 20223BC .space 0x1