From fcd9eff000a967e16a0a987609210fba4a92dd0c Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Tue, 23 Sep 2025 23:12:26 +0800 Subject: Window can show but app crash. --- src/base/io/Resource.cpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'src/base/io/Resource.cpp') diff --git a/src/base/io/Resource.cpp b/src/base/io/Resource.cpp index d369b5f5..e599f8a9 100644 --- a/src/base/io/Resource.cpp +++ b/src/base/io/Resource.cpp @@ -1,19 +1,20 @@ #include "cru/base/io/Resource.h" +#include "cru/base/Base.h" #include "cru/base/Exception.h" -#include "cru/base/log/Logger.h" #if defined(CRU_PLATFORM_OSX) #include #elif defined(CRU_PLATFORM_WINDOWS) #include +#elif defined(__linux) +#include +#include #endif #include namespace cru::io { std::filesystem::path GetResourceDir() { - constexpr auto kLogTag = u"GetResourceDir"; - #if defined(CRU_PLATFORM_OSX) CFBundleRef main_bundle = CFBundleGetMainBundle(); CFURLRef bundle_url = CFBundleCopyBundleURL(main_bundle); @@ -26,10 +27,18 @@ std::filesystem::path GetResourceDir() { CFRelease(cf_string_ref); return bundle_path / "Contents/Resources"; -#elif defined(CRU_PLATFORM_WINDOWS) +#elif defined(_WIN32) || defined(__linux) +#if defined(_WIN32) wchar_t buffer[MAX_PATH]; DWORD size = ::GetModuleFileNameW(nullptr, buffer, MAX_PATH); std::filesystem::path module_path(buffer, buffer + size); +#else // linux + std::ifstream file("/proc/self/cmdline"); + std::stringstream buffer; + buffer << file.rdbuf(); + auto str = buffer.str(); + std::filesystem::path module_path(str.substr(0, str.find('\0'))); +#endif auto p = module_path; while (p.has_parent_path()) { p = p.parent_path(); @@ -41,8 +50,9 @@ std::filesystem::path GetResourceDir() { } throw Exception(u"Failed to find resource directory."); -#else - throw Exception(u"Not implemented."); + #endif + + NotImplemented(); } } // namespace cru::io -- cgit v1.2.3