ScriptHandler.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. VCMI_LIB_NAMESPACE_BEGIN
  16. class JsonNode;
  17. class JsonSerializeFormat;
  18. class Services;
  19. namespace scripting
  20. {
  21. class Module;
  22. class ScriptImpl;
  23. class ScriptHandler;
  24. using ModulePtr = std::shared_ptr<Module>;
  25. using ScriptPtr = std::shared_ptr<ScriptImpl>;
  26. using ScriptMap = std::map<std::string, ScriptPtr>;
  27. class DLL_LINKAGE ScriptImpl : public Script
  28. {
  29. public:
  30. enum class Implements
  31. {
  32. ANYTHING,
  33. BATTLE_EFFECT
  34. //todo: adventure effect, map object(unified with building), server query, client query(with gui), may be smth else
  35. };
  36. Implements implements;
  37. std::string identifier;
  38. std::string sourcePath;
  39. std::string sourceText;
  40. std::string code;
  41. ModulePtr host;
  42. ScriptImpl(const ScriptHandler * owner_);
  43. virtual ~ScriptImpl();
  44. void compile(vstd::CLoggerBase * logger);
  45. void serializeJson(vstd::CLoggerBase * logger, JsonSerializeFormat & handler);
  46. void serializeJsonState(JsonSerializeFormat & handler);
  47. std::shared_ptr<Context> createContext(const Environment * env) const override;
  48. const std::string & getName() const override;
  49. const std::string & getSource() const override;
  50. void performRegistration(Services * services) const;
  51. private:
  52. const ScriptHandler * owner;
  53. void resolveHost();
  54. };
  55. class DLL_LINKAGE PoolImpl : public Pool
  56. {
  57. public:
  58. PoolImpl(const Environment * ENV);
  59. PoolImpl(const Environment * ENV, ServerCallback * SRV);
  60. std::shared_ptr<Context> getContext(const Script * script) override;
  61. void serializeState(const bool saving, JsonNode & data) override;
  62. private:
  63. std::map<const Script *, std::shared_ptr<Context>> cache;
  64. JsonNode state;
  65. const Environment * env;
  66. ServerCallback * srv;
  67. };
  68. class DLL_LINKAGE ScriptHandler : public IHandlerBase, public Service
  69. {
  70. public:
  71. ScriptMap objects;
  72. ScriptHandler();
  73. virtual ~ScriptHandler();
  74. const Script * resolveScript(const std::string & name) const;
  75. std::vector<JsonNode> loadLegacyData() 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. VCMI_LIB_NAMESPACE_END
  100. #endif