From 6c1b9b8678dbb40abbccaf4c935b636a52ac9493 Mon Sep 17 00:00:00 2001
From: Lactozilla <jp6781615@gmail.com>
Date: Tue, 21 Nov 2023 16:46:32 -0300
Subject: [PATCH] Make translation field return the name of the translation

---
 src/deh_lua.c       | 10 ++++------
 src/lua_mobjlib.c   | 10 +++++++---
 src/r_translation.c | 34 ++++++++++++++++++++++++++++++++--
 src/r_translation.h | 12 ++----------
 4 files changed, 45 insertions(+), 21 deletions(-)

diff --git a/src/deh_lua.c b/src/deh_lua.c
index 9a60c494b6..bbcb3879d8 100644
--- a/src/deh_lua.c
+++ b/src/deh_lua.c
@@ -587,13 +587,11 @@ 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);
-				return 1;
-			}
+			lua_pushinteger(L, id);
+			return 1;
 		}
 		return luaL_error(L, "translation '%s' could not be found.\n", word);
 	}
diff --git a/src/lua_mobjlib.c b/src/lua_mobjlib.c
index 69050ec72b..ea99a2e1c6 100644
--- a/src/lua_mobjlib.c
+++ b/src/lua_mobjlib.c
@@ -343,9 +343,13 @@ static int mobj_get(lua_State *L)
 		break;
 	case mobj_translation:
 		if (mo->translation)
-			lua_pushinteger(L, mo->translation);
-		else
-			lua_pushnil(L);
+		{
+			const char *name = R_GetCustomTranslationName(mo->translation);
+			if (name)
+				lua_pushstring(L, name);
+			break;
+		}
+		lua_pushnil(L);
 		break;
 	case mobj_blendmode:
 		lua_pushinteger(L, mo->blendmode);
diff --git a/src/r_translation.c b/src/r_translation.c
index 24019ed009..7f320d498e 100644
--- a/src/r_translation.c
+++ b/src/r_translation.c
@@ -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;
diff --git a/src/r_translation.h b/src/r_translation.h
index c7a4562e37..24b0597efe 100644
--- a/src/r_translation.h
+++ b/src/r_translation.h
@@ -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);
-- 
GitLab