2012-08-10 13:07:53 +00:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CModHandler.h"
|
2012-09-20 18:41:16 +00:00
|
|
|
#include "CDefObjInfoHandler.h"
|
2012-08-11 09:06:23 +00:00
|
|
|
#include "JsonNode.h"
|
2012-09-20 16:14:23 +00:00
|
|
|
#include "Filesystem/CResourceLoader.h"
|
|
|
|
#include "Filesystem/ISimpleResourceLoader.h"
|
2012-12-03 16:00:17 +00:00
|
|
|
|
2012-08-10 13:07:53 +00: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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
class CArtHandler;
|
|
|
|
class CHeroHandler;
|
|
|
|
class CCreatureHandler;
|
|
|
|
class CSpellHandler;
|
|
|
|
class CBuildingHandler;
|
|
|
|
class CObjectHandler;
|
|
|
|
class CDefObjInfoHandler;
|
|
|
|
class CTownHandler;
|
|
|
|
class CGeneralTextHandler;
|
2012-09-19 16:10:45 +00:00
|
|
|
class ResourceLocator;
|
2012-08-10 13:07:53 +00:00
|
|
|
|
2012-12-03 16:00:17 +00:00
|
|
|
void CIdentifierStorage::requestIdentifier(std::string name, const boost::function<void(si32)> & callback)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
// 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 13:07:53 +00:00
|
|
|
CModHandler::CModHandler()
|
|
|
|
{
|
|
|
|
VLC->modh = this;
|
|
|
|
|
2012-08-11 09:06:23 +00:00
|
|
|
loadConfigFromFile ("defaultMods");
|
2012-11-13 11:52:23 +00:00
|
|
|
findAvailableMods();
|
2012-08-10 13:07:53 +00:00
|
|
|
//CResourceHandler::loadModsFilesystems(); //scan for all mods
|
|
|
|
//TODO: mod filesystem is already initialized at LibClasses launch
|
|
|
|
//TODO: load default (last?) config
|
|
|
|
}
|
|
|
|
|
|
|
|
void CModHandler::loadConfigFromFile (std::string name)
|
2012-08-11 09:06:23 +00:00
|
|
|
{
|
|
|
|
const JsonNode config(ResourceID("config/" + name + ".json"));
|
2012-09-20 21:28:18 +00:00
|
|
|
const JsonNode & hardcodedFeatures = config["hardcodedFeatures"];
|
2012-08-11 09:06:23 +00: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 12:49:23 +00:00
|
|
|
settings.MAX_BUILDING_PER_TURN = hardcodedFeatures["MAX_BUILDING_PER_TURN"].Float();
|
2012-08-11 09:06:23 +00: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 09:37:52 +00:00
|
|
|
|
2012-09-20 21:28:18 +00:00
|
|
|
const JsonNode & gameModules = config["modules"];
|
2012-08-24 09:37:52 +00: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-09-18 07:36:07 +00:00
|
|
|
|
2012-09-19 16:10:45 +00:00
|
|
|
//TODO: load only mods from the list
|
2012-08-11 09:06:23 +00:00
|
|
|
}
|
2012-09-20 21:28:18 +00:00
|
|
|
|
2012-08-10 13:07:53 +00:00
|
|
|
void CModHandler::saveConfigToFile (std::string name)
|
2012-08-11 09:06:23 +00:00
|
|
|
{
|
|
|
|
//JsonNode savedConf = config;
|
|
|
|
//JsonNode schema(ResourceID("config/defaultSettings.json"));
|
|
|
|
|
|
|
|
//savedConf.Struct().erase("session");
|
|
|
|
//savedConf.minimize(schema);
|
|
|
|
|
|
|
|
CResourceHandler::get()->createResource("config/" + name +".json");
|
|
|
|
|
|
|
|
std::ofstream file(CResourceHandler::get()->getResourceName(ResourceID("config/" + name +".json")), std::ofstream::trunc);
|
|
|
|
//file << savedConf;
|
|
|
|
}
|
2012-08-30 14:57:24 +00:00
|
|
|
|
|
|
|
|
2012-11-13 11:52:23 +00:00
|
|
|
void CModHandler::findAvailableMods()
|
|
|
|
{
|
|
|
|
//TODO: read mods from Mods/ folder
|
2012-08-30 14:57:24 +00:00
|
|
|
|
2012-11-13 11:52:23 +00:00
|
|
|
auto & configList = CResourceHandler::get()->getResourcesWithName (ResourceID("CONFIG/mod.json"));
|
2012-08-30 14:57:24 +00:00
|
|
|
|
2012-11-13 11:52:23 +00:00
|
|
|
BOOST_FOREACH(auto & entry, configList)
|
|
|
|
{
|
|
|
|
auto stream = entry.getLoader()->load (entry.getResourceName());
|
|
|
|
std::unique_ptr<ui8[]> textData (new ui8[stream->getSize()]);
|
|
|
|
stream->read (textData.get(), stream->getSize());
|
2012-08-30 14:57:24 +00:00
|
|
|
|
2012-11-13 11:52:23 +00:00
|
|
|
tlog3 << "\t\tFound mod file: " << entry.getResourceName() << "\n";
|
|
|
|
allMods[allMods.size()].config.reset(new JsonNode((char*)textData.get(), stream->getSize()));
|
|
|
|
}
|
|
|
|
}
|
2012-09-20 18:41:16 +00:00
|
|
|
|
2012-11-13 11:52:23 +00:00
|
|
|
void CModHandler::loadActiveMods()
|
|
|
|
{
|
|
|
|
BOOST_FOREACH(auto & mod, allMods)
|
2012-09-19 16:10:45 +00:00
|
|
|
{
|
2012-11-13 11:52:23 +00:00
|
|
|
const JsonNode & config = *mod.second.config;
|
2012-09-19 16:10:45 +00:00
|
|
|
|
2012-11-13 11:52:23 +00:00
|
|
|
VLC->townh->load(config["factions"]);
|
|
|
|
VLC->creh->load(config["creatures"]);
|
|
|
|
}
|
|
|
|
VLC->creh->buildBonusTreeForTiers(); //do that after all new creatures are loaded
|
2012-12-03 16:00:17 +00:00
|
|
|
identifiers.finalize();
|
2012-11-13 11:52:23 +00:00
|
|
|
}
|
2012-08-30 14:57:24 +00:00
|
|
|
|
2012-11-13 11:52:23 +00:00
|
|
|
void CModHandler::reload()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
assert(!VLC->dobjinfo->gobjs[Obj::MONSTER].empty()); //make sure that at least some def info was found
|
2012-08-30 14:57:24 +00:00
|
|
|
|
2012-11-13 11:52:23 +00:00
|
|
|
const CGDefInfo * baseInfo = VLC->dobjinfo->gobjs[Obj::MONSTER].begin()->second;
|
2012-08-30 14:57:24 +00:00
|
|
|
|
2012-11-13 11:52:23 +00:00
|
|
|
BOOST_FOREACH(auto & crea, VLC->creh->creatures)
|
2012-09-25 12:41:48 +00:00
|
|
|
{
|
2012-11-13 11:52:23 +00:00
|
|
|
if (!vstd::contains(VLC->dobjinfo->gobjs[Obj::MONSTER], crea->idNumber)) // no obj info for this type
|
2012-09-25 12:41:48 +00:00
|
|
|
{
|
2012-11-13 11:52:23 +00: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 12:41:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-30 14:57:24 +00:00
|
|
|
{
|
2012-11-13 11:52:23 +00:00
|
|
|
assert(!VLC->dobjinfo->gobjs[Obj::TOWN].empty()); //make sure that at least some def info was found
|
2012-08-30 14:57:24 +00:00
|
|
|
|
2012-11-13 11:52:23 +00:00
|
|
|
const CGDefInfo * baseInfo = VLC->dobjinfo->gobjs[Obj::TOWN].begin()->second;
|
|
|
|
auto & townInfos = VLC->dobjinfo->gobjs[Obj::TOWN];
|
2012-09-25 12:41:48 +00:00
|
|
|
|
2012-11-13 11:52:23 +00:00
|
|
|
BOOST_FOREACH(auto & town, VLC->townh->towns)
|
2012-09-26 19:02:44 +00:00
|
|
|
{
|
2012-11-13 11:52:23 +00:00
|
|
|
auto & cientInfo = town.second.clientInfo;
|
2012-09-20 18:41:16 +00:00
|
|
|
|
2012-11-13 11:52:23 +00:00
|
|
|
if (!vstd::contains(VLC->dobjinfo->gobjs[Obj::TOWN], town.first)) // no obj info for this type
|
2012-09-19 16:10:45 +00:00
|
|
|
{
|
2012-11-13 11:52:23 +00:00
|
|
|
CGDefInfo * info = new CGDefInfo(*baseInfo);
|
|
|
|
info->subid = town.first;
|
2012-08-10 13:07:53 +00:00
|
|
|
|
2012-11-13 11:52:23 +00:00
|
|
|
townInfos[town.first] = info;
|
|
|
|
}
|
|
|
|
townInfos[town.first]->name = cientInfo.advMapCastle;
|
2012-08-24 09:37:52 +00:00
|
|
|
|
2012-11-13 11:52:23 +00:00
|
|
|
VLC->dobjinfo->villages[town.first] = new CGDefInfo(*townInfos[town.first]);
|
|
|
|
VLC->dobjinfo->villages[town.first]->name = cientInfo.advMapVillage;
|
2012-08-24 09:37:52 +00:00
|
|
|
|
2012-11-13 11:52:23 +00:00
|
|
|
VLC->dobjinfo->capitols[town.first] = new CGDefInfo(*townInfos[town.first]);
|
|
|
|
VLC->dobjinfo->capitols[town.first]->name = cientInfo.advMapCapitol;
|
2012-08-10 13:07:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|