Merge pull request #1226 from konstin/master

Solving Issue 1180 - Load addon GPs from local folder
This commit is contained in:
auriamg 2014-03-06 19:12:29 -05:00
commit 780201b1ad
5 changed files with 111 additions and 39 deletions

View File

@ -396,6 +396,9 @@ namespace UserConfigParams
PARAM_DEFAULT( BoolUserConfigParam(false, "player_last", PARAM_DEFAULT( BoolUserConfigParam(false, "player_last",
&m_gp_start_order, &m_gp_start_order,
"Always put the player at the back or not (Bully mode).") ); "Always put the player at the back or not (Bully mode).") );
PARAM_PREFIX StringUserConfigParam m_additional_gp_directory
PARAM_DEFAULT( StringUserConfigParam("", "additional_gp_directory",
"Directory with additional GP's."));
// ---- Video // ---- Video
PARAM_PREFIX GroupUserConfigParam m_video_group PARAM_PREFIX GroupUserConfigParam m_video_group

View File

@ -401,6 +401,9 @@ void cmdLineHelp()
" and the music.\n" " and the music.\n"
" -t, --track=NAME Start at track NAME.\n" " -t, --track=NAME Start at track NAME.\n"
" --gp=NAME Start the specified Grand Prix.\n" " --gp=NAME Start the specified Grand Prix.\n"
" --add-gp-dir=DIR Load Grand Prix in DIR. Setting will be saved "
"inconfig.xml under additional_gp_directory. Use "
"--add-gp-dir=\"\" to unset.\n"
" --stk-config=FILE use ./data/FILE instead of " " --stk-config=FILE use ./data/FILE instead of "
"./data/stk_config.xml\n" "./data/stk_config.xml\n"
" -k, --numkarts=NUM Number of karts on the racetrack.\n" " -k, --numkarts=NUM Number of karts on the racetrack.\n"
@ -583,6 +586,19 @@ int handleCmdLinePreliminary()
Log::info("main", "=============================="); Log::info("main", "==============================");
} // --verbose or -v } // --verbose or -v
// Enable loading GP's from local directory
if(CommandLine::has("--add-gp-dir", &s))
{
// Ensure that the path ends with a /
if (s[s.size()] == '/')
UserConfigParams::m_additional_gp_directory = s;
else
UserConfigParams::m_additional_gp_directory = s + "/";
Log::info("main", "Additional Grand Prix's will be loaded from %s",
UserConfigParams::m_additional_gp_directory.c_str());
}
int n; int n;
if(CommandLine::has("--xmas", &n)) if(CommandLine::has("--xmas", &n))
UserConfigParams::m_xmas_mode = n; UserConfigParams::m_xmas_mode = n;

View File

@ -32,15 +32,29 @@
#include <stdexcept> #include <stdexcept>
GrandPrixData::GrandPrixData(const std::string filename) throw(std::logic_error) GrandPrixData::GrandPrixData(const std::string filename) throw(std::logic_error)
{
load_from_file(file_manager->getAsset(FileManager::GRANDPRIX, filename),
filename);
}
// ----------------------------------------------------------------------------
GrandPrixData::GrandPrixData(const std::string dir, const std::string filename)
throw(std::logic_error)
{
assert(dir[dir.size()] == '/');
load_from_file(dir + filename, filename);
}
// ----------------------------------------------------------------------------
void GrandPrixData::load_from_file(const std::string fullpath,
const std::string filename)
{ {
m_filename = filename; m_filename = filename;
m_id = StringUtils::getBasename(StringUtils::removeExtension(filename)); m_id = StringUtils::getBasename(StringUtils::removeExtension(filename));
XMLNode* root = file_manager->createXMLTree(file_manager->getAsset(FileManager::GRANDPRIX,filename)); XMLNode * root = file_manager->createXMLTree(fullpath);
if (!root) if (!root)
{ {
Log::error("GrandPrixData","Error while trying to read grandprix file '%s'", Log::error("GrandPrixData","Error while trying to read grandprix file "
filename.c_str()); "'%s'", fullpath.c_str());
throw std::logic_error("File not found"); throw std::logic_error("File not found");
} }
@ -51,8 +65,9 @@ GrandPrixData::GrandPrixData(const std::string filename) throw(std::logic_error)
std::string temp_name; std::string temp_name;
if (root->get("name", &temp_name) == 0) if (root->get("name", &temp_name) == 0)
{ {
Log::error("GrandPrixData", "Error while trying to read grandprix file '%s' : " Log::error("GrandPrixData", "Error while trying to read grandprix "
"missing 'name' attribute\n", filename.c_str()); "file '%s' : missing 'name' attribute\n",
fullpath.c_str());
delete root; delete root;
throw std::logic_error("File contents are incomplete or corrupt"); throw std::logic_error("File contents are incomplete or corrupt");
} }
@ -61,8 +76,9 @@ GrandPrixData::GrandPrixData(const std::string filename) throw(std::logic_error)
} }
else else
{ {
Log::error("GrandPrixData", "Error while trying to read grandprix file '%s' : " Log::error("GrandPrixData", "Error while trying to read grandprix file "
"Root node has an unexpected name\n", filename.c_str()); "'%s' : Root node has an unexpected name\n",
fullpath.c_str());
delete root; delete root;
throw std::logic_error("File contents are incomplete or corrupt"); throw std::logic_error("File contents are incomplete or corrupt");
} }
@ -80,18 +96,20 @@ GrandPrixData::GrandPrixData(const std::string filename) throw(std::logic_error)
int numLaps; int numLaps;
bool reversed = false; bool reversed = false;
const int idFound = node->get("id", &trackID ); const int idFound = node->get("id", &trackID);
const int lapFound = node->get("laps", &numLaps ); const int lapFound = node->get("laps", &numLaps);
// Will stay false if not found // Will stay false if not found
node->get("reverse", &reversed ); node->get("reverse", &reversed );
if (!idFound || !lapFound) if (!idFound || !lapFound)
{ {
Log::error("GrandPrixData", "Error while trying to read grandprix file '%s' : " Log::error("GrandPrixData", "Error while trying to read "
"<track> tag does not have idi and laps reverse attributes. \n", "grandprix file '%s' : <track> tag does not have "
filename.c_str()); "idi and laps reverse attributes. \n",
fullpath.c_str());
delete root; delete root;
throw std::logic_error("File contents are incomplete or corrupt"); throw std::logic_error("File contents are incomplete or "
"corrupt");
} }
// Make sure the track really is reversible // Make sure the track really is reversible
@ -110,22 +128,22 @@ GrandPrixData::GrandPrixData(const std::string filename) throw(std::logic_error)
} }
else else
{ {
std::cerr << "Unknown node in Grand Prix XML file : " << node->getName().c_str() << std::endl; Log::error("Unknown node in Grand Prix XML file: %s/n",
node->getName().c_str());
delete root; delete root;
throw std::runtime_error("Unknown node in sfx XML file"); throw std::runtime_error("Unknown node in sfx XML file");
} }
}// nend for }// end for
delete root; delete root;
// sanity checks // sanity checks
if (!foundName) if (!foundName)
{ {
Log::error("GrandPrixData", "Error while trying to read grandprix file '%s' : " Log::error("GrandPrixData", "Error while trying to read grandprix file "
"missing 'name' attribute\n", filename.c_str()); "'%s' : missing 'name' attribute\n", fullpath.c_str());
throw std::logic_error("File contents are incomplete or corrupt"); throw std::logic_error("File contents are incomplete or corrupt");
} }
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
bool GrandPrixData::checkConsistency(bool chatty) const bool GrandPrixData::checkConsistency(bool chatty) const
@ -149,7 +167,6 @@ bool GrandPrixData::checkConsistency(bool chatty) const
return true; return true;
} // checkConsistency } // checkConsistency
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/** Returns true if the track is available. This is used to test if Fort Magma /** Returns true if the track is available. This is used to test if Fort Magma
* is available (this way FortMagma is not used in the last Grand Prix in * is available (this way FortMagma is not used in the last Grand Prix in

View File

@ -27,6 +27,7 @@
#include <stdexcept> #include <stdexcept>
#include "utils/translation.hpp" #include "utils/translation.hpp"
#include "io/file_manager.hpp"
/** Simple class that hold the data relevant to a 'grand_prix', aka. a number /** Simple class that hold the data relevant to a 'grand_prix', aka. a number
* of races that has to be completed one after the other * of races that has to be completed one after the other
@ -34,6 +35,7 @@
*/ */
class GrandPrixData class GrandPrixData
{ {
private:
/** The name of the grand prix. */ /** The name of the grand prix. */
irr::core::stringw m_name; irr::core::stringw m_name;
@ -63,6 +65,7 @@ class GrandPrixData
/** Whether the track in question should be done in reverse mode */ /** Whether the track in question should be done in reverse mode */
std::vector<bool> m_reversed; std::vector<bool> m_reversed;
void load_from_file(const std::string fullpath, const std::string filename);
bool isTrackAvailable(const std::string &id) const; bool isTrackAvailable(const std::string &id) const;
public: public:
@ -70,8 +73,11 @@ public:
#if (defined(WIN32) || defined(_WIN32)) && !defined(__MINGW32__) #if (defined(WIN32) || defined(_WIN32)) && !defined(__MINGW32__)
#pragma warning(disable:4290) #pragma warning(disable:4290)
#endif #endif
GrandPrixData (const std::string filename) throw(std::logic_error);
GrandPrixData () {}; // empty for initialising GrandPrixData () {}; // empty for initialising
GrandPrixData (const std::string filename) throw(std::logic_error);
GrandPrixData (const std::string dir, const std::string filename)
throw(std::logic_error);
bool checkConsistency(bool chatty=true) const; bool checkConsistency(bool chatty=true) const;
const std::vector<std::string>& getTrackNames() const; const std::vector<std::string>& getTrackNames() const;

View File

@ -21,22 +21,63 @@
#include <set> #include <set>
#include "io/file_manager.hpp" #include "io/file_manager.hpp"
#include "utils/string_utils.hpp" #include "utils/string_utils.hpp"
#include "config/user_config.hpp"
GrandPrixManager *grand_prix_manager = NULL; GrandPrixManager *grand_prix_manager = NULL;
GrandPrixManager::GrandPrixManager() GrandPrixManager::GrandPrixManager()
{ {
// Findout which grand prixs are available and load them // Findout which grand prixs are available and load them
// Grand Prix in the standart directory
std::set<std::string> result; std::set<std::string> result;
std::string gp_dir = file_manager->getAsset(FileManager::GRANDPRIX,""); std::string gp_dir = file_manager->getAsset(FileManager::GRANDPRIX, "");
file_manager->listFiles(result, gp_dir); file_manager->listFiles(result, gp_dir);
for(std::set<std::string>::iterator i = result.begin(); for(std::set<std::string>::iterator i = result.begin();
i != result.end() ; i++) i != result.end() ; i++)
{ {
if (StringUtils::hasSuffix(*i, ".grandprix")) load(*i); if (StringUtils::hasSuffix(*i, ".grandprix"))
} // for i {
} // GrandPrixManager try
{
m_gp_data.push_back(new GrandPrixData(*i));
Log::debug("GrandPrixManager", "Grand Prix %s loaded.",
i->c_str());
}
catch (std::logic_error& e)
{
Log::error("GrandPrixManager", "Ignoring GP %s ( %s ) \n",
i->c_str(), e.what());
}
}
}
// Load additional Grand Prix
const std::string dir = UserConfigParams::m_additional_gp_directory;
if(dir != "") {
Log::info("GrandPrixManager", "Loading additional Grand Prix from "
"%s ...", dir.c_str());
file_manager->listFiles(result, dir);
for(std::set<std::string>::iterator i = result.begin();
i != result.end() ; i++)
{
if (StringUtils::hasSuffix(*i, ".grandprix"))
{
try
{
m_gp_data.push_back(new GrandPrixData(dir, *i));
Log::debug("GrandPrixManager", "Grand Prix %s loaded from "
"%s", i->c_str(),
dir.c_str());
}
catch (std::logic_error& e)
{
Log::error("GrandPrixManager", "Ignoring GP %s ( %s ) \n",
i->c_str(), e.what());
}
}
} // end for
} // end if
} // GrandPrixManager
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
GrandPrixManager::~GrandPrixManager() GrandPrixManager::~GrandPrixManager()
{ {
@ -50,22 +91,11 @@ GrandPrixManager::~GrandPrixManager()
const GrandPrixData* GrandPrixManager::getGrandPrix(const std::string& s) const const GrandPrixData* GrandPrixManager::getGrandPrix(const std::string& s) const
{ {
for(unsigned int i=0; i<m_gp_data.size(); i++) 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; return NULL;
} // getGrandPrix } // getGrandPrix
// ----------------------------------------------------------------------------
void GrandPrixManager::load(const std::string& filename)
{
try
{
m_gp_data.push_back(new GrandPrixData(filename));
}
catch (std::logic_error& er)
{
Log::error("GrandPrixManager", "Ignoring GP %s ( %s ) \n", filename.c_str(), er.what());
}
} // load
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void GrandPrixManager::checkConsistency() void GrandPrixManager::checkConsistency()
{ {