|
| 1 | +#include <PR/ultratypes.h> |
| 2 | +#include "common.h" |
| 3 | +#include "dlls/engine/21_gametext.h" |
| 4 | +#include "prevent_bss_reordering.h" |
| 5 | + |
| 6 | +static s8 sCurrentBankIndex = -1; |
| 7 | + |
| 8 | +/*0x0*/ static u16 sBankCount; |
| 9 | +/*0x2*/ static u16 sBankEntryCount; |
| 10 | +/*0x4*/ static u16 sBankSize; |
| 11 | +/*0x8*/ static u8 *sCurrentBank; |
| 12 | +/*0xc*/ static s32 sCurrentBank_GlobalOffset; |
| 13 | +// Number of strings in each chunk. |
| 14 | +/*0x10*/ static u8 *sCurrentBank_StrCounts; |
| 15 | +// Byte size of each chunk. |
| 16 | +/*0x14*/ static u16 *sCurrentBank_Sizes; |
| 17 | +// Byte offset to each chunk. |
| 18 | +/*0x18*/ static u16 *sCurrentBank_Offsets; |
| 19 | + |
| 20 | +void gametext_set_bank(s8 bank); |
| 21 | + |
| 22 | +void gametext_ctor(void *self) { |
| 23 | + u16 *header; |
| 24 | + |
| 25 | + header = (u16*)malloc(4, 0x19, NULL); |
| 26 | + queue_load_file_region_to_ptr((void**)header, GAMETEXT_TAB, 0, 4); |
| 27 | + |
| 28 | + sBankCount = header[0]; |
| 29 | + sBankEntryCount = header[1]; |
| 30 | + |
| 31 | + free(header); |
| 32 | + |
| 33 | + sBankSize = sBankEntryCount * 5 + 4; |
| 34 | + sCurrentBank = malloc(sBankSize, 0x19, NULL); |
| 35 | + sCurrentBank_StrCounts = sCurrentBank + 4; |
| 36 | + sCurrentBank_Sizes = (u16*)(sCurrentBank + sBankEntryCount + 4); |
| 37 | + sCurrentBank_Offsets = (u16*)(sCurrentBank + (sBankEntryCount * 3) + 4); |
| 38 | + |
| 39 | + gametext_set_bank(0); |
| 40 | +} |
| 41 | + |
| 42 | +void gametext_dtor(void *self) { |
| 43 | + free(sCurrentBank); |
| 44 | +} |
| 45 | + |
| 46 | +u16 gametext_bank_count() { |
| 47 | + return sBankCount; |
| 48 | +} |
| 49 | + |
| 50 | +u16 gametext_chunk_count() { |
| 51 | + return sBankEntryCount; |
| 52 | +} |
| 53 | + |
| 54 | +void gametext_set_bank(s8 bank) { |
| 55 | + u32 offset; |
| 56 | + |
| 57 | + offset = (sBankSize * bank) + 4; |
| 58 | + |
| 59 | + if (sCurrentBankIndex == bank) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + sCurrentBankIndex = bank; |
| 64 | + |
| 65 | + queue_load_file_region_to_ptr( |
| 66 | + (void**)sCurrentBank, |
| 67 | + GAMETEXT_TAB, |
| 68 | + offset, |
| 69 | + sBankSize); |
| 70 | + |
| 71 | + sCurrentBank_GlobalOffset = *((u32*)sCurrentBank); |
| 72 | +} |
| 73 | + |
| 74 | +s8 gametext_curr_bank() { |
| 75 | + return sCurrentBankIndex; |
| 76 | +} |
| 77 | + |
| 78 | +GameTextChunk *gametext_get_chunk(u16 chunk) { |
| 79 | + GameTextChunk *chunkPtr; |
| 80 | + s32 headerSize; |
| 81 | + s32 alignmentPad; |
| 82 | + s32 i; |
| 83 | + s32 k; |
| 84 | + |
| 85 | + // mem map |
| 86 | + // 0x0 GameTextChunk |
| 87 | + // 0xC string pointer array |
| 88 | + // alignment padding |
| 89 | + // string chunk |
| 90 | + |
| 91 | + // Size for struct and string pointers |
| 92 | + headerSize = sCurrentBank_StrCounts[chunk] * 4 + sizeof(GameTextChunk); |
| 93 | + // Room for alignment |
| 94 | + alignmentPad = headerSize % 8; |
| 95 | + |
| 96 | + headerSize += alignmentPad + sCurrentBank_Sizes[chunk]; |
| 97 | + |
| 98 | + chunkPtr = (GameTextChunk*)malloc(headerSize, 0x19, NULL); |
| 99 | + |
| 100 | + chunkPtr->strings = (char**)((u32)chunkPtr + sizeof(GameTextChunk)); |
| 101 | + |
| 102 | + chunkPtr->count = sCurrentBank_StrCounts[chunk]; |
| 103 | + |
| 104 | + // Where to load actual chunk contents to (also contains data for some chunks) |
| 105 | + chunkPtr->unk4 = (void*)((u32)chunkPtr->strings + sCurrentBank_StrCounts[chunk] * 4 + alignmentPad); |
| 106 | + |
| 107 | + // Set up first string pointer |
| 108 | + chunkPtr->strings[0] = (char*)((u32)chunkPtr->unk4 + sCurrentBank_StrCounts[chunk] * 2); |
| 109 | + |
| 110 | + queue_load_file_region_to_ptr( |
| 111 | + (void**)chunkPtr->unk4, |
| 112 | + GAMETEXT_BIN, |
| 113 | + sCurrentBank_Offsets[chunk] * 2 + sCurrentBank_GlobalOffset, |
| 114 | + sCurrentBank_Sizes[chunk]); |
| 115 | + |
| 116 | + // Set up remaining pointers to each string |
| 117 | + for (i = 1; i < sCurrentBank_StrCounts[chunk]; i++) { |
| 118 | + k = 0; |
| 119 | + |
| 120 | + while (chunkPtr->strings[i - 1][k++] != '\0') {} |
| 121 | + |
| 122 | + chunkPtr->strings[i] = chunkPtr->strings[i - 1] + k; |
| 123 | + } |
| 124 | + |
| 125 | + return chunkPtr; |
| 126 | +} |
| 127 | + |
| 128 | +char *gametext_get_text(u16 chunk, u16 strIndex) { |
| 129 | + u8 *text; |
| 130 | + char *str; |
| 131 | + char *copy; |
| 132 | + s32 len; |
| 133 | + s32 i; |
| 134 | + s32 k; |
| 135 | + |
| 136 | + text = malloc(sCurrentBank_Sizes[chunk], 0x19, NULL); |
| 137 | + |
| 138 | + queue_load_file_region_to_ptr( |
| 139 | + (void**)text, |
| 140 | + GAMETEXT_BIN, |
| 141 | + sCurrentBank_Offsets[chunk] * 2, |
| 142 | + sCurrentBank_Sizes[chunk]); |
| 143 | + |
| 144 | + str = (char*)(text + sCurrentBank_StrCounts[chunk] * 2); |
| 145 | + |
| 146 | + for (i = 0; i != strIndex; i++) { |
| 147 | + k = 0; |
| 148 | + |
| 149 | + while (str[k++] != '\0') {} |
| 150 | + |
| 151 | + str += k; |
| 152 | + } |
| 153 | + |
| 154 | + len = strlen(str) + 1; |
| 155 | + |
| 156 | + copy = malloc(len, 0x19, NULL); |
| 157 | + bcopy(str, copy, len); |
| 158 | + |
| 159 | + free(text); |
| 160 | + |
| 161 | + return copy; |
| 162 | +} |
0 commit comments