2
0

CModHandler.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. #include "StdInc.h"
  2. #include "CModHandler.h"
  3. #include "CDefObjInfoHandler.h"
  4. #include "JsonNode.h"
  5. #include "filesystem/Filesystem.h"
  6. #include "CCreatureHandler.h"
  7. #include "CArtHandler.h"
  8. #include "CTownHandler.h"
  9. #include "CHeroHandler.h"
  10. #include "CObjectHandler.h"
  11. #include "StringConstants.h"
  12. #include "CStopWatch.h"
  13. #include "IHandlerBase.h"
  14. /*
  15. * CModHandler.cpp, part of VCMI engine
  16. *
  17. * Authors: listed in file AUTHORS in main folder
  18. *
  19. * License: GNU General Public License v2.0 or later
  20. * Full text of license available in license.txt file, in main folder
  21. *
  22. */
  23. void CIdentifierStorage::checkIdentifier(std::string & ID)
  24. {
  25. if (boost::algorithm::ends_with(ID, "."))
  26. logGlobal->warnStream() << "BIG WARNING: identifier " << ID << " seems to be broken!";
  27. else
  28. {
  29. size_t pos = 0;
  30. do
  31. {
  32. if (std::tolower(ID[pos]) != ID[pos] ) //Not in camelCase
  33. {
  34. logGlobal->warnStream() << "Warning: identifier " << ID << " is not in camelCase!";
  35. ID[pos] = std::tolower(ID[pos]);// Try to fix the ID
  36. }
  37. pos = ID.find('.', pos);
  38. }
  39. while(pos++ != std::string::npos);
  40. }
  41. }
  42. CIdentifierStorage::ObjectCallback::ObjectCallback(std::string localScope, std::string remoteScope, std::string type,
  43. std::string name, const std::function<void(si32)> & callback):
  44. localScope(localScope),
  45. remoteScope(remoteScope),
  46. type(type),
  47. name(name),
  48. callback(callback)
  49. {}
  50. static std::pair<std::string, std::string> splitString(std::string input, char separator)
  51. {
  52. std::pair<std::string, std::string> ret;
  53. size_t splitPos = input.find(separator);
  54. if (splitPos == std::string::npos)
  55. {
  56. ret.first.clear();
  57. ret.second = input;
  58. }
  59. else
  60. {
  61. ret.first = input.substr(0, splitPos);
  62. ret.second = input.substr(splitPos + 1);
  63. }
  64. return ret;
  65. }
  66. void CIdentifierStorage::requestIdentifier(ObjectCallback callback)
  67. {
  68. checkIdentifier(callback.type);
  69. checkIdentifier(callback.name);
  70. assert(!callback.localScope.empty());
  71. scheduledRequests.push_back(callback);
  72. }
  73. void CIdentifierStorage::requestIdentifier(std::string scope, std::string type, std::string name, const std::function<void(si32)> & callback)
  74. {
  75. auto pair = splitString(name, ':'); // remoteScope:name
  76. requestIdentifier(ObjectCallback(scope, pair.first, type, pair.second, callback));
  77. }
  78. void CIdentifierStorage::requestIdentifier(std::string type, const JsonNode & name, const std::function<void(si32)> & callback)
  79. {
  80. auto pair = splitString(name.String(), ':'); // remoteScope:name
  81. requestIdentifier(ObjectCallback(name.meta, pair.first, type, pair.second, callback));
  82. }
  83. void CIdentifierStorage::requestIdentifier(const JsonNode & name, const std::function<void(si32)> & callback)
  84. {
  85. auto pair = splitString(name.String(), ':'); // remoteScope:<type.name>
  86. auto pair2 = splitString(pair.second, '.'); // type.name
  87. requestIdentifier(ObjectCallback(name.meta, pair.first, pair2.first, pair2.second, callback));
  88. }
  89. void CIdentifierStorage::registerObject(std::string scope, std::string type, std::string name, si32 identifier)
  90. {
  91. ObjectData data;
  92. data.scope = scope;
  93. data.id = identifier;
  94. std::string fullID = type + '.' + name;
  95. checkIdentifier(fullID);
  96. registeredObjects.insert(std::make_pair(fullID, data));
  97. }
  98. bool CIdentifierStorage::resolveIdentifier(const ObjectCallback & request)
  99. {
  100. std::set<std::string> allowedScopes;
  101. if (request.remoteScope.empty())
  102. {
  103. // normally ID's from all required mods, own mod and virtual "core" mod are allowed
  104. if (request.localScope != "core")
  105. allowedScopes = VLC->modh->getModData(request.localScope).dependencies;
  106. allowedScopes.insert(request.localScope);
  107. allowedScopes.insert("core");
  108. }
  109. else
  110. {
  111. //...unless destination mod was specified explicitly
  112. auto myDeps = VLC->modh->getModData(request.localScope).dependencies;
  113. if (request.remoteScope == "core" || // allow only available to all core mod
  114. myDeps.count(request.remoteScope)) // or dependencies
  115. allowedScopes.insert(request.remoteScope);
  116. }
  117. std::string fullID = request.type + '.' + request.name;
  118. auto entries = registeredObjects.equal_range(fullID);
  119. if (entries.first != entries.second)
  120. {
  121. size_t matchesFound = 0;
  122. for (auto it = entries.first; it != entries.second; it++)
  123. {
  124. if (vstd::contains(allowedScopes, it->second.scope))
  125. {
  126. if (matchesFound == 0) // trigger only once
  127. request.callback(it->second.id);
  128. matchesFound++;
  129. }
  130. }
  131. if (matchesFound == 1)
  132. return true; // success, only one matching ID
  133. // error found. Try to generate some debug info
  134. if (matchesFound == 0)
  135. logGlobal->errorStream() << "Unknown identifier!";
  136. else
  137. logGlobal->errorStream() << "Ambiguous identifier request!";
  138. logGlobal->errorStream() << "Request for " << request.type << "." << request.name << " from mod " << request.localScope;
  139. for (auto it = entries.first; it != entries.second; it++)
  140. {
  141. logGlobal->errorStream() << "\tID is available in mod " << it->second.scope;
  142. }
  143. }
  144. logGlobal->errorStream() << "Unknown identifier " << request.type << "." << request.name << " from mod " << request.localScope;
  145. return false;
  146. }
  147. void CIdentifierStorage::finalize()
  148. {
  149. bool errorsFound = false;
  150. for(const ObjectCallback & request : scheduledRequests)
  151. {
  152. errorsFound |= !resolveIdentifier(request);
  153. }
  154. if (errorsFound)
  155. {
  156. for(auto object : registeredObjects)
  157. {
  158. logGlobal->traceStream() << object.first << " -> " << object.second.id;
  159. }
  160. logGlobal->errorStream() << "All known identifiers were dumped into log file";
  161. }
  162. assert(errorsFound == false);
  163. }
  164. CContentHandler::ContentTypeHandler::ContentTypeHandler(IHandlerBase * handler, std::string objectName):
  165. handler(handler),
  166. objectName(objectName),
  167. originalData(handler->loadLegacyData(VLC->modh->settings.data["textData"][objectName].Float()))
  168. {
  169. for(auto & node : originalData)
  170. {
  171. node.setMeta("core");
  172. }
  173. }
  174. bool CContentHandler::ContentTypeHandler::preloadModData(std::string modName, std::vector<std::string> fileList)
  175. {
  176. bool result;
  177. JsonNode data = JsonUtils::assembleFromFiles(fileList, result);
  178. data.setMeta(modName);
  179. ModInfo & modInfo = modData[modName];
  180. for(auto entry : data.Struct())
  181. {
  182. size_t colon = entry.first.find(':');
  183. if (colon == std::string::npos)
  184. {
  185. // normal object, local to this mod
  186. modInfo.modData[entry.first].swap(entry.second);
  187. }
  188. else
  189. {
  190. std::string remoteName = entry.first.substr(0, colon);
  191. std::string objectName = entry.first.substr(colon + 1);
  192. // patching this mod? Send warning and continue - this situation can be handled normally
  193. if (remoteName == modName)
  194. logGlobal->warnStream() << "Redundant namespace definition for " << objectName;
  195. JsonNode & remoteConf = modData[remoteName].patches[objectName];
  196. JsonUtils::merge(remoteConf, entry.second);
  197. }
  198. }
  199. return result;
  200. }
  201. bool CContentHandler::ContentTypeHandler::loadMod(std::string modName)
  202. {
  203. ModInfo & modInfo = modData[modName];
  204. bool result = true;
  205. // apply patches
  206. if (!modInfo.patches.isNull())
  207. JsonUtils::merge(modInfo.modData, modInfo.patches);
  208. for(auto entry : modInfo.modData.Struct())
  209. {
  210. const std::string & name = entry.first;
  211. JsonNode & data = entry.second;
  212. if (vstd::contains(data.Struct(), "index") && !data["index"].isNull())
  213. {
  214. // try to add H3 object data
  215. size_t index = data["index"].Float();
  216. if (originalData.size() > index)
  217. {
  218. JsonUtils::merge(originalData[index], data);
  219. result &= JsonUtils::validate(originalData[index], "vcmi:" + objectName, name);
  220. handler->loadObject(modName, name, originalData[index], index);
  221. originalData[index].clear(); // do not use same data twice (same ID)
  222. continue;
  223. }
  224. }
  225. // normal new object or one with index bigger that data size
  226. result &= JsonUtils::validate(data, "vcmi:" + objectName, name);
  227. handler->loadObject(modName, name, data);
  228. }
  229. return result;
  230. }
  231. void CContentHandler::ContentTypeHandler::afterLoadFinalization()
  232. {
  233. handler->afterLoadFinalization();
  234. }
  235. CContentHandler::CContentHandler()
  236. {
  237. handlers.insert(std::make_pair("heroClasses", ContentTypeHandler(&VLC->heroh->classes, "heroClass")));
  238. handlers.insert(std::make_pair("artifacts", ContentTypeHandler(VLC->arth, "artifact")));
  239. handlers.insert(std::make_pair("creatures", ContentTypeHandler(VLC->creh, "creature")));
  240. handlers.insert(std::make_pair("factions", ContentTypeHandler(VLC->townh, "faction")));
  241. handlers.insert(std::make_pair("heroes", ContentTypeHandler(VLC->heroh, "hero")));
  242. //TODO: spells, bonuses, something else?
  243. }
  244. bool CContentHandler::preloadModData(std::string modName, JsonNode modConfig)
  245. {
  246. bool result = true;
  247. for(auto & handler : handlers)
  248. {
  249. result &= handler.second.preloadModData(modName, modConfig[handler.first].convertTo<std::vector<std::string> >());
  250. }
  251. return result;
  252. }
  253. bool CContentHandler::loadMod(std::string modName)
  254. {
  255. bool result = true;
  256. for(auto & handler : handlers)
  257. {
  258. result &= handler.second.loadMod(modName);
  259. }
  260. return result;
  261. }
  262. void CContentHandler::afterLoadFinalization()
  263. {
  264. for(auto & handler : handlers)
  265. {
  266. handler.second.afterLoadFinalization();
  267. }
  268. }
  269. CModHandler::CModHandler()
  270. {
  271. for (int i = 0; i < GameConstants::RESOURCE_QUANTITY; ++i)
  272. {
  273. identifiers.registerObject("core", "resource", GameConstants::RESOURCE_NAMES[i], i);
  274. }
  275. for(int i=0; i<GameConstants::PRIMARY_SKILLS; ++i)
  276. identifiers.registerObject("core", "primSkill", PrimarySkill::names[i], i);
  277. }
  278. void CModHandler::loadConfigFromFile (std::string name)
  279. {
  280. settings.data = JsonUtils::assembleFromFiles("config/" + name);
  281. const JsonNode & hardcodedFeatures = settings.data["hardcodedFeatures"];
  282. settings.CREEP_SIZE = hardcodedFeatures["CREEP_SIZE"].Float();
  283. settings.WEEKLY_GROWTH = hardcodedFeatures["WEEKLY_GROWTH_PERCENT"].Float();
  284. settings.NEUTRAL_STACK_EXP = hardcodedFeatures["NEUTRAL_STACK_EXP_DAILY"].Float();
  285. settings.MAX_BUILDING_PER_TURN = hardcodedFeatures["MAX_BUILDING_PER_TURN"].Float();
  286. settings.DWELLINGS_ACCUMULATE_CREATURES = hardcodedFeatures["DWELLINGS_ACCUMULATE_CREATURES"].Bool();
  287. settings.ALL_CREATURES_GET_DOUBLE_MONTHS = hardcodedFeatures["ALL_CREATURES_GET_DOUBLE_MONTHS"].Bool();
  288. const JsonNode & gameModules = settings.data["modules"];
  289. modules.STACK_EXP = gameModules["STACK_EXPERIENCE"].Bool();
  290. modules.STACK_ARTIFACT = gameModules["STACK_ARTIFACTS"].Bool();
  291. modules.COMMANDERS = gameModules["COMMANDERS"].Bool();
  292. modules.MITHRIL = gameModules["MITHRIL"].Bool();
  293. }
  294. // currentList is passed by value to get current list of depending mods
  295. bool CModHandler::hasCircularDependency(TModID modID, std::set <TModID> currentList) const
  296. {
  297. const CModInfo & mod = allMods.at(modID);
  298. // Mod already present? We found a loop
  299. if (vstd::contains(currentList, modID))
  300. {
  301. logGlobal->errorStream() << "Error: Circular dependency detected! Printing dependency list:";
  302. logGlobal->errorStream() << "\t" << mod.name << " -> ";
  303. return true;
  304. }
  305. currentList.insert(modID);
  306. // recursively check every dependency of this mod
  307. for(const TModID & dependency : mod.dependencies)
  308. {
  309. if (hasCircularDependency(dependency, currentList))
  310. {
  311. logGlobal->errorStream() << "\t" << mod.name << " ->\n"; // conflict detected, print dependency list
  312. return true;
  313. }
  314. }
  315. return false;
  316. }
  317. bool CModHandler::checkDependencies(const std::vector <TModID> & input) const
  318. {
  319. for(const TModID & id : input)
  320. {
  321. const CModInfo & mod = allMods.at(id);
  322. for(const TModID & dep : mod.dependencies)
  323. {
  324. if (!vstd::contains(input, dep))
  325. {
  326. logGlobal->errorStream() << "Error: Mod " << mod.name << " requires missing " << dep << "!";
  327. return false;
  328. }
  329. }
  330. for(const TModID & conflicting : mod.conflicts)
  331. {
  332. if (vstd::contains(input, conflicting))
  333. {
  334. logGlobal->errorStream() << "Error: Mod " << mod.name << " conflicts with " << allMods.at(conflicting).name << "!";
  335. return false;
  336. }
  337. }
  338. if (hasCircularDependency(id))
  339. return false;
  340. }
  341. return true;
  342. }
  343. std::vector <TModID> CModHandler::resolveDependencies(std::vector <TModID> input) const
  344. {
  345. // Topological sort algorithm
  346. // May not be the fastest one but VCMI does not needs any speed here
  347. // Unless user have dozens of mods with complex dependencies this code should be fine
  348. // first - sort input to have input strictly based on name (and not on hashmap or anything else)
  349. boost::range::sort(input);
  350. std::vector <TModID> output;
  351. output.reserve(input.size());
  352. std::set <TModID> resolvedMods;
  353. // Check if all mod dependencies are resolved (moved to resolvedMods)
  354. auto isResolved = [&](const CModInfo mod) -> bool
  355. {
  356. for(const TModID & dependency : mod.dependencies)
  357. {
  358. if (!vstd::contains(resolvedMods, dependency))
  359. return false;
  360. }
  361. return true;
  362. };
  363. while (!input.empty())
  364. {
  365. std::set <TModID> toResolve; // list of mods resolved on this iteration
  366. for (auto it = input.begin(); it != input.end();)
  367. {
  368. if (isResolved(allMods.at(*it)))
  369. {
  370. toResolve.insert(*it);
  371. output.push_back(*it);
  372. it = input.erase(it);
  373. continue;
  374. }
  375. it++;
  376. }
  377. resolvedMods.insert(toResolve.begin(), toResolve.end());
  378. }
  379. return output;
  380. }
  381. static void updateModSettingsFormat(JsonNode & config)
  382. {
  383. for (auto & entry : config.Struct())
  384. {
  385. if (entry.second.getType() == JsonNode::DATA_BOOL)
  386. {
  387. entry.second["active"].Bool() = entry.second.Bool();
  388. }
  389. }
  390. }
  391. void CModHandler::initialize(std::vector<std::string> availableMods)
  392. {
  393. std::string confName = "config/modSettings.json";
  394. JsonNode modConfig;
  395. // Probably new install. Create initial configuration
  396. if (!CResourceHandler::get()->existsResource(ResourceID(confName)))
  397. CResourceHandler::get()->createResource(confName);
  398. else
  399. modConfig = JsonNode(ResourceID(confName));
  400. // mod compatibility: check if modSettings has old, 0.94 format
  401. updateModSettingsFormat(modConfig["activeMods"]);
  402. const JsonNode & modList = modConfig["activeMods"];
  403. JsonNode resultingList;
  404. std::vector <TModID> detectedMods;
  405. for(std::string name : availableMods)
  406. {
  407. boost::to_lower(name);
  408. std::string modFileName = "mods/" + name + "/mod.json";
  409. if (CResourceHandler::get()->existsResource(ResourceID(modFileName)))
  410. {
  411. const JsonNode config = JsonNode(ResourceID(modFileName));
  412. if (config.isNull())
  413. continue;
  414. if (!modList[name].isNull() && modList[name]["active"].Bool() == false )
  415. {
  416. resultingList[name]["active"].Bool() = false;
  417. continue; // disabled mod
  418. }
  419. resultingList[name]["active"].Bool() = true;
  420. CModInfo & mod = allMods[name];
  421. mod.identifier = name;
  422. mod.name = config["name"].String();
  423. mod.description = config["description"].String();
  424. mod.dependencies = config["depends"].convertTo<std::set<std::string> >();
  425. mod.conflicts = config["conflicts"].convertTo<std::set<std::string> >();
  426. detectedMods.push_back(name);
  427. }
  428. else
  429. logGlobal->warnStream() << "\t\t Directory " << name << " does not contains VCMI mod";
  430. }
  431. if (!checkDependencies(detectedMods))
  432. {
  433. logGlobal->errorStream() << "Critical error: failed to load mods! Exiting...";
  434. exit(1);
  435. }
  436. activeMods = resolveDependencies(detectedMods);
  437. modConfig["activeMods"] = resultingList;
  438. CResourceHandler::get()->createResource("CONFIG/modSettings.json");
  439. std::ofstream file(*CResourceHandler::get()->getResourceName(ResourceID("config/modSettings.json")), std::ofstream::trunc);
  440. file << modConfig;
  441. loadModFilesystems();
  442. }
  443. static JsonNode genDefaultFS()
  444. {
  445. // default FS config for mods: directory "Content" that acts as H3 root directory
  446. JsonNode defaultFS;
  447. defaultFS[""].Vector().resize(2);
  448. defaultFS[""].Vector()[0]["type"].String() = "zip";
  449. defaultFS[""].Vector()[0]["path"].String() = "/Content.zip";
  450. defaultFS[""].Vector()[1]["type"].String() = "dir";
  451. defaultFS[""].Vector()[1]["path"].String() = "/Content";
  452. return defaultFS;
  453. }
  454. static ISimpleResourceLoader * genModFilesystem(const std::string & modName, const JsonNode & conf)
  455. {
  456. static const JsonNode defaultFS = genDefaultFS();
  457. if (!conf["filesystem"].isNull())
  458. return CResourceHandler::createFileSystem("mods/" + modName, conf["filesystem"]);
  459. else
  460. return CResourceHandler::createFileSystem("mods/" + modName, defaultFS);
  461. }
  462. static ui32 calculateModChecksum(const std::string modName, ISimpleResourceLoader * filesystem)
  463. {
  464. boost::crc_32_type modChecksum;
  465. // first - add current VCMI version into checksum to force re-validation on VCMI updates
  466. modChecksum.process_bytes(reinterpret_cast<const void*>(GameConstants::VCMI_VERSION.data()), GameConstants::VCMI_VERSION.size());
  467. // second - add mod.json into checksum because filesystem does not contains this file
  468. ResourceID modConfFile("mods/" + modName + "/mod", EResType::TEXT);
  469. ui32 configChecksum = CResourceHandler::getInitial()->load(modConfFile)->calculateCRC32();
  470. modChecksum.process_bytes(reinterpret_cast<const void *>(&configChecksum), sizeof(configChecksum));
  471. // third - add all loaded files from this mod into checksum
  472. auto files = filesystem->getFilteredFiles([](const ResourceID & resID)
  473. {
  474. return resID.getType() == EResType::TEXT;
  475. });
  476. for (const ResourceID & file : files)
  477. {
  478. ui32 fileChecksum = filesystem->load(file)->calculateCRC32();
  479. modChecksum.process_bytes(reinterpret_cast<const void *>(&fileChecksum), sizeof(fileChecksum));
  480. }
  481. return modChecksum.checksum();
  482. }
  483. void CModHandler::loadModFilesystems()
  484. {
  485. for(std::string & modName : activeMods)
  486. {
  487. ResourceID modConfFile("mods/" + modName + "/mod", EResType::TEXT);
  488. auto fsConfigData = CResourceHandler::getInitial()->load(modConfFile)->readAll();
  489. const JsonNode fsConfig((char*)fsConfigData.first.get(), fsConfigData.second);
  490. auto filesystem = genModFilesystem(modName, fsConfig);
  491. CResourceHandler::get()->addLoader(filesystem, false);
  492. allMods[modName].checksum = calculateModChecksum(modName, filesystem);
  493. }
  494. }
  495. CModInfo & CModHandler::getModData(TModID modId)
  496. {
  497. CModInfo & mod = allMods.at(modId);
  498. assert(vstd::contains(activeMods, modId)); // not really necessary but won't hurt
  499. return mod;
  500. }
  501. void CModHandler::beforeLoad()
  502. {
  503. loadConfigFromFile("defaultMods.json");
  504. }
  505. void CModHandler::loadGameContent()
  506. {
  507. CStopWatch timer, totalTime;
  508. CContentHandler content;
  509. logGlobal->infoStream() << "\tInitializing content handler: " << timer.getDiff() << " ms";
  510. // first - load virtual "core" mod that contains all data
  511. // TODO? move all data into real mods? RoE, AB, SoD, WoG
  512. content.preloadModData("core", JsonNode(ResourceID("config/gameConfig.json")));
  513. logGlobal->infoStream() << "\tParsing original game data: " << timer.getDiff() << " ms";
  514. for(const TModID & modName : activeMods)
  515. {
  516. // print message in format [<8-symbols checksum>] <modname>
  517. logGlobal->infoStream() << "\t\t[" << std::noshowbase << std::hex << std::setw(8) << std::setfill('0')
  518. << allMods[modName].checksum << "] " << allMods[modName].name;
  519. std::string modFileName = "mods/" + modName + "/mod.json";
  520. const JsonNode config = JsonNode(ResourceID(modFileName));
  521. allMods[modName].validated = JsonUtils::validate(config, "vcmi:mod", modName);
  522. allMods[modName].validated &= content.preloadModData(modName, config);
  523. }
  524. logGlobal->infoStream() << "\tParsing mod data: " << timer.getDiff() << " ms";
  525. content.loadMod("core");
  526. logGlobal->infoStream() << "\tLoading original game data: " << timer.getDiff() << " ms";
  527. for(const TModID & modName : activeMods)
  528. {
  529. allMods[modName].validated &= content.loadMod(modName);
  530. if (allMods[modName].validated)
  531. logGlobal->infoStream() << "\t\t[DONE] " << allMods[modName].name;
  532. else
  533. logGlobal->errorStream() << "\t\t[FAIL] " << allMods[modName].name;
  534. }
  535. logGlobal->infoStream() << "\tLoading mod data: " << timer.getDiff() << "ms";
  536. VLC->creh->loadCrExpBon();
  537. VLC->creh->buildBonusTreeForTiers(); //do that after all new creatures are loaded
  538. identifiers.finalize();
  539. logGlobal->infoStream() << "\tResolving identifiers: " << timer.getDiff() << " ms";
  540. content.afterLoadFinalization();
  541. logGlobal->infoStream() << "\tHandlers post-load finalization: " << timer.getDiff() << " ms";
  542. logGlobal->infoStream() << "\tAll game content loaded in " << totalTime.getDiff() << " ms";
  543. }
  544. void CModHandler::reload()
  545. {
  546. {
  547. //recreate adventure map defs
  548. assert(!VLC->dobjinfo->gobjs[Obj::MONSTER].empty()); //make sure that at least some def info was found
  549. const CGDefInfo * baseInfo = VLC->dobjinfo->gobjs[Obj::MONSTER].begin()->second;
  550. for(auto & crea : VLC->creh->creatures)
  551. {
  552. if (!vstd::contains(VLC->dobjinfo->gobjs[Obj::MONSTER], crea->idNumber)) // no obj info for this type
  553. {
  554. auto info = new CGDefInfo(*baseInfo);
  555. info->subid = crea->idNumber;
  556. info->name = crea->advMapDef;
  557. VLC->dobjinfo->gobjs[Obj::MONSTER][crea->idNumber] = info;
  558. }
  559. }
  560. }
  561. {
  562. assert(!VLC->dobjinfo->gobjs[Obj::ARTIFACT].empty());
  563. const CGDefInfo * baseInfo = VLC->dobjinfo->gobjs[Obj::ARTIFACT].begin()->second;
  564. for(auto & art : VLC->arth->artifacts)
  565. {
  566. if (!vstd::contains(VLC->dobjinfo->gobjs[Obj::ARTIFACT], art->id)) // no obj info for this type
  567. {
  568. auto info = new CGDefInfo(*baseInfo);
  569. info->subid = art->id;
  570. info->name = art->advMapDef;
  571. VLC->dobjinfo->gobjs[Obj::ARTIFACT][art->id] = info;
  572. }
  573. }
  574. }
  575. {
  576. assert(!VLC->dobjinfo->gobjs[Obj::TOWN].empty()); //make sure that at least some def info was found
  577. const CGDefInfo * baseInfo = VLC->dobjinfo->gobjs[Obj::TOWN].begin()->second;
  578. auto & townInfos = VLC->dobjinfo->gobjs[Obj::TOWN];
  579. for(auto & faction : VLC->townh->factions)
  580. {
  581. TFaction index = faction->index;
  582. CTown * town = faction->town;
  583. if (town)
  584. {
  585. auto & cientInfo = town->clientInfo;
  586. if (!vstd::contains(VLC->dobjinfo->gobjs[Obj::TOWN], index)) // no obj info for this type
  587. {
  588. auto info = new CGDefInfo(*baseInfo);
  589. info->subid = index;
  590. townInfos[index] = info;
  591. }
  592. townInfos[index]->name = cientInfo.advMapCastle;
  593. VLC->dobjinfo->villages[index] = new CGDefInfo(*townInfos[index]);
  594. VLC->dobjinfo->villages[index]->name = cientInfo.advMapVillage;
  595. VLC->dobjinfo->capitols[index] = new CGDefInfo(*townInfos[index]);
  596. VLC->dobjinfo->capitols[index]->name = cientInfo.advMapCapitol;
  597. for (int i = 0; i < town->dwellings.size(); ++i)
  598. {
  599. const CGDefInfo * baseInfo = VLC->dobjinfo->gobjs[Obj::CREATURE_GENERATOR1][i]; //get same blockmap as first dwelling of tier i
  600. for (auto cre : town->creatures[i]) //both unupgraded and upgraded get same dwelling
  601. {
  602. auto info = new CGDefInfo(*baseInfo);
  603. info->subid = cre;
  604. info->name = town->dwellings[i];
  605. VLC->dobjinfo->gobjs[Obj::CREATURE_GENERATOR1][cre] = info;
  606. VLC->objh->cregens[cre] = cre; //map of dwelling -> creature id
  607. }
  608. }
  609. }
  610. }
  611. }
  612. }