IdentifierStorage.cpp 11 KB

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