From 7db930ed2f6fcfcab1efe38cff27a303f2dc2096 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Wed, 5 Feb 2003 19:16:00 +0000 Subject: [PATCH] push song searching into a function --- stepmania/src/Course.cpp | 110 +++++++++++++++++++--------------- stepmania/src/Course.h | 3 +- stepmania/src/SongManager.cpp | 8 ++- 3 files changed, 69 insertions(+), 52 deletions(-) diff --git a/stepmania/src/Course.cpp b/stepmania/src/Course.cpp index e87b2ddda9..d3aa0e9233 100644 --- a/stepmania/src/Course.cpp +++ b/stepmania/src/Course.cpp @@ -51,7 +51,66 @@ Course::Course() } } -void Course::LoadFromCRSFile( CString sPath, vector &apSongs ) +/* + * GetSongDir() contains a path to the song, possibly a full path, eg: + * Songs\Group\SongName or + * My Other Song Folder\Group\SongName or + * c:\Corny J-pop\Group\SongName + * + * Most course group names are "Group\SongName", so we want to + * match against the last two elements. Let's also support + * "SongName" alone, since the group is only important when it's + * potentially ambiguous. + * + * Let's *not* support "Songs\Group\SongName" in course files. + * That's probably a common error, but that would result in + * course files floating around that only work for people who put + * songs in "Songs"; we don't want that. + */ + +Song *Course::FindSong(CString sSongDir) +{ + const vector &apSongs = SONGMAN->GetAllSongs(); + + CStringArray split_SongDir; + sSongDir.Replace("\\", "/"); + split( sSongDir, "/", split_SongDir, true ); + + if( split_SongDir.size() > 2 ) + { + LOG->Warn( "Course file \"%s\" path \"%s\" should contain " + "at most one backslash; ignored.", + m_sPath.GetString(), sSongDir.GetString()); + return NULL; + } + + // foreach song + for( unsigned i = 0; i < apSongs.size(); i++ ) + { + CString dir = apSongs[i]->GetSongDir(); + dir.Replace("\\", "/"); + CStringArray splitted; /* splat! */ + split( dir, "/", splitted, true ); + bool matches = true; + + int split_no = splitted.size()-1; + int SongDir_no = split_SongDir.size()-1; + + while( split_no >= 0 && SongDir_no >= 0 ) { + if( stricmp(splitted[split_no--], split_SongDir[SongDir_no--] ) ) + matches=false; + } + + if(matches) + return apSongs[i]; + } + + LOG->Trace( "Course \"%s\": couldn't match song \"%s\"", + m_sPath.GetString(), sSongDir.GetString()); + return NULL; +} + +void Course::LoadFromCRSFile( CString sPath ) { m_sPath = sPath; // save path @@ -105,55 +164,8 @@ void Course::LoadFromCRSFile( CString sPath, vector &apSongs ) continue; } - /* GetSongDir() contains a path to the song, possibly a full path, eg: - * Songs\Group\SongName or - * My Other Song Folder\Group\SongName or - * c:\Corny J-pop\Group\SongName - * - * Most course group names are "Group\SongName", so we want to - * match against the last two elements. Let's also support - * "SongName" alone, since the group is only important when it's - * potentially ambiguous. - * - * Let's *not* support "Songs\Group\SongName" in course files. - * That's probably a common error, but that would result in - * course files floating around that only work for people who put - * songs in "Songs"; we don't want that. - */ + Song *pSong = FindSong(sSongDir); - CStringArray split_SongDir; - sSongDir.Replace("\\", "/"); - split( sSongDir, "/", split_SongDir, true ); - - if( split_SongDir.size() > 2 ) - { - LOG->Warn( "Course file \"%s\" path \"%s\" should contain " - "at most one backslash; ignored.", - (const char *) sPath, (const char *) sSongDir); - continue; - } - - Song *pSong = NULL; - // foreach song - for( unsigned i = 0; pSong == NULL && i < apSongs.size(); i++ ) - { - CString dir = apSongs[i]->GetSongDir(); - dir.Replace("\\", "/"); - CStringArray splitted; - split( dir, "/", splitted, true ); - bool matches = true; - - int split_no = splitted.size()-1; - int SongDir_no = split_SongDir.size()-1; - - while( split_no >= 0 && SongDir_no >= 0 ) { - if( stricmp(splitted[split_no--], split_SongDir[SongDir_no--] ) ) - matches=false; - } - - if(matches) - pSong = apSongs[i]; - } if( pSong == NULL ) // we didn't find the Song continue; // skip this song diff --git a/stepmania/src/Course.h b/stepmania/src/Course.h index c2dedc1679..9a02416d4b 100644 --- a/stepmania/src/Course.h +++ b/stepmania/src/Course.h @@ -49,7 +49,7 @@ public: void GetSongOptions( SongOptions* pSO_out) const; int GetNumStages() const; - void LoadFromCRSFile( CString sPath, vector &apSongs ); + void LoadFromCRSFile( CString sPath ); void CreateEndlessCourseFromGroupAndDifficulty( CString sGroupName, Difficulty dc, vector &apSongsInGroup ); void AddStage( Song* pSong, CString sDescription, CString sModifiers ); @@ -77,6 +77,7 @@ public: private: void Shuffle(); + Song *FindSong(CString sSongDir); }; diff --git a/stepmania/src/SongManager.cpp b/stepmania/src/SongManager.cpp index 31790ca2ba..074f9c778e 100644 --- a/stepmania/src/SongManager.cpp +++ b/stepmania/src/SongManager.cpp @@ -44,6 +44,10 @@ RageColor g_ExtraColor; SongManager::SongManager( LoadingWindow *ld ) { + /* We initialize things that assume they can get at SONGMAN; we only + * init one of these, so hook us up to it immediately. */ + SONGMAN = this; + g_vGroupColors.clear(); for( int i=0; iLoadFromCRSFile( saCourseFiles[i], m_pSongs ); + pCourse->LoadFromCRSFile( saCourseFiles[i] ); if( pCourse->GetNumStages() > 0 ) m_pCourses.push_back( pCourse ); else @@ -549,7 +553,7 @@ bool SongManager::GetExtraStageInfoFromCourse( bool bExtra2, CString sPreferredG } Course course; - course.LoadFromCRSFile( sCoursePath, m_pSongs ); + course.LoadFromCRSFile( sCoursePath ); if( course.GetNumStages() <= 0 ) return false; pSongOut = course.GetSong(0);