IdentifierStorage.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 "../JsonNode.h"
  15. #include "../VCMI_Lib.h"
  16. #include <vstd/StringUtils.h>
  17. VCMI_LIB_NAMESPACE_BEGIN
  18. CIdentifierStorage::CIdentifierStorage():
  19. state(LOADING)
  20. {
  21. }
  22. void CIdentifierStorage::checkIdentifier(std::string & ID)
  23. {
  24. if (boost::algorithm::ends_with(ID, "."))
  25. logMod->warn("BIG WARNING: identifier %s seems to be broken!", ID);
  26. else
  27. {
  28. size_t pos = 0;
  29. do
  30. {
  31. if (std::tolower(ID[pos]) != ID[pos] ) //Not in camelCase
  32. {
  33. logMod->warn("Warning: identifier %s is not in camelCase!", ID);
  34. ID[pos] = std::tolower(ID[pos]);// Try to fix the ID
  35. }
  36. pos = ID.find('.', pos);
  37. }
  38. while(pos++ != std::string::npos);
  39. }
  40. }
  41. void CIdentifierStorage::requestIdentifier(ObjectCallback callback) const
  42. {
  43. checkIdentifier(callback.type);
  44. checkIdentifier(callback.name);
  45. assert(!callback.localScope.empty());
  46. if (state != FINISHED) // enqueue request if loading is still in progress
  47. scheduledRequests.push_back(callback);
  48. else // execute immediately for "late" requests
  49. resolveIdentifier(callback);
  50. }
  51. CIdentifierStorage::ObjectCallback CIdentifierStorage::ObjectCallback::fromNameWithType(const std::string & scope, const std::string & fullName, const std::function<void(si32)> & callback, bool optional)
  52. {
  53. assert(!scope.empty());
  54. auto scopeAndFullName = vstd::splitStringToPair(fullName, ':');
  55. auto typeAndName = vstd::splitStringToPair(scopeAndFullName.second, '.');
  56. if (scope == scopeAndFullName.first)
  57. logMod->debug("Target scope for identifier '%s' is redundant! Identifier already defined in mod '%s'", fullName, scope);
  58. ObjectCallback result;
  59. result.localScope = scope;
  60. result.remoteScope = scopeAndFullName.first;
  61. result.type = typeAndName.first;
  62. result.name = typeAndName.second;
  63. result.callback = callback;
  64. result.optional = optional;
  65. return result;
  66. }
  67. 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)
  68. {
  69. assert(!scope.empty());
  70. auto scopeAndFullName = vstd::splitStringToPair(fullName, ':');
  71. auto typeAndName = vstd::splitStringToPair(scopeAndFullName.second, '.');
  72. if(!typeAndName.first.empty())
  73. {
  74. if (typeAndName.first != type)
  75. logMod->error("Identifier '%s' from mod '%s' requested with different type! Type '%s' expected!", fullName, scope, type);
  76. else
  77. logMod->debug("Target type for identifier '%s' defined in mod '%s' is redundant!", fullName, scope);
  78. }
  79. if (scope == scopeAndFullName.first)
  80. logMod->debug("Target scope for identifier '%s' is redundant! Identifier already defined in mod '%s'", fullName, scope);
  81. ObjectCallback result;
  82. result.localScope = scope;
  83. result.remoteScope = scopeAndFullName.first;
  84. result.type = type;
  85. result.name = typeAndName.second;
  86. result.callback = callback;
  87. result.optional = optional;
  88. return result;
  89. }
  90. void CIdentifierStorage::requestIdentifier(const std::string & scope, const std::string & type, const std::string & name, const std::function<void(si32)> & callback) const
  91. {
  92. requestIdentifier(ObjectCallback::fromNameAndType(scope, type, name, callback, false));
  93. }
  94. void CIdentifierStorage::requestIdentifier(const std::string & scope, const std::string & fullName, const std::function<void(si32)> & callback) const
  95. {
  96. requestIdentifier(ObjectCallback::fromNameWithType(scope, fullName, callback, false));
  97. }
  98. void CIdentifierStorage::requestIdentifier(const std::string & type, const JsonNode & name, const std::function<void(si32)> & callback) const
  99. {
  100. requestIdentifier(ObjectCallback::fromNameAndType(name.meta, type, name.String(), callback, false));
  101. }
  102. void CIdentifierStorage::requestIdentifier(const JsonNode & name, const std::function<void(si32)> & callback) const
  103. {
  104. requestIdentifier(ObjectCallback::fromNameWithType(name.meta, name.String(), callback, false));
  105. }
  106. void CIdentifierStorage::tryRequestIdentifier(const std::string & scope, const std::string & type, const std::string & name, const std::function<void(si32)> & callback) const
  107. {
  108. requestIdentifier(ObjectCallback::fromNameAndType(scope, type, name, callback, true));
  109. }
  110. void CIdentifierStorage::tryRequestIdentifier(const std::string & type, const JsonNode & name, const std::function<void(si32)> & callback) const
  111. {
  112. requestIdentifier(ObjectCallback::fromNameAndType(name.meta, type, name.String(), callback, true));
  113. }
  114. std::optional<si32> CIdentifierStorage::getIdentifier(const std::string & scope, const std::string & type, const std::string & name, bool silent) const
  115. {
  116. auto idList = getPossibleIdentifiers(ObjectCallback::fromNameAndType(scope, type, name, std::function<void(si32)>(), silent));
  117. if (idList.size() == 1)
  118. return idList.front().id;
  119. if (!silent)
  120. logMod->error("Failed to resolve identifier %s of type %s from mod %s", name , type ,scope);
  121. return std::optional<si32>();
  122. }
  123. std::optional<si32> CIdentifierStorage::getIdentifier(const std::string & type, const JsonNode & name, bool silent) const
  124. {
  125. auto idList = getPossibleIdentifiers(ObjectCallback::fromNameAndType(name.meta, type, name.String(), std::function<void(si32)>(), silent));
  126. if (idList.size() == 1)
  127. return idList.front().id;
  128. if (!silent)
  129. logMod->error("Failed to resolve identifier %s of type %s from mod %s", name.String(), type, name.meta);
  130. return std::optional<si32>();
  131. }
  132. std::optional<si32> CIdentifierStorage::getIdentifier(const JsonNode & name, bool silent) const
  133. {
  134. auto idList = getPossibleIdentifiers(ObjectCallback::fromNameWithType(name.meta, name.String(), std::function<void(si32)>(), silent));
  135. if (idList.size() == 1)
  136. return idList.front().id;
  137. if (!silent)
  138. logMod->error("Failed to resolve identifier %s from mod %s", name.String(), name.meta);
  139. return std::optional<si32>();
  140. }
  141. std::optional<si32> CIdentifierStorage::getIdentifier(const std::string & scope, const std::string & fullName, bool silent) const
  142. {
  143. auto idList = getPossibleIdentifiers(ObjectCallback::fromNameWithType(scope, fullName, std::function<void(si32)>(), silent));
  144. if (idList.size() == 1)
  145. return idList.front().id;
  146. if (!silent)
  147. logMod->error("Failed to resolve identifier %s from mod %s", fullName, scope);
  148. return std::optional<si32>();
  149. }
  150. void CIdentifierStorage::registerObject(const std::string & scope, const std::string & type, const std::string & name, si32 identifier)
  151. {
  152. ObjectData data;
  153. data.scope = scope;
  154. data.id = identifier;
  155. std::string fullID = type + '.' + name;
  156. checkIdentifier(fullID);
  157. std::pair<const std::string, ObjectData> mapping = std::make_pair(fullID, data);
  158. if(!vstd::containsMapping(registeredObjects, mapping))
  159. {
  160. logMod->trace("registered %s as %s:%s", fullID, scope, identifier);
  161. registeredObjects.insert(mapping);
  162. }
  163. }
  164. std::vector<CIdentifierStorage::ObjectData> CIdentifierStorage::getPossibleIdentifiers(const ObjectCallback & request) const
  165. {
  166. std::set<std::string> allowedScopes;
  167. bool isValidScope = true;
  168. // called have not specified destination mod explicitly
  169. if (request.remoteScope.empty())
  170. {
  171. // special scope that should have access to all in-game objects
  172. if (request.localScope == ModScope::scopeGame())
  173. {
  174. for(const auto & modName : VLC->modh->getActiveMods())
  175. allowedScopes.insert(modName);
  176. }
  177. // normally ID's from all required mods, own mod and virtual built-in mod are allowed
  178. else if(request.localScope != ModScope::scopeBuiltin() && !request.localScope.empty())
  179. {
  180. allowedScopes = VLC->modh->getModDependencies(request.localScope, isValidScope);
  181. if(!isValidScope)
  182. return std::vector<ObjectData>();
  183. allowedScopes.insert(request.localScope);
  184. }
  185. // all mods can access built-in mod
  186. allowedScopes.insert(ModScope::scopeBuiltin());
  187. }
  188. else
  189. {
  190. //if destination mod was specified explicitly, restrict lookup to this mod
  191. if(request.remoteScope == ModScope::scopeBuiltin() )
  192. {
  193. //built-in mod is an implicit dependency for all mods, allow access into it
  194. allowedScopes.insert(request.remoteScope);
  195. }
  196. else if ( request.localScope == ModScope::scopeGame() )
  197. {
  198. // allow access, this is special scope that should have access to all in-game objects
  199. allowedScopes.insert(request.remoteScope);
  200. }
  201. else if(request.remoteScope == request.localScope )
  202. {
  203. // allow self-access
  204. allowedScopes.insert(request.remoteScope);
  205. }
  206. else
  207. {
  208. // allow access only if mod is in our dependencies
  209. auto myDeps = VLC->modh->getModDependencies(request.localScope, isValidScope);
  210. if(!isValidScope)
  211. return std::vector<ObjectData>();
  212. if(myDeps.count(request.remoteScope))
  213. allowedScopes.insert(request.remoteScope);
  214. }
  215. }
  216. std::string fullID = request.type + '.' + request.name;
  217. auto entries = registeredObjects.equal_range(fullID);
  218. if (entries.first != entries.second)
  219. {
  220. std::vector<ObjectData> locatedIDs;
  221. for (auto it = entries.first; it != entries.second; it++)
  222. {
  223. if (vstd::contains(allowedScopes, it->second.scope))
  224. {
  225. locatedIDs.push_back(it->second);
  226. }
  227. }
  228. return locatedIDs;
  229. }
  230. return std::vector<ObjectData>();
  231. }
  232. bool CIdentifierStorage::resolveIdentifier(const ObjectCallback & request) const
  233. {
  234. auto identifiers = getPossibleIdentifiers(request);
  235. if (identifiers.size() == 1) // normally resolved ID
  236. {
  237. request.callback(identifiers.front().id);
  238. return true;
  239. }
  240. if (request.optional && identifiers.empty()) // failed to resolve optinal ID
  241. {
  242. return true;
  243. }
  244. // error found. Try to generate some debug info
  245. if(identifiers.empty())
  246. logMod->error("Unknown identifier!");
  247. else
  248. logMod->error("Ambiguous identifier request!");
  249. logMod->error("Request for %s.%s from mod %s", request.type, request.name, request.localScope);
  250. for(const auto & id : identifiers)
  251. {
  252. logMod->error("\tID is available in mod %s", id.scope);
  253. }
  254. return false;
  255. }
  256. void CIdentifierStorage::finalize()
  257. {
  258. state = FINALIZING;
  259. bool errorsFound = false;
  260. while ( !scheduledRequests.empty() )
  261. {
  262. // Use local copy since new requests may appear during resolving, invalidating any iterators
  263. auto request = scheduledRequests.back();
  264. scheduledRequests.pop_back();
  265. if (!resolveIdentifier(request))
  266. errorsFound = true;
  267. }
  268. if (errorsFound)
  269. {
  270. for(const auto & object : registeredObjects)
  271. {
  272. logMod->trace("%s : %s -> %d", object.second.scope, object.first, object.second.id);
  273. }
  274. logMod->error("All known identifiers were dumped into log file");
  275. }
  276. assert(errorsFound == false);
  277. state = FINISHED;
  278. }
  279. VCMI_LIB_NAMESPACE_END