2007-05-27 12:01:53 -04:00
|
|
|
//
|
|
|
|
// SuperTuxKart - a fun racing game with go-kart
|
|
|
|
// Copyright (C) 2004-2005 Ingo Ruhnke <grumbel@gmx.de>
|
|
|
|
// Copyright (C) 2006 Joerg Henrichs, Ingo Ruhnke <grumbel@gmx.de>
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License
|
2008-06-12 20:53:52 -04:00
|
|
|
// as published by the Free Software Foundation; either version 3
|
2007-05-27 12:01:53 -04:00
|
|
|
// of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
2009-06-02 21:36:48 -04:00
|
|
|
#include "race/grand_prix_data.hpp"
|
|
|
|
|
2007-05-27 12:01:53 -04:00
|
|
|
#include <iostream>
|
|
|
|
#include <stdexcept>
|
2009-06-02 21:36:48 -04:00
|
|
|
|
2009-03-11 23:49:31 -04:00
|
|
|
#include "io/file_manager.hpp"
|
2009-01-22 07:02:40 -05:00
|
|
|
#include "tracks/track_manager.hpp"
|
2009-01-22 17:27:13 -05:00
|
|
|
#include "utils/string_utils.hpp"
|
2009-08-30 14:21:59 -04:00
|
|
|
#include "utils/translation.hpp"
|
2007-05-27 12:01:53 -04:00
|
|
|
|
2010-02-15 19:33:41 -05:00
|
|
|
GrandPrixData::GrandPrixData(const std::string filename) throw(std::logic_error)
|
2007-05-27 12:01:53 -04:00
|
|
|
{
|
2008-07-29 00:30:44 -04:00
|
|
|
m_filename = filename;
|
2009-08-24 01:56:53 -04:00
|
|
|
m_id = StringUtils::getBasename(StringUtils::removeExtension(filename));
|
2010-02-15 19:33:41 -05:00
|
|
|
|
|
|
|
XMLNode* root = file_manager->createXMLTree(file_manager->getDataDir()+filename);
|
|
|
|
if (!root)
|
2007-05-27 12:01:53 -04:00
|
|
|
{
|
2010-02-15 19:33:41 -05:00
|
|
|
fprintf(stderr, "/!\\ Error while trying to read grandprix file '%s'\n", filename.c_str());
|
|
|
|
throw std::logic_error("File not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool foundName = false;
|
|
|
|
|
|
|
|
if (root->getName() == "supertuxkart_grand_prix")
|
|
|
|
{
|
|
|
|
std::string temp_name;
|
|
|
|
if (root->get("name", &temp_name) == 0)
|
2007-05-27 12:01:53 -04:00
|
|
|
{
|
2010-02-15 19:33:41 -05:00
|
|
|
fprintf(stderr, "/!\\ Error while trying to read grandprix file '%s' : "
|
|
|
|
"missing 'name' attribute\n", filename.c_str());
|
|
|
|
delete root;
|
|
|
|
throw std::logic_error("File contents are incomplete or corrupt");
|
2007-05-27 12:01:53 -04:00
|
|
|
}
|
2011-06-16 21:46:30 -04:00
|
|
|
m_name = temp_name.c_str();
|
2010-06-07 17:50:14 -04:00
|
|
|
foundName = true;
|
2007-05-27 12:01:53 -04:00
|
|
|
}
|
2010-02-15 19:33:41 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "/!\\ Error while trying to read grandprix file '%s' : "
|
|
|
|
"Root node has an unexpected name\n", filename.c_str());
|
|
|
|
delete root;
|
|
|
|
throw std::logic_error("File contents are incomplete or corrupt");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const int amount = root->getNumNodes();
|
|
|
|
for (int i=0; i<amount; i++)
|
|
|
|
{
|
|
|
|
const XMLNode* node = root->getNode(i);
|
|
|
|
|
|
|
|
// read a track entry
|
|
|
|
if (node->getName() == "track")
|
|
|
|
{
|
|
|
|
std::string trackID;
|
|
|
|
int numLaps;
|
|
|
|
|
|
|
|
const int idFound = node->get("id", &trackID );
|
|
|
|
const int lapFound = node->get("laps", &numLaps );
|
|
|
|
|
|
|
|
if (!idFound || !lapFound)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "/!\\ Error while trying to read grandprix file '%s' : "
|
|
|
|
"<track> tag does not have id and laps attributes. \n", filename.c_str());
|
|
|
|
delete root;
|
|
|
|
throw std::logic_error("File contents are incomplete or corrupt");
|
|
|
|
}
|
|
|
|
|
|
|
|
m_tracks.push_back(trackID);
|
|
|
|
m_laps.push_back(numLaps);
|
|
|
|
|
|
|
|
assert(m_tracks.size() == m_laps.size());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "Unknown node in Grand Prix XML file : " << node->getName().c_str() << std::endl;
|
|
|
|
delete root;
|
|
|
|
throw std::runtime_error("Unknown node in sfx XML file");
|
|
|
|
}
|
|
|
|
}// nend for
|
|
|
|
|
|
|
|
delete root;
|
|
|
|
|
|
|
|
// sanity checks
|
|
|
|
if (!foundName)
|
2007-05-27 12:01:53 -04:00
|
|
|
{
|
2010-02-15 19:33:41 -05:00
|
|
|
fprintf(stderr, "/!\\ Error while trying to read grandprix file '%s' : "
|
|
|
|
"missing 'name' attribute\n", filename.c_str());
|
|
|
|
throw std::logic_error("File contents are incomplete or corrupt");
|
2007-05-27 12:01:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2008-07-29 00:30:44 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
2010-05-07 13:49:04 -04:00
|
|
|
bool GrandPrixData::checkConsistency(bool chatty) const
|
2008-07-29 00:30:44 -04:00
|
|
|
{
|
|
|
|
for(unsigned int i=0; i<m_tracks.size(); i++)
|
|
|
|
{
|
2010-05-07 13:49:04 -04:00
|
|
|
Track* t = track_manager->getTrack(m_tracks[i]);
|
|
|
|
|
|
|
|
if (t == NULL)
|
2008-07-29 09:40:25 -04:00
|
|
|
{
|
2010-05-07 13:49:04 -04:00
|
|
|
if (chatty)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Grand Prix '%ls': Track '%s' does not exist!\n",
|
|
|
|
m_name.c_str(), m_tracks[i].c_str());
|
|
|
|
fprintf(stderr, "This Grand Prix will not be available.\n");
|
|
|
|
}
|
2008-07-29 09:40:25 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-07-29 00:30:44 -04:00
|
|
|
} // for i
|
2008-07-29 09:40:25 -04:00
|
|
|
return true;
|
2008-07-29 00:30:44 -04:00
|
|
|
}
|
2007-05-27 12:01:53 -04:00
|
|
|
/* EOF */
|