scripting_mainmenu.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 "scripting_mainmenu.h"
  17. #include "content/mods.h"
  18. #include "cpp_api/s_internal.h"
  19. #include "lua_api/l_base.h"
  20. #include "lua_api/l_http.h"
  21. #include "lua_api/l_mainmenu.h"
  22. #include "lua_api/l_sound.h"
  23. #include "lua_api/l_util.h"
  24. #include "lua_api/l_settings.h"
  25. #include "log.h"
  26. extern "C" {
  27. #include "lualib.h"
  28. }
  29. #define MAINMENU_NUM_ASYNC_THREADS 4
  30. MainMenuScripting::MainMenuScripting(GUIEngine* guiengine):
  31. ScriptApiBase(ScriptingType::MainMenu)
  32. {
  33. setGuiEngine(guiengine);
  34. SCRIPTAPI_PRECHECKHEADER
  35. lua_getglobal(L, "core");
  36. int top = lua_gettop(L);
  37. lua_newtable(L);
  38. lua_setglobal(L, "gamedata");
  39. // Initialize our lua_api modules
  40. initializeModApi(L, top);
  41. lua_pop(L, 1);
  42. // Push builtin initialization type
  43. lua_pushstring(L, "mainmenu");
  44. lua_setglobal(L, "INIT");
  45. infostream << "SCRIPTAPI: Initialized main menu modules" << std::endl;
  46. }
  47. /******************************************************************************/
  48. void MainMenuScripting::initializeModApi(lua_State *L, int top)
  49. {
  50. registerLuaClasses(L, top);
  51. // Initialize mod API modules
  52. ModApiMainMenu::Initialize(L, top);
  53. ModApiUtil::Initialize(L, top);
  54. ModApiSound::Initialize(L, top);
  55. ModApiHttp::Initialize(L, top);
  56. asyncEngine.registerStateInitializer(registerLuaClasses);
  57. asyncEngine.registerStateInitializer(ModApiMainMenu::InitializeAsync);
  58. asyncEngine.registerStateInitializer(ModApiUtil::InitializeAsync);
  59. asyncEngine.registerStateInitializer(ModApiHttp::InitializeAsync);
  60. // Initialize async environment
  61. //TODO possibly make number of async threads configurable
  62. asyncEngine.initialize(MAINMENU_NUM_ASYNC_THREADS);
  63. }
  64. /******************************************************************************/
  65. void MainMenuScripting::registerLuaClasses(lua_State *L, int top)
  66. {
  67. LuaSettings::Register(L);
  68. }
  69. /******************************************************************************/
  70. void MainMenuScripting::step()
  71. {
  72. asyncEngine.step(getStack());
  73. }
  74. /******************************************************************************/
  75. u32 MainMenuScripting::queueAsync(std::string &&serialized_func,
  76. std::string &&serialized_param)
  77. {
  78. return asyncEngine.queueAsyncJob(std::move(serialized_func), std::move(serialized_param));
  79. }