GameSettings.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * GameSettings.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "IGameSettings.h"
  12. #include "json/JsonNode.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. class DLL_LINKAGE GameSettings final : public IGameSettings, boost::noncopyable
  15. {
  16. struct SettingOption
  17. {
  18. EGameSettings setting;
  19. std::string group;
  20. std::string key;
  21. };
  22. static constexpr int32_t OPTIONS_COUNT = static_cast<int32_t>(EGameSettings::OPTIONS_COUNT);
  23. static const std::vector<SettingOption> settingProperties;
  24. // contains base settings, like those defined in base game or mods
  25. std::array<JsonNode, OPTIONS_COUNT> baseSettings;
  26. // contains settings that were overriden, in map or in random map template
  27. std::array<JsonNode, OPTIONS_COUNT> overridenSettings;
  28. // for convenience / performance, contains actual settings - combined version of base and override settings
  29. std::array<JsonNode, OPTIONS_COUNT> actualSettings;
  30. // converts all existing overrides into a single json node for serialization
  31. JsonNode getAllOverrides() const;
  32. public:
  33. GameSettings();
  34. ~GameSettings();
  35. /// Loads settings as 'base settings' that can be overriden
  36. /// For settings defined in vcmi or in mods
  37. void loadBase(const JsonNode & input);
  38. /// Loads setting as an override, for use in maps or rmg templates
  39. /// undefined behavior if setting was already overriden (TODO: decide which approach is better - replace or append)
  40. void addOverride(EGameSettings option, const JsonNode & input);
  41. // loads all overrides from provided json node, for deserialization
  42. void loadOverrides(const JsonNode &);
  43. JsonNode getFullConfig() const override;
  44. const JsonNode & getValue(EGameSettings option) const override;
  45. template<typename Handler>
  46. void serialize(Handler & h)
  47. {
  48. if (h.saving)
  49. {
  50. JsonNode overrides = getAllOverrides();
  51. h & overrides;
  52. }
  53. else
  54. {
  55. JsonNode overrides;
  56. h & overrides;
  57. loadOverrides(overrides);
  58. }
  59. }
  60. };
  61. VCMI_LIB_NAMESPACE_END