Skip to content
Snippets Groups Projects
Commit c51c4787 authored by James R.'s avatar James R.
Browse files

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.
parent d18fc888
No related branches found
No related tags found
No related merge requests found
...@@ -361,6 +361,9 @@ static int lib_cvRegisterVar(lua_State *L) ...@@ -361,6 +361,9 @@ static int lib_cvRegisterVar(lua_State *L)
size_t count = 0; size_t count = 0;
CV_PossibleValue_t *cvpv; CV_PossibleValue_t *cvpv;
const char * const MINMAX[2] = {"MIN", "MAX"};
int minmax_unset = 3;
lua_pushnil(L); lua_pushnil(L);
while (lua_next(L, 4)) { while (lua_next(L, 4)) {
count++; count++;
...@@ -377,16 +380,45 @@ static int lib_cvRegisterVar(lua_State *L) ...@@ -377,16 +380,45 @@ static int lib_cvRegisterVar(lua_State *L)
i = 0; i = 0;
lua_pushnil(L); lua_pushnil(L);
while (lua_next(L, 4)) { while (lua_next(L, 4)) {
INT32 n;
const char * strval;
// stack: [...] PossibleValue table, index, value // stack: [...] PossibleValue table, index, value
// 4 5 6 // 4 5 6
if (lua_type(L, 5) != LUA_TSTRING if (lua_type(L, 5) != LUA_TSTRING
|| lua_type(L, 6) != LUA_TNUMBER) || lua_type(L, 6) != LUA_TNUMBER)
FIELDERROR("PossibleValue", "custom PossibleValue table requires a format of string=integer, i.e. {MIN=0, MAX=9999}"); 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++; i++;
lua_pop(L, 1); 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].value = 0;
cvpv[i].strvalue = NULL; cvpv[i].strvalue = NULL;
cvar->PossibleValue = cvpv; cvar->PossibleValue = cvpv;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment