You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

157 lines
4.2 KiB

  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #ifndef S_BASE_H_
  17. #define S_BASE_H_
  18. #include <iostream>
  19. #include <string>
  20. #include <thread>
  21. #include <mutex>
  22. #include "common/helper.h"
  23. #include "util/basic_macros.h"
  24. extern "C" {
  25. #include <lua.h>
  26. }
  27. #include "irrlichttypes.h"
  28. #include "common/c_types.h"
  29. #include "common/c_internal.h"
  30. #define SCRIPTAPI_LOCK_DEBUG
  31. #define SCRIPTAPI_DEBUG
  32. // MUST be an invalid mod name so that mods can't
  33. // use that name to bypass security!
  34. #define BUILTIN_MOD_NAME "*builtin*"
  35. #define PCALL_RES(RES) do { \
  36. int result_ = (RES); \
  37. if (result_ != 0) { \
  38. scriptError(result_, __FUNCTION__); \
  39. } \
  40. } while (0)
  41. #define runCallbacks(nargs, mode) \
  42. runCallbacksRaw((nargs), (mode), __FUNCTION__)
  43. #define setOriginFromTable(index) \
  44. setOriginFromTableRaw(index, __FUNCTION__)
  45. enum class ScriptingType: u8 {
  46. Client,
  47. Server,
  48. MainMenu
  49. };
  50. class Server;
  51. #ifndef SERVER
  52. class Client;
  53. #endif
  54. class IGameDef;
  55. class Environment;
  56. class GUIEngine;
  57. class ServerActiveObject;
  58. class PlayerHPChangeReason;
  59. class ScriptApiBase : protected LuaHelper {
  60. public:
  61. ScriptApiBase();
  62. virtual ~ScriptApiBase();
  63. DISABLE_CLASS_COPY(ScriptApiBase);
  64. // These throw a ModError on failure
  65. void loadMod(const std::string &script_path, const std::string &mod_name);
  66. void loadScript(const std::string &script_path);
  67. #ifndef SERVER
  68. void loadModFromMemory(const std::string &mod_name);
  69. #endif
  70. void runCallbacksRaw(int nargs,
  71. RunCallbacksMode mode, const char *fxn);
  72. /* object */
  73. void addObjectReference(ServerActiveObject *cobj);
  74. void removeObjectReference(ServerActiveObject *cobj);
  75. IGameDef *getGameDef() { return m_gamedef; }
  76. Server* getServer();
  77. void setType(ScriptingType type) { m_type = type; }
  78. ScriptingType getType() { return m_type; }
  79. #ifndef SERVER
  80. Client* getClient();
  81. #endif
  82. std::string getOrigin() { return m_last_run_mod; }
  83. void setOriginDirect(const char *origin);
  84. void setOriginFromTableRaw(int index, const char *fxn);
  85. protected:
  86. friend class LuaABM;
  87. friend class LuaLBM;
  88. friend class InvRef;
  89. friend class ObjectRef;
  90. friend class NodeMetaRef;
  91. friend class ModApiBase;
  92. friend class ModApiEnvMod;
  93. friend class LuaVoxelManip;
  94. lua_State* getStack()
  95. { return m_luastack; }
  96. void realityCheck();
  97. void scriptError(int result, const char *fxn);
  98. void stackDump(std::ostream &o);
  99. void setGameDef(IGameDef* gamedef) { m_gamedef = gamedef; }
  100. Environment* getEnv() { return m_environment; }
  101. void setEnv(Environment* env) { m_environment = env; }
  102. GUIEngine* getGuiEngine() { return m_guiengine; }
  103. void setGuiEngine(GUIEngine* guiengine) { m_guiengine = guiengine; }
  104. void objectrefGetOrCreate(lua_State *L, ServerActiveObject *cobj);
  105. void pushPlayerHPChangeReason(lua_State *L, const PlayerHPChangeReason& reason);
  106. std::recursive_mutex m_luastackmutex;
  107. std::string m_last_run_mod;
  108. bool m_secure = false;
  109. #ifdef SCRIPTAPI_LOCK_DEBUG
  110. int m_lock_recursion_count;
  111. std::thread::id m_owning_thread;
  112. #endif
  113. private:
  114. static int luaPanic(lua_State *L);
  115. lua_State *m_luastack = nullptr;
  116. IGameDef *m_gamedef = nullptr;
  117. Environment *m_environment = nullptr;
  118. GUIEngine *m_guiengine = nullptr;
  119. ScriptingType m_type;
  120. };
  121. #endif /* S_BASE_H_ */