CModHandler.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  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. bool CModInfo::checkModGameplayAffecting() const
  552. {
  553. if (modGameplayAffecting.has_value())
  554. return *modGameplayAffecting;
  555. static const std::vector<std::string> keysToTest = {
  556. "heroClasses",
  557. "artifacts",
  558. "creatures",
  559. "factions",
  560. "objects",
  561. "heroes",
  562. "spells",
  563. "skills",
  564. "templates",
  565. "scripts",
  566. "battlefields",
  567. "terrains",
  568. "rivers",
  569. "roads",
  570. "obstacles"
  571. };
  572. ResourceID modFileResource(CModInfo::getModFile(identifier));
  573. if(CResourceHandler::get("initial")->existsResource(modFileResource))
  574. {
  575. const JsonNode modConfig(modFileResource);
  576. for (auto const & key : keysToTest)
  577. {
  578. if (!modConfig[key].isNull())
  579. {
  580. modGameplayAffecting = true;
  581. return *modGameplayAffecting;
  582. }
  583. }
  584. }
  585. modGameplayAffecting = false;
  586. return *modGameplayAffecting;
  587. }
  588. void CModInfo::loadLocalData(const JsonNode & data)
  589. {
  590. bool validated = false;
  591. implicitlyEnabled = true;
  592. explicitlyEnabled = !config["keepDisabled"].Bool();
  593. checksum = 0;
  594. if (data.getType() == JsonNode::JsonType::DATA_BOOL)
  595. {
  596. explicitlyEnabled = data.Bool();
  597. }
  598. if (data.getType() == JsonNode::JsonType::DATA_STRUCT)
  599. {
  600. explicitlyEnabled = data["active"].Bool();
  601. validated = data["validated"].Bool();
  602. checksum = strtol(data["checksum"].String().c_str(), nullptr, 16);
  603. }
  604. //check compatibility
  605. implicitlyEnabled &= (vcmiCompatibleMin.isNull() || CModVersion::GameVersion().compatible(vcmiCompatibleMin));
  606. implicitlyEnabled &= (vcmiCompatibleMax.isNull() || vcmiCompatibleMax.compatible(CModVersion::GameVersion()));
  607. if(!implicitlyEnabled)
  608. logGlobal->warn("Mod %s is incompatible with current version of VCMI and cannot be enabled", name);
  609. if (boost::iequals(config["modType"].String(), "translation")) // compatibility code - mods use "Translation" type at the moment
  610. {
  611. if (baseLanguage != VLC->generaltexth->getPreferredLanguage())
  612. {
  613. logGlobal->warn("Translation mod %s was not loaded: language mismatch!", name);
  614. implicitlyEnabled = false;
  615. }
  616. }
  617. if (isEnabled())
  618. validation = validated ? PASSED : PENDING;
  619. else
  620. validation = validated ? PASSED : FAILED;
  621. }
  622. bool CModInfo::isEnabled() const
  623. {
  624. return implicitlyEnabled && explicitlyEnabled;
  625. }
  626. void CModInfo::setEnabled(bool on)
  627. {
  628. explicitlyEnabled = on;
  629. }
  630. CModHandler::CModHandler() : content(std::make_shared<CContentHandler>())
  631. {
  632. //TODO: moddable spell schools
  633. for (auto i = 0; i < GameConstants::DEFAULT_SCHOOLS; ++i)
  634. identifiers.registerObject(CModHandler::scopeBuiltin(), "spellSchool", SpellConfig::SCHOOL[i].jsonName, SpellConfig::SCHOOL[i].id);
  635. identifiers.registerObject(CModHandler::scopeBuiltin(), "spellSchool", "any", SpellSchool(ESpellSchool::ANY));
  636. for (int i = 0; i < GameConstants::RESOURCE_QUANTITY; ++i)
  637. {
  638. identifiers.registerObject(CModHandler::scopeBuiltin(), "resource", GameConstants::RESOURCE_NAMES[i], i);
  639. }
  640. for(int i=0; i<GameConstants::PRIMARY_SKILLS; ++i)
  641. {
  642. identifiers.registerObject(CModHandler::scopeBuiltin(), "primSkill", PrimarySkill::names[i], i);
  643. identifiers.registerObject(CModHandler::scopeBuiltin(), "primarySkill", PrimarySkill::names[i], i);
  644. }
  645. }
  646. // currentList is passed by value to get current list of depending mods
  647. bool CModHandler::hasCircularDependency(const TModID & modID, std::set<TModID> currentList) const
  648. {
  649. const CModInfo & mod = allMods.at(modID);
  650. // Mod already present? We found a loop
  651. if (vstd::contains(currentList, modID))
  652. {
  653. logMod->error("Error: Circular dependency detected! Printing dependency list:");
  654. logMod->error("\t%s -> ", mod.name);
  655. return true;
  656. }
  657. currentList.insert(modID);
  658. // recursively check every dependency of this mod
  659. for(const TModID & dependency : mod.dependencies)
  660. {
  661. if (hasCircularDependency(dependency, currentList))
  662. {
  663. logMod->error("\t%s ->\n", mod.name); // conflict detected, print dependency list
  664. return true;
  665. }
  666. }
  667. return false;
  668. }
  669. // Returned vector affects the resource loaders call order (see CFilesystemList::load).
  670. // The loaders call order matters when dependent mod overrides resources in its dependencies.
  671. std::vector <TModID> CModHandler::validateAndSortDependencies(std::vector <TModID> modsToResolve) const
  672. {
  673. // Topological sort algorithm.
  674. // TODO: Investigate possible ways to improve performance.
  675. boost::range::sort(modsToResolve); // Sort mods per name
  676. std::vector <TModID> sortedValidMods; // Vector keeps order of elements (LIFO)
  677. sortedValidMods.reserve(modsToResolve.size()); // push_back calls won't cause memory reallocation
  678. std::set <TModID> resolvedModIDs; // Use a set for validation for performance reason, but set does not keep order of elements
  679. // Mod is resolved if it has not dependencies or all its dependencies are already resolved
  680. auto isResolved = [&](const CModInfo & mod) -> CModInfo::EValidationStatus
  681. {
  682. if(mod.dependencies.size() > resolvedModIDs.size())
  683. return CModInfo::PENDING;
  684. for(const TModID & dependency : mod.dependencies)
  685. {
  686. if(!vstd::contains(resolvedModIDs, dependency))
  687. return CModInfo::PENDING;
  688. }
  689. return CModInfo::PASSED;
  690. };
  691. while(true)
  692. {
  693. std::set <TModID> resolvedOnCurrentTreeLevel;
  694. for(auto it = modsToResolve.begin(); it != modsToResolve.end();) // One iteration - one level of mods tree
  695. {
  696. if(isResolved(allMods.at(*it)) == CModInfo::PASSED)
  697. {
  698. resolvedOnCurrentTreeLevel.insert(*it); // Not to the resolvedModIDs, so current node childs will be resolved on the next iteration
  699. sortedValidMods.push_back(*it);
  700. it = modsToResolve.erase(it);
  701. continue;
  702. }
  703. it++;
  704. }
  705. if(!resolvedOnCurrentTreeLevel.empty())
  706. {
  707. resolvedModIDs.insert(resolvedOnCurrentTreeLevel.begin(), resolvedOnCurrentTreeLevel.end());
  708. continue;
  709. }
  710. // If there're no valid mods on the current mods tree level, no more mod can be resolved, should be end.
  711. break;
  712. }
  713. // Left mods have unresolved dependencies, output all to log.
  714. for(const auto & brokenModID : modsToResolve)
  715. {
  716. const CModInfo & brokenMod = allMods.at(brokenModID);
  717. for(const TModID & dependency : brokenMod.dependencies)
  718. {
  719. if(!vstd::contains(resolvedModIDs, dependency))
  720. logMod->error("Mod '%s' will not work: it depends on mod '%s', which is not installed.", brokenMod.name, dependency);
  721. }
  722. }
  723. return sortedValidMods;
  724. }
  725. std::vector<std::string> CModHandler::getModList(const std::string & path) const
  726. {
  727. std::string modDir = boost::to_upper_copy(path + "MODS/");
  728. size_t depth = boost::range::count(modDir, '/');
  729. auto list = CResourceHandler::get("initial")->getFilteredFiles([&](const ResourceID & id) -> bool
  730. {
  731. if (id.getType() != EResType::DIRECTORY)
  732. return false;
  733. if (!boost::algorithm::starts_with(id.getName(), modDir))
  734. return false;
  735. if (boost::range::count(id.getName(), '/') != depth )
  736. return false;
  737. return true;
  738. });
  739. //storage for found mods
  740. std::vector<std::string> foundMods;
  741. for(const auto & entry : list)
  742. {
  743. std::string name = entry.getName();
  744. name.erase(0, modDir.size()); //Remove path prefix
  745. if (!name.empty())
  746. foundMods.push_back(name);
  747. }
  748. return foundMods;
  749. }
  750. bool CModHandler::isScopeReserved(const TModID & scope)
  751. {
  752. //following scopes are reserved - either in use by mod system or by filesystem
  753. static const std::array<TModID, 9> reservedScopes = {
  754. "core", "map", "game", "root", "saves", "config", "local", "initial", "mapEditor"
  755. };
  756. return std::find(reservedScopes.begin(), reservedScopes.end(), scope) != reservedScopes.end();
  757. }
  758. const TModID & CModHandler::scopeBuiltin()
  759. {
  760. static const TModID scope = "core";
  761. return scope;
  762. }
  763. const TModID & CModHandler::scopeGame()
  764. {
  765. static const TModID scope = "game";
  766. return scope;
  767. }
  768. const TModID & CModHandler::scopeMap()
  769. {
  770. //TODO: implement accessing map dependencies for both H3 and VCMI maps
  771. // for now, allow access to any identifiers
  772. static const TModID scope = "game";
  773. return scope;
  774. }
  775. void CModHandler::loadMods(const std::string & path, const std::string & parent, const JsonNode & modSettings, bool enableMods)
  776. {
  777. for(const std::string & modName : getModList(path))
  778. loadOneMod(modName, parent, modSettings, enableMods);
  779. }
  780. void CModHandler::loadOneMod(std::string modName, const std::string & parent, const JsonNode & modSettings, bool enableMods)
  781. {
  782. boost::to_lower(modName);
  783. std::string modFullName = parent.empty() ? modName : parent + '.' + modName;
  784. if ( isScopeReserved(modFullName))
  785. {
  786. logMod->error("Can not load mod %s - this name is reserved for internal use!", modFullName);
  787. return;
  788. }
  789. if(CResourceHandler::get("initial")->existsResource(ResourceID(CModInfo::getModFile(modFullName))))
  790. {
  791. CModInfo mod(modFullName, modSettings[modName], JsonNode(ResourceID(CModInfo::getModFile(modFullName))));
  792. if (!parent.empty()) // this is submod, add parent to dependencies
  793. mod.dependencies.insert(parent);
  794. allMods[modFullName] = mod;
  795. if (mod.isEnabled() && enableMods)
  796. activeMods.push_back(modFullName);
  797. loadMods(CModInfo::getModDir(modFullName) + '/', modFullName, modSettings[modName]["mods"], enableMods && mod.isEnabled());
  798. }
  799. }
  800. void CModHandler::loadMods(bool onlyEssential)
  801. {
  802. JsonNode modConfig;
  803. if(onlyEssential)
  804. {
  805. loadOneMod("vcmi", "", modConfig, true);//only vcmi and submods
  806. }
  807. else
  808. {
  809. modConfig = loadModSettings("config/modSettings.json");
  810. loadMods("", "", modConfig["activeMods"], true);
  811. }
  812. coreMod = CModInfo(CModHandler::scopeBuiltin(), modConfig[CModHandler::scopeBuiltin()], JsonNode(ResourceID("config/gameConfig.json")));
  813. coreMod.name = "Original game files";
  814. }
  815. std::vector<std::string> CModHandler::getAllMods()
  816. {
  817. std::vector<std::string> modlist;
  818. modlist.reserve(allMods.size());
  819. for (auto & entry : allMods)
  820. modlist.push_back(entry.first);
  821. return modlist;
  822. }
  823. std::vector<std::string> CModHandler::getActiveMods()
  824. {
  825. return activeMods;
  826. }
  827. const CModInfo & CModHandler::getModInfo(const TModID & modId) const
  828. {
  829. return allMods.at(modId);
  830. }
  831. static JsonNode genDefaultFS()
  832. {
  833. // default FS config for mods: directory "Content" that acts as H3 root directory
  834. JsonNode defaultFS;
  835. defaultFS[""].Vector().resize(2);
  836. defaultFS[""].Vector()[0]["type"].String() = "zip";
  837. defaultFS[""].Vector()[0]["path"].String() = "/Content.zip";
  838. defaultFS[""].Vector()[1]["type"].String() = "dir";
  839. defaultFS[""].Vector()[1]["path"].String() = "/Content";
  840. return defaultFS;
  841. }
  842. static ISimpleResourceLoader * genModFilesystem(const std::string & modName, const JsonNode & conf)
  843. {
  844. static const JsonNode defaultFS = genDefaultFS();
  845. if (!conf["filesystem"].isNull())
  846. return CResourceHandler::createFileSystem(CModInfo::getModDir(modName), conf["filesystem"]);
  847. else
  848. return CResourceHandler::createFileSystem(CModInfo::getModDir(modName), defaultFS);
  849. }
  850. static ui32 calculateModChecksum(const std::string & modName, ISimpleResourceLoader * filesystem)
  851. {
  852. boost::crc_32_type modChecksum;
  853. // first - add current VCMI version into checksum to force re-validation on VCMI updates
  854. modChecksum.process_bytes(reinterpret_cast<const void*>(GameConstants::VCMI_VERSION.data()), GameConstants::VCMI_VERSION.size());
  855. // second - add mod.json into checksum because filesystem does not contains this file
  856. // FIXME: remove workaround for core mod
  857. if (modName != CModHandler::scopeBuiltin())
  858. {
  859. ResourceID modConfFile(CModInfo::getModFile(modName), EResType::TEXT);
  860. ui32 configChecksum = CResourceHandler::get("initial")->load(modConfFile)->calculateCRC32();
  861. modChecksum.process_bytes(reinterpret_cast<const void *>(&configChecksum), sizeof(configChecksum));
  862. }
  863. // third - add all detected text files from this mod into checksum
  864. auto files = filesystem->getFilteredFiles([](const ResourceID & resID)
  865. {
  866. return resID.getType() == EResType::TEXT &&
  867. ( boost::starts_with(resID.getName(), "DATA") ||
  868. boost::starts_with(resID.getName(), "CONFIG"));
  869. });
  870. for (const ResourceID & file : files)
  871. {
  872. ui32 fileChecksum = filesystem->load(file)->calculateCRC32();
  873. modChecksum.process_bytes(reinterpret_cast<const void *>(&fileChecksum), sizeof(fileChecksum));
  874. }
  875. return modChecksum.checksum();
  876. }
  877. void CModHandler::loadModFilesystems()
  878. {
  879. CGeneralTextHandler::detectInstallParameters();
  880. activeMods = validateAndSortDependencies(activeMods);
  881. coreMod.updateChecksum(calculateModChecksum(CModHandler::scopeBuiltin(), CResourceHandler::get(CModHandler::scopeBuiltin())));
  882. for(std::string & modName : activeMods)
  883. {
  884. CModInfo & mod = allMods[modName];
  885. CResourceHandler::addFilesystem("data", modName, genModFilesystem(modName, mod.config));
  886. }
  887. }
  888. TModID CModHandler::findResourceOrigin(const ResourceID & name)
  889. {
  890. for(const auto & modID : boost::adaptors::reverse(activeMods))
  891. {
  892. if(CResourceHandler::get(modID)->existsResource(name))
  893. return modID;
  894. }
  895. if(CResourceHandler::get("core")->existsResource(name))
  896. return "core";
  897. if(CResourceHandler::get("mapEditor")->existsResource(name))
  898. return "core"; // Workaround for loading maps via map editor
  899. assert(0);
  900. return "";
  901. }
  902. std::string CModHandler::getModLanguage(const TModID& modId) const
  903. {
  904. if ( modId == "core")
  905. return VLC->generaltexth->getInstalledLanguage();
  906. return allMods.at(modId).baseLanguage;
  907. }
  908. std::set<TModID> CModHandler::getModDependencies(const TModID & modId, bool & isModFound) const
  909. {
  910. auto it = allMods.find(modId);
  911. isModFound = (it != allMods.end());
  912. if(isModFound)
  913. return it->second.dependencies;
  914. logMod->error("Mod not found: '%s'", modId);
  915. return {};
  916. }
  917. void CModHandler::initializeConfig()
  918. {
  919. VLC->settingsHandler->load(coreMod.config["settings"]);
  920. for(const TModID & modName : activeMods)
  921. {
  922. const auto & mod = allMods[modName];
  923. if (!mod.config["settings"].isNull())
  924. VLC->settingsHandler->load(mod.config["settings"]);
  925. }
  926. }
  927. bool CModHandler::validateTranslations(TModID modName) const
  928. {
  929. bool result = true;
  930. const auto & mod = allMods.at(modName);
  931. {
  932. auto fileList = mod.config["translations"].convertTo<std::vector<std::string> >();
  933. JsonNode json = JsonUtils::assembleFromFiles(fileList);
  934. result |= VLC->generaltexth->validateTranslation(mod.baseLanguage, modName, json);
  935. }
  936. for(const auto & language : Languages::getLanguageList())
  937. {
  938. if (!language.hasTranslation)
  939. continue;
  940. if (mod.config[language.identifier].isNull())
  941. continue;
  942. if (mod.config[language.identifier]["skipValidation"].Bool())
  943. continue;
  944. auto fileList = mod.config[language.identifier]["translations"].convertTo<std::vector<std::string> >();
  945. JsonNode json = JsonUtils::assembleFromFiles(fileList);
  946. result |= VLC->generaltexth->validateTranslation(language.identifier, modName, json);
  947. }
  948. return result;
  949. }
  950. void CModHandler::loadTranslation(const TModID & modName)
  951. {
  952. const auto & mod = allMods[modName];
  953. std::string preferredLanguage = VLC->generaltexth->getPreferredLanguage();
  954. std::string modBaseLanguage = allMods[modName].baseLanguage;
  955. auto baseTranslationList = mod.config["translations"].convertTo<std::vector<std::string> >();
  956. auto extraTranslationList = mod.config[preferredLanguage]["translations"].convertTo<std::vector<std::string> >();
  957. JsonNode baseTranslation = JsonUtils::assembleFromFiles(baseTranslationList);
  958. JsonNode extraTranslation = JsonUtils::assembleFromFiles(extraTranslationList);
  959. VLC->generaltexth->loadTranslationOverrides(modBaseLanguage, modName, baseTranslation);
  960. VLC->generaltexth->loadTranslationOverrides(preferredLanguage, modName, extraTranslation);
  961. }
  962. void CModHandler::load()
  963. {
  964. CStopWatch totalTime;
  965. CStopWatch timer;
  966. logMod->info("\tInitializing content handler: %d ms", timer.getDiff());
  967. content->init();
  968. for(const TModID & modName : activeMods)
  969. {
  970. logMod->trace("Generating checksum for %s", modName);
  971. allMods[modName].updateChecksum(calculateModChecksum(modName, CResourceHandler::get(modName)));
  972. }
  973. // first - load virtual builtin mod that contains all data
  974. // TODO? move all data into real mods? RoE, AB, SoD, WoG
  975. content->preloadData(coreMod);
  976. for(const TModID & modName : activeMods)
  977. content->preloadData(allMods[modName]);
  978. logMod->info("\tParsing mod data: %d ms", timer.getDiff());
  979. content->load(coreMod);
  980. for(const TModID & modName : activeMods)
  981. content->load(allMods[modName]);
  982. #if SCRIPTING_ENABLED
  983. VLC->scriptHandler->performRegistration(VLC);//todo: this should be done before any other handlers load
  984. #endif
  985. content->loadCustom();
  986. for(const TModID & modName : activeMods)
  987. loadTranslation(modName);
  988. for(const TModID & modName : activeMods)
  989. if (!validateTranslations(modName))
  990. allMods[modName].validation = CModInfo::FAILED;
  991. logMod->info("\tLoading mod data: %d ms", timer.getDiff());
  992. VLC->creh->loadCrExpMod();
  993. identifiers.finalize();
  994. logMod->info("\tResolving identifiers: %d ms", timer.getDiff());
  995. content->afterLoadFinalization();
  996. logMod->info("\tHandlers post-load finalization: %d ms ", timer.getDiff());
  997. logMod->info("\tAll game content loaded in %d ms", totalTime.getDiff());
  998. }
  999. void CModHandler::afterLoad(bool onlyEssential)
  1000. {
  1001. JsonNode modSettings;
  1002. for (auto & modEntry : allMods)
  1003. {
  1004. std::string pointer = "/" + boost::algorithm::replace_all_copy(modEntry.first, ".", "/mods/");
  1005. modSettings["activeMods"].resolvePointer(pointer) = modEntry.second.saveLocalData();
  1006. }
  1007. modSettings[CModHandler::scopeBuiltin()] = coreMod.saveLocalData();
  1008. if(!onlyEssential)
  1009. {
  1010. FileStream file(*CResourceHandler::get()->getResourceName(ResourceID("config/modSettings.json")), std::ofstream::out | std::ofstream::trunc);
  1011. file << modSettings.toJson();
  1012. }
  1013. }
  1014. std::string CModHandler::normalizeIdentifier(const std::string & scope, const std::string & remoteScope, const std::string & identifier)
  1015. {
  1016. auto p = vstd::splitStringToPair(identifier, ':');
  1017. if(p.first.empty())
  1018. p.first = scope;
  1019. if(p.first == remoteScope)
  1020. p.first.clear();
  1021. return p.first.empty() ? p.second : p.first + ":" + p.second;
  1022. }
  1023. void CModHandler::parseIdentifier(const std::string & fullIdentifier, std::string & scope, std::string & type, std::string & identifier)
  1024. {
  1025. auto p = vstd::splitStringToPair(fullIdentifier, ':');
  1026. scope = p.first;
  1027. auto p2 = vstd::splitStringToPair(p.second, '.');
  1028. if(!p2.first.empty())
  1029. {
  1030. type = p2.first;
  1031. identifier = p2.second;
  1032. }
  1033. else
  1034. {
  1035. type = p.second;
  1036. identifier.clear();
  1037. }
  1038. }
  1039. std::string CModHandler::makeFullIdentifier(const std::string & scope, const std::string & type, const std::string & identifier)
  1040. {
  1041. if(type.empty())
  1042. logGlobal->error("Full identifier (%s %s) requires type name", scope, identifier);
  1043. std::string actualScope = scope;
  1044. std::string actualName = identifier;
  1045. //ignore scope if identifier is scoped
  1046. auto scopeAndName = vstd::splitStringToPair(identifier, ':');
  1047. if(!scopeAndName.first.empty())
  1048. {
  1049. actualScope = scopeAndName.first;
  1050. actualName = scopeAndName.second;
  1051. }
  1052. if(actualScope.empty())
  1053. {
  1054. return actualName.empty() ? type : type + "." + actualName;
  1055. }
  1056. else
  1057. {
  1058. return actualName.empty() ? actualScope+ ":" + type : actualScope + ":" + type + "." + actualName;
  1059. }
  1060. }
  1061. VCMI_LIB_NAMESPACE_END