CModHandler.h 7.0 KB

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