2004-06-08 00:47:53 +00:00
|
|
|
#include "global.h"
|
2003-07-07 01:29:18 +00:00
|
|
|
#include "ActorUtil.h"
|
2003-10-30 03:51:53 +00:00
|
|
|
#include "ThemeManager.h"
|
2005-08-26 19:05:10 +00:00
|
|
|
#include "RageFileManager.h"
|
2003-10-30 21:57:38 +00:00
|
|
|
#include "RageLog.h"
|
2005-10-07 01:45:20 +00:00
|
|
|
#include "RageUtil.h"
|
2005-09-03 05:52:00 +00:00
|
|
|
#include "EnumHelper.h"
|
2005-01-07 22:01:57 +00:00
|
|
|
#include "XmlFile.h"
|
2005-12-16 04:16:09 +00:00
|
|
|
#include "XmlFileUtil.h"
|
2005-01-24 02:26:55 +00:00
|
|
|
#include "LuaManager.h"
|
2005-03-13 20:44:26 +00:00
|
|
|
#include "Foreach.h"
|
2003-07-07 01:29:18 +00:00
|
|
|
|
2004-08-07 21:32:43 +00:00
|
|
|
#include "arch/Dialog/Dialog.h"
|
|
|
|
|
|
2003-07-07 01:29:18 +00:00
|
|
|
|
2005-02-09 05:31:14 +00:00
|
|
|
// Actor registration
|
2006-01-22 01:00:06 +00:00
|
|
|
static map<RString,CreateActorFn> *g_pmapRegistrees = NULL;
|
2005-02-09 05:31:14 +00:00
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
static bool IsRegistered( const RString& sClassName )
|
2005-02-23 02:14:06 +00:00
|
|
|
{
|
|
|
|
|
return g_pmapRegistrees->find( sClassName ) != g_pmapRegistrees->end();
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
void ActorUtil::Register( const RString& sClassName, CreateActorFn pfn )
|
2005-02-09 05:31:14 +00:00
|
|
|
{
|
|
|
|
|
if( g_pmapRegistrees == NULL )
|
2006-01-22 01:00:06 +00:00
|
|
|
g_pmapRegistrees = new map<RString,CreateActorFn>;
|
2005-02-09 05:31:14 +00:00
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
map<RString,CreateActorFn>::iterator iter = g_pmapRegistrees->find( sClassName );
|
2005-02-09 05:31:14 +00:00
|
|
|
ASSERT_M( iter == g_pmapRegistrees->end(), ssprintf("Actor class '%s' already registered.", sClassName.c_str()) );
|
|
|
|
|
|
|
|
|
|
(*g_pmapRegistrees)[sClassName] = pfn;
|
|
|
|
|
}
|
|
|
|
|
|
2006-10-09 08:24:10 +00:00
|
|
|
Actor* ActorUtil::Create( const RString& sClassName, const XNode* pNode, Actor *pParentActor )
|
2005-02-09 05:31:14 +00:00
|
|
|
{
|
2006-01-22 01:00:06 +00:00
|
|
|
map<RString,CreateActorFn>::iterator iter = g_pmapRegistrees->find( sClassName );
|
2005-12-30 13:19:40 +00:00
|
|
|
ASSERT_M( iter != g_pmapRegistrees->end(), ssprintf("Actor '%s' is not registered.",sClassName.c_str()) );
|
2005-02-09 05:31:14 +00:00
|
|
|
|
2006-10-11 07:33:38 +00:00
|
|
|
const CreateActorFn &pfn = iter->second;
|
|
|
|
|
Actor *pRet = pfn();
|
|
|
|
|
|
|
|
|
|
if( pParentActor )
|
|
|
|
|
pRet->SetParent( pParentActor );
|
|
|
|
|
|
|
|
|
|
pRet->LoadFromNode( pNode );
|
|
|
|
|
return pRet;
|
2005-02-09 05:31:14 +00:00
|
|
|
}
|
|
|
|
|
|
2006-10-15 03:59:24 +00:00
|
|
|
bool ActorUtil::ResolvePath( RString &sPath, const RString &sName )
|
2005-02-26 10:07:02 +00:00
|
|
|
{
|
|
|
|
|
retry:
|
|
|
|
|
CollapsePath( sPath );
|
|
|
|
|
|
|
|
|
|
// If we know this is an exact match, don't bother with the GetDirListing,
|
|
|
|
|
// so "foo" doesn't partial match "foobar" if "foo" exists.
|
2005-08-26 19:05:10 +00:00
|
|
|
RageFileManager::FileType ft = FILEMAN->GetFileType( sPath );
|
|
|
|
|
if( ft != RageFileManager::TYPE_FILE && ft != RageFileManager::TYPE_DIR )
|
2005-02-26 10:07:02 +00:00
|
|
|
{
|
2006-01-22 01:00:06 +00:00
|
|
|
vector<RString> asPaths;
|
2005-02-26 10:07:02 +00:00
|
|
|
GetDirListing( sPath + "*", asPaths, false, true ); // return path too
|
|
|
|
|
|
|
|
|
|
if( asPaths.empty() )
|
|
|
|
|
{
|
2006-10-15 01:20:33 +00:00
|
|
|
RString sError = ssprintf( "%s: references a file \"%s\" which doesn't exist", sName.c_str(), sPath.c_str() );
|
2005-02-26 10:07:02 +00:00
|
|
|
switch( Dialog::AbortRetryIgnore( sError, "BROKEN_FILE_REFERENCE" ) )
|
|
|
|
|
{
|
|
|
|
|
case Dialog::abort:
|
2006-09-17 01:19:19 +00:00
|
|
|
RageException::Throw( "%s", sError.c_str() );
|
2005-02-26 10:07:02 +00:00
|
|
|
break;
|
|
|
|
|
case Dialog::retry:
|
|
|
|
|
FlushDirCache();
|
|
|
|
|
goto retry;
|
|
|
|
|
case Dialog::ignore:
|
2006-10-15 03:59:24 +00:00
|
|
|
return false;
|
2005-02-26 10:07:02 +00:00
|
|
|
default:
|
|
|
|
|
ASSERT(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-03-02 04:59:37 +00:00
|
|
|
|
|
|
|
|
THEME->FilterFileLanguages( asPaths );
|
|
|
|
|
|
|
|
|
|
if( asPaths.size() > 1 )
|
2005-02-26 10:07:02 +00:00
|
|
|
{
|
2006-10-15 01:20:33 +00:00
|
|
|
RString sError = ssprintf( "%s: references a file \"%s\" which has multiple matches", sName.c_str(), sPath.c_str() );
|
2006-07-27 22:56:43 +00:00
|
|
|
sError += "\n" + join( "\n", asPaths );
|
2005-02-26 10:07:02 +00:00
|
|
|
switch( Dialog::AbortRetryIgnore( sError, "BROKEN_FILE_REFERENCE" ) )
|
|
|
|
|
{
|
|
|
|
|
case Dialog::abort:
|
2006-09-17 01:19:19 +00:00
|
|
|
RageException::Throw( "%s", sError.c_str() );
|
2005-02-26 10:07:02 +00:00
|
|
|
break;
|
|
|
|
|
case Dialog::retry:
|
|
|
|
|
FlushDirCache();
|
|
|
|
|
goto retry;
|
|
|
|
|
case Dialog::ignore:
|
|
|
|
|
asPaths.erase( asPaths.begin()+1, asPaths.end() );
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
ASSERT(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sPath = asPaths[0];
|
|
|
|
|
}
|
|
|
|
|
|
2006-11-22 01:51:31 +00:00
|
|
|
if( ft == RageFileManager::TYPE_DIR )
|
|
|
|
|
{
|
|
|
|
|
RString sLuaPath = sPath + "/default.lua";
|
|
|
|
|
if( DoesFileExist(sLuaPath) )
|
|
|
|
|
{
|
|
|
|
|
sPath = sLuaPath;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-02-26 10:07:02 +00:00
|
|
|
sPath = DerefRedir( sPath );
|
2006-10-15 03:59:24 +00:00
|
|
|
return true;
|
2005-02-26 10:07:02 +00:00
|
|
|
}
|
2005-02-09 05:31:14 +00:00
|
|
|
|
2006-10-09 08:24:10 +00:00
|
|
|
Actor* ActorUtil::LoadFromNode( const XNode* pNode, Actor *pParentActor )
|
2004-12-27 10:24:54 +00:00
|
|
|
{
|
2005-01-17 05:28:16 +00:00
|
|
|
ASSERT( pNode );
|
|
|
|
|
|
2005-01-17 04:08:08 +00:00
|
|
|
{
|
2006-10-09 01:34:16 +00:00
|
|
|
bool bCond;
|
|
|
|
|
if( pNode->GetAttrValue("Condition", bCond) && !bCond )
|
|
|
|
|
return NULL;
|
2005-01-17 04:08:08 +00:00
|
|
|
}
|
|
|
|
|
|
2005-01-08 02:05:31 +00:00
|
|
|
// Element name is the type in XML.
|
|
|
|
|
// Type= is the name in INI.
|
2005-12-05 19:12:49 +00:00
|
|
|
// TODO: Remove the backward compat fallback
|
2006-10-02 06:12:42 +00:00
|
|
|
RString sClass = pNode->GetName();
|
2005-04-11 15:39:44 +00:00
|
|
|
bool bHasClass = pNode->GetAttrValue( "Class", sClass );
|
|
|
|
|
if( !bHasClass )
|
|
|
|
|
bHasClass = pNode->GetAttrValue( "Type", sClass ); // for backward compatibility
|
2004-01-25 22:22:40 +00:00
|
|
|
|
2005-07-26 21:12:00 +00:00
|
|
|
// backward compat hack
|
|
|
|
|
if( !bHasClass )
|
|
|
|
|
{
|
2006-01-22 01:00:06 +00:00
|
|
|
RString sText;
|
2005-07-26 21:12:00 +00:00
|
|
|
if( pNode->GetAttrValue( "Text", sText ) )
|
|
|
|
|
sClass = "BitmapText";
|
|
|
|
|
}
|
2005-01-16 08:15:20 +00:00
|
|
|
|
2005-07-22 19:57:42 +00:00
|
|
|
Actor *pReturn = NULL;
|
2005-02-15 07:57:38 +00:00
|
|
|
|
2005-04-11 15:39:44 +00:00
|
|
|
if( IsRegistered(sClass) )
|
2005-01-17 04:08:08 +00:00
|
|
|
{
|
2006-10-09 08:24:10 +00:00
|
|
|
pReturn = ActorUtil::Create( sClass, pNode, pParentActor );
|
2005-01-16 08:15:20 +00:00
|
|
|
}
|
2007-05-27 07:20:08 +00:00
|
|
|
else // sClass is invalid
|
2005-01-16 08:15:20 +00:00
|
|
|
{
|
2007-05-27 07:20:08 +00:00
|
|
|
RString sError = ssprintf( "%s: invalid Class \"%s\"",
|
|
|
|
|
ActorUtil::GetWhere(pNode).c_str(), sClass.c_str() );
|
|
|
|
|
Dialog::OK( sError );
|
|
|
|
|
pReturn = new Actor; // Return a dummy object so that we don't crash in AutoActor later.
|
2005-07-22 19:57:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return pReturn;
|
2003-10-30 03:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
2005-10-11 21:59:48 +00:00
|
|
|
/*
|
2006-10-11 04:54:08 +00:00
|
|
|
* Merge commands from a parent into a child. For example,
|
2005-10-11 21:59:48 +00:00
|
|
|
*
|
|
|
|
|
* parent.xml: <Layer File="child" OnCommand="x,10" />
|
|
|
|
|
* child.xml: <Layer1 File="image" OffCommand="y,10" />
|
|
|
|
|
*
|
|
|
|
|
* results in:
|
|
|
|
|
*
|
|
|
|
|
* <Layer1 File="image" OnCommand="x,10" OffCommand="y,10" />
|
|
|
|
|
*
|
2006-10-11 04:54:08 +00:00
|
|
|
* Warn about duplicate commands.
|
2005-10-11 21:59:48 +00:00
|
|
|
*/
|
|
|
|
|
static void MergeActorXML( XNode *pChild, const XNode *pParent )
|
|
|
|
|
{
|
|
|
|
|
FOREACH_CONST_Child( pParent, p )
|
|
|
|
|
pChild->AppendChild( new XNode(*p) );
|
|
|
|
|
|
|
|
|
|
FOREACH_CONST_Attr( pParent, p )
|
|
|
|
|
{
|
2006-10-11 04:54:08 +00:00
|
|
|
const RString &sName = p->first;
|
|
|
|
|
if( !EndsWith(sName, "Command") )
|
2005-10-11 21:59:48 +00:00
|
|
|
continue;
|
|
|
|
|
|
2007-05-27 07:20:41 +00:00
|
|
|
if( pChild->GetAttr(p->first) != NULL )
|
2005-10-11 21:59:48 +00:00
|
|
|
{
|
2006-01-22 01:00:06 +00:00
|
|
|
RString sWarning =
|
2006-10-11 04:58:11 +00:00
|
|
|
ssprintf( "%s: overriding \"%s\" in %s node \"%s\"",
|
2006-10-11 04:54:08 +00:00
|
|
|
ActorUtil::GetWhere(pParent).c_str(),
|
|
|
|
|
sName.c_str(),
|
2006-10-11 04:58:11 +00:00
|
|
|
ActorUtil::GetWhere(pChild).c_str(),
|
|
|
|
|
pParent->GetName().c_str() );
|
2005-10-11 21:59:48 +00:00
|
|
|
Dialog::OK( sWarning, "XML_ATTRIB_OVERRIDE" );
|
|
|
|
|
}
|
2006-10-09 05:14:23 +00:00
|
|
|
pChild->AppendAttrFrom( p->first, p->second->Copy() );
|
2005-10-11 21:59:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-10-09 05:51:12 +00:00
|
|
|
namespace
|
|
|
|
|
{
|
|
|
|
|
XNode *LoadXNodeFromLuaShowErrors( const RString &sFile )
|
|
|
|
|
{
|
|
|
|
|
RString sScript;
|
|
|
|
|
if( !GetFileContents(sFile, sScript) )
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
Lua *L = LUA->Get();
|
|
|
|
|
|
|
|
|
|
RString sError;
|
2007-02-04 08:16:46 +00:00
|
|
|
if( !LuaHelpers::LoadScript(L, sScript, "@" + sFile, sError) )
|
2006-10-09 05:51:12 +00:00
|
|
|
{
|
|
|
|
|
LUA->Release( L );
|
|
|
|
|
sError = ssprintf( "Lua runtime error: %s", sError.c_str() );
|
|
|
|
|
Dialog::OK( sError, "LUA_ERROR" );
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-06 08:03:34 +00:00
|
|
|
XNode *pRet = NULL;
|
|
|
|
|
if( ActorUtil::LoadTableFromStackShowErrors(L) )
|
|
|
|
|
pRet = XmlFileUtil::XNodeFromTable( L );
|
2006-10-09 05:51:12 +00:00
|
|
|
|
|
|
|
|
LUA->Release( L );
|
|
|
|
|
return pRet;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-06 08:03:34 +00:00
|
|
|
/* Run the function at the top of the stack, which returns an actor description
|
|
|
|
|
* table. If the table was returned, return true and leave it on the stack.
|
|
|
|
|
* If not, display an error, return false, and leave nothing on the stack. */
|
|
|
|
|
bool ActorUtil::LoadTableFromStackShowErrors( Lua *L )
|
2007-02-04 08:16:46 +00:00
|
|
|
{
|
|
|
|
|
LuaReference func;
|
|
|
|
|
lua_pushvalue( L, -1 );
|
|
|
|
|
func.SetFromStack( L );
|
|
|
|
|
|
|
|
|
|
RString sError;
|
|
|
|
|
if( !LuaHelpers::RunScriptOnStack(L, sError, 0, 1) )
|
|
|
|
|
{
|
|
|
|
|
lua_pop( L, 1 );
|
|
|
|
|
sError = ssprintf( "Lua runtime error: %s", sError.c_str() );
|
|
|
|
|
Dialog::OK( sError, "LUA_ERROR" );
|
2007-02-06 08:03:34 +00:00
|
|
|
return false;
|
2007-02-04 08:16:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( lua_type(L, -1) != LUA_TTABLE )
|
|
|
|
|
{
|
|
|
|
|
lua_pop( L, 1 );
|
|
|
|
|
|
|
|
|
|
func.PushSelf( L );
|
|
|
|
|
lua_Debug debug;
|
|
|
|
|
lua_getinfo( L, ">nS", &debug );
|
|
|
|
|
|
|
|
|
|
sError = ssprintf( "%s: must return a table", debug.short_src );
|
|
|
|
|
|
|
|
|
|
Dialog::OK( sError, "LUA_ERROR" );
|
2007-02-06 08:03:34 +00:00
|
|
|
return false;
|
2007-02-04 08:16:46 +00:00
|
|
|
}
|
2007-02-06 08:03:34 +00:00
|
|
|
return true;
|
2007-02-04 08:16:46 +00:00
|
|
|
}
|
|
|
|
|
|
2005-10-11 21:59:48 +00:00
|
|
|
/*
|
|
|
|
|
* If pParent is non-NULL, it's the parent node when nesting XML, which is
|
|
|
|
|
* used only by ActorUtil::LoadFromNode.
|
|
|
|
|
*/
|
2006-10-14 04:48:19 +00:00
|
|
|
Actor* ActorUtil::MakeActor( const RString &sPath_, Actor *pParentActor, const XNode *pParent )
|
2003-10-30 03:51:53 +00:00
|
|
|
{
|
2005-10-11 21:59:48 +00:00
|
|
|
static const XNode dummy;
|
|
|
|
|
if( pParent == NULL )
|
|
|
|
|
pParent = &dummy;
|
|
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
RString sPath( sPath_ );
|
2005-10-11 21:45:35 +00:00
|
|
|
|
2005-10-10 20:55:47 +00:00
|
|
|
FileType ft = GetFileType( sPath );
|
2006-10-09 05:16:35 +00:00
|
|
|
if( ft == FT_Directory && sPath.Right(1) != "/" )
|
|
|
|
|
sPath += '/';
|
|
|
|
|
|
2005-05-29 01:04:44 +00:00
|
|
|
switch( ft )
|
2003-10-30 03:51:53 +00:00
|
|
|
{
|
2006-10-09 05:51:12 +00:00
|
|
|
case FT_Lua:
|
|
|
|
|
{
|
|
|
|
|
auto_ptr<XNode> pNode( LoadXNodeFromLuaShowErrors(sPath) );
|
|
|
|
|
if( pNode.get() == NULL )
|
|
|
|
|
{
|
|
|
|
|
// XNode will warn about the error
|
|
|
|
|
return new Actor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MergeActorXML( pNode.get(), pParent );
|
2006-10-15 01:21:54 +00:00
|
|
|
Actor *pRet = ActorUtil::LoadFromNode( pNode.get(), pParentActor );
|
2006-10-09 05:51:12 +00:00
|
|
|
return pRet;
|
|
|
|
|
}
|
2006-10-14 03:52:11 +00:00
|
|
|
case FT_Directory:
|
|
|
|
|
{
|
2006-10-14 04:04:48 +00:00
|
|
|
XNode xml( *pParent );
|
2006-10-14 03:52:11 +00:00
|
|
|
xml.AppendAttr( "AniDir", sPath );
|
|
|
|
|
|
|
|
|
|
return ActorUtil::Create( "BGAnimation", &xml, pParentActor );
|
|
|
|
|
}
|
2005-05-29 01:04:44 +00:00
|
|
|
case FT_Bitmap:
|
|
|
|
|
case FT_Movie:
|
2005-01-17 01:32:45 +00:00
|
|
|
{
|
2005-10-11 21:59:48 +00:00
|
|
|
XNode xml( *pParent );
|
|
|
|
|
xml.AppendAttr( "Texture", sPath );
|
|
|
|
|
|
2006-10-09 08:24:10 +00:00
|
|
|
return ActorUtil::Create( "Sprite", &xml, pParentActor );
|
2005-01-17 01:32:45 +00:00
|
|
|
}
|
2005-05-29 01:04:44 +00:00
|
|
|
case FT_Model:
|
2005-01-17 01:32:45 +00:00
|
|
|
{
|
2005-10-11 21:59:48 +00:00
|
|
|
XNode xml( *pParent );
|
|
|
|
|
xml.AppendAttr( "Meshes", sPath );
|
|
|
|
|
xml.AppendAttr( "Materials", sPath );
|
|
|
|
|
xml.AppendAttr( "Bones", sPath );
|
|
|
|
|
|
2006-10-09 08:24:10 +00:00
|
|
|
return ActorUtil::Create( "Model", &xml, pParentActor );
|
2005-01-17 01:32:45 +00:00
|
|
|
}
|
2005-05-29 01:04:44 +00:00
|
|
|
default:
|
2006-10-14 04:06:49 +00:00
|
|
|
{
|
|
|
|
|
LOG->Warn( "File \"%s\" has unknown type, \"%s\".", sPath.c_str(), FileTypeToString(ft).c_str() );
|
|
|
|
|
|
|
|
|
|
XNode xml( *pParent );
|
|
|
|
|
return ActorUtil::Create( "Actor", &xml, pParentActor );
|
|
|
|
|
}
|
2004-11-26 17:28:47 +00:00
|
|
|
}
|
2003-07-07 01:29:18 +00:00
|
|
|
}
|
|
|
|
|
|
2006-10-09 07:42:18 +00:00
|
|
|
RString ActorUtil::GetSourcePath( const XNode *pNode )
|
|
|
|
|
{
|
|
|
|
|
RString sRet;
|
|
|
|
|
pNode->GetAttrValue( "_Source", sRet );
|
|
|
|
|
if( sRet.substr(0, 1) == "@" )
|
|
|
|
|
sRet.erase( 0, 1 );
|
|
|
|
|
|
|
|
|
|
return sRet;
|
|
|
|
|
}
|
|
|
|
|
|
2006-10-09 08:08:17 +00:00
|
|
|
RString ActorUtil::GetWhere( const XNode *pNode )
|
|
|
|
|
{
|
|
|
|
|
RString sPath = GetSourcePath( pNode );
|
|
|
|
|
|
|
|
|
|
int iLine;
|
|
|
|
|
if( pNode->GetAttrValue("_Line", iLine) )
|
|
|
|
|
sPath += ssprintf( ":%i", iLine );
|
|
|
|
|
return sPath;
|
|
|
|
|
}
|
|
|
|
|
|
2006-10-09 06:54:52 +00:00
|
|
|
bool ActorUtil::GetAttrPath( const XNode *pNode, const RString &sName, RString &sOut )
|
|
|
|
|
{
|
|
|
|
|
if( !pNode->GetAttrValue(sName, sOut) )
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
bool bIsRelativePath = sOut.Left(1) != "/";
|
|
|
|
|
if( bIsRelativePath )
|
|
|
|
|
{
|
|
|
|
|
RString sDir;
|
|
|
|
|
if( !pNode->GetAttrValue("_Dir", sDir) )
|
|
|
|
|
{
|
|
|
|
|
LOG->Warn( "Relative path \"%s\", but path is unknown", sOut.c_str() );
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
sOut = sDir+sOut;
|
|
|
|
|
}
|
|
|
|
|
|
2006-10-15 04:01:12 +00:00
|
|
|
return ActorUtil::ResolvePath( sOut, ActorUtil::GetWhere(pNode) );
|
2006-10-09 06:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
2006-09-29 07:24:40 +00:00
|
|
|
apActorCommands ActorUtil::ParseActorCommands( const RString &sCommands, const RString &sName )
|
2006-09-22 18:59:26 +00:00
|
|
|
{
|
|
|
|
|
Lua *L = LUA->Get();
|
2006-10-08 22:24:15 +00:00
|
|
|
LuaHelpers::ParseCommandList( L, sCommands, sName );
|
2006-09-22 04:56:35 +00:00
|
|
|
LuaReference *pRet = new LuaReference;
|
2006-09-21 02:36:40 +00:00
|
|
|
pRet->SetFromStack( L );
|
|
|
|
|
LUA->Release( L );
|
|
|
|
|
|
|
|
|
|
return apActorCommands( pRet );
|
2006-09-21 02:24:22 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
void ActorUtil::SetXY( Actor& actor, const RString &sType )
|
2003-10-31 00:50:06 +00:00
|
|
|
{
|
2005-05-31 08:09:04 +00:00
|
|
|
ASSERT( !actor.GetName().empty() );
|
2005-08-25 23:03:30 +00:00
|
|
|
|
2005-09-21 17:45:34 +00:00
|
|
|
/*
|
|
|
|
|
* Hack: This is run after InitCommand, and InitCommand might set X/Y. If
|
|
|
|
|
* these are both 0, leave the actor where it is. If InitCommand doesn't,
|
|
|
|
|
* then 0,0 is the default, anyway.
|
|
|
|
|
*/
|
|
|
|
|
float fX = THEME->GetMetricF(sType,actor.GetName()+"X");
|
|
|
|
|
float fY = THEME->GetMetricF(sType,actor.GetName()+"Y");
|
|
|
|
|
if( fX != 0 || fY != 0 )
|
|
|
|
|
actor.SetXY( fX, fY );
|
2003-10-31 00:50:06 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
void ActorUtil::LoadCommand( Actor& actor, const RString &sType, const RString &sCommandName )
|
2003-10-31 00:50:06 +00:00
|
|
|
{
|
2005-09-08 03:38:08 +00:00
|
|
|
ActorUtil::LoadCommandFromName( actor, sType, sCommandName, actor.GetName() );
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
void ActorUtil::LoadCommandFromName( Actor& actor, const RString &sType, const RString &sCommandName, const RString &sName )
|
2005-09-08 03:38:08 +00:00
|
|
|
{
|
|
|
|
|
actor.AddCommand( sCommandName, THEME->GetMetricA(sType,sName+sCommandName+"Command") );
|
2005-03-13 20:44:26 +00:00
|
|
|
}
|
2003-10-31 00:50:06 +00:00
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
void ActorUtil::LoadAllCommands( Actor& actor, const RString &sType )
|
2005-09-07 20:41:50 +00:00
|
|
|
{
|
|
|
|
|
LoadAllCommandsFromName( actor, sType, actor.GetName() );
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
void ActorUtil::LoadAllCommandsFromName( Actor& actor, const RString &sType, const RString &sName )
|
2005-02-23 23:04:06 +00:00
|
|
|
{
|
2006-01-22 01:00:06 +00:00
|
|
|
set<RString> vsValueNames;
|
2005-09-07 20:41:50 +00:00
|
|
|
THEME->GetMetricsThatBeginWith( sType, sName, vsValueNames );
|
2005-03-13 20:44:26 +00:00
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
FOREACHS_CONST( RString, vsValueNames, v )
|
2005-03-13 20:44:26 +00:00
|
|
|
{
|
2006-01-22 01:00:06 +00:00
|
|
|
const RString &sv = *v;
|
2007-02-19 09:30:07 +00:00
|
|
|
static const RString sEnding = "Command";
|
2006-01-29 22:13:16 +00:00
|
|
|
if( EndsWith(sv,sEnding) )
|
2005-03-13 20:44:26 +00:00
|
|
|
{
|
2006-01-29 22:13:16 +00:00
|
|
|
RString sCommandName( sv.begin()+sName.size(), sv.end()-sEnding.size() );
|
2005-09-08 03:38:08 +00:00
|
|
|
LoadCommandFromName( actor, sType, sCommandName, sName );
|
2005-03-13 20:44:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
2005-02-23 23:04:06 +00:00
|
|
|
}
|
|
|
|
|
|
2005-05-03 09:29:54 +00:00
|
|
|
static bool CompareActorsByZPosition(const Actor *p1, const Actor *p2)
|
|
|
|
|
{
|
|
|
|
|
return p1->GetZ() < p2->GetZ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ActorUtil::SortByZPosition( vector<Actor*> &vActors )
|
|
|
|
|
{
|
|
|
|
|
// Preserve ordering of Actors with equal Z positions.
|
|
|
|
|
stable_sort( vActors.begin(), vActors.end(), CompareActorsByZPosition );
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-04 22:30:51 +00:00
|
|
|
static const char *FileTypeNames[] = {
|
2005-05-29 01:04:44 +00:00
|
|
|
"Bitmap",
|
2006-12-04 22:32:53 +00:00
|
|
|
"Sound",
|
2005-05-29 01:04:44 +00:00
|
|
|
"Movie",
|
|
|
|
|
"Directory",
|
2006-10-09 05:51:12 +00:00
|
|
|
"Lua",
|
2005-05-29 01:04:44 +00:00
|
|
|
"Model",
|
|
|
|
|
};
|
2006-10-15 01:31:15 +00:00
|
|
|
XToString( FileType );
|
2006-10-14 23:35:31 +00:00
|
|
|
LuaXType( FileType );
|
2005-05-29 01:04:44 +00:00
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
FileType ActorUtil::GetFileType( const RString &sPath )
|
2005-05-29 01:04:44 +00:00
|
|
|
{
|
2006-01-22 01:00:06 +00:00
|
|
|
RString sExt = GetExtension( sPath );
|
2005-05-29 01:04:44 +00:00
|
|
|
sExt.MakeLower();
|
|
|
|
|
|
2007-05-27 06:24:16 +00:00
|
|
|
if( sExt=="lua" ) return FT_Lua;
|
2005-05-29 01:04:44 +00:00
|
|
|
else if(
|
|
|
|
|
sExt=="png" ||
|
|
|
|
|
sExt=="jpg" ||
|
|
|
|
|
sExt=="gif" ||
|
|
|
|
|
sExt=="bmp" ) return FT_Bitmap;
|
2006-12-04 22:32:53 +00:00
|
|
|
else if(
|
|
|
|
|
sExt=="ogg" ||
|
|
|
|
|
sExt=="wav" ||
|
|
|
|
|
sExt=="mp3" ) return FT_Sound;
|
2005-05-29 01:04:44 +00:00
|
|
|
else if(
|
|
|
|
|
sExt=="avi" ||
|
|
|
|
|
sExt=="mpeg" ||
|
|
|
|
|
sExt=="mpg" ) return FT_Movie;
|
|
|
|
|
else if(
|
2005-07-05 23:46:08 +00:00
|
|
|
sExt=="txt" ) return FT_Model;
|
2006-10-14 02:57:24 +00:00
|
|
|
else if( sPath.size() > 0 && sPath[sPath.size()-1] == '/' )
|
|
|
|
|
return FT_Directory;
|
2005-08-26 19:41:19 +00:00
|
|
|
/* Do this last, to avoid the IsADirectory in most cases. */
|
|
|
|
|
else if( IsADirectory(sPath) ) return FT_Directory;
|
2006-10-14 02:59:24 +00:00
|
|
|
else return FileType_Invalid;
|
2005-05-29 01:04:44 +00:00
|
|
|
}
|
2005-05-03 09:29:54 +00:00
|
|
|
|
2006-10-14 23:34:41 +00:00
|
|
|
|
|
|
|
|
// lua start
|
|
|
|
|
#include "LuaBinding.h"
|
|
|
|
|
|
|
|
|
|
namespace
|
|
|
|
|
{
|
|
|
|
|
int GetFileType( lua_State *L ) { Enum::Push( L, ActorUtil::GetFileType(SArg(1)) ); return 1; }
|
2006-10-15 02:42:33 +00:00
|
|
|
int ResolvePath( lua_State *L )
|
|
|
|
|
{
|
|
|
|
|
RString sPath( SArg(1) );
|
2006-10-15 04:59:48 +00:00
|
|
|
int iLevel = IArg(2);
|
|
|
|
|
luaL_where( L, iLevel );
|
|
|
|
|
RString sWhere = lua_tostring( L, -1 );
|
|
|
|
|
if( sWhere.size() > 2 && sWhere.substr(sWhere.size()-2, 2) == ": " )
|
|
|
|
|
sWhere = sWhere.substr( 0, sWhere.size()-2 ); // remove trailing ": "
|
2006-10-15 02:42:33 +00:00
|
|
|
|
|
|
|
|
LUA->YieldLua();
|
2006-10-15 04:59:48 +00:00
|
|
|
bool bRet = ActorUtil::ResolvePath( sPath, sWhere );
|
2006-10-15 02:42:33 +00:00
|
|
|
LUA->UnyieldLua();
|
|
|
|
|
|
2006-10-15 04:59:48 +00:00
|
|
|
if( !bRet )
|
|
|
|
|
return 0;
|
2006-10-15 02:42:33 +00:00
|
|
|
LuaHelpers::Push( L, sPath );
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2006-10-15 05:58:27 +00:00
|
|
|
int IsRegisteredClass( lua_State *L )
|
2006-10-15 05:54:50 +00:00
|
|
|
{
|
|
|
|
|
lua_pushboolean( L, IsRegistered(SArg(1)) );
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2006-10-14 23:34:41 +00:00
|
|
|
|
|
|
|
|
const luaL_Reg ActorUtilTable[] =
|
|
|
|
|
{
|
|
|
|
|
LIST_METHOD( GetFileType ),
|
2006-10-15 02:42:33 +00:00
|
|
|
LIST_METHOD( ResolvePath ),
|
2006-10-15 05:58:27 +00:00
|
|
|
LIST_METHOD( IsRegisteredClass ),
|
2006-10-14 23:34:41 +00:00
|
|
|
{ NULL, NULL }
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LUA_REGISTER_NAMESPACE( ActorUtil )
|
|
|
|
|
|
2004-06-08 00:47:53 +00:00
|
|
|
/*
|
|
|
|
|
* (c) 2003-2004 Chris Danford
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the
|
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
|
* distribute, and/or sell copies of the Software, and to permit persons to
|
|
|
|
|
* whom the Software is furnished to do so, provided that the above
|
|
|
|
|
* copyright notice(s) and this permission notice appear in all copies of
|
|
|
|
|
* the Software and that both the above copyright notice(s) and this
|
|
|
|
|
* permission notice appear in supporting documentation.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
|
|
|
|
|
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
|
|
|
|
|
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
|
|
|
|
|
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
|
|
|
|
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
|
|
|
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
|
* PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
*/
|