CModHandler.cpp 27 KB

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