From 6964a186050409a6c8d9cb0220a915eb3664ac3a Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 25 Oct 2024 13:58:17 +0300 Subject: [PATCH 1/5] add platform::open_folder --- src/util/platform.cpp | 20 ++++++++++++++++---- src/util/platform.hpp | 6 +++++- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/util/platform.cpp b/src/util/platform.cpp index b749718d..b1c5362c 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,19 @@ 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 "+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..973c0ebd 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 application + /// @param folder target folder + void open_folder(const std::filesystem::path& folder); } From 751cde750345d9505283535e3bf506f7abcd19d7 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 25 Oct 2024 14:07:32 +0300 Subject: [PATCH 2/5] add core.open_folder --- src/logic/scripting/lua/libs/libcore.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) 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}}; From 74b9dd6d2f7fb7e5ad6b47bf4f36382648544389 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 25 Oct 2024 14:26:59 +0300 Subject: [PATCH 3/5] update platform.cpp --- src/util/platform.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/platform.cpp b/src/util/platform.cpp index b1c5362c..2a19a39b 100644 --- a/src/util/platform.cpp +++ b/src/util/platform.cpp @@ -57,7 +57,7 @@ void platform::open_folder(const std::filesystem::path& folder) { #ifdef __APPLE__ auto cmd = "open "+util::quote(folder.u8string()); #elif defined(_WIN32) - auto cmd = "start "+util::quote(folder.u8string()); + auto cmd = "explorer "+util::quote(folder.u8string()); #else auto cmd = "xdg-open "+util::quote(folder.u8string()); #endif From 87e3247ecf3fe37b06ea23dc40b297a09e27a13e Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 25 Oct 2024 14:43:31 +0300 Subject: [PATCH 4/5] update platform.cpp --- src/util/platform.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/platform.cpp b/src/util/platform.cpp index 2a19a39b..8956abd7 100644 --- a/src/util/platform.cpp +++ b/src/util/platform.cpp @@ -57,7 +57,7 @@ void platform::open_folder(const std::filesystem::path& folder) { #ifdef __APPLE__ auto cmd = "open "+util::quote(folder.u8string()); #elif defined(_WIN32) - auto cmd = "explorer "+util::quote(folder.u8string()); + auto cmd = "start explorer "+util::quote(folder.u8string()); #else auto cmd = "xdg-open "+util::quote(folder.u8string()); #endif From df31b9e589c0bccebbc0d19cfab6d52dfc821bb6 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 25 Oct 2024 14:46:18 +0300 Subject: [PATCH 5/5] update platform::open_folder docs --- src/util/platform.cpp | 1 - src/util/platform.hpp | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/util/platform.cpp b/src/util/platform.cpp index 8956abd7..6bc3b343 100644 --- a/src/util/platform.cpp +++ b/src/util/platform.cpp @@ -49,7 +49,6 @@ std::string platform::detect_locale() { } #endif - void platform::open_folder(const std::filesystem::path& folder) { if (!std::filesystem::is_directory(folder)) { return; diff --git a/src/util/platform.hpp b/src/util/platform.hpp index 973c0ebd..b6e8bde9 100644 --- a/src/util/platform.hpp +++ b/src/util/platform.hpp @@ -7,7 +7,7 @@ namespace platform { void configure_encoding(); /// @return environment locale in ISO format ll_CC std::string detect_locale(); - /// @brief Open folder using system file manager application + /// @brief Open folder using system file manager asynchronously /// @param folder target folder void open_folder(const std::filesystem::path& folder); }