From da56e84d2c1f202789c87afc3aeab6c3b6eb65b1 Mon Sep 17 00:00:00 2001 From: Tatsuru <44866610+TatsuruIKR@users.noreply.github.com> Date: Wed, 21 Apr 2021 22:14:37 -0300 Subject: [PATCH 1/5] Change TOL_ERZ3 identifier --- src/g_game.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/g_game.c b/src/g_game.c index 13423ce77..bd09e4c79 100644 --- a/src/g_game.c +++ b/src/g_game.c @@ -3506,7 +3506,7 @@ tolinfo_t TYPEOFLEVEL[NUMTOLNAMES] = { {"2D",TOL_2D}, {"MARIO",TOL_MARIO}, {"NIGHTS",TOL_NIGHTS}, - {"OLDBRAK",TOL_ERZ3}, + {"ERZ3",TOL_ERZ3}, {"XMAS",TOL_XMAS}, {"CHRISTMAS",TOL_XMAS}, From 4f2f94d02d4f982d05900e10b5f00be28b0599ac Mon Sep 17 00:00:00 2001 From: Tatsuru <44866610+TatsuruIKR@users.noreply.github.com> Date: Wed, 21 Apr 2021 22:22:37 -0300 Subject: [PATCH 2/5] Compatibility with the current identifier --- src/g_game.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/g_game.c b/src/g_game.c index bd09e4c79..399c4f2bd 100644 --- a/src/g_game.c +++ b/src/g_game.c @@ -3506,6 +3506,7 @@ tolinfo_t TYPEOFLEVEL[NUMTOLNAMES] = { {"2D",TOL_2D}, {"MARIO",TOL_MARIO}, {"NIGHTS",TOL_NIGHTS}, + {"OLDBRAK",TOL_ERZ3}, {"ERZ3",TOL_ERZ3}, {"XMAS",TOL_XMAS}, From c51c478740d1bb22dbe0a28923c433038ec76cfc Mon Sep 17 00:00:00 2001 From: James R Date: Sun, 25 Apr 2021 21:51:24 -0700 Subject: [PATCH 3/5] Lua: ensure order of MIN, MAX possible values Cvars could now have a range (MIN, MAX) plus some preset values, but Lua could not take advantage of this due to table order not being guaranteed. --- src/lua_consolelib.c | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/lua_consolelib.c b/src/lua_consolelib.c index 5344fee76..e839d4e15 100644 --- a/src/lua_consolelib.c +++ b/src/lua_consolelib.c @@ -361,6 +361,9 @@ static int lib_cvRegisterVar(lua_State *L) size_t count = 0; CV_PossibleValue_t *cvpv; + const char * const MINMAX[2] = {"MIN", "MAX"}; + int minmax_unset = 3; + lua_pushnil(L); while (lua_next(L, 4)) { count++; @@ -377,16 +380,45 @@ static int lib_cvRegisterVar(lua_State *L) i = 0; lua_pushnil(L); while (lua_next(L, 4)) { + INT32 n; + const char * strval; + // stack: [...] PossibleValue table, index, value // 4 5 6 if (lua_type(L, 5) != LUA_TSTRING || lua_type(L, 6) != LUA_TNUMBER) FIELDERROR("PossibleValue", "custom PossibleValue table requires a format of string=integer, i.e. {MIN=0, MAX=9999}"); - cvpv[i].strvalue = Z_StrDup(lua_tostring(L, 5)); - cvpv[i].value = (INT32)lua_tonumber(L, 6); + + strval = lua_tostring(L, 5); + + if ( + stricmp(strval, MINMAX[n=0]) == 0 || + stricmp(strval, MINMAX[n=1]) == 0 + ){ + /* need to shift forward */ + if (minmax_unset == 3) + { + memmove(&cvpv[2], &cvpv[0], + i * sizeof *cvpv); + } + cvpv[n].strvalue = MINMAX[n]; + minmax_unset &= ~(1 << n); + } + else + { + n = i; + cvpv[n].strvalue = Z_StrDup(strval); + } + + cvpv[n].value = (INT32)lua_tonumber(L, 6); + i++; lua_pop(L, 1); } + + if (minmax_unset) + FIELDERROR("PossibleValue", "custom PossibleValue table requires requires both MIN and MAX keys if one is present"); + cvpv[i].value = 0; cvpv[i].strvalue = NULL; cvar->PossibleValue = cvpv; From 85914cc7cd9143b08353409b1dea2a0d536e31de Mon Sep 17 00:00:00 2001 From: James R Date: Sun, 25 Apr 2021 21:55:04 -0700 Subject: [PATCH 4/5] Free zstring when switching to preset value This is only applicable for bounded cvars (MIN, MAX), since otherwise there's no way to allocate a zstring. --- src/command.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/command.c b/src/command.c index f1bf7dfb5..00116a0cb 100644 --- a/src/command.c +++ b/src/command.c @@ -1441,6 +1441,10 @@ static void Setvalue(consvar_t *var, const char *valstr, boolean stealth) return; } + // free the old value string + Z_Free(var->zstring); + var->zstring = NULL; + var->value = var->PossibleValue[i].value; var->string = var->PossibleValue[i].strvalue; goto finish; From 92aeadc36b8e32e1c18b25eaac03e0eb9b86171b Mon Sep 17 00:00:00 2001 From: James R Date: Sun, 25 Apr 2021 22:01:40 -0700 Subject: [PATCH 5/5] It is impossible for a string to be allocated in this case --- src/command.c | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/command.c b/src/command.c index 00116a0cb..951e3dd09 100644 --- a/src/command.c +++ b/src/command.c @@ -1507,14 +1507,7 @@ static void Setvalue(consvar_t *var, const char *valstr, boolean stealth) found: if (client && execversion_enabled) { - if (var->revert.allocated) - { - Z_Free(var->revert.v.string); - var->revert.allocated = false; // the below value is not allocated in zone memory, don't try to free it! - } - var->revert.v.const_munge = var->PossibleValue[i].strvalue; - return; }