IdentifierStorage.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*
  2. * IdentifierStorage.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 "IdentifierStorage.h"
  12. #include "CModHandler.h"
  13. #include "ModScope.h"
  14. #include "../GameLibrary.h"
  15. #include "../constants/StringConstants.h"
  16. #include "../spells/CSpellHandler.h"
  17. #include <vstd/StringUtils.h>
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. CIdentifierStorage::CIdentifierStorage()
  20. {
  21. registerObject(ModScope::scopeBuiltin(), "spellSchool", "any", SpellSchool::ANY.getNum());
  22. for (int i = 0; i < GameConstants::RESOURCE_QUANTITY; ++i)
  23. registerObject(ModScope::scopeBuiltin(), "resource", GameConstants::RESOURCE_NAMES[i], i);
  24. for (int i = 0; i < std::size(GameConstants::PLAYER_COLOR_NAMES); ++i)
  25. registerObject(ModScope::scopeBuiltin(), "playerColor", GameConstants::PLAYER_COLOR_NAMES[i], i);
  26. for(int i=0; i<GameConstants::PRIMARY_SKILLS; ++i)
  27. {
  28. registerObject(ModScope::scopeBuiltin(), "primSkill", NPrimarySkill::names[i], i);
  29. registerObject(ModScope::scopeBuiltin(), "primarySkill", NPrimarySkill::names[i], i);
  30. }
  31. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "creatureDamageBoth", 0);
  32. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "creatureDamageMin", 1);
  33. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "creatureDamageMax", 2);
  34. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "damageTypeAll", -1);
  35. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "damageTypeMelee", 0);
  36. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "damageTypeRanged", 1);
  37. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "heroMovementLand", 1);
  38. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "heroMovementSea", 0);
  39. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "deathStareGorgon", 0);
  40. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "deathStareCommander", 1);
  41. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "deathStareNoRangePenalty", 2);
  42. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "deathStareRangePenalty", 3);
  43. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "deathStareObstaclePenalty", 4);
  44. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "deathStareRangeObstaclePenalty", 5);
  45. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "rebirthRegular", 0);
  46. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "rebirthSpecial", 1);
  47. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "visionsMonsters", 0);
  48. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "visionsHeroes", 1);
  49. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "visionsTowns", 2);
  50. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "immunityBattleWide", 0);
  51. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "immunityEnemyHero", 1);
  52. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "transmutationPerHealth", 0);
  53. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "transmutationPerUnit", 1);
  54. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "destructionKillPercentage", 0);
  55. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "destructionKillAmount", 1);
  56. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "soulStealPermanent", 0);
  57. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "soulStealBattle", 1);
  58. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "movementFlying", -1);
  59. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "movementTeleporting", 1);
  60. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "spellLevel1", 1);
  61. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "spellLevel2", 2);
  62. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "spellLevel3", 3);
  63. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "spellLevel4", 4);
  64. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "spellLevel5", 5);
  65. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "creatureLevel1", 1);
  66. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "creatureLevel2", 2);
  67. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "creatureLevel3", 3);
  68. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "creatureLevel4", 4);
  69. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "creatureLevel5", 5);
  70. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "creatureLevel6", 6);
  71. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "creatureLevel7", 7);
  72. registerObject(ModScope::scopeBuiltin(), "spell", "preset", SpellID::PRESET);
  73. registerObject(ModScope::scopeBuiltin(), "spell", "spellbook_preset", SpellID::SPELLBOOK_PRESET);
  74. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "combatEventBeforeAttack", static_cast<int>(CombatEventType::BEFORE_ATTACK));
  75. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "combatEventAfterAttack", static_cast<int>(CombatEventType::AFTER_ATTACK));
  76. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "combatEventBeforeAttacked", static_cast<int>(CombatEventType::BEFORE_ATTACKED));
  77. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "combatEventAfterAttacked", static_cast<int>(CombatEventType::AFTER_ATTACKED));
  78. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "combatEventWait", static_cast<int>(CombatEventType::WAIT));
  79. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "combatEventDefend", static_cast<int>(CombatEventType::DEFEND));
  80. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "combatEventBeforeMove", static_cast<int>(CombatEventType::BEFORE_MOVE));
  81. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "combatEventAfterMove", static_cast<int>(CombatEventType::AFTER_MOVE));
  82. registerObject(ModScope::scopeBuiltin(), "bonusSubtype", "combatEventCast", static_cast<int>(CombatEventType::UNIT_SPELLCAST));
  83. }
  84. void CIdentifierStorage::checkIdentifier(const std::string & ID)
  85. {
  86. if (boost::algorithm::ends_with(ID, "."))
  87. logMod->error("BIG WARNING: identifier %s seems to be broken!", ID);
  88. }
  89. void CIdentifierStorage::requestIdentifier(const ObjectCallback & callback) const
  90. {
  91. checkIdentifier(callback.type);
  92. checkIdentifier(callback.name);
  93. assert(!callback.localScope.empty());
  94. if (state != ELoadingState::FINISHED) // enqueue request if loading is still in progress
  95. scheduledRequests.push_back(callback);
  96. else // execute immediately for "late" requests
  97. resolveIdentifier(callback);
  98. }
  99. CIdentifierStorage::ObjectCallback CIdentifierStorage::ObjectCallback::fromNameWithType(const std::string & scope, const std::string & fullName, const std::function<void(si32)> & callback, bool optional, bool caseSensitive)
  100. {
  101. assert(!scope.empty());
  102. auto scopeAndFullName = vstd::splitStringToPair(fullName, ':');
  103. auto typeAndName = vstd::splitStringToPair(scopeAndFullName.second, '.');
  104. if (scope == scopeAndFullName.first)
  105. logMod->debug("Target scope for identifier '%s' is redundant! Identifier already defined in mod '%s'", fullName, scope);
  106. ObjectCallback result;
  107. result.localScope = scope;
  108. result.remoteScope = scopeAndFullName.first;
  109. result.type = typeAndName.first;
  110. result.name = typeAndName.second;
  111. result.callback = callback;
  112. result.optional = optional;
  113. result.dynamicType = true;
  114. result.caseSensitive = caseSensitive;
  115. return result;
  116. }
  117. 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, bool bypassDependenciesCheck, bool caseSensitive)
  118. {
  119. assert(!scope.empty());
  120. auto scopeAndFullName = vstd::splitStringToPair(fullName, ':');
  121. auto typeAndName = vstd::splitStringToPair(scopeAndFullName.second, '.');
  122. if(!typeAndName.first.empty())
  123. {
  124. if (typeAndName.first != type)
  125. logMod->warn("Identifier '%s' from mod '%s' requested with different type! Type '%s' expected!", fullName, scope, type);
  126. else
  127. logMod->debug("Target type for identifier '%s' defined in mod '%s' is redundant!", fullName, scope);
  128. }
  129. if (scope == scopeAndFullName.first)
  130. logMod->debug("Target scope for identifier '%s' is redundant! Identifier already defined in mod '%s'", fullName, scope);
  131. ObjectCallback result;
  132. result.localScope = boost::to_lower_copy(scope);
  133. result.remoteScope = boost::to_lower_copy(scopeAndFullName.first);
  134. result.type = type;
  135. result.name = typeAndName.second;
  136. result.callback = callback;
  137. result.optional = optional;
  138. result.bypassDependenciesCheck = bypassDependenciesCheck;
  139. result.dynamicType = false;
  140. result.caseSensitive = caseSensitive;
  141. return result;
  142. }
  143. void CIdentifierStorage::requestIdentifier(const std::string & scope, const std::string & type, const std::string & name, const std::function<void(si32)> & callback) const
  144. {
  145. requestIdentifier(ObjectCallback::fromNameAndType(scope, type, name, callback, false, false, true));
  146. }
  147. void CIdentifierStorage::requestIdentifier(const std::string & scope, const std::string & fullName, const std::function<void(si32)> & callback) const
  148. {
  149. requestIdentifier(ObjectCallback::fromNameWithType(scope, fullName, callback, false, true));
  150. }
  151. void CIdentifierStorage::requestIdentifier(const std::string & type, const JsonNode & name, const std::function<void(si32)> & callback) const
  152. {
  153. requestIdentifier(ObjectCallback::fromNameAndType(name.getModScope(), type, name.String(), callback, false, false, true));
  154. }
  155. void CIdentifierStorage::requestIdentifier(const JsonNode & name, const std::function<void(si32)> & callback) const
  156. {
  157. requestIdentifier(ObjectCallback::fromNameWithType(name.getModScope(), name.String(), callback, false, true));
  158. }
  159. void CIdentifierStorage::requestIdentifierIfNotNull(const std::string & type, const JsonNode & name, const std::function<void(si32)> & callback) const
  160. {
  161. if (!name.isNull())
  162. requestIdentifier(type, name, callback);
  163. }
  164. void CIdentifierStorage::requestIdentifierIfFound(const std::string & type, const JsonNode & name, const std::function<void(si32)> & callback) const
  165. {
  166. requestIdentifier(ObjectCallback::fromNameAndType(name.getModScope(), type, name.String(), callback, false, true, true));
  167. }
  168. void CIdentifierStorage::requestIdentifierIfFound(const std::string & scope, const std::string & type, const std::string & name, const std::function<void(si32)> & callback) const
  169. {
  170. requestIdentifier(ObjectCallback::fromNameAndType(scope, type, name, callback, false, true, true));
  171. }
  172. void CIdentifierStorage::tryRequestIdentifier(const std::string & scope, const std::string & type, const std::string & name, const std::function<void(si32)> & callback) const
  173. {
  174. requestIdentifier(ObjectCallback::fromNameAndType(scope, type, name, callback, true, false, true));
  175. }
  176. void CIdentifierStorage::tryRequestIdentifier(const std::string & type, const JsonNode & name, const std::function<void(si32)> & callback) const
  177. {
  178. requestIdentifier(ObjectCallback::fromNameAndType(name.getModScope(), type, name.String(), callback, true, false, true));
  179. }
  180. std::optional<si32> CIdentifierStorage::getIdentifier(const std::string & scope, const std::string & type, const std::string & name, bool silent) const
  181. {
  182. //assert(state != ELoadingState::LOADING);
  183. auto options = ObjectCallback::fromNameAndType(scope, type, name, std::function<void(si32)>(), silent, false, true);
  184. return getIdentifierImpl(options, silent);
  185. }
  186. std::optional<si32> CIdentifierStorage::getIdentifier(const std::string & type, const JsonNode & name, bool silent) const
  187. {
  188. assert(state != ELoadingState::LOADING);
  189. auto options = ObjectCallback::fromNameAndType(name.getModScope(), type, name.String(), std::function<void(si32)>(), silent, false, true);
  190. return getIdentifierImpl(options, silent);
  191. }
  192. std::optional<si32> CIdentifierStorage::getIdentifier(const JsonNode & name, bool silent) const
  193. {
  194. assert(state != ELoadingState::LOADING);
  195. auto options = ObjectCallback::fromNameWithType(name.getModScope(), name.String(), std::function<void(si32)>(), silent, true);
  196. return getIdentifierImpl(options, silent);
  197. }
  198. std::optional<si32> CIdentifierStorage::getIdentifier(const std::string & scope, const std::string & fullName, bool silent) const
  199. {
  200. assert(state != ELoadingState::LOADING);
  201. auto options = ObjectCallback::fromNameWithType(scope, fullName, std::function<void(si32)>(), silent, true);
  202. return getIdentifierImpl(options, silent);
  203. }
  204. std::optional<si32> CIdentifierStorage::getIdentifierCaseInsensitive(const std::string & scope, const std::string & type, const std::string & name, bool silent) const
  205. {
  206. auto options = ObjectCallback::fromNameAndType(scope, type, name, std::function<void(si32)>(), silent, false, false);
  207. return getIdentifierImpl(options, silent);
  208. }
  209. std::optional<si32> CIdentifierStorage::getIdentifierImpl(const ObjectCallback & options, bool silent) const
  210. {
  211. auto idList = getPossibleIdentifiers(options);
  212. if (idList.size() == 1)
  213. return idList.front().id;
  214. if (!silent && options.localScope != ModScope::scopeGame())
  215. showIdentifierResolutionErrorDetails(options);
  216. return std::optional<si32>();
  217. }
  218. void CIdentifierStorage::showIdentifierResolutionErrorDetails(const ObjectCallback & options) const
  219. {
  220. auto idList = getPossibleIdentifiers(options);
  221. logMod->error("Failed to resolve identifier '%s' of type '%s' from mod '%s'", options.name, options.type, options.localScope);
  222. if (options.dynamicType && options.type.empty())
  223. {
  224. bool suggestionFound = false;
  225. for (auto const & entry : registeredObjects)
  226. {
  227. if (!boost::algorithm::ends_with(entry.first, options.name))
  228. continue;
  229. suggestionFound = true;
  230. logMod->error("Perhaps you wanted to use identifier '%s' from mod '%s' instead?", entry.first, entry.second.scope);
  231. }
  232. if (suggestionFound)
  233. return;
  234. }
  235. if (idList.empty())
  236. {
  237. // check whether identifier is unavailable due to a missing dependency on a mod
  238. ObjectCallback testOptions = options;
  239. testOptions.localScope = ModScope::scopeGame();
  240. testOptions.remoteScope = {};
  241. auto testList = getPossibleIdentifiers(testOptions);
  242. if (testList.empty())
  243. {
  244. logMod->error("Identifier '%s' of type '%s' does not exists in any loaded mod!", options.name, options.type);
  245. }
  246. else
  247. {
  248. // such identifier(s) exists, but were not picked for some reason
  249. for (auto const & testOption : testList)
  250. {
  251. // attempt to access identifier from mods that is not dependency
  252. bool isValidScope = true;
  253. const auto & dependencies = LIBRARY->modh->getModDependencies(options.localScope, isValidScope);
  254. if (!vstd::contains(dependencies, testOption.scope))
  255. {
  256. logMod->error("Identifier '%s' exists in mod %s", options.name, testOption.scope);
  257. logMod->error("Please add mod '%s' as dependency of mod '%s' to access this identifier", testOption.scope, options.localScope);
  258. if (options.bypassDependenciesCheck)
  259. logMod->error("Or, to avoid this dependency, request this object as %s:%s", testOption.scope, options.name);
  260. continue;
  261. }
  262. // attempt to access identifier in form 'modName:object', but identifier is only present in different mod
  263. if (options.remoteScope.empty())
  264. {
  265. logMod->error("Identifier '%s' exists in mod '%s' but identifier was explicitly requested from mod '%s'!", options.name, testOption.scope, options.remoteScope);
  266. if (options.dynamicType)
  267. logMod->error("Please use form '%s.%s' or '%s:%s.%s' to access this identifier", options.type, options.name, testOption.scope, options.type, options.name);
  268. else
  269. logMod->error("Please use form '%s' or '%s:%s' to access this identifier", options.name, testOption.scope, options.name);
  270. continue;
  271. }
  272. }
  273. }
  274. }
  275. else
  276. {
  277. logMod->error("Multiple possible candidates:");
  278. for (auto const & testOption : idList)
  279. {
  280. logMod->error("Identifier %s exists in mod %s", options.name, testOption.scope);
  281. if (options.dynamicType)
  282. logMod->error("Please use '%s:%s.%s' to access this identifier", testOption.scope, options.type, options.name);
  283. else
  284. logMod->error("Please use '%s:%s' to access this identifier", testOption.scope, options.name);
  285. }
  286. }
  287. }
  288. void CIdentifierStorage::registerObject(const std::string & scope, const std::string & type, const std::string & name, si32 identifier)
  289. {
  290. assert(state != ELoadingState::FINISHED);
  291. ObjectData data;
  292. data.scope = scope;
  293. data.id = identifier;
  294. std::string fullID = type + '.' + name;
  295. checkIdentifier(fullID);
  296. std::pair<const std::string, ObjectData> mapping = std::make_pair(fullID, data);
  297. if(!vstd::containsMapping(registeredObjects, mapping))
  298. {
  299. logMod->trace("registered '%s' as %s:%s", fullID, scope, identifier);
  300. registeredObjects.insert(mapping);
  301. registeredObjectsCaseLookup[boost::algorithm::to_lower_copy(mapping.first)] = mapping.first;
  302. }
  303. else
  304. {
  305. logMod->trace("Duplicate object '%s' found!", fullID);
  306. }
  307. }
  308. std::vector<CIdentifierStorage::ObjectData> CIdentifierStorage::getPossibleIdentifiers(const ObjectCallback & request) const
  309. {
  310. std::set<std::string> allowedScopes;
  311. bool isValidScope = true;
  312. // called have not specified destination mod explicitly
  313. if (request.remoteScope.empty())
  314. {
  315. // special scope that should have access to all in-game objects
  316. if (request.localScope == ModScope::scopeGame())
  317. {
  318. for(const auto & modName : LIBRARY->modh->getActiveMods())
  319. allowedScopes.insert(modName);
  320. }
  321. // normally ID's from all required mods, own mod and virtual built-in mod are allowed
  322. else if(request.localScope != ModScope::scopeBuiltin() && !request.localScope.empty())
  323. {
  324. allowedScopes = LIBRARY->modh->getModDependencies(request.localScope, isValidScope);
  325. if(!isValidScope)
  326. return std::vector<ObjectData>();
  327. allowedScopes.insert(request.localScope);
  328. }
  329. // all mods can access built-in mod
  330. allowedScopes.insert(ModScope::scopeBuiltin());
  331. }
  332. else
  333. {
  334. //if destination mod was specified explicitly, restrict lookup to this mod
  335. if(request.remoteScope == ModScope::scopeBuiltin() )
  336. {
  337. //built-in mod is an implicit dependency for all mods, allow access into it
  338. allowedScopes.insert(request.remoteScope);
  339. }
  340. else if ( request.localScope == ModScope::scopeGame() )
  341. {
  342. // allow access, this is special scope that should have access to all in-game objects
  343. allowedScopes.insert(request.remoteScope);
  344. }
  345. else if(request.remoteScope == request.localScope )
  346. {
  347. // allow self-access
  348. allowedScopes.insert(request.remoteScope);
  349. }
  350. else if (request.bypassDependenciesCheck)
  351. {
  352. // this is request for an identifier that bypasses mod dependencies check
  353. allowedScopes.insert(request.remoteScope);
  354. }
  355. else
  356. {
  357. // allow access only if mod is in our dependencies
  358. auto myDeps = LIBRARY->modh->getModDependencies(request.localScope, isValidScope);
  359. if(!isValidScope)
  360. return std::vector<ObjectData>();
  361. if(myDeps.count(request.remoteScope))
  362. allowedScopes.insert(request.remoteScope);
  363. }
  364. }
  365. std::string fullID = request.type + '.' + request.name;
  366. std::string fullLowerID = boost::algorithm::to_lower_copy(fullID);
  367. std::string fullIDCaseCorrected = (request.caseSensitive || !registeredObjectsCaseLookup.count(fullLowerID)) ? fullID : registeredObjectsCaseLookup.at(fullLowerID);
  368. auto entries = registeredObjects.equal_range(fullIDCaseCorrected);
  369. if (entries.first != entries.second)
  370. {
  371. std::vector<ObjectData> locatedIDs;
  372. for (auto it = entries.first; it != entries.second; it++)
  373. {
  374. if (vstd::contains(allowedScopes, it->second.scope))
  375. {
  376. locatedIDs.push_back(it->second);
  377. }
  378. }
  379. return locatedIDs;
  380. }
  381. return std::vector<ObjectData>();
  382. }
  383. bool CIdentifierStorage::resolveIdentifier(const ObjectCallback & request) const
  384. {
  385. auto identifiers = getPossibleIdentifiers(request);
  386. if (identifiers.size() == 1) // normally resolved ID
  387. {
  388. request.callback(identifiers.front().id);
  389. return true;
  390. }
  391. if (request.optional && identifiers.empty()) // failed to resolve optional ID
  392. {
  393. return true;
  394. }
  395. if (request.bypassDependenciesCheck && !request.remoteScope.empty())
  396. {
  397. if (!vstd::contains(LIBRARY->modh->getActiveMods(), request.remoteScope))
  398. {
  399. logMod->debug("Mod '%s' requested identifier '%s' from not loaded mod '%s'. Ignoring.", request.localScope, request.name, request.remoteScope);
  400. return true; // mod was not loaded - ignore
  401. }
  402. }
  403. // error found. Try to generate some debug info
  404. failedRequests.push_back(request);
  405. showIdentifierResolutionErrorDetails(request);
  406. return false;
  407. }
  408. void CIdentifierStorage::finalize()
  409. {
  410. assert(state == ELoadingState::LOADING);
  411. state = ELoadingState::FINALIZING;
  412. while ( !scheduledRequests.empty() )
  413. {
  414. // Use local copy since new requests may appear during resolving, invalidating any iterators
  415. auto request = scheduledRequests.back();
  416. scheduledRequests.pop_back();
  417. resolveIdentifier(request);
  418. }
  419. state = ELoadingState::FINISHED;
  420. }
  421. void CIdentifierStorage::debugDumpIdentifiers()
  422. {
  423. logMod->trace("List of all registered objects:");
  424. std::map<std::string, std::vector<std::string>> objectList;
  425. for(const auto & object : registeredObjects)
  426. {
  427. size_t categoryLength = object.first.find('.');
  428. assert(categoryLength != std::string::npos);
  429. std::string objectCategory = object.first.substr(0, categoryLength);
  430. std::string objectName = object.first.substr(categoryLength + 1);
  431. objectList[objectCategory].push_back("[" + object.second.scope + "] " + objectName);
  432. }
  433. for(auto & category : objectList)
  434. boost::range::sort(category.second);
  435. for(const auto & category : objectList)
  436. {
  437. logMod->trace("");
  438. logMod->trace("### %s", category.first);
  439. logMod->trace("");
  440. for(const auto & entry : category.second)
  441. logMod->trace("- " + entry);
  442. }
  443. }
  444. std::vector<std::string> CIdentifierStorage::getModsWithFailedRequests() const
  445. {
  446. std::vector<std::string> result;
  447. for (const auto & request : failedRequests)
  448. if (!vstd::contains(result, request.localScope) && !ModScope::isScopeReserved(request.localScope))
  449. result.push_back(request.localScope);
  450. return result;
  451. }
  452. VCMI_LIB_NAMESPACE_END