Fixed language json loader on Windows

This commit is contained in:
NoHomoBoi 2020-08-23 15:40:56 -05:00
parent a6eb06e5ea
commit 646c2e08c9
3 changed files with 101 additions and 82 deletions

View File

@ -5,57 +5,75 @@
#include <string.h>
#include "dialog_ids.h"
#include "game/ingame_menu.h"
#include <unistd.h>
#include <limits.h>
#include "libs/dir_utils.h"
#include <stdlib.h>
#include <windows.h>
struct DialogEntry* * dialogPool;
#define _READFILE_GUESS 256
char* load_file(char const* path) {
char* buffer = 0;
long length = 0;
FILE * f = fopen (path, "rb");
#define SPANISH "./res/texts/es.json"
#define ENGLISH "./res/texts/us.json"
if (f) {
fseek (f, 0, SEEK_END);
length = ftell (f);
fseek (f, 0, SEEK_SET);
buffer = (char*) malloc((length+1)*sizeof(char));
if (buffer) {
fread(buffer, sizeof(char), length, f);
}
fclose(f);
buffer[length] = '\0';
return buffer;
char* read_file(char* name){
FILE* file;
file = fopen(name, "r");
if(!file)
return NULL;
char* result = malloc(sizeof(char) * _READFILE_GUESS + 1);
if(result == NULL)
return NULL;
size_t pos = 0;
size_t capacity = _READFILE_GUESS;
char ch;
while((ch = getc(file)) != EOF){
result[pos++] = ch;
if(pos >= capacity){
capacity += _READFILE_GUESS;
result = realloc(result, sizeof(char) * capacity + 1);
if(result == NULL)
return NULL;
}
}
return NULL;
fclose(file);
result = realloc(result, sizeof(char) * pos);
if(result == NULL)
return NULL;
result[pos] = '\0';
return result;
}
void preloadTexts(){
char * cwd = "/home/alex/Documents/Projects/Render96/Render96ex - FastBuild/build/us_pc";
//getcwd(cwd, sizeof(cwd));
#ifndef WIN32
char * file = "/res/texts/es.json";
#else
char * file = "\\res\\texts\\es.json";
#endif
char * file = ENGLISH;
char * language_file = malloc((strlen(cwd) + strlen(file)) * sizeof(char));
strcpy(language_file, "");
strcat(language_file, cwd);
strcat(language_file, file);
#ifndef WIN32
char * language_file = realpath(file, NULL);
#else
char * language_file = malloc(_MAX_PATH * sizeof(char));
_fullpath(language_file, file, _MAX_PATH );
#endif
printf("Loading File: %s\n", language_file);
char * jsonTxt = load_file( language_file );
if(jsonTxt == NULL) return;
char * jsonTxt = read_file(language_file);
cJSON *json = cJSON_Parse(jsonTxt);
if (json == NULL) {
const char *error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL) {
fprintf(stderr, "Error before: %s\n", error_ptr);
}else{
fprintf(stderr, "Error loading the JSON file\n");
}
exit(1);
}
@ -66,14 +84,14 @@ void preloadTexts(){
cJSON_ArrayForEach(dialog, dialogs) {
int id = cJSON_GetObjectItemCaseSensitive(dialog, "ID")->valueint;
struct DialogEntry *entry = malloc (sizeof (struct DialogEntry));
entry->unused = 1;
entry->linesPerBox = cJSON_GetObjectItemCaseSensitive(dialog, "linesPerBox")->valueint;
entry->leftOffset = cJSON_GetObjectItemCaseSensitive(dialog, "leftOffset")->valueint;
entry->width = cJSON_GetObjectItemCaseSensitive(dialog, "width")->valueint;
const cJSON *line = NULL;
const cJSON *lines = NULL;
lines = cJSON_GetObjectItemCaseSensitive(dialog, "lines");
@ -92,11 +110,12 @@ void preloadTexts(){
}
}
entry->str = getTranslatedText(dialogTxt);
entry->str = getTranslatedText(dialogTxt);
dialogPool[id] = entry;
free(dialogTxt);
}
free(jsonTxt);
cJSON_Delete(json);
}
void alloc_dialog_pool(void){
@ -104,7 +123,7 @@ void alloc_dialog_pool(void){
for(int i = 0; i < DIALOG_COUNT; i++){
struct DialogEntry *entry = malloc (sizeof (struct DialogEntry));
entry->unused = 1;
entry->linesPerBox = 6;
entry->leftOffset = 95;
@ -112,6 +131,6 @@ void alloc_dialog_pool(void){
entry->str = getTranslatedText("You are not supposed\nto be here.\n\nKeep this as a secret\n\n- Render96 Team");
dialogPool[i] = entry;
}
preloadTexts();
}
}

View File

@ -1,51 +1,51 @@
#include "txtconv.h"
struct Character charmap[340] = {
{"0", 0x00}, {"", 0x00}, {"1", 0x01}, {"", 0x01}, {"2", 0x02}, {"", 0x02}, {"3", 0x03}, {"", 0x03}, {"4", 0x04},
{"", 0x04}, {"5", 0x05}, {"", 0x05}, {"6", 0x06}, {"", 0x06}, {"7", 0x07}, {"", 0x07}, {"8", 0x08}, {"", 0x08},
{"9", 0x09}, {"", 0x09}, {"A", 0x0A}, {"", 0x0A}, {"B", 0x0B}, {"", 0x0B}, {"C", 0x0C}, {"", 0x0C}, {"D", 0x0D},
{"", 0x0D}, {"E", 0x0E}, {"", 0x0E}, {"F", 0x0F}, {"", 0x0F}, {"G", 0x10}, {"", 0x10}, {"H", 0x11}, {"", 0x11},
{"I", 0x12}, {"", 0x12}, {"J", 0x13}, {"", 0x13}, {"K", 0x14}, {"", 0x14}, {"L", 0x15}, {"", 0x15}, {"M", 0x16},
{"", 0x16}, {"N", 0x17}, {"", 0x17}, {"O", 0x18}, {"", 0x18}, {"P", 0x19}, {"", 0x19}, {"Q", 0x1A}, {"", 0x1A},
{"R", 0x1B}, {"", 0x1B}, {"S", 0x1C}, {"", 0x1C}, {"T", 0x1D}, {"", 0x1D}, {"U", 0x1E}, {"", 0x1E}, {"V", 0x1F},
{"", 0x1F}, {"W", 0x20}, {"", 0x20}, {"X", 0x21}, {"", 0x21}, {"Y", 0x22}, {"", 0x22}, {"Z", 0x23}, {"", 0x23},
{"a", 0x24}, {"b", 0x25}, {"c", 0x26}, {"d", 0x27}, {"e", 0x28}, {"f", 0x29}, {"g", 0x2A}, {"h", 0x2B}, {"i", 0x2C},
{"j", 0x2D}, {"k", 0x2E}, {"l", 0x2F}, {"m", 0x30}, {"n", 0x31}, {"o", 0x32}, {"p", 0x33}, {"q", 0x34}, {"r", 0x35},
{"s", 0x36}, {"t", 0x37}, {"u", 0x38}, {"v", 0x39}, {"w", 0x3A}, {"x", 0x3B}, {"y", 0x3C}, {"z", 0x3D}, {"\\", 0x3E},
{".", 0x3F}, {"", 0x41}, {"", 0x40}, {"", 0x41}, {"", 0x42}, {"", 0x43}, {"", 0x44}, {"", 0x45}, {"", 0x46},
{"", 0x47}, {"", 0x48}, {"", 0x49}, {"", 0x4A}, {"", 0x4B}, {"", 0x4C}, {"", 0x4D}, {"", 0x4E}, {"", 0x4F},
{"", 0x50}, {"", 0x51}, {"", 0x52}, {"", 0x53}, {"", 0x54}, {"", 0x55}, {"", 0x56}, {"", 0x57}, {"", 0x58},
{"", 0x59}, {"", 0x5A}, {"", 0x5B}, {"", 0x5C}, {"", 0x5D}, {"", 0x5E}, {"", 0x5F}, {"", 0x60}, {"", 0x61},
{"", 0x62}, {"", 0x63}, {"", 0x64}, {"", 0x65}, {"", 0x66}, {"", 0x67}, {"", 0x68}, {"", 0x69}, {"", 0x6A},
{"", 0x6B}, {"", 0x6C}, {"", 0x6D}, {"", 0x6E}, {",", 0x6F}, {"", 0x6F}, {"", 0x70}, {"", 0x71}, {"", 0x72},
{"", 0x73}, {"", 0x74}, {"", 0x75}, {"", 0x76}, {"", 0x77}, {"", 0x78}, {"", 0x79}, {"", 0x7A}, {"", 0x7B},
{"", 0x7C}, {"", 0x7D}, {"", 0x7E}, {"", 0x7F}, {"", 0x80}, {"", 0x81}, {"", 0x82}, {"", 0x83}, {"", 0x84},
{"", 0x85}, {"", 0x86}, {"", 0x87}, {"", 0x88}, {"", 0x89}, {"", 0x8A}, {"", 0x8B}, {"", 0x8C}, {"", 0x8D},
{"", 0x8E}, {"", 0x8F}, {"", 0x90}, {"", 0x91}, {"", 0x92}, {"", 0x93}, {"", 0x94}, {"", 0x95}, {"", 0x96},
{"", 0x97}, {"", 0x98}, {"", 0x99}, {"", 0x9A}, {"", 0x9B}, {"", 0x9C}, {"", 0x9D}, {" ", 0x9E}, {" ", 0x9E},
{"-", 0x9F}, {"", 0x9F}, {"", 0xA0}, {"", 0xA1}, {"", 0xA2}, {"", 0xA3}, {"", 0xA4}, {"", 0xA5}, {"", 0xA6},
{"", 0xA7}, {"", 0xA8}, {"", 0xD0}, {"", 0xD1}, {"", 0xD2}, {"", 0xD3}, {"", 0xD4}, {"", 0xD5}, {"", 0xD6},
{"", 0xD7}, {"", 0xD8}, {"[%]", 0xE0}, {"(", 0xE1}, {"", 0xE1}, {")(", 0xE2}, {"", 0xE2}, {")", 0xE3}, {"", 0xE3},
{"+", 0xE4}, {"", 0xE4}, {"&", 0xE5}, {":", 0xE6}, {"", 0xF0}, {"", 0xF1}, {"!", 0xF2}, {"", 0xF2}, {"%", 0xF3},
{"", 0xF3}, {"?", 0xF4}, {"", 0xF4}, {"", 0xF5}, {"", 0xF6}, {"~", 0xF7}, {"", 0xF7}, {"", 0xF8}, {"$", 0xF9},
{"", 0xFA}, {"×", 0xFB}, {"", 0xFC}, {"", 0xFD}, {"\n", 0xFE}, {"", 0x45}, {"", 0x46}, {"", 0x47}, {"", 0x48},
{"", 0x49}, {"", 0x4A}, {"", 0x4B}, {"", 0x4C}, {"", 0x4D}, {"", 0x4E}, {"", 0x4F}, {"", 0x50}, {"", 0x51},
{"", 0x52}, {"", 0x53}, {"", 0x59}, {"", 0x5A}, {"", 0x5B}, {"", 0x5C}, {"", 0x5D}, {"", 0x75}, {"", 0x76},
{"", 0x77}, {"", 0x78}, {"", 0x79}, {"", 0x7A}, {"", 0x7B}, {"", 0x7C}, {"", 0x7D}, {"", 0x7E}, {"", 0x7F},
{"", 0x80}, {"", 0x81}, {"", 0x82}, {"", 0x83}, {"", 0x89}, {"", 0x8A}, {"", 0x8B}, {"", 0x8C}, {"", 0x8D},
{"", 0x59}, {"", 0x5A}, {"", 0x5B}, {"", 0x5C}, {"", 0x5D}, {"", 0x89}, {"", 0x8A}, {"", 0x8B}, {"", 0x8C},
{"", 0x8D}, {"^", 0x50}, {"|", 0x51}, {"<", 0x52}, {">", 0x53}, {"[A]", 0x54}, {"[B]", 0x55}, {"[C]", 0x56}, {"[Z]", 0x57},
{"[R]", 0x58}, {"/", 0xD0}, {"the", 0xD1}, {"you", 0xD2}, {"à", 0x60}, {"â", 0x61}, {"ä", 0x62}, {"À", 0x64}, {"Â", 0x65},
{"Ä", 0x66}, {"è", 0x70}, {"ê", 0x71}, {"ë", 0x72}, {"é", 0x73}, {"È", 0x74}, {"Ê", 0x75}, {"Ë", 0x76}, {"É", 0x77},
{"ù", 0x80}, {"û", 0x81}, {"ü", 0x82}, {"Ù", 0x84}, {"Û", 0x85}, {"Ü", 0x86}, {"ô", 0x91}, {"ö", 0x92}, {"Ô", 0x95},
{"0", 0x00}, {"", 0x00}, {"1", 0x01}, {"", 0x01}, {"2", 0x02}, {"", 0x02}, {"3", 0x03}, {"", 0x03}, {"4", 0x04},
{"", 0x04}, {"5", 0x05}, {"", 0x05}, {"6", 0x06}, {"", 0x06}, {"7", 0x07}, {"", 0x07}, {"8", 0x08}, {"", 0x08},
{"9", 0x09}, {"", 0x09}, {"A", 0x0A}, {"", 0x0A}, {"B", 0x0B}, {"", 0x0B}, {"C", 0x0C}, {"", 0x0C}, {"D", 0x0D},
{"", 0x0D}, {"E", 0x0E}, {"", 0x0E}, {"F", 0x0F}, {"", 0x0F}, {"G", 0x10}, {"", 0x10}, {"H", 0x11}, {"", 0x11},
{"I", 0x12}, {"", 0x12}, {"J", 0x13}, {"", 0x13}, {"K", 0x14}, {"", 0x14}, {"L", 0x15}, {"", 0x15}, {"M", 0x16},
{"", 0x16}, {"N", 0x17}, {"", 0x17}, {"O", 0x18}, {"", 0x18}, {"P", 0x19}, {"", 0x19}, {"Q", 0x1A}, {"", 0x1A},
{"R", 0x1B}, {"", 0x1B}, {"S", 0x1C}, {"", 0x1C}, {"T", 0x1D}, {"", 0x1D}, {"U", 0x1E}, {"", 0x1E}, {"V", 0x1F},
{"", 0x1F}, {"W", 0x20}, {"", 0x20}, {"X", 0x21}, {"", 0x21}, {"Y", 0x22}, {"", 0x22}, {"Z", 0x23}, {"", 0x23},
{"a", 0x24}, {"b", 0x25}, {"c", 0x26}, {"d", 0x27}, {"e", 0x28}, {"f", 0x29}, {"g", 0x2A}, {"h", 0x2B}, {"i", 0x2C},
{"j", 0x2D}, {"k", 0x2E}, {"l", 0x2F}, {"m", 0x30}, {"n", 0x31}, {"o", 0x32}, {"p", 0x33}, {"q", 0x34}, {"r", 0x35},
{"s", 0x36}, {"t", 0x37}, {"u", 0x38}, {"v", 0x39}, {"w", 0x3A}, {"x", 0x3B}, {"y", 0x3C}, {"z", 0x3D}, {"'", 0x3E},
{".", 0x3F}, {"", 0x41}, {"", 0x40}, {"", 0x41}, {"", 0x42}, {"", 0x43}, {"", 0x44}, {"", 0x45}, {"", 0x46},
{"", 0x47}, {"", 0x48}, {"", 0x49}, {"", 0x4A}, {"", 0x4B}, {"", 0x4C}, {"", 0x4D}, {"", 0x4E}, {"", 0x4F},
{"", 0x50}, {"", 0x51}, {"", 0x52}, {"", 0x53}, {"", 0x54}, {"", 0x55}, {"", 0x56}, {"", 0x57}, {"", 0x58},
{"", 0x59}, {"", 0x5A}, {"", 0x5B}, {"", 0x5C}, {"", 0x5D}, {"", 0x5E}, {"", 0x5F}, {"", 0x60}, {"", 0x61},
{"", 0x62}, {"", 0x63}, {"", 0x64}, {"", 0x65}, {"", 0x66}, {"", 0x67}, {"", 0x68}, {"", 0x69}, {"", 0x6A},
{"", 0x6B}, {"", 0x6C}, {"", 0x6D}, {"", 0x6E}, {",", 0x6F}, {"", 0x6F}, {"", 0x70}, {"", 0x71}, {"", 0x72},
{"", 0x73}, {"", 0x74}, {"", 0x75}, {"", 0x76}, {"", 0x77}, {"", 0x78}, {"", 0x79}, {"", 0x7A}, {"", 0x7B},
{"", 0x7C}, {"", 0x7D}, {"", 0x7E}, {"", 0x7F}, {"", 0x80}, {"", 0x81}, {"", 0x82}, {"", 0x83}, {"", 0x84},
{"", 0x85}, {"", 0x86}, {"", 0x87}, {"", 0x88}, {"", 0x89}, {"", 0x8A}, {"", 0x8B}, {"", 0x8C}, {"", 0x8D},
{"", 0x8E}, {"", 0x8F}, {"", 0x90}, {"", 0x91}, {"", 0x92}, {"", 0x93}, {"", 0x94}, {"", 0x95}, {"", 0x96},
{"", 0x97}, {"", 0x98}, {"", 0x99}, {"", 0x9A}, {"", 0x9B}, {"", 0x9C}, {"", 0x9D}, {" ", 0x9E}, {" ", 0x9E},
{"-", 0x9F}, {"", 0x9F}, {"", 0xA0}, {"", 0xA1}, {"", 0xA2}, {"", 0xA3}, {"", 0xA4}, {"", 0xA5}, {"", 0xA6},
{"", 0xA7}, {"", 0xA8}, {"", 0xD0}, {"", 0xD1}, {"", 0xD2}, {"", 0xD3}, {"", 0xD4}, {"", 0xD5}, {"", 0xD6},
{"", 0xD7}, {"", 0xD8}, {"[%]", 0xE0}, {"(", 0xE1}, {"", 0xE1}, {")(", 0xE2}, {"", 0xE2}, {")", 0xE3}, {"", 0xE3},
{"+", 0xE4}, {"", 0xE4}, {"&", 0xE5}, {":", 0xE6}, {"", 0xF0}, {"", 0xF1}, {"!", 0xF2}, {"", 0xF2}, {"%", 0xF3},
{"", 0xF3}, {"?", 0xF4}, {"", 0xF4}, {"", 0xF5}, {"", 0xF6}, {"~", 0xF7}, {"", 0xF7}, {"", 0xF8}, {"$", 0xF9},
{"", 0xFA}, {"×", 0xFB}, {"", 0xFC}, {"", 0xFD}, {"\n", 0xFE}, {"", 0x45}, {"", 0x46}, {"", 0x47}, {"", 0x48},
{"", 0x49}, {"", 0x4A}, {"", 0x4B}, {"", 0x4C}, {"", 0x4D}, {"", 0x4E}, {"", 0x4F}, {"", 0x50}, {"", 0x51},
{"", 0x52}, {"", 0x53}, {"", 0x59}, {"", 0x5A}, {"", 0x5B}, {"", 0x5C}, {"", 0x5D}, {"", 0x75}, {"", 0x76},
{"", 0x77}, {"", 0x78}, {"", 0x79}, {"", 0x7A}, {"", 0x7B}, {"", 0x7C}, {"", 0x7D}, {"", 0x7E}, {"", 0x7F},
{"", 0x80}, {"", 0x81}, {"", 0x82}, {"", 0x83}, {"", 0x89}, {"", 0x8A}, {"", 0x8B}, {"", 0x8C}, {"", 0x8D},
{"", 0x59}, {"", 0x5A}, {"", 0x5B}, {"", 0x5C}, {"", 0x5D}, {"", 0x89}, {"", 0x8A}, {"", 0x8B}, {"", 0x8C},
{"", 0x8D}, {"^", 0x50}, {"|", 0x51}, {"<", 0x52}, {">", 0x53}, {"[A]", 0x54}, {"[B]", 0x55}, {"[C]", 0x56}, {"[Z]", 0x57},
{"[R]", 0x58}, {"/", 0xD0}, {"the", 0xD1}, {"you", 0xD2}, {"à", 0x60}, {"â", 0x61}, {"ä", 0x62}, {"À", 0x64}, {"Â", 0x65},
{"Ä", 0x66}, {"è", 0x70}, {"ê", 0x71}, {"ë", 0x72}, {"é", 0x73}, {"È", 0x74}, {"Ê", 0x75}, {"Ë", 0x76}, {"É", 0x77},
{"ù", 0x80}, {"û", 0x81}, {"ü", 0x82}, {"Ù", 0x84}, {"Û", 0x85}, {"Ü", 0x86}, {"ô", 0x91}, {"ö", 0x92}, {"Ô", 0x95},
{"Ö", 0x96}, {"î", 0xA1}, {"ï", 0xA2}, {"ß", 0xEC}, {"Ç", 0xED}, {"ç", 0xEE}, {"", 0xF0}
};
struct Character getCharacter(char* ch){
struct Character tmp = {NULL, 0};
for(s32 cmid = 0; cmid < 340; cmid++){
if(strcmp(charmap[cmid].txt, ch) == 0) {
tmp = charmap[cmid];
if(strncmp(charmap[cmid].txt, ch, strlen(charmap[cmid].txt)) == 0) {
tmp = charmap[cmid];
break;
}
}
@ -66,11 +66,11 @@ u8 * getTranslatedText(char * txt){
for(cid = 0; cid < strSize; cid++){
char ch = txt[cid];
if(ch == '['){
if(ch == '['){
tmpIcon[0] = ch;
tmpIcon[1] = txt[cid + 1];
tmpIcon[2] = txt[cid + 2];
struct Character ctm = getCharacter(tmpIcon);
struct Character ctm = getCharacter(tmpIcon);
if(ctm.value != NULL){
tmp[cid - shiftArr] = ctm.value;
shiftArr += 2;
@ -78,7 +78,7 @@ u8 * getTranslatedText(char * txt){
memset(tmpIcon, 0, sizeof(tmpIcon));
}
}else{
char findTxt[1] = {ch};
char findTxt[1] = {ch};
struct Character ctm = getCharacter(findTxt);
if(ctm.value != NULL){
tmp[cid - shiftArr] = ctm.value;
@ -90,6 +90,6 @@ u8 * getTranslatedText(char * txt){
tmp = realloc(tmp, (strSize - shiftArr + 1) * sizeof(u8));
tmp[strSize - shiftArr] = 0xFF;
return tmp;
}

View File

@ -4,7 +4,7 @@
#include "types.h"
struct Character{
char * txt;
char txt[3];
s32 value;
};