LuaScriptingContext.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * LuaScriptingContext.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 "LuaWrapper.h"
  12. #include "LuaReference.h"
  13. #include "../../lib/ScriptHandler.h"
  14. #include "../../lib/CScriptingModule.h"
  15. VCMI_LIB_NAMESPACE_BEGIN
  16. namespace scripting
  17. {
  18. class LuaContext : public ContextBase
  19. {
  20. public:
  21. static const std::string STATE_FIELD;
  22. LuaContext(const Script * source, const Environment * env_);
  23. virtual ~LuaContext();
  24. void run(const JsonNode & initialState) override;
  25. void run(ServerCallback * server, const JsonNode & initialState) override;
  26. //log error and return nil from LuaCFunction
  27. int errorRetVoid(const std::string & message);
  28. JsonNode callGlobal(const std::string & name, const JsonNode & parameters) override;
  29. JsonNode callGlobal(ServerCallback * cb, const std::string & name, const JsonNode & parameters) override;
  30. void getGlobal(const std::string & name, int & value) override;
  31. void getGlobal(const std::string & name, std::string & value) override;
  32. void getGlobal(const std::string & name, double & value) override;
  33. void getGlobal(const std::string & name, JsonNode & value) override;
  34. void setGlobal(const std::string & name, int value) override;
  35. void setGlobal(const std::string & name, const std::string & value) override;
  36. void setGlobal(const std::string & name, double value) override;
  37. void setGlobal(const std::string & name, const JsonNode & value) override;
  38. JsonNode saveState() override;
  39. void pop(JsonNode & value);
  40. void popAll();
  41. void push(const std::string & value);
  42. void push(lua_CFunction f, void * opaque);
  43. std::string toStringRaw(int index);
  44. private:
  45. lua_State * L;
  46. const Script * script;
  47. const Environment * env;
  48. std::shared_ptr<LuaReference> modules;
  49. std::shared_ptr<LuaReference> scriptClosure;
  50. void cleanupGlobals();
  51. void registerCore();
  52. //require global function
  53. static int require(lua_State * L);
  54. //print global function
  55. static int print(lua_State * L);
  56. static int logError(lua_State * L);
  57. //require function implementation
  58. int loadModule();
  59. int printImpl();
  60. int logErrorImpl();
  61. };
  62. }
  63. VCMI_LIB_NAMESPACE_END