IdentifierStorage.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. bool caseSensitive;
  34. /// Builds callback from identifier in form "targetMod:type.name"
  35. static ObjectCallback fromNameWithType(const std::string & scope, const std::string & fullName, const std::function<void(si32)> & callback, bool optional, bool caseSensitive = true);
  36. /// Builds callback from identifier in form "targetMod:name"
  37. 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, bool caseSensitive = true);
  38. private:
  39. ObjectCallback() = default;
  40. };
  41. struct ObjectData // entry created on ID registration
  42. {
  43. si32 id;
  44. std::string scope; /// scope in which this ID located
  45. bool operator==(const ObjectData & other) const
  46. {
  47. return id == other.id && scope == other.scope;
  48. }
  49. };
  50. std::multimap<std::string, ObjectData> registeredObjects;
  51. mutable std::vector<ObjectCallback> scheduledRequests;
  52. /// All non-optional requests that have failed to resolve, for debug & error reporting
  53. mutable std::vector<ObjectCallback> failedRequests;
  54. ELoadingState state = ELoadingState::LOADING;
  55. /// Helper method that dumps all registered identifier into log file
  56. void debugDumpIdentifiers();
  57. /// Check if identifier can be valid (camelCase, point as separator)
  58. static void checkIdentifier(const std::string & ID);
  59. void requestIdentifier(const ObjectCallback & callback) const;
  60. bool resolveIdentifier(const ObjectCallback & callback) const;
  61. std::vector<ObjectData> getPossibleIdentifiers(const ObjectCallback & callback) const;
  62. void showIdentifierResolutionErrorDetails(const ObjectCallback & callback) const;
  63. std::optional<si32> getIdentifierImpl(const ObjectCallback & callback, bool silent) const;
  64. public:
  65. CIdentifierStorage();
  66. virtual ~CIdentifierStorage() = default;
  67. /// request identifier for specific object name.
  68. /// Function callback will be called during ID resolution phase of loading
  69. void requestIdentifier(const std::string & scope, const std::string & type, const std::string & name, const std::function<void(si32)> & callback) const;
  70. ///fullName = [remoteScope:]type.name
  71. void requestIdentifier(const std::string & scope, const std::string & fullName, const std::function<void(si32)> & callback) const;
  72. void requestIdentifier(const std::string & type, const JsonNode & name, const std::function<void(si32)> & callback) const;
  73. void requestIdentifier(const JsonNode & name, const std::function<void(si32)> & callback) const;
  74. void requestIdentifierIfNotNull(const std::string & type, const JsonNode & name, const std::function<void(si32)> & callback) const;
  75. void requestIdentifierIfFound(const std::string & type, const JsonNode & name, const std::function<void(si32)> & callback) const;
  76. void requestIdentifierIfFound(const std::string & scope, const std::string & type, const std::string & name, const std::function<void(si32)> & callback) const;
  77. /// try to request ID. If ID with such name won't be loaded, callback function will not be called
  78. void tryRequestIdentifier(const std::string & scope, const std::string & type, const std::string & name, const std::function<void(si32)> & callback) const;
  79. void tryRequestIdentifier(const std::string & type, const JsonNode & name, const std::function<void(si32)> & callback) const;
  80. /// get identifier immediately. If identifier is not know and not silent call will result in error message
  81. std::optional<si32> getIdentifier(const std::string & scope, const std::string & type, const std::string & name, bool silent = false, bool caseSensitive = true) const;
  82. std::optional<si32> getIdentifier(const std::string & type, const JsonNode & name, bool silent = false, bool caseSensitive = true) const;
  83. std::optional<si32> getIdentifier(const JsonNode & name, bool silent = false, bool caseSensitive = true) const;
  84. std::optional<si32> getIdentifier(const std::string & scope, const std::string & fullName, bool silent = false, bool caseSensitive = true) const;
  85. /// registers new object
  86. void registerObject(const std::string & scope, const std::string & type, const std::string & name, si32 identifier);
  87. /// called at the very end of loading to check for any missing ID's
  88. void finalize();
  89. /// Returns list of all mods, from which at least one identifier has failed to resolve
  90. std::vector<std::string> getModsWithFailedRequests() const;
  91. };
  92. VCMI_LIB_NAMESPACE_END