move command parsing into LuaHelpers. It's not Actor-specific other
than a few compatibility hacks.
This commit is contained in:
@@ -12,8 +12,6 @@
|
|||||||
#include "XmlFileUtil.h"
|
#include "XmlFileUtil.h"
|
||||||
#include "LuaManager.h"
|
#include "LuaManager.h"
|
||||||
#include "Foreach.h"
|
#include "Foreach.h"
|
||||||
#include "Command.h"
|
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
#include "arch/Dialog/Dialog.h"
|
#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<cmd.m_vsArgs.size(); i++ )
|
|
||||||
{
|
|
||||||
RString sArg = cmd.m_vsArgs[i];
|
|
||||||
|
|
||||||
// "+200" -> "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 )
|
apActorCommands ActorUtil::ParseActorCommands( const RString &sCommands, const RString &sName )
|
||||||
{
|
{
|
||||||
Lua *L = LUA->Get();
|
Lua *L = LUA->Get();
|
||||||
ParseActorCommands( L, sCommands, sName );
|
LuaHelpers::ParseCommandList( L, sCommands, sName );
|
||||||
LuaReference *pRet = new LuaReference;
|
LuaReference *pRet = new LuaReference;
|
||||||
pRet->SetFromStack( L );
|
pRet->SetFromStack( L );
|
||||||
LUA->Release( L );
|
LUA->Release( L );
|
||||||
|
|||||||
@@ -45,7 +45,6 @@ namespace ActorUtil
|
|||||||
void Register( const RString& sClassName, CreateActorFn pfn );
|
void Register( const RString& sClassName, CreateActorFn pfn );
|
||||||
Actor* Create( const RString& sClassName, const RString& sDir, const XNode* pNode, Actor *pParentActor );
|
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 = "" );
|
apActorCommands ParseActorCommands( const RString &sCommands, const RString &sName = "" );
|
||||||
void SetXY( Actor& actor, const RString &sType );
|
void SetXY( Actor& actor, const RString &sType );
|
||||||
void LoadCommand( Actor& actor, const RString &sType, const RString &sCommandName );
|
void LoadCommand( Actor& actor, const RString &sType, const RString &sCommandName );
|
||||||
|
|||||||
@@ -8,7 +8,9 @@
|
|||||||
#include "Foreach.h"
|
#include "Foreach.h"
|
||||||
#include "arch/Dialog/Dialog.h"
|
#include "arch/Dialog/Dialog.h"
|
||||||
#include "XmlFile.h"
|
#include "XmlFile.h"
|
||||||
|
#include "Command.h"
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
#include <csetjmp>
|
#include <csetjmp>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
@@ -572,6 +574,88 @@ bool LuaHelpers::RunAtExpressionS( RString &sStr )
|
|||||||
return true;
|
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<cmd.m_vsArgs.size(); i++ )
|
||||||
|
{
|
||||||
|
RString sArg = cmd.m_vsArgs[i];
|
||||||
|
|
||||||
|
// "+200" -> "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"
|
/* 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. */
|
* in method calls, so we give a correct error message after we remove self. */
|
||||||
int LuaHelpers::TypeError( Lua *L, int iArgNo, const char *szName )
|
int LuaHelpers::TypeError( Lua *L, int iArgNo, const char *szName )
|
||||||
|
|||||||
@@ -76,6 +76,8 @@ namespace LuaHelpers
|
|||||||
/* If sStr begins with @, evaluate the rest as an expression and store the result over sStr. */
|
/* If sStr begins with @, evaluate the rest as an expression and store the result over sStr. */
|
||||||
bool RunAtExpressionS( RString &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
|
/* Pops the last iArgs arguments from the stack, and return a function that returns
|
||||||
* those values. */
|
* those values. */
|
||||||
void PushValueFunc( lua_State *L, int iArgs );
|
void PushValueFunc( lua_State *L, int iArgs );
|
||||||
|
|||||||
@@ -954,7 +954,7 @@ void ThemeManager::PushMetric( Lua *L, const RString &sClassName, const RString
|
|||||||
#if !defined(SMPACKAGE)
|
#if !defined(SMPACKAGE)
|
||||||
if( EndsWith(sValueName, "Command") )
|
if( EndsWith(sValueName, "Command") )
|
||||||
{
|
{
|
||||||
ActorUtil::ParseActorCommands( L, sValue, sName );
|
LuaHelpers::ParseCommandList( L, sValue, sName );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user