CModHandler.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #pragma once
  2. #include "filesystem/CResourceLoader.h"
  3. #include "VCMI_Lib.h"
  4. #include "JsonNode.h"
  5. /*
  6. * CModHandler.h, part of VCMI engine
  7. *
  8. * Authors: listed in file AUTHORS in main folder
  9. *
  10. * License: GNU General Public License v2.0 or later
  11. * Full text of license available in license.txt file, in main folder
  12. *
  13. */
  14. class CModHandler;
  15. class CModIndentifier;
  16. class CModInfo;
  17. class JsonNode;
  18. class IHandlerBase;
  19. /// class that stores all object identifiers strings and maps them to numeric ID's
  20. /// if possible, objects ID's should be in format <type>.<name>, camelCase e.g. "creature.grandElf"
  21. class CIdentifierStorage
  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. ObjectCallback(std::string localScope, std::string remoteScope, std::string type, std::string name, const std::function<void(si32)> & callback);
  31. };
  32. struct ObjectData // entry created on ID registration
  33. {
  34. si32 id;
  35. std::string scope; /// scope in which this ID located
  36. };
  37. std::multimap<std::string, ObjectData > registeredObjects;
  38. std::vector<ObjectCallback> scheduledRequests;
  39. /// Check if identifier can be valid (camelCase, point as separator)
  40. void checkIdentifier(std::string & ID);
  41. void requestIdentifier(ObjectCallback callback);
  42. bool resolveIdentifier(const ObjectCallback & callback);
  43. public:
  44. /// request identifier for specific object name. If ID is not yet resolved callback will be queued
  45. /// and will be called later
  46. void requestIdentifier(std::string scope, std::string type, std::string name, const std::function<void(si32)> & callback);
  47. void requestIdentifier(std::string type, const JsonNode & name, const std::function<void(si32)> & callback);
  48. void requestIdentifier(const JsonNode & name, const std::function<void(si32)> & callback);
  49. /// registers new object, calls all associated callbacks
  50. void registerObject(std::string scope, std::string type, std::string name, si32 identifier);
  51. /// called at the very end of loading to check for any missing ID's
  52. void finalize();
  53. };
  54. /// class used to load all game data into handlers. Used only during loading
  55. class CContentHandler
  56. {
  57. /// internal type to handle loading of one data type (e.g. artifacts, creatures)
  58. class ContentTypeHandler
  59. {
  60. struct ModInfo
  61. {
  62. /// mod data from this mod and for this mod
  63. JsonNode modData;
  64. /// mod data for this mod from other mods (patches)
  65. JsonNode patches;
  66. };
  67. /// handler to which all data will be loaded
  68. IHandlerBase * handler;
  69. std::string objectName;
  70. /// contains all loaded H3 data
  71. std::vector<JsonNode> originalData;
  72. std::map<std::string, ModInfo> modData;
  73. public:
  74. ContentTypeHandler(IHandlerBase * handler, std::string objectName);
  75. /// local version of methods in ContentHandler
  76. void preloadModData(std::string modName, std::vector<std::string> fileList);
  77. void loadMod(std::string modName);
  78. };
  79. std::map<std::string, ContentTypeHandler> handlers;
  80. public:
  81. /// fully initialize object. Will cause reading of H3 config files
  82. CContentHandler();
  83. /// preloads all data from fileList as data from modName
  84. void preloadModData(std::string modName, JsonNode modConfig);
  85. /// actually loads data in mod
  86. void loadMod(std::string modName);
  87. };
  88. typedef std::string TModID;
  89. class DLL_LINKAGE CModInfo
  90. {
  91. public:
  92. /// identifier, identical to name of folder with mod
  93. std::string identifier;
  94. /// human-readable strings
  95. std::string name;
  96. std::string description;
  97. /// list of mods that should be loaded before this one
  98. std::set <TModID> dependencies;
  99. /// list of mods that can't be used in the same time as this one
  100. std::set <TModID> conflicts;
  101. // mod configuration (mod.json). (no need to store it right now)
  102. // std::shared_ptr<JsonNode> config; //TODO: unique_ptr can't be serialized
  103. template <typename Handler> void serialize(Handler &h, const int version)
  104. {
  105. h & identifier & description & name & dependencies & conflicts;
  106. }
  107. };
  108. class DLL_LINKAGE CModHandler
  109. {
  110. std::map <TModID, CModInfo> allMods;
  111. std::vector <TModID> activeMods;//active mods, in order in which they were loaded
  112. void loadConfigFromFile (std::string name);
  113. bool hasCircularDependency(TModID mod, std::set <TModID> currentList = std::set <TModID>()) const;
  114. //returns false if mod list is incorrect and prints error to console. Possible errors are:
  115. // - missing dependency mod
  116. // - conflicting mod in load order
  117. // - circular dependencies
  118. bool checkDependencies(const std::vector <TModID> & input) const;
  119. // returns load order in which all dependencies are resolved, e.g. loaded after required mods
  120. // function assumes that input list is valid (checkDependencies returned true)
  121. std::vector <TModID> resolveDependencies(std::vector<TModID> input) const;
  122. // helper for loadActiveMods. Loads content from list of files
  123. template<typename Handler>
  124. void handleData(Handler handler, const JsonNode & source, std::string listName, std::string schemaName);
  125. public:
  126. CIdentifierStorage identifiers;
  127. /// receives list of available mods and trying to load mod.json from all of them
  128. void initialize(std::vector<std::string> availableMods);
  129. /// returns list of mods that should be active with order in which they shoud be loaded
  130. std::vector<std::string> getActiveMods();
  131. CModInfo & getModData(TModID modId);
  132. /// load content from all available mods
  133. void beforeLoad();
  134. void loadGameContent();
  135. /// actions that should be triggered on map restart
  136. /// TODO: merge into appropriate handlers?
  137. void reload();
  138. struct DLL_LINKAGE hardcodedFeatures
  139. {
  140. JsonNode data;
  141. int CREEP_SIZE; // neutral stacks won't grow beyond this number
  142. int WEEKLY_GROWTH; //percent
  143. int NEUTRAL_STACK_EXP;
  144. int MAX_BUILDING_PER_TURN;
  145. bool DWELLINGS_ACCUMULATE_CREATURES;
  146. bool ALL_CREATURES_GET_DOUBLE_MONTHS;
  147. template <typename Handler> void serialize(Handler &h, const int version)
  148. {
  149. h & data & CREEP_SIZE & WEEKLY_GROWTH & NEUTRAL_STACK_EXP & MAX_BUILDING_PER_TURN;
  150. h & DWELLINGS_ACCUMULATE_CREATURES & ALL_CREATURES_GET_DOUBLE_MONTHS;
  151. }
  152. } settings;
  153. struct DLL_LINKAGE gameModules
  154. {
  155. bool STACK_EXP;
  156. bool STACK_ARTIFACT;
  157. bool COMMANDERS;
  158. bool MITHRIL;
  159. template <typename Handler> void serialize(Handler &h, const int version)
  160. {
  161. h & STACK_EXP & STACK_ARTIFACT & COMMANDERS & MITHRIL;
  162. }
  163. } modules;
  164. CModHandler();
  165. template <typename Handler> void serialize(Handler &h, const int version)
  166. {
  167. h & allMods & activeMods & settings & modules;
  168. }
  169. };