Added notes for localization.

Some polishing up of tool code.
This commit is contained in:
Mari the Deer 2021-05-14 23:12:09 +02:00
commit e0b57bdddf
13 changed files with 117 additions and 67 deletions

View file

@ -1,10 +1,9 @@
/*
mklang.c : Generate LANGUAGE files for lore library entries.
This code is a mess lol.
(C)2020 Marisa Kirisame, UnSX Team.
Released under the MIT license:
This code is a mess and it's full of hacks, but that's to be expected
when it's all really just for personal use.
Copyright (c) 2020 Marisa Kirisame
Copyright (c) 2020-2021 Marisa Kirisame, UnSX Team
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
@ -25,20 +24,20 @@
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#define _POSIX_C_SOURCE 200809L
#define _XOPEN_SOURCE 700
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <limits.h>
char *lang = 0;
FILE *lf = 0;
char *txt = 0;
long txtlen = 0;
char entname[256];
void processentry()
void processentry( char *entname )
{
int gottag = 0;
long txtpos = 0;
@ -130,12 +129,12 @@ processtxt:
goto gettag;
}
void readentry( const char *path )
void readentry( char *mpath, char *entpath, char *entname )
{
FILE *f = fopen(path,"rb");
FILE *f = fopen(mpath,"rb");
if ( !f )
{
fprintf(stderr," \033[31mcannot open file \033[1m'%s'\033[22m: \033[1m%s\033[0m\n",path,strerror(errno));
fprintf(stderr," \033[31mcannot open file \033[1m'%s'\033[22m: \033[1m%s\033[0m\n",entpath,strerror(errno));
return;
}
fseek(f,0,SEEK_END);
@ -146,10 +145,7 @@ void readentry( const char *path )
memset(txt,0,txtlen+1);
fread(txt,1,txtlen,f);
fclose(f);
// copy over path, set to basename and strip extension
char *slash = strrchr(path,'/');
if ( slash ) strncpy(entname,slash+1,256);
else strncpy(entname,path,256);
// strip extension from entname and set to allcaps
char *ext = strchr(entname,'.');
if ( ext ) *ext = '\0';
printf(" \033[1;32m%s\033[22m:\033[0m",entname);
@ -161,7 +157,7 @@ void readentry( const char *path )
*c -= 0x20;
c++;
}
processentry();
processentry(entname);
free(txt);
txt = 0;
}
@ -177,28 +173,32 @@ int invalphasort( const struct dirent **a, const struct dirent **b )
return strcoll((*b)->d_name,(*a)->d_name);
}
void loopdir( const char *dname )
void loopdir( char *mpath, char *appendto, int ofs, int lim )
{
char fpath[256];
int nbase = snprintf(fpath,256,"%s/",dname);
struct dirent **e;
int ne = scandir(dname,&e,txtonly,invalphasort);
int ne = scandir(mpath,&e,txtonly,invalphasort);
if ( ne == -1 )
{
fprintf(stderr,"\033[31mcannot scan directory \033[1m'%s'\033[22m: \033[1m%s\033[0m\n",dname,strerror(errno));
fprintf(stderr,"\033[31mcannot scan directory \033[1m'%s'\033[22m: \033[1m%s\033[0m\n",appendto,strerror(errno));
return;
}
while ( ne-- )
{
snprintf(fpath+nbase,256-nbase,"%s",e[ne]->d_name);
readentry(fpath);
if ( snprintf(appendto+ofs,lim-ofs,"/%s",e[ne]->d_name) >= lim-ofs )
{
fprintf(stderr,"\033[31mpath length exceeded (over %u bytes).\033[0m\n",PATH_MAX);
free(e[ne]);
continue;
}
readentry(mpath,appendto,appendto+ofs+1);
free(e[ne]);
}
free(e);
}
int main( void )
int main( int argc, char **argv )
{
(void)argc; // shush, compiler
const char langs[][16] =
{
"default",
@ -212,31 +212,39 @@ int main( void )
};
const char langfiles[][32] =
{
"../language.def_lore",
"../language.es_lore",
/* "../language.jp_lore",
"../language.ru_lore",
"../language.fr_lore",
"../language.it_lore",
"../language.de_lore",
"../language.pl_lore",*/
"language.def_lore",
"language.es_lore",
/* "language.jp_lore",
"language.ru_lore",
"language.fr_lore",
"language.it_lore",
"language.de_lore",
"language.pl_lore",*/
};
int nlangs = sizeof(langs)/16; // hacky, but works
char mpath[PATH_MAX+1];
if ( !realpath(argv[0],mpath) )
{
fprintf(stderr,"\033[31merror getting full path: %s\033[0m\n",strerror(errno));
return 1;
}
char* appendto = strstr(mpath,"/swwmgz_m/");
if ( !appendto )
{
fprintf(stderr,"\033[31mbase swwmgz_m path could not be detected.\033[0m\n");
return 1;
}
appendto += 10;
int lim = PATH_MAX-(appendto-mpath);
for ( int i=0; i<nlangs; i++ )
{
// [HACKAROUND] only generate if there's no ignore file,
// because some people don't do things the right way
char ignpath[256];
snprintf(ignpath,256,"../lore/%s/.ignoreme",langs[i]);
FILE *ign = fopen(ignpath,"rb");
if ( ign )
if ( snprintf(appendto,lim,"%s",langfiles[i]) >= lim )
{
fclose(ign);
printf("\033[33mskipping language \033[1m'%s'\033[0m\n",langs[i]);
fprintf(stderr,"\033[31mpath length exceeded (over %u bytes).\033[0m\n",PATH_MAX);
continue;
}
printf("\033[33mprocessing language \033[1m'%s'\033[0m\n",langs[i]);
lf = fopen(langfiles[i],"wb");
lf = fopen(mpath,"wb");
if ( !lf )
{
fprintf(stderr,"\033[31mcannot open \033[1m'%s'\033[22m for writing: \033[1m%s\033[0m\n",langfiles[i],strerror(errno));
@ -244,9 +252,14 @@ int main( void )
}
fprintf(lf,"// this file was generated by mklang, do not edit directly\n");
fprintf(lf,"[%s]\n",langs[i]);
char langpath[256];
snprintf(langpath,256,"../lore/%s",langs[i]);
loopdir(langpath);
int nb = snprintf(appendto,lim,"lore/%s",langs[i]);
if ( nb >= lim )
{
fprintf(stderr,"\033[31mpath length exceeded (over %u bytes).\033[0m\n",PATH_MAX);
fclose(lf);
continue;
}
loopdir(mpath,appendto,nb,lim);
fclose(lf);
}
return 0;