Files
itgmania212121/stepmania/src/NotesLoaderKSF.cpp
T
2003-04-05 21:18:18 +00:00

278 lines
6.9 KiB
C++

#include "global.h"
#include "NotesLoaderKSF.h"
#include "RageException.h"
#include "MsdFile.h"
#include "RageLog.h"
#include "RageUtil.h"
#include "NoteData.h"
#include "NoteTypes.h"
bool KSFLoader::LoadFromKSFFile( const CString &sPath, Notes &out )
{
LOG->Trace( "Notes::LoadFromKSFFile( '%s' )", sPath.GetString() );
MsdFile msd;
bool bResult = msd.ReadFile( sPath );
if( !bResult )
RageException::Throw( "Error opening file '%s'.", sPath.GetString() );
int iTickCount = -1; // this is the value we read for TICKCOUNT
CString iStep; // this is the value we read for STEP
for( unsigned i=0; i<msd.GetNumValues(); i++ )
{
const MsdFile::value_t &sParams = msd.GetValue(i);
CString sValueName = sParams[0];
// handle the data
if( 0==stricmp(sValueName,"TICKCOUNT") )
iTickCount = atoi(sParams[1]);
else if( 0==stricmp(sValueName,"STEP") )
iStep = sParams[1];
else if( 0==stricmp(sValueName,"DIFFICULTY") )
out.SetMeter(atoi(sParams[1]));
}
if( iTickCount == -1 )
{
iTickCount = 2;
LOG->Warn( "%s:\nTICKCOUNT not found; defaulting to %i", sPath.GetString(), iTickCount );
}
NoteData notedata; // read it into here
CStringArray asRows;
TrimLeft(iStep);
split( iStep, "\n", asRows, true );
{
CString sDir, sFName, sExt;
splitrelpath( sPath, sDir, sFName, sExt );
sFName.MakeLower();
out.SetDescription(sFName);
if( sFName.Find("crazy")!=-1 )
{
out.SetDifficulty(DIFFICULTY_HARD);
if(!out.GetMeter()) out.SetMeter(8);
}
else if( sFName.Find("hard")!=-1 )
{
out.SetDifficulty(DIFFICULTY_MEDIUM);
if(!out.GetMeter()) out.SetMeter(5);
}
else if( sFName.Find("easy")!=-1 )
{
out.SetDifficulty(DIFFICULTY_EASY);
if(!out.GetMeter()) out.SetMeter(2);
}
else
{
out.SetDifficulty(DIFFICULTY_MEDIUM);
if(!out.GetMeter()) out.SetMeter(5);
}
notedata.SetNumTracks( 5 );
out.m_NotesType = NOTES_TYPE_PUMP_SINGLE;
if( sFName.Find("double") != -1 )
{
notedata.SetNumTracks( 10 );
out.m_NotesType = NOTES_TYPE_PUMP_DOUBLE;
} else if( sFName.Find("_2") != -1 ) {
notedata.SetNumTracks( 10 );
out.m_NotesType = NOTES_TYPE_PUMP_COUPLE;
}
}
int iHoldStartRow[13];
for( int t=0; t<13; t++ )
iHoldStartRow[t] = -1;
for( unsigned r=0; r<asRows.size(); r++ )
{
CString& sRowString = asRows[r];
if( sRowString == "" )
continue; // skip
/* All 2s indicates the end of the song. */
if( sRowString == "2222222222222" )
break;
if(sRowString.size() != 13)
{
LOG->Warn("File %s had a RowString with an improper length (\"%s\"); corrupt notes ignored",
sPath.GetString(), sRowString.GetString());
return false;
}
// the length of a note in a row depends on TICKCOUNT
float fBeatThisRow = r/(float)iTickCount;
int row = BeatToNoteRow(fBeatThisRow);
for( int t=0; t < notedata.GetNumTracks(); t++ )
{
if( sRowString[t] == '4' )
{
/* Remember when each hold starts; ignore the middle. */
if( iHoldStartRow[t] == -1 )
iHoldStartRow[t] = r;
continue;
}
if( iHoldStartRow[t] != -1 ) // this ends the hold
{
HoldNote hn (
t, /* button */
iHoldStartRow[t]/(float)iTickCount, /* start */
(r-1)/(float)iTickCount /* end */
);
notedata.AddHoldNote( hn );
iHoldStartRow[t] = -1;
}
TapNote tap;
switch(sRowString[t])
{
case '0': tap = TAP_EMPTY; break;
case '1': tap = TAP_TAP; break;
default: ASSERT(0); tap = TAP_EMPTY; break;
}
notedata.SetTapNote(t, row, tap);
}
}
out.SetNoteData(&notedata);
return true;
}
void KSFLoader::GetApplicableFiles( CString sPath, CStringArray &out )
{
GetDirListing( sPath + CString("*.ksf"), out );
}
bool KSFLoader::LoadFromDir( CString sDir, Song &out )
{
LOG->Trace( "Song::LoadFromKSFDir(%s)", sDir.GetString() );
CStringArray arrayKSFFileNames;
GetDirListing( sDir + CString("*.ksf"), arrayKSFFileNames );
/* We shouldn't have been called to begin with if there were no KSFs. */
if( arrayKSFFileNames.empty() )
RageException::Throw( "Couldn't find any KSF files in '%s'", sDir.GetString() );
// load the Notes from the rest of the KSF files
unsigned i;
for( i=0; i<arrayKSFFileNames.size(); i++ )
{
Notes* pNewNotes = new Notes;
if(!LoadFromKSFFile( out.GetSongDir() + arrayKSFFileNames[i], *pNewNotes ))
{
delete pNewNotes;
continue;
}
out.m_apNotes.push_back( pNewNotes );
}
CString sPath = out.GetSongDir() + arrayKSFFileNames[0];
/* XXX: We might have some corrupt KSF's; it'd be better to read global
* stuff from the first successful one above. */
MsdFile msd;
bool bResult = msd.ReadFile( sPath );
if( !bResult )
RageException::Throw( "Error opening file '%s'.", sPath.GetString() );
for( i=0; i < msd.GetNumValues(); i++ )
{
const MsdFile::value_t &sParams = msd.GetValue(i);
CString sValueName = sParams[0];
// handle the data
if( 0==stricmp(sValueName,"TITLE") )
{
//title is usually in format "artist - songtitle"
CStringArray asBits;
split( sParams[1], " - ", asBits, false );
/* It's often "artist - songtitle - difficulty". Ignore
* the difficulty, since we get that from the filename. */
if( asBits.size() == 3 &&
(!stricmp(asBits[2], "double") ||
!stricmp(asBits[2], "easy") ||
!stricmp(asBits[2], "normal") ||
!stricmp(asBits[2], "hard") ||
!stricmp(asBits[2], "crazy")) )
{
asBits.erase(asBits.begin()+2, asBits.begin()+3);
}
if( asBits.size() == 2 )
{
out.m_sArtist = asBits[0];
out.m_sMainTitle = asBits[1];
}
else
{
out.m_sMainTitle = sParams[1];
}
for( int j=0; j<out.m_sMainTitle.GetLength(); j++ )
{
char c = out.m_sMainTitle[j];
if( c < 0 ) // this title has a foreign char
{
CStringArray asBits;
split( sDir, "\\", asBits, true);
CString sSongFolderName = asBits[ asBits.size()-1 ];
asBits.clear();
split( sSongFolderName, " - ", asBits, false );
if( asBits.size() == 2 )
{
out.m_sArtist = asBits[0];
out.m_sMainTitle = asBits[1];
}
else
{
out.m_sMainTitle = asBits[0];
}
break;
}
}
}
else if( 0==stricmp(sValueName,"BPM") )
out.AddBPMSegment( BPMSegment(0, (float)atof(sParams[1])) );
else if( 0==stricmp(sValueName,"STARTTIME") )
out.m_fBeat0OffsetInSeconds = -(float)atof(sParams[1])/100;
else if( 0==stricmp(sValueName,"TICKCOUNT") ||
0==stricmp(sValueName,"STEP") ||
0==stricmp(sValueName,"DIFFICULTY"))
; /* Handled in LoadFromKSFFile; don't warn. */
else
LOG->Trace( "Unexpected value named '%s'", sValueName.GetString() );
}
// search for music with song in the file name
CStringArray arrayPossibleMusic;
GetDirListing( out.GetSongDir() + CString("song.mp3"), arrayPossibleMusic );
GetDirListing( out.GetSongDir() + CString("song.ogg"), arrayPossibleMusic );
GetDirListing( out.GetSongDir() + CString("song.wav"), arrayPossibleMusic );
if( !arrayPossibleMusic.empty() ) // we found a match
out.m_sMusicFile = arrayPossibleMusic[0];
return true;
}