diff --git a/src/logic/scripting/lua/libs/libcore.cpp b/src/logic/scripting/lua/libs/libcore.cpp index 6f0dd8b8..b0a960b2 100644 --- a/src/logic/scripting/lua/libs/libcore.cpp +++ b/src/logic/scripting/lua/libs/libcore.cpp @@ -167,6 +167,14 @@ static int l_get_setting_info(lua::State* L) { throw std::runtime_error("unsupported setting type"); } +#include "util/platform.hpp" + +static int l_open_folder(lua::State* L) { + auto path = engine->getPaths()->resolve(lua::require_string(L, 1)); + platform::open_folder(path); + return 0; +} + /// @brief Quit the game static int l_quit(lua::State*) { Window::setShouldClose(true); @@ -184,5 +192,6 @@ const luaL_Reg corelib[] = { {"set_setting", lua::wrap}, {"str_setting", lua::wrap}, {"get_setting_info", lua::wrap}, + {"open_folder", lua::wrap}, {"quit", lua::wrap}, {NULL, NULL}}; diff --git a/src/util/platform.cpp b/src/util/platform.cpp index b749718d..6bc3b343 100644 --- a/src/util/platform.cpp +++ b/src/util/platform.cpp @@ -7,12 +7,11 @@ #include #include "typedefs.hpp" +#include "stringutil.hpp" #ifdef _WIN32 #include -#include "stringutil.hpp" - void platform::configure_encoding() { // set utf-8 encoding to console output SetConsoleOutputCP(CP_UTF8); @@ -35,7 +34,6 @@ std::string platform::detect_locale() { .replace(2, 1, "_") .substr(0, 5); } - #else void platform::configure_encoding() { @@ -49,5 +47,18 @@ std::string platform::detect_locale() { return preferredLocaleName.substr(0, 5); } - #endif + +void platform::open_folder(const std::filesystem::path& folder) { + if (!std::filesystem::is_directory(folder)) { + return; + } +#ifdef __APPLE__ + auto cmd = "open "+util::quote(folder.u8string()); +#elif defined(_WIN32) + auto cmd = "start explorer "+util::quote(folder.u8string()); +#else + auto cmd = "xdg-open "+util::quote(folder.u8string()); +#endif + system(cmd.c_str()); +} diff --git a/src/util/platform.hpp b/src/util/platform.hpp index a620acad..b6e8bde9 100644 --- a/src/util/platform.hpp +++ b/src/util/platform.hpp @@ -1,9 +1,13 @@ #pragma once #include +#include namespace platform { void configure_encoding(); - // @return environment locale in ISO format ll_CC + /// @return environment locale in ISO format ll_CC std::string detect_locale(); + /// @brief Open folder using system file manager asynchronously + /// @param folder target folder + void open_folder(const std::filesystem::path& folder); }