Files
itgmania212121/stepmania/src/SongManager.cpp
T

1522 lines
41 KiB
C++
Raw Normal View History

2003-02-16 04:01:45 +00:00
#include "global.h"
2002-02-28 19:40:40 +00:00
/*
-----------------------------------------------------------------------------
Class: SongManager
Desc: See header.
2002-05-19 01:59:48 +00:00
Copyright (c) 2001-2002 by the person(s) listed below. All rights reserved.
2002-02-28 19:40:40 +00:00
Chris Danford
-----------------------------------------------------------------------------
*/
#include "SongManager.h"
#include "IniFile.h"
2002-05-01 19:14:55 +00:00
#include "RageLog.h"
#include "MsdFile.h"
#include "NotesLoaderDWI.h"
2003-06-15 01:53:51 +00:00
#include "BannerCache.h"
2003-07-22 07:47:27 +00:00
#include "arch/arch.h"
2002-06-14 22:25:22 +00:00
2002-07-23 01:41:40 +00:00
#include "GameState.h"
2002-05-29 09:47:24 +00:00
#include "PrefsManager.h"
2002-07-27 19:29:51 +00:00
#include "RageException.h"
#include "arch/LoadingWindow/LoadingWindow.h"
2003-07-17 20:11:24 +00:00
#include "Course.h"
2002-02-28 19:40:40 +00:00
#include "AnnouncerManager.h"
#include "ThemeManager.h"
#include "GameManager.h"
2003-07-22 07:47:27 +00:00
#include "RageFile.h"
#include "ProductInfo.h"
2002-05-19 01:59:48 +00:00
SongManager* SONGMAN = NULL; // global and accessable from anywhere in our program
2002-02-28 19:40:40 +00:00
2003-09-06 21:48:35 +00:00
#define SM_300_STATISTICS_FILE BASE_PATH "statistics.ini"
#define SONGS_DIR BASE_PATH "Songs" SLASH
#define COURSES_DIR BASE_PATH "Courses" SLASH
#define STATS_PATH BASE_PATH "stats.html"
const CString CATEGORY_RANKING_FILE = BASE_PATH "Data" SLASH "CategoryRanking.dat";
const CString MACHINE_STEPS_MEM_CARD_DATA = BASE_PATH "Data" SLASH "MachineStepsMemCardData.dat";
const CString MACHINE_COURSE_MEM_CARD_DATA = BASE_PATH "Data" SLASH "MachineCourseMemCardData.dat";
2003-10-20 01:34:01 +00:00
const int CATEGORY_RANKING_VERSION = 3;
const int STEPS_MEM_CARD_DATA_VERSION = 7;
const int COURSE_MEM_CARD_DATA_VERSION = 5;
2003-01-22 05:29:27 +00:00
2002-02-28 19:40:40 +00:00
2002-09-03 06:33:08 +00:00
#define NUM_GROUP_COLORS THEME->GetMetricI("SongManager","NumGroupColors")
#define GROUP_COLOR( i ) THEME->GetMetricC("SongManager",ssprintf("GroupColor%d",i+1))
#define BEGINNER_COLOR THEME->GetMetricC("SongManager","BeginnerColor")
#define EASY_COLOR THEME->GetMetricC("SongManager","EasyColor")
#define MEDIUM_COLOR THEME->GetMetricC("SongManager","MediumColor")
#define HARD_COLOR THEME->GetMetricC("SongManager","HardColor")
#define CHALLENGE_COLOR THEME->GetMetricC("SongManager","ChallengeColor")
2002-08-28 22:42:40 +00:00
#define EXTRA_COLOR THEME->GetMetricC("SongManager","ExtraColor")
vector<RageColor> g_vGroupColors;
2003-07-31 22:48:49 +00:00
RageTimer g_LastMetricUpdate; /* can't use RageTimer globally */
2002-04-16 17:31:00 +00:00
2003-07-31 22:48:49 +00:00
static void UpdateMetrics()
{
if( g_LastMetricUpdate.PeekDeltaTime() < 1 )
return;
g_LastMetricUpdate.Touch();
g_vGroupColors.clear();
for( int i=0; i<NUM_GROUP_COLORS; i++ )
g_vGroupColors.push_back( GROUP_COLOR(i) );
}
2002-02-28 19:40:40 +00:00
2002-11-17 09:13:35 +00:00
SongManager::SongManager( LoadingWindow *ld )
2002-02-28 19:40:40 +00:00
{
2003-02-05 19:16:00 +00:00
/* We initialize things that assume they can get at SONGMAN; we only
* init one of these, so hook us up to it immediately. */
SONGMAN = this;
try
{
InitSongsFromDisk( ld );
InitCoursesFromDisk( ld );
InitAutogenCourses();
InitMachineScoresFromDisk();
} catch(...) {
SONGMAN = NULL;
throw;
}
2003-07-31 22:48:49 +00:00
g_LastMetricUpdate.SetZero();
UpdateMetrics();
2002-02-28 19:40:40 +00:00
}
SongManager::~SongManager()
{
2003-01-22 05:29:27 +00:00
SaveMachineScoresToDisk();
2003-07-22 07:47:27 +00:00
WriteStatsWebPage();
FreeSongs();
}
2003-10-15 01:03:14 +00:00
void SongManager::CategoryData::AddHighScore( HighScore hs, int &iIndexOut )
{
int i;
for( i=0; i<(int)vHighScores.size(); i++ )
{
2003-10-20 01:06:26 +00:00
if( hs >= vHighScores[i] )
2003-10-15 01:03:14 +00:00
break;
}
if( i < NUM_RANKING_LINES )
{
vHighScores.insert( vHighScores.begin()+i, hs );
iIndexOut = i;
2003-10-21 20:31:47 +00:00
if( int(vHighScores.size()) > NUM_RANKING_LINES )
2003-10-15 01:03:14 +00:00
vHighScores.erase( vHighScores.begin()+NUM_RANKING_LINES, vHighScores.end() );
}
}
2003-08-06 08:06:27 +00:00
void SongManager::Reload()
{
FlushDirCache();
2003-08-18 17:31:25 +00:00
SaveMachineScoresToDisk();
2003-08-06 08:06:27 +00:00
FreeSongs();
FreeCourses();
2003-08-06 08:06:27 +00:00
m_sGroupNames.clear();
m_sGroupBannerPaths.clear();
InitSongsFromDisk(NULL);
InitCoursesFromDisk(NULL);
InitAutogenCourses();
2003-08-18 17:31:25 +00:00
InitMachineScoresFromDisk();
2002-02-28 19:40:40 +00:00
}
2003-08-06 08:06:27 +00:00
2003-07-22 07:47:27 +00:00
void SongManager::SaveMachineScoresToDisk()
{
SaveCategoryRankingsToFile( CATEGORY_RANKING_FILE );
SaveStepsMemCardDataToFile( MACHINE_STEPS_MEM_CARD_DATA, MEMORY_CARD_MACHINE );
SaveCourseMemCardDataToFile( MACHINE_COURSE_MEM_CARD_DATA, MEMORY_CARD_MACHINE );
2003-07-22 07:47:27 +00:00
}
2002-02-28 19:40:40 +00:00
void SongManager::InitSongsFromDisk( LoadingWindow *ld )
2002-02-28 19:40:40 +00:00
{
2003-09-25 02:27:17 +00:00
RageTimer tm;
2003-07-22 07:47:27 +00:00
LoadStepManiaSongDir( SONGS_DIR, ld );
2002-06-27 17:49:10 +00:00
for( unsigned i=0; i<PREFSMAN->m_asAdditionalSongFolders.size(); i++ )
2002-11-17 09:13:35 +00:00
LoadStepManiaSongDir( PREFSMAN->m_asAdditionalSongFolders[i], ld );
2002-04-16 17:31:00 +00:00
2002-09-10 08:38:46 +00:00
if( PREFSMAN->m_DWIPath != "" )
LoadStepManiaSongDir( PREFSMAN->m_DWIPath + "/Songs", ld );
2002-09-10 08:38:46 +00:00
2003-10-21 20:57:48 +00:00
LOG->Trace( "Found %d songs in %f seconds.", (int)m_pSongs.size(), tm.GetDeltaTime() );
2002-03-06 08:25:09 +00:00
}
void SongManager::SanityCheckGroupDir( CString sDir ) const
{
// Check to see if they put a song directly inside the group folder.
CStringArray arrayFiles;
GetDirListing( sDir + "/*.mp3", arrayFiles );
GetDirListing( sDir + "/*.ogg", arrayFiles );
GetDirListing( sDir + "/*.wav", arrayFiles );
if( !arrayFiles.empty() )
2002-12-21 19:34:02 +00:00
RageException::Throw(
"The folder '%s' contains music files.\n\n"
"This means that you have a music outside of a song folder.\n"
"All song folders must reside in a group folder. For example, 'Songs/DDR 4th Mix/B4U'.\n"
"See the StepMania readme for more info.",
2003-04-25 00:01:35 +00:00
sDir.c_str()
);
}
2002-09-10 07:11:29 +00:00
void SongManager::AddGroup( CString sDir, CString sGroupDirName )
{
unsigned j;
2003-08-06 08:06:27 +00:00
for(j = 0; j < m_sGroupNames.size(); ++j)
if( sGroupDirName == m_sGroupNames[j] ) break;
2002-09-10 07:11:29 +00:00
2003-08-06 08:06:27 +00:00
if( j != m_sGroupNames.size() )
2002-09-10 07:11:29 +00:00
return; /* the group is already added */
// Look for a group banner in this group folder
CStringArray arrayGroupBanners;
2003-01-23 04:43:22 +00:00
GetDirListing( sDir+sGroupDirName+"/*.png", arrayGroupBanners );
GetDirListing( sDir+sGroupDirName+"/*.jpg", arrayGroupBanners );
GetDirListing( sDir+sGroupDirName+"/*.gif", arrayGroupBanners );
GetDirListing( sDir+sGroupDirName+"/*.bmp", arrayGroupBanners );
2002-09-10 07:11:29 +00:00
2003-06-17 21:34:43 +00:00
CString sBannerPath;
if( !arrayGroupBanners.empty() )
2003-07-22 07:47:27 +00:00
sBannerPath = sDir+sGroupDirName+SLASH+arrayGroupBanners[0] ;
2003-06-17 21:34:43 +00:00
else
{
// Look for a group banner in the parent folder
GetDirListing( sDir+sGroupDirName+".png", arrayGroupBanners );
GetDirListing( sDir+sGroupDirName+".jpg", arrayGroupBanners );
GetDirListing( sDir+sGroupDirName+".gif", arrayGroupBanners );
GetDirListing( sDir+sGroupDirName+".bmp", arrayGroupBanners );
if( !arrayGroupBanners.empty() )
sBannerPath = sDir+arrayGroupBanners[0];
2002-09-10 07:11:29 +00:00
}
2003-06-17 21:34:43 +00:00
if( sBannerPath != "" )
LOG->Trace( "Group banner for '%s' is '%s'.", sGroupDirName.c_str(), sBannerPath.c_str() );
2003-08-06 08:06:27 +00:00
m_sGroupNames.push_back( sGroupDirName );
m_sGroupBannerPaths.push_back(sBannerPath);
2002-09-10 07:11:29 +00:00
}
2002-11-17 09:13:35 +00:00
void SongManager::LoadStepManiaSongDir( CString sDir, LoadingWindow *ld )
2002-03-06 08:25:09 +00:00
{
2003-01-23 04:43:22 +00:00
/* Make sure sDir has a trailing slash. */
2002-10-31 08:05:13 +00:00
TrimRight( sDir, "/\\" );
2003-07-22 07:47:27 +00:00
sDir += SLASH;
2002-03-06 08:25:09 +00:00
// Find all group directories in "Songs" folder
CStringArray arrayGroupDirs;
2003-01-23 04:43:22 +00:00
GetDirListing( sDir+"*", arrayGroupDirs, true );
2002-03-06 08:25:09 +00:00
SortCStringArray( arrayGroupDirs );
for( unsigned i=0; i< arrayGroupDirs.size(); i++ ) // for each dir in /Songs/
2002-03-06 08:25:09 +00:00
{
CString sGroupDirName = arrayGroupDirs[i];
if( 0 == stricmp( sGroupDirName, "cvs" ) ) // the directory called "CVS"
continue; // ignore it
2003-01-23 04:43:22 +00:00
SanityCheckGroupDir(sDir+sGroupDirName);
2002-03-06 08:25:09 +00:00
// Find all Song folders in this group directory
CStringArray arraySongDirs;
2003-07-22 07:47:27 +00:00
GetDirListing( sDir+sGroupDirName + SLASH "*", arraySongDirs, true, true );
2002-03-06 08:25:09 +00:00
SortCStringArray( arraySongDirs );
unsigned j;
2002-09-09 02:23:47 +00:00
int loaded = 0;
for( j=0; j< arraySongDirs.size(); j++ ) // for each song dir
2002-03-06 08:25:09 +00:00
{
CString sSongDirName = arraySongDirs[j];
2003-01-23 04:43:22 +00:00
if( 0 == stricmp( Basename(sSongDirName), "cvs" ) ) // the directory called "CVS"
2002-03-06 08:25:09 +00:00
continue; // ignore it
// this is a song directory. Load a new song!
2002-11-17 09:13:35 +00:00
if( ld ) {
2003-01-23 04:43:22 +00:00
ld->SetText( ssprintf("Loading songs...\n%s\n%s",
2003-04-25 00:01:35 +00:00
Basename(sGroupDirName).c_str(),
Basename(sSongDirName).c_str()));
2002-11-17 09:13:35 +00:00
ld->Paint();
}
2002-03-06 08:25:09 +00:00
Song* pNewSong = new Song;
2003-01-23 04:43:22 +00:00
if( !pNewSong->LoadFromSongDir( sSongDirName ) ) {
/* The song failed to load. */
delete pNewSong;
continue;
}
2002-10-31 04:23:39 +00:00
m_pSongs.push_back( pNewSong );
2002-09-09 02:23:47 +00:00
loaded++;
}
/* Don't add the group name if we didn't load any songs in this group. */
if(!loaded) continue;
/* Add this group to the group array. */
2002-09-10 07:11:29 +00:00
AddGroup(sDir, sGroupDirName);
2003-06-15 01:53:51 +00:00
/* Cache and load the group banner. */
BANNERCACHE->CacheBanner( GetGroupBannerPath(sGroupDirName) );
/* Load the group sym links (if any)*/
LoadGroupSymLinks(sDir, sGroupDirName);
}
}
void SongManager::LoadGroupSymLinks(CString sDir, CString sGroupFolder)
{
// Find all symlink files in this folder
CStringArray arraySymLinks;
GetDirListing( sDir+sGroupFolder+SLASH+"*.include", arraySymLinks, false );
SortCStringArray( arraySymLinks );
for( unsigned s=0; s< arraySymLinks.size(); s++ ) // for each symlink in this dir, add it in as a song.
{
MsdFile msdF;
msdF.ReadFile( sDir+sGroupFolder+SLASH+arraySymLinks[s].c_str() );
CString sSymDestination = msdF.GetParam(0,1); // Should only be 1 vale&param...period.
Song* pNewSong = new Song;
if( !pNewSong->LoadFromSongDir( sSymDestination ) )
delete pNewSong; // The song failed to load.
else
{
pNewSong->m_apNotes.clear(); // No memory hogs..
pNewSong->m_BackgroundChanges.clear();
pNewSong->m_bIsSymLink = true; // Very important so we don't double-parse later
pNewSong->m_sGroupName = sGroupFolder;
m_pSongs.push_back( pNewSong );
}
2002-03-06 08:25:09 +00:00
}
}
void SongManager::FreeSongs()
2002-02-28 19:40:40 +00:00
{
for( unsigned i=0; i<m_pSongs.size(); i++ )
2002-02-28 19:40:40 +00:00
SAFE_DELETE( m_pSongs[i] );
2002-10-24 20:15:24 +00:00
m_pSongs.clear();
2002-06-14 22:25:22 +00:00
2003-08-06 08:06:27 +00:00
m_sGroupBannerPaths.clear();
2002-02-28 19:40:40 +00:00
}
2003-09-25 23:27:25 +00:00
2003-10-19 23:30:09 +00:00
//
// Helper function for reading/writing scores
//
bool FileRead( ifstream& f, CString& sOut )
{
if( f.eof() )
return false;
getline(f, sOut);
return true;
}
bool FileRead( ifstream& f, int& iOut )
{
CString s;
if( !FileRead( f, s ) )
return false;
iOut = atoi( s );
return true;
}
bool FileRead( ifstream& f, unsigned& uOut )
{
CString s;
if( !FileRead( f, s ) )
return false;
uOut = atoi( s );
return true;
}
bool FileRead( ifstream& f, float& fOut )
{
CString s;
if( !FileRead( f, s ) )
return false;
2003-10-20 06:28:19 +00:00
fOut = (float) atof( s );
2003-10-19 23:30:09 +00:00
return true;
}
void FileWrite( ofstream& f, const CString& sWrite )
{
2003-10-23 21:00:47 +00:00
f << sWrite << "\n";
2003-10-19 23:30:09 +00:00
}
void FileWrite( ofstream& f, int iWrite )
{
2003-10-23 21:00:47 +00:00
f << iWrite << "\n";
2003-10-19 23:30:09 +00:00
}
2003-10-21 20:35:32 +00:00
void FileWrite( ofstream& f, size_t uWrite )
2003-10-19 23:30:09 +00:00
{
2003-10-23 21:00:47 +00:00
f << uWrite << "\n";
2003-10-19 23:30:09 +00:00
}
void FileWrite( ofstream& f, float fWrite )
{
2003-10-23 21:00:47 +00:00
f << fWrite << "\n";
2003-09-25 23:27:25 +00:00
}
2002-02-28 19:40:40 +00:00
2003-10-19 23:30:09 +00:00
#define WARN_AND_RETURN { LOG->Warn("Error parsing file '%s' at %s:%d",fn.c_str(),__FILE__,__LINE__); return; }
2002-02-28 19:40:40 +00:00
2003-10-19 23:30:09 +00:00
void SongManager::ReadStepsMemCardDataFromFile( CString fn, int mc )
2002-02-28 19:40:40 +00:00
{
ifstream f(fn);
if( !f.good() )
2003-10-19 23:30:09 +00:00
WARN_AND_RETURN;
2002-02-28 19:40:40 +00:00
int version;
2003-10-19 23:30:09 +00:00
if( !FileRead(f, version) )
WARN_AND_RETURN;
if( version != STEPS_MEM_CARD_DATA_VERSION )
2003-10-19 23:30:09 +00:00
WARN_AND_RETURN;
int iNumSongs;
if( !FileRead(f, iNumSongs) )
WARN_AND_RETURN;
2003-10-19 23:30:09 +00:00
for( int s=0; s<iNumSongs; s++ )
2003-01-22 05:29:27 +00:00
{
CString sSongDir;
2003-10-19 23:30:09 +00:00
if( !FileRead(f, sSongDir) )
WARN_AND_RETURN;
Song* pSong = this->GetSongFromDir( sSongDir );
2003-10-19 23:30:09 +00:00
int iNumNotes;
if( !FileRead(f, iNumNotes) )
WARN_AND_RETURN;
2003-10-20 06:28:19 +00:00
for( int n=0; n<iNumNotes; n++ )
{
2003-08-07 06:16:17 +00:00
StepsType nt;
2003-10-19 23:30:09 +00:00
if( !FileRead(f, (int&)nt) )
WARN_AND_RETURN;
2003-10-19 23:30:09 +00:00
Difficulty dc;
if( !FileRead(f, (int&)dc) )
WARN_AND_RETURN;
CString sDescription;
2003-10-19 23:30:09 +00:00
if( !FileRead(f, sDescription) )
WARN_AND_RETURN;
// Even if pSong or pNotes is null, we still have to skip over that data.
2003-08-03 00:13:55 +00:00
Steps* pNotes = NULL;
if( pSong )
{
if( dc==DIFFICULTY_INVALID )
pNotes = pSong->GetStepsByDescription( nt, sDescription );
else
pNotes = pSong->GetStepsByDifficulty( nt, dc );
}
int iNumTimesPlayed;
2003-10-19 23:30:09 +00:00
if( !FileRead(f, iNumTimesPlayed) )
WARN_AND_RETURN;
if( pNotes )
2003-10-19 23:30:09 +00:00
pNotes->m_MemCardDatas[mc].iNumTimesPlayed = iNumTimesPlayed;
if( pNotes )
2003-10-19 23:30:09 +00:00
pNotes->m_MemCardDatas[mc].vHighScores.resize(NUM_RANKING_LINES);
2003-09-25 23:27:25 +00:00
2003-10-19 23:30:09 +00:00
for( int l=0; l<NUM_RANKING_LINES; l++ )
{
2003-10-20 01:34:01 +00:00
CString sName;
if( !FileRead(f, sName) )
WARN_AND_RETURN;
Grade grade;
2003-10-19 23:30:09 +00:00
if( !FileRead(f, (int&)grade) )
WARN_AND_RETURN;
CLAMP( grade, (Grade)0, (Grade)(NUM_GRADES-1) );
2003-10-20 06:28:19 +00:00
int iScore;
2003-10-20 01:06:26 +00:00
if( !FileRead(f, iScore) )
2003-10-19 23:30:09 +00:00
WARN_AND_RETURN;
2003-10-20 01:06:26 +00:00
float fPercentDP;
if( !FileRead(f, fPercentDP) )
WARN_AND_RETURN;
if( pNotes )
{
2003-10-19 23:30:09 +00:00
pNotes->m_MemCardDatas[mc].vHighScores[l].sName = sName;
2003-10-20 01:06:26 +00:00
pNotes->m_MemCardDatas[mc].vHighScores[l].grade = grade;
pNotes->m_MemCardDatas[mc].vHighScores[l].iScore = iScore;
pNotes->m_MemCardDatas[mc].vHighScores[l].fPercentDP = fPercentDP;
}
}
}
}
}
void SongManager::ReadCategoryRankingsFromFile( CString fn )
{
2003-10-19 23:30:09 +00:00
ifstream f(fn);
if( !f.good() )
WARN_AND_RETURN;
int version;
2003-10-19 23:30:09 +00:00
if( !FileRead(f, version) )
WARN_AND_RETURN;
if( version != CATEGORY_RANKING_VERSION )
WARN_AND_RETURN;
for( int st=0; st<NUM_STEPS_TYPES; st++ )
{
for( int rc=0; rc<NUM_RANKING_CATEGORIES; rc++ )
{
2003-10-19 23:30:09 +00:00
m_CategoryDatas[st][rc].vHighScores.resize(NUM_RANKING_LINES);
for( int l=0; l<NUM_RANKING_LINES; l++ )
{
2003-10-19 23:30:09 +00:00
CString sName;
if( !FileRead(f, sName) )
WARN_AND_RETURN;
2003-10-20 01:34:01 +00:00
int iScore;
if( !FileRead(f, iScore) )
WARN_AND_RETURN;
2003-10-19 23:30:09 +00:00
m_CategoryDatas[st][rc].vHighScores[l].sName = sName;
2003-10-20 01:34:01 +00:00
m_CategoryDatas[st][rc].vHighScores[l].iScore = iScore;
}
}
}
}
2003-10-19 23:30:09 +00:00
void SongManager::ReadCourseMemCardDataFromFile( CString fn, int mc )
{
2003-10-19 23:30:09 +00:00
ifstream f(fn);
if( !f.good() )
WARN_AND_RETURN;
int version;
2003-10-19 23:30:09 +00:00
if( !FileRead(f, version) )
WARN_AND_RETURN;
if( version != COURSE_MEM_CARD_DATA_VERSION )
WARN_AND_RETURN;
2003-09-25 23:27:25 +00:00
2003-10-19 23:30:09 +00:00
int iNumCourses;
if( !FileRead(f, iNumCourses) )
WARN_AND_RETURN;
for( int c=0; c<iNumCourses; c++ )
{
CString sPath;
if( !FileRead(f, sPath) )
WARN_AND_RETURN;
Course* pCourse = GetCourseFromPath( sPath );
if( pCourse == NULL )
pCourse = GetCourseFromName( sPath );
2003-10-19 23:30:09 +00:00
// even if we don't find the Course*, we still have to read past the input
int NumStepsPlayed = 0;
if( !FileRead(f, NumStepsPlayed) )
WARN_AND_RETURN;
while( NumStepsPlayed-- )
2003-10-19 23:30:09 +00:00
{
int st;
if( !FileRead(f, st) )
WARN_AND_RETURN;
2003-10-19 23:30:09 +00:00
int iNumTimesPlayed;
if( !FileRead(f, iNumTimesPlayed) )
WARN_AND_RETURN;
if( pCourse )
pCourse->m_MemCardDatas[st][mc].iNumTimesPlayed = iNumTimesPlayed;
if( pCourse )
pCourse->m_MemCardDatas[st][mc].vHighScores.resize(NUM_RANKING_LINES);
for( int l=0; l<NUM_RANKING_LINES; l++ )
{
2003-10-20 01:34:01 +00:00
CString sName;
if( !FileRead(f, sName) )
WARN_AND_RETURN;
2003-10-19 23:30:09 +00:00
int iScore;
if( !FileRead(f, iScore) )
WARN_AND_RETURN;
float fSurviveTime;
if( !FileRead(f, fSurviveTime) )
WARN_AND_RETURN;
if( pCourse && st < NUM_STEPS_TYPES )
{
2003-10-20 01:34:01 +00:00
pCourse->m_MemCardDatas[st][mc].vHighScores[l].sName = sName;
2003-10-19 23:30:09 +00:00
pCourse->m_MemCardDatas[st][mc].vHighScores[l].iScore = iScore;
pCourse->m_MemCardDatas[st][mc].vHighScores[l].fSurviveTime = fSurviveTime;
}
}
}
}
}
void SongManager::InitMachineScoresFromDisk()
{
2003-09-06 21:48:35 +00:00
// read old style notes scores
ReadSM300NoteScores();
// category ranking
ReadCategoryRankingsFromFile( CATEGORY_RANKING_FILE );
ReadCourseMemCardDataFromFile( MACHINE_COURSE_MEM_CARD_DATA, MEMORY_CARD_MACHINE );
ReadStepsMemCardDataFromFile( MACHINE_STEPS_MEM_CARD_DATA, MEMORY_CARD_MACHINE );
2002-02-28 19:40:40 +00:00
}
2003-09-06 21:48:35 +00:00
void SongManager::ReadSM300NoteScores()
{
2003-09-06 21:48:35 +00:00
IniFile ini;
ini.SetPath( SM_300_STATISTICS_FILE );
if( !ini.ReadFile() ) {
LOG->Trace( "WARNING: Could not read SM 3.0 final statistics '%s'.", SM_300_STATISTICS_FILE );
return; // load nothing
}
// load song statistics
const IniFile::key* pKey = ini.GetKey( "Statistics" );
if( pKey )
{
for( IniFile::key::const_iterator iter = pKey->begin();
iter != pKey->end();
iter++ )
{
CString name = iter->first;
CString value = iter->second;
// Each value has the format "SongName::StepsType::StepsDescription=TimesPlayed::TopGrade::TopScore::MaxCombo".
char szSongDir[256];
char szStepsType[256];
char szStepsDescription[256];
int iRetVal;
// Parse for Song name and Notes name
iRetVal = sscanf( name, "%[^:]::%[^:]::%[^:]", szSongDir, szStepsType, szStepsDescription );
if( iRetVal != 3 )
continue; // this line doesn't match what is expected
CString sSongDir = FixSlashes( szSongDir );
// Search for the corresponding Song pointer.
Song* pSong = GetSongFromDir( sSongDir );
if( pSong == NULL ) // didn't find a match
continue; // skip this entry
StepsType st = GAMEMAN->StringToNotesType( szStepsType );
Difficulty dc = StringToDifficulty( szStepsDescription );
// Search for the corresponding Notes pointer.
Steps* pNotes = pSong->GetStepsByDifficulty( st, dc );
if( pNotes == NULL ) // didn't find a match
continue; // skip this entry
// Parse the Notes statistics.
char szGradeLetters[10]; // longest possible string is "AAA"
int iMaxCombo; // throw away
pNotes->m_MemCardDatas[MEMORY_CARD_MACHINE].vHighScores.resize(1);
2003-09-06 21:48:35 +00:00
iRetVal = sscanf(
value,
2003-10-20 01:06:26 +00:00
"%d::%[^:]::%d::%d",
&pNotes->m_MemCardDatas[MEMORY_CARD_MACHINE].iNumTimesPlayed,
2003-09-06 21:48:35 +00:00
szGradeLetters,
2003-10-20 01:06:26 +00:00
&pNotes->m_MemCardDatas[MEMORY_CARD_MACHINE].vHighScores[0].iScore,
2003-09-06 21:48:35 +00:00
&iMaxCombo
);
if( iRetVal != 4 )
continue;
pNotes->m_MemCardDatas[MEMORY_CARD_MACHINE].vHighScores[0].grade = StringToGrade( szGradeLetters );
2003-09-06 21:48:35 +00:00
}
}
}
2003-07-22 07:47:27 +00:00
void SongManager::SaveCategoryRankingsToFile( CString fn )
2002-02-28 19:40:40 +00:00
{
LOG->Trace("SongManager::SaveCategoryRankingsToFile");
2003-07-22 07:47:27 +00:00
2003-10-19 23:30:09 +00:00
ofstream f(fn);
if( !f.good() )
return;
FileWrite( f, CATEGORY_RANKING_VERSION );
for( int st=0; st<NUM_STEPS_TYPES; st++ )
2002-02-28 19:40:40 +00:00
{
2003-10-19 23:30:09 +00:00
for( int rc=0; rc<NUM_RANKING_CATEGORIES; rc++ )
{
2003-10-19 23:30:09 +00:00
m_CategoryDatas[st][rc].vHighScores.resize(NUM_RANKING_LINES);
for( int l=0; l<NUM_RANKING_LINES; l++ )
{
// tricky: wipe out "name to fill in" markers
if( IsRankingToFillIn(m_CategoryDatas[st][rc].vHighScores[l].sName) )
m_CategoryDatas[st][rc].vHighScores[l].sName = "";
2003-10-19 23:30:09 +00:00
FileWrite( f, m_CategoryDatas[st][rc].vHighScores[l].sName );
2003-10-20 01:34:01 +00:00
FileWrite( f, m_CategoryDatas[st][rc].vHighScores[l].iScore );
}
}
2003-01-22 05:29:27 +00:00
}
2003-07-22 07:47:27 +00:00
}
2002-02-28 19:40:40 +00:00
2003-10-19 23:30:09 +00:00
void SongManager::SaveCourseMemCardDataToFile( CString fn, int mc )
2003-07-22 07:47:27 +00:00
{
LOG->Trace("SongManager::SaveCourseMemCardDataToFile");
2002-02-28 19:40:40 +00:00
2003-10-19 23:30:09 +00:00
ofstream f(fn);
if( !f.good() )
return;
FileWrite( f, COURSE_MEM_CARD_DATA_VERSION );
2003-10-19 23:30:09 +00:00
FileWrite( f, m_pCourses.size() );
for( unsigned c=0; c<m_pCourses.size(); c++ ) // foreach course
{
2003-10-19 23:30:09 +00:00
Course* pCourse = m_pCourses[c];
ASSERT(pCourse);
2003-10-19 23:30:09 +00:00
if( pCourse->m_bIsAutogen )
FileWrite( f, pCourse->m_sName );
else
FileWrite( f, pCourse->m_sPath );
int NumStepsPlayed = 0;
int st;
for( st=0; st<NUM_STEPS_TYPES; st++ )
if( pCourse->m_MemCardDatas[st][mc].iNumTimesPlayed )
++NumStepsPlayed;
FileWrite( f, NumStepsPlayed );
for( st=0; st<NUM_STEPS_TYPES; st++ )
2003-10-19 23:30:09 +00:00
{
if( !pCourse->m_MemCardDatas[st][mc].iNumTimesPlayed )
continue;
--NumStepsPlayed;
FileWrite( f, st );
2003-10-19 23:30:09 +00:00
FileWrite( f, pCourse->m_MemCardDatas[st][mc].iNumTimesPlayed );
pCourse->m_MemCardDatas[st][mc].vHighScores.resize(NUM_RANKING_LINES);
for( int l=0; l<NUM_RANKING_LINES; l++ )
{
// tricky: wipe out "name to fill in" markers
if( IsRankingToFillIn(pCourse->m_MemCardDatas[st][mc].vHighScores[l].sName) )
pCourse->m_MemCardDatas[st][mc].vHighScores[l].sName = "";
2003-10-20 01:34:01 +00:00
FileWrite( f, pCourse->m_MemCardDatas[st][mc].vHighScores[l].sName );
2003-10-19 23:30:09 +00:00
FileWrite( f, pCourse->m_MemCardDatas[st][mc].vHighScores[l].iScore );
FileWrite( f, pCourse->m_MemCardDatas[st][mc].vHighScores[l].fSurviveTime );
2003-01-22 05:29:27 +00:00
}
2002-02-28 19:40:40 +00:00
}
ASSERT( !NumStepsPlayed );
}
2003-07-22 07:47:27 +00:00
}
2003-10-19 23:30:09 +00:00
void SongManager::SaveStepsMemCardDataToFile( CString fn, int mc )
2003-07-22 07:47:27 +00:00
{
LOG->Trace("SongManager::SaveStepsMemCardDataToFile %s", fn.c_str());
2003-10-19 23:30:09 +00:00
ofstream f(fn);
if( !f.good() )
return;
2003-10-19 23:30:09 +00:00
FileWrite( f, STEPS_MEM_CARD_DATA_VERSION );
2003-10-19 23:30:09 +00:00
FileWrite( f, m_pSongs.size() );
2003-10-19 23:30:09 +00:00
for( unsigned s=0; s<m_pSongs.size(); s++ ) // foreach song
{
Song* pSong = m_pSongs[s];
ASSERT(pSong);
2003-10-31 23:17:14 +00:00
/* If the song has never been played, don't write anything. This keeps
* us from saving a dozen copies of each song for all autogen difficulties,
* since most people only use a couple game modes. */
vector<Steps*> vNotes;
for( unsigned i=0; i<pSong->m_apNotes.size(); ++i )
{
Steps* pNotes = pSong->m_apNotes[i];
if( !pNotes->m_MemCardDatas[mc].iNumTimesPlayed )
continue;
vNotes.push_back( pNotes );
}
2003-10-19 23:30:09 +00:00
FileWrite( f, pSong->GetSongDir() );
FileWrite( f, vNotes.size() );
2003-10-31 23:17:14 +00:00
if( vNotes.size() == 0 )
continue; // skip
2003-10-19 23:30:09 +00:00
for( unsigned n=0; n<vNotes.size(); n++ )
{
Steps* pNotes = vNotes[n];
ASSERT(pNotes);
2003-10-19 23:30:09 +00:00
FileWrite( f, pNotes->m_StepsType );
FileWrite( f, pNotes->GetDifficulty() );
FileWrite( f, pNotes->GetDescription() );
FileWrite( f, pNotes->m_MemCardDatas[mc].iNumTimesPlayed );
2003-07-22 07:47:27 +00:00
2003-10-19 23:30:09 +00:00
pNotes->m_MemCardDatas[mc].vHighScores.resize(NUM_RANKING_LINES);
for( int l=0; l<NUM_RANKING_LINES; l++ )
{
// tricky: wipe out "name to fill in" markers
if( IsRankingToFillIn(pNotes->m_MemCardDatas[mc].vHighScores[l].sName) )
pNotes->m_MemCardDatas[mc].vHighScores[l].sName = "";
2003-10-19 23:30:09 +00:00
FileWrite( f, pNotes->m_MemCardDatas[mc].vHighScores[l].sName );
2003-10-20 01:06:26 +00:00
FileWrite( f, pNotes->m_MemCardDatas[mc].vHighScores[l].grade );
FileWrite( f, pNotes->m_MemCardDatas[mc].vHighScores[l].iScore );
FileWrite( f, pNotes->m_MemCardDatas[mc].vHighScores[l].fPercentDP );
2003-10-19 23:30:09 +00:00
}
}
2003-01-22 05:29:27 +00:00
}
2002-03-06 08:25:09 +00:00
}
CString SongManager::GetGroupBannerPath( CString sGroupName )
2002-03-06 08:25:09 +00:00
{
unsigned i;
2003-08-06 08:06:27 +00:00
for(i = 0; i < m_sGroupNames.size(); ++i)
if( sGroupName == m_sGroupNames[i] ) break;
2002-03-06 08:25:09 +00:00
2003-08-06 08:06:27 +00:00
if( i == m_sGroupNames.size() )
2002-03-06 08:25:09 +00:00
return "";
2002-09-07 07:24:44 +00:00
2003-08-06 08:06:27 +00:00
return m_sGroupBannerPaths[i];
2002-03-06 08:25:09 +00:00
}
2002-04-01 02:04:43 +00:00
void SongManager::GetGroupNames( CStringArray &AddTo )
{
2003-08-06 08:06:27 +00:00
AddTo.insert(AddTo.end(), m_sGroupNames.begin(), m_sGroupNames.end() );
2002-04-16 17:31:00 +00:00
}
2002-04-01 02:04:43 +00:00
bool SongManager::DoesGroupExist( CString sGroupName )
{
2003-09-21 21:35:59 +00:00
for( unsigned i = 0; i < m_sGroupNames.size(); ++i )
if( !m_sGroupNames[i].CompareNoCase(sGroupName) )
return true;
return false;
}
RageColor SongManager::GetGroupColor( const CString &sGroupName )
2002-04-16 17:31:00 +00:00
{
2003-07-31 22:48:49 +00:00
UpdateMetrics();
2002-04-16 17:31:00 +00:00
// search for the group index
unsigned i;
2003-08-06 08:06:27 +00:00
for( i=0; i<m_sGroupNames.size(); i++ )
2002-04-01 02:04:43 +00:00
{
2003-08-06 08:06:27 +00:00
if( m_sGroupNames[i] == sGroupName )
2002-04-16 17:31:00 +00:00
break;
2002-04-01 02:04:43 +00:00
}
2003-08-06 08:06:27 +00:00
ASSERT( i != m_sGroupNames.size() ); // this is not a valid group
2002-04-16 17:31:00 +00:00
return g_vGroupColors[i%g_vGroupColors.size()];
2002-04-01 02:04:43 +00:00
}
2002-12-17 05:22:32 +00:00
RageColor SongManager::GetSongColor( const Song* pSong )
{
ASSERT( pSong );
/* XXX:
* Previously, this matched all notes, which set a song to "extra" if it
* had any 10-foot steps at all, even edits or doubles.
*
* For now, only look at notes for the current note type. This means that
* if a song has 10-foot steps on Doubles, it'll only show up red in Doubles.
* That's not too bad, I think. This will also change it in the song scroll,
* which is a little odd but harmless.
*
2003-04-20 22:57:35 +00:00
* XXX: Ack. This means this function can only be called when we have a style
* set up, which is too restrictive. How to handle this?
*
* XXX: Once we support edits, ignore them, too. */
2003-08-07 06:36:34 +00:00
// const StepsType nt = GAMESTATE->GetCurrentStyleDef()->m_StepsType;
for( unsigned i=0; i<pSong->m_apNotes.size(); i++ )
{
2003-08-03 00:13:55 +00:00
const Steps* pNotes = pSong->m_apNotes[i];
2003-07-17 20:11:24 +00:00
if( pNotes->GetDifficulty() == DIFFICULTY_CHALLENGE )
continue;
2003-08-07 06:36:34 +00:00
// if(pNotes->m_StepsType != nt)
2003-04-20 22:57:35 +00:00
// continue;
if( pNotes->GetMeter() >= 10 && PREFSMAN->m_bTenFooterInRed )
return EXTRA_COLOR;
}
return GetGroupColor( pSong->m_sGroupName );
}
RageColor SongManager::GetDifficultyColor( Difficulty dc )
{
switch( dc )
{
case DIFFICULTY_BEGINNER: return BEGINNER_COLOR;
case DIFFICULTY_EASY: return EASY_COLOR;
case DIFFICULTY_MEDIUM: return MEDIUM_COLOR;
case DIFFICULTY_HARD: return HARD_COLOR;
case DIFFICULTY_CHALLENGE: return CHALLENGE_COLOR;
default: ASSERT(0); return CHALLENGE_COLOR;
}
}
void SongManager::GetSongs( vector<Song*> &AddTo, CString sGroupName, int iMaxStages ) const
2002-04-01 02:04:43 +00:00
{
AddTo.clear();
for( unsigned i=0; i<m_pSongs.size(); i++ )
2003-05-19 21:50:56 +00:00
if( sGroupName==GROUP_ALL_MUSIC || sGroupName==m_pSongs[i]->m_sGroupName )
if( GetNumStagesForSong(m_pSongs[i])<=iMaxStages )
AddTo.push_back( m_pSongs[i] );
2002-04-01 02:04:43 +00:00
}
2002-04-16 17:31:00 +00:00
2003-02-05 18:34:27 +00:00
int SongManager::GetNumSongs() const
{
return m_pSongs.size();
}
2003-02-05 18:34:27 +00:00
int SongManager::GetNumGroups() const
{
2003-08-06 08:06:27 +00:00
return m_sGroupNames.size();
}
int SongManager::GetNumCourses() const
{
return m_pCourses.size();
}
CString SongManager::ShortenGroupName( CString sLongGroupName )
2002-04-16 17:31:00 +00:00
{
sLongGroupName.Replace( "Dance Dance Revolution", "DDR" );
sLongGroupName.Replace( "dance dance revolution", "DDR" );
sLongGroupName.Replace( "DANCE DANCE REVOLUTION", "DDR" );
sLongGroupName.Replace( "Pump It Up", "PIU" );
sLongGroupName.Replace( "pump it up", "PIU" );
sLongGroupName.Replace( "PUMP IT UP", "PIU" );
sLongGroupName.Replace( "ParaParaParadise", "PPP" );
sLongGroupName.Replace( "paraparaparadise", "PPP" );
sLongGroupName.Replace( "PARAPARAPARADISE", "PPP" );
sLongGroupName.Replace( "Para Para Paradise", "PPP" );
sLongGroupName.Replace( "para para paradise", "PPP" );
sLongGroupName.Replace( "PARA PARA PARADISE", "PPP" );
sLongGroupName.Replace( "Dancing Stage", "DS" );
sLongGroupName.Replace( "Dancing Stage", "DS" );
sLongGroupName.Replace( "Dancing Stage", "DS" );
2003-05-22 01:34:03 +00:00
sLongGroupName.Replace( "Ez2dancer", "EZ2" );
sLongGroupName.Replace( "Ez 2 Dancer", "EZ2");
sLongGroupName.Replace( "Technomotion", "TM");
sLongGroupName.Replace( "Dance Station 3DDX", "3DDX");
sLongGroupName.Replace( "DS3DDX", "3DDX");
sLongGroupName.Replace( "BeatMania", "BM");
sLongGroupName.Replace( "Beatmania", "BM");
sLongGroupName.Replace( "BEATMANIA", "BM");
sLongGroupName.Replace( "beatmania", "BM");
return sLongGroupName;
}
int SongManager::GetNumStagesForSong( const Song* pSong )
{
ASSERT( pSong );
if( pSong->m_fMusicLengthSeconds > PREFSMAN->m_fMarathonVerSongSeconds )
return 3;
if( pSong->m_fMusicLengthSeconds > PREFSMAN->m_fLongVerSongSeconds )
return 2;
else
return 1;
2002-04-16 17:31:00 +00:00
}
2002-06-14 22:25:22 +00:00
void SongManager::InitCoursesFromDisk( LoadingWindow *ld )
2002-06-14 22:25:22 +00:00
{
2003-04-11 00:29:44 +00:00
unsigned i;
LOG->Trace( "Loading courses." );
2002-07-02 17:34:20 +00:00
//
2003-04-11 00:29:44 +00:00
// Load courses from in Courses dir
2002-07-02 17:34:20 +00:00
//
CStringArray saCourseFiles;
2003-07-22 07:47:27 +00:00
GetDirListing( COURSES_DIR "*.crs", saCourseFiles, false, true );
2003-04-11 00:29:44 +00:00
for( i=0; i<saCourseFiles.size(); i++ )
2002-07-02 17:34:20 +00:00
{
2003-01-22 05:29:27 +00:00
Course* pCourse = new Course;
2003-02-05 19:16:00 +00:00
pCourse->LoadFromCRSFile( saCourseFiles[i] );
m_pCourses.push_back( pCourse );
if( ld )
{
ld->SetText( ssprintf("Loading courses...\n%s\n%s",
"Courses",
Basename(saCourseFiles[i]).c_str()));
ld->Paint();
}
2002-07-02 17:34:20 +00:00
}
2003-04-11 00:29:44 +00:00
// Find all group directories in Courses dir
CStringArray arrayGroupDirs;
2003-07-22 07:47:27 +00:00
GetDirListing( COURSES_DIR "*", arrayGroupDirs, true );
2003-04-11 00:29:44 +00:00
SortCStringArray( arrayGroupDirs );
for( i=0; i< arrayGroupDirs.size(); i++ ) // for each dir in /Courses/
{
CString sGroupDirName = arrayGroupDirs[i];
if( 0 == stricmp( sGroupDirName, "cvs" ) ) // the directory called "CVS"
continue; // ignore it
// Find all CRS files in this group directory
CStringArray arrayCoursePaths;
2003-07-22 07:47:27 +00:00
GetDirListing( COURSES_DIR + sGroupDirName + "/*.crs", arrayCoursePaths, false, true );
2003-04-11 00:29:44 +00:00
SortCStringArray( arrayCoursePaths );
for( unsigned j=0; j<arrayCoursePaths.size(); j++ )
{
if( ld )
{
ld->SetText( ssprintf("Loading courses...\n%s\n%s",
Basename(arrayGroupDirs[i]).c_str(),
Basename(arrayCoursePaths[j]).c_str()));
ld->Paint();
}
2003-04-11 00:29:44 +00:00
Course* pCourse = new Course;
pCourse->LoadFromCRSFile( arrayCoursePaths[j] );
m_pCourses.push_back( pCourse );
}
}
}
void SongManager::InitAutogenCourses()
{
2002-07-02 17:34:20 +00:00
//
// Create group courses for Endless and Nonstop
2002-07-02 17:34:20 +00:00
//
2002-06-14 22:25:22 +00:00
CStringArray saGroupNames;
this->GetGroupNames( saGroupNames );
unsigned g;
for( g=0; g<saGroupNames.size(); g++ ) // foreach Group
2002-06-14 22:25:22 +00:00
{
CString sGroupName = saGroupNames[g];
2003-01-03 05:56:28 +00:00
vector<Song*> apGroupSongs;
GetSongs( apGroupSongs, sGroupName );
2002-06-14 22:25:22 +00:00
Course* pCourse;
2002-07-04 21:05:18 +00:00
pCourse = new Course;
pCourse->AutogenEndlessFromGroup( sGroupName, apGroupSongs );
m_pCourses.push_back( pCourse );
pCourse = new Course;
pCourse->AutogenNonstopFromGroup( sGroupName, apGroupSongs );
m_pCourses.push_back( pCourse );
2002-06-14 22:25:22 +00:00
}
}
2003-01-22 05:29:27 +00:00
void SongManager::FreeCourses()
2002-06-14 22:25:22 +00:00
{
2003-01-22 05:29:27 +00:00
for( unsigned i=0; i<m_pCourses.size(); i++ )
delete m_pCourses[i];
m_pCourses.clear();
2002-06-14 22:25:22 +00:00
}
2002-08-01 13:42:56 +00:00
2002-12-21 18:23:37 +00:00
/* Called periodically to wipe out cached NoteData. This is called when we change
* screens. */
void SongManager::CompressSongs()
2002-12-21 18:23:37 +00:00
{
for( unsigned i=0; i<m_pSongs.size(); i++ )
{
2003-05-05 04:13:39 +00:00
Song* pSong = m_pSongs[i];
for( unsigned n=0; n<pSong->m_apNotes.size(); n++ )
{
2003-08-03 00:13:55 +00:00
Steps* pNotes = pSong->m_apNotes[n];
2003-05-05 04:13:39 +00:00
pNotes->Compress();
2002-12-21 18:23:37 +00:00
}
}
}
2002-12-21 18:23:37 +00:00
2003-07-20 00:30:24 +00:00
void SongManager::GetAllCourses( vector<Course*> &AddTo, bool bIncludeAutogen )
{
for( unsigned i=0; i<m_pCourses.size(); i++ )
if( bIncludeAutogen || !m_pCourses[i]->m_bIsAutogen )
AddTo.push_back( m_pCourses[i] );
}
void SongManager::GetNonstopCourses( vector<Course*> &AddTo, bool bIncludeAutogen )
{
2003-01-22 05:29:27 +00:00
for( unsigned i=0; i<m_pCourses.size(); i++ )
2003-06-30 08:06:47 +00:00
if( m_pCourses[i]->IsNonstop() )
if( bIncludeAutogen || !m_pCourses[i]->m_bIsAutogen )
AddTo.push_back( m_pCourses[i] );
2002-12-21 18:23:37 +00:00
}
void SongManager::GetOniCourses( vector<Course*> &AddTo, bool bIncludeAutogen )
{
2003-01-22 05:29:27 +00:00
for( unsigned i=0; i<m_pCourses.size(); i++ )
2003-06-30 08:06:47 +00:00
if( m_pCourses[i]->IsOni() )
if( bIncludeAutogen || !m_pCourses[i]->m_bIsAutogen )
AddTo.push_back( m_pCourses[i] );
}
void SongManager::GetEndlessCourses( vector<Course*> &AddTo, bool bIncludeAutogen )
{
2003-01-22 05:29:27 +00:00
for( unsigned i=0; i<m_pCourses.size(); i++ )
2003-06-30 08:06:47 +00:00
if( m_pCourses[i]->IsEndless() )
if( bIncludeAutogen || !m_pCourses[i]->m_bIsAutogen )
AddTo.push_back( m_pCourses[i] );
}
2002-08-30 20:49:56 +00:00
bool SongManager::GetExtraStageInfoFromCourse( bool bExtra2, CString sPreferredGroup,
2003-08-03 00:13:55 +00:00
Song*& pSongOut, Steps*& pNotesOut, PlayerOptions& po_out, SongOptions& so_out )
2002-08-30 20:49:56 +00:00
{
2003-07-22 07:47:27 +00:00
const CString sCourseSuffix = sPreferredGroup + SLASH + (bExtra2 ? "extra2" : "extra1") + ".crs";
CString sCoursePath = SONGS_DIR + sCourseSuffix;
if( !DoesFileExist(sCoursePath) )
{
/* try alternative song folders */
for( unsigned i=0; i<PREFSMAN->m_asAdditionalSongFolders.size(); i++ )
{
2003-07-22 07:47:27 +00:00
sCoursePath = PREFSMAN->m_asAdditionalSongFolders[i] + SLASH + sCourseSuffix;
if( DoesFileExist(sCoursePath) )
break;
}
2003-02-28 08:42:02 +00:00
}
2003-02-28 08:42:02 +00:00
if( !DoesFileExist(sCoursePath) && PREFSMAN->m_DWIPath.size() )
2003-07-22 07:47:27 +00:00
sCoursePath = PREFSMAN->m_DWIPath + SLASH "Songs" SLASH + sCourseSuffix;
2003-02-28 08:42:02 +00:00
/* Couldn't find course in DWI path or alternative song folders */
if( !DoesFileExist(sCoursePath) )
return false;
2002-08-30 20:49:56 +00:00
Course course;
2003-02-05 19:16:00 +00:00
course.LoadFromCRSFile( sCoursePath );
if( course.GetEstimatedNumStages() <= 0 ) return false;
2002-08-30 20:49:56 +00:00
2003-07-27 18:46:38 +00:00
vector<Course::Info> ci;
2003-08-07 06:36:34 +00:00
course.GetCourseInfo( GAMESTATE->GetCurrentStyleDef()->m_StepsType, ci );
2003-07-27 18:46:38 +00:00
if( ci.empty() )
return false;
2003-04-23 07:28:24 +00:00
po_out.Init();
2003-07-27 18:46:38 +00:00
po_out.FromString( ci[0].Modifiers );
2003-04-23 07:28:24 +00:00
so_out.Init();
2003-07-27 18:46:38 +00:00
so_out.FromString( ci[0].Modifiers );
pSongOut = ci[0].pSong;
pNotesOut = ci[0].pNotes;
2003-04-23 07:28:24 +00:00
return true;
2002-08-30 20:49:56 +00:00
}
2002-08-01 13:42:56 +00:00
/* Return true if n1 < n2. */
2003-08-03 00:13:55 +00:00
bool CompareNotesPointersForExtra(const Steps *n1, const Steps *n2)
{
/* Equate CHALLENGE to HARD. */
Difficulty d1 = min(n1->GetDifficulty(), DIFFICULTY_HARD);
Difficulty d2 = min(n2->GetDifficulty(), DIFFICULTY_HARD);
if(d1 < d2) return true;
if(d1 > d2) return false;
/* n1 difficulty == n2 difficulty */
if(CompareNotesPointersByMeter(n1,n2)) return true;
if(CompareNotesPointersByMeter(n2,n1)) return false;
/* n1 meter == n2 meter */
return CompareNotesPointersByRadarValues(n1,n2);
}
2003-07-30 02:01:19 +00:00
void SongManager::GetExtraStageInfo( bool bExtra2, const StyleDef *sd,
2003-08-03 00:13:55 +00:00
Song*& pSongOut, Steps*& pNotesOut, PlayerOptions& po_out, SongOptions& so_out )
2002-08-01 13:42:56 +00:00
{
2003-07-30 05:34:47 +00:00
CString sGroup = GAMESTATE->m_sPreferredGroup;
if(sGroup == GROUP_ALL_MUSIC)
2003-04-19 22:24:01 +00:00
{
ASSERT(GAMESTATE->m_pCurSong);
2003-07-30 05:34:47 +00:00
sGroup = GAMESTATE->m_pCurSong->m_sGroupName;
2003-04-19 22:24:01 +00:00
}
2003-07-31 01:27:54 +00:00
/* XXX: Temporary extra info: someone reported an odd assertion failure here. */
// ASSERT(sGroup != "");
if( sGroup == "" )
{
LOG->Warn("GetExtraStageInfo error: sGroup == \"\", m_pCurSong %p '%s' '%s'",
GAMESTATE->m_pCurSong,
GAMESTATE->m_pCurSong? GAMESTATE->m_pCurSong->GetSongDir().c_str():"",
GAMESTATE->m_pCurSong? GAMESTATE->m_pCurSong->m_sGroupName.c_str():"");
ASSERT(0); /* get a backtrace */
}
2003-04-19 22:24:01 +00:00
2003-07-30 05:34:47 +00:00
if(GetExtraStageInfoFromCourse(bExtra2, sGroup, pSongOut, pNotesOut, po_out, so_out))
2002-08-01 13:42:56 +00:00
return;
// Choose a hard song for the extra stage
2003-08-03 00:13:55 +00:00
Song* pExtra1Song = NULL; // the absolute hardest Song and Steps. Use this for extra stage 1.
Steps* pExtra1Notes = NULL;
Song* pExtra2Song = NULL; // a medium-hard Song and Steps. Use this for extra stage 2.
Steps* pExtra2Notes = NULL;
2002-08-01 13:42:56 +00:00
2003-01-03 05:56:28 +00:00
vector<Song*> apSongs;
SONGMAN->GetSongs( apSongs, sGroup );
for( unsigned s=0; s<apSongs.size(); s++ ) // foreach song
2002-08-01 13:42:56 +00:00
{
Song* pSong = apSongs[s];
2003-08-03 00:13:55 +00:00
vector<Steps*> apNotes;
pSong->GetSteps( apNotes, sd->m_StepsType );
2003-08-03 00:13:55 +00:00
for( unsigned n=0; n<apNotes.size(); n++ ) // foreach Steps
2002-08-01 13:42:56 +00:00
{
2003-08-03 00:13:55 +00:00
Steps* pNotes = apNotes[n];
2002-08-01 13:42:56 +00:00
if( pExtra1Notes == NULL || CompareNotesPointersForExtra(pExtra1Notes,pNotes) ) // pNotes is harder than pHardestNotes
2002-08-01 13:42:56 +00:00
{
pExtra1Song = pSong;
pExtra1Notes = pNotes;
}
2003-08-03 00:13:55 +00:00
// for extra 2, we don't want to choose the hardest notes possible. So, we'll disgard Steps with meter > 8
2003-01-02 22:10:51 +00:00
if( bExtra2 && pNotes->GetMeter() > 8 )
2002-08-01 13:42:56 +00:00
continue; // skip
if( pExtra2Notes == NULL || CompareNotesPointersForExtra(pExtra2Notes,pNotes) ) // pNotes is harder than pHardestNotes
2002-08-01 13:42:56 +00:00
{
pExtra2Song = pSong;
pExtra2Notes = pNotes;
}
}
}
if( pExtra2Song == NULL && pExtra1Song != NULL )
{
pExtra2Song = pExtra1Song;
pExtra2Notes = pExtra1Notes;
}
2003-08-07 06:16:17 +00:00
// If there are any notes at all that match this StepsType, everything should be filled out.
// Also, it's guaranteed that there is at least one Steps that matches the StepsType because the player
2002-08-01 13:42:56 +00:00
// had to play something before reaching the extra stage!
ASSERT( pExtra2Song && pExtra1Song && pExtra2Notes && pExtra1Notes );
pSongOut = (bExtra2 ? pExtra2Song : pExtra1Song);
pNotesOut = (bExtra2 ? pExtra2Notes : pExtra1Notes);
po_out.Init();
so_out.Init();
2003-08-17 00:15:54 +00:00
po_out.m_fScrolls[PlayerOptions::SCROLL_REVERSE] = 1;
2003-01-25 11:05:12 +00:00
po_out.m_fScrollSpeed = 1.5f;
2002-08-01 13:42:56 +00:00
so_out.m_DrainType = (bExtra2 ? SongOptions::DRAIN_SUDDEN_DEATH : SongOptions::DRAIN_NO_RECOVER);
2003-04-01 19:31:27 +00:00
po_out.m_fDark = 1;
2002-08-01 13:42:56 +00:00
}
Song* SongManager::GetRandomSong()
{
if( m_pSongs.empty() )
return NULL;
return SONGMAN->m_pSongs[ rand()%m_pSongs.size() ];
}
2002-12-02 05:25:44 +00:00
Song* SongManager::GetSongFromDir( CString sDir )
{
2003-09-06 21:48:35 +00:00
if( sDir.Right(1) != SLASH )
sDir += SLASH;
2002-12-02 05:25:44 +00:00
for( unsigned int i=0; i<m_pSongs.size(); i++ )
2002-12-02 05:25:44 +00:00
if( sDir.CompareNoCase(m_pSongs[i]->GetSongDir()) == 0 )
return m_pSongs[i];
return NULL;
}
Course* SongManager::GetCourseFromPath( CString sPath )
{
2003-01-22 05:29:27 +00:00
for( unsigned int i=0; i<m_pCourses.size(); i++ )
if( sPath.CompareNoCase(m_pCourses[i]->m_sPath) == 0 )
return m_pCourses[i];
return NULL;
}
2003-01-22 05:29:27 +00:00
Course* SongManager::GetCourseFromName( CString sName )
{
for( unsigned int i=0; i<m_pCourses.size(); i++ )
if( sName.CompareNoCase(m_pCourses[i]->m_sName) == 0 )
return m_pCourses[i];
return NULL;
}
/*
* GetSongDir() contains a path to the song, possibly a full path, eg:
* Songs\Group\SongName or
* My Other Song Folder\Group\SongName or
* c:\Corny J-pop\Group\SongName
*
* Most course group names are "Group\SongName", so we want to
* match against the last two elements. Let's also support
* "SongName" alone, since the group is only important when it's
* potentially ambiguous.
*
* Let's *not* support "Songs\Group\SongName" in course files.
* That's probably a common error, but that would result in
* course files floating around that only work for people who put
* songs in "Songs"; we don't want that.
*/
Song *SongManager::FindSong( CString sPath )
{
sPath.Replace( "\\", "/" );
CStringArray bits;
split( sPath, "/", bits );
if( bits.size() == 1 )
return FindSong( "", bits[0] );
else if( bits.size() == 2 )
return FindSong( bits[0], bits[1] );
return NULL;
}
2003-07-09 04:09:35 +00:00
Song *SongManager::FindSong( CString sGroup, CString sSong )
{
// foreach song
for( unsigned i = 0; i < m_pSongs.size(); i++ )
{
if( m_pSongs[i]->Matches(sGroup, sSong) )
return m_pSongs[i];
}
return NULL;
}
Course *SongManager::FindCourse( CString sName )
{
for( unsigned i = 0; i < m_pCourses.size(); i++ )
{
if( !sName.CompareNoCase(m_pCourses[i]->m_sName) )
return m_pCourses[i];
}
return NULL;
2003-07-21 21:45:59 +00:00
}
void SongManager::UpdateBest()
{
m_pBestSongs = m_pSongs;
SortSongPointerArrayByMostPlayed( m_pBestSongs );
}
void SongManager::UpdateRankingCourses()
{
/* Updating the ranking courses data is fairly expensive
* since it involves comparing strings. Do so sparingly.
*/
CStringArray RankingCourses;
split( THEME->GetMetric("ScreenRanking","CoursesToShow"),",", RankingCourses);
for(unsigned i=0; i < m_pCourses.size(); i++)
{
if (m_pCourses[i]->GetEstimatedNumStages() > 7)
m_pCourses[i]->SortOrder_Ranking = 3;
else
m_pCourses[i]->SortOrder_Ranking = 2;
for(unsigned j = 0; j < RankingCourses.size(); j++)
if (!RankingCourses[j].CompareNoCase(m_pCourses[i]->m_sPath))
m_pCourses[i]->SortOrder_Ranking = 1;
}
2003-08-10 03:23:17 +00:00
}
2003-09-18 19:20:17 +00:00
static CString HTMLQuoteDoubleQuotes( CString str )
{
str.Replace( "\"", "&quot;" );
return str;
}
static bool CompareStepsPointersByTypeAndDifficulty(const Steps *pStep1, const Steps *pStep2)
{
if( pStep1->m_StepsType < pStep2->m_StepsType )
return true;
if( pStep1->m_StepsType > pStep2->m_StepsType )
return false;
return pStep1->GetDifficulty() < pStep2->GetDifficulty();
}
static void SortStepsByTypeAndDifficulty( vector<Steps*> &arraySongPointers )
{
sort( arraySongPointers.begin(), arraySongPointers.end(), CompareStepsPointersByTypeAndDifficulty );
}
static void HTMLWritePerGameHeader( FILE* fp, Game game )
{
const GameDef* pGameDef = GAMEMAN->GetGameDefForGame(game);
vector<StepsType> aStepsTypes;
GAMEMAN->GetNotesTypesForGame( game, aStepsTypes );
fprintf( fp, "<h1>%s</h1>\n", pGameDef->m_szName );
fprintf( fp, "<table border='1'>\n" );
fprintf( fp, "<tr><td>title</td>" );
unsigned j;
for( j=0; j<aStepsTypes.size(); j++ )
{
StepsType st = aStepsTypes[j];
fprintf( fp, "<td colspan='%d'>%s</td>\n", NUM_DIFFICULTIES, GAMEMAN->NotesTypeToString(st).c_str() );
}
fprintf( fp, "</tr>\n" );
fprintf( fp, "<tr><td>&nbsp;</td>" );
for( j=0; j<aStepsTypes.size(); j++ )
{
for( unsigned k=0; k<NUM_DIFFICULTIES; k++ )
{
Difficulty d = (Difficulty)k;
fprintf( fp, "<td>%s</td>\n", Capitalize(DifficultyToString(d).Left(3)).c_str() );
}
}
fprintf( fp, "</tr>\n" );
}
// TODO: Move this to a different file. No need to clutter SongManager.
void SongManager::WriteStatsWebPage()
{
FILE* fp = fopen( STATS_PATH, "w" );
if( fp == NULL )
return;
fprintf( fp,
"<html>\n"
"<head>\n"
2003-09-04 07:00:43 +00:00
"<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\">\n"
"<title>%s</title>\n"
"</head>\n"
"<body>\n",
PRODUCT_NAME_VER );
vector<Song*> vSongs = m_pSongs;
SortSongPointerArrayByGroupAndTitle( vSongs );
//
// Print song list
//
fprintf( fp, "<table border='1'>\n" );
for( unsigned i=0; i<vSongs.size(); i++ )
{
Song* pSong = m_pSongs[i];
fprintf( fp, "<tr>" );
2003-09-04 06:57:36 +00:00
/* XXX: We can't call pSong->HasBanner on every song; it'll effectively re-traverse the entire
* song directory tree checking if each banner file really exists.
*
* (Note for testing this: remember that we'll cache directories for a time; this is only slow if
* the directory cache expires before we get here.) */
//CString sImagePath = pSong->HasBanner() ? pSong->GetBannerPath() : (pSong->HasBackground() ? pSong->GetBackgroundPath() : "" );
CString sImagePath = pSong->GetBannerPath();
if( sImagePath.empty() )
fprintf( fp, "<td> </td>" );
else
2003-09-18 19:20:17 +00:00
fprintf( fp, "<td><img src=\"%s\" width='120'></td>", HTMLQuoteDoubleQuotes(sImagePath).c_str() );
fprintf( fp, "<td>%s<br>", pSong->GetTranslitMainTitle().c_str() );
fprintf( fp, "<font size='-1'>%s</font><br>", pSong->GetTranslitSubTitle().c_str() );
fprintf( fp, "<font size='-1'><i>%s</i></font></td>", pSong->GetTranslitArtist().c_str() );
fprintf( fp, "</tr>\n" );
}
fprintf( fp, "</table>\n<br>\n" );
//
// Print steps tables
//
for( int g=0; g<NUM_GAMES; g++ )
{
Game game = (Game)g;
vector<StepsType> aStepsTypes;
GAMEMAN->GetNotesTypesForGame( game, aStepsTypes );
bool WroteHeader = false;
for( unsigned i=0; i<vSongs.size(); i++ )
{
/* Get the steps for this game type. */
Song* pSong = m_pSongs[i];
vector<Steps*> Steps;
unsigned j;
for( j=0; j < aStepsTypes.size(); j++ )
pSong->GetSteps( Steps, (StepsType) aStepsTypes[j], DIFFICULTY_INVALID, -1, -1, "", false );
/* Don't write anything for songs that have no steps at all for this
* game. Otherwise, we'll write pages and pages of empty fields for
* all of the less-used game types. */
if( Steps.size() == 0 )
continue; // skip
/* We have some steps for this game. Make sure we've written the game header. */
if( !WroteHeader )
{
HTMLWritePerGameHeader( fp, game );
WroteHeader = true;
}
fprintf( fp, "<tr>\n" );
fprintf( fp, "<td>%s</td>", pSong->GetTranslitMainTitle().c_str() );
SortStepsByTypeAndDifficulty( Steps );
unsigned CurSteps = 0;
for( j=0; j<aStepsTypes.size(); j++ )
{
for( int k=0; k<NUM_DIFFICULTIES; k++ )
{
if( CurSteps < Steps.size() &&
Steps[CurSteps]->m_StepsType == aStepsTypes[j] &&
Steps[CurSteps]->GetDifficulty() == k )
{
fprintf( fp, "<td>%d</td>\n", Steps[CurSteps]->GetMeter() );
++CurSteps;
}
else
fprintf( fp, "<td>&nbsp;</td>\n" );
}
}
fprintf( fp, "</tr>" );
}
if( WroteHeader )
fprintf( fp, "</table>\n<br>\n" ); // footer
}
fprintf( fp,
"</body>\n"
"</html>\n"
);
fclose( fp );
}