Skip to content

Commit

Permalink
Add language selection on first load & from HOME
Browse files Browse the repository at this point in the history
  • Loading branch information
Epicpkmn11 authored and d0k3 committed Apr 15, 2023
1 parent 8303440 commit 439e063
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 0 deletions.
2 changes: 2 additions & 0 deletions arm9/source/filesys/support.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "common.h"

// scripts / payloads dir names
#define LANGUAGES_DIR "languages"
#define SCRIPTS_DIR "scripts"
#define PAYLOADS_DIR "payloads"

Expand All @@ -11,5 +12,6 @@ size_t LoadSupportFile(const char* fname, void* buffer, size_t max_len);
bool SaveSupportFile(const char* fname, void* buffer, size_t len);
bool SetAsSupportFile(const char* fname, const char* source);

bool GetSupportDir(char* path, const char* dname);
bool CheckSupportDir(const char* fpath);
bool FileSelectorSupport(char* result, const char* text, const char* dname, const char* pattern);
62 changes: 62 additions & 0 deletions arm9/source/godmode.c
Original file line number Diff line number Diff line change
Expand Up @@ -2446,6 +2446,34 @@ u32 GodMode(int entrypoint) {

if (!language_loaded) {
SetLanguage(NULL, 0);

char loadpath[256];
if (LanguageMenu(loadpath, "Select Language for GodMode9:")) {
size_t fsize = FileGetSize(loadpath);
if (fsize > 0) {
char* data = (char*)malloc(fsize);
if (data) {
FileGetData(loadpath, data, fsize, 0);
SaveSupportFile("language.trf", data, fsize);
SetLanguage(data, fsize);
free(data);
}
}

// Try load font with the same name
char *ext = strstr(loadpath, ".trf");
strcpy(ext, ".frf");
fsize = FileGetSize(loadpath);
if (fsize > 0) {
char* data = (char*)malloc(fsize);
if (data) {
FileGetData(loadpath, data, fsize, 0);
SaveSupportFile("font.frf", data, fsize);
SetFont(data, fsize);
free(data);
}
}
}
}

// check for embedded essential backup
Expand Down Expand Up @@ -2888,6 +2916,7 @@ u32 GodMode(int entrypoint) {
u32 n_opt = 0;
int poweroff = ++n_opt;
int reboot = ++n_opt;
int language = ++n_opt;
int brick = (HID_ReadState() & BUTTON_R1) ? ++n_opt : 0;
int titleman = ++n_opt;
int scripts = ++n_opt;
Expand All @@ -2896,6 +2925,7 @@ u32 GodMode(int entrypoint) {
if (poweroff > 0) optionstr[poweroff - 1] = STR_POWEROFF_SYSTEM;
if (reboot > 0) optionstr[reboot - 1] = STR_REBOOT_SYSTEM;
if (titleman > 0) optionstr[titleman - 1] = STR_TITLE_MANAGER;
if (language > 0) optionstr[language - 1] = STR_LANGUAGE;
if (brick > 0) optionstr[brick - 1] = STR_BRICK_MY_3DS;
if (scripts > 0) optionstr[scripts - 1] = STR_SCRIPTS;
if (payloads > 0) optionstr[payloads - 1] = STR_PAYLOADS;
Expand Down Expand Up @@ -2934,6 +2964,38 @@ u32 GodMode(int entrypoint) {
break;
} else ShowPrompt(false, "%s", STR_FAILED_SETTING_UP_TITLE_MANAGER);
}
} else if (user_select == language) {
if (!CheckSupportDir(LANGUAGES_DIR)) {
ShowPrompt(false, STR_LANGUAGES_DIRECTORY_NOT_FOUND, LANGUAGES_DIR);
} else if (LanguageMenu(loadpath, STR_HOME_LANGUAGE_MENU_SELECT_LANGUAGE)) {
size_t fsize = FileGetSize(loadpath);
if (fsize > 0) {
char* data = (char*)malloc(fsize);
if (data) {
FileGetData(loadpath, data, fsize, 0);
SaveSupportFile("language.trf", data, fsize);
SetLanguage(data, fsize);
free(data);
}

// Try load font with the same name
char *ext = strstr(loadpath, ".trf");
strcpy(ext, ".frf");
fsize = FileGetSize(loadpath);
if (fsize > 0) {
char* data = (char*)malloc(fsize);
if (data) {
FileGetData(loadpath, data, fsize, 0);
SaveSupportFile("font.frf", data, fsize);
SetFont(data, fsize);
free(data);
}
}
}
GetDirContents(current_dir, current_path);
ClearScreenF(true, true, COLOR_STD_BG);
break;
}
} else if (user_select == scripts) {
if (!CheckSupportDir(SCRIPTS_DIR)) {
ShowPrompt(false, STR_SCRIPTS_DIRECTORY_NOT_FOUND, SCRIPTS_DIR);
Expand Down
53 changes: 53 additions & 0 deletions arm9/source/language.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#include "language.h"
#include "fsdrive.h"
#include "fsutil.h"
#include "support.h"
#include "ui.h"

#define STRING(what, def) const char* STR_##what = NULL;
#include "language.inl"
Expand Down Expand Up @@ -133,3 +137,52 @@ const void* GetLanguage(const void* riff, const u32 riff_size, u32* version, u32

return NULL;
}

int compLanguage(const void *e1, const void *e2) {
const Language *entry2 = (const Language *) e2;
const Language *entry1 = (const Language *) e1;
return strncasecmp(entry1->name, entry2->name, 256);
}

bool LanguageMenu(char* result, const char* title) {
DirStruct* langDir = (DirStruct*)malloc(sizeof(DirStruct));
if (!langDir) return false;

char path[256];
if (!GetSupportDir(path, LANGUAGES_DIR)) return false;
GetDirContents(langDir, path);

char* header = (char*)malloc(0x2C0);
Language* langs = (Language*)malloc(langDir->n_entries * sizeof(Language));
int langCount = 0;

// Find all valid files and get their language names
for (u32 i = 0; i < langDir->n_entries; i++) {
if (langDir->entry[i].type == T_FILE) {
size_t fsize = FileGetSize(langDir->entry[i].path);
FileGetData(langDir->entry[i].path, header, 0x2C0, 0);
if (GetLanguage(header, fsize, NULL, NULL, langs[langCount].name)) {
memcpy(langs[langCount].path, langDir->entry[i].path, 256);
langCount++;
}
}
}

free(langDir);
free(header);

qsort(langs, langCount, sizeof(Language), compLanguage);

// Make an array of just the names for the select promt
const char* langNames[langCount];
for (int i = 0; i < langCount; i++) {
langNames[i] = langs[i].name;
}

u32 selected = ShowSelectPrompt(langCount, langNames, "%s", title);
if (selected > 0 && result) {
memcpy(result, langs[selected - 1].path, 256);
}

return selected > 0;
}
2 changes: 2 additions & 0 deletions arm9/source/language.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@

bool SetLanguage(const void* translation, u32 translation_size);
const void* GetLanguage(const void* riff, u32 riff_size, u32* version, u32* count, char* language_name);

bool LanguageMenu(char* result, const char* title);
3 changes: 3 additions & 0 deletions arm9/source/language.inl
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ STRING(FAILED_CREATING_FOLDER_PATH, "Failed creating folder:\n%s")
STRING(FAILED_CREATING_FILE_PATH, "Failed creating file:\n%s")
STRING(TITLE_MANAGER, "Title manager")
STRING(BRICK_MY_3DS, "Brick my 3DS")
STRING(LANGUAGE, "Language...")
STRING(SCRIPTS, "Scripts...")
STRING(PAYLOADS, "Payloads...")
STRING(MORE, "More...")
Expand All @@ -469,7 +470,9 @@ STRING(4_DRIVE_NAND_TWL, "[4:] NAND / TWL")
STRING(A_DRIVE_SD_CARD, "[A:] SD CARD")
STRING(B_DRIVE_SD_CARD, "[B:] SD CARD")
STRING(TITLE_MANAGER_MENU_SELECT_TITLES_SOURCE, "Title manager menu.\nSelect titles source:")
STRING(LANGUAGES_DIRECTORY_NOT_FOUND, "Languages directory not found.\n(default path: 0:/gm9/%s)")
STRING(SCRIPTS_DIRECTORY_NOT_FOUND, "Scripts directory not found.\n(default path: 0:/gm9/%s)")
STRING(HOME_LANGUAGE_MENU_SELECT_LANGUAGE, "HOME language... menu.\nSelect language:")
STRING(HOME_SCRIPTS_MENU_SELECT_SCRIPT, "HOME scripts... menu.\nSelect script:")
STRING(PAYLOADS_DIRECTORY_NOT_FOUND, "Payloads directory not found.\n(default path: 0:/gm9/%s)")
STRING(HOME_PAYLOADS_MENU_SELECT_PAYLOAD, "HOME payloads... menu.\nSelect payload:")
Expand Down
3 changes: 3 additions & 0 deletions resources/languages/source.json
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@
"FAILED_CREATING_FILE_PATH": "Failed creating file:\n%s",
"TITLE_MANAGER": "Title manager",
"BRICK_MY_3DS": "Brick my 3DS",
"LANGUAGE": "Language...",
"SCRIPTS": "Scripts...",
"PAYLOADS": "Payloads...",
"MORE": "More...",
Expand All @@ -467,7 +468,9 @@
"A_DRIVE_SD_CARD": "[A:] SD CARD",
"B_DRIVE_SD_CARD": "[B:] SD CARD",
"TITLE_MANAGER_MENU_SELECT_TITLES_SOURCE": "Title manager menu.\nSelect titles source:",
"LANGUAGES_DIRECTORY_NOT_FOUND": "Languages directory not found.\n(default path: 0:/gm9/%s)",
"SCRIPTS_DIRECTORY_NOT_FOUND": "Scripts directory not found.\n(default path: 0:/gm9/%s)",
"HOME_LANGUAGE_MENU_SELECT_LANGUAGE": "HOME language... menu.\nSelect language:",
"HOME_SCRIPTS_MENU_SELECT_SCRIPT": "HOME scripts... menu.\nSelect script:",
"PAYLOADS_DIRECTORY_NOT_FOUND": "Payloads directory not found.\n(default path: 0:/gm9/%s)",
"HOME_PAYLOADS_MENU_SELECT_PAYLOAD": "HOME payloads... menu.\nSelect payload:",
Expand Down

0 comments on commit 439e063

Please sign in to comment.