Files
itgmania212121/stepmania/src/ActorUtil.cpp
T

395 lines
11 KiB
C++
Raw Normal View History

2004-06-08 00:47:53 +00:00
#include "global.h"
#include "ActorUtil.h"
#include "Sprite.h"
#include "BitmapText.h"
#include "Model.h"
2003-10-31 00:50:06 +00:00
#include "BGAnimation.h"
#include "IniFile.h"
#include "ThemeManager.h"
#include "RageLog.h"
2004-01-25 22:22:40 +00:00
#include "song.h"
#include "GameState.h"
#include "RageTextureManager.h"
2004-02-01 03:14:37 +00:00
#include "SongManager.h"
2004-02-01 23:06:43 +00:00
#include "Course.h"
2005-01-07 22:01:57 +00:00
#include "XmlFile.h"
2005-01-07 21:27:30 +00:00
#include "FontCharAliases.h"
2005-01-24 02:26:55 +00:00
#include "LuaManager.h"
2005-02-09 05:31:14 +00:00
#include "MessageManager.h"
#include "arch/Dialog/Dialog.h"
2005-02-09 05:31:14 +00:00
// Actor registration
static map<CString,CreateActorFn> *g_pmapRegistrees = NULL;
static bool IsRegistered( const CString& sClassName )
{
return g_pmapRegistrees->find( sClassName ) != g_pmapRegistrees->end();
}
2005-02-09 05:31:14 +00:00
void ActorUtil::Register( const CString& sClassName, CreateActorFn pfn )
{
if( g_pmapRegistrees == NULL )
g_pmapRegistrees = new map<CString,CreateActorFn>;
map<CString,CreateActorFn>::iterator iter = g_pmapRegistrees->find( sClassName );
ASSERT_M( iter == g_pmapRegistrees->end(), ssprintf("Actor class '%s' already registered.", sClassName.c_str()) );
(*g_pmapRegistrees)[sClassName] = pfn;
}
Actor* ActorUtil::Create( const CString& sClassName, const CString& sDir, const XNode* pNode )
{
map<CString,CreateActorFn>::iterator iter = g_pmapRegistrees->find( sClassName );
ASSERT_M( iter != g_pmapRegistrees->end(), ssprintf("Actor '%s' is not registered.",sClassName.c_str()) )
CreateActorFn pfn = iter->second;
return pfn( sDir, pNode );
}
2005-02-26 10:07:02 +00:00
/* Return false to retry. */
void ActorUtil::ResolvePath( CString &sPath, const CString &sName )
{
const CString sOriginalPath( sPath );
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.
if( !IsAFile(sPath) && !IsADirectory(sPath) )
{
CStringArray asPaths;
GetDirListing( sPath + "*", asPaths, false, true ); // return path too
if( asPaths.empty() )
{
CString sError = ssprintf( "A file in '%s' references a file '%s' which doesn't exist.", sName.c_str(), sPath.c_str() );
switch( Dialog::AbortRetryIgnore( sError, "BROKEN_FILE_REFERENCE" ) )
{
case Dialog::abort:
RageException::Throw( sError );
break;
case Dialog::retry:
FlushDirCache();
goto retry;
case Dialog::ignore:
asPaths.push_back( sPath );
if( GetExtension(asPaths[0]) == "" )
asPaths[0] = SetExtension( asPaths[0], "png" );
break;
default:
ASSERT(0);
}
}
else if( asPaths.size() > 1 )
{
CString sError = ssprintf( "A file in '%s' references a file '%s' which has multiple matches.", sName.c_str(), sPath.c_str() );
switch( Dialog::AbortRetryIgnore( sError, "BROKEN_FILE_REFERENCE" ) )
{
case Dialog::abort:
RageException::Throw( sError );
break;
case Dialog::retry:
FlushDirCache();
goto retry;
case Dialog::ignore:
asPaths.erase( asPaths.begin()+1, asPaths.end() );
break;
default:
ASSERT(0);
}
}
sPath = asPaths[0];
}
sPath = DerefRedir( sPath );
}
2005-02-09 05:31:14 +00:00
2005-01-17 05:42:52 +00:00
Actor* ActorUtil::LoadFromActorFile( const CString& sAniDir, const XNode* pNode )
{
ASSERT( pNode );
2005-01-17 04:08:08 +00:00
{
CString expr;
if( pNode->GetAttrValue("Condition",expr) )
2005-01-17 04:08:08 +00:00
{
2005-01-19 22:27:24 +00:00
if( !LUA->RunExpressionB(expr) )
2005-01-17 04:08:08 +00:00
return NULL;
}
}
2005-01-08 02:05:31 +00:00
// Element name is the type in XML.
// Type= is the name in INI.
CString sType = pNode->m_sName;
2005-02-15 20:51:03 +00:00
bool bHasType = pNode->GetAttrValue( "Type", sType );
2004-01-25 22:22:40 +00:00
CString sFile;
pNode->GetAttrValue( "File", sFile );
2005-02-28 04:10:46 +00:00
// FIXME: If specifying a path in Lua, assume it is absolute. We need a way to specify
// absolute paths. We can't use a slash at the beginning to mean absolute because FILEMAN
// maps that to the root of the filesystem.
bool bIsAbsolutePath = LUA->RunAtExpressionS( sFile );
FixSlashesInPlace( sFile );
2004-01-25 22:22:40 +00:00
2005-01-16 08:15:20 +00:00
CString sText;
bool bHasText = pNode->GetAttrValue( "Text", sText );
2005-01-16 08:15:20 +00:00
2005-02-15 20:51:03 +00:00
//
2005-01-16 08:15:20 +00:00
// backward compatibility hacks
2005-02-15 20:51:03 +00:00
//
if( bHasText && !bHasType )
2005-01-16 08:15:20 +00:00
sType = "BitmapText";
else if( sFile.CompareNoCase("songbackground") == 0 )
sType = "SongBackground";
else if( sFile.CompareNoCase("songbanner") == 0 )
sType = "SongBanner";
else if( sFile.CompareNoCase("coursebanner") == 0 )
sType = "CourseBanner";
2005-02-15 07:57:38 +00:00
if( IsRegistered(sType) )
2005-01-17 04:08:08 +00:00
{
return ActorUtil::Create( sType, sAniDir, pNode );
2005-01-16 08:15:20 +00:00
}
else if( sType == "SongBackground" )
{
Song *pSong = GAMESTATE->m_pCurSong;
if( pSong && pSong->HasBackground() )
sFile = pSong->GetBackgroundPath();
else
2005-02-06 03:32:53 +00:00
sFile = THEME->GetPathG("Common","fallback background");
2005-01-16 08:15:20 +00:00
/* Always load song backgrounds with SongBGTexture. It sets texture properties;
* if we load a background without setting those properties, we'll end up
* with duplicates. */
Sprite* pSprite = new Sprite;
pSprite->LoadBG( sFile );
pSprite->LoadFromNode( sAniDir, pNode );
return pSprite;
2005-01-16 08:15:20 +00:00
}
else if( sType == "SongBanner" )
{
Song *pSong = GAMESTATE->m_pCurSong;
if( pSong == NULL )
2004-01-25 22:22:40 +00:00
{
2005-01-16 08:15:20 +00:00
// probe for a random banner
for( int i=0; i<300; i++ )
{
pSong = SONGMAN->GetRandomSong();
if( pSong == NULL )
break;
if( !pSong->ShowInDemonstrationAndRanking() )
continue;
break;
}
2004-01-25 22:22:40 +00:00
}
2005-01-16 08:15:20 +00:00
if( pSong && pSong->HasBanner() )
sFile = pSong->GetBannerPath();
2004-01-25 22:22:40 +00:00
else
2005-02-06 03:32:53 +00:00
sFile = THEME->GetPathG("Common","fallback banner");
2005-01-16 08:15:20 +00:00
TEXTUREMAN->DisableOddDimensionWarning();
/* Always load banners with BannerTex. It sets texture properties;
* if we load a background without setting those properties, we'll end up
* with duplicates. */
Sprite* pSprite = new Sprite;
pSprite->Load( Sprite::SongBannerTexture(sFile) );
pSprite->LoadFromNode( sAniDir, pNode );
2005-01-16 08:15:20 +00:00
TEXTUREMAN->EnableOddDimensionWarning();
return pSprite;
2005-01-16 08:15:20 +00:00
}
else if( sType == "CourseBanner" )
{
Course *pCourse = GAMESTATE->m_pCurCourse;
if( pCourse == NULL )
2004-01-25 22:22:40 +00:00
{
2005-01-16 08:15:20 +00:00
// probe for a random banner
for( int i=0; i<300; i++ )
2004-01-25 22:22:40 +00:00
{
2005-01-16 08:15:20 +00:00
pCourse = SONGMAN->GetRandomCourse();
if( pCourse == NULL )
break;
if( !pCourse->ShowInDemonstrationAndRanking() )
continue;
if( pCourse->m_bIsAutogen )
continue;
break;
2004-01-25 22:22:40 +00:00
}
2005-01-16 08:15:20 +00:00
}
2004-02-01 03:14:37 +00:00
2005-01-16 08:15:20 +00:00
if( pCourse && pCourse->HasBanner() )
sFile = pCourse->m_sBannerPath;
else
2005-02-06 03:32:53 +00:00
sFile = THEME->GetPathG("Common","fallback banner");
2004-01-25 22:22:40 +00:00
2005-01-16 08:15:20 +00:00
TEXTUREMAN->DisableOddDimensionWarning();
Sprite* pSprite = new Sprite;
pSprite->Load( Sprite::SongBannerTexture(sFile) );
pSprite->LoadFromNode( sAniDir, pNode );
2005-01-16 08:15:20 +00:00
TEXTUREMAN->EnableOddDimensionWarning();
return pSprite;
2005-01-16 08:15:20 +00:00
}
else // sType is empty or garbage (e.g. "1" // 0==Sprite")
{
// automatically figure out the type
/* Be careful: if sFile is "", and we don't check it, then we can end up recursively
* loading the BGAnimationLayer that we're in. */
if( sFile == "" )
2005-02-23 19:06:31 +00:00
RageException::Throw( "The actor file in '%s' is missing the File attribute or has an invalid Type \"%s\"",
sAniDir.c_str(), sType.c_str() );
2004-03-26 08:08:02 +00:00
2005-02-28 04:10:46 +00:00
CString sNewPath = bIsAbsolutePath ? sFile : sAniDir+sFile;
2005-01-16 08:15:20 +00:00
2005-02-26 10:07:02 +00:00
ActorUtil::ResolvePath( sNewPath, sAniDir );
2005-01-16 08:15:20 +00:00
Actor *pActor = ActorUtil::MakeActor( sNewPath );
2005-01-17 04:08:08 +00:00
if( pActor == NULL )
return NULL;
pActor->LoadFromNode( sAniDir, pNode );
return pActor;
}
}
2005-01-17 05:42:52 +00:00
Actor* ActorUtil::MakeActor( const RageTextureID &ID )
{
CString sExt = GetExtension( ID.filename );
sExt.MakeLower();
2005-01-07 22:01:57 +00:00
if( sExt=="xml" )
{
XNode xml;
2005-02-18 13:05:17 +00:00
PARSEINFO pi;
if( !xml.LoadFromFile( ID.filename, &pi ) )
2005-02-26 03:35:10 +00:00
RageException::Throw( ssprintf("Error loading %s: %s", ID.filename.c_str(), pi.error_string.c_str()) );
2005-01-07 22:01:57 +00:00
CString sDir = Dirname( ID.filename );
return LoadFromActorFile( sDir, &xml );
2005-01-07 22:01:57 +00:00
}
else if( sExt=="actor" )
{
// TODO: Check for recursive loading
IniFile ini;
if( !ini.ReadFile( ID.filename ) )
RageException::Throw( "%s", ini.GetError().c_str() );
CString sDir = Dirname( ID.filename );
const XNode* pNode = ini.GetChild( "Actor" );
if( pNode == NULL )
RageException::Throw( "The file '%s' doesn't have layer 'Actor'.", ID.filename.c_str() );
return LoadFromActorFile( sDir, pNode );
}
else if( sExt=="png" ||
sExt=="jpg" ||
sExt=="gif" ||
sExt=="bmp" ||
sExt=="avi" ||
sExt=="mpeg" ||
sExt=="mpg" ||
sExt=="sprite" )
{
Sprite* pSprite = new Sprite;
pSprite->Load( ID );
return pSprite;
}
2004-02-08 11:17:03 +00:00
else if( sExt=="txt" ||
sExt=="model" )
{
Model* pModel = new Model;
2004-02-08 11:17:03 +00:00
pModel->Load( ID.filename );
return pModel;
}
2003-10-31 00:50:06 +00:00
/* Do this last, to avoid the IsADirectory in most cases. */
else if( IsADirectory(ID.filename) )
{
2005-01-17 04:08:08 +00:00
CString sDir = ID.filename;
if( sDir.Right(1) != "/" )
sDir += '/';
CString sIni = sDir + "BGAnimation.ini";
CString sXml = sDir + "default.xml";
if( DoesFileExist(sXml) )
{
XNode xml;
2005-02-18 13:05:17 +00:00
PARSEINFO pi;
if( !xml.LoadFromFile( sXml, &pi ) )
2005-02-26 03:35:10 +00:00
RageException::Throw( ssprintf("Error loading %s: %s", sXml.c_str(), pi.error_string.c_str()) );
return LoadFromActorFile( sDir, &xml );
}
else
{
BGAnimation *pBGA = new BGAnimation;
pBGA->LoadFromAniDir( sDir );
return pBGA;
}
2003-10-31 00:50:06 +00:00
}
else
2004-11-26 17:28:47 +00:00
{
RageException::Throw("File \"%s\" has unknown type, \"%s\"",
ID.filename.c_str(), sExt.c_str() );
}
}
2005-02-23 22:21:39 +00:00
void ActorUtil::SetXY( Actor& actor, const CString &sType )
2003-10-31 00:50:06 +00:00
{
2003-11-14 23:01:35 +00:00
ASSERT( !actor.GetID().empty() );
2005-02-23 22:21:39 +00:00
actor.SetXY( THEME->GetMetricF(sType,actor.GetID()+"X"), THEME->GetMetricF(sType,actor.GetID()+"Y") );
2003-10-31 00:50:06 +00:00
}
2005-02-23 22:21:39 +00:00
void ActorUtil::RunCommand( Actor& actor, const CString &sType, const CString &sCommandName )
2003-10-31 00:50:06 +00:00
{
actor.PlayCommand( sCommandName );
2003-10-31 00:50:06 +00:00
// HACK: It's very often that we command things to TweenOffScreen
// that we aren't drawing. We know that an Actor is not being
2003-11-05 06:43:26 +00:00
// used if its name is blank. So, do nothing on Actors with a blank name.
2003-10-31 02:01:16 +00:00
// (Do "playcommand" anyway; BGAs often have no name.)
if( sCommandName=="Off" )
{
2003-11-14 23:01:35 +00:00
if( actor.GetID().empty() )
2004-02-01 03:14:37 +00:00
return;
2004-11-06 03:09:29 +00:00
}
else
{
2004-06-16 00:38:31 +00:00
ASSERT_M( !actor.GetID().empty(), ssprintf("!actor.GetID().empty() ('%s', '%s')",
2005-02-23 22:21:39 +00:00
sType.c_str(), sCommandName.c_str()) );
2004-11-06 03:09:29 +00:00
}
2003-10-31 01:02:50 +00:00
2005-02-23 22:21:39 +00:00
actor.RunCommands( THEME->GetMetricA(sType,actor.GetID()+sCommandName+"Command") );
2003-10-31 00:50:06 +00:00
}
2005-02-23 23:04:06 +00:00
void ActorUtil::LoadCommand( Actor& actor, const CString &sType, const CString &sCommandName )
{
actor.AddCommand( sCommandName, THEME->GetMetricA(sType,actor.GetID()+sCommandName+"Command") );
}
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.
*/