Skip to content

Commit 890520f

Browse files
committed
C2D_FontLoadSystem: use libctru romfs API instead of winging it
1 parent 23d26a8 commit 890520f

1 file changed

Lines changed: 72 additions & 79 deletions

File tree

source/font.c

Lines changed: 72 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -104,101 +104,94 @@ C2D_Font C2D_FontLoadFromHandle(FILE* handle)
104104
return font;
105105
}
106106

107-
static C2D_Font C2Di_FontLoadFromArchive(u64 binary_lowpath)
107+
static C2D_Font C2Di_FontLoadFromArchive(u64 tid, const char* path)
108108
{
109-
C2D_Font font = C2Di_FontAlloc();
110-
if (font)
109+
void* fontLzData = NULL;
110+
u32 fontLzSize = 0;
111+
112+
Result rc = romfsMountFromTitle(tid, MEDIATYPE_NAND, "font");
113+
if (R_FAILED(rc))
114+
return NULL;
115+
116+
FILE* f = fopen(path, "rb");
117+
if (f)
111118
{
112-
int fontNum = (binary_lowpath & 0x300) >> 8;
113-
const u8 sizeMod = fontNum == 0 ? 0xA0 : 0xB0;
114-
u64 lowPath[] = { binary_lowpath, 0x00000001FFFFFE00 };
115-
Handle romfs_handle;
116-
u64 romfs_size = 0;
117-
u32 romfs_bytes_read = 0;
118-
119-
FS_Path savedatacheck_path = { PATH_BINARY, 16, (u8*)lowPath };
120-
u8 file_binary_lowpath[20] = {};
121-
FS_Path romfs_path = { PATH_BINARY, 20, file_binary_lowpath };
122-
123-
if (R_FAILED(FSUSER_OpenFileDirectly(&romfs_handle, (FS_ArchiveID)0x2345678a, savedatacheck_path, romfs_path, FS_OPEN_READ, 0)))
124-
{
125-
free(font);
126-
return NULL;
127-
}
128-
if (R_FAILED(FSFILE_GetSize(romfs_handle, &romfs_size)))
129-
{
130-
free(font);
131-
FSFILE_Close(romfs_handle);
132-
return NULL;
133-
}
119+
fseek(f, 0, SEEK_END);
120+
fontLzSize = ftell(f);
121+
rewind(f);
134122

135-
u8* romfs_data_buffer = malloc(romfs_size);
136-
if (!romfs_data_buffer)
137-
{
138-
free(font);
139-
FSFILE_Close(romfs_handle);
140-
return NULL;
141-
}
142-
if (R_FAILED(FSFILE_Read(romfs_handle, &romfs_bytes_read, 0, romfs_data_buffer, romfs_size)))
143-
{
144-
free(romfs_data_buffer);
145-
free(font);
146-
FSFILE_Close(romfs_handle);
147-
return NULL;
148-
}
149-
FSFILE_Close(romfs_handle);
123+
fontLzData = malloc(fontLzSize);
124+
if (fontLzData)
125+
fread(fontLzData, 1, fontLzSize, f);
150126

151-
u8* compFontData = romfs_data_buffer + sizeMod;
127+
fclose(f);
128+
}
152129

153-
u32 fontSize = *(u32*)(compFontData) >> 8;
154-
font->cfnt = linearAlloc(fontSize);
155-
if (font->cfnt)
156-
{
157-
if (!decompress_LZ11(font->cfnt, fontSize, NULL, compFontData + 4, romfs_size - sizeMod - 4))
158-
{
159-
C2D_FontFree(font);
160-
return NULL;
161-
}
162-
}
130+
romfsUnmount("font");
163131

164-
free(romfs_data_buffer);
132+
if (!fontLzData)
133+
return NULL;
165134

166-
font = C2Di_PostLoadFont(font);
135+
C2D_Font font = C2Di_FontAlloc();
136+
if (!font)
137+
{
138+
free(fontLzData);
139+
return NULL;
167140
}
168141

169-
return font;
142+
u32 fontSize = *(u32*)fontLzData >> 8;
143+
font->cfnt = linearAlloc(fontSize);
144+
if (font->cfnt && !decompress_LZ11(font->cfnt, fontSize, NULL, (u8*)fontLzData + 4, fontLzSize - 4))
145+
{
146+
linearFree(font->cfnt);
147+
font->cfnt = NULL;
148+
}
149+
free(fontLzData);
150+
151+
return C2Di_PostLoadFont(font);
170152
}
171153

154+
static unsigned C2Di_RegionToFontIndex(CFG_Region region)
155+
{
156+
switch (region)
157+
{
158+
default:
159+
case CFG_REGION_JPN:
160+
case CFG_REGION_USA:
161+
case CFG_REGION_EUR:
162+
case CFG_REGION_AUS:
163+
return 0;
164+
case CFG_REGION_CHN:
165+
return 1;
166+
case CFG_REGION_KOR:
167+
return 2;
168+
case CFG_REGION_TWN:
169+
return 3;
170+
}
171+
}
172+
173+
static const char* const C2Di_FontPaths[] =
174+
{
175+
"font:/cbf_std.bcfnt.lz",
176+
"font:/cbf_zh-Hans-CN.bcfnt.lz",
177+
"font:/cbf_ko-Hang-KR.bcfnt.lz",
178+
"font:/cbf_zh-Hant-TW.bcfnt.lz",
179+
};
180+
172181
C2D_Font C2D_FontLoadSystem(CFG_Region region)
173182
{
174-
u8 systemRegion;
175-
if (R_FAILED(CFGU_SecureInfoGetRegion(&systemRegion)))
176-
return NULL;
183+
unsigned fontIdx = C2Di_RegionToFontIndex(region);
177184

178-
switch (region)
185+
u8 systemRegion = 0;
186+
Result rc = CFGU_SecureInfoGetRegion(&systemRegion);
187+
if (R_FAILED(rc) || fontIdx == C2Di_RegionToFontIndex((CFG_Region)systemRegion))
179188
{
180-
case 0:
181-
case 1:
182-
case 2:
183-
case 3:
184-
if (systemRegion > 3)
185-
return C2Di_FontLoadFromArchive(0x0004009b00014002);
186-
break;
187-
case 4:
188-
if (systemRegion == 4)
189-
break;
190-
return C2Di_FontLoadFromArchive(0x0004009b00014102);
191-
case 5:
192-
if (systemRegion == 5)
193-
break;
194-
return C2Di_FontLoadFromArchive(0x0004009b00014202);
195-
case 6:
196-
if (systemRegion == 6)
197-
break;
198-
return C2Di_FontLoadFromArchive(0x0004009b00014302);
189+
fontEnsureMapped();
190+
return NULL;
199191
}
200-
fontEnsureMapped();
201-
return NULL;
192+
193+
// Load the font
194+
return C2Di_FontLoadFromArchive(0x0004009b00014002ULL | (fontIdx<<8), C2Di_FontPaths[fontIdx]);
202195
}
203196

204197
void C2D_FontFree(C2D_Font font)

0 commit comments

Comments
 (0)