CModHandler.cpp 32 KB

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