From 19e8a2da6a21ad75bf466ecdc0c711abccd364c6 Mon Sep 17 00:00:00 2001 From: taynpg Date: Tue, 28 May 2024 16:05:05 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E4=BF=AE=E6=AD=A3=E5=BD=93?= =?UTF-8?q?=E5=89=8D=E7=9B=AE=E5=BD=95=E4=B8=8D=E6=AD=A3=E7=A1=AE=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++- main.cpp | 8 ++++---- src/pub.cpp | 40 +++++++++++++++++++++++++++++++++++++++- src/pub.h | 1 + 4 files changed, 46 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 1f7f3cb..9948f46 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ build .cache .vs -vs* \ No newline at end of file +vs* +*.exe \ No newline at end of file diff --git a/main.cpp b/main.cpp index 3608195..e1af336 100644 --- a/main.cpp +++ b/main.cpp @@ -381,13 +381,13 @@ int main(int argc, char** argv) #if _DEBUG std::cout << "Debug Mode." << std::endl; - fs::path exe_path(argv[0]); - gParam.exe_dir = fs::current_path().parent_path().string(); + fs::path exe_path(CUtil::get_exe_path()); + gParam.exe_dir = exe_path.parent_path().parent_path().string(); std::cout << std::format("current_path is: {}", gParam.exe_dir) << "\n"; #else std::cout << "Release Mode." << std::endl; - fs::path exe_path(argv[0]); - gParam.exe_dir = fs::current_path().string(); + fs::path exe_path(CUtil::get_exe_path()); + gParam.exe_dir = exe_path.parent_path().string(); std::cout << std::format("current_path is: {}", gParam.exe_dir) << "\n"; #endif diff --git a/src/pub.cpp b/src/pub.cpp index 89faf1f..16994d5 100644 --- a/src/pub.cpp +++ b/src/pub.cpp @@ -1,6 +1,15 @@ #include "pub.h" #include +#ifdef _WIN32 +#include +#elif defined(__clang__) && defined(__APPLE__) +#include +#include +#else +#include +#endif + namespace fs = std::filesystem; CUtil::CUtil() = default; @@ -81,4 +90,33 @@ std::string CUtil::get_keybindings_file() #endif path = ret.append("keybindings.json").string(); return path; -} \ No newline at end of file +} + +std::string CUtil::get_exe_path() +{ + std::string path; +#ifdef _WIN32 + char buffer[MAX_PATH]; + DWORD length = GetModuleFileName(NULL, buffer, MAX_PATH); + if (length == 0) { + return ""; + } + return std::string(buffer, length); +#elif defined(__clang__) && defined(__APPLE__) + uint32_t size = 0; + _NSGetExecutablePath(NULL, &size); // 获取路径缓冲区的大小 + std::vector buffer(size); // 创建缓冲区 + if (_NSGetExecutablePath(buffer.data(), &size) != 0) { + return ""; + } + return std::string(buffer.data()); +#else + char buffer[PATH_MAX]; + ssize_t len = readlink("/proc/self/exe", buffer, sizeof(buffer) - 1); + if (len == -1) { + return ""; + } + buffer[len] = '\0'; // 确保字符串以null终止 + return std::string(buffer); +#endif +} diff --git a/src/pub.h b/src/pub.h index 03b77eb..b6c131e 100644 --- a/src/pub.h +++ b/src/pub.h @@ -16,6 +16,7 @@ public: static std::string replace(const std::string& content, const std::string& from, const std::string& to); static std::string get_home(); static std::string get_keybindings_file(); + static std::string get_exe_path(); };