diff --git a/stepmania/src/NotesLoader.cpp b/stepmania/src/NotesLoader.cpp new file mode 100644 index 0000000000..95ff949f71 --- /dev/null +++ b/stepmania/src/NotesLoader.cpp @@ -0,0 +1,11 @@ +#include "stdafx.h" +#include "NotesLoader.h" +#include "NoteTypes.h" +#include "GameManager.h" + +bool NotesLoader::Loadable( CString sPath ) +{ + CStringArray list; + GetApplicableFiles( sPath, list ); + return list.GetSize() != 0; +} diff --git a/stepmania/src/NotesLoader.h b/stepmania/src/NotesLoader.h index 92997187e5..250864bd1c 100644 --- a/stepmania/src/NotesLoader.h +++ b/stepmania/src/NotesLoader.h @@ -1,6 +1,8 @@ #ifndef NOTES_LOADER_H #define NOTES_LOADER_H +#include "song.h" + typedef int DanceNote; enum { DANCE_NOTE_NONE = 0, @@ -18,4 +20,13 @@ enum { DANCE_NOTE_PAD2_RIGHT }; +class NotesLoader { +protected: + virtual void GetApplicableFiles( CString sPath, CStringArray &out )=0; + +public: + virtual bool LoadFromDir( CString sPath, Song &out ) = 0; + bool Loadable( CString sPath ); +}; + #endif \ No newline at end of file diff --git a/stepmania/src/NotesLoaderBMS.cpp b/stepmania/src/NotesLoaderBMS.cpp index f96c59045f..c3ce7d54ca 100644 --- a/stepmania/src/NotesLoaderBMS.cpp +++ b/stepmania/src/NotesLoaderBMS.cpp @@ -272,12 +272,17 @@ bool BMSLoader::LoadFromBMSFile( const CString &sPath, Notes &out, Notes &out2 ) return true; } -bool BMSLoader::LoadFromBMSDir( CString sDir, Song &out ) +void BMSLoader::GetApplicableFiles( CString sPath, CStringArray &out ) +{ + GetDirListing( sPath + CString("*.bms"), out ); +} + +bool BMSLoader::LoadFromDir( CString sDir, Song &out ) { LOG->Trace( "Song::LoadFromBMSDir(%s)", sDir ); CStringArray arrayBMSFileNames; - GetDirListing( sDir + CString("*.bms"), arrayBMSFileNames ); + GetApplicableFiles( sDir, arrayBMSFileNames ); if( arrayBMSFileNames.GetSize() == 0 ) throw RageException( "Couldn't find any BMS files in '%s'", sDir ); diff --git a/stepmania/src/NotesLoaderBMS.h b/stepmania/src/NotesLoaderBMS.h index 056a94f87a..0725f624c9 100644 --- a/stepmania/src/NotesLoaderBMS.h +++ b/stepmania/src/NotesLoaderBMS.h @@ -5,12 +5,13 @@ #include "Notes.h" #include "NotesLoader.h" -class BMSLoader { +class BMSLoader: public NotesLoader { bool LoadFromBMSFile( const CString &sPath, Notes &out1, Notes &out2 ); void mapBMSTrackToDanceNote( int iBMSTrack, int &iDanceColOut, char &cNoteCharOut ); public: - bool LoadFromBMSDir( CString sDir, Song &out ); + void GetApplicableFiles( CString sPath, CStringArray &out ); + bool LoadFromDir( CString sDir, Song &out ); }; #endif diff --git a/stepmania/src/NotesLoaderDWI.cpp b/stepmania/src/NotesLoaderDWI.cpp index 8c8b164706..6534329f96 100644 --- a/stepmania/src/NotesLoaderDWI.cpp +++ b/stepmania/src/NotesLoaderDWI.cpp @@ -382,3 +382,24 @@ bool DWILoader::LoadFromDWIFile( CString sPath, Song &out ) return true; } + +void DWILoader::GetApplicableFiles( CString sPath, CStringArray &out ) +{ + GetDirListing( sPath + CString("*.dwi"), out ); +} + +bool DWILoader::LoadFromDir( CString sPath, Song &out ) +{ + CStringArray aFileNames; + GetApplicableFiles( sPath, aFileNames ); + + if( aFileNames.GetSize() > 1 ) + throw RageException( "There is more than one DWI file in '%s'. There should be only one!", sPath ); + + /* We should have exactly one; if we had none, we shouldn't have been + * called to begin with. */ + ASSERT( aFileNames.GetSize() == 1 ); + + return LoadFromDWIFile( sPath + aFileNames[0], out ); +} + diff --git a/stepmania/src/NotesLoaderDWI.h b/stepmania/src/NotesLoaderDWI.h index fd4eb15df0..2bf69e2dda 100644 --- a/stepmania/src/NotesLoaderDWI.h +++ b/stepmania/src/NotesLoaderDWI.h @@ -9,7 +9,12 @@ #include "Song.h" #include "Notes.h" -class DWILoader { +/* Return NA if no files in the directory can be loaded by + * this loader, OK on success, ERROR if an applicable file was found + * but there was a fatal error loading. (ERROR not used yet--we + * always throw.) + */ +class DWILoader: public NotesLoader { void DWIcharToNote( char c, GameController i, DanceNote ¬e1Out, DanceNote ¬e2Out ); bool LoadFromDWITokens( @@ -17,8 +22,12 @@ class DWILoader { CString sStepData2, Notes &out, Notes &out2); -public: bool LoadFromDWIFile( CString sPath, Song &out ); + +public: + void GetApplicableFiles( CString sPath, CStringArray &out ); + bool Loadable( CString sPath ); + bool LoadFromDir( CString sPath, Song &out ); }; #endif diff --git a/stepmania/src/NotesLoaderKSF.cpp b/stepmania/src/NotesLoaderKSF.cpp index a0d42a2c73..3592179089 100644 --- a/stepmania/src/NotesLoaderKSF.cpp +++ b/stepmania/src/NotesLoaderKSF.cpp @@ -143,8 +143,12 @@ bool KSFLoader::LoadFromKSFFile( const CString &sPath, Notes &out ) return true; } +void KSFLoader::GetApplicableFiles( CString sPath, CStringArray &out ) +{ + GetDirListing( sPath + CString("*.ksf"), out ); +} -bool KSFLoader::LoadFromKSFDir( CString sDir, Song &out ) +bool KSFLoader::LoadFromDir( CString sDir, Song &out ) { LOG->Trace( "Song::LoadFromKSFDir(%s)", sDir ); diff --git a/stepmania/src/NotesLoaderKSF.h b/stepmania/src/NotesLoaderKSF.h index 81e4927bfb..95cb4a48fd 100644 --- a/stepmania/src/NotesLoaderKSF.h +++ b/stepmania/src/NotesLoaderKSF.h @@ -3,12 +3,14 @@ #include "Song.h" #include "Notes.h" +#include "NotesLoader.h" -class KSFLoader { +class KSFLoader: public NotesLoader { bool LoadFromKSFFile( const CString &sPath, Notes &out ); public: - bool LoadFromKSFDir( CString sDir, Song &out ); + void GetApplicableFiles( CString sPath, CStringArray &out ); + bool LoadFromDir( CString sDir, Song &out ); }; diff --git a/stepmania/src/NotesLoaderSM.cpp b/stepmania/src/NotesLoaderSM.cpp index 8d47c3acd2..d96acb24c5 100644 --- a/stepmania/src/NotesLoaderSM.cpp +++ b/stepmania/src/NotesLoaderSM.cpp @@ -42,6 +42,10 @@ void SMLoader::LoadFromSMTokens( out.TidyUpData(); } +void SMLoader::GetApplicableFiles( CString sPath, CStringArray &out ) +{ + GetDirListing( sPath + CString("*.sm"), out ); +} bool SMLoader::LoadFromSMFile( CString sPath, Song &out ) { @@ -208,3 +212,20 @@ bool SMLoader::LoadFromSMFile( CString sPath, Song &out ) return true; } + + +bool SMLoader::LoadFromDir( CString sPath, Song &out ) +{ + CStringArray aFileNames; + GetApplicableFiles( sPath, aFileNames ); + + if( aFileNames.GetSize() > 1 ) + throw RageException( "There is more than one SM file in '%s'. There should be only one!", sPath ); + + /* We should have exactly one; if we had none, we shouldn't have been + * called to begin with. */ + ASSERT( aFileNames.GetSize() == 1 ); + + return LoadFromSMFile( sPath + aFileNames[0], out ); +} + diff --git a/stepmania/src/NotesLoaderSM.h b/stepmania/src/NotesLoaderSM.h index c5eae2ddea..571aa88be6 100644 --- a/stepmania/src/NotesLoaderSM.h +++ b/stepmania/src/NotesLoaderSM.h @@ -3,8 +3,9 @@ #include "Song.h" #include "Notes.h" +#include "NotesLoader.h" -class SMLoader { +class SMLoader: public NotesLoader { void LoadFromSMTokens( CString sNotesType, CString sDescription, @@ -17,6 +18,8 @@ class SMLoader { public: bool LoadFromSMFile( CString sPath, Song &out ); + void GetApplicableFiles( CString sPath, CStringArray &out ); + bool LoadFromDir( CString sPath, Song &out ); }; #endif diff --git a/stepmania/src/Song.cpp b/stepmania/src/Song.cpp index 52ecf904f3..fec651339e 100644 --- a/stepmania/src/Song.cpp +++ b/stepmania/src/Song.cpp @@ -292,60 +292,52 @@ const CString &Song::GetSongFilePath() const return m_sSongFileName; } +NotesLoader *Song::MakeLoader( CString sDir ) const +{ + 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; + + ret = new DWILoader; + if(ret->Loadable( sDir )) return ret; + delete ret; + + ret = new BMSLoader; + if(ret->Loadable( sDir )) return ret; + delete ret; + + ret = new KSFLoader; + if(ret->Loadable( sDir )) return ret; + delete ret; + + return NULL; +} + bool Song::LoadWithoutCache( CString sDir ) { // // There was no entry in the cache for this song. // Let's load it from a file, then write a cache entry. // - CStringArray arrayBMSFileNames; - GetDirListing( sDir + CString("*.bms"), arrayBMSFileNames ); - int iNumBMSFiles = arrayBMSFileNames.GetSize(); - - CStringArray arrayKSFFileNames; - GetDirListing( sDir + CString("*.ksf"), arrayKSFFileNames ); - int iNumKSFFiles = arrayKSFFileNames.GetSize(); - - CStringArray arrayDWIFileNames; - GetDirListing( sDir + CString("*.dwi"), arrayDWIFileNames ); - int iNumDWIFiles = arrayDWIFileNames.GetSize(); - - CStringArray arraySMFileNames; - GetDirListing( sDir + CString("*.sm"), arraySMFileNames ); - int iNumSMFiles = arraySMFileNames.GetSize(); - - - if( iNumSMFiles > 1 ) - throw RageException( "There is more than one SM file in '%s'. There should be only one!", sDir ); - if( iNumDWIFiles > 1 ) - throw RageException( "There is more than one DWI file in '%s'. There should be only one!", sDir ); - if( iNumSMFiles == 1 ) - { - SMLoader ld; - ld.LoadFromSMFile( sDir + arraySMFileNames[0], *this ); - } - else if( iNumDWIFiles == 1 ) - { - DWILoader ld; - ld.LoadFromDWIFile( sDir + arrayDWIFileNames[0], *this ); - } - else if( iNumBMSFiles > 0 ) - { - BMSLoader ld; - ld.LoadFromBMSDir( sDir, *this ); - } - else if( iNumKSFFiles > 0 ) - { - KSFLoader ld; - ld.LoadFromKSFDir( sDir, *this ); - } - else + 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 ); return false; } + bool success = ld->LoadFromDir( sDir, *this ); + delete ld; + + if(!success) + return false; + AddAutoGenNotes(); TidyUpData(); diff --git a/stepmania/src/StepMania.dsp b/stepmania/src/StepMania.dsp index 20b105ca22..549440e457 100644 --- a/stepmania/src/StepMania.dsp +++ b/stepmania/src/StepMania.dsp @@ -318,6 +318,10 @@ SOURCE=.\Notes.h # End Source File # Begin Source File +SOURCE=.\NotesLoader.cpp +# End Source File +# Begin Source File + SOURCE=.\NotesLoader.h # End Source File # Begin Source File diff --git a/stepmania/src/StepMania.vcproj b/stepmania/src/StepMania.vcproj index 6865a60db9..290a74cbd4 100644 --- a/stepmania/src/StepMania.vcproj +++ b/stepmania/src/StepMania.vcproj @@ -445,6 +445,9 @@ cl /Zl /nologo /c verstub.cpp /Fo$(IntDir)\ + + diff --git a/stepmania/src/song.h b/stepmania/src/song.h index 3d39c1fb35..ff528fbf09 100644 --- a/stepmania/src/song.h +++ b/stepmania/src/song.h @@ -15,6 +15,7 @@ struct Notes; class StyleDef; +class NotesLoader; extern const int FILE_CACHE_VERSION; @@ -56,6 +57,7 @@ public: ~Song(); bool LoadWithoutCache( CString sDir ); + NotesLoader *MakeLoader( CString sDir ) const; bool LoadFromSongDir( CString sDir );