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);
}
@ -96,7 +114,8 @@ void preloadTexts(){
dialogPool[id] = entry;
free(dialogTxt);
}
free(jsonTxt);
cJSON_Delete(json);
}
void alloc_dialog_pool(void){

View File

@ -11,7 +11,7 @@ struct Character charmap[340] = {
{"", 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},
{"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},
@ -44,7 +44,7 @@ struct Character charmap[340] = {
struct Character getCharacter(char* ch){
struct Character tmp = {NULL, 0};
for(s32 cmid = 0; cmid < 340; cmid++){
if(strcmp(charmap[cmid].txt, ch) == 0) {
if(strncmp(charmap[cmid].txt, ch, strlen(charmap[cmid].txt)) == 0) {
tmp = charmap[cmid];
break;
}

View File

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