ActorCommands -> ActorUtil::ParseActorCommands

This commit is contained in:
Glenn Maynard
2006-09-21 02:36:40 +00:00
parent d4cbd09737
commit 355fdd7264
2 changed files with 99 additions and 103 deletions
-102
View File
@@ -7,108 +7,6 @@
#include <sstream>
#include "LuaManager.h"
apActorCommands ActorCommands( const RString &sCommands )
{
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 == "horizalign" ||
sName == "vertalign" ||
sName == "effectclock" ||
sName == "blend" ||
sName == "ztestmode" ||
sName == "cullmode" ||
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();
}
Lua *L = LUA->Get();
LuaReference *pRet = new LuaReference;
RString sError;
if( !LuaHelpers::RunScript( L, sLuaFunction, "in", sError, 1 ) )
{
FAIL_M( ssprintf("Compiling \"%s\": %s", sLuaFunction.c_str(), sError.c_str()) );
}
/* The function is now on the stack. */
pRet->SetFromStack( L );
LUA->Release( L );
ASSERT_M( !pRet->IsNil(), sLuaFunction.c_str() );
pRet->SetName( sLuaFunction );
return apActorCommands( pRet );
}
/*
* (c) 2004 Chris Danford
* All rights reserved.
+99 -1
View File
@@ -12,6 +12,8 @@
#include "XmlFileUtil.h"
#include "LuaManager.h"
#include "Foreach.h"
#include "Command.h"
#include <sstream>
#include "arch/Dialog/Dialog.h"
@@ -396,7 +398,103 @@ Actor* ActorUtil::MakeActor( const RString &sPath_, const XNode *pParent, Actor
apActorCommands ActorUtil::ParseActorCommands( const RString &sCommands )
{
return ActorCommands( sCommands );
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 == "horizalign" ||
sName == "vertalign" ||
sName == "effectclock" ||
sName == "blend" ||
sName == "ztestmode" ||
sName == "cullmode" ||
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();
}
Lua *L = LUA->Get();
LuaReference *pRet = new LuaReference;
RString sError;
if( !LuaHelpers::RunScript( L, sLuaFunction, "in", sError, 1 ) )
{
FAIL_M( ssprintf("Compiling \"%s\": %s", sLuaFunction.c_str(), sError.c_str()) );
}
/* The function is now on the stack. */
pRet->SetFromStack( L );
LUA->Release( L );
ASSERT_M( !pRet->IsNil(), sLuaFunction.c_str() );
pRet->SetName( sLuaFunction );
return apActorCommands( pRet );
}
void ActorUtil::SetXY( Actor& actor, const RString &sType )