Files
itgmania212121/stepmania/src/NotesLoaderSM.cpp
T

470 lines
13 KiB
C++
Raw Normal View History

2003-02-16 04:01:45 +00:00
#include "global.h"
2002-09-06 23:24:40 +00:00
#include "NotesLoaderSM.h"
#include "GameManager.h"
#include "RageException.h"
#include "MsdFile.h"
#include "RageLog.h"
2002-09-07 11:43:36 +00:00
#include "RageUtil.h"
2004-02-08 01:05:53 +00:00
#include "SongManager.h"
2004-04-23 00:26:51 +00:00
#include "RageFileManager.h"
#define MAX_EDIT_SIZE_BYTES 20*1024 // 20 KB
2002-09-06 23:24:40 +00:00
void SMLoader::LoadFromSMTokens(
CString sNotesType,
CString sDescription,
2002-09-29 05:06:18 +00:00
CString sDifficulty,
2002-09-06 23:24:40 +00:00
CString sMeter,
CString sRadarValues,
CString sNoteData,
2003-11-17 03:38:24 +00:00
CString sAttackData,
2003-08-03 00:13:55 +00:00
Steps &out
2002-09-06 23:24:40 +00:00
)
{
2002-10-31 08:05:13 +00:00
TrimLeft(sNotesType);
TrimRight(sNotesType);
TrimLeft(sDescription);
TrimRight(sDescription);
TrimLeft(sDifficulty);
TrimRight(sDifficulty);
2002-09-06 23:24:40 +00:00
2003-08-03 00:13:55 +00:00
// LOG->Trace( "Steps::LoadFromSMTokens()" );
2002-09-06 23:24:40 +00:00
2003-08-07 06:36:34 +00:00
out.m_StepsType = GameManager::StringToNotesType(sNotesType);
2003-01-02 22:10:51 +00:00
out.SetDescription(sDescription);
out.SetDifficulty(StringToDifficulty( sDifficulty ));
2003-01-21 23:07:22 +00:00
// HACK: We used to store SMANIAC as DIFFICULTY_HARD with special description.
2003-11-29 18:46:40 +00:00
// Now, it has its own DIFFICULTY_CHALLENGE
2003-01-21 23:07:22 +00:00
if( sDescription.CompareNoCase("smaniac") == 0 )
out.SetDifficulty( DIFFICULTY_CHALLENGE );
// HACK: We used to store CHALLENGE as DIFFICULTY_HARD with special description.
2003-11-29 18:46:40 +00:00
// Now, it has its own DIFFICULTY_CHALLENGE
2003-01-21 23:07:22 +00:00
if( sDescription.CompareNoCase("challenge") == 0 )
out.SetDifficulty( DIFFICULTY_CHALLENGE );
2003-01-02 22:10:51 +00:00
out.SetMeter(atoi(sMeter));
2002-09-06 23:24:40 +00:00
CStringArray saValues;
split( sRadarValues, ",", saValues, true );
2003-01-30 07:18:33 +00:00
if( saValues.size() == NUM_RADAR_CATEGORIES )
for( int r=0; r<NUM_RADAR_CATEGORIES; r++ )
2003-01-02 22:10:51 +00:00
out.SetRadarValue(r, (float)atof(saValues[r]));
2002-09-06 23:24:40 +00:00
2003-11-17 03:38:24 +00:00
out.SetSMNoteData(sNoteData, sAttackData);
2002-09-06 23:24:40 +00:00
out.TidyUpData();
}
2002-09-11 05:15:46 +00:00
void SMLoader::GetApplicableFiles( CString sPath, CStringArray &out )
{
GetDirListing( sPath + CString("*.sm"), out );
}
2002-09-06 23:24:40 +00:00
2004-01-17 23:21:07 +00:00
bool SMLoader::LoadTimingFromFile( const CString &fn, TimingData &out )
{
MsdFile msd;
if( !msd.ReadFile( fn ) )
{
LOG->Warn( "Couldn't load %s, \"%s\"", fn.c_str(), msd.GetError().c_str() );
return false;
}
out.m_sFile = fn;
LoadTimingFromSMFile( msd, out );
return true;
}
void SMLoader::LoadTimingFromSMFile( const MsdFile &msd, TimingData &out )
2003-12-18 08:21:18 +00:00
{
2004-01-11 11:16:56 +00:00
out.m_fBeat0OffsetInSeconds = 0;
2003-12-18 08:21:18 +00:00
out.m_BPMSegments.clear();
out.m_StopSegments.clear();
for( unsigned i=0; i<msd.GetNumValues(); i++ )
{
const MsdFile::value_t &sParams = msd.GetValue(i);
const CString sValueName = sParams[0];
if( 0==stricmp(sValueName,"OFFSET") )
out.m_fBeat0OffsetInSeconds = (float)atof( sParams[1] );
else if( 0==stricmp(sValueName,"STOPS") || 0==stricmp(sValueName,"FREEZES") )
{
CStringArray arrayFreezeExpressions;
split( sParams[1], ",", arrayFreezeExpressions );
for( unsigned f=0; f<arrayFreezeExpressions.size(); f++ )
{
CStringArray arrayFreezeValues;
split( arrayFreezeExpressions[f], "=", arrayFreezeValues );
/* XXX: Once we have a way to display warnings that the user actually
* cares about (unlike most warnings), this should be one of them. */
if( arrayFreezeValues.size() != 2 )
{
LOG->Warn( "Invalid #%s value \"%s\" (must have exactly one '='), ignored",
sValueName.c_str(), arrayFreezeExpressions[f].c_str() );
continue;
}
const float fFreezeBeat = (float)atof( arrayFreezeValues[0] );
const float fFreezeSeconds = (float)atof( arrayFreezeValues[1] );
StopSegment new_seg;
new_seg.m_fStartBeat = fFreezeBeat;
new_seg.m_fStopSeconds = fFreezeSeconds;
// LOG->Trace( "Adding a freeze segment: beat: %f, seconds = %f", new_seg.m_fStartBeat, new_seg.m_fStopSeconds );
out.AddStopSegment( new_seg );
}
}
else if( 0==stricmp(sValueName,"BPMS") )
{
CStringArray arrayBPMChangeExpressions;
split( sParams[1], ",", arrayBPMChangeExpressions );
for( unsigned b=0; b<arrayBPMChangeExpressions.size(); b++ )
{
CStringArray arrayBPMChangeValues;
split( arrayBPMChangeExpressions[b], "=", arrayBPMChangeValues );
/* XXX: Once we have a way to display warnings that the user actually
* cares about (unlike most warnings), this should be one of them. */
if(arrayBPMChangeValues.size() != 2)
{
LOG->Warn( "Invalid #%s value \"%s\" (must have exactly one '='), ignored",
sValueName.c_str(), arrayBPMChangeExpressions[b].c_str() );
continue;
}
const float fBeat = (float)atof( arrayBPMChangeValues[0] );
const float fNewBPM = (float)atof( arrayBPMChangeValues[1] );
BPMSegment new_seg;
new_seg.m_fStartBeat = fBeat;
new_seg.m_fBPM = fNewBPM;
out.AddBPMSegment( new_seg );
}
}
}
}
2004-01-07 00:13:32 +00:00
bool LoadFromBGChangesString( BackgroundChange &change, const CString &sBGChangeExpression )
{
CStringArray aBGChangeValues;
split( sBGChangeExpression, "=", aBGChangeValues );
switch( aBGChangeValues.size() )
{
case 6:
change.m_fRate = (float)atof( aBGChangeValues[2] );
change.m_bFadeLast = atoi( aBGChangeValues[3] ) != 0;
change.m_bRewindMovie = atoi( aBGChangeValues[4] ) != 0;
change.m_bLoop = atoi( aBGChangeValues[5] ) != 0;
// fall through
case 2:
change.m_fStartBeat = (float)atof( aBGChangeValues[0] );
change.m_sBGName = aBGChangeValues[1];
return true;
default:
LOG->Warn("Invalid #BGCHANGES value \"%s\" was ignored", sBGChangeExpression.c_str());
return false;
}
}
2002-09-06 23:24:40 +00:00
bool SMLoader::LoadFromSMFile( CString sPath, Song &out )
{
2003-11-01 22:12:12 +00:00
LOG->Trace( "Song::LoadFromSMFile(%s)", sPath.c_str() );
2002-09-06 23:24:40 +00:00
MsdFile msd;
bool bResult = msd.ReadFile( sPath );
if( !bResult )
2003-04-25 00:01:35 +00:00
RageException::Throw( "Error opening file '%s'.", sPath.c_str() );
2002-09-06 23:24:40 +00:00
2004-01-17 23:21:07 +00:00
out.m_Timing.m_sFile = sPath;
2003-12-18 08:21:18 +00:00
LoadTimingFromSMFile( msd, out.m_Timing );
2003-01-14 22:10:04 +00:00
for( unsigned i=0; i<msd.GetNumValues(); i++ )
2002-09-06 23:24:40 +00:00
{
2003-01-14 22:44:30 +00:00
int iNumParams = msd.GetNumParams(i);
const MsdFile::value_t &sParams = msd.GetValue(i);
const CString sValueName = sParams[0];
2002-09-06 23:24:40 +00:00
// handle the data
/* Don't use GetMainAndSubTitlesFromFullTitle; that's only for heuristically
* splitting other formats that *don't* natively support #SUBTITLE. */
2002-09-06 23:24:40 +00:00
if( 0==stricmp(sValueName,"TITLE") )
out.m_sMainTitle = sParams[1];
2002-09-06 23:24:40 +00:00
else if( 0==stricmp(sValueName,"SUBTITLE") )
out.m_sSubTitle = sParams[1];
else if( 0==stricmp(sValueName,"ARTIST") )
out.m_sArtist = sParams[1];
else if( 0==stricmp(sValueName,"TITLETRANSLIT") )
out.m_sMainTitleTranslit = sParams[1];
else if( 0==stricmp(sValueName,"SUBTITLETRANSLIT") )
out.m_sSubTitleTranslit = sParams[1];
else if( 0==stricmp(sValueName,"ARTISTTRANSLIT") )
out.m_sArtistTranslit = sParams[1];
2002-09-06 23:24:40 +00:00
else if( 0==stricmp(sValueName,"CREDIT") )
out.m_sCredit = sParams[1];
else if( 0==stricmp(sValueName,"BANNER") )
out.m_sBannerFile = sParams[1];
else if( 0==stricmp(sValueName,"BACKGROUND") )
out.m_sBackgroundFile = sParams[1];
2003-03-19 19:35:32 +00:00
/* Save "#LYRICS" for later, so we can add an internal lyrics tag. */
else if( 0==stricmp(sValueName,"LYRICSPATH") )
out.m_sLyricsFile = sParams[1];
2002-09-06 23:24:40 +00:00
else if( 0==stricmp(sValueName,"CDTITLE") )
out.m_sCDTitleFile = sParams[1];
else if( 0==stricmp(sValueName,"MUSIC") )
out.m_sMusicFile = sParams[1];
else if( 0==stricmp(sValueName,"MUSICLENGTH") )
2004-02-02 10:06:26 +00:00
{
if(!FromCache)
continue;
2002-09-06 23:24:40 +00:00
out.m_fMusicLengthSeconds = (float)atof( sParams[1] );
2004-02-02 10:06:26 +00:00
}
2002-09-06 23:24:40 +00:00
2003-09-02 20:47:47 +00:00
else if( 0==stricmp(sValueName,"MUSICBYTES") )
; /* ignore */
/* We calculate these. Some SMs in circulation have bogus values for
* these, so make sure we always calculate it ourself. */
2002-09-06 23:24:40 +00:00
else if( 0==stricmp(sValueName,"FIRSTBEAT") )
{
if(!FromCache)
continue;
2002-09-06 23:24:40 +00:00
out.m_fFirstBeat = (float)atof( sParams[1] );
}
2002-09-06 23:24:40 +00:00
else if( 0==stricmp(sValueName,"LASTBEAT") )
{
if(!FromCache)
LOG->Trace("Ignored #LASTBEAT (cache only)");
2002-09-06 23:24:40 +00:00
out.m_fLastBeat = (float)atof( sParams[1] );
}
2003-12-30 02:46:47 +00:00
else if( 0==stricmp(sValueName,"SONGFILENAME") )
{
2003-12-30 03:40:29 +00:00
if( FromCache )
out.m_sSongFileName = sParams[1];
2003-12-30 02:46:47 +00:00
}
2003-12-30 03:40:29 +00:00
else if( 0==stricmp(sValueName,"HASMUSIC") )
{
if( FromCache )
out.m_bHasMusic = atoi( sParams[1] ) != 0;
}
else if( 0==stricmp(sValueName,"HASBANNER") )
{
if( FromCache )
out.m_bHasBanner = atoi( sParams[1] ) != 0;
}
2002-09-06 23:24:40 +00:00
else if( 0==stricmp(sValueName,"SAMPLESTART") )
2004-02-22 19:51:46 +00:00
out.m_fMusicSampleStartSeconds = HHMMSSToSeconds( sParams[1] );
2002-09-06 23:24:40 +00:00
else if( 0==stricmp(sValueName,"SAMPLELENGTH") )
2004-02-22 19:51:46 +00:00
out.m_fMusicSampleLengthSeconds = HHMMSSToSeconds( sParams[1] );
2002-09-06 23:24:40 +00:00
else if( 0==stricmp(sValueName,"DISPLAYBPM") )
{
// #DISPLAYBPM:[xxx][xxx:xxx]|[*];
if( sParams[1] == "*" )
out.m_DisplayBPMType = Song::DISPLAY_RANDOM;
else
{
out.m_DisplayBPMType = Song::DISPLAY_SPECIFIED;
2003-06-16 17:28:58 +00:00
out.m_fSpecifiedBPMMin = (float)atof( sParams[1] );
if( sParams[2].empty() )
2003-06-16 17:28:58 +00:00
out.m_fSpecifiedBPMMax = out.m_fSpecifiedBPMMin;
else
2003-06-16 17:28:58 +00:00
out.m_fSpecifiedBPMMax = (float)atof( sParams[2] );
}
}
2002-09-06 23:24:40 +00:00
else if( 0==stricmp(sValueName,"SELECTABLE") )
{
if(!stricmp(sParams[1],"YES"))
out.m_SelectionDisplay = out.SHOW_ALWAYS;
else if(!stricmp(sParams[1],"NO"))
out.m_SelectionDisplay = out.SHOW_NEVER;
else if(!stricmp(sParams[1],"ROULETTE"))
out.m_SelectionDisplay = out.SHOW_ROULETTE;
else
2003-04-25 00:01:35 +00:00
LOG->Warn( "The song file '%s' has an unknown #SELECTABLE value, '%s'; ignored.", sPath.c_str(), sParams[1].c_str());
2002-09-06 23:24:40 +00:00
}
else if( 0==stricmp(sValueName,"BGCHANGES") || 0==stricmp(sValueName,"ANIMATIONS") )
2002-09-06 23:24:40 +00:00
{
CStringArray aBGChangeExpressions;
split( sParams[1], ",", aBGChangeExpressions );
for( unsigned b=0; b<aBGChangeExpressions.size(); b++ )
2002-09-06 23:24:40 +00:00
{
BackgroundChange change;
2004-01-07 00:13:32 +00:00
if( LoadFromBGChangesString( change, aBGChangeExpressions[b] ) )
out.AddBackgroundChange( change );
2004-01-07 00:13:32 +00:00
}
}
else if( 0==stricmp(sValueName,"FGCHANGES") )
{
CStringArray aFGChangeExpressions;
split( sParams[1], ",", aFGChangeExpressions );
for( unsigned b=0; b<aFGChangeExpressions.size(); b++ )
{
BackgroundChange change;
if( LoadFromBGChangesString( change, aFGChangeExpressions[b] ) )
out.AddForegroundChange( change );
2002-09-06 23:24:40 +00:00
}
}
else if( 0==stricmp(sValueName,"NOTES") )
{
2003-08-03 00:13:55 +00:00
Steps* pNewNotes = new Steps;
2002-09-06 23:24:40 +00:00
ASSERT( pNewNotes );
2004-05-24 03:41:39 +00:00
out.m_vpSteps.push_back( pNewNotes );
2002-09-06 23:24:40 +00:00
2003-11-17 03:38:24 +00:00
if( iNumParams < 7 )
2002-10-06 16:56:58 +00:00
{
2003-11-17 03:38:24 +00:00
LOG->Trace( "The song file '%s' is has %d fields in a #NOTES tag, but should have at least %d.", sPath.c_str(), iNumParams, 7 );
2002-12-14 21:41:34 +00:00
continue;
2002-10-06 16:56:58 +00:00
}
2002-09-06 23:24:40 +00:00
2002-12-14 21:41:34 +00:00
LoadFromSMTokens(
2003-11-17 03:38:24 +00:00
sParams[1], sParams[2], sParams[3], sParams[4], sParams[5], sParams[6], (iNumParams>=8)?sParams[7]:"",
2002-12-14 21:41:34 +00:00
*pNewNotes);
}
2003-12-18 08:21:18 +00:00
else if( 0==stricmp(sValueName,"OFFSET") || 0==stricmp(sValueName,"BPMS") ||
0==stricmp(sValueName,"STOPS") || 0==stricmp(sValueName,"FREEZES") )
;
2002-09-06 23:24:40 +00:00
else
2003-04-25 00:01:35 +00:00
LOG->Trace( "Unexpected value named '%s'", sValueName.c_str() );
2002-09-06 23:24:40 +00:00
}
return true;
}
2002-09-11 05:15:46 +00:00
bool SMLoader::LoadFromDir( CString sPath, Song &out )
{
CStringArray aFileNames;
GetApplicableFiles( sPath, aFileNames );
if( aFileNames.size() > 1 )
2003-04-25 00:01:35 +00:00
RageException::Throw( "There is more than one SM file in '%s'. There should be only one!", sPath.c_str() );
2002-09-11 05:15:46 +00:00
/* We should have exactly one; if we had none, we shouldn't have been
* called to begin with. */
ASSERT( aFileNames.size() == 1 );
2002-09-11 05:15:46 +00:00
return LoadFromSMFile( sPath + aFileNames[0], out );
}
bool SMLoader::LoadEdit( CString sEditFilePath, ProfileSlot slot )
2004-02-08 01:05:53 +00:00
{
LOG->Trace( "Song::LoadEdit(%s)", sEditFilePath.c_str() );
2004-04-23 00:26:51 +00:00
int iBytes = FILEMAN->GetFileSizeInBytes( sEditFilePath );
if( iBytes > MAX_EDIT_SIZE_BYTES )
{
LOG->Warn( "The edit '%s' is unreasonably large. It won't be loaded.", sEditFilePath.c_str() );
return false;
}
2004-02-08 01:05:53 +00:00
MsdFile msd;
bool bResult = msd.ReadFile( sEditFilePath );
if( !bResult )
RageException::Throw( "Error opening file '%s'.", sEditFilePath.c_str() );
Song* pSong = NULL;
for( unsigned i=0; i<msd.GetNumValues(); i++ )
{
int iNumParams = msd.GetNumParams(i);
const MsdFile::value_t &sParams = msd.GetValue(i);
const CString sValueName = sParams[0];
// handle the data
if( 0==stricmp(sValueName,"SONG") )
{
if( pSong )
{
LOG->Warn( "The edit file '%s' has more than one #SONG tag.", sEditFilePath.c_str() );
return false;
}
2004-02-08 06:22:58 +00:00
CString sSongFullTitle = sParams[1];
2004-02-08 01:05:53 +00:00
sSongFullTitle.Replace( '\\', '/' );
pSong = SONGMAN->FindSong( sSongFullTitle );
if( pSong == NULL )
{
LOG->Warn( "The edit file '%s' required a song '%s' that isn't present.", sEditFilePath.c_str(), sSongFullTitle.c_str() );
return false;
}
2004-04-23 00:26:51 +00:00
2004-04-23 00:27:58 +00:00
if( pSong->GetNumStepsLoadedFromProfile(slot) >= MAX_EDITS_PER_SONG_PER_PROFILE )
2004-04-23 00:26:51 +00:00
{
LOG->Warn( "The song '%s' already has the maximum number of edits allowed for ProfileSlotP%d.", sSongFullTitle.c_str(), slot+1 );
return false;
}
2004-02-08 01:05:53 +00:00
}
else if( 0==stricmp(sValueName,"NOTES") )
{
if( pSong == NULL )
{
LOG->Warn( "The edit file '%s' has doesn't have a #SONG tag preceeding the first #NOTES tag.", sEditFilePath.c_str() );
return false;
}
Steps* pNewNotes = new Steps;
ASSERT( pNewNotes );
if( iNumParams < 7 )
{
LOG->Trace( "The song file '%s' is has %d fields in a #NOTES tag, but should have at least %d.", sEditFilePath.c_str(), iNumParams, 7 );
continue;
}
LoadFromSMTokens(
2004-02-08 06:22:58 +00:00
sParams[1], sParams[2], sParams[3], sParams[4], sParams[5], sParams[6], (iNumParams>=8)?sParams[7]:"",
2004-02-08 01:05:53 +00:00
*pNewNotes);
pNewNotes->SetLoadedFromProfile( slot );
pNewNotes->SetDifficulty( DIFFICULTY_EDIT );
2004-04-23 00:26:51 +00:00
2004-04-23 01:33:08 +00:00
if( pSong->IsEditAlreadyLoaded(pNewNotes) )
{
LOG->Warn( "The edit file '%s' is a duplicate of another edit that was already loaded.", sEditFilePath.c_str() );
SAFE_DELETE( pNewNotes );
return false;
}
2004-05-24 03:41:39 +00:00
pSong->m_vpSteps.push_back( pNewNotes );
2004-04-23 00:26:51 +00:00
return true; // Only allow one Steps per edit file!
2004-02-08 01:05:53 +00:00
}
else
LOG->Trace( "Unexpected value named '%s'", sValueName.c_str() );
}
return true;
}