diff --git a/CMakeLists.txt b/CMakeLists.txt index 48396cf..162f509 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,11 @@ # SPDX-License-Identifier: GPL-3.0-only cmake_minimum_required(VERSION 3.24) project(OpenBMH VERSION 0.2.0 LANGUAGES CXX C) +if(APPLE) + enable_language(OBJCXX) + set(CMAKE_OBJCXX_STANDARD 20) + set(CMAKE_OBJCXX_STANDARD_REQUIRED ON) +endif() # the one place of the version: --version of both programs and the window title (src/version.hpp) add_compile_definitions(OPENBMH_VERSION="${PROJECT_VERSION}") diff --git a/src/cli/main.cpp b/src/cli/main.cpp index 2ba61f4..ea2b453 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -33,6 +33,7 @@ #include "launch/archive.hpp" #include "launch/game_data.hpp" #include "content/package.hpp" +#include "launch/process.hpp" #include "launch/mods.hpp" #include "version.hpp" @@ -723,7 +724,7 @@ int main(int argc, char** argv) { args.data = mount->root(); if (args.saves.empty()) { bmh::launch::ModLibrary library( - std::filesystem::absolute(bmh::launch::path_from_utf8(argv[0])).parent_path()); + bmh::launch::executable_path(bmh::launch::path_from_utf8(argv[0])).parent_path()); bmh::launch::ModEntry mod; mod.content_id = contents->content_id; args.saves = library.user_directory(mod); diff --git a/src/import/CMakeLists.txt b/src/import/CMakeLists.txt index 5b6d459..422b2e0 100644 --- a/src/import/CMakeLists.txt +++ b/src/import/CMakeLists.txt @@ -34,7 +34,6 @@ if(WIN32) target_sources(bmh_launcher PRIVATE ../launch/launcher_win32.cpp) target_link_libraries(bmh_launcher PRIVATE comctl32 ole32 shell32 uuid) elseif(APPLE) - enable_language(OBJCXX) set_property(TARGET bmh_launcher PROPERTY OBJCXX_STANDARD 20) target_sources(bmh_launcher PRIVATE ../launch/launcher_cocoa.mm) set_source_files_properties(../launch/launcher_cocoa.mm PROPERTIES COMPILE_OPTIONS "-fobjc-arc") diff --git a/src/launch/launcher_cocoa.mm b/src/launch/launcher_cocoa.mm index 00e6983..3af979b 100644 --- a/src/launch/launcher_cocoa.mm +++ b/src/launch/launcher_cocoa.mm @@ -124,9 +124,9 @@ class CocoaLauncher final : public LauncherUI { [NSApp activateIgnoringOtherApps:YES]; } ~CocoaLauncher() override { + finish_progress(); bridge_->action = {}; bridge_->selection = {}; - finish_progress(); window_.delegate = nil; [window_ orderOut:nil]; [window_ close]; diff --git a/src/launch/process.cpp b/src/launch/process.cpp index 7a05f02..ba0c15d 100644 --- a/src/launch/process.cpp +++ b/src/launch/process.cpp @@ -11,9 +11,40 @@ #include #include #include +#ifdef __APPLE__ +#include +#endif extern char **environ; #endif namespace bmh::launch { +std::filesystem::path executable_path(const std::filesystem::path &invocation) { +#ifdef _WIN32 + std::vector buffer(1024); + for (;;) { + const auto length = GetModuleFileNameW(nullptr, buffer.data(), DWORD(buffer.size())); + if (!length) + throw std::runtime_error("Cannot locate OpenBMH executable"); + if (length < buffer.size()) + return std::filesystem::path(std::wstring(buffer.data(), length)); + if (buffer.size() > 32768) + throw std::runtime_error("Executable path is too long"); + buffer.resize(buffer.size() * 2); + } +#elif defined(__APPLE__) + uint32_t size = 0; + _NSGetExecutablePath(nullptr, &size); + std::vector buffer(size); + if (_NSGetExecutablePath(buffer.data(), &size) != 0) + throw std::runtime_error("Cannot locate OpenBMH executable"); + return std::filesystem::canonical(path_from_utf8(buffer.data())); +#else + std::error_code error; + const auto path = std::filesystem::read_symlink("/proc/self/exe", error); + if (!error) + return path; + return std::filesystem::absolute(invocation).lexically_normal(); +#endif +} int run_child(const std::filesystem::path &executable, const std::vector &args, const std::function &pump) { #ifdef _WIN32 diff --git a/src/launch/process.hpp b/src/launch/process.hpp index af67c45..e5634b7 100644 --- a/src/launch/process.hpp +++ b/src/launch/process.hpp @@ -5,6 +5,7 @@ #include #include namespace bmh::launch { +std::filesystem::path executable_path(const std::filesystem::path &invocation); int run_child(const std::filesystem::path &, const std::vector &, const std::function &pump); } // namespace bmh::launch diff --git a/tests/native_content_tests.cpp b/tests/native_content_tests.cpp index 26f18a3..b95edb9 100644 --- a/tests/native_content_tests.cpp +++ b/tests/native_content_tests.cpp @@ -2,6 +2,8 @@ #include "content/package.hpp" #include "content/save.hpp" #include "launch/mods.hpp" +#include "launch/process.hpp" +#include "launch/archive.hpp" #include "formats/content.hpp" #include "formats/native_state.hpp" #include "formats/vga.hpp" @@ -22,7 +24,20 @@ template void rejects(F f, const char *message) { } check(failed, message); } -int main() try { +int main(int argc, char **argv) try { + const auto arguments = launch::utf8_arguments(argc, argv); + if (arguments.size() > 1 && arguments[1] == "--child-check") { + return arguments == std::vector{arguments[0], "--child-check", "Grüße with spaces", + "quote\" and trailing\\", ""} + ? 42 + : 43; + } + const auto executable = launch::executable_path("not-the-executable"); + check(fs::is_regular_file(executable), + "portable root uses the actual process path rather than argv or current directory"); + check(launch::run_child(executable, {"--child-check", "Grüße with spaces", "quote\" and trailing\\", ""}, + {}) == 42, + "child startup preserves Unicode, spaces, quotes, trailing slashes and empty arguments"); check(content::sha256("") == "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "SHA256 empty vector"); check(content::sha256("abc") == "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",