Files
itgmania212121/stepmania/src/Song.cpp
T

1474 lines
42 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-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"
#include "SongCacheIndex.h"
#include "GameManager.h"
2002-08-30 04:28:12 +00:00
#include "PrefsManager.h"
2004-06-28 07:26:00 +00:00
#include "Style.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-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"
#include "SongUtil.h"
2005-07-01 04:28:29 +00:00
#include "StepsUtil.h"
#include "Foreach.h"
2005-06-04 21:22:50 +00:00
#include "BackgroundUtil.h"
2005-12-29 02:40:09 +00:00
#include "SpecialFiles.h"
#include "NotesLoader.h"
2002-09-06 23:36:04 +00:00
#include "NotesLoaderSM.h"
#include "NotesWriterDWI.h"
2002-12-14 00:21:22 +00:00
#include "NotesWriterSM.h"
#include "UnlockManager.h"
#include "LyricsLoader.h"
2007-02-03 02:32:57 +00:00
#include <time.h>
2003-07-28 08:23:29 +00:00
#include <set>
#include <float.h>
2003-07-28 08:23:29 +00:00
2007-05-17 03:26:50 +00:00
const int FILE_CACHE_VERSION = 159; // 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
2006-08-17 01:05:00 +00:00
static Preference<float> g_fLongVerSongSeconds( "LongVerSongSeconds", 60*2.5f );
static Preference<float> g_fMarathonVerSongSeconds( "MarathonVerSongSeconds", 60*5.f );
2007-02-03 02:32:57 +00:00
static Preference<bool> g_BackUpAllSongSaves( "BackUpAllSongSaves", false );
2001-12-19 01:50:57 +00:00
2007-05-17 03:26:50 +00:00
static const char *InstrumentTrackNames[] = {
"Guitar",
"Rhythm",
"Bass",
};
XToString( InstrumentTrack );
StringToX( InstrumentTrack );
Song::Song()
{
2005-06-04 21:22:50 +00:00
FOREACH_BackgroundLayer( i )
2006-11-12 03:26:25 +00:00
m_BackgroundChanges[i] = AutoPtrCopyOnWrite<VBackgroundChange>(new VBackgroundChange);
m_ForegroundChanges = AutoPtrCopyOnWrite<VBackgroundChange>(new VBackgroundChange);
2005-06-04 21:22:50 +00:00
2006-10-07 03:32:16 +00:00
m_LoadedFromProfile = ProfileSlot_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;
m_fSpecifiedLastBeat = -1;
2002-08-30 04:28:12 +00:00
m_SelectionDisplay = SHOW_ALWAYS;
m_bEnabled = true;
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-08-12 04:49:15 +00:00
FOREACH( Steps*, m_vpSteps, s )
SAFE_DELETE( *s );
2004-05-24 03:41:39 +00:00
m_vpSteps.clear();
2004-08-12 04:49:15 +00:00
// It's the responsibility of the owner of this Song to make sure
// that all pointers to this Song and its Steps are invalidated.
2002-06-14 22:25:22 +00:00
}
2006-11-12 03:26:25 +00:00
void Song::DetachSteps()
{
m_vpSteps.clear();
FOREACH_ENUM( StepsType, st )
2006-11-12 03:26:25 +00:00
m_vpStepsByType[st].clear();
}
/* Reset to an empty song. */
2003-06-24 20:04:35 +00:00
void Song::Reset()
{
2006-11-12 03:26:25 +00:00
FOREACH( Steps*, m_vpSteps, s )
SAFE_DELETE( *s );
m_vpSteps.clear();
FOREACH_ENUM( StepsType, st )
2006-11-12 03:26:25 +00:00
m_vpStepsByType[st].clear();
2003-06-24 20:04:35 +00:00
Song empty;
2006-11-12 03:26:25 +00:00
*this = empty;
2004-08-12 04:49:15 +00:00
// It's the responsibility of the owner of this Song to make sure
// that all pointers to this Song and its Steps are invalidated.
2003-06-24 20:04:35 +00:00
}
2001-12-19 01:50:57 +00:00
2005-06-04 21:22:50 +00:00
void Song::AddBackgroundChange( BackgroundLayer iLayer, BackgroundChange seg )
2002-06-24 22:04:31 +00:00
{
// Delete old background change at this start beat, if any.
FOREACH( BackgroundChange, GetBackgroundChanges(iLayer), bgc )
{
if( bgc->m_fStartBeat == seg.m_fStartBeat )
{
GetBackgroundChanges(iLayer).erase( bgc );
break;
}
}
2005-06-04 21:22:50 +00:00
ASSERT( iLayer >= 0 && iLayer < NUM_BackgroundLayer );
GetBackgroundChanges(iLayer).push_back( seg );
BackgroundUtil::SortBackgroundChangesArray( GetBackgroundChanges(iLayer) );
2002-06-24 22:04:31 +00:00
}
2004-01-07 00:13:32 +00:00
void Song::AddForegroundChange( BackgroundChange seg )
{
2005-06-04 21:22:50 +00:00
GetForegroundChanges().push_back( seg );
BackgroundUtil::SortBackgroundChangesArray( GetForegroundChanges() );
2004-01-07 00:13:32 +00:00
}
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
}
2005-06-04 21:22:50 +00:00
const BackgroundChange &Song::GetBackgroundAtBeat( BackgroundLayer iLayer, float fBeat ) const
2003-12-18 04:48:26 +00:00
{
2005-06-04 21:22:50 +00:00
for( unsigned i=0; i<GetBackgroundChanges(iLayer).size()-1; i++ )
if( GetBackgroundChanges(iLayer)[i+1].m_fStartBeat > fBeat )
return GetBackgroundChanges(iLayer)[i];
return GetBackgroundChanges(iLayer)[0];
2002-04-16 17:31:00 +00:00
}
2003-12-18 04:48:26 +00:00
2006-01-22 01:00:06 +00:00
RString Song::GetCacheFilePath() const
2002-05-19 01:59:48 +00:00
{
return SongCacheIndex::GetCacheFilePath( "Songs", 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. */
2006-01-22 01:00:06 +00:00
const RString &Song::GetSongFilePath() const
2002-07-02 00:27:58 +00:00
{
2005-12-21 07:50:14 +00:00
ASSERT( !m_sSongFileName.empty() );
return m_sSongFileName;
2002-07-02 00:27:58 +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. */
2006-02-28 23:04:24 +00:00
static set<RString> 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.
*/
2006-11-12 03:26:25 +00:00
bool Song::LoadFromSongDir( RString 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
2006-01-22 01:00:06 +00:00
vector<RString> sDirectoryParts;
2003-12-10 09:26:05 +00:00
split( m_sSongDir, "/", sDirectoryParts, false );
2005-06-03 01:57:10 +00:00
ASSERT( sDirectoryParts.size() >= 4 ); /* e.g. "/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)
//
2006-05-05 00:41:17 +00:00
unsigned uCacheHash = SONGINDEX->GetCacheHash(m_sSongDir);
2006-11-12 03:26:25 +00:00
bool bUseCache = true;
RString sCacheFilePath = GetCacheFilePath();
2007-04-28 08:59:00 +00:00
if( !DoesFileExist(sCacheFilePath) )
2003-12-30 04:26:39 +00:00
bUseCache = false;
2006-05-05 00:41:17 +00:00
if( !PREFSMAN->m_bFastLoad && GetHashForDirectory(m_sSongDir) != uCacheHash )
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() );
2007-04-28 08:59:00 +00:00
SMLoader::LoadFromSMFile( sCacheFilePath, *this, true );
SMLoader::TidyUpData( *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.
//
if( !NotesLoader::LoadFromDir(sDir, *this, BlacklistedImages) )
{
2006-09-24 03:57:26 +00:00
LOG->UserLog( "Song", sDir, "has no SM, DWI, BMS, or KSF files." );
2006-01-22 01:00:06 +00:00
vector<RString> vs;
GetDirListing( sDir + "*.mp3", vs, false, false );
GetDirListing( sDir + "*.ogg", vs, false, false );
bool bHasMusic = !vs.empty();
if( !bHasMusic )
{
2006-09-24 03:57:26 +00:00
LOG->UserLog( "Song", sDir, "has no music file either. Ignoring this song directory." );
return false;
}
// Continue on with a blank Song so that people can make adjustments using the editor.
}
TidyUpData();
// save a cache file so we don't have to parse it all over again next time
if( !SaveToCacheFile() )
sCacheFilePath = RString();
}
FOREACH( Steps*, m_vpSteps, s )
{
2007-04-28 08:59:00 +00:00
(*s)->SetFilename( sCacheFilePath );
/* Compress all Steps. During initial caching, this will remove cached NoteData;
* during cached loads, this will just remove cached SMData. */
(*s)->Compress();
}
2003-06-15 01:53:51 +00:00
/* Load the cached banners, if it's not loaded already. */
2006-10-07 07:43:18 +00:00
if( PREFSMAN->m_BannerCache == BNCACHE_LOW_RES_PRELOAD && 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 )
{
2006-09-24 03:57:26 +00:00
LOG->UserLog( "Song", sDir, "has no music; ignored." );
return false; // don't load this song
}
return true; // do load this song
2001-11-03 10:52:42 +00:00
}
bool Song::ReloadFromSongDir( RString sDir )
{
RemoveAutoGenNotes();
vector<Steps*> vOldSteps = m_vpSteps;
Song copy;
if( !copy.LoadFromSongDir( sDir ) )
return false;
copy.RemoveAutoGenNotes();
*this = copy;
// First we assemble a map to let us easily find the new steps
map<StepsID, Steps*> mNewSteps;
for( vector<Steps*>::const_iterator it = m_vpSteps.begin(); it != m_vpSteps.end(); ++it )
{
StepsID id;
id.FromSteps( *it );
mNewSteps[id] = *it;
}
// Now we wipe out the new pointers, which were shallow copied and not deep copied...
m_vpSteps.clear();
FOREACH_ENUM( StepsType, i )
m_vpStepsByType[i].clear();
// Then we copy as many Steps as possible on top of the old pointers.
// The only pointers that change are pointers to Steps that are not in the
// reverted file, which we delete, and pointers to Steps that are in the
// reverted file but not the original *this, which we create new copies of.
// We have to go through these hoops because many places assume the Steps
// pointers don't change - even though there are other ways they can change,
// such as deleting a Steps via the editor.
for( vector<Steps*>::const_iterator itOld = vOldSteps.begin(); itOld != vOldSteps.end(); ++itOld )
{
StepsID id;
id.FromSteps( *itOld );
map<StepsID, Steps*>::iterator itNew = mNewSteps.find( id );
if( itNew == mNewSteps.end() )
{
// This stepchart didn't exist in the file we reverted from
delete *itOld;
}
else
{
Steps *OldSteps = *itOld;
*OldSteps = *(itNew->second);
AddSteps( OldSteps );
mNewSteps.erase( itNew );
}
}
// The leftovers in the map are steps that didn't exist before we reverted
for( map<StepsID, Steps*>::const_iterator it = mNewSteps.begin(); it != mNewSteps.end(); ++it )
{
Steps *NewSteps = new Steps();
*NewSteps = *(it->second);
AddSteps( NewSteps );
}
AddAutoGenNotes();
return true;
}
2007-05-25 22:13:38 +00:00
static void GetImageDirListing( RString sPath, vector<RString> &AddTo )
2003-06-22 20:59:57 +00:00
{
2007-05-25 22:13:38 +00:00
GetDirListing( sPath + ".png", AddTo, false, false );
GetDirListing( sPath + ".jpg", AddTo, false, false );
GetDirListing( sPath + ".bmp", AddTo, false, false );
GetDirListing( sPath + ".gif", AddTo, false, false );
2003-06-22 20:59:57 +00:00
}
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 "..". */
2006-01-22 01:00:06 +00:00
void FixupPath( RString &path, const RString &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. */
2007-12-01 23:33:38 +00:00
Trim( path );
2003-12-11 07:26:59 +00:00
}
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 );
2007-05-17 03:26:50 +00:00
FOREACH_ENUM( InstrumentTrack, i )
if( !m_sInstrumentTrackFile[i].empty() )
FixupPath( m_sInstrumentTrackFile[i], m_sSongDir );
2004-04-06 04:06:37 +00:00
FixupPath( m_sBannerFile, m_sSongDir );
FixupPath( m_sLyricsFile, m_sSongDir );
FixupPath( m_sBackgroundFile, m_sSongDir );
FixupPath( m_sCDTitleFile, m_sSongDir );
if( !HasMusic() )
{
2006-01-22 01:00:06 +00:00
vector<RString> arrayPossibleMusic;
GetDirListing( m_sSongDir + RString("*.mp3"), arrayPossibleMusic );
GetDirListing( m_sSongDir + RString("*.ogg"), arrayPossibleMusic );
GetDirListing( m_sSongDir + RString("*.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() )
{
2006-01-22 01:00:06 +00:00
RString error;
RageSoundReader *Sample = RageSoundReader_FileReader::OpenFile( GetMusicPath(), error );
2005-03-12 06:28:30 +00:00
/* XXX: Checking if the music file exists eliminates a warning originating from BMS files
* (which have no music file, per se) but it's something of a hack. */
if( Sample == NULL && m_sMusicFile != "" )
{
2006-09-24 03:57:26 +00:00
LOG->UserLog( "Sound file", GetMusicPath(), "couldn't be opened: %s", error.c_str() );
2003-02-10 21:21:48 +00:00
/* Don't use this file. */
m_sMusicFile = "";
}
2005-03-12 06:28:30 +00:00
else if ( Sample != NULL )
{
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 )
{
2006-09-24 03:57:26 +00:00
LOG->UserLog( "Sound file", GetMusicPath(), "is empty." );
}
}
}
else // ! HasMusic()
{
m_fMusicLengthSeconds = 100; // guess
2006-09-24 03:57:26 +00:00
LOG->UserLog( "Song", GetSongDir(), "has no music file; guessing at %f seconds", m_fMusicLengthSeconds );
}
if( m_fMusicLengthSeconds < 0 )
2003-02-10 21:21:48 +00:00
{
2006-09-24 03:57:26 +00:00
LOG->UserLog( "Sound file", GetMusicPath(), "has a negative length %f.", 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();
2007-12-01 23:33:38 +00:00
Trim( m_sMainTitle );
Trim( m_sSubTitle );
Trim( m_sArtist );
2004-07-12 20:46:20 +00:00
/* Fall back on the song directory name. */
2003-03-04 06:25:11 +00:00
if( m_sMainTitle == "" )
2004-07-12 20:46:20 +00:00
NotesLoader::GetMainAndSubTitlesFromFullTitle( Basename(this->GetSongDir()), m_sMainTitle, m_sSubTitle );
2003-03-04 06:25:11 +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
{
2009-02-26 23:21:02 +00:00
LOG->UserLog( "Song file", m_sSongDir + m_sSongFileName, "has no BPM segments, default provided." );
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
}
/* Make sure the first BPM segment starts at beat 0. */
2007-04-25 22:42:40 +00:00
if( m_Timing.m_BPMSegments[0].m_iStartRow != 0 )
m_Timing.m_BPMSegments[0].m_iStartRow = 0;
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 )
{
int iBeat = lrintf( m_fLastBeat/2 );
iBeat -= iBeat%4;
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
2006-01-22 01:00:06 +00:00
vector<RString> 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
2006-01-22 01:00:06 +00:00
vector<RString> 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
2006-01-22 01:00:06 +00:00
vector<RString> 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
2006-01-22 01:00:06 +00:00
vector<RString> arrayLyricFiles;
GetDirListing(m_sSongDir + RString("*.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.
//
2006-01-22 01:00:06 +00:00
vector<RString> arrayImages;
2003-06-22 20:59:57 +00:00
GetImageDirListing( m_sSongDir + "*", arrayImages );
2004-09-21 07:53:39 +00:00
for( unsigned i=0; i<arrayImages.size(); i++ ) // foreach image
2001-11-03 10:52:42 +00:00
{
if( HasBanner() && HasCDTitle() && HasBackground() )
break; /* done */
// ignore DWI "-char" graphics
2006-02-28 23:04:24 +00:00
RString sLower = arrayImages[i];
2006-03-01 00:39:32 +00:00
sLower.MakeLower();
2006-02-28 23:04:24 +00:00
if( BlacklistedImages.find(sLower) != 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
2006-01-22 01:00:06 +00:00
RString sPath = m_sSongDir + arrayImages[i];
2004-06-14 01:12:22 +00:00
/* We only care about the dimensions. */
2006-01-22 01:00:06 +00:00
RString error;
2004-06-14 01:12:22 +00:00
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
{
2006-09-24 03:57:26 +00:00
LOG->UserLog( "Graphic file", sPath, "couldn't be loaded: %s", 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
{
2006-01-22 01:00:06 +00:00
vector<RString> arrayPossibleMovies;
GetDirListing( m_sSongDir + RString("*.avi"), arrayPossibleMovies );
GetDirListing( m_sSongDir + RString("*.mpg"), arrayPossibleMovies );
GetDirListing( m_sSongDir + RString("*.mpeg"), arrayPossibleMovies );
/* Use this->GetBeatFromElapsedTime(0) instead of 0 to start when the
* music starts. */
if( arrayPossibleMovies.size() == 1 )
2005-06-04 21:22:50 +00:00
this->AddBackgroundChange( BACKGROUND_LAYER_1, BackgroundChange(0,arrayPossibleMovies[0],"",1.f,SBE_StretchNoLoop) );
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. */
SongUtil::AdjustDuplicateSteps( this );
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;
2006-01-22 01:00:06 +00:00
vector<RString> asFileNames;
2003-12-30 02:46:47 +00:00
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";
}
}
}
/* If no time signature specified, assume 4/4 time for the whole song. */
if( m_Timing.m_vTimeSignatureSegments.empty() )
{
TimeSignatureSegment seg;
seg.m_iStartRow = 0;
seg.m_iNumerator = 4;
seg.m_iDenominator = 4;
m_Timing.m_vTimeSignatureSegments.push_back( seg );
}
}
void Song::TranslateTitles()
{
2005-10-06 07:01:58 +00:00
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()
{
float fFirstBeat = FLT_MAX; /* inf */
float fLastBeat = m_fSpecifiedLastBeat; // Make sure we're at least as long as the specified amount.
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
pSteps->CalculateRadarValues( m_fMusicLengthSeconds );
//
// calculate lastBeat
//
/* If it's autogen, then 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;
/* Don't calculate with edits. Otherwise, edits installed on the machine could
* extend the length of the song. */
if( pSteps->IsAnEdit() )
continue;
// Don't set first/last beat based on lights. They often start very
// early and end very late.
2008-12-21 01:53:48 +00:00
if( pSteps->m_StepsType == StepsType_lights_cabinet )
continue;
2003-09-21 23:16:44 +00:00
NoteData tempNoteData;
2004-10-23 17:43:49 +00:00
pSteps->GetNoteData( tempNoteData );
/* 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. */
if( tempNoteData.GetLastRow() == 0 )
2003-02-01 03:26:19 +00:00
continue;
fFirstBeat = min( fFirstBeat, tempNoteData.GetFirstBeat() );
fLastBeat = max( fLastBeat, tempNoteData.GetLastBeat() );
}
m_fFirstBeat = fFirstBeat;
m_fLastBeat = fLastBeat;
}
/* Return whether the song is playable in the given style. */
2004-06-28 07:26:00 +00:00
bool Song::SongCompleteForStyle( const Style *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
{
return SongUtil::GetOneSteps( this, st ) != NULL;
}
2004-06-02 05:52:16 +00:00
bool Song::HasStepsTypeAndDifficulty( StepsType st, Difficulty dc ) const
{
return SongUtil::GetOneSteps( this, st, dc ) != NULL;
}
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. */
if( !SaveToSMFile(GetSongFilePath(), false) )
return;
2003-12-21 07:44:41 +00:00
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. */
2006-01-22 01:00:06 +00:00
vector<RString> 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
{
2006-01-22 01:00:06 +00:00
const RString sOldPath = m_sSongDir + arrayOldFileNames[i];
const RString sNewPath = sOldPath + ".old";
2002-07-02 00:27:58 +00:00
2003-12-21 07:44:41 +00:00
if( !FileCopy( sOldPath, sNewPath ) )
{
2006-09-24 03:57:26 +00:00
LOG->UserLog( "Song file", sOldPath, "couldn't be backed up." );
2003-12-21 07:44:41 +00:00
/* Don't remove. */
}
else
2003-12-21 07:44:41 +00:00
FILEMAN->Remove( sOldPath );
}
}
2002-07-02 00:27:58 +00:00
2007-02-03 00:13:35 +00:00
bool Song::SaveToSMFile( RString 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" );
vector<Steps*> vpStepsToSave;
FOREACH_CONST( Steps*, m_vpSteps, s )
{
Steps *pSteps = *s;
if( pSteps->IsAutogen() )
continue; /* don't write autogen notes */
/* Only save steps that weren't loaded from a profile. */
if( pSteps->WasLoadedFromProfile() )
continue;
vpStepsToSave.push_back( pSteps );
}
if( !NotesWriterSM::Write(sPath, *this, vpStepsToSave, bSavingCache) )
return false;
2007-02-03 02:32:57 +00:00
if( !bSavingCache && g_BackUpAllSongSaves.Get() )
{
RString sExt = GetExtension( sPath );
RString sBackupFile = SetExtension( sPath, "" );
time_t cur_time;
time( &cur_time );
struct tm now;
localtime_r( &cur_time, &now );
sBackupFile += ssprintf( "-%04i-%02i-%02i--%02i-%02i-%02i",
1900+now.tm_year, now.tm_mon+1, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec );
sBackupFile = SetExtension( sBackupFile, sExt );
sBackupFile += ssprintf( ".old" );
if( FileCopy(sPath, sBackupFile) )
LOG->Trace( "Backed up %s to %s", sPath.c_str(), sBackupFile.c_str() );
else
LOG->Trace( "Failed to back up %s to %s", sPath.c_str(), sBackupFile.c_str() );
}
if( !bSavingCache )
{
/* Mark these steps saved to disk. */
FOREACH( Steps*, vpStepsToSave, s )
(*s)->SetSavedToDisk( true );
}
return true;
2002-02-11 04:46:31 +00:00
}
2007-02-03 00:13:35 +00:00
bool Song::SaveToCacheFile()
2003-12-21 07:44:41 +00:00
{
SONGINDEX->AddCacheIndex(m_sSongDir, GetHashForDirectory(m_sSongDir));
const RString sPath = GetCacheFilePath();
2007-02-03 00:13:35 +00:00
if( !SaveToSMFile(sPath, true) )
return false;
FOREACH( Steps*, m_vpSteps, pSteps )
(*pSteps)->SetFilename( sPath );
2007-02-03 00:13:35 +00:00
return true;
2003-12-21 07:44:41 +00:00
}
2007-02-03 00:13:35 +00:00
bool Song::SaveToDWIFile()
2002-08-17 06:44:04 +00:00
{
2006-01-22 01:00:06 +00:00
const RString sPath = SetExtension( GetSongFilePath(), "dwi" );
2003-11-01 22:16:42 +00:00
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" );
2007-02-03 00:13:35 +00:00
return NotesWriterDWI::Write( sPath, *this );
2002-08-17 06:44:04 +00:00
}
void Song::AddAutoGenNotes()
{
2006-09-26 20:22:28 +00:00
bool HasNotes[NUM_StepsType];
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_ENUM( StepsType, stMissing )
{
if( HasNotes[stMissing] )
continue;
/* If m_bAutogenSteps is disabled, only autogen lights. */
2008-12-21 01:53:48 +00:00
if( !PREFSMAN->m_bAutogenSteps && stMissing != StepsType_lights_cabinet )
continue;
if( !GameManager::GetStepsTypeInfo(stMissing).bAllowAutogen )
continue;
2003-08-03 00:13:55 +00:00
// missing Steps of this type
int iNumTracksOfMissing = GameManager::GetStepsTypeInfo(stMissing).iNumTracks;
// look for closest match
2006-10-05 07:33:47 +00:00
StepsType stBestMatch = StepsType_Invalid;
int iBestTrackDifference = INT_MAX;
FOREACH_ENUM( 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 = GameManager::GetStepsTypeInfo(st).iNumTracks;
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;
}
}
2006-10-05 07:33:47 +00:00
if( stBestMatch != StepsType_Invalid )
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 )
{
2008-03-24 12:50:16 +00:00
// int iNumTracksOfTo = GameManager::StepsTypeToNumTracks(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;
pNewNotes->AutogenFrom( pOriginalNotes, ntTo );
this->AddSteps( pNewNotes );
}
}
}
2003-01-30 07:18:33 +00:00
void Song::RemoveAutoGenNotes()
{
FOREACH_ENUM( 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
{
2004-10-10 14:36:11 +00:00
/* Very fast songs and songs with wide tempo changes are hard for new players,
* even if they have beginner steps. */
DisplayBpms bpms;
this->GetDisplayBpms(bpms);
if( bpms.GetMax() >= 250 || bpms.GetMax() - bpms.GetMin() >= 75 )
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 = SongUtil::GetStepsByDifficulty( this, st, Difficulty_Beginner );
2003-07-02 01:38:00 +00:00
if( pBeginnerNotes )
return true;
const Steps* pEasyNotes = SongUtil::GetStepsByDifficulty( this, 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 )
{
2008-12-21 01:53:48 +00:00
if( (*s)->m_StepsType == StepsType_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 &&
pSteps->GetDifficulty() == Difficulty_Edit )
2004-04-23 01:33:08 +00:00
{
return true;
}
}
return false;
}
/* Return false if the song should not be displayed for selection in normal
* gameplay (but may still be available in random selection, during extra
* stages, or in other special conditions). */
bool Song::NormallyDisplayed() const
2002-08-30 04:28:12 +00:00
{
return UNLOCKMAN == NULL || !UNLOCKMAN->SongIsLocked(this);
2002-08-30 04:28:12 +00:00
}
bool Song::ShowInDemonstrationAndRanking() const { return !IsTutorial() && NormallyDisplayed(); }
2004-07-25 20:51:44 +00:00
/* Hack: see Song::TidyUpData comments. */
bool Song::HasMusic() const
{
/* If we have keys, we always have music. */
if( m_vsKeysoundFile.size() != 0 )
return true;
return m_sMusicFile != "" && IsAFile(GetMusicPath());
}
2007-04-24 03:15:00 +00:00
bool Song::HasBanner() const {return m_sBannerFile != "" && IsAFile(GetBannerPath()); }
2007-05-17 03:26:50 +00:00
bool Song::HasInstrumentTrack( InstrumentTrack it ) const
{
return m_sInstrumentTrackFile[it] != "" && IsAFile(GetInstrumentTrackPath(it));
}
2007-04-24 03:15:00 +00:00
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()); }
2005-05-26 09:35:57 +00:00
bool Song::HasBGChanges() const
{
2005-06-04 21:22:50 +00:00
FOREACH_BackgroundLayer( i )
2005-05-26 09:35:57 +00:00
{
2005-06-04 21:22:50 +00:00
if( !GetBackgroundChanges(i).empty() )
2005-05-26 09:35:57 +00:00
return true;
}
return false;
}
2002-09-07 11:45:15 +00:00
2005-06-04 21:22:50 +00:00
const vector<BackgroundChange> &Song::GetBackgroundChanges( BackgroundLayer bl ) const
{
return *(m_BackgroundChanges[bl]);
}
vector<BackgroundChange> &Song::GetBackgroundChanges( BackgroundLayer bl )
{
2006-11-12 03:26:25 +00:00
return *(m_BackgroundChanges[bl].Get());
2005-06-04 21:22:50 +00:00
}
const vector<BackgroundChange> &Song::GetForegroundChanges() const
{
return *m_ForegroundChanges;
}
vector<BackgroundChange> &Song::GetForegroundChanges()
{
2006-11-12 03:26:25 +00:00
return *m_ForegroundChanges.Get();
2005-06-04 21:22:50 +00:00
}
2006-01-22 01:00:06 +00:00
RString GetSongAssetPath( RString sPath, const RString &sSongPath )
2004-04-06 04:39:59 +00:00
{
if( sPath == "" )
2006-01-22 01:00:06 +00:00
return RString();
2004-04-06 04:39:59 +00:00
/* If there's no path in the file, the file is in the same directory
* as the song. (This is the preferred configuration.) */
2005-12-21 08:43:44 +00:00
if( sPath.find('/') == string::npos )
2004-04-06 04:39:59 +00:00
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) == "../" )
2006-01-22 01:00:06 +00:00
return RString();
2004-04-06 04:39:59 +00:00
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. */
2006-01-22 01:00:06 +00:00
RString Song::GetMusicPath() const
2002-09-10 08:39:58 +00:00
{
2004-04-06 04:39:59 +00:00
return GetSongAssetPath( m_sMusicFile, m_sSongDir );
2002-09-10 08:39:58 +00:00
}
2007-05-17 03:26:50 +00:00
RString Song::GetInstrumentTrackPath( InstrumentTrack it ) const
2007-04-24 03:15:00 +00:00
{
2007-05-17 03:26:50 +00:00
return GetSongAssetPath( m_sInstrumentTrackFile[it], m_sSongDir );
2007-04-24 03:15:00 +00:00
}
2006-01-22 01:00:06 +00:00
RString Song::GetBannerPath() const
2002-09-10 08:39:58 +00:00
{
2004-04-06 04:39:59 +00:00
return GetSongAssetPath( m_sBannerFile, m_sSongDir );
2002-09-10 08:39:58 +00:00
}
2006-01-22 01:00:06 +00:00
RString Song::GetLyricsPath() const
{
2004-04-06 04:39:59 +00:00
return GetSongAssetPath( m_sLyricsFile, m_sSongDir );
}
2006-01-22 01:00:06 +00:00
RString Song::GetCDTitlePath() const
2002-09-10 08:39:58 +00:00
{
2004-04-06 04:39:59 +00:00
return GetSongAssetPath( m_sCDTitleFile, m_sSongDir );
2002-09-10 08:39:58 +00:00
}
2006-01-22 01:00:06 +00:00
RString 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
2006-01-22 01:00:06 +00:00
RString Song::GetDisplayMainTitle() const
2003-01-09 09:01:20 +00:00
{
2004-12-04 06:09:30 +00:00
if(!PREFSMAN->m_bShowNativeLanguage) return GetTranslitMainTitle();
2003-02-11 23:52:18 +00:00
return m_sMainTitle;
}
2003-01-09 09:01:20 +00:00
2006-01-22 01:00:06 +00:00
RString Song::GetDisplaySubTitle() const
2003-02-11 23:52:18 +00:00
{
2004-12-04 06:09:30 +00:00
if(!PREFSMAN->m_bShowNativeLanguage) return GetTranslitSubTitle();
2003-02-11 23:52:18 +00:00
return m_sSubTitle;
}
2003-01-09 09:01:20 +00:00
2006-01-22 01:00:06 +00:00
RString Song::GetDisplayArtist() const
2003-02-11 23:52:18 +00:00
{
2004-12-04 06:09:30 +00:00
if(!PREFSMAN->m_bShowNativeLanguage) return GetTranslitArtist();
2003-02-11 23:52:18 +00:00
return m_sArtist;
}
2006-01-22 01:00:06 +00:00
RString Song::GetDisplayFullTitle() const
2003-02-11 23:52:18 +00:00
{
2006-01-22 01:00:06 +00:00
RString Title = GetDisplayMainTitle();
RString SubTitle = GetDisplaySubTitle();
2003-02-11 23:52:18 +00:00
if(!SubTitle.empty()) Title += " " + SubTitle;
return Title;
}
2006-01-22 01:00:06 +00:00
RString Song::GetTranslitFullTitle() const
2003-02-11 23:52:18 +00:00
{
2006-01-22 01:00:06 +00:00
RString Title = GetTranslitMainTitle();
RString 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 );
2006-09-26 20:22:28 +00:00
ASSERT_M( pSteps->m_StepsType < NUM_StepsType, ssprintf("%i", pSteps->m_StepsType) );
m_vpStepsByType[pSteps->m_StepsType].push_back( pSteps );
2003-01-30 07:18:33 +00:00
}
void Song::DeleteSteps( const Steps* pSteps, bool bReAutoGen )
2003-01-30 07:18:33 +00:00
{
ASSERT( !pSteps->IsAutogen() );
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.
if( bReAutoGen )
RemoveAutoGenNotes();
2003-01-30 07:18:33 +00:00
2006-11-12 03:26:25 +00:00
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;
}
}
if( bReAutoGen )
AddAutoGenNotes();
2003-01-30 07:18:33 +00:00
}
2003-06-16 20:23:07 +00:00
2006-01-22 01:00:06 +00:00
bool Song::Matches(RString sGroup, RString sSong) const
2003-07-09 04:09:35 +00:00
{
if( sGroup.size() && sGroup.CompareNoCase(this->m_sGroupName) != 0)
return false;
2006-01-22 01:00:06 +00:00
RString sDir = this->GetSongDir();
2003-07-09 04:09:35 +00:00
sDir.Replace("\\","/");
2006-01-22 01:00:06 +00:00
vector<RString> bits;
2003-07-09 04:09:35 +00:00
split( sDir, "/", bits );
ASSERT(bits.size() >= 2); /* should always have at least two parts */
2006-01-22 01:00:06 +00:00
const RString &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;
2005-05-23 00:38:09 +00:00
if( !sSong.CompareNoCase(this->GetTranslitFullTitle()) )
2003-07-09 04:09:35 +00:00
return true;
return false;
}
/* If apInUse is set, it contains a list of steps which are in use elsewhere, and
* should not be deleted. */
void Song::FreeAllLoadedFromProfile( ProfileSlot slot, const set<Steps*> *setInUse )
2004-02-08 01:05:53 +00:00
{
/* DeleteSteps will remove and recreate autogen notes, which may reorder
* m_vpSteps, so be careful not to skip over entries. */
vector<Steps*> apToRemove;
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];
if( !pSteps->WasLoadedFromProfile() )
continue;
if( slot != ProfileSlot_Invalid && pSteps->GetLoadedFromProfileSlot() != slot )
continue;
if( setInUse != NULL && setInUse->find(pSteps) != setInUse->end() )
continue;
apToRemove.push_back( pSteps );
2004-02-08 01:05:53 +00:00
}
for( unsigned i = 0; i < apToRemove.size(); ++i )
2005-07-29 02:23:02 +00:00
this->DeleteSteps( apToRemove[i] );
2004-02-08 01:05:53 +00:00
}
2005-11-30 22:42:28 +00:00
void Song::GetStepsLoadedFromProfile( ProfileSlot slot, vector<Steps*> &vpStepsOut ) const
{
for( unsigned s=0; s<m_vpSteps.size(); s++ )
{
Steps* pSteps = m_vpSteps[s];
if( pSteps->GetLoadedFromProfileSlot() == slot )
vpStepsOut.push_back( pSteps );
}
}
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-04-23 01:33:08 +00:00
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];
if( pOther->GetDifficulty() == Difficulty_Edit &&
2004-04-23 01:33:08 +00:00
pOther->m_StepsType == pSteps->m_StepsType &&
pOther->GetHash() == pSteps->GetHash() )
{
return true;
}
}
return false;
}
bool Song::HasSignificantBpmChangesOrStops() const
{
if( m_Timing.HasStops() )
return true;
// Don't consider BPM changes that only are only for maintaining sync as
// a real BpmChange.
if( m_DisplayBPMType == DISPLAY_SPECIFIED )
{
if( m_fSpecifiedBPMMin != m_fSpecifiedBPMMax )
return true;
}
else if( m_Timing.HasBpmChanges() )
{
return true;
}
return false;
}
2004-05-31 21:35:31 +00:00
2005-04-27 21:46:35 +00:00
float Song::GetStepsSeconds() const
{
return GetElapsedTimeFromBeat( m_fLastBeat ) - GetElapsedTimeFromBeat( m_fFirstBeat );
}
2006-08-17 00:27:58 +00:00
bool Song::IsLong() const
{
2006-08-17 01:05:00 +00:00
return !IsMarathon() && m_fMusicLengthSeconds > g_fLongVerSongSeconds;
2006-08-17 00:27:58 +00:00
}
bool Song::IsMarathon() const
{
2006-08-17 01:05:00 +00:00
return m_fMusicLengthSeconds >= g_fMarathonVerSongSeconds;
2006-08-17 00:27:58 +00:00
}
2005-02-22 23:06:51 +00:00
// lua start
#include "LuaBinding.h"
2005-06-20 05:02:03 +00:00
class LunaSong: public Luna<Song>
2005-02-22 23:06:51 +00:00
{
public:
2005-05-23 00:38:09 +00:00
static int GetDisplayFullTitle( T* p, lua_State *L ) { lua_pushstring(L, p->GetDisplayFullTitle() ); return 1; }
static int GetTranslitFullTitle( T* p, lua_State *L ) { lua_pushstring(L, p->GetTranslitFullTitle() ); return 1; }
static int GetDisplayMainTitle( T* p, lua_State *L ) { lua_pushstring(L, p->GetDisplayMainTitle() ); return 1; }
static int GetTranslitMainTitle( T* p, lua_State *L ) { lua_pushstring(L, p->GetTranslitMainTitle() ); return 1; }
static int GetDisplaySubTitle( T* p, lua_State *L ) { lua_pushstring(L, p->GetDisplaySubTitle() ); return 1; }
static int GetTranslitSubTitle( T* p, lua_State *L ) { lua_pushstring(L, p->GetTranslitSubTitle() ); return 1; }
2006-05-01 12:45:42 +00:00
static int GetDisplayArtist( T* p, lua_State *L ) { lua_pushstring(L, p->GetDisplayArtist() ); return 1; }
static int GetTranslitArtist( T* p, lua_State *L ) { lua_pushstring(L, p->GetTranslitArtist() ); return 1; }
static int GetGenre( T* p, lua_State *L ) { lua_pushstring(L, p->m_sGenre ); return 1; }
2005-02-22 23:06:51 +00:00
static int GetAllSteps( T* p, lua_State *L )
{
const vector<Steps*> &v = p->GetAllSteps();
LuaHelpers::CreateTableFromArray<Steps*>( v, L );
2005-02-22 23:06:51 +00:00
return 1;
}
2005-02-23 22:14:58 +00:00
static int GetStepsByStepsType( T* p, lua_State *L )
{
2006-09-27 06:09:52 +00:00
StepsType st = Enum::Check<StepsType>(L, 1);
2005-02-23 22:14:58 +00:00
const vector<Steps*> &v = p->GetStepsByStepsType( st );
LuaHelpers::CreateTableFromArray<Steps*>( v, L );
2005-02-23 22:14:58 +00:00
return 1;
}
2006-05-01 12:45:42 +00:00
static int GetSongDir( T* p, lua_State *L ) { lua_pushstring(L, p->GetSongDir() ); return 1; }
static int GetBannerPath( T* p, lua_State *L ) { RString s = p->GetBannerPath(); if( s.empty() ) return 0; LuaHelpers::Push(L, s); return 1; }
static int GetBackgroundPath( T* p, lua_State *L ) { RString s = p->GetBackgroundPath(); if( s.empty() ) return 0; lua_pushstring(L, s); return 1; }
2006-05-01 12:45:42 +00:00
static int IsTutorial( T* p, lua_State *L ) { lua_pushboolean(L, p->IsTutorial()); return 1; }
static int GetGroupName( T* p, lua_State *L ) { lua_pushstring(L, p->m_sGroupName); return 1; }
2006-08-17 00:27:58 +00:00
static int MusicLengthSeconds( T* p, lua_State *L ) { lua_pushnumber(L, p->m_fMusicLengthSeconds); return 1; }
static int IsLong( T* p, lua_State *L ) { lua_pushboolean(L, p->IsLong()); return 1; }
static int IsMarathon( T* p, lua_State *L ) { lua_pushboolean(L, p->IsMarathon()); return 1; }
2006-08-20 04:40:32 +00:00
static int HasStepsTypeAndDifficulty( T* p, lua_State *L )
{
2006-09-27 06:09:52 +00:00
StepsType st = Enum::Check<StepsType>(L, 1);
2006-09-26 05:04:14 +00:00
Difficulty dc = Enum::Check<Difficulty>( L, 2 );
2006-08-20 04:40:32 +00:00
lua_pushboolean( L, p->HasStepsTypeAndDifficulty(st, dc) );
return 1;
}
2007-02-09 03:23:24 +00:00
// TODO: HasStepsTypeAndDifficulty and GetOneSteps should be in a SongUtil Lua table and a method of Steps.
static int GetOneSteps( T* p, lua_State *L )
{
StepsType st = Enum::Check<StepsType>(L, 1);
Difficulty dc = Enum::Check<Difficulty>( L, 2 );
Steps *pSteps = SongUtil::GetOneSteps( p, st, dc );
if( pSteps )
pSteps->PushSelf(L);
else
lua_pushnil(L);
return 1;
}
2008-02-15 09:18:29 +00:00
static int GetTimingData( T* p, lua_State *L )
{
p->m_Timing.PushSelf(L);
return 1;
}
2005-09-07 22:05:41 +00:00
2006-09-27 20:03:31 +00:00
LunaSong()
2005-02-22 23:06:51 +00:00
{
ADD_METHOD( GetDisplayFullTitle );
ADD_METHOD( GetTranslitFullTitle );
ADD_METHOD( GetDisplayMainTitle );
ADD_METHOD( GetTranslitMainTitle );
ADD_METHOD( GetDisplaySubTitle );
ADD_METHOD( GetTranslitSubTitle );
ADD_METHOD( GetDisplayArtist );
ADD_METHOD( GetTranslitArtist );
ADD_METHOD( GetGenre );
ADD_METHOD( GetAllSteps );
ADD_METHOD( GetStepsByStepsType );
ADD_METHOD( GetSongDir );
ADD_METHOD( GetBannerPath );
ADD_METHOD( GetBackgroundPath );
ADD_METHOD( IsTutorial );
2006-05-01 12:45:42 +00:00
ADD_METHOD( GetGroupName );
2006-08-17 00:27:58 +00:00
ADD_METHOD( MusicLengthSeconds );
ADD_METHOD( IsLong );
ADD_METHOD( IsMarathon );
2006-08-20 04:40:32 +00:00
ADD_METHOD( HasStepsTypeAndDifficulty );
2007-02-09 03:23:24 +00:00
ADD_METHOD( GetOneSteps );
2008-02-15 09:18:29 +00:00
ADD_METHOD( GetTimingData );
2005-02-22 23:06:51 +00:00
}
};
LUA_REGISTER_CLASS( Song )
// lua end
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.
*/