diff --git a/stepmania/src/ActorUtil.cpp b/stepmania/src/ActorUtil.cpp index eaca28ec82..75fe0a56e1 100644 --- a/stepmania/src/ActorUtil.cpp +++ b/stepmania/src/ActorUtil.cpp @@ -12,8 +12,6 @@ #include "XmlFileUtil.h" #include "LuaManager.h" #include "Foreach.h" -#include "Command.h" -#include #include "arch/Dialog/Dialog.h" @@ -396,92 +394,10 @@ Actor* ActorUtil::MakeActor( const RString &sPath_, const XNode *pParent, Actor } } -void ActorUtil::ParseActorCommands( Lua *L, const RString &sCommands, const RString &sName ) -{ - RString sLuaFunction; - if( sCommands.size() > 0 && sCommands[0] == '\033' ) - { - /* This is a compiled Lua chunk. Just pass it on directly. */ - sLuaFunction = sCommands; - } - else if( sCommands.size() > 0 && sCommands[0] == '%' ) - { - sLuaFunction = "return "; - sLuaFunction.append( sCommands.begin()+1, sCommands.end() ); - } - else - { - Commands cmds; - ParseCommands( sCommands, cmds ); - - // - // Convert cmds to a Lua function - // - ostringstream s; - - s << "return function(self,parent)\n"; - - FOREACH_CONST( Command, cmds.v, c ) - { - const Command& cmd = (*c); - RString sName = cmd.GetName(); - TrimLeft( sName ); - TrimRight( sName ); - s << "\tself:" << sName << "("; - - bool bFirstParamIsString = - sName == "effectclock" || - sName == "playcommand" || - sName == "queuecommand" || - sName == "queuemessage"; - - for( unsigned i=1; i "200" - if( sArg[0] == '+' ) - sArg.erase( sArg.begin() ); - - if( i==1 && bFirstParamIsString ) // string literal - { - sArg.Replace( "'", "\\'" ); // escape quote - s << "'" << sArg << "'"; - } - else if( sArg[0] == '#' ) // HTML color - { - RageColor c; // in case FromString fails - c.FromString( sArg ); - // c is still valid if FromString fails - s << c.r << "," << c.g << "," << c.b << "," << c.a; - } - else - { - s << sArg; - } - - if( i != cmd.m_vsArgs.size()-1 ) - s << ","; - } - s << ")\n"; - } - - s << "end\n"; - - sLuaFunction = s.str(); - } - - RString sError; - if( !LuaHelpers::RunScript( L, sLuaFunction, sName, sError, 1 ) ) - LOG->Warn( "Compiling \"%s\": %s", sLuaFunction.c_str(), sError.c_str() ); - - /* The function is now on the stack. */ -} - apActorCommands ActorUtil::ParseActorCommands( const RString &sCommands, const RString &sName ) { Lua *L = LUA->Get(); - ParseActorCommands( L, sCommands, sName ); + LuaHelpers::ParseCommandList( L, sCommands, sName ); LuaReference *pRet = new LuaReference; pRet->SetFromStack( L ); LUA->Release( L ); diff --git a/stepmania/src/ActorUtil.h b/stepmania/src/ActorUtil.h index 6e5cb2d7ee..0d2e4730a5 100644 --- a/stepmania/src/ActorUtil.h +++ b/stepmania/src/ActorUtil.h @@ -45,7 +45,6 @@ namespace ActorUtil void Register( const RString& sClassName, CreateActorFn pfn ); Actor* Create( const RString& sClassName, const RString& sDir, const XNode* pNode, Actor *pParentActor ); - void ParseActorCommands( Lua *L, const RString &sCommands, const RString &sName = "" ); apActorCommands ParseActorCommands( const RString &sCommands, const RString &sName = "" ); void SetXY( Actor& actor, const RString &sType ); void LoadCommand( Actor& actor, const RString &sType, const RString &sCommandName ); diff --git a/stepmania/src/LuaManager.cpp b/stepmania/src/LuaManager.cpp index 02a8f19496..8242ed5111 100644 --- a/stepmania/src/LuaManager.cpp +++ b/stepmania/src/LuaManager.cpp @@ -8,7 +8,9 @@ #include "Foreach.h" #include "arch/Dialog/Dialog.h" #include "XmlFile.h" +#include "Command.h" +#include #include #include @@ -572,6 +574,88 @@ bool LuaHelpers::RunAtExpressionS( RString &sStr ) return true; } +void LuaHelpers::ParseCommandList( Lua *L, const RString &sCommands, const RString &sName ) +{ + RString sLuaFunction; + if( sCommands.size() > 0 && sCommands[0] == '\033' ) + { + /* This is a compiled Lua chunk. Just pass it on directly. */ + sLuaFunction = sCommands; + } + else if( sCommands.size() > 0 && sCommands[0] == '%' ) + { + sLuaFunction = "return "; + sLuaFunction.append( sCommands.begin()+1, sCommands.end() ); + } + else + { + Commands cmds; + ParseCommands( sCommands, cmds ); + + // + // Convert cmds to a Lua function + // + ostringstream s; + + s << "return function(self,parent)\n"; + + FOREACH_CONST( Command, cmds.v, c ) + { + const Command& cmd = (*c); + RString sName = cmd.GetName(); + TrimLeft( sName ); + TrimRight( sName ); + s << "\tself:" << sName << "("; + + bool bFirstParamIsString = + sName == "effectclock" || + sName == "playcommand" || + sName == "queuecommand" || + sName == "queuemessage"; + + for( unsigned i=1; i "200" + if( sArg[0] == '+' ) + sArg.erase( sArg.begin() ); + + if( i==1 && bFirstParamIsString ) // string literal + { + sArg.Replace( "'", "\\'" ); // escape quote + s << "'" << sArg << "'"; + } + else if( sArg[0] == '#' ) // HTML color + { + RageColor c; // in case FromString fails + c.FromString( sArg ); + // c is still valid if FromString fails + s << c.r << "," << c.g << "," << c.b << "," << c.a; + } + else + { + s << sArg; + } + + if( i != cmd.m_vsArgs.size()-1 ) + s << ","; + } + s << ")\n"; + } + + s << "end\n"; + + sLuaFunction = s.str(); + } + + RString sError; + if( !LuaHelpers::RunScript( L, sLuaFunction, sName, sError, 1 ) ) + LOG->Warn( "Compiling \"%s\": %s", sLuaFunction.c_str(), sError.c_str() ); + + /* The function is now on the stack. */ +} + /* Like luaL_typerror, but without the special case for argument 1 being "self" * in method calls, so we give a correct error message after we remove self. */ int LuaHelpers::TypeError( Lua *L, int iArgNo, const char *szName ) diff --git a/stepmania/src/LuaManager.h b/stepmania/src/LuaManager.h index ad0211fea4..7725e82be2 100644 --- a/stepmania/src/LuaManager.h +++ b/stepmania/src/LuaManager.h @@ -76,6 +76,8 @@ namespace LuaHelpers /* If sStr begins with @, evaluate the rest as an expression and store the result over sStr. */ bool RunAtExpressionS( RString &sStr ); + void ParseCommandList( lua_State *L, const RString &sCommands, const RString &sName ); + /* Pops the last iArgs arguments from the stack, and return a function that returns * those values. */ void PushValueFunc( lua_State *L, int iArgs ); diff --git a/stepmania/src/ThemeManager.cpp b/stepmania/src/ThemeManager.cpp index e369167740..a4b2011794 100644 --- a/stepmania/src/ThemeManager.cpp +++ b/stepmania/src/ThemeManager.cpp @@ -954,7 +954,7 @@ void ThemeManager::PushMetric( Lua *L, const RString &sClassName, const RString #if !defined(SMPACKAGE) if( EndsWith(sValueName, "Command") ) { - ActorUtil::ParseActorCommands( L, sValue, sName ); + LuaHelpers::ParseCommandList( L, sValue, sName ); } else #endif