diff --git a/src/logic/scripting/lua/libs/libapp.cpp b/src/logic/scripting/lua/libs/libapp.cpp index 736fc8624..09ea33ce3 100644 --- a/src/logic/scripting/lua/libs/libapp.cpp +++ b/src/logic/scripting/lua/libs/libapp.cpp @@ -22,21 +22,18 @@ using namespace scripting; -static int l_get_version(lua::State* L) { - return lua::pushvec_stack( - L, glm::vec2(ENGINE_VERSION_MAJOR, ENGINE_VERSION_MINOR) - ); -} - +/// @brief Check if content is loaded static int l_is_content_loaded(lua::State* L) { return lua::pushboolean(L, content != nullptr); } +/// @brief Load content static int l_load_content(lua::State* L) { content_control->loadContent(); return 0; } +/// @brief Reset content, excluding specified packs modules static int l_reset_content(lua::State* L) { if (level != nullptr) { throw std::runtime_error("world must be closed before"); @@ -89,59 +86,7 @@ static int l_reconfig_packs(lua::State* L) { return 0; } -static int l_start_debug_instance(lua::State* L) { - if (!engine->getProject().permissions.has(Permissions::DEBUGGING)) { - throw std::runtime_error("project has no debugging permission"); - } - - int port = lua::tointeger(L, 1); - if (port == 0) { - auto network = engine->getNetwork(); - if (network == nullptr) { - throw std::runtime_error("project has no network permission"); - } - port = network->findFreePort(); - if (port == -1) { - throw std::runtime_error("could not find free port"); - } - } - auto projectPath = lua::isstring(L, 2) ? lua::require_lstring(L, 2) : ""; - const auto& paths = engine->getPaths(); - - std::vector args { - "--res", paths.getResourcesFolder().u8string(), - "--dir", paths.getUserFilesFolder().u8string(), - "--dbg-server", "tcp:" + std::to_string(port), - }; - if (!projectPath.empty()) { - args.emplace_back("--project"); - args.emplace_back(io::resolve(std::string(projectPath)).string()); - } - - platform::new_engine_instance(std::move(args)); - return lua::pushinteger(L, port); -} - -static int l_focus(lua::State* L) { - engine->getWindow().focus(); - return 0; -} - -static int l_create_memory_device(lua::State* L) { - std::string name = lua::require_string(L, 1); - if (io::get_device(name)) { - throw std::runtime_error( - "entry-point '" + name + "' is already used" - ); - } - if (name.find(':') != std::string::npos) { - throw std::runtime_error("invalid entry point name"); - } - - io::set_device(name, std::make_unique()); - return 0; -} - +/// @brief Get content sources list static int l_get_content_sources(lua::State* L) { const auto& sources = engine->getContentControl().getContentSources(); lua::createtable(L, static_cast(sources.size()), 0); @@ -152,6 +97,7 @@ static int l_get_content_sources(lua::State* L) { return 1; } +/// @brief Set content sources list static int l_set_content_sources(lua::State* L) { if (!lua::istable(L, 1)) { throw std::runtime_error("table expected as argument 1"); @@ -167,17 +113,12 @@ static int l_set_content_sources(lua::State* L) { return 0; } +/// @brief Reset content sources to defaults static int l_reset_content_sources(lua::State* L) { engine->getContentControl().resetContentSources(); return 0; } -static int l_set_title(lua::State* L) { - auto title = lua::require_string(L, 1); - engine->getWindow().setTitle(title); - return 0; -} - /// @brief Get a setting value /// @param name The name of the setting /// @return The value of the setting @@ -245,11 +186,13 @@ static int l_get_setting_info(lua::State* L) { throw std::runtime_error("unsupported setting type"); } +/// @brief Open folder in file explorer static int l_open_folder(lua::State* L) { platform::open_folder(io::resolve(lua::require_string(L, 1))); return 0; } +/// @brief Open URL in default browser with confirmation dialog static int l_open_url(lua::State* L) { auto url = lua::require_string(L, 1); @@ -268,6 +211,26 @@ static int l_open_url(lua::State* L) { return 0; } + +/// @brief Bring application window to focus +static int l_focus(lua::State* L) { + engine->getWindow().focus(); + return 0; +} + +/// @brief Set window title +static int l_set_title(lua::State* L) { + auto title = lua::require_string(L, 1); + engine->getWindow().setTitle(title); + return 0; +} + +/// @brief Quit the game +static int l_quit(lua::State*) { + engine->quit(); + return 0; +} + /// @brief Creating new world /// @param name Name world /// @param seed Seed world @@ -345,39 +308,95 @@ static int l_delete_world(lua::State* L) { return 0; } +/// @brief Get engine version +/// @return major, minor +static int l_get_version(lua::State* L) { + return lua::pushvec_stack( + L, glm::vec2(ENGINE_VERSION_MAJOR, ENGINE_VERSION_MINOR) + ); +} -/// @brief Quit the game -static int l_quit(lua::State*) { - engine->quit(); +/// @brief Create in-memory named IO device +static int l_create_memory_device(lua::State* L) { + std::string name = lua::require_string(L, 1); + if (io::get_device(name)) { + throw std::runtime_error( + "entry-point '" + name + "' is already used" + ); + } + if (name.find(':') != std::string::npos) { + throw std::runtime_error("invalid entry point name"); + } + + io::set_device(name, std::make_unique()); return 0; } +/// @brief Start a new engine instance with debugging server +static int l_start_debug_instance(lua::State* L) { + if (!engine->getProject().permissions.has(Permissions::DEBUGGING)) { + throw std::runtime_error("project has no debugging permission"); + } + + int port = lua::tointeger(L, 1); + if (port == 0) { + auto network = engine->getNetwork(); + if (network == nullptr) { + throw std::runtime_error("project has no network permission"); + } + port = network->findFreePort(); + if (port == -1) { + throw std::runtime_error("could not find free port"); + } + } + auto projectPath = lua::isstring(L, 2) ? lua::require_lstring(L, 2) : ""; + const auto& paths = engine->getPaths(); + + std::vector args { + "--res", paths.getResourcesFolder().u8string(), + "--dir", paths.getUserFilesFolder().u8string(), + "--dbg-server", "tcp:" + std::to_string(port), + }; + if (!projectPath.empty()) { + args.emplace_back("--project"); + args.emplace_back(io::resolve(std::string(projectPath)).string()); + } + + platform::new_engine_instance(std::move(args)); + return lua::pushinteger(L, port); +} + const luaL_Reg applib[] = { - {"get_version", lua::wrap}, + /// content {"is_content_loaded", lua::wrap}, {"load_content", lua::wrap}, {"reset_content", lua::wrap}, {"reconfig_packs", lua::wrap}, - {"start_debug_instance", lua::wrap}, - {"focus", lua::wrap}, - {"create_memory_device", lua::wrap}, {"get_content_sources", lua::wrap}, {"set_content_sources", lua::wrap}, {"reset_content_sources", lua::wrap}, - {"set_title", lua::wrap}, - {"open_folder", lua::wrap}, - {"open_url", lua::wrap}, + /// settings {"get_setting", lua::wrap}, {"set_setting", lua::wrap}, {"str_setting", lua::wrap}, {"get_setting_info", lua::wrap}, + /// system applications + {"open_folder", lua::wrap}, + {"open_url", lua::wrap}, + /// window + {"focus", lua::wrap}, + {"set_title", lua::wrap}, + {"quit", lua::wrap}, + /// world {"new_world", lua::wrap}, {"open_world", lua::wrap}, {"reopen_world", lua::wrap}, {"save_world", lua::wrap}, {"close_world", lua::wrap}, {"delete_world", lua::wrap}, - {"quit", lua::wrap}, - // for other functions see libcore.cpp and stdlib.lua + /// other + {"get_version", lua::wrap}, + {"create_memory_device", lua::wrap}, + {"start_debug_instance", lua::wrap}, {nullptr, nullptr} };