CModHandler.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #pragma once
  2. #include "filesystem/Filesystem.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. void afterLoadFinalization();
  79. };
  80. std::map<std::string, ContentTypeHandler> handlers;
  81. public:
  82. /// fully initialize object. Will cause reading of H3 config files
  83. CContentHandler();
  84. /// preloads all data from fileList as data from modName
  85. void preloadModData(std::string modName, JsonNode modConfig);
  86. /// actually loads data in mod
  87. void loadMod(std::string modName);
  88. /// all data was loaded, time for final validation / integration
  89. void afterLoadFinalization();
  90. };
  91. typedef std::string TModID;
  92. class DLL_LINKAGE CModInfo
  93. {
  94. public:
  95. /// identifier, identical to name of folder with mod
  96. std::string identifier;
  97. /// human-readable strings
  98. std::string name;
  99. std::string description;
  100. /// list of mods that should be loaded before this one
  101. std::set <TModID> dependencies;
  102. /// list of mods that can't be used in the same time as this one
  103. std::set <TModID> conflicts;
  104. // mod configuration (mod.json). (no need to store it right now)
  105. // std::shared_ptr<JsonNode> config; //TODO: unique_ptr can't be serialized
  106. template <typename Handler> void serialize(Handler &h, const int version)
  107. {
  108. h & identifier & description & name & dependencies & conflicts;
  109. }
  110. };
  111. class DLL_LINKAGE CModHandler
  112. {
  113. std::map <TModID, CModInfo> allMods;
  114. std::vector <TModID> activeMods;//active mods, in order in which they were loaded
  115. void loadConfigFromFile (std::string name);
  116. bool hasCircularDependency(TModID mod, std::set <TModID> currentList = std::set <TModID>()) const;
  117. //returns false if mod list is incorrect and prints error to console. Possible errors are:
  118. // - missing dependency mod
  119. // - conflicting mod in load order
  120. // - circular dependencies
  121. bool checkDependencies(const std::vector <TModID> & input) const;
  122. // returns load order in which all dependencies are resolved, e.g. loaded after required mods
  123. // function assumes that input list is valid (checkDependencies returned true)
  124. std::vector <TModID> resolveDependencies(std::vector<TModID> input) const;
  125. // helper for loadActiveMods. Loads content from list of files
  126. template<typename Handler>
  127. void handleData(Handler handler, const JsonNode & source, std::string listName, std::string schemaName);
  128. public:
  129. CIdentifierStorage identifiers;
  130. /// receives list of available mods and trying to load mod.json from all of them
  131. void initialize(std::vector<std::string> availableMods);
  132. /// returns list of mods that should be active with order in which they shoud be loaded
  133. std::vector<std::string> getActiveMods();
  134. CModInfo & getModData(TModID modId);
  135. /// load content from all available mods
  136. void beforeLoad();
  137. void loadGameContent();
  138. /// actions that should be triggered on map restart
  139. /// TODO: merge into appropriate handlers?
  140. void reload();
  141. struct DLL_LINKAGE hardcodedFeatures
  142. {
  143. JsonNode data;
  144. int CREEP_SIZE; // neutral stacks won't grow beyond this number
  145. int WEEKLY_GROWTH; //percent
  146. int NEUTRAL_STACK_EXP;
  147. int MAX_BUILDING_PER_TURN;
  148. bool DWELLINGS_ACCUMULATE_CREATURES;
  149. bool ALL_CREATURES_GET_DOUBLE_MONTHS;
  150. template <typename Handler> void serialize(Handler &h, const int version)
  151. {
  152. h & data & CREEP_SIZE & WEEKLY_GROWTH & NEUTRAL_STACK_EXP & MAX_BUILDING_PER_TURN;
  153. h & DWELLINGS_ACCUMULATE_CREATURES & ALL_CREATURES_GET_DOUBLE_MONTHS;
  154. }
  155. } settings;
  156. struct DLL_LINKAGE gameModules
  157. {
  158. bool STACK_EXP;
  159. bool STACK_ARTIFACT;
  160. bool COMMANDERS;
  161. bool MITHRIL;
  162. template <typename Handler> void serialize(Handler &h, const int version)
  163. {
  164. h & STACK_EXP & STACK_ARTIFACT & COMMANDERS & MITHRIL;
  165. }
  166. } modules;
  167. CModHandler();
  168. template <typename Handler> void serialize(Handler &h, const int version)
  169. {
  170. h & allMods & activeMods & settings & modules;
  171. }
  172. };