ScriptHandler.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * ScriptHandler.cpp, 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. #include "StdInc.h"
  11. #include "ScriptHandler.h"
  12. #if SCRIPTING_ENABLED
  13. #include <vcmi/Services.h>
  14. #include <vcmi/Environment.h>
  15. #include "CGameInterface.h"
  16. #include "CScriptingModule.h"
  17. #include "VCMIDirs.h"
  18. #include "serializer/JsonDeserializer.h"
  19. #include "serializer/JsonSerializer.h"
  20. #include "filesystem/Filesystem.h"
  21. VCMI_LIB_NAMESPACE_BEGIN
  22. static const std::vector<std::string> IMPLEMENTS_MAP =
  23. {
  24. "ANYTHING",
  25. "BATTLE_EFFECT"
  26. };
  27. namespace scripting
  28. {
  29. ScriptImpl::ScriptImpl(const ScriptHandler * owner_):
  30. owner(owner_),
  31. implements(Implements::ANYTHING)
  32. {
  33. }
  34. ScriptImpl::~ScriptImpl() = default;
  35. void ScriptImpl::compile(vstd::CLoggerBase * logger)
  36. {
  37. code = host->compile(sourcePath, sourceText, logger);
  38. if(host == owner->erm)
  39. {
  40. host = owner->lua;
  41. sourceText = code;
  42. code = host->compile(getName(), getSource(), logger);
  43. }
  44. }
  45. std::shared_ptr<Context> ScriptImpl::createContext(const Environment * env) const
  46. {
  47. return host->createContextFor(this, env);
  48. }
  49. const std::string & ScriptImpl::getName() const
  50. {
  51. return identifier;
  52. }
  53. const std::string & ScriptImpl::getSource() const
  54. {
  55. return sourceText;
  56. }
  57. void ScriptImpl::performRegistration(Services * services) const
  58. {
  59. switch(implements)
  60. {
  61. case Implements::ANYTHING:
  62. break;
  63. case Implements::BATTLE_EFFECT:
  64. host->registerSpellEffect(services->spellEffects(), this);
  65. break;
  66. }
  67. }
  68. void ScriptImpl::serializeJson(vstd::CLoggerBase * logger, JsonSerializeFormat & handler)
  69. {
  70. handler.serializeString("source", sourcePath);
  71. handler.serializeEnum("implements", implements, Implements::ANYTHING, IMPLEMENTS_MAP);
  72. if(!handler.saving)
  73. {
  74. resolveHost();
  75. ResourcePath sourcePathId("SCRIPTS/" + sourcePath);
  76. auto rawData = CResourceHandler::get()->load(sourcePathId)->readAll();
  77. sourceText = std::string(reinterpret_cast<char *>(rawData.first.get()), rawData.second);
  78. compile(logger);
  79. }
  80. }
  81. void ScriptImpl::serializeJsonState(JsonSerializeFormat & handler)
  82. {
  83. handler.serializeString("sourcePath", sourcePath);
  84. handler.serializeString("sourceText", sourceText);
  85. handler.serializeString("code", code);
  86. handler.serializeEnum("implements", implements, Implements::ANYTHING, IMPLEMENTS_MAP);
  87. if(!handler.saving)
  88. {
  89. host = owner->lua;
  90. }
  91. }
  92. void ScriptImpl::resolveHost()
  93. {
  94. ResourcePath sourcePathId(sourcePath);
  95. if(sourcePathId.getType() == EResType::ERM)
  96. host = owner->erm;
  97. else if(sourcePathId.getType() == EResType::LUA)
  98. host = owner->lua;
  99. else
  100. throw std::runtime_error("Unknown script language in:" + sourcePath);
  101. }
  102. PoolImpl::PoolImpl(const Environment * ENV)
  103. : env(ENV),
  104. srv(nullptr)
  105. {
  106. }
  107. PoolImpl::PoolImpl(const Environment * ENV, ServerCallback * SRV)
  108. : env(ENV),
  109. srv(SRV)
  110. {
  111. }
  112. std::shared_ptr<Context> PoolImpl::getContext(const Script * script)
  113. {
  114. auto iter = cache.find(script);
  115. if(iter == cache.end())
  116. {
  117. auto context = script->createContext(env);
  118. cache[script] = context;
  119. const auto & key = script->getName();
  120. const JsonNode & scriptState = state[key];
  121. if(srv)
  122. context->run(srv, scriptState);
  123. else
  124. context->run(scriptState);
  125. return context;
  126. }
  127. else
  128. {
  129. return iter->second;
  130. }
  131. }
  132. void PoolImpl::serializeState(const bool saving, JsonNode & data)
  133. {
  134. if(saving)
  135. {
  136. for(auto & scriptAndContext : cache)
  137. {
  138. const auto * script = scriptAndContext.first;
  139. auto context = scriptAndContext.second;
  140. state[script->getName()] = context->saveState();
  141. data = state;
  142. }
  143. }
  144. else
  145. {
  146. state = data;
  147. }
  148. }
  149. ScriptHandler::ScriptHandler()
  150. :erm(nullptr), lua(nullptr)
  151. {
  152. boost::filesystem::path filePath = VCMIDirs::get().fullLibraryPath("scripting", "vcmiERM");
  153. if (boost::filesystem::exists(filePath))
  154. {
  155. erm = CDynLibHandler::getNewScriptingModule(filePath);
  156. }
  157. filePath = VCMIDirs::get().fullLibraryPath("scripting", "vcmiLua");
  158. if (boost::filesystem::exists(filePath))
  159. {
  160. lua = CDynLibHandler::getNewScriptingModule(filePath);
  161. }
  162. }
  163. ScriptHandler::~ScriptHandler() = default;
  164. const Script * ScriptHandler::resolveScript(const std::string & name) const
  165. {
  166. auto iter = objects.find(name);
  167. if(iter == objects.end())
  168. {
  169. logMod->error("Unknown script id '%s'", name);
  170. return nullptr;
  171. }
  172. else
  173. {
  174. return iter->second.get();
  175. }
  176. }
  177. std::vector<JsonNode> ScriptHandler::loadLegacyData()
  178. {
  179. return std::vector<JsonNode>();
  180. }
  181. ScriptPtr ScriptHandler::loadFromJson(vstd::CLoggerBase * logger, const std::string & scope,
  182. const JsonNode & json, const std::string & identifier) const
  183. {
  184. ScriptPtr ret = std::make_shared<ScriptImpl>(this);
  185. JsonDeserializer handler(nullptr, json);
  186. ret->identifier = identifier;
  187. ret->serializeJson(logger, handler);
  188. return ret;
  189. }
  190. void ScriptHandler::loadObject(std::string scope, std::string name, const JsonNode & data)
  191. {
  192. auto object = loadFromJson(logMod, scope, data, name);
  193. objects[object->identifier] = object;
  194. }
  195. void ScriptHandler::loadObject(std::string scope, std::string name, const JsonNode & data, size_t index)
  196. {
  197. throw std::runtime_error("No legacy data load allowed for scripts");
  198. }
  199. void ScriptHandler::performRegistration(Services * services) const
  200. {
  201. for(const auto & keyValue : objects)
  202. {
  203. auto script = keyValue.second;
  204. script->performRegistration(services);
  205. }
  206. }
  207. void ScriptHandler::run(std::shared_ptr<Pool> pool) const
  208. {
  209. for(const auto & keyValue : objects)
  210. {
  211. auto script = keyValue.second;
  212. if(script->implements == ScriptImpl::Implements::ANYTHING)
  213. {
  214. auto context = pool->getContext(script.get());
  215. //todo: consider explicit run for generic scripts
  216. }
  217. }
  218. }
  219. void ScriptHandler::loadState(const JsonNode & state)
  220. {
  221. objects.clear();
  222. const JsonNode & scriptsData = state["scripts"];
  223. for(const auto & keyValue : scriptsData.Struct())
  224. {
  225. std::string name = keyValue.first;
  226. const JsonNode & scriptData = keyValue.second;
  227. ScriptPtr script = std::make_shared<ScriptImpl>(this);
  228. JsonDeserializer handler(nullptr, scriptData);
  229. script->serializeJsonState(handler);
  230. objects[name] = script;
  231. }
  232. }
  233. void ScriptHandler::saveState(JsonNode & state)
  234. {
  235. JsonNode & scriptsData = state["scripts"];
  236. for(auto & keyValue : objects)
  237. {
  238. std::string name = keyValue.first;
  239. ScriptPtr script = keyValue.second;
  240. JsonNode scriptData;
  241. JsonSerializer handler(nullptr, scriptData);
  242. script->serializeJsonState(handler);
  243. scriptsData[name] = scriptData;
  244. }
  245. }
  246. }
  247. VCMI_LIB_NAMESPACE_END
  248. #endif