[WIP] Language loader optimizations

This commit is contained in:
NoHomoBoi 2020-09-07 03:16:07 -05:00
parent 3096e20fd6
commit 0fbd6263cc
4 changed files with 32 additions and 25 deletions

View File

@ -9,13 +9,15 @@
#include <dirent.h> #include <dirent.h>
#include "libs/cJSON.h" #include "libs/cJSON.h"
#define PLACEHOLDER "You are not supposed\nto be here.\n\nKeep this as a secret\n\n- Render96 Team"
struct DialogEntry* * dialogPool; struct DialogEntry* * dialogPool;
struct LanguageEntry* * languages; struct LanguageEntry* * languages;
s8 languagesAmount = 0; s8 languagesAmount = 0;
char* currentLanguage = "english"; struct LanguageEntry* current_language;
void preloadLanguageText(char* jsonTxt, s8 language){ void load_language(char* jsonTxt, s8 language){
languages[language] = malloc (sizeof (struct LanguageEntry)); languages[language] = malloc (sizeof (struct LanguageEntry));
cJSON *json = cJSON_Parse(jsonTxt); cJSON *json = cJSON_Parse(jsonTxt);
@ -38,8 +40,7 @@ void preloadLanguageText(char* jsonTxt, s8 language){
struct DialogEntry** entries = malloc(DIALOG_COUNT * sizeof(struct DialogEntry)); struct DialogEntry** entries = malloc(DIALOG_COUNT * sizeof(struct DialogEntry));
languages[language]->name = cJSON_GetObjectItemCaseSensitive(manifest, "languageName")->valuestring; languages[language]->name = cJSON_GetObjectItemCaseSensitive(manifest, "languageName")->valuestring;
languages[language]->logo = cJSON_GetObjectItemCaseSensitive(manifest, "languageLogo")->valuestring; languages[language]->logo = cJSON_GetObjectItemCaseSensitive(manifest, "languageLogo")->valuestring;
languages[language]->placeholder = cJSON_GetObjectItemCaseSensitive(manifest, "placeholder")->valuestring;
dialogs = cJSON_GetObjectItemCaseSensitive(json, "dialogs"); dialogs = cJSON_GetObjectItemCaseSensitive(json, "dialogs");
@ -108,32 +109,37 @@ void alloc_languages(void){
languagesAmount++; languagesAmount++;
char * jsonTxt = read_file(language_file); char * jsonTxt = read_file(language_file);
preloadLanguageText(jsonTxt, languagesAmount - 1); load_language(jsonTxt, languagesAmount - 1);
} }
} }
languages = realloc(languages, sizeof(struct LanguageEntry*) * (languagesAmount)); languages = realloc(languages, sizeof(struct LanguageEntry*) * (languagesAmount));
} }
void selectLanguage(char* languageName){ struct LanguageEntry* get_language_by_name(char* name){
char* lowerName = malloc(sizeof(languageName)); char* lowerName = malloc(sizeof(name));
for(char l = 0; l < sizeof(lowerName) / sizeof(char); l++) for(char l = 0; l < sizeof(lowerName) / sizeof(char); l++)
lowerName[l] = tolower(languageName[l]); lowerName[l] = tolower(name[l]);
int id = 0; int id = 0;
char* languageTmp = "none";
for(int l = 0; l < languagesAmount; l++){ for(int l = 0; l < languagesAmount; l++){
if(strcmp(languages[l]->name, lowerName) == 0){ if(strcmp(languages[l]->name, lowerName) == 0){
id = l; id = l;
languageTmp = languages[l]->name;
break; break;
} }
} }
currentLanguage = languageTmp; return languages[id];
dialogPool = languages[id]->entries; }
struct LanguageEntry* get_language(){
return current_language;
}
void set_language(struct LanguageEntry* new_language){
current_language = new_language;
dialogPool = new_language->entries;
} }
void alloc_dialog_pool(void){ void alloc_dialog_pool(void){
@ -141,8 +147,7 @@ void alloc_dialog_pool(void){
languages[0] = malloc (sizeof (struct LanguageEntry)); languages[0] = malloc (sizeof (struct LanguageEntry));
languages[0]->name = "none"; languages[0]->name = "none";
languages[0]->logo = "none"; languages[0]->logo = "none";
languages[0]->placeholder = "You are not supposed\nto be here.\n\nKeep this as a secret\n\n- Render96 Team";
languages[0]->entries = malloc(DIALOG_COUNT * sizeof(struct DialogEntry)); languages[0]->entries = malloc(DIALOG_COUNT * sizeof(struct DialogEntry));
for(int i = 0; i < DIALOG_COUNT; i++){ for(int i = 0; i < DIALOG_COUNT; i++){
@ -152,11 +157,11 @@ void alloc_dialog_pool(void){
entry->linesPerBox = 6; entry->linesPerBox = 6;
entry->leftOffset = 95; entry->leftOffset = 95;
entry->width = 200; entry->width = 200;
entry->str = getTranslatedText(languages[0]->placeholder); entry->str = getTranslatedText(PLACEHOLDER);
languages[0]->entries[i] = entry; languages[0]->entries[i] = entry;
} }
alloc_languages(); alloc_languages();
selectLanguage(currentLanguage); set_language(get_language_by_name("english"));
} }

View File

@ -7,15 +7,19 @@
extern char* currentLanguage; extern char* currentLanguage;
extern s8 languagesAmount; extern s8 languagesAmount;
extern struct DialogEntry ** dialogPool; extern struct DialogEntry* * dialogPool;
extern struct LanguageEntry* * languages;
extern char* read_file(char* name); extern char* read_file(char* name);
struct LanguageEntry { struct LanguageEntry {
char * name; char * name;
char * logo; char * logo;
char * placeholder;
struct DialogEntry* * entries; struct DialogEntry* * entries;
}; };
extern struct LanguageEntry* get_language_by_name(char* name);
extern struct LanguageEntry* get_language();
extern void set_language(struct LanguageEntry* new_language);
extern void alloc_dialog_pool(void); extern void alloc_dialog_pool(void);
#endif #endif

View File

@ -1,8 +1,7 @@
{ {
"manifest": { "manifest": {
"languageName": "Spanish", "languageName": "Spanish",
"languageLogo": "none", "languageLogo": "none"
"placeholder": "You are not supposed\nto be here.\n\nKeep this as a secret\n\n- Render96 Team"
}, },
"dialogs": [ "dialogs": [
{ {

View File

@ -1,8 +1,7 @@
{ {
"manifest": { "manifest": {
"languageName": "English", "languageName": "English",
"languageLogo": "none", "languageLogo": "none"
"placeholder": "You are not supposed\nto be here.\n\nKeep this as a secret\n\n- Render96 Team"
}, },
"dialogs": [ "dialogs": [
{ {