CModHandler.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #pragma once
  2. #include "filesystem/CResourceLoader.h"
  3. #include "VCMI_Lib.h"
  4. /*
  5. * CModHandler.h, part of VCMI engine
  6. *
  7. * Authors: listed in file AUTHORS in main folder
  8. *
  9. * License: GNU General Public License v2.0 or later
  10. * Full text of license available in license.txt file, in main folder
  11. *
  12. */
  13. class CModHandler;
  14. class CModIndentifier;
  15. class CModInfo;
  16. class JsonNode;
  17. /// class that stores all object identifiers strings and maps them to numeric ID's
  18. /// if possible, objects ID's should be in format <type>.<name>, camelCase e.g. "creature.grandElf"
  19. class CIdentifierStorage
  20. {
  21. std::map<std::string, si32 > registeredObjects;
  22. std::map<std::string, std::vector<boost::function<void(si32)> > > missingObjects;
  23. //Check if identifier can be valid (camelCase, point as separator)
  24. void checkIdentifier(std::string & ID);
  25. public:
  26. /// request identifier for specific object name. If ID is not yet resolved callback will be queued
  27. /// and will be called later
  28. void requestIdentifier(std::string name, const boost::function<void(si32)> & callback);
  29. /// registers new object, calls all associated callbacks
  30. void registerObject(std::string name, si32 identifier);
  31. /// called at the very end of loading to check for any missing ID's
  32. void finalize() const;
  33. };
  34. typedef std::string TModID;
  35. class DLL_LINKAGE CModInfo
  36. {
  37. public:
  38. /// identifier, identical to name of folder with mod
  39. std::string identifier;
  40. /// human-readable strings
  41. std::string name;
  42. std::string description;
  43. /// list of mods that should be loaded before this one
  44. std::set <TModID> dependencies;
  45. /// list of mods that can't be used in the same time as this one
  46. std::set <TModID> conflicts;
  47. // mod configuration (mod.json). (no need to store it right now)
  48. // std::shared_ptr<JsonNode> config; //TODO: unique_ptr can't be serialized
  49. template <typename Handler> void serialize(Handler &h, const int version)
  50. {
  51. h & identifier & description & name & dependencies & conflicts;
  52. }
  53. };
  54. class DLL_LINKAGE CModHandler
  55. {
  56. std::map <TModID, CModInfo> allMods;
  57. std::vector <TModID> activeMods;//active mods, in order in which they were loaded
  58. void loadConfigFromFile (std::string name);
  59. bool hasCircularDependency(TModID mod, std::set <TModID> currentList = std::set <TModID>()) const;
  60. //returns false if mod list is incorrect and prints error to console. Possible errors are:
  61. // - missing dependency mod
  62. // - conflicting mod in load order
  63. // - circular dependencies
  64. bool checkDependencies(const std::vector <TModID> & input) const;
  65. // returns load order in which all dependencies are resolved, e.g. loaded after required mods
  66. // function assumes that input list is valid (checkDependencies returned true)
  67. std::vector <TModID> resolveDependencies(std::vector<TModID> input) const;
  68. // helper for loadActiveMods. Loads content from list of files
  69. template<typename Handler>
  70. void handleData(Handler handler, const JsonNode & source, std::string listName, std::string schemaName);
  71. public:
  72. CIdentifierStorage identifiers;
  73. /// receives list of available mods and trying to load mod.json from all of them
  74. void initialize(std::vector<std::string> availableMods);
  75. /// returns list of mods that should be active with order in which they shoud be loaded
  76. std::vector<std::string> getActiveMods();
  77. /// load content from all available mods
  78. void loadActiveMods();
  79. /// actions that should be triggered on map restart
  80. /// TODO: merge into appropriate handlers?
  81. void reload();
  82. struct DLL_LINKAGE hardcodedFeatures
  83. {
  84. int CREEP_SIZE; // neutral stacks won't grow beyond this number
  85. int WEEKLY_GROWTH; //percent
  86. int NEUTRAL_STACK_EXP;
  87. int MAX_BUILDING_PER_TURN;
  88. bool DWELLINGS_ACCUMULATE_CREATURES;
  89. bool ALL_CREATURES_GET_DOUBLE_MONTHS;
  90. template <typename Handler> void serialize(Handler &h, const int version)
  91. {
  92. h & CREEP_SIZE & WEEKLY_GROWTH & NEUTRAL_STACK_EXP;
  93. h & DWELLINGS_ACCUMULATE_CREATURES & ALL_CREATURES_GET_DOUBLE_MONTHS;
  94. }
  95. } settings;
  96. struct DLL_LINKAGE gameModules
  97. {
  98. bool STACK_EXP;
  99. bool STACK_ARTIFACT;
  100. bool COMMANDERS;
  101. bool MITHRIL;
  102. template <typename Handler> void serialize(Handler &h, const int version)
  103. {
  104. h & STACK_EXP & STACK_ARTIFACT & COMMANDERS & MITHRIL;
  105. }
  106. } modules;
  107. CModHandler();
  108. template <typename Handler> void serialize(Handler &h, const int version)
  109. {
  110. h & allMods & activeMods & settings & modules;
  111. }
  112. };