IdentifierStorage.h 4.8 KB

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