ScriptHandler.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * ScriptHandler.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 <vcmi/scripting/Service.h>
  12. #include "IHandlerBase.h"
  13. #include "JsonNode.h"
  14. class JsonNode;
  15. class JsonSerializeFormat;
  16. class Services;
  17. namespace scripting
  18. {
  19. class Module;
  20. class ScriptImpl;
  21. class ScriptHandler;
  22. using ModulePtr = std::shared_ptr<Module>;
  23. using ScriptPtr = std::shared_ptr<ScriptImpl>;
  24. using ScriptMap = std::map<std::string, ScriptPtr>;
  25. class DLL_LINKAGE ScriptImpl : public Script
  26. {
  27. public:
  28. enum class Implements
  29. {
  30. ANYTHING,
  31. BATTLE_EFFECT
  32. //todo: adventure effect, map object(unified with building), server query, client query(with gui), may be smth else
  33. };
  34. Implements implements;
  35. std::string identifier;
  36. std::string sourcePath;
  37. std::string sourceText;
  38. std::string code;
  39. ModulePtr host;
  40. ScriptImpl(const ScriptHandler * owner_);
  41. virtual ~ScriptImpl();
  42. void compile(vstd::CLoggerBase * logger);
  43. void serializeJson(vstd::CLoggerBase * logger, JsonSerializeFormat & handler);
  44. void serializeJsonState(JsonSerializeFormat & handler);
  45. std::shared_ptr<Context> createContext(const Environment * env) const override;
  46. const std::string & getName() const override;
  47. const std::string & getSource() const override;
  48. void performRegistration(::Services * services) const;
  49. private:
  50. const ScriptHandler * owner;
  51. void resolveHost();
  52. };
  53. class DLL_LINKAGE PoolImpl : public Pool
  54. {
  55. public:
  56. PoolImpl(const Environment * ENV);
  57. PoolImpl(const Environment * ENV, ServerCallback * SRV);
  58. std::shared_ptr<Context> getContext(const Script * script) override;
  59. void serializeState(const bool saving, JsonNode & data) override;
  60. private:
  61. std::map<const Script *, std::shared_ptr<Context>> cache;
  62. JsonNode state;
  63. const Environment * env;
  64. ServerCallback * srv;
  65. };
  66. class DLL_LINKAGE ScriptHandler : public ::IHandlerBase, public Service
  67. {
  68. public:
  69. ScriptMap objects;
  70. ScriptHandler();
  71. virtual ~ScriptHandler();
  72. const Script * resolveScript(const std::string & name) const;
  73. std::vector<bool> getDefaultAllowed() const override;
  74. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  75. ScriptPtr loadFromJson(vstd::CLoggerBase * logger, const std::string & scope, const JsonNode & json, const std::string & identifier) const;
  76. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  77. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  78. void performRegistration(Services * services) const override;
  79. void run(std::shared_ptr<Pool> pool) const override;
  80. template <typename Handler> void serialize(Handler & h, const int version)
  81. {
  82. JsonNode state;
  83. if(h.saving)
  84. saveState(state);
  85. h & state;
  86. if(!h.saving)
  87. loadState(state);
  88. }
  89. ModulePtr erm;
  90. ModulePtr lua;
  91. protected:
  92. private:
  93. friend class ScriptImpl;
  94. void loadState(const JsonNode & state);
  95. void saveState(JsonNode & state);
  96. };
  97. }