2
0

ScriptHandler.h 3.0 KB

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