CModHandler.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  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 "rmg/CRmgTemplateStorage.h"
  13. #include "filesystem/FileStream.h"
  14. #include "filesystem/AdapterLoaders.h"
  15. #include "filesystem/CFilesystemLoader.h"
  16. #include "filesystem/Filesystem.h"
  17. #include "CCreatureHandler.h"
  18. #include "CArtHandler.h"
  19. #include "CTownHandler.h"
  20. #include "CHeroHandler.h"
  21. #include "StringConstants.h"
  22. #include "CStopWatch.h"
  23. #include "IHandlerBase.h"
  24. #include "spells/CSpellHandler.h"
  25. #include "CSkillHandler.h"
  26. #include "CGeneralTextHandler.h"
  27. #include "Languages.h"
  28. #include "ScriptHandler.h"
  29. #include "RoadHandler.h"
  30. #include "GameSettings.h"
  31. #include "RiverHandler.h"
  32. #include "TerrainHandler.h"
  33. #include "BattleFieldHandler.h"
  34. #include "ObstacleHandler.h"
  35. #include "mapObjectConstructors/CObjectClassesHandler.h"
  36. #include <vstd/StringUtils.h>
  37. VCMI_LIB_NAMESPACE_BEGIN
  38. CIdentifierStorage::CIdentifierStorage():
  39. state(LOADING)
  40. {
  41. }
  42. void CIdentifierStorage::checkIdentifier(std::string & ID)
  43. {
  44. if (boost::algorithm::ends_with(ID, "."))
  45. logMod->warn("BIG WARNING: identifier %s seems to be broken!", ID);
  46. else
  47. {
  48. size_t pos = 0;
  49. do
  50. {
  51. if (std::tolower(ID[pos]) != ID[pos] ) //Not in camelCase
  52. {
  53. logMod->warn("Warning: identifier %s is not in camelCase!", ID);
  54. ID[pos] = std::tolower(ID[pos]);// Try to fix the ID
  55. }
  56. pos = ID.find('.', pos);
  57. }
  58. while(pos++ != std::string::npos);
  59. }
  60. }
  61. void CIdentifierStorage::requestIdentifier(ObjectCallback callback)
  62. {
  63. checkIdentifier(callback.type);
  64. checkIdentifier(callback.name);
  65. assert(!callback.localScope.empty());
  66. if (state != FINISHED) // enqueue request if loading is still in progress
  67. scheduledRequests.push_back(callback);
  68. else // execute immediately for "late" requests
  69. resolveIdentifier(callback);
  70. }
  71. CIdentifierStorage::ObjectCallback CIdentifierStorage::ObjectCallback::fromNameWithType(const std::string & scope, const std::string & fullName, const std::function<void(si32)> & callback, bool optional)
  72. {
  73. assert(!scope.empty());
  74. auto scopeAndFullName = vstd::splitStringToPair(fullName, ':');
  75. auto typeAndName = vstd::splitStringToPair(scopeAndFullName.second, '.');
  76. if (scope == scopeAndFullName.first)
  77. logMod->debug("Target scope for identifier '%s' is redundant! Identifier already defined in mod '%s'", fullName, scope);
  78. ObjectCallback result;
  79. result.localScope = scope;
  80. result.remoteScope = scopeAndFullName.first;
  81. result.type = typeAndName.first;
  82. result.name = typeAndName.second;
  83. result.callback = callback;
  84. result.optional = optional;
  85. return result;
  86. }
  87. CIdentifierStorage::ObjectCallback CIdentifierStorage::ObjectCallback::fromNameAndType(const std::string & scope, const std::string & type, const std::string & fullName, const std::function<void(si32)> & callback, bool optional)
  88. {
  89. assert(!scope.empty());
  90. auto scopeAndFullName = vstd::splitStringToPair(fullName, ':');
  91. auto typeAndName = vstd::splitStringToPair(scopeAndFullName.second, '.');
  92. if(!typeAndName.first.empty())
  93. {
  94. if (typeAndName.first != type)
  95. logMod->error("Identifier '%s' from mod '%s' requested with different type! Type '%s' expected!", fullName, scope, type);
  96. else
  97. logMod->debug("Target type for identifier '%s' defined in mod '%s' is redundant!", fullName, scope);
  98. }
  99. if (scope == scopeAndFullName.first)
  100. logMod->debug("Target scope for identifier '%s' is redundant! Identifier already defined in mod '%s'", fullName, scope);
  101. ObjectCallback result;
  102. result.localScope = scope;
  103. result.remoteScope = scopeAndFullName.first;
  104. result.type = type;
  105. result.name = typeAndName.second;
  106. result.callback = callback;
  107. result.optional = optional;
  108. return result;
  109. }
  110. void CIdentifierStorage::requestIdentifier(const std::string & scope, const std::string & type, const std::string & name, const std::function<void(si32)> & callback)
  111. {
  112. requestIdentifier(ObjectCallback::fromNameAndType(scope, type, name, callback, false));
  113. }
  114. void CIdentifierStorage::requestIdentifier(const std::string & scope, const std::string & fullName, const std::function<void(si32)> & callback)
  115. {
  116. requestIdentifier(ObjectCallback::fromNameWithType(scope, fullName, callback, false));
  117. }
  118. void CIdentifierStorage::requestIdentifier(const std::string & type, const JsonNode & name, const std::function<void(si32)> & callback)
  119. {
  120. requestIdentifier(ObjectCallback::fromNameAndType(name.meta, type, name.String(), callback, false));
  121. }
  122. void CIdentifierStorage::requestIdentifier(const JsonNode & name, const std::function<void(si32)> & callback)
  123. {
  124. requestIdentifier(ObjectCallback::fromNameWithType(name.meta, name.String(), callback, false));
  125. }
  126. void CIdentifierStorage::tryRequestIdentifier(const std::string & scope, const std::string & type, const std::string & name, const std::function<void(si32)> & callback)
  127. {
  128. requestIdentifier(ObjectCallback::fromNameAndType(scope, type, name, callback, true));
  129. }
  130. void CIdentifierStorage::tryRequestIdentifier(const std::string & type, const JsonNode & name, const std::function<void(si32)> & callback)
  131. {
  132. requestIdentifier(ObjectCallback::fromNameAndType(name.meta, type, name.String(), callback, true));
  133. }
  134. std::optional<si32> CIdentifierStorage::getIdentifier(const std::string & scope, const std::string & type, const std::string & name, bool silent)
  135. {
  136. auto idList = getPossibleIdentifiers(ObjectCallback::fromNameAndType(scope, type, name, std::function<void(si32)>(), silent));
  137. if (idList.size() == 1)
  138. return idList.front().id;
  139. if (!silent)
  140. logMod->error("Failed to resolve identifier %s of type %s from mod %s", name , type ,scope);
  141. return std::optional<si32>();
  142. }
  143. std::optional<si32> CIdentifierStorage::getIdentifier(const std::string & type, const JsonNode & name, bool silent)
  144. {
  145. auto idList = getPossibleIdentifiers(ObjectCallback::fromNameAndType(name.meta, type, name.String(), 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(), type, name.meta);
  150. return std::optional<si32>();
  151. }
  152. std::optional<si32> CIdentifierStorage::getIdentifier(const JsonNode & name, bool silent)
  153. {
  154. auto idList = getPossibleIdentifiers(ObjectCallback::fromNameWithType(name.meta, name.String(), std::function<void(si32)>(), silent));
  155. if (idList.size() == 1)
  156. return idList.front().id;
  157. if (!silent)
  158. logMod->error("Failed to resolve identifier %s from mod %s", name.String(), name.meta);
  159. return std::optional<si32>();
  160. }
  161. std::optional<si32> CIdentifierStorage::getIdentifier(const std::string & scope, const std::string & fullName, bool silent)
  162. {
  163. auto idList = getPossibleIdentifiers(ObjectCallback::fromNameWithType(scope, fullName, std::function<void(si32)>(), silent));
  164. if (idList.size() == 1)
  165. return idList.front().id;
  166. if (!silent)
  167. logMod->error("Failed to resolve identifier %s from mod %s", fullName, scope);
  168. return std::optional<si32>();
  169. }
  170. void CIdentifierStorage::registerObject(const std::string & scope, const std::string & type, const std::string & name, si32 identifier)
  171. {
  172. ObjectData data;
  173. data.scope = scope;
  174. data.id = identifier;
  175. std::string fullID = type + '.' + name;
  176. checkIdentifier(fullID);
  177. std::pair<const std::string, ObjectData> mapping = std::make_pair(fullID, data);
  178. if(!vstd::containsMapping(registeredObjects, mapping))
  179. {
  180. logMod->trace("registered %s as %s:%s", fullID, scope, identifier);
  181. registeredObjects.insert(mapping);
  182. }
  183. }
  184. std::vector<CIdentifierStorage::ObjectData> CIdentifierStorage::getPossibleIdentifiers(const ObjectCallback & request)
  185. {
  186. std::set<std::string> allowedScopes;
  187. bool isValidScope = true;
  188. // called have not specified destination mod explicitly
  189. if (request.remoteScope.empty())
  190. {
  191. // special scope that should have access to all in-game objects
  192. if (request.localScope == CModHandler::scopeGame())
  193. {
  194. for(const auto & modName : VLC->modh->getActiveMods())
  195. allowedScopes.insert(modName);
  196. }
  197. // normally ID's from all required mods, own mod and virtual built-in mod are allowed
  198. else if(request.localScope != CModHandler::scopeBuiltin() && !request.localScope.empty())
  199. {
  200. allowedScopes = VLC->modh->getModDependencies(request.localScope, isValidScope);
  201. if(!isValidScope)
  202. return std::vector<ObjectData>();
  203. allowedScopes.insert(request.localScope);
  204. }
  205. // all mods can access built-in mod
  206. allowedScopes.insert(CModHandler::scopeBuiltin());
  207. }
  208. else
  209. {
  210. //if destination mod was specified explicitly, restrict lookup to this mod
  211. if(request.remoteScope == CModHandler::scopeBuiltin() )
  212. {
  213. //built-in mod is an implicit dependency for all mods, allow access into it
  214. allowedScopes.insert(request.remoteScope);
  215. }
  216. else if ( request.localScope == CModHandler::scopeGame() )
  217. {
  218. // allow access, this is special scope that should have access to all in-game objects
  219. allowedScopes.insert(request.remoteScope);
  220. }
  221. else if(request.remoteScope == request.localScope )
  222. {
  223. // allow self-access
  224. allowedScopes.insert(request.remoteScope);
  225. }
  226. else
  227. {
  228. // allow access only if mod is in our dependencies
  229. auto myDeps = VLC->modh->getModDependencies(request.localScope, isValidScope);
  230. if(!isValidScope)
  231. return std::vector<ObjectData>();
  232. if(myDeps.count(request.remoteScope))
  233. allowedScopes.insert(request.remoteScope);
  234. }
  235. }
  236. std::string fullID = request.type + '.' + request.name;
  237. auto entries = registeredObjects.equal_range(fullID);
  238. if (entries.first != entries.second)
  239. {
  240. std::vector<ObjectData> locatedIDs;
  241. for (auto it = entries.first; it != entries.second; it++)
  242. {
  243. if (vstd::contains(allowedScopes, it->second.scope))
  244. {
  245. locatedIDs.push_back(it->second);
  246. }
  247. }
  248. return locatedIDs;
  249. }
  250. return std::vector<ObjectData>();
  251. }
  252. bool CIdentifierStorage::resolveIdentifier(const ObjectCallback & request)
  253. {
  254. auto identifiers = getPossibleIdentifiers(request);
  255. if (identifiers.size() == 1) // normally resolved ID
  256. {
  257. request.callback(identifiers.front().id);
  258. return true;
  259. }
  260. if (request.optional && identifiers.empty()) // failed to resolve optinal ID
  261. {
  262. return true;
  263. }
  264. // error found. Try to generate some debug info
  265. if(identifiers.empty())
  266. logMod->error("Unknown identifier!");
  267. else
  268. logMod->error("Ambiguous identifier request!");
  269. logMod->error("Request for %s.%s from mod %s", request.type, request.name, request.localScope);
  270. for(const auto & id : identifiers)
  271. {
  272. logMod->error("\tID is available in mod %s", id.scope);
  273. }
  274. return false;
  275. }
  276. void CIdentifierStorage::finalize()
  277. {
  278. state = FINALIZING;
  279. bool errorsFound = false;
  280. while ( !scheduledRequests.empty() )
  281. {
  282. // Use local copy since new requests may appear during resolving, invalidating any iterators
  283. auto request = scheduledRequests.back();
  284. scheduledRequests.pop_back();
  285. if (!resolveIdentifier(request))
  286. errorsFound = true;
  287. }
  288. if (errorsFound)
  289. {
  290. for(const auto & object : registeredObjects)
  291. {
  292. logMod->trace("%s : %s -> %d", object.second.scope, object.first, object.second.id);
  293. }
  294. logMod->error("All known identifiers were dumped into log file");
  295. }
  296. assert(errorsFound == false);
  297. state = FINISHED;
  298. }
  299. ContentTypeHandler::ContentTypeHandler(IHandlerBase * handler, const std::string & objectName):
  300. handler(handler),
  301. objectName(objectName),
  302. originalData(handler->loadLegacyData())
  303. {
  304. for(auto & node : originalData)
  305. {
  306. node.setMeta(CModHandler::scopeBuiltin());
  307. }
  308. }
  309. bool ContentTypeHandler::preloadModData(const std::string & modName, const std::vector<std::string> & fileList, bool validate)
  310. {
  311. bool result = false;
  312. JsonNode data = JsonUtils::assembleFromFiles(fileList, result);
  313. data.setMeta(modName);
  314. ModInfo & modInfo = modData[modName];
  315. for(auto entry : data.Struct())
  316. {
  317. size_t colon = entry.first.find(':');
  318. if (colon == std::string::npos)
  319. {
  320. // normal object, local to this mod
  321. modInfo.modData[entry.first].swap(entry.second);
  322. }
  323. else
  324. {
  325. std::string remoteName = entry.first.substr(0, colon);
  326. std::string objectName = entry.first.substr(colon + 1);
  327. // patching this mod? Send warning and continue - this situation can be handled normally
  328. if (remoteName == modName)
  329. logMod->warn("Redundant namespace definition for %s", objectName);
  330. logMod->trace("Patching object %s (%s) from %s", objectName, remoteName, modName);
  331. JsonNode & remoteConf = modData[remoteName].patches[objectName];
  332. JsonUtils::merge(remoteConf, entry.second);
  333. }
  334. }
  335. return result;
  336. }
  337. bool ContentTypeHandler::loadMod(const std::string & modName, bool validate)
  338. {
  339. ModInfo & modInfo = modData[modName];
  340. bool result = true;
  341. auto performValidate = [&,this](JsonNode & data, const std::string & name){
  342. handler->beforeValidate(data);
  343. if (validate)
  344. result &= JsonUtils::validate(data, "vcmi:" + objectName, name);
  345. };
  346. // apply patches
  347. if (!modInfo.patches.isNull())
  348. JsonUtils::merge(modInfo.modData, modInfo.patches);
  349. for(auto & entry : modInfo.modData.Struct())
  350. {
  351. const std::string & name = entry.first;
  352. JsonNode & data = entry.second;
  353. if (data.meta != modName)
  354. logMod->warn("Mod %s is attempting to inject object %s into mod %s! This may not be supported in future versions!", data.meta, name, modName);
  355. if (vstd::contains(data.Struct(), "index") && !data["index"].isNull())
  356. {
  357. if (modName != "core")
  358. logMod->warn("Mod %s is attempting to load original data! This should be reserved for built-in mod.", modName);
  359. // try to add H3 object data
  360. size_t index = static_cast<size_t>(data["index"].Float());
  361. if(originalData.size() > index)
  362. {
  363. logMod->trace("found original data in loadMod(%s) at index %d", name, index);
  364. JsonUtils::merge(originalData[index], data);
  365. std::swap(originalData[index], data);
  366. originalData[index].clear(); // do not use same data twice (same ID)
  367. }
  368. else
  369. {
  370. logMod->trace("no original data in loadMod(%s) at index %d", name, index);
  371. }
  372. performValidate(data, name);
  373. handler->loadObject(modName, name, data, index);
  374. }
  375. else
  376. {
  377. // normal new object
  378. logMod->trace("no index in loadMod(%s)", name);
  379. performValidate(data,name);
  380. handler->loadObject(modName, name, data);
  381. }
  382. }
  383. return result;
  384. }
  385. void ContentTypeHandler::loadCustom()
  386. {
  387. handler->loadCustom();
  388. }
  389. void ContentTypeHandler::afterLoadFinalization()
  390. {
  391. handler->afterLoadFinalization();
  392. }
  393. void CContentHandler::init()
  394. {
  395. handlers.insert(std::make_pair("heroClasses", ContentTypeHandler(&VLC->heroh->classes, "heroClass")));
  396. handlers.insert(std::make_pair("artifacts", ContentTypeHandler(VLC->arth, "artifact")));
  397. handlers.insert(std::make_pair("creatures", ContentTypeHandler(VLC->creh, "creature")));
  398. handlers.insert(std::make_pair("factions", ContentTypeHandler(VLC->townh, "faction")));
  399. handlers.insert(std::make_pair("objects", ContentTypeHandler(VLC->objtypeh, "object")));
  400. handlers.insert(std::make_pair("heroes", ContentTypeHandler(VLC->heroh, "hero")));
  401. handlers.insert(std::make_pair("spells", ContentTypeHandler(VLC->spellh, "spell")));
  402. handlers.insert(std::make_pair("skills", ContentTypeHandler(VLC->skillh, "skill")));
  403. handlers.insert(std::make_pair("templates", ContentTypeHandler(VLC->tplh, "template")));
  404. #if SCRIPTING_ENABLED
  405. handlers.insert(std::make_pair("scripts", ContentTypeHandler(VLC->scriptHandler, "script")));
  406. #endif
  407. handlers.insert(std::make_pair("battlefields", ContentTypeHandler(VLC->battlefieldsHandler, "battlefield")));
  408. handlers.insert(std::make_pair("terrains", ContentTypeHandler(VLC->terrainTypeHandler, "terrain")));
  409. handlers.insert(std::make_pair("rivers", ContentTypeHandler(VLC->riverTypeHandler, "river")));
  410. handlers.insert(std::make_pair("roads", ContentTypeHandler(VLC->roadTypeHandler, "road")));
  411. handlers.insert(std::make_pair("obstacles", ContentTypeHandler(VLC->obstacleHandler, "obstacle")));
  412. //TODO: any other types of moddables?
  413. }
  414. bool CContentHandler::preloadModData(const std::string & modName, JsonNode modConfig, bool validate)
  415. {
  416. bool result = true;
  417. for(auto & handler : handlers)
  418. {
  419. result &= handler.second.preloadModData(modName, modConfig[handler.first].convertTo<std::vector<std::string> >(), validate);
  420. }
  421. return result;
  422. }
  423. bool CContentHandler::loadMod(const std::string & modName, bool validate)
  424. {
  425. bool result = true;
  426. for(auto & handler : handlers)
  427. {
  428. result &= handler.second.loadMod(modName, validate);
  429. }
  430. return result;
  431. }
  432. void CContentHandler::loadCustom()
  433. {
  434. for(auto & handler : handlers)
  435. {
  436. handler.second.loadCustom();
  437. }
  438. }
  439. void CContentHandler::afterLoadFinalization()
  440. {
  441. for(auto & handler : handlers)
  442. {
  443. handler.second.afterLoadFinalization();
  444. }
  445. }
  446. void CContentHandler::preloadData(CModInfo & mod)
  447. {
  448. bool validate = (mod.validation != CModInfo::PASSED);
  449. // print message in format [<8-symbols checksum>] <modname>
  450. logMod->info("\t\t[%08x]%s", mod.checksum, mod.name);
  451. if (validate && mod.identifier != CModHandler::scopeBuiltin())
  452. {
  453. if (!JsonUtils::validate(mod.config, "vcmi:mod", mod.identifier))
  454. mod.validation = CModInfo::FAILED;
  455. }
  456. if (!preloadModData(mod.identifier, mod.config, validate))
  457. mod.validation = CModInfo::FAILED;
  458. }
  459. void CContentHandler::load(CModInfo & mod)
  460. {
  461. bool validate = (mod.validation != CModInfo::PASSED);
  462. if (!loadMod(mod.identifier, validate))
  463. mod.validation = CModInfo::FAILED;
  464. if (validate)
  465. {
  466. if (mod.validation != CModInfo::FAILED)
  467. logMod->info("\t\t[DONE] %s", mod.name);
  468. else
  469. logMod->error("\t\t[FAIL] %s", mod.name);
  470. }
  471. else
  472. logMod->info("\t\t[SKIP] %s", mod.name);
  473. }
  474. const ContentTypeHandler & CContentHandler::operator[](const std::string & name) const
  475. {
  476. return handlers.at(name);
  477. }
  478. static JsonNode loadModSettings(const std::string & path)
  479. {
  480. if (CResourceHandler::get("local")->existsResource(ResourceID(path)))
  481. {
  482. return JsonNode(ResourceID(path, EResType::TEXT));
  483. }
  484. // Probably new install. Create initial configuration
  485. CResourceHandler::get("local")->createResource(path);
  486. return JsonNode();
  487. }
  488. JsonNode addMeta(JsonNode config, const std::string & meta)
  489. {
  490. config.setMeta(meta);
  491. return config;
  492. }
  493. CModInfo::CModInfo():
  494. checksum(0),
  495. explicitlyEnabled(false),
  496. implicitlyEnabled(true),
  497. validation(PENDING)
  498. {
  499. }
  500. CModInfo::CModInfo(const std::string & identifier, const JsonNode & local, const JsonNode & config):
  501. identifier(identifier),
  502. name(config["name"].String()),
  503. description(config["description"].String()),
  504. dependencies(config["depends"].convertTo<std::set<std::string>>()),
  505. conflicts(config["conflicts"].convertTo<std::set<std::string>>()),
  506. checksum(0),
  507. explicitlyEnabled(false),
  508. implicitlyEnabled(true),
  509. validation(PENDING),
  510. config(addMeta(config, identifier))
  511. {
  512. version = CModVersion::fromString(config["version"].String());
  513. if(!config["compatibility"].isNull())
  514. {
  515. vcmiCompatibleMin = CModVersion::fromString(config["compatibility"]["min"].String());
  516. vcmiCompatibleMax = CModVersion::fromString(config["compatibility"]["max"].String());
  517. }
  518. if (!config["language"].isNull())
  519. baseLanguage = config["language"].String();
  520. else
  521. baseLanguage = "english";
  522. loadLocalData(local);
  523. }
  524. JsonNode CModInfo::saveLocalData() const
  525. {
  526. std::ostringstream stream;
  527. stream << std::noshowbase << std::hex << std::setw(8) << std::setfill('0') << checksum;
  528. JsonNode conf;
  529. conf["active"].Bool() = explicitlyEnabled;
  530. conf["validated"].Bool() = validation != FAILED;
  531. conf["checksum"].String() = stream.str();
  532. return conf;
  533. }
  534. std::string CModInfo::getModDir(const std::string & name)
  535. {
  536. return "MODS/" + boost::algorithm::replace_all_copy(name, ".", "/MODS/");
  537. }
  538. std::string CModInfo::getModFile(const std::string & name)
  539. {
  540. return getModDir(name) + "/mod.json";
  541. }
  542. void CModInfo::updateChecksum(ui32 newChecksum)
  543. {
  544. // comment-out next line to force validation of all mods ignoring checksum
  545. if (newChecksum != checksum)
  546. {
  547. checksum = newChecksum;
  548. validation = PENDING;
  549. }
  550. }
  551. void CModInfo::loadLocalData(const JsonNode & data)
  552. {
  553. bool validated = false;
  554. implicitlyEnabled = true;
  555. explicitlyEnabled = !config["keepDisabled"].Bool();
  556. checksum = 0;
  557. if (data.getType() == JsonNode::JsonType::DATA_BOOL)
  558. {
  559. explicitlyEnabled = data.Bool();
  560. }
  561. if (data.getType() == JsonNode::JsonType::DATA_STRUCT)
  562. {
  563. explicitlyEnabled = data["active"].Bool();
  564. validated = data["validated"].Bool();
  565. checksum = strtol(data["checksum"].String().c_str(), nullptr, 16);
  566. }
  567. //check compatibility
  568. implicitlyEnabled &= (vcmiCompatibleMin.isNull() || CModVersion::GameVersion().compatible(vcmiCompatibleMin));
  569. implicitlyEnabled &= (vcmiCompatibleMax.isNull() || vcmiCompatibleMax.compatible(CModVersion::GameVersion()));
  570. if(!implicitlyEnabled)
  571. logGlobal->warn("Mod %s is incompatible with current version of VCMI and cannot be enabled", name);
  572. if (boost::iequals(config["modType"].String(), "translation")) // compatibility code - mods use "Translation" type at the moment
  573. {
  574. if (baseLanguage != VLC->generaltexth->getPreferredLanguage())
  575. {
  576. logGlobal->warn("Translation mod %s was not loaded: language mismatch!", name);
  577. implicitlyEnabled = false;
  578. }
  579. }
  580. if (isEnabled())
  581. validation = validated ? PASSED : PENDING;
  582. else
  583. validation = validated ? PASSED : FAILED;
  584. }
  585. bool CModInfo::isEnabled() const
  586. {
  587. return implicitlyEnabled && explicitlyEnabled;
  588. }
  589. void CModInfo::setEnabled(bool on)
  590. {
  591. explicitlyEnabled = on;
  592. }
  593. CModHandler::CModHandler() : content(std::make_shared<CContentHandler>())
  594. {
  595. //TODO: moddable spell schools
  596. for (auto i = 0; i < GameConstants::DEFAULT_SCHOOLS; ++i)
  597. identifiers.registerObject(CModHandler::scopeBuiltin(), "spellSchool", SpellConfig::SCHOOL[i].jsonName, SpellConfig::SCHOOL[i].id);
  598. identifiers.registerObject(CModHandler::scopeBuiltin(), "spellSchool", "any", SpellSchool(ESpellSchool::ANY));
  599. for (int i = 0; i < GameConstants::RESOURCE_QUANTITY; ++i)
  600. {
  601. identifiers.registerObject(CModHandler::scopeBuiltin(), "resource", GameConstants::RESOURCE_NAMES[i], i);
  602. }
  603. for(int i=0; i<GameConstants::PRIMARY_SKILLS; ++i)
  604. {
  605. identifiers.registerObject(CModHandler::scopeBuiltin(), "primSkill", PrimarySkill::names[i], i);
  606. identifiers.registerObject(CModHandler::scopeBuiltin(), "primarySkill", PrimarySkill::names[i], i);
  607. }
  608. }
  609. // currentList is passed by value to get current list of depending mods
  610. bool CModHandler::hasCircularDependency(const TModID & modID, std::set<TModID> currentList) const
  611. {
  612. const CModInfo & mod = allMods.at(modID);
  613. // Mod already present? We found a loop
  614. if (vstd::contains(currentList, modID))
  615. {
  616. logMod->error("Error: Circular dependency detected! Printing dependency list:");
  617. logMod->error("\t%s -> ", mod.name);
  618. return true;
  619. }
  620. currentList.insert(modID);
  621. // recursively check every dependency of this mod
  622. for(const TModID & dependency : mod.dependencies)
  623. {
  624. if (hasCircularDependency(dependency, currentList))
  625. {
  626. logMod->error("\t%s ->\n", mod.name); // conflict detected, print dependency list
  627. return true;
  628. }
  629. }
  630. return false;
  631. }
  632. // Returned vector affects the resource loaders call order (see CFilesystemList::load).
  633. // The loaders call order matters when dependent mod overrides resources in its dependencies.
  634. std::vector <TModID> CModHandler::validateAndSortDependencies(std::vector <TModID> modsToResolve) const
  635. {
  636. // Topological sort algorithm.
  637. // TODO: Investigate possible ways to improve performance.
  638. boost::range::sort(modsToResolve); // Sort mods per name
  639. std::vector <TModID> sortedValidMods; // Vector keeps order of elements (LIFO)
  640. sortedValidMods.reserve(modsToResolve.size()); // push_back calls won't cause memory reallocation
  641. std::set <TModID> resolvedModIDs; // Use a set for validation for performance reason, but set does not keep order of elements
  642. // Mod is resolved if it has not dependencies or all its dependencies are already resolved
  643. auto isResolved = [&](const CModInfo & mod) -> CModInfo::EValidationStatus
  644. {
  645. if(mod.dependencies.size() > resolvedModIDs.size())
  646. return CModInfo::PENDING;
  647. for(const TModID & dependency : mod.dependencies)
  648. {
  649. if(!vstd::contains(resolvedModIDs, dependency))
  650. return CModInfo::PENDING;
  651. }
  652. return CModInfo::PASSED;
  653. };
  654. while(true)
  655. {
  656. std::set <TModID> resolvedOnCurrentTreeLevel;
  657. for(auto it = modsToResolve.begin(); it != modsToResolve.end();) // One iteration - one level of mods tree
  658. {
  659. if(isResolved(allMods.at(*it)) == CModInfo::PASSED)
  660. {
  661. resolvedOnCurrentTreeLevel.insert(*it); // Not to the resolvedModIDs, so current node childs will be resolved on the next iteration
  662. sortedValidMods.push_back(*it);
  663. it = modsToResolve.erase(it);
  664. continue;
  665. }
  666. it++;
  667. }
  668. if(!resolvedOnCurrentTreeLevel.empty())
  669. {
  670. resolvedModIDs.insert(resolvedOnCurrentTreeLevel.begin(), resolvedOnCurrentTreeLevel.end());
  671. continue;
  672. }
  673. // If there're no valid mods on the current mods tree level, no more mod can be resolved, should be end.
  674. break;
  675. }
  676. // Left mods have unresolved dependencies, output all to log.
  677. for(const auto & brokenModID : modsToResolve)
  678. {
  679. const CModInfo & brokenMod = allMods.at(brokenModID);
  680. for(const TModID & dependency : brokenMod.dependencies)
  681. {
  682. if(!vstd::contains(resolvedModIDs, dependency))
  683. logMod->error("Mod '%s' will not work: it depends on mod '%s', which is not installed.", brokenMod.name, dependency);
  684. }
  685. }
  686. return sortedValidMods;
  687. }
  688. std::vector<std::string> CModHandler::getModList(const std::string & path) const
  689. {
  690. std::string modDir = boost::to_upper_copy(path + "MODS/");
  691. size_t depth = boost::range::count(modDir, '/');
  692. auto list = CResourceHandler::get("initial")->getFilteredFiles([&](const ResourceID & id) -> bool
  693. {
  694. if (id.getType() != EResType::DIRECTORY)
  695. return false;
  696. if (!boost::algorithm::starts_with(id.getName(), modDir))
  697. return false;
  698. if (boost::range::count(id.getName(), '/') != depth )
  699. return false;
  700. return true;
  701. });
  702. //storage for found mods
  703. std::vector<std::string> foundMods;
  704. for(const auto & entry : list)
  705. {
  706. std::string name = entry.getName();
  707. name.erase(0, modDir.size()); //Remove path prefix
  708. if (!name.empty())
  709. foundMods.push_back(name);
  710. }
  711. return foundMods;
  712. }
  713. bool CModHandler::isScopeReserved(const TModID & scope)
  714. {
  715. //following scopes are reserved - either in use by mod system or by filesystem
  716. static const std::array<TModID, 9> reservedScopes = {
  717. "core", "map", "game", "root", "saves", "config", "local", "initial", "mapEditor"
  718. };
  719. return std::find(reservedScopes.begin(), reservedScopes.end(), scope) != reservedScopes.end();
  720. }
  721. const TModID & CModHandler::scopeBuiltin()
  722. {
  723. static const TModID scope = "core";
  724. return scope;
  725. }
  726. const TModID & CModHandler::scopeGame()
  727. {
  728. static const TModID scope = "game";
  729. return scope;
  730. }
  731. const TModID & CModHandler::scopeMap()
  732. {
  733. //TODO: implement accessing map dependencies for both H3 and VCMI maps
  734. // for now, allow access to any identifiers
  735. static const TModID scope = "game";
  736. return scope;
  737. }
  738. void CModHandler::loadMods(const std::string & path, const std::string & parent, const JsonNode & modSettings, bool enableMods)
  739. {
  740. for(const std::string & modName : getModList(path))
  741. loadOneMod(modName, parent, modSettings, enableMods);
  742. }
  743. void CModHandler::loadOneMod(std::string modName, const std::string & parent, const JsonNode & modSettings, bool enableMods)
  744. {
  745. boost::to_lower(modName);
  746. std::string modFullName = parent.empty() ? modName : parent + '.' + modName;
  747. if ( isScopeReserved(modFullName))
  748. {
  749. logMod->error("Can not load mod %s - this name is reserved for internal use!", modFullName);
  750. return;
  751. }
  752. if(CResourceHandler::get("initial")->existsResource(ResourceID(CModInfo::getModFile(modFullName))))
  753. {
  754. CModInfo mod(modFullName, modSettings[modName], JsonNode(ResourceID(CModInfo::getModFile(modFullName))));
  755. if (!parent.empty()) // this is submod, add parent to dependencies
  756. mod.dependencies.insert(parent);
  757. allMods[modFullName] = mod;
  758. if (mod.isEnabled() && enableMods)
  759. activeMods.push_back(modFullName);
  760. loadMods(CModInfo::getModDir(modFullName) + '/', modFullName, modSettings[modName]["mods"], enableMods && mod.isEnabled());
  761. }
  762. }
  763. void CModHandler::loadMods(bool onlyEssential)
  764. {
  765. JsonNode modConfig;
  766. if(onlyEssential)
  767. {
  768. loadOneMod("vcmi", "", modConfig, true);//only vcmi and submods
  769. }
  770. else
  771. {
  772. modConfig = loadModSettings("config/modSettings.json");
  773. loadMods("", "", modConfig["activeMods"], true);
  774. }
  775. coreMod = CModInfo(CModHandler::scopeBuiltin(), modConfig[CModHandler::scopeBuiltin()], JsonNode(ResourceID("config/gameConfig.json")));
  776. coreMod.name = "Original game files";
  777. }
  778. std::vector<std::string> CModHandler::getAllMods()
  779. {
  780. std::vector<std::string> modlist;
  781. modlist.reserve(allMods.size());
  782. for (auto & entry : allMods)
  783. modlist.push_back(entry.first);
  784. return modlist;
  785. }
  786. std::vector<std::string> CModHandler::getActiveMods()
  787. {
  788. return activeMods;
  789. }
  790. const CModInfo & CModHandler::getModInfo(const TModID & modId) const
  791. {
  792. return allMods.at(modId);
  793. }
  794. static JsonNode genDefaultFS()
  795. {
  796. // default FS config for mods: directory "Content" that acts as H3 root directory
  797. JsonNode defaultFS;
  798. defaultFS[""].Vector().resize(2);
  799. defaultFS[""].Vector()[0]["type"].String() = "zip";
  800. defaultFS[""].Vector()[0]["path"].String() = "/Content.zip";
  801. defaultFS[""].Vector()[1]["type"].String() = "dir";
  802. defaultFS[""].Vector()[1]["path"].String() = "/Content";
  803. return defaultFS;
  804. }
  805. static ISimpleResourceLoader * genModFilesystem(const std::string & modName, const JsonNode & conf)
  806. {
  807. static const JsonNode defaultFS = genDefaultFS();
  808. if (!conf["filesystem"].isNull())
  809. return CResourceHandler::createFileSystem(CModInfo::getModDir(modName), conf["filesystem"]);
  810. else
  811. return CResourceHandler::createFileSystem(CModInfo::getModDir(modName), defaultFS);
  812. }
  813. static ui32 calculateModChecksum(const std::string & modName, ISimpleResourceLoader * filesystem)
  814. {
  815. boost::crc_32_type modChecksum;
  816. // first - add current VCMI version into checksum to force re-validation on VCMI updates
  817. modChecksum.process_bytes(reinterpret_cast<const void*>(GameConstants::VCMI_VERSION.data()), GameConstants::VCMI_VERSION.size());
  818. // second - add mod.json into checksum because filesystem does not contains this file
  819. // FIXME: remove workaround for core mod
  820. if (modName != CModHandler::scopeBuiltin())
  821. {
  822. ResourceID modConfFile(CModInfo::getModFile(modName), EResType::TEXT);
  823. ui32 configChecksum = CResourceHandler::get("initial")->load(modConfFile)->calculateCRC32();
  824. modChecksum.process_bytes(reinterpret_cast<const void *>(&configChecksum), sizeof(configChecksum));
  825. }
  826. // third - add all detected text files from this mod into checksum
  827. auto files = filesystem->getFilteredFiles([](const ResourceID & resID)
  828. {
  829. return resID.getType() == EResType::TEXT &&
  830. ( boost::starts_with(resID.getName(), "DATA") ||
  831. boost::starts_with(resID.getName(), "CONFIG"));
  832. });
  833. for (const ResourceID & file : files)
  834. {
  835. ui32 fileChecksum = filesystem->load(file)->calculateCRC32();
  836. modChecksum.process_bytes(reinterpret_cast<const void *>(&fileChecksum), sizeof(fileChecksum));
  837. }
  838. return modChecksum.checksum();
  839. }
  840. void CModHandler::loadModFilesystems()
  841. {
  842. CGeneralTextHandler::detectInstallParameters();
  843. activeMods = validateAndSortDependencies(activeMods);
  844. coreMod.updateChecksum(calculateModChecksum(CModHandler::scopeBuiltin(), CResourceHandler::get(CModHandler::scopeBuiltin())));
  845. for(std::string & modName : activeMods)
  846. {
  847. CModInfo & mod = allMods[modName];
  848. CResourceHandler::addFilesystem("data", modName, genModFilesystem(modName, mod.config));
  849. }
  850. }
  851. TModID CModHandler::findResourceOrigin(const ResourceID & name)
  852. {
  853. for(const auto & modID : boost::adaptors::reverse(activeMods))
  854. {
  855. if(CResourceHandler::get(modID)->existsResource(name))
  856. return modID;
  857. }
  858. if(CResourceHandler::get("core")->existsResource(name))
  859. return "core";
  860. if(CResourceHandler::get("mapEditor")->existsResource(name))
  861. return "core"; // Workaround for loading maps via map editor
  862. assert(0);
  863. return "";
  864. }
  865. std::string CModHandler::getModLanguage(const TModID& modId) const
  866. {
  867. if ( modId == "core")
  868. return VLC->generaltexth->getInstalledLanguage();
  869. return allMods.at(modId).baseLanguage;
  870. }
  871. std::set<TModID> CModHandler::getModDependencies(const TModID & modId, bool & isModFound) const
  872. {
  873. auto it = allMods.find(modId);
  874. isModFound = (it != allMods.end());
  875. if(isModFound)
  876. return it->second.dependencies;
  877. logMod->error("Mod not found: '%s'", modId);
  878. return {};
  879. }
  880. void CModHandler::initializeConfig()
  881. {
  882. VLC->settingsHandler->load(coreMod.config["settings"]);
  883. for(const TModID & modName : activeMods)
  884. {
  885. const auto & mod = allMods[modName];
  886. if (!mod.config["settings"].isNull())
  887. VLC->settingsHandler->load(mod.config["settings"]);
  888. }
  889. }
  890. bool CModHandler::validateTranslations(TModID modName) const
  891. {
  892. bool result = true;
  893. const auto & mod = allMods.at(modName);
  894. {
  895. auto fileList = mod.config["translations"].convertTo<std::vector<std::string> >();
  896. JsonNode json = JsonUtils::assembleFromFiles(fileList);
  897. result |= VLC->generaltexth->validateTranslation(mod.baseLanguage, modName, json);
  898. }
  899. for(const auto & language : Languages::getLanguageList())
  900. {
  901. if (!language.hasTranslation)
  902. continue;
  903. if (mod.config[language.identifier].isNull())
  904. continue;
  905. if (mod.config[language.identifier]["skipValidation"].Bool())
  906. continue;
  907. auto fileList = mod.config[language.identifier]["translations"].convertTo<std::vector<std::string> >();
  908. JsonNode json = JsonUtils::assembleFromFiles(fileList);
  909. result |= VLC->generaltexth->validateTranslation(language.identifier, modName, json);
  910. }
  911. return result;
  912. }
  913. void CModHandler::loadTranslation(const TModID & modName)
  914. {
  915. const auto & mod = allMods[modName];
  916. std::string preferredLanguage = VLC->generaltexth->getPreferredLanguage();
  917. std::string modBaseLanguage = allMods[modName].baseLanguage;
  918. auto baseTranslationList = mod.config["translations"].convertTo<std::vector<std::string> >();
  919. auto extraTranslationList = mod.config[preferredLanguage]["translations"].convertTo<std::vector<std::string> >();
  920. JsonNode baseTranslation = JsonUtils::assembleFromFiles(baseTranslationList);
  921. JsonNode extraTranslation = JsonUtils::assembleFromFiles(extraTranslationList);
  922. VLC->generaltexth->loadTranslationOverrides(modBaseLanguage, modName, baseTranslation);
  923. VLC->generaltexth->loadTranslationOverrides(preferredLanguage, modName, extraTranslation);
  924. }
  925. void CModHandler::load()
  926. {
  927. CStopWatch totalTime;
  928. CStopWatch timer;
  929. logMod->info("\tInitializing content handler: %d ms", timer.getDiff());
  930. content->init();
  931. for(const TModID & modName : activeMods)
  932. {
  933. logMod->trace("Generating checksum for %s", modName);
  934. allMods[modName].updateChecksum(calculateModChecksum(modName, CResourceHandler::get(modName)));
  935. }
  936. // first - load virtual builtin mod that contains all data
  937. // TODO? move all data into real mods? RoE, AB, SoD, WoG
  938. content->preloadData(coreMod);
  939. for(const TModID & modName : activeMods)
  940. content->preloadData(allMods[modName]);
  941. logMod->info("\tParsing mod data: %d ms", timer.getDiff());
  942. content->load(coreMod);
  943. for(const TModID & modName : activeMods)
  944. content->load(allMods[modName]);
  945. #if SCRIPTING_ENABLED
  946. VLC->scriptHandler->performRegistration(VLC);//todo: this should be done before any other handlers load
  947. #endif
  948. content->loadCustom();
  949. for(const TModID & modName : activeMods)
  950. loadTranslation(modName);
  951. for(const TModID & modName : activeMods)
  952. if (!validateTranslations(modName))
  953. allMods[modName].validation = CModInfo::FAILED;
  954. logMod->info("\tLoading mod data: %d ms", timer.getDiff());
  955. VLC->creh->loadCrExpMod();
  956. identifiers.finalize();
  957. logMod->info("\tResolving identifiers: %d ms", timer.getDiff());
  958. content->afterLoadFinalization();
  959. logMod->info("\tHandlers post-load finalization: %d ms ", timer.getDiff());
  960. logMod->info("\tAll game content loaded in %d ms", totalTime.getDiff());
  961. }
  962. void CModHandler::afterLoad(bool onlyEssential)
  963. {
  964. JsonNode modSettings;
  965. for (auto & modEntry : allMods)
  966. {
  967. std::string pointer = "/" + boost::algorithm::replace_all_copy(modEntry.first, ".", "/mods/");
  968. modSettings["activeMods"].resolvePointer(pointer) = modEntry.second.saveLocalData();
  969. }
  970. modSettings[CModHandler::scopeBuiltin()] = coreMod.saveLocalData();
  971. if(!onlyEssential)
  972. {
  973. FileStream file(*CResourceHandler::get()->getResourceName(ResourceID("config/modSettings.json")), std::ofstream::out | std::ofstream::trunc);
  974. file << modSettings.toJson();
  975. }
  976. }
  977. std::string CModHandler::normalizeIdentifier(const std::string & scope, const std::string & remoteScope, const std::string & identifier)
  978. {
  979. auto p = vstd::splitStringToPair(identifier, ':');
  980. if(p.first.empty())
  981. p.first = scope;
  982. if(p.first == remoteScope)
  983. p.first.clear();
  984. return p.first.empty() ? p.second : p.first + ":" + p.second;
  985. }
  986. void CModHandler::parseIdentifier(const std::string & fullIdentifier, std::string & scope, std::string & type, std::string & identifier)
  987. {
  988. auto p = vstd::splitStringToPair(fullIdentifier, ':');
  989. scope = p.first;
  990. auto p2 = vstd::splitStringToPair(p.second, '.');
  991. if(!p2.first.empty())
  992. {
  993. type = p2.first;
  994. identifier = p2.second;
  995. }
  996. else
  997. {
  998. type = p.second;
  999. identifier.clear();
  1000. }
  1001. }
  1002. std::string CModHandler::makeFullIdentifier(const std::string & scope, const std::string & type, const std::string & identifier)
  1003. {
  1004. if(type.empty())
  1005. logGlobal->error("Full identifier (%s %s) requires type name", scope, identifier);
  1006. std::string actualScope = scope;
  1007. std::string actualName = identifier;
  1008. //ignore scope if identifier is scoped
  1009. auto scopeAndName = vstd::splitStringToPair(identifier, ':');
  1010. if(!scopeAndName.first.empty())
  1011. {
  1012. actualScope = scopeAndName.first;
  1013. actualName = scopeAndName.second;
  1014. }
  1015. if(actualScope.empty())
  1016. {
  1017. return actualName.empty() ? type : type + "." + actualName;
  1018. }
  1019. else
  1020. {
  1021. return actualName.empty() ? actualScope+ ":" + type : actualScope + ":" + type + "." + actualName;
  1022. }
  1023. }
  1024. VCMI_LIB_NAMESPACE_END