2012-08-10 16:07:53 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CModHandler.h"
|
2012-09-20 21:41:16 +03:00
|
|
|
#include "CDefObjInfoHandler.h"
|
2012-08-11 12:06:23 +03:00
|
|
|
#include "JsonNode.h"
|
2012-09-20 19:14:23 +03:00
|
|
|
#include "Filesystem/CResourceLoader.h"
|
|
|
|
#include "Filesystem/ISimpleResourceLoader.h"
|
2012-12-03 19:00:17 +03:00
|
|
|
|
2012-12-15 11:47:02 +03:00
|
|
|
#include "CCreatureHandler.h"
|
|
|
|
#include "CArtHandler.h"
|
|
|
|
#include "CTownHandler.h"
|
|
|
|
#include "CHeroHandler.h"
|
2012-12-19 19:35:58 +03:00
|
|
|
#include "CObjectHandler.h"
|
2012-12-26 12:46:09 +03:00
|
|
|
#include "StringConstants.h"
|
2012-12-15 11:47:02 +03:00
|
|
|
|
2012-08-10 16:07:53 +03:00
|
|
|
/*
|
|
|
|
* CModHandler.h, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-12-22 19:47:12 +03:00
|
|
|
void CIdentifierStorage::checkIdentifier(std::string & ID)
|
|
|
|
{
|
|
|
|
if (boost::algorithm::ends_with(ID, "."))
|
|
|
|
tlog0 << "BIG WARNING: identifier " << ID << " seems to be broken!\n";
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (std::tolower(ID[pos]) != ID[pos] ) //Not in camelCase
|
|
|
|
{
|
|
|
|
tlog0 << "Warning: identifier " << ID << " is not in camelCase!\n";
|
|
|
|
ID[pos] = std::tolower(ID[pos]);// Try to fix the ID
|
|
|
|
}
|
|
|
|
pos = ID.find('.', pos);
|
|
|
|
}
|
|
|
|
while(pos++ != std::string::npos);
|
|
|
|
}
|
|
|
|
}
|
2012-08-10 16:07:53 +03:00
|
|
|
|
2012-12-03 19:00:17 +03:00
|
|
|
void CIdentifierStorage::requestIdentifier(std::string name, const boost::function<void(si32)> & callback)
|
|
|
|
{
|
2012-12-22 19:47:12 +03:00
|
|
|
checkIdentifier(name);
|
|
|
|
|
2012-12-03 19:00:17 +03:00
|
|
|
auto iter = registeredObjects.find(name);
|
|
|
|
|
|
|
|
if (iter != registeredObjects.end())
|
|
|
|
callback(iter->second); //already registered - trigger callback immediately
|
|
|
|
else
|
|
|
|
missingObjects[name].push_back(callback); // queue callback
|
|
|
|
}
|
|
|
|
|
|
|
|
void CIdentifierStorage::registerObject(std::string name, si32 identifier)
|
|
|
|
{
|
2012-12-22 19:47:12 +03:00
|
|
|
checkIdentifier(name);
|
|
|
|
|
2012-12-03 19:00:17 +03:00
|
|
|
// do not allow to register same object twice
|
|
|
|
assert(registeredObjects.find(name) == registeredObjects.end());
|
|
|
|
|
|
|
|
registeredObjects[name] = identifier;
|
|
|
|
|
|
|
|
auto iter = missingObjects.find(name);
|
|
|
|
if (iter != missingObjects.end())
|
|
|
|
{
|
|
|
|
//call all awaiting callbacks
|
|
|
|
BOOST_FOREACH(auto function, iter->second)
|
|
|
|
{
|
|
|
|
function(identifier);
|
|
|
|
}
|
|
|
|
missingObjects.erase(iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CIdentifierStorage::finalize() const
|
|
|
|
{
|
|
|
|
// print list of missing objects and crash
|
|
|
|
// in future should try to do some cleanup (like returning all id's as 0)
|
|
|
|
BOOST_FOREACH(auto object, missingObjects)
|
|
|
|
{
|
|
|
|
tlog1 << "Error: object " << object.first << " was not found!\n";
|
|
|
|
}
|
|
|
|
assert(missingObjects.empty());
|
|
|
|
}
|
|
|
|
|
2012-08-10 16:07:53 +03:00
|
|
|
CModHandler::CModHandler()
|
|
|
|
{
|
2012-12-26 12:46:09 +03:00
|
|
|
for (int i = 0; i < GameConstants::RESOURCE_QUANTITY; ++i)
|
|
|
|
{
|
|
|
|
identifiers.registerObject("resource." + GameConstants::RESOURCE_NAMES[i], i);
|
|
|
|
}
|
|
|
|
|
2012-08-11 12:06:23 +03:00
|
|
|
loadConfigFromFile ("defaultMods");
|
2012-08-10 16:07:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CModHandler::loadConfigFromFile (std::string name)
|
2012-08-11 12:06:23 +03:00
|
|
|
{
|
|
|
|
const JsonNode config(ResourceID("config/" + name + ".json"));
|
2012-09-21 00:28:18 +03:00
|
|
|
const JsonNode & hardcodedFeatures = config["hardcodedFeatures"];
|
2012-08-11 12:06:23 +03:00
|
|
|
|
|
|
|
settings.CREEP_SIZE = hardcodedFeatures["CREEP_SIZE"].Float();
|
|
|
|
settings.WEEKLY_GROWTH = hardcodedFeatures["WEEKLY_GROWTH_PERCENT"].Float();
|
|
|
|
settings.NEUTRAL_STACK_EXP = hardcodedFeatures["NEUTRAL_STACK_EXP_DAILY"].Float();
|
2012-09-05 15:49:23 +03:00
|
|
|
settings.MAX_BUILDING_PER_TURN = hardcodedFeatures["MAX_BUILDING_PER_TURN"].Float();
|
2012-08-11 12:06:23 +03:00
|
|
|
settings.DWELLINGS_ACCUMULATE_CREATURES = hardcodedFeatures["DWELLINGS_ACCUMULATE_CREATURES"].Bool();
|
|
|
|
settings.ALL_CREATURES_GET_DOUBLE_MONTHS = hardcodedFeatures["ALL_CREATURES_GET_DOUBLE_MONTHS"].Bool();
|
2012-08-24 12:37:52 +03:00
|
|
|
|
2012-09-21 00:28:18 +03:00
|
|
|
const JsonNode & gameModules = config["modules"];
|
2012-08-24 12:37:52 +03:00
|
|
|
modules.STACK_EXP = gameModules["STACK_EXPERIENCE"].Bool();
|
|
|
|
modules.STACK_ARTIFACT = gameModules["STACK_ARTIFACTS"].Bool();
|
|
|
|
modules.COMMANDERS = gameModules["COMMANDERS"].Bool();
|
|
|
|
modules.MITHRIL = gameModules["MITHRIL"].Bool();
|
2012-08-11 12:06:23 +03:00
|
|
|
}
|
2012-09-21 00:28:18 +03:00
|
|
|
|
2013-01-10 21:53:49 +03:00
|
|
|
// currentList is passed by value to get current list of depending mods
|
|
|
|
bool CModHandler::hasCircularDependency(TModID modID, std::set <TModID> currentList) const
|
|
|
|
{
|
|
|
|
const CModInfo & mod = allMods.at(modID);
|
|
|
|
|
|
|
|
// Mod already present? We found a loop
|
|
|
|
if (vstd::contains(currentList, modID))
|
|
|
|
{
|
|
|
|
tlog0 << "Error: Circular dependency detected! Printing dependency list:\n";
|
|
|
|
tlog0 << "\t" << mod.name << " -> \n";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentList.insert(modID);
|
|
|
|
|
|
|
|
// recursively check every dependency of this mod
|
|
|
|
BOOST_FOREACH(const TModID & dependency, mod.dependencies)
|
|
|
|
{
|
|
|
|
if (hasCircularDependency(dependency, currentList))
|
|
|
|
{
|
|
|
|
tlog0 << "\t" << mod.name << " ->\n"; // conflict detected, print dependency list
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CModHandler::checkDependencies(const std::vector <TModID> & input) const
|
|
|
|
{
|
|
|
|
BOOST_FOREACH(const TModID & id, input)
|
|
|
|
{
|
|
|
|
const CModInfo & mod = allMods.at(id);
|
|
|
|
|
|
|
|
BOOST_FOREACH(const TModID & dep, mod.dependencies)
|
|
|
|
{
|
|
|
|
if (!vstd::contains(input, dep))
|
|
|
|
{
|
|
|
|
tlog0 << "Error: Mod " << mod.name << " requires missing " << dep << "!\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_FOREACH(const TModID & conflicting, mod.conflicts)
|
|
|
|
{
|
|
|
|
if (vstd::contains(input, conflicting))
|
|
|
|
{
|
|
|
|
tlog0 << "Error: Mod " << mod.name << " conflicts with " << allMods.at(conflicting).name << "!\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasCircularDependency(id))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector <TModID> CModHandler::resolveDependencies(std::vector <TModID> input) const
|
|
|
|
{
|
|
|
|
// Algorithm may not be the fastest one but VCMI does not needs any speed here
|
|
|
|
// Unless user have dozens of mods with complex dependencies this cide should be fine
|
|
|
|
|
|
|
|
std::vector <TModID> output;
|
|
|
|
output.reserve(input.size());
|
|
|
|
|
|
|
|
std::set <TModID> resolvedMods;
|
|
|
|
|
|
|
|
// Check if all mod dependencies are resolved (moved to resolvedMods)
|
|
|
|
auto isResolved = [&](const CModInfo mod) -> bool
|
|
|
|
{
|
|
|
|
BOOST_FOREACH(const TModID & dependency, mod.dependencies)
|
|
|
|
{
|
|
|
|
if (!vstd::contains(resolvedMods, dependency))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
while (!input.empty())
|
|
|
|
{
|
|
|
|
for (auto it = input.begin(); it != input.end();)
|
|
|
|
{
|
|
|
|
if (isResolved(allMods.at(*it)))
|
|
|
|
{
|
|
|
|
resolvedMods.insert(*it);
|
|
|
|
output.push_back(*it);
|
|
|
|
it = input.erase(it);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2012-12-12 14:13:57 +03:00
|
|
|
void CModHandler::initialize(std::vector<std::string> availableMods)
|
2012-08-11 12:06:23 +03:00
|
|
|
{
|
2013-01-10 21:53:49 +03:00
|
|
|
JsonNode modConfig(ResourceID("config/modSettings.json"));
|
|
|
|
const JsonNode & modList = modConfig["activeMods"];
|
|
|
|
JsonNode resultingList;
|
|
|
|
|
|
|
|
std::vector <TModID> detectedMods;
|
|
|
|
|
|
|
|
BOOST_FOREACH(std::string name, availableMods)
|
2012-12-12 14:13:57 +03:00
|
|
|
{
|
2013-01-10 21:53:49 +03:00
|
|
|
boost::to_lower(name);
|
2012-12-12 14:13:57 +03:00
|
|
|
std::string modFileName = "mods/" + name + "/mod.json";
|
2012-08-11 12:06:23 +03:00
|
|
|
|
2012-12-12 14:13:57 +03:00
|
|
|
if (CResourceHandler::get()->existsResource(ResourceID(modFileName)))
|
|
|
|
{
|
|
|
|
const JsonNode config = JsonNode(ResourceID(modFileName));
|
2012-08-11 12:06:23 +03:00
|
|
|
|
2013-01-10 21:53:49 +03:00
|
|
|
if (config.isNull())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!modList[name].isNull() && modList[name].Bool() == false )
|
2012-12-12 14:13:57 +03:00
|
|
|
{
|
2013-01-10 21:53:49 +03:00
|
|
|
resultingList[name].Bool() = false;
|
|
|
|
continue; // disabled mod
|
2012-12-12 14:13:57 +03:00
|
|
|
}
|
2013-01-10 21:53:49 +03:00
|
|
|
resultingList[name].Bool() = true;
|
|
|
|
|
|
|
|
CModInfo & mod = allMods[name];
|
|
|
|
|
|
|
|
mod.identifier = name;
|
|
|
|
mod.name = config["name"].String();
|
|
|
|
mod.description = config["description"].String();
|
|
|
|
mod.dependencies = config["depends"].convertTo<std::set<std::string> >();
|
|
|
|
mod.conflicts = config["conflicts"].convertTo<std::set<std::string> >();
|
|
|
|
detectedMods.push_back(name);
|
2012-12-12 14:13:57 +03:00
|
|
|
}
|
|
|
|
else
|
2013-01-17 21:15:00 +03:00
|
|
|
tlog3 << "\t\t Directory " << name << " does not contains VCMI mod\n";
|
2012-12-12 14:13:57 +03:00
|
|
|
}
|
2012-08-11 12:06:23 +03:00
|
|
|
|
2013-01-10 21:53:49 +03:00
|
|
|
if (!checkDependencies(detectedMods))
|
2012-12-12 14:13:57 +03:00
|
|
|
{
|
2013-01-10 21:53:49 +03:00
|
|
|
tlog0 << "Critical error: failed to load mods! Exiting...\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
activeMods = resolveDependencies(detectedMods);
|
|
|
|
|
|
|
|
modConfig["activeMods"] = resultingList;
|
|
|
|
CResourceHandler::get()->createResource("CONFIG/modSettings.json");
|
|
|
|
|
|
|
|
std::ofstream file(CResourceHandler::get()->getResourceName(ResourceID("config/modSettings.json")), std::ofstream::trunc);
|
|
|
|
file << modConfig;
|
2012-08-11 12:06:23 +03:00
|
|
|
}
|
2012-08-30 17:57:24 +03:00
|
|
|
|
|
|
|
|
2012-12-12 14:13:57 +03:00
|
|
|
std::vector<std::string> CModHandler::getActiveMods()
|
2012-11-13 14:52:23 +03:00
|
|
|
{
|
2012-12-12 14:13:57 +03:00
|
|
|
return activeMods;
|
2012-11-13 14:52:23 +03:00
|
|
|
}
|
2012-09-20 21:41:16 +03:00
|
|
|
|
2012-12-15 11:47:02 +03:00
|
|
|
template<typename Handler>
|
|
|
|
void handleData(Handler handler, const JsonNode & config)
|
|
|
|
{
|
|
|
|
handler->load(JsonUtils::assembleFromFiles(config.convertTo<std::vector<std::string> >()));
|
|
|
|
}
|
|
|
|
|
2012-11-13 14:52:23 +03:00
|
|
|
void CModHandler::loadActiveMods()
|
|
|
|
{
|
2013-01-10 21:53:49 +03:00
|
|
|
BOOST_FOREACH(const TModID & modName, activeMods)
|
2012-09-19 19:10:45 +03:00
|
|
|
{
|
2013-01-10 21:53:49 +03:00
|
|
|
tlog1 << "\t\tLoading mod ";
|
|
|
|
tlog2 << allMods[modName].name << "\n";
|
|
|
|
|
2012-12-12 14:13:57 +03:00
|
|
|
std::string modFileName = "mods/" + modName + "/mod.json";
|
|
|
|
|
|
|
|
const JsonNode config = JsonNode(ResourceID(modFileName));
|
2012-09-19 19:10:45 +03:00
|
|
|
|
2012-12-15 11:47:02 +03:00
|
|
|
handleData(VLC->townh, config["factions"]);
|
|
|
|
handleData(VLC->creh, config["creatures"]);
|
|
|
|
handleData(VLC->arth, config["artifacts"]);
|
2013-01-15 17:20:48 +03:00
|
|
|
//todo: spells
|
2012-12-15 11:47:02 +03:00
|
|
|
|
|
|
|
handleData(&VLC->heroh->classes, config["heroClasses"]);
|
|
|
|
handleData(VLC->heroh, config["heroes"]);
|
2012-11-13 14:52:23 +03:00
|
|
|
}
|
2012-12-12 14:13:57 +03:00
|
|
|
|
2012-11-13 14:52:23 +03:00
|
|
|
VLC->creh->buildBonusTreeForTiers(); //do that after all new creatures are loaded
|
2012-12-03 19:00:17 +03:00
|
|
|
identifiers.finalize();
|
2012-11-13 14:52:23 +03:00
|
|
|
}
|
2012-08-30 17:57:24 +03:00
|
|
|
|
2012-11-13 14:52:23 +03:00
|
|
|
void CModHandler::reload()
|
|
|
|
{
|
|
|
|
{
|
2012-12-16 14:35:14 +03:00
|
|
|
//recreate adventure map defs
|
2012-11-13 14:52:23 +03:00
|
|
|
assert(!VLC->dobjinfo->gobjs[Obj::MONSTER].empty()); //make sure that at least some def info was found
|
2012-08-30 17:57:24 +03:00
|
|
|
|
2012-11-13 14:52:23 +03:00
|
|
|
const CGDefInfo * baseInfo = VLC->dobjinfo->gobjs[Obj::MONSTER].begin()->second;
|
2012-08-30 17:57:24 +03:00
|
|
|
|
2012-11-13 14:52:23 +03:00
|
|
|
BOOST_FOREACH(auto & crea, VLC->creh->creatures)
|
2012-09-25 15:41:48 +03:00
|
|
|
{
|
2012-11-13 14:52:23 +03:00
|
|
|
if (!vstd::contains(VLC->dobjinfo->gobjs[Obj::MONSTER], crea->idNumber)) // no obj info for this type
|
2012-09-25 15:41:48 +03:00
|
|
|
{
|
2012-11-13 14:52:23 +03:00
|
|
|
CGDefInfo * info = new CGDefInfo(*baseInfo);
|
|
|
|
info->subid = crea->idNumber;
|
|
|
|
info->name = crea->advMapDef;
|
|
|
|
|
|
|
|
VLC->dobjinfo->gobjs[Obj::MONSTER][crea->idNumber] = info;
|
2012-09-25 15:41:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-16 14:35:14 +03:00
|
|
|
{
|
|
|
|
assert(!VLC->dobjinfo->gobjs[Obj::ARTIFACT].empty());
|
|
|
|
|
|
|
|
const CGDefInfo * baseInfo = VLC->dobjinfo->gobjs[Obj::ARTIFACT].begin()->second;
|
|
|
|
|
|
|
|
BOOST_FOREACH(auto & art, VLC->arth->artifacts)
|
|
|
|
{
|
|
|
|
if (!vstd::contains(VLC->dobjinfo->gobjs[Obj::ARTIFACT], art->id)) // no obj info for this type
|
|
|
|
{
|
|
|
|
CGDefInfo * info = new CGDefInfo(*baseInfo);
|
|
|
|
info->subid = art->id;
|
|
|
|
info->name = art->advMapDef;
|
|
|
|
|
|
|
|
VLC->dobjinfo->gobjs[Obj::ARTIFACT][art->id] = info;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-09-25 15:41:48 +03:00
|
|
|
|
2012-08-30 17:57:24 +03:00
|
|
|
{
|
2012-11-13 14:52:23 +03:00
|
|
|
assert(!VLC->dobjinfo->gobjs[Obj::TOWN].empty()); //make sure that at least some def info was found
|
2012-08-30 17:57:24 +03:00
|
|
|
|
2012-11-13 14:52:23 +03:00
|
|
|
const CGDefInfo * baseInfo = VLC->dobjinfo->gobjs[Obj::TOWN].begin()->second;
|
|
|
|
auto & townInfos = VLC->dobjinfo->gobjs[Obj::TOWN];
|
2012-09-25 15:41:48 +03:00
|
|
|
|
2012-11-13 14:52:23 +03:00
|
|
|
BOOST_FOREACH(auto & town, VLC->townh->towns)
|
2012-09-26 22:02:44 +03:00
|
|
|
{
|
2012-11-13 14:52:23 +03:00
|
|
|
auto & cientInfo = town.second.clientInfo;
|
2012-09-20 21:41:16 +03:00
|
|
|
|
2012-11-13 14:52:23 +03:00
|
|
|
if (!vstd::contains(VLC->dobjinfo->gobjs[Obj::TOWN], town.first)) // no obj info for this type
|
2012-09-19 19:10:45 +03:00
|
|
|
{
|
2012-11-13 14:52:23 +03:00
|
|
|
CGDefInfo * info = new CGDefInfo(*baseInfo);
|
|
|
|
info->subid = town.first;
|
2012-08-10 16:07:53 +03:00
|
|
|
|
2012-11-13 14:52:23 +03:00
|
|
|
townInfos[town.first] = info;
|
|
|
|
}
|
|
|
|
townInfos[town.first]->name = cientInfo.advMapCastle;
|
2012-08-24 12:37:52 +03:00
|
|
|
|
2012-11-13 14:52:23 +03:00
|
|
|
VLC->dobjinfo->villages[town.first] = new CGDefInfo(*townInfos[town.first]);
|
|
|
|
VLC->dobjinfo->villages[town.first]->name = cientInfo.advMapVillage;
|
2012-08-24 12:37:52 +03:00
|
|
|
|
2012-11-13 14:52:23 +03:00
|
|
|
VLC->dobjinfo->capitols[town.first] = new CGDefInfo(*townInfos[town.first]);
|
|
|
|
VLC->dobjinfo->capitols[town.first]->name = cientInfo.advMapCapitol;
|
2012-12-19 19:35:58 +03:00
|
|
|
|
|
|
|
for (int i = 0; i < town.second.dwellings.size(); ++i)
|
|
|
|
{
|
|
|
|
const CGDefInfo * baseInfo = VLC->dobjinfo->gobjs[Obj::CREATURE_GENERATOR1][i]; //get same blockmap as first dwelling of tier i
|
|
|
|
|
|
|
|
BOOST_FOREACH (auto cre, town.second.creatures[i]) //both unupgraded and upgraded get same dwelling
|
|
|
|
{
|
|
|
|
CGDefInfo * info = new CGDefInfo(*baseInfo);
|
|
|
|
info->subid = cre;
|
|
|
|
info->name = town.second.dwellings[i];
|
|
|
|
VLC->dobjinfo->gobjs[Obj::CREATURE_GENERATOR1][cre] = info;
|
|
|
|
|
|
|
|
VLC->objh->cregens[cre] = cre; //map of dwelling -> creature id
|
|
|
|
}
|
|
|
|
}
|
2012-08-10 16:07:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|