Files
itgmania212121/stepmania/src/NotesWriterSM.cpp
T

349 lines
12 KiB
C++
Raw Normal View History

2003-02-16 04:01:45 +00:00
#include "global.h"
2002-12-13 23:56:13 +00:00
#include "NotesWriterSM.h"
2003-08-03 00:13:55 +00:00
#include "Steps.h"
2002-12-14 21:36:52 +00:00
#include "RageUtil.h"
#include "GameManager.h"
#include "RageLog.h"
2003-07-22 07:56:46 +00:00
#include "RageFile.h"
#include "RageFileManager.h"
2004-05-31 21:55:14 +00:00
#include "song.h"
#include "NoteTypes.h"
2004-04-05 05:22:32 +00:00
#include <cstring>
#include <cerrno>
2005-05-26 09:35:57 +00:00
#include "Foreach.h"
2005-06-04 21:22:50 +00:00
#include "BackgroundUtil.h"
2005-12-03 20:34:49 +00:00
#include "ProfileManager.h"
#include "Profile.h"
#include "LocalizedString.h"
2002-12-13 23:56:13 +00:00
2006-01-22 01:00:06 +00:00
static RString BackgroundChangeToString( const BackgroundChange &bgc )
{
2006-01-22 01:00:06 +00:00
RString s = ssprintf(
2005-06-05 22:21:55 +00:00
"%.3f=%s=%.3f=%d=%d=%d=%s=%s=%s=%s=%s",
bgc.m_fStartBeat,
2005-06-04 21:22:50 +00:00
bgc.m_def.m_sFile1.c_str(),
bgc.m_fRate,
bgc.m_sTransition == SBT_CrossFade, // backward compat
2005-06-04 21:22:50 +00:00
bgc.m_def.m_sEffect == SBE_StretchRewind, // backward compat
bgc.m_def.m_sEffect != SBE_StretchNoLoop, // backward compat
bgc.m_def.m_sEffect.c_str(),
bgc.m_def.m_sFile2.c_str(),
2005-06-05 22:21:55 +00:00
bgc.m_sTransition.c_str(),
bgc.m_def.m_sColor1.c_str(),
bgc.m_def.m_sColor2.c_str()
);
2005-06-05 22:21:55 +00:00
s.Replace( ',', '^' ); // UGLY: escape "," in colors.
return s;
}
2003-12-04 22:58:18 +00:00
void NotesWriterSM::WriteGlobalTags( RageFile &f, const Song &out )
2002-12-13 23:56:13 +00:00
{
2003-12-04 22:58:18 +00:00
f.PutLine( ssprintf( "#TITLE:%s;", out.m_sMainTitle.c_str() ) );
f.PutLine( ssprintf( "#SUBTITLE:%s;", out.m_sSubTitle.c_str() ) );
f.PutLine( ssprintf( "#ARTIST:%s;", out.m_sArtist.c_str() ) );
f.PutLine( ssprintf( "#TITLETRANSLIT:%s;", out.m_sMainTitleTranslit.c_str() ) );
f.PutLine( ssprintf( "#SUBTITLETRANSLIT:%s;", out.m_sSubTitleTranslit.c_str() ) );
f.PutLine( ssprintf( "#ARTISTTRANSLIT:%s;", out.m_sArtistTranslit.c_str() ) );
2005-01-31 05:25:27 +00:00
f.PutLine( ssprintf( "#GENRE:%s;", out.m_sGenre.c_str() ) );
2003-12-04 22:58:18 +00:00
f.PutLine( ssprintf( "#CREDIT:%s;", out.m_sCredit.c_str() ) );
f.PutLine( ssprintf( "#BANNER:%s;", out.m_sBannerFile.c_str() ) );
f.PutLine( ssprintf( "#BACKGROUND:%s;", out.m_sBackgroundFile.c_str() ) );
f.PutLine( ssprintf( "#LYRICSPATH:%s;", out.m_sLyricsFile.c_str() ) );
f.PutLine( ssprintf( "#CDTITLE:%s;", out.m_sCDTitleFile.c_str() ) );
f.PutLine( ssprintf( "#MUSIC:%s;", out.m_sMusicFile.c_str() ) );
2003-12-18 04:48:26 +00:00
f.PutLine( ssprintf( "#OFFSET:%.3f;", out.m_Timing.m_fBeat0OffsetInSeconds ) );
2003-12-04 22:58:18 +00:00
f.PutLine( ssprintf( "#SAMPLESTART:%.3f;", out.m_fMusicSampleStartSeconds ) );
f.PutLine( ssprintf( "#SAMPLELENGTH:%.3f;", out.m_fMusicSampleLengthSeconds ) );
f.Write( "#SELECTABLE:" );
2003-06-05 19:27:30 +00:00
switch(out.m_SelectionDisplay) {
default: ASSERT(0); /* fallthrough */
case Song::SHOW_ALWAYS: f.Write( "YES" ); break;
case Song::SHOW_NEVER: f.Write( "NO" ); break;
case Song::SHOW_ROULETTE: f.Write( "ROULETTE" ); break;
2003-06-05 19:27:30 +00:00
}
2003-12-04 22:58:18 +00:00
f.PutLine( ";" );
2003-06-05 19:27:30 +00:00
switch( out.m_DisplayBPMType )
{
case Song::DISPLAY_ACTUAL:
// write nothing
break;
case Song::DISPLAY_SPECIFIED:
2003-06-16 17:28:58 +00:00
if( out.m_fSpecifiedBPMMin == out.m_fSpecifiedBPMMax )
2003-12-04 22:58:18 +00:00
f.PutLine( ssprintf( "#DISPLAYBPM:%.3f;", out.m_fSpecifiedBPMMin ) );
else
2003-12-04 22:58:18 +00:00
f.PutLine( ssprintf( "#DISPLAYBPM:%.3f:%.3f;", out.m_fSpecifiedBPMMin, out.m_fSpecifiedBPMMax ) );
break;
case Song::DISPLAY_RANDOM:
2003-12-04 22:58:18 +00:00
f.PutLine( ssprintf( "#DISPLAYBPM:*;" ) );
break;
}
2002-12-13 23:56:13 +00:00
2003-12-04 22:58:18 +00:00
f.Write( "#BPMS:" );
2004-09-21 07:53:39 +00:00
for( unsigned i=0; i<out.m_Timing.m_BPMSegments.size(); i++ )
2002-12-13 23:56:13 +00:00
{
2003-12-18 04:48:26 +00:00
const BPMSegment &bs = out.m_Timing.m_BPMSegments[i];
2002-12-13 23:56:13 +00:00
f.Write( ssprintf( "%.3f=%.3f", NoteRowToBeat(bs.m_iStartIndex), bs.GetBPM() ) );
2003-12-18 04:48:26 +00:00
if( i != out.m_Timing.m_BPMSegments.size()-1 )
2003-12-04 22:58:18 +00:00
f.Write( "," );
2002-12-13 23:56:13 +00:00
}
2003-12-04 22:58:18 +00:00
f.PutLine( ";" );
2002-12-13 23:56:13 +00:00
2003-12-04 22:58:18 +00:00
f.Write( "#STOPS:" );
2004-09-21 07:53:39 +00:00
for( unsigned i=0; i<out.m_Timing.m_StopSegments.size(); i++ )
2002-12-13 23:56:13 +00:00
{
2003-12-18 04:48:26 +00:00
const StopSegment &fs = out.m_Timing.m_StopSegments[i];
2002-12-13 23:56:13 +00:00
f.PutLine( ssprintf( "%.3f=%.3f", NoteRowToBeat(fs.m_iStartRow), fs.m_fStopSeconds ) );
2003-12-18 04:48:26 +00:00
if( i != out.m_Timing.m_StopSegments.size()-1 )
2003-12-04 22:58:18 +00:00
f.Write( "," );
2002-12-13 23:56:13 +00:00
}
2003-12-04 22:58:18 +00:00
f.PutLine( ";" );
2005-05-26 09:35:57 +00:00
2005-06-04 21:22:50 +00:00
FOREACH_BackgroundLayer( b )
2002-12-13 23:56:13 +00:00
{
2005-05-26 09:35:57 +00:00
if( b==0 )
f.Write( "#BGCHANGES:" );
2005-06-04 21:22:50 +00:00
else if( out.GetBackgroundChanges(b).empty() )
2005-05-26 09:35:57 +00:00
continue; // skip
else
2005-06-05 03:14:56 +00:00
f.Write( ssprintf("#BGCHANGES%d:", b+1) );
2005-05-26 09:35:57 +00:00
2005-06-04 21:22:50 +00:00
FOREACH_CONST( BackgroundChange, out.GetBackgroundChanges(b), bgc )
2005-06-05 22:21:55 +00:00
f.PutLine( BackgroundChangeToString(*bgc)+"," );
2002-12-13 23:56:13 +00:00
2005-05-26 09:35:57 +00:00
/* If there's an animation plan at all, add a dummy "-nosongbg-" tag to indicate that
* this file doesn't want a song BG entry added at the end. See SMLoader::TidyUpData.
* This tag will be removed on load. Add it at a very high beat, so it won't cause
* problems if loaded in older versions. */
2005-06-04 21:22:50 +00:00
if( b==0 && !out.GetBackgroundChanges(b).empty() )
2005-05-26 09:35:57 +00:00
f.PutLine( "99999=-nosongbg-=1.000=0=0=0 // don't automatically add -songbackground-" );
f.PutLine( ";" );
2002-12-13 23:56:13 +00:00
}
2004-01-07 00:13:32 +00:00
2005-06-04 21:22:50 +00:00
if( out.GetForegroundChanges().size() )
2004-01-07 00:13:32 +00:00
{
2004-01-07 01:06:58 +00:00
f.Write( "#FGCHANGES:" );
2005-06-04 21:22:50 +00:00
FOREACH_CONST( BackgroundChange, out.GetForegroundChanges(), bgc )
2004-01-07 01:06:58 +00:00
{
2005-06-05 22:21:55 +00:00
f.PutLine( BackgroundChangeToString(*bgc)+"," );
2004-01-07 01:06:58 +00:00
}
f.PutLine( ";" );
2004-01-07 00:13:32 +00:00
}
f.Write( "#KEYSOUNDS:" );
for( unsigned i=0; i<out.m_vsKeysoundFile.size(); i++ )
{
f.Write( out.m_vsKeysoundFile[i] );
if( i != out.m_vsKeysoundFile.size()-1 )
f.Write( "," );
}
f.PutLine( ";" );
2003-12-04 22:58:18 +00:00
}
2002-12-13 23:56:13 +00:00
2006-01-22 01:00:06 +00:00
static RString JoinLineList( vector<RString> &lines )
2003-12-04 22:58:18 +00:00
{
for( unsigned i = 0; i < lines.size(); ++i )
TrimRight( lines[i] );
2005-02-19 07:48:16 +00:00
/* Skip leading blanks. */
2005-02-20 04:49:36 +00:00
unsigned j = 0;
while( j < lines.size() && lines.size() == 0 )
++j;
2005-02-19 07:48:16 +00:00
2005-02-28 09:01:13 +00:00
return join( "\r\n", lines.begin()+j, lines.end() );
2002-12-13 23:56:13 +00:00
}
2006-01-22 01:00:06 +00:00
RString NotesWriterSM::GetSMNotesTag( const Song &song, const Steps &in, bool bSavingCache )
2002-12-14 21:36:52 +00:00
{
2006-01-22 01:00:06 +00:00
vector<RString> lines;
2005-02-28 09:01:13 +00:00
lines.push_back( "" );
lines.push_back( ssprintf("//---------------%s - %s----------------",
GameManager::StepsTypeToString(in.m_StepsType).c_str(), in.GetDescription().c_str()) );
lines.push_back( song.m_vsKeysoundFile.empty() ? "#NOTES:" : "#NOTES2:" );
lines.push_back( ssprintf( " %s:", GameManager::StepsTypeToString(in.m_StepsType).c_str() ) );
lines.push_back( ssprintf( " %s:", in.GetDescription().c_str() ) );
lines.push_back( ssprintf( " %s:", DifficultyToString(in.GetDifficulty()).c_str() ) );
lines.push_back( ssprintf( " %d:", in.GetMeter() ) );
2002-12-14 21:36:52 +00:00
2006-01-07 04:11:29 +00:00
int MaxRadar = bSavingCache? NUM_RadarCategory:5;
2006-01-22 01:00:06 +00:00
vector<RString> asRadarValues;
2003-11-26 17:41:32 +00:00
for( int r=0; r < MaxRadar; r++ )
2003-01-02 22:10:51 +00:00
asRadarValues.push_back( ssprintf("%.3f", in.GetRadarValues()[r]) );
2003-09-15 06:22:36 +00:00
/* Don't append a newline here; it's added in NoteDataUtil::GetSMNoteDataString.
* If we add it here, then every time we write unmodified data we'll add an extra
* newline and they'll accumulate. */
2005-02-28 09:01:13 +00:00
lines.push_back( ssprintf( " %s:", join(",",asRadarValues).c_str() ) );
2002-12-14 21:36:52 +00:00
2006-01-22 01:00:06 +00:00
RString sNoteData;
2004-10-23 23:41:49 +00:00
in.GetSMNoteData( sNoteData );
2003-11-17 03:38:24 +00:00
2005-02-28 09:01:13 +00:00
split( sNoteData, "\n", lines, true );
lines.push_back( ";" );
2003-12-04 22:58:18 +00:00
2005-02-28 09:01:13 +00:00
return JoinLineList( lines );
2002-12-14 21:36:52 +00:00
}
2006-01-22 01:00:06 +00:00
bool NotesWriterSM::Write(RString sPath, const Song &out, bool bSavingCache)
2002-12-13 23:56:13 +00:00
{
/* Flush dir cache when writing steps, so the old size isn't cached. */
FILEMAN->FlushDirCache( Dirname(sPath) );
int flags = RageFile::WRITE;
/* If we're not saving cache, we're saving real data, so enable SLOW_FLUSH
* to prevent data loss. If we're saving cache, this will slow things down
* too much. */
if( !bSavingCache )
flags |= RageFile::SLOW_FLUSH;
2003-12-04 22:58:18 +00:00
RageFile f;
if( !f.Open( sPath, flags ) )
{
2003-12-04 22:58:18 +00:00
LOG->Warn( "Error opening song file '%s' for writing: %s", sPath.c_str(), f.GetError().c_str() );
return false;
}
2002-12-13 23:56:13 +00:00
2003-12-04 22:58:18 +00:00
WriteGlobalTags( f, out );
if( bSavingCache )
{
2004-02-20 05:28:34 +00:00
f.PutLine( ssprintf( "// cache tags:" ) );
f.PutLine( ssprintf( "#FIRSTBEAT:%.3f;", out.m_fFirstBeat ) );
f.PutLine( ssprintf( "#LASTBEAT:%.3f;", out.m_fLastBeat ) );
f.PutLine( ssprintf( "#SONGFILENAME:%s;", out.m_sSongFileName.c_str() ) );
f.PutLine( ssprintf( "#HASMUSIC:%i;", out.m_bHasMusic ) );
f.PutLine( ssprintf( "#HASBANNER:%i;", out.m_bHasBanner ) );
2004-02-02 10:06:26 +00:00
f.PutLine( ssprintf( "#MUSICLENGTH:%.3f;", out.m_fMusicLengthSeconds ) );
2004-02-20 05:28:34 +00:00
f.PutLine( ssprintf( "// end cache tags" ) );
2002-12-13 23:56:13 +00:00
}
//
2003-08-03 00:13:55 +00:00
// Save all Steps for this file
2002-12-13 23:56:13 +00:00
//
2004-06-04 23:36:50 +00:00
const vector<Steps*>& vpSteps = out.GetAllSteps();
2005-12-03 20:34:49 +00:00
FOREACH_CONST( Steps*, vpSteps, s )
2002-12-13 23:56:13 +00:00
{
2005-12-03 20:34:49 +00:00
const Steps* pSteps = *s;
2004-05-24 03:41:39 +00:00
if( pSteps->IsAutogen() )
2003-01-02 22:10:51 +00:00
continue; /* don't write autogen notes */
2002-12-14 21:36:52 +00:00
2004-02-25 23:48:45 +00:00
/* Only save steps that weren't loaded from a profile. */
2004-05-24 03:41:39 +00:00
if( pSteps->WasLoadedFromProfile() )
2004-02-25 23:48:45 +00:00
continue;
2006-01-22 01:00:06 +00:00
RString sTag = GetSMNotesTag( out, *pSteps, bSavingCache );
2005-02-19 07:48:16 +00:00
f.PutLine( sTag );
2002-12-13 23:56:13 +00:00
}
return true;
}
2004-05-31 21:55:14 +00:00
2006-01-22 01:00:06 +00:00
void NotesWriterSM::GetEditFileContents( const Song *pSong, const Steps *pSteps, RString &sOut )
2005-02-19 07:48:16 +00:00
{
sOut = "";
2006-01-22 01:00:06 +00:00
RString sDir = pSong->GetSongDir();
2005-02-19 07:48:16 +00:00
/* "Songs/foo/bar"; strip off "Songs/". */
2006-01-22 01:00:06 +00:00
vector<RString> asParts;
2005-02-19 07:48:16 +00:00
split( sDir, "/", asParts );
if( asParts.size() )
sDir = join( "/", asParts.begin()+1, asParts.end() );
sOut += ssprintf( "#SONG:%s;\n", sDir.c_str() );
sOut += GetSMNotesTag( *pSong, *pSteps, false );
}
2006-01-22 01:00:06 +00:00
RString NotesWriterSM::GetEditFileName( const Song *pSong, const Steps *pSteps )
2005-12-03 20:34:49 +00:00
{
/* Try to make a unique name. This isn't guaranteed. Edit descriptions are
* case-sensitive, filenames on disk are usually not, and we decimate certain
* characters for FAT filesystems. */
2006-01-22 01:00:06 +00:00
RString sFile = pSong->GetTranslitFullTitle() + " - " + pSteps->GetDescription();
// HACK:
if( pSteps->m_StepsType == STEPS_TYPE_DANCE_DOUBLE )
sFile += " (doubles)";
sFile += ".edit";
MakeValidFilename( sFile );
return sFile;
2005-12-03 20:34:49 +00:00
}
static LocalizedString DESTINATION_ALREADY_EXISTS ("NotesWriterSM", "Error renaming file. Destination file '%s' already exists.");
static LocalizedString ERROR_WRITING_FILE ("NotesWriterSM", "Error writing file '%s'.");
bool NotesWriterSM::WriteEditFileToMachine( const Song *pSong, Steps *pSteps, RString &sErrorOut )
2005-12-03 20:34:49 +00:00
{
2006-01-22 01:00:06 +00:00
RString sDir = PROFILEMAN->GetProfileDir( ProfileSlot_Machine ) + EDIT_STEPS_SUBDIR;
2005-12-03 20:34:49 +00:00
2006-01-22 01:00:06 +00:00
RString sPath = sDir + GetEditFileName(pSong,pSteps);
2005-12-03 20:34:49 +00:00
/* Flush dir cache when writing steps, so the old size isn't cached. */
FILEMAN->FlushDirCache( Dirname(sPath) );
// Check to make sure that we're not clobering an existing file before opening.
bool bFileNameChanging =
pSteps->GetSavedToDisk() &&
pSteps->GetFilename() != sPath;
if( bFileNameChanging && DoesFileExist(sPath) )
{
sErrorOut = ssprintf( DESTINATION_ALREADY_EXISTS.GetValue(), sPath.c_str() );
return false;
}
2005-12-06 12:41:33 +00:00
2005-12-03 20:34:49 +00:00
RageFile f;
2005-12-29 02:24:12 +00:00
if( !f.Open(sPath, RageFile::WRITE | RageFile::SLOW_FLUSH) )
2005-12-03 20:34:49 +00:00
{
sErrorOut = ssprintf( ERROR_WRITING_FILE.GetValue(), sPath.c_str() );
2005-12-03 20:34:49 +00:00
return false;
}
2006-01-22 01:00:06 +00:00
RString sTag;
2005-12-03 20:34:49 +00:00
GetEditFileContents( pSong, pSteps, sTag );
2005-12-06 17:45:35 +00:00
if( f.PutLine(sTag) == -1 || f.Flush() == -1 )
{
sErrorOut = ssprintf( ERROR_WRITING_FILE.GetValue(), sPath.c_str() );
2005-12-06 17:45:35 +00:00
return false;
}
2005-12-03 20:34:49 +00:00
/* If the file name of the edit has changed since the last save, then delete the old
* file after saving the new one. If we delete it first, then we'll lose data on error. */
if( bFileNameChanging )
FILEMAN->Remove( pSteps->GetFilename() );
pSteps->SetFilename( sPath );
/* Flush dir cache or else the new file won't be seen. */
FILEMAN->FlushDirCache( Dirname(sPath) );
2005-12-03 20:34:49 +00:00
return true;
}
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.
*/