s_base.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #pragma once
  17. #include <iostream>
  18. #include <string>
  19. #include <thread>
  20. #include <mutex>
  21. #include <unordered_map>
  22. #include "util/basic_macros.h"
  23. extern "C" {
  24. #include <lua.h>
  25. #include <lualib.h>
  26. }
  27. #include "irrlichttypes.h"
  28. #include "common/c_types.h"
  29. #include "common/c_internal.h"
  30. #include "debug.h"
  31. #include "config.h"
  32. #define SCRIPTAPI_LOCK_DEBUG
  33. #define SCRIPTAPI_DEBUG
  34. // MUST be an invalid mod name so that mods can't
  35. // use that name to bypass security!
  36. #define BUILTIN_MOD_NAME "*builtin*"
  37. #define PCALL_RES(RES) { \
  38. int result_ = (RES); \
  39. if (result_ != 0) { \
  40. scriptError(result_, __FUNCTION__); \
  41. } \
  42. }
  43. #define runCallbacks(nargs, mode) \
  44. runCallbacksRaw((nargs), (mode), __FUNCTION__)
  45. #define setOriginFromTable(index) \
  46. setOriginFromTableRaw(index, __FUNCTION__)
  47. enum class ScriptingType: u8 {
  48. Async,
  49. Client,
  50. MainMenu,
  51. Server
  52. };
  53. class Server;
  54. #ifndef SERVER
  55. class Client;
  56. #endif
  57. class IGameDef;
  58. class Environment;
  59. class GUIEngine;
  60. class ServerActiveObject;
  61. struct PlayerHPChangeReason;
  62. class ScriptApiBase {
  63. public:
  64. ScriptApiBase(ScriptingType type);
  65. // fake constructor to allow script API classes (e.g ScriptApiEnv) to virtually inherit from this one.
  66. ScriptApiBase()
  67. {
  68. FATAL_ERROR("ScriptApiBase created without ScriptingType!");
  69. }
  70. virtual ~ScriptApiBase();
  71. DISABLE_CLASS_COPY(ScriptApiBase);
  72. // These throw a ModError on failure
  73. void loadMod(const std::string &script_path, const std::string &mod_name);
  74. void loadScript(const std::string &script_path);
  75. #ifndef SERVER
  76. void loadModFromMemory(const std::string &mod_name);
  77. #endif
  78. void runCallbacksRaw(int nargs,
  79. RunCallbacksMode mode, const char *fxn);
  80. /* object */
  81. void addObjectReference(ServerActiveObject *cobj);
  82. void removeObjectReference(ServerActiveObject *cobj);
  83. IGameDef *getGameDef() { return m_gamedef; }
  84. Server* getServer();
  85. ScriptingType getType() { return m_type; }
  86. #ifndef SERVER
  87. Client* getClient();
  88. #endif
  89. std::string getOrigin() { return m_last_run_mod; }
  90. void setOriginDirect(const char *origin);
  91. void setOriginFromTableRaw(int index, const char *fxn);
  92. void clientOpenLibs(lua_State *L);
  93. protected:
  94. friend class LuaABM;
  95. friend class LuaLBM;
  96. friend class InvRef;
  97. friend class ObjectRef;
  98. friend class NodeMetaRef;
  99. friend class ModApiBase;
  100. friend class ModApiEnvMod;
  101. friend class LuaVoxelManip;
  102. lua_State* getStack()
  103. { return m_luastack; }
  104. void realityCheck();
  105. void scriptError(int result, const char *fxn);
  106. void stackDump(std::ostream &o);
  107. void setGameDef(IGameDef* gamedef) { m_gamedef = gamedef; }
  108. Environment* getEnv() { return m_environment; }
  109. void setEnv(Environment* env) { m_environment = env; }
  110. GUIEngine* getGuiEngine() { return m_guiengine; }
  111. void setGuiEngine(GUIEngine* guiengine) { m_guiengine = guiengine; }
  112. void objectrefGetOrCreate(lua_State *L, ServerActiveObject *cobj);
  113. void pushPlayerHPChangeReason(lua_State *L, const PlayerHPChangeReason& reason);
  114. std::recursive_mutex m_luastackmutex;
  115. std::string m_last_run_mod;
  116. bool m_secure = false;
  117. #ifdef SCRIPTAPI_LOCK_DEBUG
  118. int m_lock_recursion_count{};
  119. std::thread::id m_owning_thread;
  120. #endif
  121. private:
  122. static int luaPanic(lua_State *L);
  123. lua_State *m_luastack = nullptr;
  124. IGameDef *m_gamedef = nullptr;
  125. Environment *m_environment = nullptr;
  126. GUIEngine *m_guiengine = nullptr;
  127. ScriptingType m_type;
  128. };