l_base.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 "common/c_types.h"
  18. #include "common/c_internal.h"
  19. #include "common/helper.h"
  20. #include "gamedef.h"
  21. #include <unordered_map>
  22. extern "C" {
  23. #include <lua.h>
  24. #include <lauxlib.h>
  25. }
  26. #ifndef SERVER
  27. class Client;
  28. class GUIEngine;
  29. #endif
  30. class ScriptApiBase;
  31. class Server;
  32. class Environment;
  33. class ServerInventoryManager;
  34. class ModApiBase : protected LuaHelper {
  35. public:
  36. static ScriptApiBase* getScriptApiBase(lua_State *L);
  37. static Server* getServer(lua_State *L);
  38. static ServerInventoryManager *getServerInventoryMgr(lua_State *L);
  39. #ifndef SERVER
  40. static Client* getClient(lua_State *L);
  41. static GUIEngine* getGuiEngine(lua_State *L);
  42. #endif // !SERVER
  43. static IGameDef* getGameDef(lua_State *L);
  44. static Environment* getEnv(lua_State *L);
  45. // When we are not loading the mod, this function returns "."
  46. static std::string getCurrentModPath(lua_State *L);
  47. // Get an arbitrary subclass of ScriptApiBase
  48. // by using dynamic_cast<> on getScriptApiBase()
  49. template<typename T>
  50. static T* getScriptApi(lua_State *L) {
  51. ScriptApiBase *scriptIface = getScriptApiBase(L);
  52. T *scriptIfaceDowncast = dynamic_cast<T*>(scriptIface);
  53. if (!scriptIfaceDowncast) {
  54. throw LuaError("Requested unavailable ScriptApi - core engine bug!");
  55. }
  56. return scriptIfaceDowncast;
  57. }
  58. static bool registerFunction(lua_State *L,
  59. const char* name,
  60. lua_CFunction func,
  61. int top);
  62. /**
  63. * A wrapper for deprecated functions.
  64. *
  65. * When called, handles the deprecation according to user settings and then calls `func`.
  66. *
  67. * @throws Lua Error if required by the user settings.
  68. *
  69. * @param L Lua state
  70. * @param good Name of good function/method
  71. * @param bad Name of deprecated function/method
  72. * @param func Actual implementation of function
  73. * @return value from `func`
  74. */
  75. static int l_deprecated_function(lua_State *L, const char *good, const char *bad, lua_CFunction func);
  76. };