From c894e79b68bb4ca678699cc28ea0cc4f2808c315 Mon Sep 17 00:00:00 2001 From: Chris Danford Date: Sun, 17 Aug 2008 06:46:20 +0000 Subject: [PATCH] add Enum:ToString --- stepmania/src/EnumHelper.cpp | 26 +++++++++++++---- stepmania/src/EnumHelper.h | 39 +++++++++++++++++--------- stepmania/src/ScreenNetSelectMusic.cpp | 2 ++ 3 files changed, 48 insertions(+), 19 deletions(-) diff --git a/stepmania/src/EnumHelper.cpp b/stepmania/src/EnumHelper.cpp index fa2111912f..592d047ab7 100644 --- a/stepmania/src/EnumHelper.cpp +++ b/stepmania/src/EnumHelper.cpp @@ -99,11 +99,24 @@ namespace return 1; } + + int ToString( lua_State *L ) + { + luaL_checktype( L, 1, LUA_TTABLE ); + + /* Look up the tostring table. If there is no metafield, then we were + * called on the wrong type. */ + if( !luaL_getmetafield(L, 1, "tostring") ) + luaL_typerror( L, 1, "enum" ); + + return 1; + } } static const luaL_Reg EnumLib[] = { { "GetName", GetName }, { "Reverse", Reverse }, + { "ToString", ToString }, { NULL, NULL } }; @@ -113,14 +126,17 @@ static void PushEnumMethodTable( lua_State *L ) } /* Set up the enum table on the stack, and pop the table. */ -void Enum::SetMetatable( lua_State *L, LuaReference &EnumTable, LuaReference &EnumIndexTable, const char *szName ) +void Enum::SetMetatable( lua_State *L, LuaReference &IndexToEnumString, LuaReference &EnumStringToZeroIndex, LuaReference &EnumStringToString, const char *szName ) { - EnumTable.PushSelf( L ); + IndexToEnumString.PushSelf( L ); + lua_newtable( L ); { - lua_newtable( L ); - EnumIndexTable.PushSelf( L ); + EnumStringToZeroIndex.PushSelf( L ); lua_setfield( L, -2, "reverse" ); + EnumStringToString.PushSelf( L ); + lua_setfield( L, -2, "tostring" ); + lua_pushstring( L, szName ); lua_setfield( L, -2, "name" ); @@ -132,7 +148,7 @@ void Enum::SetMetatable( lua_State *L, LuaReference &EnumTable, LuaReference &En lua_setfield( L, -2, "__type" ); // for luaL_pushtype } lua_setmetatable( L, -2 ); - lua_pop( L, 2 ); + lua_pop( L, 1 ); } /* diff --git a/stepmania/src/EnumHelper.h b/stepmania/src/EnumHelper.h index 710da063eb..b3e93f13fd 100644 --- a/stepmania/src/EnumHelper.h +++ b/stepmania/src/EnumHelper.h @@ -18,20 +18,22 @@ int CheckEnum( lua_State *L, LuaReference &table, int iPos, int iInvalid, const template struct EnumTraits { - static LuaReference StringToEnum; - static LuaReference EnumToString; + static LuaReference IndexToEnumString; + static LuaReference EnumStringToZeroIndex; + static LuaReference EnumStringToString; static T Invalid; static const char *szName; }; -template LuaReference EnumTraits::StringToEnum; -template LuaReference EnumTraits::EnumToString; +template LuaReference EnumTraits::IndexToEnumString; +template LuaReference EnumTraits::EnumStringToZeroIndex; +template LuaReference EnumTraits::EnumStringToString; // same as XToString namespace Enum { template static T Check( lua_State *L, int iPos, bool bAllowInvalid = false ) { - return (T) CheckEnum( L, EnumTraits::StringToEnum, iPos, EnumTraits::Invalid, EnumTraits::szName, bAllowInvalid ); + return (T) CheckEnum( L, EnumTraits::EnumStringToZeroIndex, iPos, EnumTraits::Invalid, EnumTraits::szName, bAllowInvalid ); } template static void Push( lua_State *L, T iVal ) @@ -44,12 +46,12 @@ namespace Enum } /* Look up the string value. */ - EnumTraits::EnumToString.PushSelf( L ); + EnumTraits::IndexToEnumString.PushSelf( L ); lua_rawgeti( L, -1, iVal + 1 ); lua_remove( L, -2 ); } - void SetMetatable( lua_State *L, LuaReference &EnumTable, LuaReference &EnumIndexTable, const char *szName ); + void SetMetatable( lua_State *L, LuaReference &EnumTable, LuaReference &EnumIndexTable, LuaReference &EnumStringToString, const char *szName ); }; const RString &EnumToString( int iVal, int iMax, const char **szNameArray, auto_ptr *pNameCache ); // XToString helper @@ -94,7 +96,7 @@ const RString &EnumToString( int iVal, int iMax, const char **szNameArray, auto_ template struct EnumTraits; \ static void Lua##X(lua_State* L) \ { \ - /* Create the EnumToString table: { "UnlockEntry_ArcadePoints", "UnlockEntry_DancePoints" } */ \ + /* Create the IndexToEnumString table: { 1 = "UnlockEntry_ArcadePoints", 2 = "UnlockEntry_DancePoints" } */ \ lua_newtable( L ); \ FOREACH_ENUM( X, i ) \ { \ @@ -102,10 +104,10 @@ static void Lua##X(lua_State* L) \ lua_pushstring( L, (#X "_")+s ); \ lua_rawseti( L, -2, i+1 ); /* 1-based */ \ } \ - EnumTraits::EnumToString.SetFromStack( L ); \ - EnumTraits::EnumToString.PushSelf( L ); \ + EnumTraits::IndexToEnumString.SetFromStack( L ); \ + EnumTraits::IndexToEnumString.PushSelf( L ); \ lua_setglobal( L, #X ); \ - /* Create the StringToEnum table: { "UnlockEntry_ArcadePoints" = 0, "UnlockEntry_DancePoints" = 1 } */ \ + /* Create the EnumStringToZeroIndex table: { "UnlockEntry_ArcadePoints" = 0, "UnlockEntry_DancePoints" = 1 } */ \ lua_newtable( L ); \ FOREACH_ENUM( X, i ) \ { \ @@ -114,9 +116,18 @@ static void Lua##X(lua_State* L) \ lua_pushnumber( L, i ); /* 0-based */ \ lua_rawset( L, -3 ); \ } \ - EnumTraits::StringToEnum.SetFromStack( L ); \ - EnumTraits::StringToEnum.PushSelf( L ); \ - Enum::SetMetatable( L, EnumTraits::EnumToString, EnumTraits::StringToEnum, #X ); \ + EnumTraits::EnumStringToZeroIndex.SetFromStack( L ); \ + /* Create the EnumStringToString table: { "UnlockEntry_ArcadePoints" = "ArcadePoints", "UnlockEntry_DancePoints" = "DancePoints" } */ \ + lua_newtable( L ); \ + FOREACH_ENUM( X, i ) \ + { \ + RString s = X##ToString( i ); \ + lua_pushstring( L, (#X "_")+s ); \ + lua_pushstring( L, s ); \ + lua_rawset( L, -3 ); \ + } \ + EnumTraits::EnumStringToString.SetFromStack( L ); \ + Enum::SetMetatable( L, EnumTraits::IndexToEnumString, EnumTraits::EnumStringToZeroIndex, EnumTraits::EnumStringToString, #X ); \ } \ REGISTER_WITH_LUA_FUNCTION( Lua##X ); \ template<> X EnumTraits::Invalid = X##_Invalid; \ diff --git a/stepmania/src/ScreenNetSelectMusic.cpp b/stepmania/src/ScreenNetSelectMusic.cpp index 91ee7389d9..cc474ee007 100644 --- a/stepmania/src/ScreenNetSelectMusic.cpp +++ b/stepmania/src/ScreenNetSelectMusic.cpp @@ -498,7 +498,9 @@ void ScreenNetSelectMusic::UpdateDifficulties( PlayerNumber pn ) m_DifficultyIcon[pn].SetFromDifficulty( m_DC[pn] ); } else + { m_DifficultyIcon[pn].SetFromSteps( pn, NULL ); //It will blank it out + } StepsType st = GAMESTATE->GetCurrentStyle()->m_StepsType;