From 9e8469eb65cd1a94c1c9a78d79af30dd6855dca8 Mon Sep 17 00:00:00 2001 From: Chris Danford Date: Sat, 3 Dec 2005 20:34:49 +0000 Subject: [PATCH] save edits to machine profile --- stepmania/src/CourseWriterCRS.cpp | 2 +- stepmania/src/CourseWriterCRS.h | 3 +- stepmania/src/NotesLoaderSM.h | 1 + stepmania/src/NotesWriterSM.cpp | 51 +++++++++++++++++++++++++++++-- stepmania/src/NotesWriterSM.h | 4 ++- stepmania/src/ScreenEdit.cpp | 16 +++++++++- stepmania/src/Song.cpp | 2 +- stepmania/src/Steps.cpp | 5 --- stepmania/src/Steps.h | 7 +++-- 9 files changed, 75 insertions(+), 16 deletions(-) diff --git a/stepmania/src/CourseWriterCRS.cpp b/stepmania/src/CourseWriterCRS.cpp index c35a08f695..152d9610e3 100644 --- a/stepmania/src/CourseWriterCRS.cpp +++ b/stepmania/src/CourseWriterCRS.cpp @@ -20,7 +20,7 @@ bool CourseWriterCRS::Write( const Course &course, const CString &sPath, bool bS return CourseWriterCRS::Write( course, f, bSavingCache ); } -void CourseWriterCRS::GetEditFile( const Course *pCourse, CString &sOut ) +void CourseWriterCRS::GetEditFileContents( const Course *pCourse, CString &sOut ) { RageFileObjMem mem; CourseWriterCRS::Write( *pCourse, mem, true ); diff --git a/stepmania/src/CourseWriterCRS.h b/stepmania/src/CourseWriterCRS.h index d38e6702c9..e740bb01cc 100644 --- a/stepmania/src/CourseWriterCRS.h +++ b/stepmania/src/CourseWriterCRS.h @@ -11,7 +11,8 @@ class CourseWriterCRS public: static bool Write( const Course &course, RageFileBasic &f, bool bSavingCache ); static bool Write( const Course &course, const CString &sPath, bool bSavingCache ); - static void GetEditFile( const Course *pCourse, CString &sOut ); + static void GetEditFileContents( const Course *pCourse, CString &sOut ); + static void WriteEditFileToMachine( const Course *pCourse ); }; #endif diff --git a/stepmania/src/NotesLoaderSM.h b/stepmania/src/NotesLoaderSM.h index f07f6da199..95716aeded 100644 --- a/stepmania/src/NotesLoaderSM.h +++ b/stepmania/src/NotesLoaderSM.h @@ -40,6 +40,7 @@ public: static void LoadTimingFromSMFile( const MsdFile &msd, TimingData &out ); static bool LoadEdit( CString sEditFilePath, ProfileSlot slot ); static bool LoadEditFromBuffer( const CString &sBuffer, CString sEditFilePath, ProfileSlot slot ); +private: static bool LoadEditFromMsd( const MsdFile &msd, CString sEditFilePath, ProfileSlot slot ); }; diff --git a/stepmania/src/NotesWriterSM.cpp b/stepmania/src/NotesWriterSM.cpp index 14d18fe07c..d8c07de3b2 100644 --- a/stepmania/src/NotesWriterSM.cpp +++ b/stepmania/src/NotesWriterSM.cpp @@ -12,6 +12,8 @@ #include #include "Foreach.h" #include "BackgroundUtil.h" +#include "ProfileManager.h" +#include "Profile.h" static CString BackgroundChangeToString( const BackgroundChange &bgc ) { @@ -222,9 +224,9 @@ bool NotesWriterSM::Write(CString sPath, const Song &out, bool bSavingCache) // Save all Steps for this file // const vector& vpSteps = out.GetAllSteps(); - for( unsigned i=0; iIsAutogen() ) continue; /* don't write autogen notes */ @@ -239,7 +241,7 @@ bool NotesWriterSM::Write(CString sPath, const Song &out, bool bSavingCache) return true; } -void NotesWriterSM::GetEditFile( const Song *pSong, const Steps *pSteps, CString &sOut ) +void NotesWriterSM::GetEditFileContents( const Song *pSong, const Steps *pSteps, CString &sOut ) { sOut = ""; CString sDir = pSong->GetSongDir(); @@ -253,6 +255,49 @@ void NotesWriterSM::GetEditFile( const Song *pSong, const Steps *pSteps, CString sOut += GetSMNotesTag( *pSong, *pSteps, false ); } +CString NotesWriterSM::GetEditFileName( const Song *pSong, const Steps *pSteps ) +{ + // guaranteed to be a unique name + return pSong->GetTranslitFullTitle() + " - " + pSteps->GetDescription(); +} + +bool NotesWriterSM::WriteEditFileToMachine( const Song *pSong, Steps *pSteps ) +{ + ASSERT( pSteps->WasLoadedFromProfile() ); + + CString sDir = PROFILEMAN->GetProfileDir( ProfileSlot_Machine ) + EDIT_STEPS_SUBDIR; + + /* If the file name of the edit has changed since the last save, then delete the old + * file before saving the new one. + */ + bool bFileNameChanged = + pSteps->GetSavedToDisk() && + pSteps->GetFilename() != GetEditFileName(pSong,pSteps); + if( bFileNameChanged ) + FILEMAN->Remove( pSteps->GetFilename() ); + + CString sPath = sDir + GetEditFileName(pSong,pSteps); + pSteps->SetFilename( sPath ); + + /* Flush dir cache when writing steps, so the old size isn't cached. */ + FILEMAN->FlushDirCache( Dirname(sPath) ); + + int flags = RageFile::WRITE | RageFile::SLOW_FLUSH; + + RageFile f; + if( !f.Open( sPath, flags ) ) + { + LOG->Warn( "Error opening song file '%s' for writing: %s", sPath.c_str(), f.GetError().c_str() ); + return false; + } + + CString sTag; + GetEditFileContents( pSong, pSteps, sTag ); + f.PutLine( sTag ); + + return true; +} + /* * (c) 2001-2004 Chris Danford, Glenn Maynard * All rights reserved. diff --git a/stepmania/src/NotesWriterSM.h b/stepmania/src/NotesWriterSM.h index 6a591abbc7..7a3aa37d31 100644 --- a/stepmania/src/NotesWriterSM.h +++ b/stepmania/src/NotesWriterSM.h @@ -13,7 +13,9 @@ class NotesWriterSM public: bool Write( CString sPath, const Song &out, bool bSavingCache ); - static void GetEditFile( const Song *pSong, const Steps *pSteps, CString &sOut ); + static void GetEditFileContents( const Song *pSong, const Steps *pSteps, CString &sOut ); + CString GetEditFileName( const Song *pSong, const Steps *pSteps ); + bool WriteEditFileToMachine( const Song *pSong, Steps *pSteps ); }; #endif diff --git a/stepmania/src/ScreenEdit.cpp b/stepmania/src/ScreenEdit.cpp index 304b13c969..73de32f637 100644 --- a/stepmania/src/ScreenEdit.cpp +++ b/stepmania/src/ScreenEdit.cpp @@ -32,6 +32,7 @@ #include #include #include "InputEventPlus.h" +#include "NotesWriterSM.h" static Preference g_iDefaultRecordLength( "DefaultRecordLength", 4 ); @@ -2546,6 +2547,18 @@ void ScreenEdit::HandleMainMenuChoice( MainMenuChoice c, const vector &iAns { case EDIT_MODE_HOME: { + ASSERT( pSteps->IsAnEdit() ); + + pSteps->SetSavedToDisk( true ); + CopyToLastSave(); + + NotesWriterSM w; + w.WriteEditFileToMachine( pSong, pSteps ); + + SCREENMAN->ZeroNextUpdate(); + + /* FIXME + CString s; switch( c ) { @@ -2554,6 +2567,7 @@ void ScreenEdit::HandleMainMenuChoice( MainMenuChoice c, const vector &iAns default: ASSERT(0); } SCREENMAN->AddNewScreenToTop( s ); + */ } break; case EDIT_MODE_FULL: @@ -2561,7 +2575,7 @@ void ScreenEdit::HandleMainMenuChoice( MainMenuChoice c, const vector &iAns pSteps->SetSavedToDisk( true ); CopyToLastSave(); - GAMESTATE->m_pCurSong->Save(); + pSong->Save(); SCREENMAN->ZeroNextUpdate(); // we shouldn't say we're saving a DWI if we're on any game besides diff --git a/stepmania/src/Song.cpp b/stepmania/src/Song.cpp index 4d8dd0a4ba..c4e42bcb7c 100644 --- a/stepmania/src/Song.cpp +++ b/stepmania/src/Song.cpp @@ -292,7 +292,7 @@ bool Song::LoadFromSongDir( CString sDir ) FOREACH( Steps*, m_vpSteps, s ) { - (*s)->SetFile( GetCacheFilePath() ); + (*s)->SetFilename( GetCacheFilePath() ); /* Compress all Steps. During initial caching, this will remove cached NoteData; * during cached loads, this will just remove cached SMData. */ diff --git a/stepmania/src/Steps.cpp b/stepmania/src/Steps.cpp index ef45e974bb..eb1c1fa194 100644 --- a/stepmania/src/Steps.cpp +++ b/stepmania/src/Steps.cpp @@ -321,11 +321,6 @@ bool Steps::IsAutogen() const return parent != NULL; } -void Steps::SetFile( CString fn ) -{ - m_sFilename = fn; -} - void Steps::SetDifficultyAndDescription( Difficulty dc, CString sDescription ) { DeAutogen(); diff --git a/stepmania/src/Steps.h b/stepmania/src/Steps.h index 56b7b0e638..1a587ee496 100644 --- a/stepmania/src/Steps.h +++ b/stepmania/src/Steps.h @@ -41,9 +41,10 @@ public: int GetMeter() const { return Real()->m_iMeter; } const RadarValues& GetRadarValues() const { return Real()->m_CachedRadarValues; } - void SetFile( CString fn ); + void SetFilename( CString fn ) { m_sFilename = fn; } + CString GetFilename() const { return m_sFilename; } void SetSavedToDisk( bool b ) { m_bSavedToDisk = b; } - bool GetSavedToDisk() { return m_bSavedToDisk; } + bool GetSavedToDisk() const { return m_bSavedToDisk; } void SetDifficultyAndDescription( Difficulty dc, CString sDescription ); void SetDifficulty( Difficulty dc ) { SetDifficultyAndDescription( dc, this->GetDescription() ); } void SetDescription( CString sDescription ) { SetDifficultyAndDescription( this->GetDifficulty(), sDescription ); } @@ -83,7 +84,7 @@ protected: const Steps *Real() const; CString m_sFilename; - bool m_bSavedToDisk; // true if this was loaded from disk or has been saved to disk. + bool m_bSavedToDisk; // true if this was loaded from disk or has been saved to disk. /* These values are pulled from the autogen source first, if there is one. */ ProfileSlot m_LoadedFromProfile; // ProfileSlot_INVALID if wasn't loaded from a profile