ScriptHandler.cpp 6.5 KB

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