From 5051d85288da534947f83055a8a1e318e0493d8e Mon Sep 17 00:00:00 2001 From: "Devin J. Pohly" Date: Sun, 23 Feb 2014 15:01:34 -0500 Subject: [PATCH 1/4] Add lowercase strings to Lua enum reverse table Step one toward compatibility with case-insensitive legacy command syntax. --- src/EnumHelper.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/EnumHelper.h b/src/EnumHelper.h index dc1049cc0c..d3bf4267f3 100644 --- a/src/EnumHelper.h +++ b/src/EnumHelper.h @@ -135,6 +135,11 @@ static void Lua##X(lua_State* L) \ lua_pushstring( L, (#X "_")+s ); \ lua_pushnumber( L, i ); /* 0-based */ \ lua_rawset( L, -3 ); \ + /* Compatibility with old, case-insensitive values */ \ + s.MakeLower(); \ + lua_pushstring( L, s ); \ + lua_pushnumber( L, i ); /* 0-based */ \ + lua_rawset( L, -3 ); \ } \ EnumTraits::StringToEnum.SetFromStack( L ); \ EnumTraits::StringToEnum.PushSelf( L ); \ From 2907fa06a752319edfc8c3848d4c92671e96d4b4 Mon Sep 17 00:00:00 2001 From: "Devin J. Pohly" Date: Sun, 23 Feb 2014 15:28:20 -0500 Subject: [PATCH 2/4] Try to resolve enums old-style if new-style fails This makes e.g. cmd(blend,add) work like cmd(blend,'BlendMode_Add'). Resolution in this case is case-insensitive like it used to be. --- src/EnumHelper.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/EnumHelper.cpp b/src/EnumHelper.cpp index dec4665019..a7e7ec5b27 100644 --- a/src/EnumHelper.cpp +++ b/src/EnumHelper.cpp @@ -22,6 +22,24 @@ int CheckEnum( lua_State *L, LuaReference &table, int iPos, int iInvalid, const lua_pushvalue( L, iPos ); lua_gettable( L, -2 ); + // If not found, check case-insensitively for legacy compatibility + if( lua_isnil(L, -1) && lua_isstring(L, iPos) ) { + RString sLower; + + // Get rid of nil value on stack + lua_pop( L, 1 ); + + // Get the string and lowercase it + lua_pushvalue( L, iPos ); + LuaHelpers::Pop( L, sLower ); + sLower.MakeLower(); + + // Try again to read the value + table.PushSelf( L ); + LuaHelpers::Push( L, sLower ); + lua_gettable( L, -2 ); + } + // If the result is nil, then a string was passed that is not a member of this enum. Throw // an error. To specify the invalid value, pass nil. That way, typos will throw an error, // and not silently result in nil, or an out-of-bounds value. From 10d872db3f2dda4cb3037ad83b34c0000cacaeb2 Mon Sep 17 00:00:00 2001 From: "Devin J. Pohly" Date: Sun, 23 Feb 2014 16:39:10 -0500 Subject: [PATCH 3/4] Accept old-style raw values for enum parameters These will still be checked to ensure they are valid members of the enumeration, so it's not as ugly or dangerous as it used to be. --- src/EnumHelper.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/EnumHelper.h b/src/EnumHelper.h index d3bf4267f3..72d57381fc 100644 --- a/src/EnumHelper.h +++ b/src/EnumHelper.h @@ -140,6 +140,9 @@ static void Lua##X(lua_State* L) \ lua_pushstring( L, s ); \ lua_pushnumber( L, i ); /* 0-based */ \ lua_rawset( L, -3 ); \ + /* Compatibility with old, raw values */ \ + lua_pushnumber( L, i ); \ + lua_rawseti( L, -2, i ); \ } \ EnumTraits::StringToEnum.SetFromStack( L ); \ EnumTraits::StringToEnum.PushSelf( L ); \ From bb1b6a81c6c1fa68b214a3c6ab6b70575479e66d Mon Sep 17 00:00:00 2001 From: "Devin J. Pohly" Date: Sun, 23 Feb 2014 16:41:19 -0500 Subject: [PATCH 4/4] Special case player number 1/2 in ApplyGameCommand This function, oddly, used to take a 1 or 2 for the player number where other nearby functions (e.g. IsPlayerEnabled) would take the proper enum value, 0 or 1. If the old-style syntax (raw number) is used, also use the old-style interpretation. --- src/GameState.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/GameState.cpp b/src/GameState.cpp index 90e53de748..20d9fad9e9 100644 --- a/src/GameState.cpp +++ b/src/GameState.cpp @@ -2223,8 +2223,17 @@ public: static int ApplyGameCommand( T* p, lua_State *L ) { PlayerNumber pn = PLAYER_INVALID; - if( lua_gettop(L) >= 2 && !lua_isnil(L,2) ) + if( lua_gettop(L) >= 2 && !lua_isnil(L,2) ) { + // Legacy behavior: if an old-style numerical argument + // is given, decrement it before trying to parse + if( lua_isnumber(L,2) ) { + int arg = (int) lua_tonumber( L, 2 ); + arg--; + LuaHelpers::Push( L, arg ); + lua_replace( L, -2 ); + } pn = Enum::Check(L, 2); + } p->ApplyGameCommand(SArg(1),pn); return 0; }