aboutsummaryrefslogtreecommitdiff
path: root/src/base/io/Resource.cpp
diff options
context:
space:
mode:
authorYuqian Yang <crupest@crupest.life>2025-09-23 23:12:26 +0800
committerYuqian Yang <crupest@crupest.life>2025-09-23 23:12:26 +0800
commitfcd9eff000a967e16a0a987609210fba4a92dd0c (patch)
tree5e2461ca67d94e460b7da08c9ab11af2607d1f73 /src/base/io/Resource.cpp
parentd79c8842fac8ddea402e9dd411e55ea583f74f03 (diff)
downloadcru-fcd9eff000a967e16a0a987609210fba4a92dd0c.tar.gz
cru-fcd9eff000a967e16a0a987609210fba4a92dd0c.tar.bz2
cru-fcd9eff000a967e16a0a987609210fba4a92dd0c.zip
Window can show but app crash.
Diffstat (limited to 'src/base/io/Resource.cpp')
-rw-r--r--src/base/io/Resource.cpp22
1 files changed, 16 insertions, 6 deletions
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 <CoreFoundation/CoreFoundation.h>
#elif defined(CRU_PLATFORM_WINDOWS)
#include <Windows.h>
+#elif defined(__linux)
+#include <fstream>
+#include <sstream>
#endif
#include <filesystem>
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