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.
 
 
 
 
 
 

88 lines
2.3 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. #include "lua_api/l_base.h"
  17. #include "lua_api/l_internal.h"
  18. #include "cpp_api/s_base.h"
  19. #include <mods.h>
  20. #include "server.h"
  21. #include <cmath>
  22. ScriptApiBase *ModApiBase::getScriptApiBase(lua_State *L)
  23. {
  24. // Get server from registry
  25. lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_SCRIPTAPI);
  26. ScriptApiBase *sapi_ptr = (ScriptApiBase*) lua_touserdata(L, -1);
  27. lua_pop(L, 1);
  28. return sapi_ptr;
  29. }
  30. Server *ModApiBase::getServer(lua_State *L)
  31. {
  32. return getScriptApiBase(L)->getServer();
  33. }
  34. #ifndef SERVER
  35. Client *ModApiBase::getClient(lua_State *L)
  36. {
  37. return getScriptApiBase(L)->getClient();
  38. }
  39. #endif
  40. IGameDef *ModApiBase::getGameDef(lua_State *L)
  41. {
  42. return getScriptApiBase(L)->getGameDef();
  43. }
  44. Environment *ModApiBase::getEnv(lua_State *L)
  45. {
  46. return getScriptApiBase(L)->getEnv();
  47. }
  48. GUIEngine *ModApiBase::getGuiEngine(lua_State *L)
  49. {
  50. return getScriptApiBase(L)->getGuiEngine();
  51. }
  52. std::string ModApiBase::getCurrentModPath(lua_State *L)
  53. {
  54. lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
  55. std::string current_mod_name = readParam<std::string>(L, -1, "");
  56. if (current_mod_name.empty())
  57. return ".";
  58. const ModSpec *mod = getServer(L)->getModSpec(current_mod_name);
  59. if (!mod)
  60. return ".";
  61. return mod->path;
  62. }
  63. bool ModApiBase::registerFunction(lua_State *L, const char *name,
  64. lua_CFunction func, int top)
  65. {
  66. // TODO: Check presence first!
  67. lua_pushcfunction(L, func);
  68. lua_setfield(L, top, name);
  69. return true;
  70. }