Merge pull request #1226 from konstin/master
Solving Issue 1180 - Load addon GPs from local folder
This commit is contained in:
commit
780201b1ad
@ -396,6 +396,9 @@ namespace UserConfigParams
|
||||
PARAM_DEFAULT( BoolUserConfigParam(false, "player_last",
|
||||
&m_gp_start_order,
|
||||
"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
|
||||
PARAM_PREFIX GroupUserConfigParam m_video_group
|
||||
|
16
src/main.cpp
16
src/main.cpp
@ -401,6 +401,9 @@ void cmdLineHelp()
|
||||
" and the music.\n"
|
||||
" -t, --track=NAME Start at track NAME.\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 "
|
||||
"./data/stk_config.xml\n"
|
||||
" -k, --numkarts=NUM Number of karts on the racetrack.\n"
|
||||
@ -583,6 +586,19 @@ int handleCmdLinePreliminary()
|
||||
Log::info("main", "==============================");
|
||||
} // --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;
|
||||
if(CommandLine::has("--xmas", &n))
|
||||
UserConfigParams::m_xmas_mode = n;
|
||||
|
@ -32,15 +32,29 @@
|
||||
#include <stdexcept>
|
||||
|
||||
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_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)
|
||||
{
|
||||
Log::error("GrandPrixData","Error while trying to read grandprix file '%s'",
|
||||
filename.c_str());
|
||||
Log::error("GrandPrixData","Error while trying to read grandprix file "
|
||||
"'%s'", fullpath.c_str());
|
||||
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;
|
||||
if (root->get("name", &temp_name) == 0)
|
||||
{
|
||||
Log::error("GrandPrixData", "Error while trying to read grandprix file '%s' : "
|
||||
"missing 'name' attribute\n", filename.c_str());
|
||||
Log::error("GrandPrixData", "Error while trying to read grandprix "
|
||||
"file '%s' : missing 'name' attribute\n",
|
||||
fullpath.c_str());
|
||||
delete root;
|
||||
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
|
||||
{
|
||||
Log::error("GrandPrixData", "Error while trying to read grandprix file '%s' : "
|
||||
"Root node has an unexpected name\n", filename.c_str());
|
||||
Log::error("GrandPrixData", "Error while trying to read grandprix file "
|
||||
"'%s' : Root node has an unexpected name\n",
|
||||
fullpath.c_str());
|
||||
delete root;
|
||||
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;
|
||||
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",
|
||||
filename.c_str());
|
||||
Log::error("GrandPrixData", "Error while trying to read "
|
||||
"grandprix file '%s' : <track> tag does not have "
|
||||
"idi and laps reverse attributes. \n",
|
||||
fullpath.c_str());
|
||||
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
|
||||
@ -110,22 +128,22 @@ GrandPrixData::GrandPrixData(const std::string filename) throw(std::logic_error)
|
||||
}
|
||||
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;
|
||||
throw std::runtime_error("Unknown node in sfx XML file");
|
||||
}
|
||||
}// nend for
|
||||
}// end for
|
||||
|
||||
delete root;
|
||||
|
||||
// sanity checks
|
||||
if (!foundName)
|
||||
{
|
||||
Log::error("GrandPrixData", "Error while trying to read grandprix file '%s' : "
|
||||
"missing 'name' attribute\n", filename.c_str());
|
||||
Log::error("GrandPrixData", "Error while trying to read grandprix file "
|
||||
"'%s' : missing 'name' attribute\n", fullpath.c_str());
|
||||
throw std::logic_error("File contents are incomplete or corrupt");
|
||||
}
|
||||
|
||||
}
|
||||
// ----------------------------------------------------------------------------
|
||||
bool GrandPrixData::checkConsistency(bool chatty) const
|
||||
@ -149,7 +167,6 @@ bool GrandPrixData::checkConsistency(bool chatty) const
|
||||
return true;
|
||||
} // checkConsistency
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
/** 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
|
||||
|
@ -27,6 +27,7 @@
|
||||
#include <stdexcept>
|
||||
|
||||
#include "utils/translation.hpp"
|
||||
#include "io/file_manager.hpp"
|
||||
|
||||
/** 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
|
||||
@ -34,6 +35,7 @@
|
||||
*/
|
||||
class GrandPrixData
|
||||
{
|
||||
private:
|
||||
/** The name of the grand prix. */
|
||||
irr::core::stringw m_name;
|
||||
|
||||
@ -63,6 +65,7 @@ class GrandPrixData
|
||||
/** Whether the track in question should be done in reverse mode */
|
||||
std::vector<bool> m_reversed;
|
||||
|
||||
void load_from_file(const std::string fullpath, const std::string filename);
|
||||
bool isTrackAvailable(const std::string &id) const;
|
||||
public:
|
||||
|
||||
@ -70,8 +73,11 @@ public:
|
||||
#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) throw(std::logic_error);
|
||||
GrandPrixData (const std::string dir, const std::string filename)
|
||||
throw(std::logic_error);
|
||||
|
||||
|
||||
bool checkConsistency(bool chatty=true) const;
|
||||
const std::vector<std::string>& getTrackNames() const;
|
||||
|
@ -21,22 +21,63 @@
|
||||
#include <set>
|
||||
#include "io/file_manager.hpp"
|
||||
#include "utils/string_utils.hpp"
|
||||
#include "config/user_config.hpp"
|
||||
|
||||
GrandPrixManager *grand_prix_manager = NULL;
|
||||
|
||||
GrandPrixManager::GrandPrixManager()
|
||||
{
|
||||
// Findout which grand prixs are available and load them
|
||||
// Grand Prix in the standart directory
|
||||
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);
|
||||
for(std::set<std::string>::iterator i = result.begin();
|
||||
i != result.end() ; i++)
|
||||
{
|
||||
if (StringUtils::hasSuffix(*i, ".grandprix")) load(*i);
|
||||
} // for i
|
||||
} // GrandPrixManager
|
||||
if (StringUtils::hasSuffix(*i, ".grandprix"))
|
||||
{
|
||||
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()
|
||||
{
|
||||
@ -50,22 +91,11 @@ GrandPrixManager::~GrandPrixManager()
|
||||
const GrandPrixData* GrandPrixManager::getGrandPrix(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;
|
||||
} // 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()
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user