Files
itgmania212121/stepmania/src/Song.cpp
T

1463 lines
40 KiB
C++
Raw Normal View History

2003-02-16 04:01:45 +00:00
#include "global.h"
#include "song.h"
2003-08-03 00:13:55 +00:00
#include "Steps.h"
2001-11-03 10:52:42 +00:00
#include "RageUtil.h"
2002-05-01 19:14:55 +00:00
#include "RageLog.h"
2002-06-30 23:19:33 +00:00
#include "IniFile.h"
2002-07-03 03:13:13 +00:00
#include "NoteData.h"
2004-02-20 05:35:05 +00:00
#include "RageSoundReader_FileReader.h"
2004-05-17 02:38:01 +00:00
#include "RageSurface_Load.h"
2002-07-27 19:29:51 +00:00
#include "RageException.h"
2003-12-21 02:54:23 +00:00
#include "SongManager.h"
#include "SongCacheIndex.h"
#include "GameManager.h"
2002-08-30 04:28:12 +00:00
#include "PrefsManager.h"
2002-09-07 11:45:15 +00:00
#include "StyleDef.h"
#include "GameState.h"
2003-01-16 20:21:31 +00:00
#include "FontCharAliases.h"
2003-02-10 22:59:24 +00:00
#include "TitleSubstitution.h"
2003-06-04 22:42:43 +00:00
#include "BannerCache.h"
#include "Sprite.h"
2003-07-22 07:47:27 +00:00
#include "arch/arch.h"
#include "RageFile.h"
2003-12-21 07:44:41 +00:00
#include "RageFileManager.h"
2004-06-14 00:51:00 +00:00
#include "RageSurface.h"
2003-08-07 06:36:34 +00:00
#include "NoteDataUtil.h"
2003-12-04 20:43:22 +00:00
#include "SDL_utils.h"
2004-02-09 06:26:13 +00:00
#include "ProfileManager.h"
2004-02-16 09:12:59 +00:00
#include "StageStats.h"
#include "StepsUtil.h"
#include "Foreach.h"
2002-06-14 22:25:22 +00:00
2002-09-06 23:36:04 +00:00
#include "NotesLoaderSM.h"
#include "NotesLoaderDWI.h"
#include "NotesLoaderBMS.h"
#include "NotesLoaderKSF.h"
#include "NotesWriterDWI.h"
2002-12-14 00:21:22 +00:00
#include "NotesWriterSM.h"
2001-11-03 10:52:42 +00:00
#include "LyricsLoader.h"
2003-07-28 08:23:29 +00:00
#include <set>
2003-12-10 09:02:55 +00:00
#define CACHE_DIR "Cache/"
2004-05-25 05:38:17 +00:00
const int FILE_CACHE_VERSION = 139; // increment this to invalidate cache
2003-02-19 23:49:57 +00:00
const float DEFAULT_MUSIC_SAMPLE_LENGTH = 12.f;
2001-12-28 10:15:59 +00:00
2001-12-19 01:50:57 +00:00
2002-07-11 19:02:26 +00:00
2002-10-24 07:56:20 +00:00
int CompareBackgroundChanges(const BackgroundChange &seg1, const BackgroundChange &seg2)
2002-07-11 19:02:26 +00:00
{
2002-10-24 07:56:20 +00:00
return seg1.m_fStartBeat < seg2.m_fStartBeat;
2002-07-11 19:02:26 +00:00
}
2003-01-03 05:56:28 +00:00
void SortBackgroundChangesArray( vector<BackgroundChange> &arrayBackgroundChanges )
2002-07-11 19:02:26 +00:00
{
2002-10-24 08:40:27 +00:00
sort( arrayBackgroundChanges.begin(), arrayBackgroundChanges.end(), CompareBackgroundChanges );
2001-12-28 10:15:59 +00:00
}
2001-12-19 01:50:57 +00:00
2001-11-03 10:52:42 +00:00
//////////////////////////////
// Song
//////////////////////////////
Song::Song()
{
2004-02-08 01:05:53 +00:00
m_LoadedFromProfile = PROFILE_SLOT_INVALID;
m_fMusicSampleStartSeconds = -1;
m_fMusicSampleLengthSeconds = DEFAULT_MUSIC_SAMPLE_LENGTH;
2002-06-29 11:59:09 +00:00
m_fMusicLengthSeconds = 0;
2002-07-11 19:02:26 +00:00
m_fFirstBeat = -1;
m_fLastBeat = -1;
2002-08-30 04:28:12 +00:00
m_SelectionDisplay = SHOW_ALWAYS;
m_DisplayBPMType = DISPLAY_ACTUAL;
2003-06-16 17:28:58 +00:00
m_fSpecifiedBPMMin = 0;
m_fSpecifiedBPMMax = 0;
m_bIsSymLink = false;
2003-12-30 03:40:29 +00:00
m_bHasMusic = false;
m_bHasBanner = false;
}
2001-11-03 10:52:42 +00:00
2002-06-14 22:25:22 +00:00
Song::~Song()
{
2004-05-24 03:41:39 +00:00
for( unsigned i=0; i<m_vpSteps.size(); i++ )
SAFE_DELETE( m_vpSteps[i] );
2002-06-14 22:25:22 +00:00
2004-05-24 03:41:39 +00:00
m_vpSteps.clear();
/* We deleted some Steps*; clear stuff that used it. */
SONGMAN->FlushCaches();
2002-06-14 22:25:22 +00:00
}
2003-06-24 20:04:35 +00:00
/* Reset to an empty song. */
void Song::Reset()
{
2004-05-24 03:41:39 +00:00
for( unsigned i=0; i<m_vpSteps.size(); i++ )
SAFE_DELETE( m_vpSteps[i] );
m_vpSteps.clear();
FOREACH_StepsType( st )
m_vpStepsByType[st].clear();
2003-06-24 20:04:35 +00:00
Song empty;
*this = empty;
2003-12-21 02:54:23 +00:00
/* Courses cache Notes* pointers. On the off chance that this isn't the last
* thing this screen does, clear that cache. */
SONGMAN->FlushCaches();
2003-06-24 20:04:35 +00:00
}
2001-12-19 01:50:57 +00:00
void Song::AddBackgroundChange( BackgroundChange seg )
2002-06-24 22:04:31 +00:00
{
2002-10-31 04:23:39 +00:00
m_BackgroundChanges.push_back( seg );
SortBackgroundChangesArray( m_BackgroundChanges );
2002-06-24 22:04:31 +00:00
}
2004-01-07 00:13:32 +00:00
void Song::AddForegroundChange( BackgroundChange seg )
{
m_ForegroundChanges.push_back( seg );
SortBackgroundChangesArray( m_ForegroundChanges );
}
void Song::AddLyricSegment( LyricSegment seg )
{
m_LyricSegments.push_back( seg );
}
void Song::GetDisplayBpms( DisplayBpms &AddTo ) const
2002-08-18 23:20:18 +00:00
{
2003-12-18 04:48:26 +00:00
if( m_DisplayBPMType == DISPLAY_SPECIFIED )
2001-12-28 10:15:59 +00:00
{
AddTo.Add( m_fSpecifiedBPMMin );
AddTo.Add( m_fSpecifiedBPMMax );
}
2003-12-18 04:48:26 +00:00
else
{
float fMinBPM, fMaxBPM;
m_Timing.GetActualBPM( fMinBPM, fMaxBPM );
AddTo.Add( fMinBPM );
AddTo.Add( fMaxBPM );
}
2003-12-18 04:48:26 +00:00
}
2003-12-18 04:48:26 +00:00
CString Song::GetBackgroundAtBeat( float fBeat ) const
{
unsigned i;
for( i=0; i<m_BackgroundChanges.size()-1; i++ )
if( m_BackgroundChanges[i+1].m_fStartBeat > fBeat )
2003-02-09 03:32:04 +00:00
break;
2003-12-18 04:48:26 +00:00
return m_BackgroundChanges[i].m_sBGName;
2002-04-16 17:31:00 +00:00
}
2003-12-18 04:48:26 +00:00
CString Song::GetCacheFilePath() const
2002-05-19 01:59:48 +00:00
{
2003-12-10 09:26:05 +00:00
return ssprintf( CACHE_DIR "Songs/%u", GetHashForString(m_sSongDir) );
2002-05-19 01:59:48 +00:00
}
/* Get a path to the SM containing data for this song. It might
* be a cache file. */
const CString &Song::GetSongFilePath() const
2002-07-02 00:27:58 +00:00
{
ASSERT ( m_sSongFileName.GetLength() != 0 );
return m_sSongFileName;
2002-07-02 00:27:58 +00:00
}
2002-09-11 05:15:46 +00:00
NotesLoader *Song::MakeLoader( CString sDir ) const
2001-11-03 10:52:42 +00:00
{
2002-09-11 05:15:46 +00:00
NotesLoader *ret;
/* Actually, none of these have any persistant data, so we
* could optimize this, but since they don't have any data,
* there's no real point ... */
ret = new SMLoader;
if(ret->Loadable( sDir )) return ret;
delete ret;
2001-11-03 10:52:42 +00:00
2002-09-11 05:15:46 +00:00
ret = new DWILoader;
if(ret->Loadable( sDir )) return ret;
delete ret;
2002-09-11 05:15:46 +00:00
ret = new BMSLoader;
if(ret->Loadable( sDir )) return ret;
delete ret;
2001-11-03 10:52:42 +00:00
2002-09-11 05:15:46 +00:00
ret = new KSFLoader;
if(ret->Loadable( sDir )) return ret;
delete ret;
2001-11-03 10:52:42 +00:00
2002-09-11 05:15:46 +00:00
return NULL;
}
2002-04-16 17:31:00 +00:00
2003-07-28 08:23:29 +00:00
/* Hack: This should be a parameter to TidyUpData, but I don't want to
* pull in <set> into Song.h, which is heavily used. */
static set<istring> BlacklistedImages;
2003-07-28 08:23:29 +00:00
2003-12-30 04:26:39 +00:00
/*
2003-12-31 09:32:21 +00:00
* If PREFSMAN->m_bFastLoad is true, always load from cache if possible. Don't read
* the contents of sDir if we can avoid it. That means we can't call HasMusic(),
2003-12-30 04:26:39 +00:00
* HasBanner() or GetHashForDirectory().
*
* If true, check the directory hash and reload the song from scratch if it's changed.
*/
bool Song::LoadFromSongDir( CString sDir )
{
2003-04-25 00:27:30 +00:00
// LOG->Trace( "Song::LoadFromSongDir(%s)", sDir.c_str() );
2003-06-24 20:04:35 +00:00
ASSERT( sDir != "" );
// make sure there is a trailing slash at the end of sDir
2003-12-10 09:26:05 +00:00
if( sDir.Right(1) != "/" )
sDir += "/";
// save song dir
m_sSongDir = sDir;
// save group name
CStringArray sDirectoryParts;
2003-12-10 09:26:05 +00:00
split( m_sSongDir, "/", sDirectoryParts, false );
ASSERT( sDirectoryParts.size() >= 4 ); /* Songs/Slow/Taps/ */
m_sGroupName = sDirectoryParts[sDirectoryParts.size()-3]; // second from last item
ASSERT( m_sGroupName != "" );
//
// First look in the cache for this song (without loading NoteData)
//
2002-09-07 10:22:49 +00:00
unsigned uDirHash = SONGINDEX->GetCacheHash(m_sSongDir);
2003-12-30 04:26:39 +00:00
bool bUseCache = true;
if( !DoesFileExist(GetCacheFilePath()) )
bUseCache = false;
2003-12-31 09:32:21 +00:00
if( !PREFSMAN->m_bFastLoad && GetHashForDirectory(m_sSongDir) != uDirHash )
2003-12-30 04:26:39 +00:00
bUseCache = false; // this cache is out of date
if( bUseCache )
{
2003-04-25 00:27:30 +00:00
// LOG->Trace( "Loading '%s' from cache file '%s'.", m_sSongDir.c_str(), GetCacheFilePath().c_str() );
2002-09-06 23:36:04 +00:00
SMLoader ld;
ld.LoadFromSMFile( GetCacheFilePath(), *this, true );
}
else
{
//
// There was no entry in the cache for this song, or it was out of date.
// Let's load it from a file, then write a cache entry.
//
NotesLoader *ld = MakeLoader( sDir );
if(!ld)
{
LOG->Warn( "Couldn't find any SM, DWI, BMS, or KSF files in '%s'. This is not a valid song directory.", sDir.c_str() );
return false;
}
bool success = ld->LoadFromDir( sDir, *this );
BlacklistedImages = ld->GetBlacklistedImages();
delete ld;
if(!success)
return false;
TidyUpData();
// save a cache file so we don't have to parse it all over again next time
SaveToCacheFile();
}
2003-06-15 01:53:51 +00:00
/* Load the cached banners, if it's not loaded already. */
2003-12-30 03:40:29 +00:00
if( m_bHasBanner )
2003-06-15 00:57:05 +00:00
BANNERCACHE->LoadBanner( GetBannerPath() );
2003-01-02 22:10:51 +00:00
/* Add AutoGen pointers. (These aren't cached.) */
AddAutoGenNotes();
2003-12-30 03:40:29 +00:00
if( !m_bHasMusic )
return false; // don't load this song
else
return true; // do load this song
2001-11-03 10:52:42 +00:00
}
/* If bAllowNotesLoss is true, any global notes pointers which no longer exist
* (or exist but couldn't be matched) will be set to NULL. This is used when
* reverting out of the editor. If false, this is unexpected and will assert.
* This is used when reverting out of gameplay, in which case we may have StageStats,
* etc. which may cause hard-to-trace crashes down the line if we set them to NULL. */
void Song::RevertFromDisk( bool bAllowNotesLoss )
{
// Ugly: When we re-load the song, the Steps* will change.
// Fix GAMESTATE->m_CurSteps, g_CurStageStats, g_vPlayedStageStats[] after reloading.
/* XXX: This is very brittle. However, we must know about all globals uses of Steps*,
* so we can check to make sure we didn't lose any steps which are referenced ... */
StepsID OldCurSteps[NUM_PLAYERS];
StepsID OldCurStageStats[NUM_PLAYERS];
vector<StepsID> OldPlayedStageStats[NUM_PLAYERS];
2004-06-06 20:57:13 +00:00
FOREACH_PlayerNumber( p )
{
Steps* pCurSteps = GAMESTATE->m_pCurSteps[p];
Steps* pCurStageStats = g_CurStageStats.pSteps[p];
OldCurSteps[p].FromSteps( pCurSteps );
OldCurStageStats[p].FromSteps( pCurStageStats );
for( unsigned i = 0; i < g_vPlayedStageStats.size(); ++i )
{
const StageStats &ss = g_vPlayedStageStats[i];;
OldPlayedStageStats[p].push_back( StepsID() );
OldPlayedStageStats[p][i].FromSteps( ss.pSteps[p] );
}
}
2003-07-02 01:38:00 +00:00
2003-06-24 20:04:35 +00:00
const CString dir = GetSongDir();
/* Erase all existing data. */
Reset();
FILEMAN->FlushDirCache( dir );
const bool OldVal = PREFSMAN->m_bFastLoad;
PREFSMAN->m_bFastLoad = false;
LoadFromSongDir( dir );
/* XXX: reload edits? */
2003-07-02 01:38:00 +00:00
PREFSMAN->m_bFastLoad = OldVal;
2004-06-06 20:57:13 +00:00
FOREACH_PlayerNumber( p )
2003-07-02 01:38:00 +00:00
{
2004-06-06 20:57:13 +00:00
CHECKPOINT;
if( GAMESTATE->m_pCurSong == this )
GAMESTATE->m_pCurSteps[p] = OldCurSteps[p].ToSteps( this, bAllowNotesLoss );
2004-06-06 20:57:13 +00:00
CHECKPOINT;
if( g_CurStageStats.pSong == this )
g_CurStageStats.pSteps[p] = OldCurStageStats[p].ToSteps( this, bAllowNotesLoss );
CHECKPOINT;
for( unsigned i = 0; i < g_vPlayedStageStats.size(); ++i )
2003-07-02 01:38:00 +00:00
{
CHECKPOINT_M(ssprintf("%i", i));
2004-06-06 20:57:13 +00:00
if( g_vPlayedStageStats[i].pSong == this )
g_vPlayedStageStats[i].pSteps[p] = OldPlayedStageStats[p][i].ToSteps( this, bAllowNotesLoss );
2003-07-02 01:38:00 +00:00
}
}
StepsID::FlushCache();
2003-06-24 20:04:35 +00:00
}
2001-12-28 10:15:59 +00:00
2003-06-22 20:59:57 +00:00
static void GetImageDirListing( CString sPath, CStringArray &AddTo, bool bReturnPathToo=false )
{
GetDirListing( sPath + ".png", AddTo, false, bReturnPathToo );
GetDirListing( sPath + ".jpg", AddTo, false, bReturnPathToo );
GetDirListing( sPath + ".bmp", AddTo, false, bReturnPathToo );
GetDirListing( sPath + ".gif", AddTo, false, bReturnPathToo );
}
static CString RemoveInitialWhitespace( CString s )
{
unsigned i = s.find_first_not_of(" \t\r\n");
if( i != s.npos )
s.erase( 0, i );
return s;
}
/* This is called within TidyUpData, before autogen notes are added. */
2004-06-05 05:08:00 +00:00
void Song::DeleteDuplicateSteps( vector<Steps*> &vSteps )
{
/* vSteps have the same StepsType and Difficulty. Delete them if they have the
* same m_sDescription, m_iMeter and SMNoteData. */
CHECKPOINT;
for( unsigned i=0; i<vSteps.size(); i++ )
{
CHECKPOINT;
const Steps *s1 = vSteps[i];
2003-10-04 00:47:57 +00:00
for( unsigned j=i+1; j<vSteps.size(); j++ )
{
CHECKPOINT;
const Steps *s2 = vSteps[j];
2003-10-04 00:47:57 +00:00
if( s1->GetDescription() != s2->GetDescription() )
continue;
if( s1->GetMeter() != s2->GetMeter() )
continue;
/* Compare, ignoring whitespace. */
2003-11-17 03:38:24 +00:00
CString sSMNoteData1, sSMAttackData1;
s1->GetSMNoteData( sSMNoteData1, sSMAttackData1 );
CString sSMNoteData2, sSMAttackData2;
s2->GetSMNoteData( sSMNoteData2, sSMAttackData2 );
if( RemoveInitialWhitespace(sSMNoteData1) != RemoveInitialWhitespace(sSMNoteData2) )
continue;
LOG->Trace("Removed %p duplicate steps in song \"%s\" with description \"%s\" and meter \"%i\"",
2004-06-05 05:08:00 +00:00
s2, this->GetSongDir().c_str(), s1->GetDescription().c_str(), s1->GetMeter() );
2004-05-24 03:41:39 +00:00
/* Don't use RemoveSteps; autogen notes havn't yet been created and it'll
* create them. */
FOREACH_StepsType( st )
{
for( int k=this->m_vpStepsByType[st].size()-1; k>=0; k-- )
{
if( this->m_vpStepsByType[st][k] == s2 )
{
// delete this->m_vpStepsByType[k]; // delete below
this->m_vpStepsByType[st].erase( this->m_vpStepsByType[st].begin()+k );
break;
}
}
}
2004-06-05 05:08:00 +00:00
for( int k=this->m_vpSteps.size()-1; k>=0; k-- )
{
2004-06-05 05:08:00 +00:00
if( this->m_vpSteps[k] == s2 )
{
2004-06-05 05:08:00 +00:00
delete this->m_vpSteps[k];
this->m_vpSteps.erase( this->m_vpSteps.begin()+k );
break;
}
}
vSteps.erase(vSteps.begin()+j);
--j;
}
}
}
/* Make any duplicate difficulties edits. (Note that BMS files do a first pass
* on this; see BMSLoader::SlideDuplicateDifficulties.) */
void Song::AdjustDuplicateSteps()
{
for( int i=0; i<NUM_STEPS_TYPES; i++ )
{
StepsType st = (StepsType)i;
for( unsigned j=0; j<=DIFFICULTY_CHALLENGE; j++ ) // not DIFFICULTY_EDIT
{
Difficulty dc = (Difficulty)j;
vector<Steps*> vSteps;
this->GetSteps( vSteps, st, dc );
/* Delete steps that are completely identical. This happened due to a
* bug in an earlier version. */
2004-06-05 05:08:00 +00:00
DeleteDuplicateSteps( vSteps );
CHECKPOINT;
StepsUtil::SortNotesArrayByDifficulty( vSteps );
CHECKPOINT;
for( unsigned k=1; k<vSteps.size(); k++ )
{
vSteps[k]->SetDifficulty( DIFFICULTY_EDIT );
if( vSteps[k]->GetDescription() == "" )
{
/* "Hard Edit" */
CString EditName = Capitalize( DifficultyToString(dc) ) + " Edit";
vSteps[k]->SetDescription( EditName );
}
}
}
/* XXX: Don't allow edits to have descriptions that look like regular difficulties.
* These are confusing, and they're ambiguous when passed to GetStepsByID. */
}
}
2003-12-11 07:26:59 +00:00
/* Fix up song paths. If there's a leading "./", be sure to keep it: it's
* a signal that the path is from the root directory, not the song directory.
* Other than a leading "./", song paths must never contain "." or "..". */
void FixupPath( CString &path, const CString &sSongPath )
2003-12-11 07:26:59 +00:00
{
/* Replace backslashes with slashes in all paths. */
FixSlashesInPlace( path );
/* Many imported files contain erroneous whitespace before or after
* filenames. Paths usually don't actually start or end with spaces,
* so let's just remove it. */
TrimLeft( path );
TrimRight( path );
}
2003-07-28 08:23:29 +00:00
/* Songs in BlacklistImages will never be autodetected as song images. */
2001-11-25 04:31:44 +00:00
void Song::TidyUpData()
2001-11-03 10:52:42 +00:00
{
2004-04-06 04:06:37 +00:00
/* We need to do this before calling any of HasMusic, HasHasCDTitle, etc. */
ASSERT_M( m_sSongDir.Left(3) != "../", m_sSongDir ); /* meaningless */
FixupPath( m_sSongDir, "" );
FixupPath( m_sMusicFile, m_sSongDir );
FixupPath( m_sBannerFile, m_sSongDir );
FixupPath( m_sLyricsFile, m_sSongDir );
FixupPath( m_sBackgroundFile, m_sSongDir );
FixupPath( m_sCDTitleFile, m_sSongDir );
if( !HasMusic() )
{
CStringArray arrayPossibleMusic;
GetDirListing( m_sSongDir + CString("*.mp3"), arrayPossibleMusic );
GetDirListing( m_sSongDir + CString("*.ogg"), arrayPossibleMusic );
GetDirListing( m_sSongDir + CString("*.wav"), arrayPossibleMusic );
if( !arrayPossibleMusic.empty() )
{
int idx = 0;
/* If the first song is "intro", and we have more than one available,
* don't use it--it's probably a KSF intro music file, which we don't support. */
if( arrayPossibleMusic.size() > 1 &&
!arrayPossibleMusic[0].Left(5).CompareNoCase("intro") )
++idx;
// we found a match
m_sMusicFile = arrayPossibleMusic[idx];
}
}
/* This must be done before radar calculation. */
if( HasMusic() )
{
2004-02-20 05:35:05 +00:00
CString error;
SoundReader *Sample = SoundReader_FileReader::OpenFile( GetMusicPath(), error );
if( Sample == NULL )
{
LOG->Warn( "Error opening sound \"%s\": %s", GetMusicPath().c_str(), error.c_str() );
2003-02-10 21:21:48 +00:00
/* Don't use this file. */
m_sMusicFile = "";
}
else
{
m_fMusicLengthSeconds = Sample->GetLength() / 1000.0f;
delete Sample;
if( m_fMusicLengthSeconds < 0 )
{
/* It failed; bad file or something. It's already logged a warning. */
m_fMusicLengthSeconds = 100; // guess
}
else if( m_fMusicLengthSeconds == 0 )
{
LOG->Warn( "File %s is empty?", GetMusicPath().c_str() );
}
}
}
else // ! HasMusic()
{
m_fMusicLengthSeconds = 100; // guess
2003-09-19 01:06:20 +00:00
LOG->Warn("Song \"%s\" has no music file; guessing at %f seconds", this->GetSongDir().c_str(), m_fMusicLengthSeconds);
}
2003-02-10 21:21:48 +00:00
if(m_fMusicLengthSeconds < 0)
{
2003-09-19 01:06:20 +00:00
LOG->Warn( "File %s has negative length? (%f)", GetMusicPath().c_str(), m_fMusicLengthSeconds );
2003-02-10 21:21:48 +00:00
m_fMusicLengthSeconds = 0;
}
/* Generate these before we autogen notes, so the new notes can inherit
* their source's values. */
ReCalculateRadarValuesAndLastBeat();
2002-10-31 08:05:13 +00:00
TrimRight(m_sMainTitle);
2003-03-04 06:25:11 +00:00
if( m_sMainTitle == "" )
{
/* Use the song directory name. */
CString SongDir = this->GetSongDir();
vector<CString> parts;
2003-12-10 09:26:05 +00:00
split(SongDir, "/", parts);
2003-03-04 06:25:11 +00:00
ASSERT(parts.size() > 0);
NotesLoader::GetMainAndSubTitlesFromFullTitle( parts[parts.size()-1], m_sMainTitle, m_sSubTitle );
}
2002-10-31 08:05:13 +00:00
TrimRight(m_sSubTitle);
2002-04-28 20:42:32 +00:00
if( m_sArtist == "" ) m_sArtist = "Unknown artist";
TranslateTitles();
2003-01-12 04:59:40 +00:00
2003-12-18 04:48:26 +00:00
if( m_Timing.m_BPMSegments.empty() )
2003-03-04 05:59:56 +00:00
{
/* XXX: Once we have a way to display warnings that the user actually
* cares about (unlike most warnings), this should be one of them. */
LOG->Warn( "No BPM segments specified in '%s%s', default provided.",
2003-04-25 00:27:30 +00:00
m_sSongDir.c_str(), m_sSongFileName.c_str() );
2003-03-04 05:59:56 +00:00
2003-12-18 04:48:26 +00:00
m_Timing.AddBPMSegment( BPMSegment(0, 60) );
2003-03-04 05:59:56 +00:00
}
2001-11-03 10:52:42 +00:00
/* Only automatically set the sample time if there was no sample length
* (m_fMusicSampleStartSeconds == -1). */
/* We don't want to test if m_fMusicSampleStartSeconds == 0, since some
* people really do want the sample to start at the very beginning of the song. */
/* Having a sample start of 0 sounds terrible for most songs because because
* of the silence at the beginning. Many of my files have a 0 for the
* sample start that was not manually set, and I assume other people have the same.
* If there are complaints atou manually-set sample start at 0 is being ignored,
* then change this back. -Chris */
if( m_fMusicSampleStartSeconds == -1 ||
m_fMusicSampleStartSeconds == 0 ||
m_fMusicSampleStartSeconds+m_fMusicSampleLengthSeconds > this->m_fMusicLengthSeconds )
2003-02-19 23:49:57 +00:00
{
2002-09-11 06:01:07 +00:00
m_fMusicSampleStartSeconds = this->GetElapsedTimeFromBeat( 100 );
2003-02-19 23:49:57 +00:00
if( m_fMusicSampleStartSeconds+m_fMusicSampleLengthSeconds > this->m_fMusicLengthSeconds )
{
// fix for BAG and other slow songs
2003-02-19 23:49:57 +00:00
int iBeat = (int)(m_fLastBeat/2);
/* Er. I see that this truncates the beat down to a multiple
* of 10, but what's the logic behind doing that? (It'd make
* sense to use a multiple of 4, so we try to line up to a
* measure ...) -glenn */
2003-02-19 23:49:57 +00:00
iBeat = iBeat - iBeat%10;
m_fMusicSampleStartSeconds = this->GetElapsedTimeFromBeat( (float)iBeat );
2003-02-19 23:49:57 +00:00
}
}
2002-09-11 06:01:07 +00:00
2003-01-12 04:24:38 +00:00
/* Some DWIs have lengths in ms when they meant seconds, eg. #SAMPLELENGTH:10;.
* If the sample length is way too short, change it. */
2003-02-19 23:49:57 +00:00
if( m_fMusicSampleLengthSeconds < 3 || m_fMusicSampleLengthSeconds > 30 )
m_fMusicSampleLengthSeconds = DEFAULT_MUSIC_SAMPLE_LENGTH;
2003-01-12 04:24:38 +00:00
2002-07-11 19:02:26 +00:00
//
// Here's the problem: We have a directory full of images. We want to determine which
// image is the banner, which is the background, and which is the CDTitle.
//
CHECKPOINT_M( "Looking for images..." );
2002-07-11 19:02:26 +00:00
//
// First, check the file name for hints.
//
2002-07-03 21:27:26 +00:00
if( !HasBanner() )
2001-11-03 10:52:42 +00:00
{
2003-12-30 03:40:29 +00:00
/* If a nonexistant banner file is specified, and we can't find a replacement,
* don't wipe out the old value. */
// m_sBannerFile = "";
2001-12-19 14:56:22 +00:00
2002-07-11 19:02:26 +00:00
// find an image with "banner" in the file name
2001-11-25 04:31:44 +00:00
CStringArray arrayPossibleBanners;
2003-06-22 20:59:57 +00:00
GetImageDirListing( m_sSongDir + "*banner*", arrayPossibleBanners );
/* Some people do things differently for the sake of being different. Don't
* match eg. abnormal, numbness. */
GetImageDirListing( m_sSongDir + "* BN", arrayPossibleBanners );
if( !arrayPossibleBanners.empty() )
2002-07-11 19:02:26 +00:00
m_sBannerFile = arrayPossibleBanners[0];
}
2001-11-25 04:31:44 +00:00
2002-07-11 19:02:26 +00:00
if( !HasBackground() )
{
// m_sBackgroundFile = "";
2001-12-19 14:56:22 +00:00
2002-07-11 19:02:26 +00:00
// find an image with "bg" or "background" in the file name
CStringArray arrayPossibleBGs;
2003-06-22 20:59:57 +00:00
GetImageDirListing( m_sSongDir + "*bg*", arrayPossibleBGs );
GetImageDirListing( m_sSongDir + "*background*", arrayPossibleBGs );
if( !arrayPossibleBGs.empty() )
2002-07-11 19:02:26 +00:00
m_sBackgroundFile = arrayPossibleBGs[0];
}
2001-12-19 14:56:22 +00:00
2002-07-11 19:02:26 +00:00
if( !HasCDTitle() )
{
// find an image with "cdtitle" in the file name
CStringArray arrayPossibleCDTitles;
2003-06-22 20:59:57 +00:00
GetImageDirListing( m_sSongDir + "*cdtitle*", arrayPossibleCDTitles );
if( !arrayPossibleCDTitles.empty() )
2002-07-11 19:02:26 +00:00
m_sCDTitleFile = arrayPossibleCDTitles[0];
2001-11-03 10:52:42 +00:00
}
2002-01-16 10:01:32 +00:00
2003-03-19 20:01:33 +00:00
if( !HasLyrics() )
{
2003-03-19 20:01:33 +00:00
// Check if there is a lyric file in here
CStringArray arrayLyricFiles;
GetDirListing(m_sSongDir + CString("*.lrc"), arrayLyricFiles );
if( !arrayLyricFiles.empty() )
2003-03-19 20:01:33 +00:00
m_sLyricsFile = arrayLyricFiles[0];
}
2002-07-11 19:02:26 +00:00
//
// Now, For the images we still haven't found, look at the image dimensions of the remaining unclassified images.
//
CStringArray arrayImages;
2003-06-22 20:59:57 +00:00
GetImageDirListing( m_sSongDir + "*", arrayImages );
unsigned i;
for( i=0; i<arrayImages.size(); i++ ) // foreach image
2001-11-03 10:52:42 +00:00
{
if( m_sBannerFile != "" && m_sCDTitleFile != "" && m_sBackgroundFile != "" )
break; /* done */
// ignore DWI "-char" graphics
if( BlacklistedImages.find( arrayImages[i].c_str() ) != BlacklistedImages.end() )
continue; // skip
2002-07-11 19:02:26 +00:00
// Skip any image that we've already classified
2002-07-23 01:41:40 +00:00
if( HasBanner() && stricmp(m_sBannerFile, arrayImages[i])==0 )
2002-07-11 19:02:26 +00:00
continue; // skip
2001-12-19 14:56:22 +00:00
2002-07-23 01:41:40 +00:00
if( HasBackground() && stricmp(m_sBackgroundFile, arrayImages[i])==0 )
2002-07-11 19:02:26 +00:00
continue; // skip
2001-12-19 14:56:22 +00:00
2002-07-23 01:41:40 +00:00
if( HasCDTitle() && stricmp(m_sCDTitleFile, arrayImages[i])==0 )
2002-07-11 19:02:26 +00:00
continue; // skip
2001-11-25 04:31:44 +00:00
2003-07-22 07:47:27 +00:00
CString sPath = m_sSongDir + arrayImages[i];
2004-06-14 01:12:22 +00:00
/* We only care about the dimensions. */
2004-06-14 01:12:22 +00:00
CString error;
RageSurface *img = RageSurfaceUtils::LoadFile( sPath, error, true );
2003-03-05 09:51:32 +00:00
if( !img )
2001-12-19 14:56:22 +00:00
{
2004-06-14 01:12:22 +00:00
LOG->Trace( "Couldn't load '%s': %s", sPath.c_str(), error.c_str() );
2003-03-05 09:51:32 +00:00
continue;
}
2003-03-05 09:51:32 +00:00
const int width = img->w;
const int height = img->h;
2004-06-14 00:51:00 +00:00
delete img;
2003-03-05 09:51:32 +00:00
if( !HasBackground() && width >= 320 && height >= 240 )
{
m_sBackgroundFile = arrayImages[i];
continue;
}
if( !HasBanner() && Sprite::IsDiagonalBanner(width, height) )
{
m_sBannerFile = arrayImages[i];
continue;
}
2003-03-05 09:51:32 +00:00
if( !HasBanner() && 100<=width && width<=320 && 50<=height && height<=240 )
{
m_sBannerFile = arrayImages[i];
continue;
}
2003-06-16 08:03:20 +00:00
/* Some songs have overlarge banners. Check if the ratio is reasonable (over
* 2:1; usually over 3:1), and large (not a cdtitle). */
if( !HasBanner() && width > 200 && float(width) / height > 2.0f )
{
m_sBannerFile = arrayImages[i];
continue;
}
/* Agh. DWI's inline title images are triggering this, resulting in kanji,
* etc., being used as a CDTitle for songs with none. Some sample data
2003-06-04 22:42:43 +00:00
* from random incarnations:
* 42x50 35x50 50x50 144x49
* It looks like ~50 height is what people use to align to DWI's font.
*
* My tallest CDTitle is 44. Let's cut off in the middle and hope for
* the best. */
if( !HasCDTitle() && width<=100 && height<=48 )
2003-03-05 09:51:32 +00:00
{
m_sCDTitleFile = arrayImages[i];
continue;
2001-12-19 14:56:22 +00:00
}
2001-11-03 10:52:42 +00:00
}
2003-12-30 03:40:29 +00:00
/* These will be written to cache, for Song::LoadFromSongDir to use later. */
m_bHasMusic = HasMusic();
m_bHasBanner = HasBanner();
2003-06-04 22:42:43 +00:00
if( HasBanner() )
2003-06-15 00:57:05 +00:00
BANNERCACHE->CacheBanner( GetBannerPath() );
2003-06-04 22:42:43 +00:00
2002-07-03 21:27:26 +00:00
// If no BGChanges are specified and there are movies in the song directory, then assume
// they are DWI style where the movie begins at beat 0.
if( !HasBGChanges() )
2001-11-30 09:38:35 +00:00
{
2002-07-11 19:02:26 +00:00
CStringArray arrayPossibleMovies;
GetDirListing( m_sSongDir + CString("*movie*.avi"), arrayPossibleMovies );
GetDirListing( m_sSongDir + CString("*movie*.mpg"), arrayPossibleMovies );
GetDirListing( m_sSongDir + CString("*movie*.mpeg"), arrayPossibleMovies );
GetDirListing( m_sSongDir + CString("*.avi"), arrayPossibleMovies );
GetDirListing( m_sSongDir + CString("*.mpg"), arrayPossibleMovies );
GetDirListing( m_sSongDir + CString("*.mpeg"), arrayPossibleMovies );
/* Use this->GetBeatFromElapsedTime(0) instead of 0 to start when the
* music starts. */
if( arrayPossibleMovies.size() == 1 )
this->AddBackgroundChange( BackgroundChange(0,arrayPossibleMovies[0],1.f,true,true,false) );
2001-11-30 09:38:35 +00:00
}
2002-05-19 01:59:48 +00:00
2004-03-14 02:25:38 +00:00
/* Don't allow multiple Steps of the same StepsType and Difficulty (except for edits).
* We should be able to use difficulty names as unique identifiers for steps. */
AdjustDuplicateSteps();
2003-12-30 02:46:47 +00:00
{
/* Generated filename; this doesn't always point to a loadable file,
* but instead points to the file we should write changed files to,
* and will always be an .SM.
*
* This is a little tricky. We can't always use the song title directly,
* since it might contain characters we can't store in filenames. Two
* easy options: we could manually filter out invalid characters, or we
* could use the name of the directory, which is always a valid filename
* and should always be the same as the song. The former might not catch
* everything--filename restrictions are platform-specific; we might even
* be on an 8.3 filesystem, so let's do the latter.
*
* We can't rely on searching for other data filenames; it works for DWIs,
* but not KSFs and BMSs.
*
* So, let's do this (by priority):
* 1. If there's an .SM file, use that filename. No reason to use anything
* else; it's the filename in use.
* 2. If there's a .DWI, use it with a changed extension.
* 3. Otherwise, use the name of the directory, since it's definitely a valid
* filename, and should always be the title of the song (unlike KSFs).
*/
m_sSongFileName = m_sSongDir;
CStringArray asFileNames;
GetDirListing( m_sSongDir+"*.sm", asFileNames );
if( !asFileNames.empty() )
m_sSongFileName += asFileNames[0];
else {
GetDirListing( m_sSongDir+"*.dwi", asFileNames );
if( !asFileNames.empty() ) {
m_sSongFileName += SetExtension( asFileNames[0], "sm" );
} else {
m_sSongFileName += Basename(m_sSongDir);
m_sSongFileName += ".sm";
}
}
}
// Compress all Steps
2004-05-24 03:41:39 +00:00
for( i=0; i<m_vpSteps.size(); i++ )
{
2004-05-24 03:41:39 +00:00
m_vpSteps[i]->Compress();
}
}
void Song::TranslateTitles()
{
static TitleSubst tsub("songs");
2003-01-13 06:04:44 +00:00
2004-05-31 00:59:33 +00:00
TitleFields title;
title.LoadFromStrings( m_sMainTitle, m_sSubTitle, m_sArtist, m_sMainTitleTranslit, m_sSubTitleTranslit, m_sArtistTranslit );
tsub.Subst( title );
title.SaveToStrings( m_sMainTitle, m_sSubTitle, m_sArtist, m_sMainTitleTranslit, m_sSubTitleTranslit, m_sArtistTranslit );
2001-11-30 09:38:35 +00:00
}
void Song::ReCalculateRadarValuesAndLastBeat()
{
2004-05-24 03:41:39 +00:00
for( unsigned i=0; i<m_vpSteps.size(); i++ )
{
2004-05-24 03:41:39 +00:00
Steps* pSteps = m_vpSteps[i];
2003-09-21 23:16:44 +00:00
/* If it's autogen, radar vals and first/last beat will come from the parent. */
2004-05-24 03:41:39 +00:00
if( pSteps->IsAutogen() )
2003-09-21 20:57:49 +00:00
continue;
2003-09-21 23:16:44 +00:00
//
// calculate radar values
//
NoteData tempNoteData;
2004-05-24 03:41:39 +00:00
pSteps->GetNoteData( &tempNoteData );
2003-01-30 07:18:33 +00:00
for( int r=0; r<NUM_RADAR_CATEGORIES; r++ )
{
2003-09-21 23:16:44 +00:00
float fVal = NoteDataUtil::GetRadarValue( tempNoteData, (RadarCategory)r, m_fMusicLengthSeconds );
2004-05-24 03:41:39 +00:00
pSteps->SetRadarValue(r, fVal);
}
2003-09-21 23:16:44 +00:00
2003-12-18 04:48:26 +00:00
/* Calculate first/last beat.
*
* Many songs have stray, empty song patterns. Ignore them, so
2003-11-02 04:17:28 +00:00
* they don't force the first beat of the whole song to 0. */
2003-02-01 03:26:19 +00:00
if(tempNoteData.GetLastRow() == 0)
continue;
// Don't set first/last beat based on lights. They often start very
// early and end very late.
2004-05-24 03:41:39 +00:00
if( pSteps->m_StepsType == STEPS_TYPE_LIGHTS_CABINET )
continue;
float fFirstBeat = tempNoteData.GetFirstBeat();
float fLastBeat = tempNoteData.GetLastBeat();
if( m_fFirstBeat == -1 )
m_fFirstBeat = fFirstBeat;
else
m_fFirstBeat = min( m_fFirstBeat, fFirstBeat );
if( m_fLastBeat == -1 )
m_fLastBeat = fLastBeat;
else
m_fLastBeat = max( m_fLastBeat, fLastBeat );
}
}
void Song::GetSteps( vector<Steps*>& arrayAddTo, StepsType st, Difficulty dc, int iMeterLow, int iMeterHigh, const CString &sDescription, bool bIncludeAutoGen, int Max ) const
2002-04-16 17:31:00 +00:00
{
2004-06-05 05:08:00 +00:00
if( !Max )
return;
const vector<Steps*>& vpSteps = GetAllSteps(st);
for( unsigned i=0; i<vpSteps.size(); i++ ) // for each of the Song's Steps
2003-04-20 01:31:13 +00:00
{
Steps* pSteps = vpSteps[i];
2004-04-20 02:35:30 +00:00
if( dc != DIFFICULTY_INVALID && dc != pSteps->GetDifficulty() )
2003-04-20 01:31:13 +00:00
continue;
2004-04-20 02:35:30 +00:00
if( iMeterLow != -1 && iMeterLow > pSteps->GetMeter() )
2003-04-20 01:31:13 +00:00
continue;
2004-04-20 02:35:30 +00:00
if( iMeterHigh != -1 && iMeterHigh < pSteps->GetMeter() )
2003-04-20 01:31:13 +00:00
continue;
2004-06-05 05:08:00 +00:00
if( sDescription.size() && sDescription != pSteps->GetDescription() )
2003-04-20 01:31:13 +00:00
continue;
2004-04-20 02:35:30 +00:00
if( !bIncludeAutoGen && pSteps->IsAutogen() )
2003-04-20 01:31:13 +00:00
continue;
2003-12-21 02:21:30 +00:00
2004-06-06 02:08:48 +00:00
arrayAddTo.push_back( pSteps );
2003-12-21 02:21:30 +00:00
if( Max != -1 )
2004-06-05 05:08:00 +00:00
{
2003-12-21 02:21:30 +00:00
--Max;
2004-06-05 05:08:00 +00:00
if( !Max )
break;
}
2003-04-20 01:31:13 +00:00
}
2003-01-30 07:18:33 +00:00
}
Steps* Song::GetStepsByDifficulty( StepsType st, Difficulty dc, bool bIncludeAutoGen ) const
2003-01-30 07:18:33 +00:00
{
const vector<Steps*>& vpSteps = GetAllSteps(st);
for( unsigned i=0; i<vpSteps.size(); i++ ) // for each of the Song's Steps
{
Steps* pSteps = vpSteps[i];
if( dc != DIFFICULTY_INVALID && dc != pSteps->GetDifficulty() )
continue;
if( !bIncludeAutoGen && pSteps->IsAutogen() )
continue;
return pSteps;
}
return NULL;
}
Steps* Song::GetStepsByMeter( StepsType st, int iMeterLow, int iMeterHigh ) const
{
const vector<Steps*>& vpSteps = GetAllSteps(st);
for( unsigned i=0; i<vpSteps.size(); i++ ) // for each of the Song's Steps
{
Steps* pSteps = vpSteps[i];
if( iMeterLow > pSteps->GetMeter() )
continue;
if( iMeterHigh < pSteps->GetMeter() )
continue;
return pSteps;
}
return NULL;
}
Steps* Song::GetStepsByDescription( StepsType st, CString sDescription ) const
{
2003-08-03 00:13:55 +00:00
vector<Steps*> vNotes;
GetSteps( vNotes, st, DIFFICULTY_INVALID, -1, -1, sDescription );
if( vNotes.size() == 0 )
return NULL;
else
return vNotes[0];
}
Steps* Song::GetClosestNotes( StepsType st, Difficulty dc ) const
{
Difficulty newDC = dc;
2004-05-24 03:41:39 +00:00
Steps* pSteps;
pSteps = GetStepsByDifficulty( st, newDC );
if( pSteps )
return pSteps;
newDC = (Difficulty)(dc-1);
CLAMP( (int&)newDC, 0, NUM_DIFFICULTIES-1 );
2004-05-24 03:41:39 +00:00
pSteps = GetStepsByDifficulty( st, newDC );
if( pSteps )
return pSteps;
newDC = (Difficulty)(dc+1);
CLAMP( (int&)newDC, 0, NUM_DIFFICULTIES-1 );
2004-05-24 03:41:39 +00:00
pSteps = GetStepsByDifficulty( st, newDC );
if( pSteps )
return pSteps;
newDC = (Difficulty)(dc-2);
CLAMP( (int&)newDC, 0, NUM_DIFFICULTIES-1 );
2004-05-24 03:41:39 +00:00
pSteps = GetStepsByDifficulty( st, newDC );
if( pSteps )
return pSteps;
newDC = (Difficulty)(dc+2);
CLAMP( (int&)newDC, 0, NUM_DIFFICULTIES-1 );
2004-05-24 03:41:39 +00:00
pSteps = GetStepsByDifficulty( st, newDC );
return pSteps;
2001-12-28 10:15:59 +00:00
}
/* Return whether the song is playable in the given style. */
bool Song::SongCompleteForStyle( const StyleDef *st ) const
{
2004-06-02 05:52:16 +00:00
return HasStepsType( st->m_StepsType );
}
2004-06-02 05:52:16 +00:00
bool Song::HasStepsType( StepsType st ) const
{
2004-02-10 21:13:51 +00:00
vector<Steps*> add;
GetSteps( add, st, DIFFICULTY_INVALID, -1, -1, "", true, 1 );
2004-02-10 21:13:51 +00:00
return !add.empty();
}
2004-06-02 05:52:16 +00:00
bool Song::HasStepsTypeAndDifficulty( StepsType st, Difficulty dc ) const
{
2004-02-10 21:13:51 +00:00
vector<Steps*> add;
GetSteps( add, st, dc, -1, -1, "", true, 1 );
2004-02-10 21:13:51 +00:00
return !add.empty();
}
2002-09-11 04:49:07 +00:00
void Song::Save()
2002-04-16 17:31:00 +00:00
{
LOG->Trace( "Song::SaveToSongFile()" );
2003-12-21 07:44:41 +00:00
ReCalculateRadarValuesAndLastBeat();
TranslateTitles();
2004-04-16 22:52:30 +00:00
/* Save the new files. These calls make backups on their own. */
2003-12-21 07:44:41 +00:00
SaveToSMFile( GetSongFilePath(), false );
SaveToDWIFile();
SaveToCacheFile();
2003-12-21 07:44:41 +00:00
/* We've safely written our files and created backups. Rename non-SM and non-DWI
2004-04-16 22:52:30 +00:00
* files to avoid confusion. */
CStringArray arrayOldFileNames;
GetDirListing( m_sSongDir + "*.bms", arrayOldFileNames );
GetDirListing( m_sSongDir + "*.ksf", arrayOldFileNames );
for( unsigned i=0; i<arrayOldFileNames.size(); i++ )
2002-07-02 00:27:58 +00:00
{
2003-12-21 07:44:41 +00:00
const CString sOldPath = m_sSongDir + arrayOldFileNames[i];
const CString sNewPath = sOldPath + ".old";
2002-07-02 00:27:58 +00:00
2003-12-21 07:44:41 +00:00
if( !FileCopy( sOldPath, sNewPath ) )
{
LOG->Warn( "Backup of \"%s\" failed", sOldPath.c_str() );
/* Don't remove. */
} else
FILEMAN->Remove( sOldPath );
}
}
2002-07-02 00:27:58 +00:00
void Song::SaveToSMFile( CString sPath, bool bSavingCache )
{
2003-04-25 00:27:30 +00:00
LOG->Trace( "Song::SaveToSMFile('%s')", sPath.c_str() );
2002-04-16 17:31:00 +00:00
2003-12-21 07:44:41 +00:00
/* If the file exists, make a backup. */
if( !bSavingCache && IsAFile(sPath) )
FileCopy( sPath, sPath + ".old" );
2002-12-14 00:21:22 +00:00
NotesWriterSM wr;
wr.Write(sPath, *this, bSavingCache);
2002-02-11 04:46:31 +00:00
}
2003-12-21 07:44:41 +00:00
void Song::SaveToCacheFile()
{
SONGINDEX->AddCacheIndex(m_sSongDir, GetHashForDirectory(m_sSongDir));
SaveToSMFile( GetCacheFilePath(), true );
}
2002-09-11 04:49:07 +00:00
void Song::SaveToDWIFile()
2002-08-17 06:44:04 +00:00
{
2003-11-01 22:16:42 +00:00
const CString sPath = SetExtension( GetSongFilePath(), "dwi" );
LOG->Trace( "Song::SaveToDWIFile(%s)", sPath.c_str() );
2002-08-17 06:44:04 +00:00
2003-12-21 07:44:41 +00:00
/* If the file exists, make a backup. */
if( IsAFile(sPath) )
FileCopy( sPath, sPath + ".old" );
NotesWriterDWI wr;
wr.Write(sPath, *this);
2002-08-17 06:44:04 +00:00
}
void Song::AddAutoGenNotes()
{
bool HasNotes[NUM_STEPS_TYPES];
memset( HasNotes, 0, sizeof(HasNotes) );
2004-05-24 03:41:39 +00:00
for( unsigned i=0; i < m_vpSteps.size(); i++ ) // foreach Steps
{
2004-05-24 03:41:39 +00:00
if( m_vpSteps[i]->IsAutogen() )
continue;
2004-05-24 03:41:39 +00:00
StepsType st = m_vpSteps[i]->m_StepsType;
HasNotes[st] = true;
}
FOREACH_StepsType( stMissing )
{
if( HasNotes[stMissing] )
continue;
/* If m_bAutogenSteps is disabled, only autogen lights. */
if( !PREFSMAN->m_bAutogenSteps && stMissing != STEPS_TYPE_LIGHTS_CABINET )
continue;
2003-08-03 00:13:55 +00:00
// missing Steps of this type
int iNumTracksOfMissing = GAMEMAN->NotesTypeToNumTracks(stMissing);
// look for closest match
StepsType stBestMatch = (StepsType)-1;
int iBestTrackDifference = 10000; // inf
FOREACH_StepsType( st )
{
if( !HasNotes[st] )
2003-12-29 23:02:27 +00:00
continue;
2003-01-02 22:10:51 +00:00
/* has (non-autogen) Steps of this type */
const int iNumTracks = GAMEMAN->NotesTypeToNumTracks(st);
2003-12-29 23:02:27 +00:00
const int iTrackDifference = abs(iNumTracks-iNumTracksOfMissing);
2003-01-02 22:10:51 +00:00
if( iTrackDifference < iBestTrackDifference )
{
stBestMatch = st;
iBestTrackDifference = iTrackDifference;
}
}
if( stBestMatch != -1 )
AutoGen( stMissing, stBestMatch );
}
}
2002-10-11 18:25:15 +00:00
2003-08-07 06:16:17 +00:00
void Song::AutoGen( StepsType ntTo, StepsType ntFrom )
{
2003-01-02 22:10:51 +00:00
// int iNumTracksOfTo = GAMEMAN->NotesTypeToNumTracks(ntTo);
2002-10-11 18:25:15 +00:00
2004-05-24 03:41:39 +00:00
for( unsigned int j=0; j<m_vpSteps.size(); j++ )
{
2004-05-24 03:41:39 +00:00
const Steps* pOriginalNotes = m_vpSteps[j];
2003-08-07 06:36:34 +00:00
if( pOriginalNotes->m_StepsType == ntFrom )
{
2003-08-03 00:13:55 +00:00
Steps* pNewNotes = new Steps;
2003-01-02 22:10:51 +00:00
pNewNotes->AutogenFrom(pOriginalNotes, ntTo);
this->AddSteps( pNewNotes );
}
}
}
2003-01-30 07:18:33 +00:00
void Song::RemoveAutoGenNotes()
{
FOREACH_StepsType(st)
{
for( int j=m_vpStepsByType[st].size()-1; j>=0; j-- )
{
if( m_vpStepsByType[st][j]->IsAutogen() )
{
// delete m_vpSteps[j]; // delete below
m_vpStepsByType[st].erase( m_vpStepsByType[st].begin()+j );
}
}
}
2004-05-24 03:41:39 +00:00
for( int j=m_vpSteps.size()-1; j>=0; j-- )
2003-01-30 07:18:33 +00:00
{
2004-05-24 03:41:39 +00:00
if( m_vpSteps[j]->IsAutogen() )
2003-01-30 07:18:33 +00:00
{
2004-05-24 03:41:39 +00:00
delete m_vpSteps[j];
m_vpSteps.erase( m_vpSteps.begin()+j );
2003-01-30 07:18:33 +00:00
}
}
}
bool Song::IsEasy( StepsType st ) const
2002-08-20 21:00:56 +00:00
{
const Steps* pHardNotes = GetStepsByDifficulty( st, DIFFICULTY_HARD );
2003-02-11 02:20:38 +00:00
// HACK: Looks bizarre to see the easy mark by Legend of MAX.
2003-02-15 04:35:18 +00:00
if( pHardNotes && pHardNotes->GetMeter() > 9 )
2003-02-11 02:20:38 +00:00
return false;
/* The easy marker indicates which songs a beginner, having selected "beginner",
* can play and actually get a very easy song: if there are actual beginner
* steps, or if the light steps are 1- or 2-foot. */
const Steps* pBeginnerNotes = GetStepsByDifficulty( st, DIFFICULTY_BEGINNER );
2003-07-02 01:38:00 +00:00
if( pBeginnerNotes )
return true;
const Steps* pEasyNotes = GetStepsByDifficulty( st, DIFFICULTY_EASY );
if( pEasyNotes && pEasyNotes->GetMeter() == 1 )
return true;
return false;
2002-08-20 21:00:56 +00:00
}
2002-05-19 01:59:48 +00:00
bool Song::IsTutorial() const
{
// A Song is a tutorial song is it has only Beginner steps.
FOREACH_CONST( Steps*, m_vpSteps, s )
{
if( (*s)->m_StepsType == STEPS_TYPE_LIGHTS_CABINET )
continue; // ignore
if( (*s)->GetDifficulty() != DIFFICULTY_BEGINNER )
return false;
}
return true;
}
bool Song::HasEdits( StepsType st ) const
{
2004-05-24 03:41:39 +00:00
for( unsigned i=0; i<m_vpSteps.size(); i++ )
2004-04-23 01:33:08 +00:00
{
2004-05-24 03:41:39 +00:00
Steps* pSteps = m_vpSteps[i];
if( pSteps->m_StepsType == st &&
2004-04-23 01:33:08 +00:00
pSteps->GetDifficulty() == DIFFICULTY_EDIT )
{
return true;
}
}
return false;
}
2004-02-26 20:29:37 +00:00
Song::SelectionDisplay Song::GetDisplayed() const
2002-08-30 04:28:12 +00:00
{
2004-02-26 20:29:37 +00:00
if( !PREFSMAN->m_bHiddenSongs )
return SHOW_ALWAYS;
return m_SelectionDisplay;
2002-08-30 04:28:12 +00:00
}
2002-09-07 11:45:15 +00:00
bool Song::HasMusic() const {return m_sMusicFile != "" && IsAFile(GetMusicPath()); }
bool Song::HasBanner() const {return m_sBannerFile != "" && IsAFile(GetBannerPath()); }
bool Song::HasLyrics() const {return m_sLyricsFile != "" && IsAFile(GetLyricsPath()); }
bool Song::HasBackground() const {return m_sBackgroundFile != "" && IsAFile(GetBackgroundPath()); }
bool Song::HasCDTitle() const {return m_sCDTitleFile != "" && IsAFile(GetCDTitlePath()); }
bool Song::HasBGChanges() const {return !m_BackgroundChanges.empty(); }
2002-09-07 11:45:15 +00:00
2004-04-06 04:39:59 +00:00
CString GetSongAssetPath( CString sPath, const CString &sSongPath )
{
if( sPath == "" )
return "";
/* If there's no path in the file, the file is in the same directory
* as the song. (This is the preferred configuration.) */
if( sPath.Find('/') == -1 )
return sSongPath+sPath;
/* The song contains a path; treat it as relative to the top SM directory. */
if( sPath.Left(3) == "../" )
{
/* The path begins with "../". Resolve it wrt. the song directory. */
sPath = sSongPath + sPath;
}
CollapsePath( sPath );
/* If the path still begins with "../", then there were an unreasonable number
* of them at the beginning of the path. Ignore the path entirely. */
if( sPath.Left(3) == "../" )
return "";
return sPath;
}
2002-09-10 08:39:58 +00:00
2003-12-05 04:38:22 +00:00
/* Note that supplying a path relative to the top-level directory is only for compatibility
* with DWI. We prefer paths relative to the song directory. */
2002-09-10 08:39:58 +00:00
CString Song::GetMusicPath() const
{
2004-04-06 04:39:59 +00:00
return GetSongAssetPath( m_sMusicFile, m_sSongDir );
2002-09-10 08:39:58 +00:00
}
CString Song::GetBannerPath() const
{
2004-04-06 04:39:59 +00:00
return GetSongAssetPath( m_sBannerFile, m_sSongDir );
2002-09-10 08:39:58 +00:00
}
CString Song::GetLyricsPath() const
{
2004-04-06 04:39:59 +00:00
return GetSongAssetPath( m_sLyricsFile, m_sSongDir );
}
2002-09-10 08:39:58 +00:00
CString Song::GetCDTitlePath() const
{
2004-04-06 04:39:59 +00:00
return GetSongAssetPath( m_sCDTitleFile, m_sSongDir );
2002-09-10 08:39:58 +00:00
}
CString Song::GetBackgroundPath() const
2002-09-10 08:39:58 +00:00
{
2004-04-06 04:39:59 +00:00
return GetSongAssetPath( m_sBackgroundFile, m_sSongDir );
2002-09-10 08:39:58 +00:00
}
2002-10-24 07:56:20 +00:00
2003-02-11 23:52:18 +00:00
CString Song::GetDisplayMainTitle() const
2003-01-09 09:01:20 +00:00
{
2003-05-20 04:41:47 +00:00
if(!PREFSMAN->m_bShowNative) return GetTranslitMainTitle();
2003-02-11 23:52:18 +00:00
return m_sMainTitle;
}
2003-01-09 09:01:20 +00:00
2003-02-11 23:52:18 +00:00
CString Song::GetDisplaySubTitle() const
{
2003-05-20 04:41:47 +00:00
if(!PREFSMAN->m_bShowNative) return GetTranslitSubTitle();
2003-02-11 23:52:18 +00:00
return m_sSubTitle;
}
2003-01-09 09:01:20 +00:00
2003-02-11 23:52:18 +00:00
CString Song::GetDisplayArtist() const
{
2003-05-20 04:41:47 +00:00
if(!PREFSMAN->m_bShowNative) return GetTranslitArtist();
2003-02-11 23:52:18 +00:00
return m_sArtist;
}
CString Song::GetFullDisplayTitle() const
{
CString Title = GetDisplayMainTitle();
CString SubTitle = GetDisplaySubTitle();
if(!SubTitle.empty()) Title += " " + SubTitle;
return Title;
}
CString Song::GetFullTranslitTitle() const
{
CString Title = GetTranslitMainTitle();
CString SubTitle = GetTranslitSubTitle();
2003-01-09 09:01:20 +00:00
2003-02-11 23:52:18 +00:00
if(!SubTitle.empty()) Title += " " + SubTitle;
2003-01-09 09:01:20 +00:00
return Title;
}
2004-05-24 03:41:39 +00:00
void Song::AddSteps( Steps* pSteps )
2003-01-30 07:18:33 +00:00
{
2004-05-24 03:41:39 +00:00
m_vpSteps.push_back( pSteps );
ASSERT_M( pSteps->m_StepsType < NUM_STEPS_TYPES, ssprintf("%i", pSteps->m_StepsType) );
m_vpStepsByType[pSteps->m_StepsType].push_back( pSteps );
2003-01-30 07:18:33 +00:00
}
2004-05-24 03:41:39 +00:00
void Song::RemoveSteps( const Steps* pSteps )
2003-01-30 07:18:33 +00:00
{
2003-08-03 00:13:55 +00:00
// Avoid any stale Note::parent pointers by removing all AutoGen'd Steps,
2003-01-30 07:18:33 +00:00
// then adding them again.
RemoveAutoGenNotes();
vector<Steps*> &vpSteps = m_vpStepsByType[pSteps->m_StepsType];
for( int j=vpSteps.size()-1; j>=0; j-- )
{
if( vpSteps[j] == pSteps )
{
// delete vpSteps[j]; // delete below
vpSteps.erase( vpSteps.begin()+j );
break;
}
}
2004-05-24 03:41:39 +00:00
for( int j=m_vpSteps.size()-1; j>=0; j-- )
2003-01-30 07:18:33 +00:00
{
2004-05-24 03:41:39 +00:00
if( m_vpSteps[j] == pSteps )
2003-01-30 07:18:33 +00:00
{
2004-05-24 03:41:39 +00:00
delete m_vpSteps[j];
m_vpSteps.erase( m_vpSteps.begin()+j );
2003-01-30 07:18:33 +00:00
break;
}
}
AddAutoGenNotes();
}
2003-06-16 20:23:07 +00:00
2003-07-09 04:09:35 +00:00
bool Song::Matches(CString sGroup, CString sSong) const
{
if( sGroup.size() && sGroup.CompareNoCase(this->m_sGroupName) != 0)
return false;
CString sDir = this->GetSongDir();
sDir.Replace("\\","/");
CStringArray bits;
split( sDir, "/", bits );
ASSERT(bits.size() >= 2); /* should always have at least two parts */
2003-08-05 01:50:44 +00:00
const CString &sLastBit = bits[bits.size()-1];
2003-07-09 04:09:35 +00:00
// match on song dir or title (ala DWI)
if( !sSong.CompareNoCase(sLastBit) )
return true;
if( !sSong.CompareNoCase(this->GetFullTranslitTitle()) )
return true;
return false;
}
2004-02-08 01:05:53 +00:00
void Song::FreeAllLoadedFromProfiles()
{
2004-06-05 07:51:40 +00:00
for( int s=m_vpSteps.size()-1; s>=0; s-- )
2004-02-08 01:05:53 +00:00
{
2004-05-24 03:41:39 +00:00
Steps* pSteps = m_vpSteps[s];
2004-02-08 01:05:53 +00:00
if( pSteps->WasLoadedFromProfile() )
2004-05-24 03:41:39 +00:00
this->RemoveSteps( pSteps );
2004-02-08 01:05:53 +00:00
}
}
2004-04-23 00:26:51 +00:00
int Song::GetNumStepsLoadedFromProfile( ProfileSlot slot ) const
{
int iCount = 0;
2004-05-24 03:41:39 +00:00
for( unsigned s=0; s<m_vpSteps.size(); s++ )
2004-04-23 00:26:51 +00:00
{
2004-05-24 03:41:39 +00:00
Steps* pSteps = m_vpSteps[s];
2004-04-23 00:26:51 +00:00
if( pSteps->GetLoadedFromProfileSlot() == slot )
iCount++;
}
return iCount;
}
2004-04-23 01:33:08 +00:00
bool Song::IsEditAlreadyLoaded( Steps* pSteps ) const
{
ASSERT( pSteps->GetDifficulty() == DIFFICULTY_EDIT );
2004-05-24 03:41:39 +00:00
for( unsigned i=0; i<m_vpSteps.size(); i++ )
2004-04-23 01:33:08 +00:00
{
2004-05-24 03:41:39 +00:00
Steps* pOther = m_vpSteps[i];
2004-04-23 01:33:08 +00:00
if( pOther->GetDifficulty() == DIFFICULTY_EDIT &&
pOther->m_StepsType == pSteps->m_StepsType &&
pOther->GetHash() == pSteps->GetHash() )
{
return true;
}
}
return false;
}
bool Song::HasSignificantBpmChangesOrStops() const
{
// Don't consider BPM changes that only are only for maintaining sync as
// a real BpmChange.
if( m_DisplayBPMType == DISPLAY_SPECIFIED )
return false;
return m_Timing.HasBpmChangesOrStops();
}
2004-05-31 21:35:31 +00:00
/*
* (c) 2001-2004 Chris Danford, Glenn Maynard
* 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.
*/