fixed identing, added comments, corrected spelling, etc.

This commit is contained in:
konstin 2014-04-25 17:03:12 +02:00 committed by hiker
parent 4bddf29320
commit ca08ee9d86
5 changed files with 157 additions and 143 deletions

View File

@ -35,10 +35,11 @@
// ----------------------------------------------------------------------------
GrandPrixData::GrandPrixData(const std::string& filename) throw(std::logic_error)
GrandPrixData::GrandPrixData(const std::string& filename)
{
m_filename = filename;
m_id = StringUtils::getBasename(StringUtils::removeExtension(filename));
m_id = StringUtils::getBasename(
StringUtils::removeExtension(filename));
m_editable = (filename.find(file_manager->getGPDir(), 0) == 0);
reload();
}
@ -77,8 +78,9 @@ void GrandPrixData::reload()
std::auto_ptr<XMLNode> root(file_manager->createXMLTree(m_filename));
if (root.get() == NULL)
{
Log::error("GrandPrixData","Error while trying to read grandprix file '%s'",
m_filename.c_str());
Log::error("GrandPrixData",
"Error while trying to read grandprix file '%s'",
m_filename.c_str());
throw std::logic_error("File not found");
}
@ -88,20 +90,21 @@ void GrandPrixData::reload()
{
if (root->get("name", &m_name) == 0)
{
Log::error("GrandPrixData", "Error while trying to read grandprix file '%s' : "
"missing 'name' attribute\n", m_filename.c_str());
Log::error("GrandPrixData",
"Error while trying to read grandprix file '%s': "
"missing 'name' attribute", m_filename.c_str());
throw std::logic_error("File contents are incomplete or corrupt");
}
foundName = true;
}
else
{
Log::error("GrandPrixData", "Error while trying to read grandprix file '%s' : "
"Root node has an unexpected name\n", m_filename.c_str());
Log::error("GrandPrixData",
"Error while trying to read grandprix file '%s': "
"Root node has an unexpected name", m_filename.c_str());
throw std::logic_error("File contents are incomplete or corrupt");
}
const int amount = root->getNumNodes();
for (int i=0; i<amount; i++)
{
@ -114,25 +117,24 @@ void GrandPrixData::reload()
int numLaps;
bool reversed = false;
const int idFound = node->get("id", &trackID );
const int lapFound = node->get("laps", &numLaps );
const int idFound = node->get("id", &trackID );
const int lapFound = node->get("laps", &numLaps );
// Will stay false if not found
node->get("reverse", &reversed );
if (!idFound || !lapFound)
{
Log::error("GrandPrixData", "Error while trying to read grandprix file '%s' : "
"<track> tag does not have idi and laps reverse attributes. \n",
m_filename.c_str());
Log::error("GrandPrixData",
"Error while trying to read grandprix file '%s' : "
"<track> tag does not have id and laps reverse "
"attributes.", m_filename.c_str());
throw std::logic_error("File contents are incomplete or corrupt");
}
// Make sure the track really is reversible
Track* t = track_manager->getTrack(trackID);
if (t != NULL && reversed)
{
reversed = t->reverseAvailable();
}
m_tracks.push_back(trackID);
m_laps.push_back(numLaps);
@ -143,16 +145,19 @@ void GrandPrixData::reload()
}
else
{
std::cerr << "Unknown node in Grand Prix XML file : " << node->getName().c_str() << std::endl;
throw std::runtime_error("Unknown node in sfx XML file");
Log::error("GrandPrixData"
"Unknown node in Grand Prix XML file: %s",
node->getName().c_str());
throw std::runtime_error("Unknown node in the XML file");
}
}// nend for
} // end for
// sanity checks
if (!foundName)
{
Log::error("GrandPrixData", "Error while trying to read grandprix file '%s' : "
"missing 'name' attribute\n", m_filename.c_str());
Log::error("GrandPrixData",
"Error while trying to read grandprix file '%s': "
"missing 'name' attribute\n", m_filename.c_str());
throw std::logic_error("File contents are incomplete or corrupt");
}
}
@ -165,14 +170,15 @@ bool GrandPrixData::writeToFile()
UTFWriter file(m_filename.c_str());
if (file.is_open())
{
file << L"\n<supertuxkart_grand_prix name=\"" << m_name << L"\">\n\n";
file << L"\n<supertuxkart_grand_prix name=\"" << m_name
<< L"\">\n\n";
for (unsigned int i = 0; i < m_tracks.size(); i++)
{
file <<
L"\t<track id=\"" << m_tracks[i] <<
L"\" laps=\"" << m_laps[i] <<
L"\" reverse=\"" << (m_reversed[i] ? L"true" : L"false") <<
L"\" />\n";
L"\t<track id=\"" << m_tracks[i] <<
L"\" laps=\"" << m_laps[i] <<
L"\" reverse=\"" << (m_reversed[i] ? L"true" : L"false")
<< L"\" />\n";
}
file << L"\n</supertuxkart_grand_prix>\n";
@ -185,33 +191,32 @@ bool GrandPrixData::writeToFile()
}
catch (std::runtime_error& e)
{
Log::error("GrandPrixData", "Failed to write '%s'; cause: %s\n",
m_filename.c_str(), e.what());
Log::error("GrandPrixData",
"Failed to write grand prix to '%s'; cause: %s",
m_filename.c_str(), e.what());
return false;
}
}
// ----------------------------------------------------------------------------
bool GrandPrixData::checkConsistency(bool chatty) const
bool GrandPrixData::checkConsistency(bool log_error) const
{
for (unsigned int i = 0; i<m_tracks.size(); i++)
for (unsigned int i = 0; i < m_tracks.size(); i++)
{
Track* t = track_manager->getTrack(m_tracks[i]);
if (t == NULL)
if (track_manager->getTrack(m_tracks[i]) == NULL)
{
if (chatty)
if (log_error)
{
Log::error("GrandPrixData", "Grand Prix '%ls': Track '%s' does not exist!\n",
m_name.c_str(), m_tracks[i].c_str());
Log::error("GrandPrixData", "This Grand Prix will not be available.\n");
Log::error("GrandPrixData",
"The grand prix '%ls' won't be available because "
"the track '%s' does not exist!", m_name.c_str(),
m_tracks[i].c_str());
}
return false;
}
} // for i
}
return true;
} // checkConsistency
}
// ----------------------------------------------------------------------------
@ -221,27 +226,28 @@ bool GrandPrixData::checkConsistency(bool chatty) const
* is unlocked). It also prevents people from using the grand prix editor as
* a way to play tracks that still haven't been unlocked
*/
bool GrandPrixData::isTrackAvailable(const std::string &id, bool includeLocked) const
bool GrandPrixData::isTrackAvailable(const std::string &id, bool includeLocked)
const
{
if (includeLocked)
return true;
else if (id == "fortmagma")
return !PlayerManager::getCurrentPlayer()->isLocked("fortmagma");
else
return (!m_editable || !PlayerManager::get()->getCurrentPlayer()->isLocked(id));
} // isTrackAvailable
return (!m_editable
|| !PlayerManager::get()->getCurrentPlayer()->isLocked(id));
}
// ----------------------------------------------------------------------------
std::vector<std::string> GrandPrixData::getTrackNames(bool includeLocked) const
{
std::vector<std::string> names;
for (unsigned int i = 0; i < m_tracks.size(); i++)
{
if(isTrackAvailable(m_tracks[i], includeLocked))
names.push_back(m_tracks[i]);
} // for i
return names;
} // getTrackNames
}
// ----------------------------------------------------------------------------
std::vector<int> GrandPrixData::getLaps(bool includeLocked) const
@ -250,8 +256,9 @@ std::vector<int> GrandPrixData::getLaps(bool includeLocked) const
for (unsigned int i = 0; i< m_tracks.size(); i++)
if(isTrackAvailable(m_tracks[i], includeLocked))
laps.push_back(m_laps[i]);
return laps;
} // getLaps
}
// ----------------------------------------------------------------------------
std::vector<bool> GrandPrixData::getReverse(bool includeLocked) const
@ -260,14 +267,15 @@ std::vector<bool> GrandPrixData::getReverse(bool includeLocked) const
for (unsigned int i = 0; i< m_tracks.size(); i++)
if(isTrackAvailable(m_tracks[i], includeLocked))
reverse.push_back(m_reversed[i]);
return reverse;
} // getReverse
}
// ----------------------------------------------------------------------------
bool GrandPrixData::isEditable() const
{
return m_editable;
} // isEditable
}
// ----------------------------------------------------------------------------
unsigned int GrandPrixData::getNumberOfTracks(bool includeLocked) const
@ -281,7 +289,7 @@ unsigned int GrandPrixData::getNumberOfTracks(bool includeLocked) const
// ----------------------------------------------------------------------------
irr::core::stringw GrandPrixData::getTrackName(const unsigned int track) const
{
assert(track < getNumberOfTracks(true));
assert(0 <= track && track < getNumberOfTracks(true));
Track* t = track_manager->getTrack(m_tracks[track]);
assert(t != NULL);
return t->getName();
@ -334,49 +342,47 @@ void GrandPrixData::moveDown(const unsigned int track)
// ----------------------------------------------------------------------------
void GrandPrixData::addTrack(Track* track, unsigned int laps, bool reverse,
int position)
int position)
{
int n;
n = getNumberOfTracks(true);
int n = getNumberOfTracks(true);
assert (track != NULL);
assert (laps > 0);
assert (position >= -1 && position < n);
assert (-1 < position && position < n);
if (position < 0 || position == (n - 1) || m_tracks.empty())
{
//Append new track to the end of the list
// Append new track to the end of the list
m_tracks.push_back(track->getIdent());
m_laps.push_back(laps);
m_reversed.push_back(reverse);
}
else
{
//Insert new track right after the specified position. Caution:
//std::vector inserts elements _before_ the specified position
m_tracks.insert(m_tracks.begin() + position + 1, track->getIdent());
m_laps.insert(m_laps.begin() + position + 1, laps);
m_reversed.insert(m_reversed.begin() + position + 1, reverse);
// Insert new track right after the specified position. Caution:
// std::vector inserts elements _before_ the specified position
m_tracks. insert(m_tracks.begin() + position + 1, track->getIdent());
m_laps. insert(m_laps.begin() + position + 1, laps );
m_reversed.insert(m_reversed.begin() + position + 1, reverse );
}
}
// ----------------------------------------------------------------------------
void GrandPrixData::editTrack(unsigned int t, Track* track,
unsigned int laps, bool reverse)
void GrandPrixData::editTrack(unsigned int index, Track* track,
unsigned int laps, bool reverse)
{
assert (t < getNumberOfTracks(true));
assert (index < getNumberOfTracks(true));
assert (track != NULL);
assert (laps > 0);
m_tracks[t] = track->getIdent();
m_laps[t] = laps;
m_reversed[t] = reverse;
m_tracks[index] = track->getIdent();
m_laps[index] = laps;
m_reversed[index] = reverse;
}
// ----------------------------------------------------------------------------
void GrandPrixData::remove(const unsigned int track)
{
assert (track < getNumberOfTracks(true));
assert (0 < track && track < getNumberOfTracks(true));
m_tracks.erase(m_tracks.begin() + track);
m_laps.erase(m_laps.begin() + track);

View File

@ -51,7 +51,7 @@ private:
* (ie. 'volcano'). */
std::vector<std::string> m_tracks;
/** The number of laps that each track should be raced, in the right order */
/** The number of laps that each track will be raced, in the right order */
std::vector<int> m_laps;
/** Whether the track in question should be done in reverse mode */
@ -69,22 +69,25 @@ private:
bool isTrackAvailable(const std::string &id, bool includeLocked) const;
public:
#if (defined(WIN32) || defined(_WIN32)) && !defined(__MINGW32__)
#pragma warning(disable:4290)
#endif
/** Load the GrandPrixData from the given filename */
#if (defined(WIN32) || defined(_WIN32)) && !defined(__MINGW32__)
#pragma warning(disable:4290)
#endif
GrandPrixData (const std::string& filename) throw(std::logic_error);
GrandPrixData () {}; // empty for initialising
GrandPrixData(const std::string& filename);
/** Needed for simple creation of an instance of GrandPrixData */
GrandPrixData() {};
// Methods for the GP editor
void setId(const std::string& id);
void setName(const irr::core::stringw& name);
void setFilename(const std::string& filename);
void setEditable(const bool editable);
/** Load the grand prix from the file set by the constructor or the grand
* prix editor */
void reload();
bool writeToFile();
bool checkConsistency(bool chatty=true) const;
bool checkConsistency(bool log_error=true) const;
std::vector<std::string> getTrackNames(const bool includeLocked=false) const;
std::vector<int> getLaps(const bool includeLocked=false) const;
std::vector<bool> getReverse(const bool includeLocked=false) const;
@ -98,22 +101,22 @@ public:
void moveDown(const unsigned int track);
void addTrack(Track* track, unsigned int laps,
bool reverse, int position=-1);
void editTrack(unsigned int t, Track* track,
void editTrack(unsigned int index, Track* track,
unsigned int laps, bool reverse);
void remove(const unsigned int track);
// ------------------------------------------------------------------------
/** @return the (potentially translated) user-visible name of the Grand
* Prix (apply fribidi as needed) */
irr::core::stringw getName() const { return _LTR(m_name.c_str()); }
irr::core::stringw getName() const { return _LTR(m_name.c_str()); }
// ------------------------------------------------------------------------
/** @return the internal name identifier of the Grand Prix (not translated) */
const std::string& getId() const { return m_id; }
/** @return the internal indentifier of the Grand Prix (not translated) */
const std::string& getId() const { return m_id; }
// ------------------------------------------------------------------------
/** Returns the filename of the grand prix xml file. */
const std::string& getFilename() const { return m_filename; }
const std::string& getFilename() const { return m_filename; }
}; // GrandPrixData

View File

@ -19,7 +19,7 @@
#include "race/grand_prix_manager.hpp"
#include "config/user_config.hpp"
#include "grand_prix_data.hpp"
#include "race/grand_prix_data.hpp"
#include "io/file_manager.hpp"
#include "utils/string_utils.hpp"
@ -36,12 +36,13 @@ void GrandPrixManager::loadFiles()
{
std::set<std::string> dirs;
//Add all the directories to a set to avoid duplicates
// Add all the directories to a set to avoid duplicates
dirs.insert(file_manager->getAsset(FileManager::GRANDPRIX, ""));
dirs.insert(file_manager->getGPDir());
dirs.insert(UserConfigParams::m_additional_gp_directory);
for (std::set<std::string>::const_iterator it = dirs.begin(); it != dirs.end(); ++it)
for (std::set<std::string>::const_iterator it = dirs.begin();
it != dirs.end (); ++it)
{
std::string dir = *it;
if (!dir.empty() && dir[dir.size() - 1] == '/')
@ -52,35 +53,34 @@ void GrandPrixManager::loadFiles()
// ----------------------------------------------------------------------------
void GrandPrixManager::loadDir(const std::string& dir)
{
Log::info("GrandPrixManager", "Loading Grand Prix files from %s", dir.c_str());
Log::info("GrandPrixManager",
"Loading Grand Prix files from %s", dir.c_str());
assert(!dir.empty() && dir[dir.size() - 1] == '/');
// Findout which grand prixs are available and load them
// Findout which grand prix are available and load them
std::set<std::string> result;
file_manager->listFiles(result, dir);
for(std::set<std::string>::iterator i = result.begin(); i != result.end(); i++)
{
for(std::set<std::string>::iterator i = result.begin();
i != result.end(); i++)
if (StringUtils::hasSuffix(*i, SUFFIX))
load(dir + *i);
} // for i
} // loadDir
}
// ----------------------------------------------------------------------------
void GrandPrixManager::load(const std::string& filename)
{
GrandPrixData* gp;
try
{
gp = new GrandPrixData(filename);
GrandPrixData* gp = new GrandPrixData(filename);
m_gp_data.push_back(gp);
Log::debug("GrandPrixManager", "Grand Prix '%s' loaded from %s",
gp->getId().c_str(), filename.c_str());
Log::debug("GrandPrixManager",
"Grand Prix '%s' loaded from %s",
gp->getId().c_str(), filename.c_str());
}
catch (std::logic_error& er)
catch (std::logic_error& e)
{
Log::error("GrandPrixManager", "Ignoring GP %s (%s)\n",
filename.c_str(), er.what());
Log::error("GrandPrixManager",
"Ignoring grand prix %s (%s)\n", filename.c_str(), e.what());
}
} // load
@ -99,66 +99,63 @@ std::string GrandPrixManager::generateId()
{
std::stringstream s;
do
bool unique = false;
while(!unique)
{
s.clear();
s << "usr_gp_" << ((rand() % 90000000) + 10000000);
} while (existsId(s.str()));
// Check if the id already exists
unique = true;
for (unsigned int i = 0; i < m_gp_data.size(); i++)
{
if (m_gp_data[i]->getId() == s.str())
{
unique = false;
break;
}
}
}
return s.str();
}
// ----------------------------------------------------------------------------
bool GrandPrixManager::existsId(const std::string& id) const
{
bool exists;
exists = false;
for (unsigned int i = 0; !exists && i < m_gp_data.size(); i++)
exists = (m_gp_data[i]->getId() == id);
return exists;
}
// ----------------------------------------------------------------------------
bool GrandPrixManager::existsName(const irr::core::stringw& name) const
{
bool exists;
for (unsigned int i = 0; i < m_gp_data.size(); i++)
if (m_gp_data[i]->getName() == name)
return true;
exists = false;
for (unsigned int i = 0; !exists && i < m_gp_data.size(); i++)
exists = (m_gp_data[i]->getName() == name);
return exists;
return false;
}
// ----------------------------------------------------------------------------
GrandPrixManager::GrandPrixManager()
{
loadFiles();
} // GrandPrixManager
}
// ----------------------------------------------------------------------------
GrandPrixManager::~GrandPrixManager()
{
for(unsigned int i=0; i<m_gp_data.size(); i++)
{
delete m_gp_data[i];
} // for i
} // ~GrandPrixManager
}
// ----------------------------------------------------------------------------
const GrandPrixData* GrandPrixManager::getGrandPrix(const std::string& s) const
GrandPrixData* GrandPrixManager::getGrandPrix(const std::string& s) const
{
return editGrandPrix(s);
} // getGrandPrix
}
// ----------------------------------------------------------------------------
GrandPrixData* GrandPrixManager::editGrandPrix(const std::string& s) const
{
for(unsigned int i=0; i<m_gp_data.size(); i++)
if(m_gp_data[i]->getId()==s) return m_gp_data[i];
if(m_gp_data[i]->getId() == s)
return m_gp_data[i];
return NULL;
}
@ -177,7 +174,7 @@ void GrandPrixManager::checkConsistency()
} // checkConsistency
// ----------------------------------------------------------------------------
GrandPrixData* GrandPrixManager::createNew(const irr::core::stringw& newName)
GrandPrixData* GrandPrixManager::createNewGP(const irr::core::stringw& newName)
{
if (existsName(newName))
return NULL;
@ -197,7 +194,7 @@ GrandPrixData* GrandPrixManager::createNew(const irr::core::stringw& newName)
// ----------------------------------------------------------------------------
GrandPrixData* GrandPrixManager::copy(const std::string& id,
const irr::core::stringw& newName)
const irr::core::stringw& newName)
{
if (existsName(newName))
return NULL;
@ -228,6 +225,7 @@ void GrandPrixManager::remove(const std::string& id)
}
else
{
Log::warn("GrandPrixManager", "Grand Prix '%s' cannot be removed\n", gp->getId().c_str());
Log::warn("GrandPrixManager",
"Grand Prix '%s' can not be removed", gp->getId().c_str());
}
}

View File

@ -32,30 +32,37 @@ class GrandPrixManager
private:
static const char* SUFFIX;
std::vector<GrandPrixData*> m_gp_data;
/** Load all the grands prix from the 3 directories known */
void loadFiles();
/** Load all the grands prix in one directory */
void loadDir(const std::string& dir);
/** Load a grand prix and add it to m_gp_data*/
void load(const std::string &filename);
/** Generates a new unique indentifier for a user defined grand prix */
std::string generateId();
bool existsId(const std::string& id) const;
bool existsName(const irr::core::stringw& name) const;
std::vector<GrandPrixData*> m_gp_data;
public:
GrandPrixManager();
~GrandPrixManager();
void reload();
const GrandPrixData* getGrandPrix(const int i) const { return m_gp_data[i]; }
const GrandPrixData* getGrandPrix(const std::string& s) const;
GrandPrixData* editGrandPrix(const std::string& s) const;
unsigned int getNumberOfGrandPrix() const { return m_gp_data.size(); }
void checkConsistency();
GrandPrixManager();
~GrandPrixManager();
void reload();
GrandPrixData* getGrandPrix(const int i) const { return m_gp_data[i]; }
GrandPrixData* getGrandPrix(const std::string& s) const;
unsigned int getNumberOfGrandPrix() const { return m_gp_data.size(); }
void checkConsistency();
GrandPrixData* createNew(const irr::core::stringw& newName);
GrandPrixData* copy(const std::string& id, const irr::core::stringw& newName);
void remove(const std::string& id);
// Methods for the gp editor
GrandPrixData* editGrandPrix(const std::string& s) const;
GrandPrixData* createNewGP(const irr::core::stringw& newName);
GrandPrixData* copy(const std::string& id,
const irr::core::stringw& newName);
void remove(const std::string& id);
}; // GrandPrixManager
extern GrandPrixManager *grand_prix_manager;
#endif

View File

@ -264,7 +264,7 @@ void GrandPrixEditorScreen::onNewGPWithName(const stringw& newName)
}
else if (m_action == "new")
{
setSelection(grand_prix_manager->createNew(newName));
setSelection(grand_prix_manager->createNewGP(newName));
}
loadGPList();