CModHandler.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 incorrec 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. public:
  69. CIdentifierStorage identifiers;
  70. /// receives list of available mods and trying to load mod.json from all of them
  71. void initialize(std::vector<std::string> availableMods);
  72. /// returns list of mods that should be active with order in which they shoud be loaded
  73. std::vector<std::string> getActiveMods();
  74. /// load content from all available mods
  75. void loadActiveMods();
  76. /// actions that should be triggered on map restart
  77. /// TODO: merge into appropriate handlers?
  78. void reload();
  79. struct DLL_LINKAGE hardcodedFeatures
  80. {
  81. int CREEP_SIZE; // neutral stacks won't grow beyond this number
  82. int WEEKLY_GROWTH; //percent
  83. int NEUTRAL_STACK_EXP;
  84. int MAX_BUILDING_PER_TURN;
  85. bool DWELLINGS_ACCUMULATE_CREATURES;
  86. bool ALL_CREATURES_GET_DOUBLE_MONTHS;
  87. template <typename Handler> void serialize(Handler &h, const int version)
  88. {
  89. h & CREEP_SIZE & WEEKLY_GROWTH & NEUTRAL_STACK_EXP;
  90. h & DWELLINGS_ACCUMULATE_CREATURES & ALL_CREATURES_GET_DOUBLE_MONTHS;
  91. }
  92. } settings;
  93. struct DLL_LINKAGE gameModules
  94. {
  95. bool STACK_EXP;
  96. bool STACK_ARTIFACT;
  97. bool COMMANDERS;
  98. bool MITHRIL;
  99. template <typename Handler> void serialize(Handler &h, const int version)
  100. {
  101. h & STACK_EXP & STACK_ARTIFACT & COMMANDERS & MITHRIL;
  102. }
  103. } modules;
  104. CModHandler();
  105. template <typename Handler> void serialize(Handler &h, const int version)
  106. {
  107. h & allMods & activeMods & settings & modules;
  108. }
  109. };