From e95d5ad5dd9fa7d0d01e2fff729e6a267db7fe21 Mon Sep 17 00:00:00 2001 From: hikerstk Date: Thu, 25 Mar 2010 22:29:47 +0000 Subject: [PATCH] A historical commit: replaced highscores lisp files with xml, allowing us to completely remove the lisp code from stk - hooray! git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@5072 178a84e3-b1eb-0310-8ba1-8eac791a3b58 --- src/Makefile.am | 8 - src/ide/vc9/supertuxkart.vcproj | 40 ----- src/modes/world.cpp | 20 +-- src/modes/world.hpp | 2 +- src/race/highscore_manager.cpp | 156 +++++++----------- src/race/highscore_manager.hpp | 26 +-- src/race/highscores.cpp | 106 ++++++------ src/race/highscores.hpp | 26 +-- .../dialogs/race_over_dialog.cpp | 2 +- .../dialogs/track_info_dialog.cpp | 14 +- 10 files changed, 161 insertions(+), 239 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index c88f83127..0b8346b8e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -172,14 +172,6 @@ supertuxkart_SOURCES = \ karts/kart_properties_manager.hpp \ karts/moveable.cpp \ karts/moveable.hpp \ - lisp/lexer.cpp \ - lisp/lexer.hpp \ - lisp/lisp.cpp \ - lisp/lisp.hpp \ - lisp/parser.cpp \ - lisp/parser.hpp \ - lisp/writer.cpp \ - lisp/writer.hpp \ modes/follow_the_leader.cpp \ modes/follow_the_leader.hpp \ modes/linear_world.cpp \ diff --git a/src/ide/vc9/supertuxkart.vcproj b/src/ide/vc9/supertuxkart.vcproj index d2057bd1b..b4815ce1f 100644 --- a/src/ide/vc9/supertuxkart.vcproj +++ b/src/ide/vc9/supertuxkart.vcproj @@ -285,26 +285,6 @@ - - - - - - - - - - @@ -1019,26 +999,6 @@ - - - - - - - - - - diff --git a/src/modes/world.cpp b/src/modes/world.cpp index 98d541442..a25b00c4b 100644 --- a/src/modes/world.cpp +++ b/src/modes/world.cpp @@ -445,18 +445,18 @@ void World::update(float dt) // ---------------------------------------------------------------------------- -HighscoreEntry* World::getHighscores() const +Highscores* World::getHighscores() const { if(!m_use_highscores) return NULL; - const HighscoreEntry::HighscoreType type = "HST_" + getIdent(); + const Highscores::HighscoreType type = "HST_" + getIdent(); - HighscoreEntry* highscores = - highscore_manager->getHighscoreEntry(type, - getNumKarts(), - race_manager->getDifficulty(), - race_manager->getTrackName(), - race_manager->getNumLaps()); + Highscores * highscores = + highscore_manager->getHighscores(type, + getNumKarts(), + race_manager->getDifficulty(), + race_manager->getTrackName(), + race_manager->getNumLaps()); return highscores; } // getHighscores @@ -515,14 +515,14 @@ void World::updateHighscores() assert(index[pos] < m_karts.size()); Kart *k = (Kart*)m_karts[index[pos]]; - HighscoreEntry* highscores = getHighscores(); + Highscores* highscores = getHighscores(); PlayerController *controller = (PlayerController*)(k->getController()); if(highscores->addData(k->getIdent(), controller->getPlayer()->getProfile()->getName(), k->getFinishTime())>0 ) { - highscore_manager->Save(); + highscore_manager->saveHighscores(); } } // next position delete []index; diff --git a/src/modes/world.hpp b/src/modes/world.hpp index 663d21cd1..06d5b58b4 100644 --- a/src/modes/world.hpp +++ b/src/modes/world.hpp @@ -179,7 +179,7 @@ public: float getFastestLapTime() const { return m_fastest_lap; } void setFastestLap(Kart *k, float time){ m_fastest_kart = k; m_fastest_lap = time; } - HighscoreEntry *getHighscores() const; + Highscores *getHighscores() const; virtual void terminateRace(); diff --git a/src/race/highscore_manager.cpp b/src/race/highscore_manager.cpp index 3a5ceeaa4..92c80c260 100644 --- a/src/race/highscore_manager.cpp +++ b/src/race/highscore_manager.cpp @@ -20,12 +20,10 @@ #include "race/highscore_manager.hpp" #include -#include +#include #include "config/user_config.hpp" #include "io/file_manager.hpp" -#include "lisp/parser.hpp" -#include "lisp/writer.hpp" #include "race/race_manager.hpp" #include "utils/string_utils.hpp" #include "utils/translation.hpp" @@ -35,53 +33,45 @@ HighscoreManager* highscore_manager=0; HighscoreManager::HighscoreManager() { m_can_write=true; - SetFilename(); - Load(); + setFilename(); + loadHighscores(); } // HighscoreManager // ----------------------------------------------------------------------------- HighscoreManager::~HighscoreManager() { - Save(); - for(type_all_scores::iterator i = m_allScores.begin(); - i != m_allScores.end(); i++) + saveHighscores(); + for(type_all_scores::iterator i = m_all_scores.begin(); + i != m_all_scores.end(); i++) delete *i; } // ~HighscoreManager // ----------------------------------------------------------------------------- /** Determines the path to store the highscore file in */ -void HighscoreManager::SetFilename() +void HighscoreManager::setFilename() { if ( getenv("SUPERTUXKART_HIGHSCOREDIR") != NULL ) { m_filename = getenv("SUPERTUXKART_HIGHSCOREDIR") - + std::string("/highscore.data"); + + std::string("/highscore.xml"); } else { - m_filename=file_manager->getHighscoreFile("highscore.data"); + m_filename=file_manager->getHighscoreFile("highscore.xml"); } return; } // SetFilename // ----------------------------------------------------------------------------- -void HighscoreManager::Load() +void HighscoreManager::loadHighscores() { - - const lisp::Lisp* root = 0; - std::exception err; - - try + XMLNode *root = NULL; + root = file_manager->createXMLTree(m_filename); + if(!root) { - lisp::Parser parser; - root = parser.parse(m_filename); - } - catch(std::exception& err) - { - (void)err; // remove warning about unused variable - Save(); + saveHighscores(); if(m_can_write) { fprintf(stderr, "New highscore file '%s' created.\n", @@ -90,55 +80,39 @@ void HighscoreManager::Load() delete root; return; } + try { - // check for opening 'highscores' nodes - const lisp::Lisp* const node = root->getLisp("highscores"); - if(!node) - { + if(!root || root->getName()!="highscores") throw std::runtime_error("No 'highscore' node found."); - } // check file version int v; - if (!node->get("file-version",v) || v<(int)CURRENT_HSCORE_FILE_VERSION) + if (!root->get("version", &v) || v<(int)CURRENT_HSCORE_FILE_VERSION) { - fprintf(stderr, "Highscore file format too old, a new one will be created.\n"); - irr::core::stringw warning = _("The highscore file was too old,\nall highscores have been erased."); + fprintf(stderr, + "Highscore file format too old, a new one will be created.\n"); + irr::core::stringw warning = + _("The highscore file was too old,\nall highscores have been erased."); user_config->setWarning( warning ); // since we haven't had the chance to load the current scores yet, - // calling Save() now will generate an empty file. - Save(); + // calling Save() now will generate an empty file with the right format. + saveHighscores(); return; } - // get number of entries - int n; - if (!node->get("number-entries",n)) + // read all entries one by one and store them in 'm_all_scores' + for(unsigned int i=0; igetNumNodes(); i++) { - throw std::runtime_error("No 'number-entries' node found."); - } - - // read all entries one by one and store them in 'm_allScores' - for(int i=0; igetLisp(record_name.str()); - if(!node_record) - { - std::ostringstream msg; - msg << "Can't find record '" << i << "' in '" - << m_filename << "'"; - throw std::runtime_error(msg.str()); - } - HighscoreEntry *highscores = new HighscoreEntry(node_record); - m_allScores.push_back(highscores); - //highscores->Read(node_record); - }// next entry + const XMLNode *node = root->getNode(i); + Highscores *highscores = new Highscores(*node); + m_all_scores.push_back(highscores); + } // next entry - fprintf(stderr, "Highscores will be saved in '%s'.\n",m_filename.c_str()); + if(UserConfigParams::m_verbosity>=4) + fprintf(stderr, "Highscores will be saved in '%s'.\n", + m_filename.c_str()); } catch(std::exception& err) { @@ -149,78 +123,60 @@ void HighscoreManager::Load() fprintf(stderr, "No old highscores will be available.\n"); } delete root; -} // Load +} // loadHighscores // ----------------------------------------------------------------------------- -void HighscoreManager::Save() +void HighscoreManager::saveHighscores() { // Print error message only once if(!m_can_write) return; -#if 0 + try { std::ofstream highscore_file; - highscore_file.open(m_filename); + highscore_file.open(m_filename.c_str()); highscore_file << "\n"; - configfile << "\n\n"; + highscore_file << "\n"; - } -#else - try - { - lisp::Writer writer(m_filename); - writer.beginList("highscores"); - writer.writeComment("File format version"); - writer.write("file-version\t", CURRENT_HSCORE_FILE_VERSION); - writer.writeComment("Number of highscores in this file"); - writer.write("number-entries\t",(unsigned int)m_allScores.size()); - int record_number=0; - for(type_all_scores::iterator i = m_allScores.begin(); - i != m_allScores.end(); i++) + for(unsigned int i=0; iWrite(&writer); - writer.endList(record_name.str()); - } // next score - writer.endList("highscores"); - m_can_write=true; - } // try + m_all_scores[i]->writeEntry(highscore_file); + } + highscore_file << "\n"; + highscore_file.close(); + } catch(std::exception &e) { - printf("Problems saving highscores in '%s'\n", - m_filename.c_str()); + printf("Problems saving highscores in '%s'\n", m_filename.c_str()); puts(e.what()); m_can_write=false; } -#endif -} // Save + +} // saveHighscores // ----------------------------------------------------------------------------- /* * Returns the high scores entry for a specific type of race. Creates one if none exists yet. */ -HighscoreEntry* HighscoreManager::getHighscoreEntry(const HighscoreEntry::HighscoreType highscore_type, - int num_karts, const RaceManager::Difficulty difficulty, - const std::string trackName, const int number_of_laps) +Highscores* HighscoreManager::getHighscores(const Highscores::HighscoreType highscore_type, + int num_karts, const RaceManager::Difficulty difficulty, + const std::string trackName, const int number_of_laps) { - HighscoreEntry *highscores = 0; + Highscores *highscores = 0; // See if we already have a record for this type - for(type_all_scores::iterator i = m_allScores.begin(); - i != m_allScores.end(); i++) + for(type_all_scores::iterator i = m_all_scores.begin(); + i != m_all_scores.end(); i++) { if((*i)->matches(highscore_type, num_karts, difficulty, trackName, number_of_laps) ) { // we found one entry for this kind of race, return it return (*i); } - } // for i in m_allScores + } // for i in m_all_scores // we don't have an entry for such a race currently. Create one. - highscores = new HighscoreEntry(highscore_type, num_karts, difficulty, trackName, number_of_laps); - m_allScores.push_back(highscores); + highscores = new Highscores(highscore_type, num_karts, difficulty, trackName, number_of_laps); + m_all_scores.push_back(highscores); return highscores; -} // getHighscoreEntry +} // getHighscores diff --git a/src/race/highscore_manager.hpp b/src/race/highscore_manager.hpp index eb8f43489..e7fff8ed5 100644 --- a/src/race/highscore_manager.hpp +++ b/src/race/highscore_manager.hpp @@ -25,9 +25,6 @@ #include #include "race/highscores.hpp" -#include "lisp/lisp.hpp" - -const unsigned int CURRENT_HSCORE_FILE_VERSION = 1; /** * This class reads and writes the 'highscores.data' file, and also takes @@ -38,22 +35,25 @@ class HighscoreManager { public: private: - typedef std::vector type_all_scores; - type_all_scores m_allScores; + static const unsigned int CURRENT_HSCORE_FILE_VERSION = 1; + typedef std::vector type_all_scores; + type_all_scores m_all_scores; std::string m_filename; bool m_can_write; - void Load(); - void SetFilename(); + void loadHighscores(); + void setFilename(); public: - HighscoreManager(); - ~HighscoreManager(); - void Save(); - HighscoreEntry *getHighscoreEntry(const HighscoreEntry::HighscoreType highscore_type, - int num_karts, const RaceManager::Difficulty difficulty, - const std::string trackName, const int number_of_laps); + HighscoreManager(); + ~HighscoreManager(); + void saveHighscores(); + Highscores *getHighscores(const Highscores::HighscoreType highscore_type, + int num_karts, + const RaceManager::Difficulty difficulty, + const std::string trackName, + const int number_of_laps); }; // HighscoreManager extern HighscoreManager* highscore_manager; diff --git a/src/race/highscores.cpp b/src/race/highscores.cpp index 7f8ba3aef..83dbbbf0a 100644 --- a/src/race/highscores.cpp +++ b/src/race/highscores.cpp @@ -20,14 +20,17 @@ #include "race/highscores.hpp" #include -#include +#include +#include "io/xml_node.hpp" #include "race/race_manager.hpp" // ----------------------------------------------------------------------------- -HighscoreEntry::HighscoreEntry(const HighscoreEntry::HighscoreType highscore_type, - int num_karts, const RaceManager::Difficulty difficulty, - const std::string trackName, const int number_of_laps) +Highscores::Highscores(const HighscoreType highscore_type, + int num_karts, + const RaceManager::Difficulty difficulty, + const std::string trackName, + const int number_of_laps) { m_track = trackName; m_highscore_type = highscore_type; @@ -43,7 +46,7 @@ HighscoreEntry::HighscoreEntry(const HighscoreEntry::HighscoreType highscore_typ } } // ----------------------------------------------------------------------------- -HighscoreEntry::HighscoreEntry(const lisp::Lisp* const node) +Highscores::Highscores(const XMLNode &node) { m_track = ""; m_highscore_type = "HST_UNDEFINED"; @@ -58,54 +61,62 @@ HighscoreEntry::HighscoreEntry(const lisp::Lisp* const node) m_time[i] = -9.9f; } - Read(node); -} + readEntry(node); +} // Highscores + // ----------------------------------------------------------------------------- -void HighscoreEntry::Read(const lisp::Lisp* const node) +void Highscores::readEntry(const XMLNode &node) { - node->get("track-name", m_track ); - node->get("number-karts", m_number_of_karts ); + node.get("track-name", &m_track ); + node.get("number-karts", &m_number_of_karts ); std::string hst="HST_UNDEFINED"; - node->get("hscore-type", hst ); + node.get("hscore-type", &hst ); m_highscore_type = (HighscoreType)hst; - node->get("difficulty", m_difficulty ); - node->get("number-of-laps", m_number_of_laps ); + node.get("difficulty", &m_difficulty ); + node.get("number-of-laps", &m_number_of_laps ); + + for(unsigned int i=0; iget("time", &m_time[i] ); + entry->get("name", &m_name[i] ); + entry->get("kart-name", &m_kart_name[i] ); + } +} // readEntry + +// ----------------------------------------------------------------------------- +/** Writes the highscores in this entry to the writer. It will only write + * anything if there is actually a highscore recored (i.e. time >=0). Empty + * entries are created e.g. when changing the number of laps in the GUI, + * resulting in empty entries here. + * \param writer The file stream to write the data to. + */ +void Highscores::writeEntry(std::ofstream &writer) +{ + // Only + bool one_is_set = false; + for(unsigned int i=0; i=0; + if(!one_is_set) return; + + writer << " \n"; for(int i=0; iget(s.str(),m_time[i] ); - s.str(""); s << "name-" << i; - node->get(s.str(),m_name[i] ); - s.str(""); s << "kartname-" << i; - node->get(s.str(), m_kart_name[i] ); - } -} // Read + writer << " \n"; + } // for i + writer << " \n"; +} // writeEntry // ----------------------------------------------------------------------------- -void HighscoreEntry::Write(lisp::Writer *writer) -{ - writer->write("track-name\t", m_track ); - writer->write("number-karts\t", m_number_of_karts ); - writer->write("difficulty\t\t", m_difficulty ); - writer->write("hscore-type\t\t", m_highscore_type ); - writer->write("number-of-laps\t", m_number_of_laps ); - for(int j=0; jwrite(s.str(), m_time[j] ); - s.str(""); s << "name-" << j << "\t\t"; - writer->write(s.str(), m_name[j] ); - s.str(""); s << "kartname-" << j << "\t\t"; - writer->write(s.str(), m_kart_name[j] ); - } // for j - -} // Write - -// ----------------------------------------------------------------------------- -int HighscoreEntry::matches(HighscoreType highscore_type, +int Highscores::matches(HighscoreType highscore_type, int num_karts, RaceManager::Difficulty difficulty, const std::string track, const int number_of_laps) { @@ -115,13 +126,14 @@ int HighscoreEntry::matches(HighscoreType highscore_type, m_number_of_laps == number_of_laps && m_number_of_karts == num_karts ); } // matches + // ----------------------------------------------------------------------------- /** Inserts the data into the highscore list. * If the new entry is fast enough to * be in the highscore list, the new position (1-HIGHSCORE_LEN) is returned, * otherwise a 0. */ -int HighscoreEntry::addData(const std::string& kart_name, +int Highscores::addData(const std::string& kart_name, const std::string& name, const float time) { int position=-1; @@ -163,7 +175,7 @@ int HighscoreEntry::addData(const std::string& kart_name, } // addData // ----------------------------------------------------------------------------- -int HighscoreEntry::getNumberEntries() const +int Highscores::getNumberEntries() const { for(int i=HIGHSCORE_LEN-1; i>=0; i--) { @@ -173,7 +185,7 @@ int HighscoreEntry::getNumberEntries() const } // getNumberEntries // ----------------------------------------------------------------------------- -void HighscoreEntry::getEntry(int number, std::string &kart_name, +void Highscores::getEntry(int number, std::string &kart_name, std::string &name, float *const time) const { if(number<0 || number>getNumberEntries()) diff --git a/src/race/highscores.hpp b/src/race/highscores.hpp index 3c4b1ec34..07fdaf7d5 100644 --- a/src/race/highscores.hpp +++ b/src/race/highscores.hpp @@ -22,15 +22,17 @@ #include #include #include +#include -#include "lisp/lisp.hpp" -#include "lisp/writer.hpp" #include "race/race_manager.hpp" +class XMLNode; + /** - * Represents one highscore entry. - */ -class HighscoreEntry + * Represents one highscore entry, i.e. the (atm up to three) highscores + * for a particular setting (track, #karts, difficulty etc). + */ +class Highscores { public: typedef std::string HighscoreType; @@ -48,15 +50,15 @@ private: public: /** Creates a new entry */ - HighscoreEntry (const HighscoreEntry::HighscoreType highscore_type, - int num_karts, const RaceManager::Difficulty difficulty, - const std::string trackName, const int number_of_laps); + Highscores (const Highscores::HighscoreType highscore_type, + int num_karts, const RaceManager::Difficulty difficulty, + const std::string trackName, const int number_of_laps); /** Creates an entry from a file */ - HighscoreEntry (const lisp::Lisp* const node); + Highscores (const XMLNode &node); - void Read (const lisp::Lisp* const node); - void Write (lisp::Writer* writer); + void readEntry (const XMLNode &node); + void writeEntry(std::ofstream &writer); int matches (HighscoreType highscore_type, int num_karts, const RaceManager::Difficulty difficulty, const std::string track, const int number_of_laps); @@ -65,6 +67,6 @@ public: int getNumberEntries() const; void getEntry (int number, std::string &kart_name, std::string &name, float *const time) const; -}; // HighscoreEntry +}; // Highscores #endif diff --git a/src/states_screens/dialogs/race_over_dialog.cpp b/src/states_screens/dialogs/race_over_dialog.cpp index bca479664..bb6267799 100644 --- a/src/states_screens/dialogs/race_over_dialog.cpp +++ b/src/states_screens/dialogs/race_over_dialog.cpp @@ -181,7 +181,7 @@ RaceOverDialog::RaceOverDialog(const float percentWidth, // ---- Highscores if (show_highscores) { - const HighscoreEntry *hs = World::getWorld()->getHighscores(); + const Highscores *hs = World::getWorld()->getHighscores(); if (hs != NULL) { core::rect< s32 > hsarea(m_area.getWidth()*2/3, 0, m_area.getWidth(), text_height); diff --git a/src/states_screens/dialogs/track_info_dialog.cpp b/src/states_screens/dialogs/track_info_dialog.cpp index b4c600389..7469f8c5e 100644 --- a/src/states_screens/dialogs/track_info_dialog.cpp +++ b/src/states_screens/dialogs/track_info_dialog.cpp @@ -213,14 +213,14 @@ void TrackInfoDialog::addHighScoreWidgets(const int hscores_y_from, const int hs void TrackInfoDialog::updateHighScores() { std::string game_mode_ident = RaceManager::getIdentOf( race_manager->getMinorMode() ); - const HighscoreEntry::HighscoreType type = "HST_" + game_mode_ident; + const Highscores::HighscoreType type = "HST_" + game_mode_ident; - HighscoreEntry* highscores = - highscore_manager->getHighscoreEntry(type, - race_manager->getNumberOfKarts(), - race_manager->getDifficulty(), - m_track_ident, - race_manager->getNumLaps() ); + Highscores* highscores = + highscore_manager->getHighscores(type, + race_manager->getNumberOfKarts(), + race_manager->getDifficulty(), + m_track_ident, + race_manager->getNumLaps() ); const int amount = highscores->getNumberEntries(); std::string kart_name;