Make translation field return the name of the translation

This commit is contained in:
Lactozilla 2023-11-21 16:46:32 -03:00
parent 803fe613d2
commit 6c1b9b8678
4 changed files with 45 additions and 21 deletions

View file

@ -587,14 +587,12 @@ static int ScanConstants(lua_State *L, boolean mathlib, const char *word)
else if (mathlib && fastncmp("TRANSLATION_",word,12))
{
p = word+12;
for (i = 0; i < (signed)numcustomtranslations; i++)
int id = R_FindCustomTranslation_CaseInsensitive(p);
if (id != -1)
{
if (fasticmp(p, customtranslations[i].name))
{
lua_pushinteger(L, (int)customtranslations[i].id);
lua_pushinteger(L, id);
return 1;
}
}
return luaL_error(L, "translation '%s' could not be found.\n", word);
}

View file

@ -343,8 +343,12 @@ static int mobj_get(lua_State *L)
break;
case mobj_translation:
if (mo->translation)
lua_pushinteger(L, mo->translation);
else
{
const char *name = R_GetCustomTranslationName(mo->translation);
if (name)
lua_pushstring(L, name);
break;
}
lua_pushnil(L);
break;
case mobj_blendmode:

View file

@ -819,8 +819,15 @@ fail:
Z_Free(text);
}
customtranslation_t *customtranslations = NULL;
unsigned numcustomtranslations = 0;
typedef struct CustomTranslation
{
char *name;
unsigned id;
UINT32 hash;
} customtranslation_t;
static customtranslation_t *customtranslations = NULL;
static unsigned numcustomtranslations = 0;
int R_FindCustomTranslation(const char *name)
{
@ -835,6 +842,18 @@ int R_FindCustomTranslation(const char *name)
return -1;
}
// This is needed for SOC (which is case insensitive)
int R_FindCustomTranslation_CaseInsensitive(const char *name)
{
for (unsigned i = 0; i < numcustomtranslations; i++)
{
if (stricmp(name, customtranslations[i].name) == 0)
return (int)customtranslations[i].id;
}
return -1;
}
void R_AddCustomTranslation(const char *name, int trnum)
{
customtranslation_t *tr = NULL;
@ -862,6 +881,17 @@ void R_AddCustomTranslation(const char *name, int trnum)
tr->hash = quickncasehash(name, strlen(name));
}
const char *R_GetCustomTranslationName(unsigned id)
{
for (unsigned i = 0; i < numcustomtranslations; i++)
{
if (id == customtranslations[i].id)
return customtranslations[i].name;
}
return NULL;
}
unsigned R_NumCustomTranslations(void)
{
return numcustomtranslations;

View file

@ -82,18 +82,10 @@ struct PaletteRemapParseResult *PaletteRemap_ParseTranslation(const char *transl
void PaletteRemap_ApplyResult(remaptable_t *tr, struct PaletteRemapParseResult *data);
typedef struct CustomTranslation
{
char *name;
unsigned id;
UINT32 hash;
} customtranslation_t;
extern customtranslation_t *customtranslations;
extern unsigned numcustomtranslations;
int R_FindCustomTranslation(const char *name);
int R_FindCustomTranslation_CaseInsensitive(const char *name);
void R_AddCustomTranslation(const char *name, int trnum);
const char *R_GetCustomTranslationName(unsigned id);
unsigned R_NumCustomTranslations(void);
remaptable_t *R_GetTranslationByID(int id);
boolean R_TranslationIsValid(int id);