IdentifierStorage.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * IdentifierStorage.h, 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. #pragma once
  11. VCMI_LIB_NAMESPACE_BEGIN
  12. class JsonNode;
  13. /// class that stores all object identifiers strings and maps them to numeric ID's
  14. /// if possible, objects ID's should be in format <type>.<name>, camelCase e.g. "creature.grandElf"
  15. class DLL_LINKAGE CIdentifierStorage
  16. {
  17. enum class ELoadingState
  18. {
  19. LOADING,
  20. FINALIZING,
  21. FINISHED
  22. };
  23. struct ObjectCallback // entry created on ID request
  24. {
  25. std::string localScope; /// scope from which this ID was requested
  26. std::string remoteScope; /// scope in which this object must be found
  27. std::string type; /// type, e.g. creature, faction, hero, etc
  28. std::string name; /// string ID
  29. std::function<void(si32)> callback;
  30. bool optional;
  31. bool bypassDependenciesCheck;
  32. bool dynamicType;
  33. /// Builds callback from identifier in form "targetMod:type.name"
  34. static ObjectCallback fromNameWithType(const std::string & scope, const std::string & fullName, const std::function<void(si32)> & callback, bool optional);
  35. /// Builds callback from identifier in form "targetMod:name"
  36. static ObjectCallback fromNameAndType(const std::string & scope, const std::string & type, const std::string & fullName, const std::function<void(si32)> & callback, bool optional, bool bypassDependenciesCheck);
  37. private:
  38. ObjectCallback() = default;
  39. };
  40. struct ObjectData // entry created on ID registration
  41. {
  42. si32 id;
  43. std::string scope; /// scope in which this ID located
  44. bool operator==(const ObjectData & other) const
  45. {
  46. return id == other.id && scope == other.scope;
  47. }
  48. };
  49. std::multimap<std::string, ObjectData> registeredObjects;
  50. mutable std::vector<ObjectCallback> scheduledRequests;
  51. /// All non-optional requests that have failed to resolve, for debug & error reporting
  52. mutable std::vector<ObjectCallback> failedRequests;
  53. ELoadingState state = ELoadingState::LOADING;
  54. /// Helper method that dumps all registered identifier into log file
  55. void debugDumpIdentifiers();
  56. /// Check if identifier can be valid (camelCase, point as separator)
  57. static void checkIdentifier(const std::string & ID);
  58. void requestIdentifier(const ObjectCallback & callback) const;
  59. bool resolveIdentifier(const ObjectCallback & callback) const;
  60. std::vector<ObjectData> getPossibleIdentifiers(const ObjectCallback & callback) const;
  61. void showIdentifierResolutionErrorDetails(const ObjectCallback & callback) const;
  62. std::optional<si32> getIdentifierImpl(const ObjectCallback & callback, bool silent) const;
  63. public:
  64. CIdentifierStorage();
  65. virtual ~CIdentifierStorage() = default;
  66. /// request identifier for specific object name.
  67. /// Function callback will be called during ID resolution phase of loading
  68. void requestIdentifier(const std::string & scope, const std::string & type, const std::string & name, const std::function<void(si32)> & callback) const;
  69. ///fullName = [remoteScope:]type.name
  70. void requestIdentifier(const std::string & scope, const std::string & fullName, const std::function<void(si32)> & callback) const;
  71. void requestIdentifier(const std::string & type, const JsonNode & name, const std::function<void(si32)> & callback) const;
  72. void requestIdentifier(const JsonNode & name, const std::function<void(si32)> & callback) const;
  73. void requestIdentifierIfNotNull(const std::string & type, const JsonNode & name, const std::function<void(si32)> & callback) const;
  74. void requestIdentifierIfFound(const std::string & type, const JsonNode & name, const std::function<void(si32)> & callback) const;
  75. void requestIdentifierIfFound(const std::string & scope, const std::string & type, const std::string & name, const std::function<void(si32)> & callback) const;
  76. /// try to request ID. If ID with such name won't be loaded, callback function will not be called
  77. void tryRequestIdentifier(const std::string & scope, const std::string & type, const std::string & name, const std::function<void(si32)> & callback) const;
  78. void tryRequestIdentifier(const std::string & type, const JsonNode & name, const std::function<void(si32)> & callback) const;
  79. /// get identifier immediately. If identifier is not know and not silent call will result in error message
  80. std::optional<si32> getIdentifier(const std::string & scope, const std::string & type, const std::string & name, bool silent = false) const;
  81. std::optional<si32> getIdentifier(const std::string & type, const JsonNode & name, bool silent = false) const;
  82. std::optional<si32> getIdentifier(const JsonNode & name, bool silent = false) const;
  83. std::optional<si32> getIdentifier(const std::string & scope, const std::string & fullName, bool silent = false) const;
  84. /// registers new object
  85. void registerObject(const std::string & scope, const std::string & type, const std::string & name, si32 identifier);
  86. /// called at the very end of loading to check for any missing ID's
  87. void finalize();
  88. /// Returns list of all mods, from which at least one identifier has failed to resolve
  89. std::vector<std::string> getModsWithFailedRequests() const;
  90. };
  91. VCMI_LIB_NAMESPACE_END