CModHandler.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. /*
  2. * CModHandler.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CModHandler.h"
  12. #include "mapObjects/CObjectClassesHandler.h"
  13. #include "filesystem/FileStream.h"
  14. #include "filesystem/AdapterLoaders.h"
  15. #include "filesystem/CFilesystemLoader.h"
  16. #include "CCreatureHandler.h"
  17. #include "CArtHandler.h"
  18. #include "CTownHandler.h"
  19. #include "CHeroHandler.h"
  20. #include "mapObjects/CObjectHandler.h"
  21. #include "StringConstants.h"
  22. #include "CStopWatch.h"
  23. #include "IHandlerBase.h"
  24. #include "spells/CSpellHandler.h"
  25. #include "CSkillHandler.h"
  26. CIdentifierStorage::CIdentifierStorage():
  27. state(LOADING)
  28. {
  29. }
  30. CIdentifierStorage::~CIdentifierStorage()
  31. {
  32. }
  33. void CIdentifierStorage::checkIdentifier(std::string & ID)
  34. {
  35. if (boost::algorithm::ends_with(ID, "."))
  36. logGlobal->warn("BIG WARNING: identifier %s seems to be broken!", ID);
  37. else
  38. {
  39. size_t pos = 0;
  40. do
  41. {
  42. if (std::tolower(ID[pos]) != ID[pos] ) //Not in camelCase
  43. {
  44. logGlobal->warn("Warning: identifier %s is not in camelCase!", ID);
  45. ID[pos] = std::tolower(ID[pos]);// Try to fix the ID
  46. }
  47. pos = ID.find('.', pos);
  48. }
  49. while(pos++ != std::string::npos);
  50. }
  51. }
  52. CIdentifierStorage::ObjectCallback::ObjectCallback(
  53. std::string localScope, std::string remoteScope, std::string type,
  54. std::string name, const std::function<void(si32)> & callback,
  55. bool optional):
  56. localScope(localScope),
  57. remoteScope(remoteScope),
  58. type(type),
  59. name(name),
  60. callback(callback),
  61. optional(optional)
  62. {}
  63. static std::pair<std::string, std::string> splitString(std::string input, char separator)
  64. {
  65. std::pair<std::string, std::string> ret;
  66. size_t splitPos = input.find(separator);
  67. if (splitPos == std::string::npos)
  68. {
  69. ret.first.clear();
  70. ret.second = input;
  71. }
  72. else
  73. {
  74. ret.first = input.substr(0, splitPos);
  75. ret.second = input.substr(splitPos + 1);
  76. }
  77. return ret;
  78. }
  79. void CIdentifierStorage::requestIdentifier(ObjectCallback callback)
  80. {
  81. checkIdentifier(callback.type);
  82. checkIdentifier(callback.name);
  83. assert(!callback.localScope.empty());
  84. if (state != FINISHED) // enqueue request if loading is still in progress
  85. scheduledRequests.push_back(callback);
  86. else // execute immediately for "late" requests
  87. resolveIdentifier(callback);
  88. }
  89. void CIdentifierStorage::requestIdentifier(std::string scope, std::string type, std::string name, const std::function<void(si32)> & callback)
  90. {
  91. auto pair = splitString(name, ':'); // remoteScope:name
  92. requestIdentifier(ObjectCallback(scope, pair.first, type, pair.second, callback, false));
  93. }
  94. void CIdentifierStorage::requestIdentifier(std::string scope, std::string fullName, const std::function<void(si32)>& callback)
  95. {
  96. auto scopeAndFullName = splitString(fullName, ':');
  97. auto typeAndName = splitString(scopeAndFullName.second, '.');
  98. requestIdentifier(ObjectCallback(scope, scopeAndFullName.first, typeAndName.first, typeAndName.second, callback, false));
  99. }
  100. void CIdentifierStorage::requestIdentifier(std::string type, const JsonNode & name, const std::function<void(si32)> & callback)
  101. {
  102. auto pair = splitString(name.String(), ':'); // remoteScope:name
  103. requestIdentifier(ObjectCallback(name.meta, pair.first, type, pair.second, callback, false));
  104. }
  105. void CIdentifierStorage::requestIdentifier(const JsonNode & name, const std::function<void(si32)> & callback)
  106. {
  107. auto pair = splitString(name.String(), ':'); // remoteScope:<type.name>
  108. auto pair2 = splitString(pair.second, '.'); // type.name
  109. requestIdentifier(ObjectCallback(name.meta, pair.first, pair2.first, pair2.second, callback, false));
  110. }
  111. void CIdentifierStorage::tryRequestIdentifier(std::string scope, std::string type, std::string name, const std::function<void(si32)> & callback)
  112. {
  113. auto pair = splitString(name, ':'); // remoteScope:name
  114. requestIdentifier(ObjectCallback(scope, pair.first, type, pair.second, callback, true));
  115. }
  116. void CIdentifierStorage::tryRequestIdentifier(std::string type, const JsonNode & name, const std::function<void(si32)> & callback)
  117. {
  118. auto pair = splitString(name.String(), ':'); // remoteScope:name
  119. requestIdentifier(ObjectCallback(name.meta, pair.first, type, pair.second, callback, true));
  120. }
  121. boost::optional<si32> CIdentifierStorage::getIdentifier(std::string scope, std::string type, std::string name, bool silent)
  122. {
  123. auto pair = splitString(name, ':'); // remoteScope:name
  124. auto idList = getPossibleIdentifiers(ObjectCallback(scope, pair.first, type, pair.second, std::function<void(si32)>(), silent));
  125. if (idList.size() == 1)
  126. return idList.front().id;
  127. if (!silent)
  128. logGlobal->error("Failed to resolve identifier %s of type %s from mod %s", name , type ,scope);
  129. return boost::optional<si32>();
  130. }
  131. boost::optional<si32> CIdentifierStorage::getIdentifier(std::string type, const JsonNode & name, bool silent)
  132. {
  133. auto pair = splitString(name.String(), ':'); // remoteScope:name
  134. auto idList = getPossibleIdentifiers(ObjectCallback(name.meta, pair.first, type, pair.second, std::function<void(si32)>(), silent));
  135. if (idList.size() == 1)
  136. return idList.front().id;
  137. if (!silent)
  138. logGlobal->error("Failed to resolve identifier %s of type %s from mod %s", name.String(), type, name.meta);
  139. return boost::optional<si32>();
  140. }
  141. boost::optional<si32> CIdentifierStorage::getIdentifier(const JsonNode & name, bool silent)
  142. {
  143. auto pair = splitString(name.String(), ':'); // remoteScope:<type.name>
  144. auto pair2 = splitString(pair.second, '.'); // type.name
  145. auto idList = getPossibleIdentifiers(ObjectCallback(name.meta, pair.first, pair2.first, pair2.second, std::function<void(si32)>(), silent));
  146. if (idList.size() == 1)
  147. return idList.front().id;
  148. if (!silent)
  149. logGlobal->error("Failed to resolve identifier %s of type %s from mod %s", name.String(), pair2.first, name.meta);
  150. return boost::optional<si32>();
  151. }
  152. boost::optional<si32> CIdentifierStorage::getIdentifier(std::string scope, std::string fullName, bool silent)
  153. {
  154. auto pair = splitString(fullName, ':'); // remoteScope:<type.name>
  155. auto pair2 = splitString(pair.second, '.'); // type.name
  156. auto idList = getPossibleIdentifiers(ObjectCallback(scope, pair.first, pair2.first, pair2.second, std::function<void(si32)>(), silent));
  157. if (idList.size() == 1)
  158. return idList.front().id;
  159. if (!silent)
  160. logGlobal->error("Failed to resolve identifier %s of type %s from mod %s", fullName, pair2.first, scope);
  161. return boost::optional<si32>();
  162. }
  163. void CIdentifierStorage::registerObject(std::string scope, std::string type, std::string name, si32 identifier)
  164. {
  165. ObjectData data;
  166. data.scope = scope;
  167. data.id = identifier;
  168. std::string fullID = type + '.' + name;
  169. checkIdentifier(fullID);
  170. registeredObjects.insert(std::make_pair(fullID, data));
  171. }
  172. std::vector<CIdentifierStorage::ObjectData> CIdentifierStorage::getPossibleIdentifiers(const ObjectCallback & request)
  173. {
  174. std::set<std::string> allowedScopes;
  175. if (request.remoteScope.empty())
  176. {
  177. // normally ID's from all required mods, own mod and virtual "core" mod are allowed
  178. if (request.localScope != "core" && request.localScope != "")
  179. allowedScopes = VLC->modh->getModData(request.localScope).dependencies;
  180. allowedScopes.insert(request.localScope);
  181. allowedScopes.insert("core");
  182. }
  183. else
  184. {
  185. //...unless destination mod was specified explicitly
  186. //note: getModData does not work for "core" by design
  187. //for map format support core mod has access to any mod
  188. //TODO: better solution for access from map?
  189. if(request.localScope == "core" || request.localScope == "")
  190. {
  191. allowedScopes.insert(request.remoteScope);
  192. }
  193. else
  194. {
  195. // allow only available to all core mod or dependencies
  196. auto myDeps = VLC->modh->getModData(request.localScope).dependencies;
  197. if (request.remoteScope == "core" || myDeps.count(request.remoteScope))
  198. allowedScopes.insert(request.remoteScope);
  199. }
  200. }
  201. std::string fullID = request.type + '.' + request.name;
  202. auto entries = registeredObjects.equal_range(fullID);
  203. if (entries.first != entries.second)
  204. {
  205. std::vector<ObjectData> locatedIDs;
  206. for (auto it = entries.first; it != entries.second; it++)
  207. {
  208. if (vstd::contains(allowedScopes, it->second.scope))
  209. {
  210. locatedIDs.push_back(it->second);
  211. }
  212. }
  213. return locatedIDs;
  214. }
  215. return std::vector<ObjectData>();
  216. }
  217. bool CIdentifierStorage::resolveIdentifier(const ObjectCallback & request)
  218. {
  219. auto identifiers = getPossibleIdentifiers(request);
  220. if (identifiers.size() == 1) // normally resolved ID
  221. {
  222. request.callback(identifiers.front().id);
  223. return true;
  224. }
  225. if (request.optional && identifiers.empty()) // failed to resolve optinal ID
  226. {
  227. return true;
  228. }
  229. // error found. Try to generate some debug info
  230. if (identifiers.size() == 0)
  231. logGlobal->error("Unknown identifier!");
  232. else
  233. logGlobal->error("Ambiguous identifier request!");
  234. logGlobal->error("Request for %s.%s from mod %s", request.type, request.name, request.localScope);
  235. for (auto id : identifiers)
  236. {
  237. logGlobal->error("\tID is available in mod %s", id.scope);
  238. }
  239. return false;
  240. }
  241. void CIdentifierStorage::finalize()
  242. {
  243. state = FINALIZING;
  244. bool errorsFound = false;
  245. //Note: we may receive new requests during resolution phase -> end may change -> range for can't be used
  246. for(auto it = scheduledRequests.begin(); it != scheduledRequests.end(); it++)
  247. {
  248. errorsFound |= !resolveIdentifier(*it);
  249. }
  250. if (errorsFound)
  251. {
  252. for(auto object : registeredObjects)
  253. {
  254. logGlobal->trace("%s : %s -> %d", object.second.scope, object.first, object.second.id);
  255. }
  256. logGlobal->error("All known identifiers were dumped into log file");
  257. }
  258. assert(errorsFound == false);
  259. state = FINISHED;
  260. }
  261. CContentHandler::ContentTypeHandler::ContentTypeHandler(IHandlerBase * handler, std::string objectName):
  262. handler(handler),
  263. objectName(objectName),
  264. originalData(handler->loadLegacyData(VLC->modh->settings.data["textData"][objectName].Float()))
  265. {
  266. for(auto & node : originalData)
  267. {
  268. node.setMeta("core");
  269. }
  270. }
  271. bool CContentHandler::ContentTypeHandler::preloadModData(std::string modName, std::vector<std::string> fileList, bool validate)
  272. {
  273. bool result;
  274. JsonNode data = JsonUtils::assembleFromFiles(fileList, result);
  275. data.setMeta(modName);
  276. ModInfo & modInfo = modData[modName];
  277. for(auto entry : data.Struct())
  278. {
  279. size_t colon = entry.first.find(':');
  280. if (colon == std::string::npos)
  281. {
  282. // normal object, local to this mod
  283. modInfo.modData[entry.first].swap(entry.second);
  284. }
  285. else
  286. {
  287. std::string remoteName = entry.first.substr(0, colon);
  288. std::string objectName = entry.first.substr(colon + 1);
  289. // patching this mod? Send warning and continue - this situation can be handled normally
  290. if (remoteName == modName)
  291. logGlobal->warn("Redundant namespace definition for %s", objectName);
  292. logGlobal->trace("Patching object %s (%s) from %s", objectName, remoteName, modName);
  293. JsonNode & remoteConf = modData[remoteName].patches[objectName];
  294. JsonUtils::merge(remoteConf, entry.second);
  295. }
  296. }
  297. return result;
  298. }
  299. bool CContentHandler::ContentTypeHandler::loadMod(std::string modName, bool validate)
  300. {
  301. ModInfo & modInfo = modData[modName];
  302. bool result = true;
  303. auto performValidate = [&,this](JsonNode & data, const std::string & name){
  304. handler->beforeValidate(data);
  305. if (validate)
  306. result &= JsonUtils::validate(data, "vcmi:" + objectName, name);
  307. };
  308. // apply patches
  309. if (!modInfo.patches.isNull())
  310. JsonUtils::merge(modInfo.modData, modInfo.patches);
  311. for(auto & entry : modInfo.modData.Struct())
  312. {
  313. const std::string & name = entry.first;
  314. JsonNode & data = entry.second;
  315. if (vstd::contains(data.Struct(), "index") && !data["index"].isNull())
  316. {
  317. // try to add H3 object data
  318. size_t index = data["index"].Float();
  319. if (originalData.size() > index)
  320. {
  321. JsonUtils::merge(originalData[index], data);
  322. performValidate(originalData[index],name);
  323. handler->loadObject(modName, name, originalData[index], index);
  324. originalData[index].clear(); // do not use same data twice (same ID)
  325. continue;
  326. }
  327. }
  328. // normal new object or one with index bigger that data size
  329. performValidate(data,name);
  330. handler->loadObject(modName, name, data);
  331. }
  332. return result;
  333. }
  334. void CContentHandler::ContentTypeHandler::loadCustom()
  335. {
  336. handler->loadCustom();
  337. }
  338. void CContentHandler::ContentTypeHandler::afterLoadFinalization()
  339. {
  340. handler->afterLoadFinalization();
  341. }
  342. CContentHandler::CContentHandler()
  343. {
  344. handlers.insert(std::make_pair("heroClasses", ContentTypeHandler(&VLC->heroh->classes, "heroClass")));
  345. handlers.insert(std::make_pair("artifacts", ContentTypeHandler(VLC->arth, "artifact")));
  346. handlers.insert(std::make_pair("creatures", ContentTypeHandler(VLC->creh, "creature")));
  347. handlers.insert(std::make_pair("factions", ContentTypeHandler(VLC->townh, "faction")));
  348. handlers.insert(std::make_pair("objects", ContentTypeHandler(VLC->objtypeh, "object")));
  349. handlers.insert(std::make_pair("heroes", ContentTypeHandler(VLC->heroh, "hero")));
  350. handlers.insert(std::make_pair("spells", ContentTypeHandler(VLC->spellh, "spell")));
  351. handlers.insert(std::make_pair("skills", ContentTypeHandler(VLC->skillh, "skill")));
  352. handlers.insert(std::make_pair("templates", ContentTypeHandler((IHandlerBase *)VLC->tplh, "template")));
  353. //TODO: any other types of moddables?
  354. }
  355. bool CContentHandler::preloadModData(std::string modName, JsonNode modConfig, bool validate)
  356. {
  357. bool result = true;
  358. for(auto & handler : handlers)
  359. {
  360. result &= handler.second.preloadModData(modName, modConfig[handler.first].convertTo<std::vector<std::string> >(), validate);
  361. }
  362. return result;
  363. }
  364. bool CContentHandler::loadMod(std::string modName, bool validate)
  365. {
  366. bool result = true;
  367. for(auto & handler : handlers)
  368. {
  369. result &= handler.second.loadMod(modName, validate);
  370. }
  371. return result;
  372. }
  373. void CContentHandler::loadCustom()
  374. {
  375. for(auto & handler : handlers)
  376. {
  377. handler.second.loadCustom();
  378. }
  379. }
  380. void CContentHandler::afterLoadFinalization()
  381. {
  382. for(auto & handler : handlers)
  383. {
  384. handler.second.afterLoadFinalization();
  385. }
  386. }
  387. void CContentHandler::preloadData(CModInfo & mod)
  388. {
  389. bool validate = (mod.validation != CModInfo::PASSED);
  390. // print message in format [<8-symbols checksum>] <modname>
  391. logGlobal->info("\t\t[%08x]%s", mod.checksum, mod.name);
  392. if (validate && mod.identifier != "core")
  393. {
  394. if (!JsonUtils::validate(mod.config, "vcmi:mod", mod.identifier))
  395. mod.validation = CModInfo::FAILED;
  396. }
  397. if (!preloadModData(mod.identifier, mod.config, validate))
  398. mod.validation = CModInfo::FAILED;
  399. }
  400. void CContentHandler::load(CModInfo & mod)
  401. {
  402. bool validate = (mod.validation != CModInfo::PASSED);
  403. if (!loadMod(mod.identifier, validate))
  404. mod.validation = CModInfo::FAILED;
  405. if (validate)
  406. {
  407. if (mod.validation != CModInfo::FAILED)
  408. logGlobal->info("\t\t[DONE] %s", mod.name);
  409. else
  410. logGlobal->error("\t\t[FAIL] %s", mod.name);
  411. }
  412. else
  413. logGlobal->info("\t\t[SKIP] %s", mod.name);
  414. }
  415. static JsonNode loadModSettings(std::string path)
  416. {
  417. if (CResourceHandler::get("local")->existsResource(ResourceID(path)))
  418. {
  419. return JsonNode(ResourceID(path, EResType::TEXT));
  420. }
  421. // Probably new install. Create initial configuration
  422. CResourceHandler::get("local")->createResource(path);
  423. return JsonNode();
  424. }
  425. JsonNode addMeta(JsonNode config, std::string meta)
  426. {
  427. config.setMeta(meta);
  428. return config;
  429. }
  430. CModInfo::CModInfo():
  431. checksum(0),
  432. enabled(false),
  433. validation(PENDING)
  434. {
  435. }
  436. CModInfo::CModInfo(std::string identifier,const JsonNode & local, const JsonNode & config):
  437. identifier(identifier),
  438. name(config["name"].String()),
  439. description(config["description"].String()),
  440. dependencies(config["depends"].convertTo<std::set<std::string> >()),
  441. conflicts(config["conflicts"].convertTo<std::set<std::string> >()),
  442. checksum(0),
  443. enabled(false),
  444. validation(PENDING),
  445. config(addMeta(config, identifier))
  446. {
  447. loadLocalData(local);
  448. }
  449. JsonNode CModInfo::saveLocalData() const
  450. {
  451. std::ostringstream stream;
  452. stream << std::noshowbase << std::hex << std::setw(8) << std::setfill('0') << checksum;
  453. JsonNode conf;
  454. conf["active"].Bool() = enabled;
  455. conf["validated"].Bool() = validation != FAILED;
  456. conf["checksum"].String() = stream.str();
  457. return conf;
  458. }
  459. std::string CModInfo::getModDir(std::string name)
  460. {
  461. return "MODS/" + boost::algorithm::replace_all_copy(name, ".", "/MODS/");
  462. }
  463. std::string CModInfo::getModFile(std::string name)
  464. {
  465. return getModDir(name) + "/mod.json";
  466. }
  467. void CModInfo::updateChecksum(ui32 newChecksum)
  468. {
  469. // comment-out next line to force validation of all mods ignoring checksum
  470. if (newChecksum != checksum)
  471. {
  472. checksum = newChecksum;
  473. validation = PENDING;
  474. }
  475. }
  476. void CModInfo::loadLocalData(const JsonNode & data)
  477. {
  478. bool validated = false;
  479. enabled = true;
  480. checksum = 0;
  481. if (data.getType() == JsonNode::DATA_BOOL)
  482. {
  483. enabled = data.Bool();
  484. }
  485. if (data.getType() == JsonNode::DATA_STRUCT)
  486. {
  487. enabled = data["active"].Bool();
  488. validated = data["validated"].Bool();
  489. checksum = strtol(data["checksum"].String().c_str(), nullptr, 16);
  490. }
  491. if (enabled)
  492. validation = validated ? PASSED : PENDING;
  493. else
  494. validation = validated ? PASSED : FAILED;
  495. }
  496. CModHandler::CModHandler()
  497. {
  498. modules.COMMANDERS = false;
  499. modules.STACK_ARTIFACT = false;
  500. modules.STACK_EXP = false;
  501. modules.MITHRIL = false;
  502. for (int i = 0; i < GameConstants::RESOURCE_QUANTITY; ++i)
  503. {
  504. identifiers.registerObject("core", "resource", GameConstants::RESOURCE_NAMES[i], i);
  505. }
  506. for(int i=0; i<GameConstants::PRIMARY_SKILLS; ++i)
  507. {
  508. identifiers.registerObject("core", "primSkill", PrimarySkill::names[i], i);
  509. identifiers.registerObject("core", "primarySkill", PrimarySkill::names[i], i);
  510. }
  511. }
  512. CModHandler::~CModHandler()
  513. {
  514. }
  515. void CModHandler::loadConfigFromFile (std::string name)
  516. {
  517. std::string paths;
  518. for(auto& p : CResourceHandler::get()->getResourceNames(ResourceID("config/" + name)))
  519. {
  520. paths += p.string() + ", ";
  521. }
  522. paths = paths.substr(0, paths.size() - 2);
  523. logGlobal->debug("Loading hardcoded features settings from [%s], result:", paths);
  524. settings.data = JsonUtils::assembleFromFiles("config/" + name);
  525. const JsonNode & hardcodedFeatures = settings.data["hardcodedFeatures"];
  526. settings.MAX_HEROES_AVAILABLE_PER_PLAYER = hardcodedFeatures["MAX_HEROES_AVAILABLE_PER_PLAYER"].Integer();
  527. logGlobal->debug("\tMAX_HEROES_AVAILABLE_PER_PLAYER\t%d", settings.MAX_HEROES_AVAILABLE_PER_PLAYER);
  528. settings.MAX_HEROES_ON_MAP_PER_PLAYER = hardcodedFeatures["MAX_HEROES_ON_MAP_PER_PLAYER"].Integer();
  529. logGlobal->debug("\tMAX_HEROES_ON_MAP_PER_PLAYER\t%d", settings.MAX_HEROES_ON_MAP_PER_PLAYER);
  530. settings.CREEP_SIZE = hardcodedFeatures["CREEP_SIZE"].Integer();
  531. logGlobal->debug("\tCREEP_SIZE\t%d", settings.CREEP_SIZE);
  532. settings.WEEKLY_GROWTH = hardcodedFeatures["WEEKLY_GROWTH_PERCENT"].Integer();
  533. logGlobal->debug("\tWEEKLY_GROWTH\t%d", settings.WEEKLY_GROWTH);
  534. settings.NEUTRAL_STACK_EXP = hardcodedFeatures["NEUTRAL_STACK_EXP_DAILY"].Integer();
  535. logGlobal->debug("\tNEUTRAL_STACK_EXP\t%d", settings.NEUTRAL_STACK_EXP);
  536. settings.MAX_BUILDING_PER_TURN = hardcodedFeatures["MAX_BUILDING_PER_TURN"].Integer();
  537. logGlobal->debug("\tMAX_BUILDING_PER_TURN\t%d", settings.MAX_BUILDING_PER_TURN);
  538. settings.DWELLINGS_ACCUMULATE_CREATURES = hardcodedFeatures["DWELLINGS_ACCUMULATE_CREATURES"].Bool();
  539. logGlobal->debug("\tDWELLINGS_ACCUMULATE_CREATURES\t%d", static_cast<int>(settings.DWELLINGS_ACCUMULATE_CREATURES));
  540. settings.ALL_CREATURES_GET_DOUBLE_MONTHS = hardcodedFeatures["ALL_CREATURES_GET_DOUBLE_MONTHS"].Bool();
  541. logGlobal->debug("\tALL_CREATURES_GET_DOUBLE_MONTHS\t%d", static_cast<int>(settings.ALL_CREATURES_GET_DOUBLE_MONTHS));
  542. settings.WINNING_HERO_WITH_NO_TROOPS_RETREATS = hardcodedFeatures["WINNING_HERO_WITH_NO_TROOPS_RETREATS"].Bool();
  543. logGlobal->debug("\tWINNING_HERO_WITH_NO_TROOPS_RETREATS\t%d", static_cast<int>(settings.WINNING_HERO_WITH_NO_TROOPS_RETREATS));
  544. settings.BLACK_MARKET_MONTHLY_ARTIFACTS_CHANGE = hardcodedFeatures["BLACK_MARKET_MONTHLY_ARTIFACTS_CHANGE"].Bool();
  545. logGlobal->debug("\tBLACK_MARKET_MONTHLY_ARTIFACTS_CHANGE\t%d", static_cast<int>(settings.BLACK_MARKET_MONTHLY_ARTIFACTS_CHANGE));
  546. const JsonNode & gameModules = settings.data["modules"];
  547. modules.STACK_EXP = gameModules["STACK_EXPERIENCE"].Bool();
  548. logGlobal->debug("\tSTACK_EXP\t%d", static_cast<int>(modules.STACK_EXP));
  549. modules.STACK_ARTIFACT = gameModules["STACK_ARTIFACTS"].Bool();
  550. logGlobal->debug("\tSTACK_ARTIFACT\t%d", static_cast<int>(modules.STACK_ARTIFACT));
  551. modules.COMMANDERS = gameModules["COMMANDERS"].Bool();
  552. logGlobal->debug("\tCOMMANDERS\t%d", static_cast<int>(modules.COMMANDERS));
  553. modules.MITHRIL = gameModules["MITHRIL"].Bool();
  554. logGlobal->debug("\tMITHRIL\t%d", static_cast<int>(modules.MITHRIL));
  555. }
  556. // currentList is passed by value to get current list of depending mods
  557. bool CModHandler::hasCircularDependency(TModID modID, std::set <TModID> currentList) const
  558. {
  559. const CModInfo & mod = allMods.at(modID);
  560. // Mod already present? We found a loop
  561. if (vstd::contains(currentList, modID))
  562. {
  563. logGlobal->error("Error: Circular dependency detected! Printing dependency list:");
  564. logGlobal->error("\t%s -> ", mod.name);
  565. return true;
  566. }
  567. currentList.insert(modID);
  568. // recursively check every dependency of this mod
  569. for(const TModID & dependency : mod.dependencies)
  570. {
  571. if (hasCircularDependency(dependency, currentList))
  572. {
  573. logGlobal->error("\t%s ->\n", mod.name); // conflict detected, print dependency list
  574. return true;
  575. }
  576. }
  577. return false;
  578. }
  579. bool CModHandler::checkDependencies(const std::vector <TModID> & input) const
  580. {
  581. for(const TModID & id : input)
  582. {
  583. const CModInfo & mod = allMods.at(id);
  584. for(const TModID & dep : mod.dependencies)
  585. {
  586. if (!vstd::contains(input, dep))
  587. {
  588. logGlobal->error("Error: Mod %s requires missing %s!", mod.name, dep);
  589. return false;
  590. }
  591. }
  592. for(const TModID & conflicting : mod.conflicts)
  593. {
  594. if (vstd::contains(input, conflicting))
  595. {
  596. logGlobal->error("Error: Mod %s conflicts with %s!", mod.name, allMods.at(conflicting).name);
  597. return false;
  598. }
  599. }
  600. if (hasCircularDependency(id))
  601. return false;
  602. }
  603. return true;
  604. }
  605. std::vector <TModID> CModHandler::resolveDependencies(std::vector <TModID> input) const
  606. {
  607. // Topological sort algorithm
  608. // May not be the fastest one but VCMI does not needs any speed here
  609. // Unless user have dozens of mods with complex dependencies this code should be fine
  610. // first - sort input to have input strictly based on name (and not on hashmap or anything else)
  611. boost::range::sort(input);
  612. std::vector <TModID> output;
  613. output.reserve(input.size());
  614. std::set <TModID> resolvedMods;
  615. // Check if all mod dependencies are resolved (moved to resolvedMods)
  616. auto isResolved = [&](const CModInfo & mod) -> bool
  617. {
  618. for(const TModID & dependency : mod.dependencies)
  619. {
  620. if (!vstd::contains(resolvedMods, dependency))
  621. return false;
  622. }
  623. return true;
  624. };
  625. while (!input.empty())
  626. {
  627. std::set <TModID> toResolve; // list of mods resolved on this iteration
  628. for (auto it = input.begin(); it != input.end();)
  629. {
  630. if (isResolved(allMods.at(*it)))
  631. {
  632. toResolve.insert(*it);
  633. output.push_back(*it);
  634. it = input.erase(it);
  635. continue;
  636. }
  637. it++;
  638. }
  639. resolvedMods.insert(toResolve.begin(), toResolve.end());
  640. }
  641. return output;
  642. }
  643. std::vector<std::string> CModHandler::getModList(std::string path)
  644. {
  645. std::string modDir = boost::to_upper_copy(path + "MODS/");
  646. size_t depth = boost::range::count(modDir, '/');
  647. auto list = CResourceHandler::get("initial")->getFilteredFiles([&](const ResourceID & id) -> bool
  648. {
  649. if (id.getType() != EResType::DIRECTORY)
  650. return false;
  651. if (!boost::algorithm::starts_with(id.getName(), modDir))
  652. return false;
  653. if (boost::range::count(id.getName(), '/') != depth )
  654. return false;
  655. return true;
  656. });
  657. //storage for found mods
  658. std::vector<std::string> foundMods;
  659. for (auto & entry : list)
  660. {
  661. std::string name = entry.getName();
  662. name.erase(0, modDir.size()); //Remove path prefix
  663. // check if wog is actually present. Hack-ish but better than crash
  664. // TODO: remove soon (hopefully - before 0.96)
  665. if (name == "WOG")
  666. {
  667. if (!CResourceHandler::get("initial")->existsResource(ResourceID("DATA/ZVS", EResType::DIRECTORY)) &&
  668. !CResourceHandler::get("initial")->existsResource(ResourceID("MODS/WOG/DATA/ZVS", EResType::DIRECTORY)))
  669. {
  670. continue;
  671. }
  672. }
  673. if (!name.empty())
  674. foundMods.push_back(name);
  675. }
  676. return foundMods;
  677. }
  678. void CModHandler::loadMods(std::string path, std::string parent, const JsonNode & modSettings, bool enableMods)
  679. {
  680. for (std::string modName : getModList(path))
  681. {
  682. boost::to_lower(modName);
  683. std::string modFullName = parent.empty() ? modName : parent + '.' + modName;
  684. if (CResourceHandler::get("initial")->existsResource(ResourceID(CModInfo::getModFile(modFullName))))
  685. {
  686. CModInfo mod(modFullName, modSettings[modName], JsonNode(ResourceID(CModInfo::getModFile(modFullName))));
  687. if (!parent.empty()) // this is submod, add parent to dependecies
  688. mod.dependencies.insert(parent);
  689. allMods[modFullName] = mod;
  690. if (mod.enabled && enableMods)
  691. activeMods.push_back(modFullName);
  692. loadMods(CModInfo::getModDir(modFullName) + '/', modFullName, modSettings[modName]["mods"], enableMods && mod.enabled);
  693. }
  694. }
  695. }
  696. void CModHandler::loadMods()
  697. {
  698. const JsonNode modConfig = loadModSettings("config/modSettings.json");
  699. loadMods("", "", modConfig["activeMods"], true);
  700. coreMod = CModInfo("core", modConfig["core"], JsonNode(ResourceID("config/gameConfig.json")));
  701. coreMod.name = "Original game files";
  702. }
  703. std::vector<std::string> CModHandler::getAllMods()
  704. {
  705. std::vector<std::string> modlist;
  706. for (auto & entry : allMods)
  707. modlist.push_back(entry.first);
  708. return modlist;
  709. }
  710. std::vector<std::string> CModHandler::getActiveMods()
  711. {
  712. return activeMods;
  713. }
  714. static JsonNode genDefaultFS()
  715. {
  716. // default FS config for mods: directory "Content" that acts as H3 root directory
  717. JsonNode defaultFS;
  718. defaultFS[""].Vector().resize(2);
  719. defaultFS[""].Vector()[0]["type"].String() = "zip";
  720. defaultFS[""].Vector()[0]["path"].String() = "/Content.zip";
  721. defaultFS[""].Vector()[1]["type"].String() = "dir";
  722. defaultFS[""].Vector()[1]["path"].String() = "/Content";
  723. return defaultFS;
  724. }
  725. static ISimpleResourceLoader * genModFilesystem(const std::string & modName, const JsonNode & conf)
  726. {
  727. static const JsonNode defaultFS = genDefaultFS();
  728. if (!conf["filesystem"].isNull())
  729. return CResourceHandler::createFileSystem(CModInfo::getModDir(modName), conf["filesystem"]);
  730. else
  731. return CResourceHandler::createFileSystem(CModInfo::getModDir(modName), defaultFS);
  732. }
  733. static ui32 calculateModChecksum(const std::string modName, ISimpleResourceLoader * filesystem)
  734. {
  735. boost::crc_32_type modChecksum;
  736. // first - add current VCMI version into checksum to force re-validation on VCMI updates
  737. modChecksum.process_bytes(reinterpret_cast<const void*>(GameConstants::VCMI_VERSION.data()), GameConstants::VCMI_VERSION.size());
  738. // second - add mod.json into checksum because filesystem does not contains this file
  739. // FIXME: remove workaround for core mod
  740. if (modName != "core")
  741. {
  742. ResourceID modConfFile(CModInfo::getModFile(modName), EResType::TEXT);
  743. ui32 configChecksum = CResourceHandler::get("initial")->load(modConfFile)->calculateCRC32();
  744. modChecksum.process_bytes(reinterpret_cast<const void *>(&configChecksum), sizeof(configChecksum));
  745. }
  746. // third - add all detected text files from this mod into checksum
  747. auto files = filesystem->getFilteredFiles([](const ResourceID & resID)
  748. {
  749. return resID.getType() == EResType::TEXT &&
  750. ( boost::starts_with(resID.getName(), "DATA") ||
  751. boost::starts_with(resID.getName(), "CONFIG"));
  752. });
  753. for (const ResourceID & file : files)
  754. {
  755. ui32 fileChecksum = filesystem->load(file)->calculateCRC32();
  756. modChecksum.process_bytes(reinterpret_cast<const void *>(&fileChecksum), sizeof(fileChecksum));
  757. }
  758. return modChecksum.checksum();
  759. }
  760. void CModHandler::loadModFilesystems()
  761. {
  762. activeMods = resolveDependencies(activeMods);
  763. coreMod.updateChecksum(calculateModChecksum("core", CResourceHandler::get("core")));
  764. for(std::string & modName : activeMods)
  765. {
  766. CModInfo & mod = allMods[modName];
  767. CResourceHandler::addFilesystem("data", modName, genModFilesystem(modName, mod.config));
  768. }
  769. }
  770. CModInfo & CModHandler::getModData(TModID modId)
  771. {
  772. auto it = allMods.find(modId);
  773. if(it == allMods.end())
  774. {
  775. throw std::runtime_error("Mod not found '" + modId+"'");
  776. }
  777. else
  778. {
  779. return it->second;
  780. }
  781. }
  782. void CModHandler::initializeConfig()
  783. {
  784. loadConfigFromFile("defaultMods.json");
  785. }
  786. void CModHandler::load()
  787. {
  788. CStopWatch totalTime, timer;
  789. CContentHandler content;
  790. logGlobal->info("\tInitializing content handler: %d ms", timer.getDiff());
  791. for(const TModID & modName : activeMods)
  792. {
  793. logGlobal->trace("Generating checksum for %s", modName);
  794. allMods[modName].updateChecksum(calculateModChecksum(modName, CResourceHandler::get(modName)));
  795. }
  796. // first - load virtual "core" mod that contains all data
  797. // TODO? move all data into real mods? RoE, AB, SoD, WoG
  798. content.preloadData(coreMod);
  799. for(const TModID & modName : activeMods)
  800. content.preloadData(allMods[modName]);
  801. logGlobal->info("\tParsing mod data: %d ms", timer.getDiff());
  802. content.load(coreMod);
  803. for(const TModID & modName : activeMods)
  804. content.load(allMods[modName]);
  805. content.loadCustom();
  806. logGlobal->info("\tLoading mod data: %d ms", timer.getDiff());
  807. VLC->creh->loadCrExpBon();
  808. VLC->creh->buildBonusTreeForTiers(); //do that after all new creatures are loaded
  809. identifiers.finalize();
  810. logGlobal->info("\tResolving identifiers: %d ms", timer.getDiff());
  811. content.afterLoadFinalization();
  812. logGlobal->info("\tHandlers post-load finalization: %d ms ", timer.getDiff());
  813. logGlobal->info("\tAll game content loaded in %d ms", totalTime.getDiff());
  814. }
  815. void CModHandler::afterLoad()
  816. {
  817. JsonNode modSettings;
  818. for (auto & modEntry : allMods)
  819. {
  820. std::string pointer = "/" + boost::algorithm::replace_all_copy(modEntry.first, ".", "/mods/");
  821. modSettings["activeMods"].resolvePointer(pointer) = modEntry.second.saveLocalData();
  822. }
  823. modSettings["core"] = coreMod.saveLocalData();
  824. FileStream file(*CResourceHandler::get()->getResourceName(ResourceID("config/modSettings.json")), std::ofstream::out | std::ofstream::trunc);
  825. file << modSettings.toJson();
  826. }
  827. std::string CModHandler::normalizeIdentifier(const std::string & scope, const std::string & remoteScope, const std::string & identifier)
  828. {
  829. auto p = splitString(identifier, ':');
  830. if(p.first.empty())
  831. p.first = scope;
  832. if(p.first == remoteScope)
  833. p.first.clear();
  834. return p.first.empty() ? p.second : p.first + ":" + p.second;
  835. }
  836. void CModHandler::parseIdentifier(const std::string & fullIdentifier, std::string & scope, std::string & type, std::string & identifier)
  837. {
  838. auto p = splitString(fullIdentifier, ':');
  839. scope = p.first;
  840. auto p2 = splitString(p.second, '.');
  841. if(p2.first != "")
  842. {
  843. type = p2.first;
  844. identifier = p2.second;
  845. }
  846. else
  847. {
  848. type = p.second;
  849. identifier = "";
  850. }
  851. }
  852. std::string CModHandler::makeFullIdentifier(const std::string & scope, const std::string & type, const std::string & identifier)
  853. {
  854. auto p = splitString(identifier, ':');
  855. if(p.first != "")
  856. return p.first + ":" + type + "." + p.second;//ignore type if identifier is scoped
  857. else
  858. return scope == "" ? (identifier == "" ? type : type + "." + identifier) : scope + ":" + type + "." + identifier;
  859. }