Files
itgmania212121/stepmania/src/NotesLoaderSM.cpp
T

643 lines
19 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"
2006-11-29 22:04:29 +00:00
#include "BackgroundUtil.h"
2002-09-06 23:24:40 +00:00
#include "GameManager.h"
#include "MsdFile.h"
2006-11-29 22:04:29 +00:00
#include "NoteTypes.h"
#include "RageFileManager.h"
2002-09-06 23:24:40 +00:00
#include "RageLog.h"
2002-09-07 11:43:36 +00:00
#include "RageUtil.h"
2005-07-03 02:51:29 +00:00
#include "song.h"
2006-11-29 22:04:29 +00:00
#include "SongManager.h"
2005-07-03 02:51:29 +00:00
#include "Steps.h"
2004-04-23 00:26:51 +00:00
2005-07-31 05:41:32 +00:00
const int MAX_EDIT_STEPS_SIZE_BYTES = 30*1024; // 30KB
2002-09-06 23:24:40 +00:00
2007-02-14 10:08:19 +00:00
static void LoadFromSMTokens(
2006-01-22 01:00:06 +00:00
RString sStepsType,
RString sDescription,
RString sDifficulty,
RString sMeter,
RString sRadarValues,
RString sNoteData,
2003-08-03 00:13:55 +00:00
Steps &out
2002-09-06 23:24:40 +00:00
)
{
out.SetSavedToDisk( true ); // we're loading from disk, so this is by definintion already saved
2006-06-20 06:28:57 +00:00
TrimLeft( sStepsType ); TrimRight( sStepsType );
TrimLeft( sDescription ); TrimRight( sDescription );
TrimLeft( sDifficulty ); TrimRight( sDifficulty );
2007-02-03 02:39:50 +00:00
TrimLeft( sNoteData ); TrimRight( sNoteData );
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
2006-06-20 06:28:57 +00:00
out.m_StepsType = GameManager::StringToStepsType( sStepsType );
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 );
2006-06-20 06:28:57 +00:00
out.SetMeter( atoi(sMeter) );
2006-01-22 01:00:06 +00:00
vector<RString> saValues;
2002-09-06 23:24:40 +00:00
split( sRadarValues, ",", saValues, true );
if( saValues.size() == NUM_RadarCategory * NUM_PLAYERS )
2004-07-11 07:21:33 +00:00
{
RadarValues v[NUM_PLAYERS];
FOREACH_PlayerNumber( pn )
FOREACH_RadarCategory( rc )
v[pn][rc] = StringToFloat( saValues[pn*NUM_RadarCategory + rc] );
out.SetCachedRadarValues( v );
2004-07-11 07:21:33 +00:00
}
2002-09-06 23:24:40 +00:00
2006-06-20 06:28:57 +00:00
out.SetSMNoteData( sNoteData );
2002-09-06 23:24:40 +00:00
out.TidyUpData();
}
2006-02-02 23:52:28 +00:00
void SMLoader::GetApplicableFiles( const RString &sPath, vector<RString> &out )
2002-09-11 05:15:46 +00:00
{
2006-01-22 01:00:06 +00:00
GetDirListing( sPath + RString("*.sm"), out );
2002-09-11 05:15:46 +00:00
}
2002-09-06 23:24:40 +00:00
2006-01-22 01:00:06 +00:00
bool SMLoader::LoadTimingFromFile( const RString &fn, TimingData &out )
2004-01-17 23:21:07 +00:00
{
MsdFile msd;
if( !msd.ReadFile( fn, true ) ) // unescape
2004-01-17 23:21:07 +00:00
{
2006-09-24 03:57:26 +00:00
LOG->UserLog( "Song file", fn, "couldn't be loaded: %s", msd.GetError().c_str() );
2004-01-17 23:21:07 +00:00
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);
2006-01-22 01:00:06 +00:00
RString sValueName = sParams[0];
2005-05-26 09:35:57 +00:00
sValueName.MakeUpper();
2003-12-18 08:21:18 +00:00
2005-05-26 09:35:57 +00:00
if( sValueName=="OFFSET" )
{
2006-06-12 06:42:25 +00:00
out.m_fBeat0OffsetInSeconds = StringToFloat( sParams[1] );
2005-05-26 09:35:57 +00:00
}
else if( sValueName=="STOPS" || sValueName=="FREEZES" )
2003-12-18 08:21:18 +00:00
{
2006-01-22 01:00:06 +00:00
vector<RString> arrayFreezeExpressions;
2003-12-18 08:21:18 +00:00
split( sParams[1], ",", arrayFreezeExpressions );
for( unsigned f=0; f<arrayFreezeExpressions.size(); f++ )
{
2006-01-22 01:00:06 +00:00
vector<RString> arrayFreezeValues;
2003-12-18 08:21:18 +00:00
split( arrayFreezeExpressions[f], "=", arrayFreezeValues );
if( arrayFreezeValues.size() != 2 )
{
// XXX: Hard to tell which file caused this.
2006-09-24 03:57:26 +00:00
LOG->UserLog( "Song file", "(UNKNOWN)", "has an invalid #%s value \"%s\" (must have exactly one '='), ignored.",
sValueName.c_str(), arrayFreezeExpressions[f].c_str() );
2003-12-18 08:21:18 +00:00
continue;
}
2006-06-12 06:42:25 +00:00
const float fFreezeBeat = StringToFloat( arrayFreezeValues[0] );
const float fFreezeSeconds = StringToFloat( arrayFreezeValues[1] );
2003-12-18 08:21:18 +00:00
StopSegment new_seg( BeatToNoteRow(fFreezeBeat), fFreezeSeconds );
2003-12-18 08:21:18 +00:00
// LOG->Trace( "Adding a freeze segment: beat: %f, seconds = %f", new_seg.m_fStartBeat, new_seg.m_fStopSeconds );
out.AddStopSegment( new_seg );
}
}
2005-05-26 09:35:57 +00:00
else if( sValueName=="BPMS" )
2003-12-18 08:21:18 +00:00
{
2006-01-22 01:00:06 +00:00
vector<RString> arrayBPMChangeExpressions;
2003-12-18 08:21:18 +00:00
split( sParams[1], ",", arrayBPMChangeExpressions );
for( unsigned b=0; b<arrayBPMChangeExpressions.size(); b++ )
{
2006-01-22 01:00:06 +00:00
vector<RString> arrayBPMChangeValues;
2003-12-18 08:21:18 +00:00
split( arrayBPMChangeExpressions[b], "=", arrayBPMChangeValues );
// XXX: Hard to tell which file caused this.
if( arrayBPMChangeValues.size() != 2 )
2003-12-18 08:21:18 +00:00
{
2006-09-24 03:57:26 +00:00
LOG->UserLog( "Song file", "(UNKNOWN)", "has an invalid #%s value \"%s\" (must have exactly one '='), ignored.",
sValueName.c_str(), arrayBPMChangeExpressions[b].c_str() );
2003-12-18 08:21:18 +00:00
continue;
}
2006-06-12 06:42:25 +00:00
const float fBeat = StringToFloat( arrayBPMChangeValues[0] );
const float fNewBPM = StringToFloat( arrayBPMChangeValues[1] );
2003-12-18 08:21:18 +00:00
if( fNewBPM > 0.0f )
out.AddBPMSegment( BPMSegment(BeatToNoteRow(fBeat), fNewBPM) );
else
2006-09-24 03:57:26 +00:00
LOG->UserLog( "Song file", "(UNKNOWN)", "has an invalid BPM change at beat %f, BPM %f.", fBeat, fNewBPM );
2003-12-18 08:21:18 +00:00
}
}
}
}
2006-01-22 01:00:06 +00:00
bool LoadFromBGChangesString( BackgroundChange &change, const RString &sBGChangeExpression )
2004-01-07 00:13:32 +00:00
{
2006-01-22 01:00:06 +00:00
vector<RString> aBGChangeValues;
2005-06-05 03:36:32 +00:00
split( sBGChangeExpression, "=", aBGChangeValues, false );
2004-01-07 00:13:32 +00:00
2005-06-05 22:21:55 +00:00
aBGChangeValues.resize( min((int)aBGChangeValues.size(),11) );
switch( aBGChangeValues.size() )
{
2005-06-05 22:21:55 +00:00
case 11:
change.m_def.m_sColor2 = aBGChangeValues[10];
// .sm files made before we started escaping will still have '^' instead of ','
change.m_def.m_sColor2.Replace( '^', ',' );
2005-06-05 22:21:55 +00:00
// fall through
case 10:
change.m_def.m_sColor1 = aBGChangeValues[9];
// .sm files made before we started escaping will still have '^' instead of ','
change.m_def.m_sColor1.Replace( '^', ',' );
2005-06-05 22:21:55 +00:00
// fall through
case 9:
change.m_sTransition = aBGChangeValues[8];
2005-06-05 22:21:55 +00:00
// fall through
case 8:
change.m_def.m_sFile2 = aBGChangeValues[7];
// fall through
case 7:
change.m_def.m_sEffect = aBGChangeValues[6];
// fall through
case 6:
// param 7 overrides this.
// Backward compatibility:
if( change.m_def.m_sEffect.empty() )
{
bool bLoop = atoi( aBGChangeValues[5] ) != 0;
if( !bLoop )
change.m_def.m_sEffect = SBE_StretchNoLoop;
}
// fall through
case 5:
// param 7 overrides this.
// Backward compatibility:
if( change.m_def.m_sEffect.empty() )
{
bool bRewindMovie = atoi( aBGChangeValues[4] ) != 0;
if( bRewindMovie )
change.m_def.m_sEffect = SBE_StretchRewind;
}
// fall through
case 4:
// param 9 overrides this.
// Backward compatibility:
if( change.m_sTransition.empty() )
change.m_sTransition = (atoi( aBGChangeValues[3] ) != 0) ? "CrossFade" : "";
// fall through
case 3:
2006-06-12 06:42:25 +00:00
change.m_fRate = StringToFloat( aBGChangeValues[2] );
2005-06-05 22:21:55 +00:00
// fall through
case 2:
2005-06-04 21:22:50 +00:00
change.m_def.m_sFile1 = aBGChangeValues[1];
2005-06-05 22:21:55 +00:00
// fall through
case 1:
2006-06-12 06:42:25 +00:00
change.m_fStartBeat = StringToFloat( aBGChangeValues[0] );
2005-06-05 22:21:55 +00:00
// fall through
2004-01-07 00:13:32 +00:00
}
2005-06-05 22:21:55 +00:00
return aBGChangeValues.size() >= 2;
2004-01-07 00:13:32 +00:00
}
2007-02-14 10:08:19 +00:00
bool SMLoader::LoadFromSMFile( const RString &sPath, Song &out, bool bFromCache )
2002-09-06 23:24:40 +00:00
{
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;
if( !msd.ReadFile( sPath, true ) ) // unescape
2005-01-25 18:54:46 +00:00
{
2006-09-24 03:57:26 +00:00
LOG->UserLog( "Song file", sPath, "couldn't be opened: %s", msd.GetError().c_str() );
2005-01-25 18:54:46 +00:00
return false;
}
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);
2006-01-22 01:00:06 +00:00
RString sValueName = sParams[0];
2005-05-26 09:35:57 +00:00
sValueName.MakeUpper();
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. */
2005-05-26 09:35:57 +00:00
if( sValueName=="TITLE" )
out.m_sMainTitle = sParams[1];
2002-09-06 23:24:40 +00:00
2005-05-26 09:35:57 +00:00
else if( sValueName=="SUBTITLE" )
2002-09-06 23:24:40 +00:00
out.m_sSubTitle = sParams[1];
2005-05-26 09:35:57 +00:00
else if( sValueName=="ARTIST" )
2002-09-06 23:24:40 +00:00
out.m_sArtist = sParams[1];
2005-05-26 09:35:57 +00:00
else if( sValueName=="TITLETRANSLIT" )
out.m_sMainTitleTranslit = sParams[1];
2005-05-26 09:35:57 +00:00
else if( sValueName=="SUBTITLETRANSLIT" )
out.m_sSubTitleTranslit = sParams[1];
2005-05-26 09:35:57 +00:00
else if( sValueName=="ARTISTTRANSLIT" )
out.m_sArtistTranslit = sParams[1];
2005-05-26 09:35:57 +00:00
else if( sValueName=="GENRE" )
2005-01-31 05:25:27 +00:00
out.m_sGenre = sParams[1];
2005-05-26 09:35:57 +00:00
else if( sValueName=="CREDIT" )
2002-09-06 23:24:40 +00:00
out.m_sCredit = sParams[1];
2005-05-26 09:35:57 +00:00
else if( sValueName=="BANNER" )
2002-09-06 23:24:40 +00:00
out.m_sBannerFile = sParams[1];
2005-05-26 09:35:57 +00:00
else if( sValueName=="BACKGROUND" )
2002-09-06 23:24:40 +00:00
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. */
2005-05-26 09:35:57 +00:00
else if( sValueName=="LYRICSPATH" )
2003-03-19 19:35:32 +00:00
out.m_sLyricsFile = sParams[1];
2005-05-26 09:35:57 +00:00
else if( sValueName=="CDTITLE" )
2002-09-06 23:24:40 +00:00
out.m_sCDTitleFile = sParams[1];
2005-05-26 09:35:57 +00:00
else if( sValueName=="MUSIC" )
2002-09-06 23:24:40 +00:00
out.m_sMusicFile = sParams[1];
2005-05-26 09:35:57 +00:00
else if( sValueName=="MUSICLENGTH" )
2004-02-02 10:06:26 +00:00
{
2007-02-14 10:08:19 +00:00
if( !bFromCache )
2004-02-02 10:06:26 +00:00
continue;
2006-06-12 06:42:25 +00:00
out.m_fMusicLengthSeconds = StringToFloat( sParams[1] );
2004-02-02 10:06:26 +00:00
}
else if( sValueName=="LASTBEATHINT" )
out.m_fSpecifiedLastBeat = StringToFloat( sParams[1] );
2002-09-06 23:24:40 +00:00
2005-05-26 09:35:57 +00:00
else if( sValueName=="MUSICBYTES" )
2003-09-02 20:47:47 +00:00
; /* ignore */
/* We calculate these. Some SMs in circulation have bogus values for
* these, so make sure we always calculate it ourself. */
2005-05-26 09:35:57 +00:00
else if( sValueName=="FIRSTBEAT" )
{
2007-02-14 10:08:19 +00:00
if( bFromCache )
out.m_fFirstBeat = StringToFloat( sParams[1] );
}
2005-05-26 09:35:57 +00:00
else if( sValueName=="LASTBEAT" )
{
2007-02-14 10:08:19 +00:00
if( bFromCache )
out.m_fLastBeat = StringToFloat( sParams[1] );
}
2005-05-26 09:35:57 +00:00
else if( sValueName=="SONGFILENAME" )
2003-12-30 02:46:47 +00:00
{
2007-02-14 10:08:19 +00:00
if( bFromCache )
2003-12-30 03:40:29 +00:00
out.m_sSongFileName = sParams[1];
2003-12-30 02:46:47 +00:00
}
2005-05-26 09:35:57 +00:00
else if( sValueName=="HASMUSIC" )
2003-12-30 03:40:29 +00:00
{
2007-02-14 10:08:19 +00:00
if( bFromCache )
2003-12-30 03:40:29 +00:00
out.m_bHasMusic = atoi( sParams[1] ) != 0;
}
2005-05-26 09:35:57 +00:00
else if( sValueName=="HASBANNER" )
2003-12-30 03:40:29 +00:00
{
2007-02-14 10:08:19 +00:00
if( bFromCache )
2003-12-30 03:40:29 +00:00
out.m_bHasBanner = atoi( sParams[1] ) != 0;
}
2005-05-26 09:35:57 +00:00
else if( sValueName=="SAMPLESTART" )
2004-02-22 19:51:46 +00:00
out.m_fMusicSampleStartSeconds = HHMMSSToSeconds( sParams[1] );
2002-09-06 23:24:40 +00:00
2005-05-26 09:35:57 +00:00
else if( sValueName=="SAMPLELENGTH" )
2004-02-22 19:51:46 +00:00
out.m_fMusicSampleLengthSeconds = HHMMSSToSeconds( sParams[1] );
2002-09-06 23:24:40 +00:00
2005-05-26 09:35:57 +00:00
else if( sValueName=="DISPLAYBPM" )
{
// #DISPLAYBPM:[xxx][xxx:xxx]|[*];
if( sParams[1] == "*" )
out.m_DisplayBPMType = Song::DISPLAY_RANDOM;
else
{
out.m_DisplayBPMType = Song::DISPLAY_SPECIFIED;
2006-06-12 06:42:25 +00:00
out.m_fSpecifiedBPMMin = StringToFloat( sParams[1] );
if( sParams[2].empty() )
2003-06-16 17:28:58 +00:00
out.m_fSpecifiedBPMMax = out.m_fSpecifiedBPMMin;
else
2006-06-12 06:42:25 +00:00
out.m_fSpecifiedBPMMax = StringToFloat( sParams[2] );
}
}
2005-05-26 09:35:57 +00:00
else if( sValueName=="SELECTABLE" )
2002-09-06 23:24:40 +00:00
{
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
2006-09-24 03:57:26 +00:00
LOG->UserLog( "Song file", sPath, "has an unknown #SELECTABLE value, \"%s\"; ignored.", sParams[1].c_str() );
2002-09-06 23:24:40 +00:00
}
2005-06-05 03:29:55 +00:00
else if( sValueName.Left(strlen("BGCHANGES"))=="BGCHANGES" || sValueName=="ANIMATIONS" )
2002-09-06 23:24:40 +00:00
{
2005-06-04 21:22:50 +00:00
BackgroundLayer iLayer = BACKGROUND_LAYER_1;
if( sscanf(sValueName, "BGCHANGES%d", &*ConvertValue<int>(&iLayer)) == 1 )
enum_add(iLayer, -1); // #BGCHANGES2 = BACKGROUND_LAYER_2
2002-09-06 23:24:40 +00:00
2005-06-05 03:24:30 +00:00
bool bValid = iLayer>=0 && iLayer<NUM_BackgroundLayer;
if( !bValid )
2002-09-06 23:24:40 +00:00
{
2006-09-24 03:57:26 +00:00
LOG->UserLog( "Song file", sPath, "has a #BGCHANGES tag \"%s\" that is out of range.", sValueName.c_str() );
2005-05-26 09:35:57 +00:00
}
else
{
2006-01-22 01:00:06 +00:00
vector<RString> aBGChangeExpressions;
2005-05-26 09:35:57 +00:00
split( sParams[1], ",", aBGChangeExpressions );
for( unsigned b=0; b<aBGChangeExpressions.size(); b++ )
{
BackgroundChange change;
if( LoadFromBGChangesString( change, aBGChangeExpressions[b] ) )
out.AddBackgroundChange( iLayer, change );
}
2004-01-07 00:13:32 +00:00
}
}
2005-05-26 09:35:57 +00:00
else if( sValueName=="FGCHANGES" )
2004-01-07 00:13:32 +00:00
{
2006-01-22 01:00:06 +00:00
vector<RString> aFGChangeExpressions;
2004-01-07 00:13:32 +00:00
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
}
}
2005-05-26 09:35:57 +00:00
else if( sValueName=="KEYSOUNDS" )
{
2006-06-20 09:31:51 +00:00
split( sParams[1], ",", out.m_vsKeysoundFile );
}
2005-05-26 09:35:57 +00:00
else if( sValueName=="NOTES" || sValueName=="NOTES2" )
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
{
2006-09-24 03:57:26 +00:00
LOG->UserLog( "Song file", sPath, "has %d fields in a #NOTES tag, but should have at least 7.", iNumParams );
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
2004-12-06 07:32:29 +00:00
Steps* pNewNotes = new Steps;
2002-12-14 21:41:34 +00:00
LoadFromSMTokens(
sParams[1],
sParams[2],
sParams[3],
sParams[4],
sParams[5],
2004-10-23 23:41:49 +00:00
sParams[6],
*pNewNotes );
2004-06-05 05:39:43 +00:00
out.AddSteps( pNewNotes );
2002-12-14 21:41:34 +00:00
}
else if( sValueName=="OFFSET" || sValueName=="BPMS" || sValueName=="STOPS" || sValueName=="FREEZES" )
2003-12-18 08:21:18 +00:00
;
2002-09-06 23:24:40 +00:00
else
2006-09-24 03:57:26 +00:00
LOG->UserLog( "Song file", sPath, "has an 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
2006-02-02 23:52:28 +00:00
bool SMLoader::LoadFromDir( const RString &sPath, Song &out )
2002-09-11 05:15:46 +00:00
{
2006-01-22 01:00:06 +00:00
vector<RString> aFileNames;
2002-09-11 05:15:46 +00:00
GetApplicableFiles( sPath, aFileNames );
if( aFileNames.size() > 1 )
2005-01-25 18:54:46 +00:00
{
2006-09-24 03:57:26 +00:00
LOG->UserLog( "Song", sPath, "has more than one SM file. There should be only one!" );
2005-01-25 18:54:46 +00:00
return false;
}
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::LoadEditFromFile( RString sEditFilePath, ProfileSlot slot, bool bAddStepsToSong )
2004-02-08 01:05:53 +00:00
{
LOG->Trace( "SMLoader::LoadEditFromFile(%s)", sEditFilePath.c_str() );
2004-02-08 01:05:53 +00:00
2004-04-23 00:26:51 +00:00
int iBytes = FILEMAN->GetFileSizeInBytes( sEditFilePath );
2005-07-31 05:41:32 +00:00
if( iBytes > MAX_EDIT_STEPS_SIZE_BYTES )
2004-04-23 00:26:51 +00:00
{
2006-09-24 03:57:26 +00:00
LOG->UserLog( "Edit file", sEditFilePath, "is unreasonably large. It won't be loaded." );
2004-04-23 00:26:51 +00:00
return false;
}
2004-02-08 01:05:53 +00:00
MsdFile msd;
if( !msd.ReadFile( sEditFilePath, true ) ) // unescape
2005-01-25 18:54:46 +00:00
{
2006-09-24 03:57:26 +00:00
LOG->UserLog( "Edit file", sEditFilePath, "couldn't be opened: %s", msd.GetError().c_str() );
2005-01-25 18:54:46 +00:00
return false;
}
2004-02-08 01:05:53 +00:00
return LoadEditFromMsd( msd, sEditFilePath, slot, bAddStepsToSong );
}
2006-02-02 23:52:28 +00:00
bool SMLoader::LoadEditFromBuffer( const RString &sBuffer, const RString &sEditFilePath, ProfileSlot slot )
{
MsdFile msd;
msd.ReadFromString( sBuffer, true ); // unescape
return LoadEditFromMsd( msd, sEditFilePath, slot, true );
}
bool SMLoader::LoadEditFromMsd( const MsdFile &msd, const RString &sEditFilePath, ProfileSlot slot, bool bAddStepsToSong )
{
2004-02-08 01:05:53 +00:00
Song* pSong = NULL;
for( unsigned i=0; i<msd.GetNumValues(); i++ )
{
int iNumParams = msd.GetNumParams(i);
const MsdFile::value_t &sParams = msd.GetValue(i);
2006-01-22 01:00:06 +00:00
RString sValueName = sParams[0];
2005-05-26 09:35:57 +00:00
sValueName.MakeUpper();
2004-02-08 01:05:53 +00:00
// handle the data
2005-05-26 09:35:57 +00:00
if( sValueName=="SONG" )
2004-02-08 01:05:53 +00:00
{
if( pSong )
{
2006-09-24 03:57:26 +00:00
LOG->UserLog( "Edit file", sEditFilePath, "has more than one #SONG tag." );
2004-02-08 01:05:53 +00:00
return false;
}
2006-01-22 01:00:06 +00:00
RString sSongFullTitle = sParams[1];
2004-02-08 01:05:53 +00:00
sSongFullTitle.Replace( '\\', '/' );
pSong = SONGMAN->FindSong( sSongFullTitle );
if( pSong == NULL )
{
2006-09-24 03:57:26 +00:00
LOG->UserLog( "Edit file", sEditFilePath, "requires a song \"%s\" that isn't present.", sSongFullTitle.c_str() );
2004-02-08 01:05:53 +00:00
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
{
2006-09-24 03:57:26 +00:00
LOG->UserLog( "Song file", sSongFullTitle, "already has the maximum number of edits allowed for ProfileSlotP%d.", slot+1 );
2004-04-23 00:26:51 +00:00
return false;
}
2004-02-08 01:05:53 +00:00
}
2005-05-26 09:35:57 +00:00
else if( sValueName=="NOTES" )
2004-02-08 01:05:53 +00:00
{
if( pSong == NULL )
{
2006-09-24 03:57:26 +00:00
LOG->UserLog( "Edit file", sEditFilePath, "doesn't have a #SONG tag preceeding the first #NOTES tag." );
2004-02-08 01:05:53 +00:00
return false;
}
if( iNumParams < 7 )
{
2006-09-24 03:57:26 +00:00
LOG->UserLog( "Edit file", sEditFilePath, "has %d fields in a #NOTES tag, but should have at least 7.", iNumParams );
2004-02-08 01:05:53 +00:00
continue;
}
if( !bAddStepsToSong )
return true;
2004-12-06 07:32:29 +00:00
Steps* pNewNotes = new Steps;
2004-02-08 01:05:53 +00:00
LoadFromSMTokens(
2004-10-23 23:41:49 +00:00
sParams[1], sParams[2], sParams[3], sParams[4], sParams[5], sParams[6],
2004-02-08 01:05:53 +00:00
*pNewNotes);
pNewNotes->SetLoadedFromProfile( slot );
pNewNotes->SetDifficulty( DIFFICULTY_EDIT );
2005-12-06 12:41:33 +00:00
pNewNotes->SetFilename( sEditFilePath );
2004-04-23 01:33:08 +00:00
if( pSong->IsEditAlreadyLoaded(pNewNotes) )
{
2006-09-24 03:57:26 +00:00
LOG->UserLog( "Edit file", sEditFilePath, "is a duplicate of another edit that was already loaded." );
2004-04-23 01:33:08 +00:00
SAFE_DELETE( pNewNotes );
return false;
}
2004-06-05 05:13:23 +00:00
pSong->AddSteps( 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
{
2006-09-24 03:57:26 +00:00
LOG->UserLog( "Edit file", sEditFilePath, "has an unexpected value \"%s\".", sValueName.c_str() );
}
2004-02-08 01:05:53 +00:00
}
return true;
}
2004-05-31 21:55:14 +00:00
2007-02-14 10:08:19 +00:00
void SMLoader::TidyUpData( Song &song, bool bFromCache )
{
/*
* Hack: if the song has any changes at all (so it won't use a random BGA)
* and doesn't end with "-nosongbg-", add a song background BGC. Remove
* "-nosongbg-" if it exists.
*
* This way, songs that were created earlier, when we added the song BG
* at the end by default, will still behave as expected; all new songs will
* have to add an explicit song BG tag if they want it. This is really a
* formatting hack only; nothing outside of SMLoader ever sees "-nosongbg-".
*/
2005-06-04 21:22:50 +00:00
vector<BackgroundChange> &bg = song.GetBackgroundChanges(BACKGROUND_LAYER_1);
if( !bg.empty() )
{
/* BGChanges have been sorted. On the odd chance that a BGChange exists
* with a very high beat, search the whole list. */
bool bHasNoSongBgTag = false;
for( unsigned i = 0; !bHasNoSongBgTag && i < bg.size(); ++i )
{
2005-06-04 21:22:50 +00:00
if( !bg[i].m_def.m_sFile1.CompareNoCase(NO_SONG_BG_FILE) )
{
bg.erase( bg.begin()+i );
bHasNoSongBgTag = true;
}
}
/* If there's no -nosongbg- tag, add the song BG. */
if( !bHasNoSongBgTag ) do
{
/* If we're loading cache, -nosongbg- should always be in there. We must
* not call IsAFile(song.GetBackgroundPath()) when loading cache. */
2007-02-14 10:08:19 +00:00
if( bFromCache )
break;
/* If BGChanges already exist after the last beat, don't add the background
* in the middle. */
if( !bg.empty() && bg.back().m_fStartBeat-0.0001f >= song.m_fLastBeat )
break;
/* If the last BGA is already the song BGA, don't add a duplicate. */
2005-06-04 21:22:50 +00:00
if( !bg.empty() && !bg.back().m_def.m_sFile1.CompareNoCase(song.m_sBackgroundFile) )
break;
if( !IsAFile( song.GetBackgroundPath() ) )
break;
bg.push_back( BackgroundChange(song.m_fLastBeat,song.m_sBackgroundFile) );
} while(0);
}
}
2004-05-31 21:55:14 +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.
*/