IdentifierStorage.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. ELoadingState state = ELoadingState::LOADING;
  51. /// Helper method that dumps all registered identifier into log file
  52. void debugDumpIdentifiers();
  53. /// Check if identifier can be valid (camelCase, point as separator)
  54. static void checkIdentifier(std::string & ID);
  55. void requestIdentifier(ObjectCallback callback) const;
  56. bool resolveIdentifier(const ObjectCallback & callback) const;
  57. std::vector<ObjectData> getPossibleIdentifiers(const ObjectCallback & callback) const;
  58. void showIdentifierResolutionErrorDetails(const ObjectCallback & callback) const;
  59. std::optional<si32> getIdentifierImpl(const ObjectCallback & callback, bool silent) const;
  60. public:
  61. CIdentifierStorage();
  62. virtual ~CIdentifierStorage() = default;
  63. /// request identifier for specific object name.
  64. /// Function callback will be called during ID resolution phase of loading
  65. void requestIdentifier(const std::string & scope, const std::string & type, const std::string & name, const std::function<void(si32)> & callback) const;
  66. ///fullName = [remoteScope:]type.name
  67. void requestIdentifier(const std::string & scope, const std::string & fullName, const std::function<void(si32)> & callback) const;
  68. void requestIdentifier(const std::string & type, const JsonNode & name, const std::function<void(si32)> & callback) const;
  69. void requestIdentifier(const JsonNode & name, const std::function<void(si32)> & callback) const;
  70. void requestIdentifierOptional(const std::string & type, const JsonNode & name, const std::function<void(si32)> & callback) const;
  71. /// try to request ID. If ID with such name won't be loaded, callback function will not be called
  72. void tryRequestIdentifier(const std::string & scope, const std::string & type, const std::string & name, const std::function<void(si32)> & callback) const;
  73. void tryRequestIdentifier(const std::string & type, const JsonNode & name, const std::function<void(si32)> & callback) const;
  74. /// get identifier immediately. If identifier is not know and not silent call will result in error message
  75. std::optional<si32> getIdentifier(const std::string & scope, const std::string & type, const std::string & name, bool silent = false) const;
  76. std::optional<si32> getIdentifier(const std::string & type, const JsonNode & name, bool silent = false) const;
  77. std::optional<si32> getIdentifier(const JsonNode & name, bool silent = false) const;
  78. std::optional<si32> getIdentifier(const std::string & scope, const std::string & fullName, bool silent = false) const;
  79. /// registers new object
  80. void registerObject(const std::string & scope, const std::string & type, const std::string & name, si32 identifier);
  81. /// called at the very end of loading to check for any missing ID's
  82. void finalize();
  83. };
  84. VCMI_LIB_NAMESPACE_END