game: prepare for use additional sources for level messages

This commit is contained in:
Denis Pauk 2024-09-14 21:39:25 +03:00
parent 3dd4dbf2ce
commit 466ca3cba4
2 changed files with 58 additions and 40 deletions

View file

@ -27,8 +27,15 @@
#include "header/local.h" #include "header/local.h"
localmessages_t *localmessages = NULL; typedef struct
int nlocalmessages = 0; {
char *key;
char *value;
char *sound;
} localmessages_t;
static localmessages_t *localmessages = NULL;
static int nlocalmessages = 0;
static int static int
LocalizationSort(const void *p1, const void *p2) LocalizationSort(const void *p1, const void *p2)
@ -44,7 +51,8 @@ void
LocalizationInit(void) LocalizationInit(void)
{ {
byte *raw = NULL; byte *raw = NULL;
int len; char *buf_loc = NULL;
int len, curr_pos;
localmessages = NULL; localmessages = NULL;
nlocalmessages = 0; nlocalmessages = 0;
@ -53,15 +61,19 @@ LocalizationInit(void)
len = gi.FS_LoadFile("localization/loc_english.txt", (void **)&raw); len = gi.FS_LoadFile("localization/loc_english.txt", (void **)&raw);
if (len > 1) if (len > 1)
{ {
char *buf, *curr; buf_loc = malloc(len + 1);
int i; memcpy(buf_loc, raw, len);
buf_loc[len] = 0;
gi.FS_FreeFile(raw);
}
buf = malloc(len + 1); /* localization lines count */
memcpy(buf, raw, len); if (buf_loc)
buf[len] = 0; {
char *curr;
/* get lines count */ /* get lines count */
curr = buf; curr = buf_loc;
while(*curr) while(*curr)
{ {
size_t linesize = 0; size_t linesize = 0;
@ -73,28 +85,35 @@ LocalizationInit(void)
nlocalmessages ++; nlocalmessages ++;
} }
curr += linesize; curr += linesize;
if (curr >= (buf + len)) if (curr >= (buf_loc + len))
{ {
break; break;
} }
/* skip our endline */ /* skip our endline */
curr++; curr++;
} }
}
if (nlocalmessages) if (nlocalmessages)
{ {
localmessages = gi.TagMalloc(nlocalmessages * sizeof(*localmessages), TAG_GAME); localmessages = gi.TagMalloc(nlocalmessages * sizeof(*localmessages), TAG_GAME);
memset(localmessages, 0, nlocalmessages * sizeof(*localmessages)); memset(localmessages, 0, nlocalmessages * sizeof(*localmessages));
} }
curr_pos = 0;
/* localization load */
if (buf_loc)
{
char *curr;
/* parse lines */ /* parse lines */
curr = buf; curr = buf_loc;
i = 0;
while(*curr) while(*curr)
{ {
size_t linesize = 0; size_t linesize = 0;
if (i == nlocalmessages) if (curr_pos == nlocalmessages)
{ {
break; break;
} }
@ -167,30 +186,37 @@ LocalizationInit(void)
*currend = 0; *currend = 0;
} }
localmessages[i].key = gi.TagMalloc(strlen(curr) + 2, TAG_GAME); localmessages[curr_pos].key = gi.TagMalloc(strlen(curr) + 2, TAG_GAME);
localmessages[i].key[0] = '$'; localmessages[curr_pos].key[0] = '$';
strcpy(localmessages[i].key + 1, curr); strcpy(localmessages[curr_pos].key + 1, curr);
localmessages[i].value = gi.TagMalloc(strlen(sign) + 1, TAG_GAME); localmessages[curr_pos].value = gi.TagMalloc(strlen(sign) + 1, TAG_GAME);
strcpy(localmessages[i].value, sign); strcpy(localmessages[curr_pos].value, sign);
i ++; /* ReRelease does not have merged sound files to message */
localmessages[curr_pos].sound = NULL;
curr_pos ++;
} }
} }
curr += linesize; curr += linesize;
if (curr >= (buf + len)) if (curr >= (buf_loc + len))
{ {
break; break;
} }
/* skip our endline */ /* skip our endline */
curr++; curr++;
} }
free(buf_loc);
nlocalmessages = i;
/* sort messages */
qsort(localmessages, nlocalmessages, sizeof(localmessages_t), LocalizationSort);
gi.FS_FreeFile(raw);
free(buf);
} }
/* save last used position */
nlocalmessages = curr_pos;
if (!curr_pos)
{
return;
}
/* sort messages */
qsort(localmessages, nlocalmessages, sizeof(localmessages_t), LocalizationSort);
} }
static int static int

View file

@ -995,14 +995,6 @@ void SaveClientData(void);
void EndDMLevel(void); void EndDMLevel(void);
/* g_translate.c */ /* g_translate.c */
typedef struct
{
char *key;
char *value;
} localmessages_t;
extern localmessages_t *localmessages;
extern int nlocalmessages;
void LocalizationInit(void); void LocalizationInit(void);
const char* LocalizationMessage(const char *message); const char* LocalizationMessage(const char *message);