Bugfix: incorrect track name or GP in a challenge will not cause a crash

when trying to use to do the challenge, instead STK will abort with an
error message indicating the problem at startup (see bug 2316973).


git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/trunk/supertuxkart@2851 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
hikerstk 2009-01-07 00:54:20 +00:00
parent 4c3834e4e2
commit b643cfd599
6 changed files with 50 additions and 2 deletions

View File

@ -94,5 +94,7 @@ public:
virtual bool raceFinished() {return false;} // end of a race
virtual bool grandPrixFinished() {return false;} // end of a GP
virtual void setRace() const = 0; // set race to use
/** Checks if a challenge is valid. */
virtual void check() const = 0;
};
#endif

View File

@ -24,6 +24,7 @@
#include "translation.hpp"
#include "grand_prix_data.hpp"
#include "grand_prix_manager.hpp"
#include "track_manager.hpp"
#include "karts/kart.hpp"
#include "lisp/lisp.hpp"
#include "lisp/parser.hpp"
@ -135,7 +136,7 @@ ChallengeData::ChallengeData(const std::string& filename)
} // ChallengeData
// ----------------------------------------------------------------------------
void ChallengeData::error(const char *id)
void ChallengeData::error(const char *id) const
{
char msg[MAX_ERROR_MESSAGE_LENGTH];
snprintf(msg, sizeof(msg), "Undefined or incorrect value for '%s' in challenge file '%s'.",
@ -143,6 +144,33 @@ void ChallengeData::error(const char *id)
throw std::runtime_error(msg);
} // error
// ----------------------------------------------------------------------------
/** Checks if this challenge is valid, i.e. contains a valid track or a valid
* GP. If incorrect data are found, STK is aborted with an error message.
* (otherwise STK aborts when trying to do this challenge, which is worse).
*/
void ChallengeData::check() const
{
if(m_major==RaceManager::MAJOR_MODE_SINGLE)
{
try
{
track_manager->getTrack(m_track_name);
}
catch(std::exception&)
{
error("track");
}
}
else if(m_major==RaceManager::MAJOR_MODE_GRAND_PRIX)
{
if(!grand_prix_manager->getGrandPrix(m_gp_id))
{
error("gp");
}
}
} // check
// ----------------------------------------------------------------------------
void ChallengeData::getUnlocks(const lisp::Lisp *lisp, const char* type,

View File

@ -45,11 +45,12 @@ private:
std::string m_filename;
void getUnlocks(const lisp::Lisp *lisp, const char* type, REWARD_TYPE reward);
void error(const char *id);
void error(const char *id) const;
public:
ChallengeData(const std::string& filename);
void setRace() const;
virtual void check() const;
virtual bool raceFinished();
virtual bool grandPrixFinished();
}; // ChallengeData

View File

@ -451,6 +451,8 @@ void InitTuxkart()
grand_prix_manager = new GrandPrixManager ();
network_manager = new NetworkManager ();
track_manager->loadTrackList();
// Check needs GP and track manager.
unlock_manager->check();
sound_manager->addMusicToTracks();
stk_config->load(file_manager->getConfigFile("stk_config.data"));

View File

@ -128,6 +128,20 @@ void UnlockManager::addChallenge(const std::string& filename)
{
addChallenge(new ChallengeData(filename));
} // addChallenge
//-----------------------------------------------------------------------------
/** Checks if all challenges are valid, i.e. contain a valid track or GP.
* If not, STK is aborted with an error message.
*/
void UnlockManager::check() const
{
for(AllChallengesType::const_iterator i =m_all_challenges.begin();
i!=m_all_challenges.end(); i++)
{
i->second->check();
} // for i
} // check
//-----------------------------------------------------------------------------
std::vector<const Challenge*> UnlockManager::getActiveChallenges()
{

View File

@ -53,6 +53,7 @@ public:
void unlockFeature (Challenge* c, bool save=true);
void lockFeature (Challenge* challenge);
bool isLocked (const std::string& feature);
void check () const;
}; // UnlockManager
extern UnlockManager* unlock_manager;