[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 "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 LanguageEntry* * languages;
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));
cJSON *json = cJSON_Parse(jsonTxt);
@ -39,7 +41,6 @@ void preloadLanguageText(char* jsonTxt, s8 language){
languages[language]->name = cJSON_GetObjectItemCaseSensitive(manifest, "languageName")->valuestring;
languages[language]->logo = cJSON_GetObjectItemCaseSensitive(manifest, "languageLogo")->valuestring;
languages[language]->placeholder = cJSON_GetObjectItemCaseSensitive(manifest, "placeholder")->valuestring;
dialogs = cJSON_GetObjectItemCaseSensitive(json, "dialogs");
@ -108,32 +109,37 @@ void alloc_languages(void){
languagesAmount++;
char * jsonTxt = read_file(language_file);
preloadLanguageText(jsonTxt, languagesAmount - 1);
load_language(jsonTxt, languagesAmount - 1);
}
}
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++)
lowerName[l] = tolower(languageName[l]);
lowerName[l] = tolower(name[l]);
int id = 0;
char* languageTmp = "none";
for(int l = 0; l < languagesAmount; l++){
if(strcmp(languages[l]->name, lowerName) == 0){
id = l;
languageTmp = languages[l]->name;
break;
}
}
}
currentLanguage = languageTmp;
dialogPool = languages[id]->entries;
return languages[id];
}
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){
@ -142,7 +148,6 @@ void alloc_dialog_pool(void){
languages[0] = malloc (sizeof (struct LanguageEntry));
languages[0]->name = "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));
for(int i = 0; i < DIALOG_COUNT; i++){
@ -152,11 +157,11 @@ void alloc_dialog_pool(void){
entry->linesPerBox = 6;
entry->leftOffset = 95;
entry->width = 200;
entry->str = getTranslatedText(languages[0]->placeholder);
entry->str = getTranslatedText(PLACEHOLDER);
languages[0]->entries[i] = entry;
}
alloc_languages();
selectLanguage(currentLanguage);
set_language(get_language_by_name("english"));
}

View File

@ -7,15 +7,19 @@
extern char* currentLanguage;
extern s8 languagesAmount;
extern struct DialogEntry ** dialogPool;
extern struct DialogEntry* * dialogPool;
extern struct LanguageEntry* * languages;
extern char* read_file(char* name);
struct LanguageEntry {
char * name;
char * logo;
char * placeholder;
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);
#endif

View File

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

View File

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