From 40f566974ce1fb20653dc9eaebf65dad12d85cf1 Mon Sep 17 00:00:00 2001 From: alves Date: Thu, 8 Jan 2026 14:53:53 +0800 Subject: [PATCH] fix code format for read config file data. --- src/common_func/common_func.cpp | 50 +++++++++++++++++++--------- src/sentry_wrapper/SentryWrapper.cpp | 36 +++++++++++++++----- 2 files changed, 63 insertions(+), 23 deletions(-) diff --git a/src/common_func/common_func.cpp b/src/common_func/common_func.cpp index 4b31cfd860..120985e138 100644 --- a/src/common_func/common_func.cpp +++ b/src/common_func/common_func.cpp @@ -78,13 +78,19 @@ namespace common std::string versionFilePath = ""; #ifdef _WIN32 - wchar_t appDataPath[MAX_PATH] = {0}; - auto hr = SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, appDataPath); - char* path = new char[MAX_PATH]; - size_t pathLength; - wcstombs_s(&pathLength, path, MAX_PATH, appDataPath, MAX_PATH); + PWSTR pszPath = nullptr; + char* path = new char[MAX_PATH](); + size_t pathLength = 0; + HRESULT hr = SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, nullptr, &pszPath); + if (SUCCEEDED(hr)) { + wcstombs_s(&pathLength, path, MAX_PATH, pszPath, MAX_PATH); + CoTaskMemFree(pszPath); + } + std::string filePath = path; versionFilePath = filePath + "\\" + std::string("Snapmaker_Orca\\web\\flutter_web\\version.json"); + + delete[] path; #elif __APPLE__ const char* home_env = getenv("HOME"); versionFilePath = home_env; @@ -115,13 +121,20 @@ namespace common std::string localArea = ""; std::string cfgfile = ""; #ifdef _WIN32 - wchar_t appDataPath[MAX_PATH] = {0}; - auto hr = SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, appDataPath); - char* path = new char[MAX_PATH]; - size_t pathLength; - wcstombs_s(&pathLength, path, MAX_PATH, appDataPath, MAX_PATH); + + PWSTR pszPath = nullptr; + char* path = new char[MAX_PATH](); + size_t pathLength = 0; + HRESULT hr = SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, nullptr, &pszPath); + if (SUCCEEDED(hr)) { + wcstombs_s(&pathLength, path, MAX_PATH, pszPath, MAX_PATH); + CoTaskMemFree(pszPath); + } + std::string filePath = path; cfgfile = filePath + "\\" + std::string("Snapmaker_Orca\\Snapmaker_Orca.conf"); + delete[] path; + #elif __APPLE__ const char* home_env = getenv("HOME"); cfgfile = home_env; @@ -150,13 +163,20 @@ namespace common std::string cfgfile = ""; #ifdef _WIN32 - wchar_t appDataPath[MAX_PATH] = {0}; - auto hr = SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, appDataPath); - char* path = new char[MAX_PATH]; - size_t pathLength; - wcstombs_s(&pathLength, path, MAX_PATH, appDataPath, MAX_PATH); + + PWSTR pszPath = nullptr; + char* path = new char[MAX_PATH](); + size_t pathLength = 0; + HRESULT hr = SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, nullptr, &pszPath); + if (SUCCEEDED(hr)) { + wcstombs_s(&pathLength, path, MAX_PATH, pszPath, MAX_PATH); + CoTaskMemFree(pszPath); + } + std::string filePath = path; cfgfile = filePath + "\\" + std::string("Snapmaker_Orca\\Snapmaker_Orca.conf"); + delete[] path; + #elif __APPLE__ const char* home_env = getenv("HOME"); cfgfile = home_env; diff --git a/src/sentry_wrapper/SentryWrapper.cpp b/src/sentry_wrapper/SentryWrapper.cpp index 33d4163a50..ef996b28b6 100644 --- a/src/sentry_wrapper/SentryWrapper.cpp +++ b/src/sentry_wrapper/SentryWrapper.cpp @@ -15,6 +15,9 @@ #ifdef _WIN32 #include #include +#include +#include +#pragma comment(lib, "iphlpapi.lib") #endif #ifdef __APPLE__ @@ -174,14 +177,20 @@ void initSentryEx() handlerDir = wstringTostring(desDir); - wchar_t appDataPath[MAX_PATH] = {0}; - auto hr = SHGetFolderPathW(NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, appDataPath); - char* path = new char[MAX_PATH]; - size_t pathLength; - wcstombs_s(&pathLength, path, MAX_PATH, appDataPath, MAX_PATH); + PWSTR pszPath = nullptr; + char* path = new char[MAX_PATH](); + size_t pathLength = 0; + HRESULT hr = SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, nullptr, &pszPath); + + if (SUCCEEDED(hr)) { + wcstombs_s(&pathLength, path, MAX_PATH, pszPath, MAX_PATH); + CoTaskMemFree(pszPath); + } + std::string filePath = path; std::string appName = "\\" + std::string("Snapmaker_Orca\\"); dataBaseDir = filePath + appName; + delete[] path; #endif if (!handlerDir.empty()) @@ -276,17 +285,28 @@ void sentryReportLogEx(SENTRY_LOG_LEVEL logLevel, sentry_value_t attr = sentry_value_new_attribute(sentry_value_new_string(pcName.c_str()), NULL); sentry_value_set_by_key(tags, "pc_name", attr); } - std::string machineID = common::getMachineId(); + static std::string machineID = ""; + if (machineID.empty()) + machineID = common::getMachineId(); + if (!machineID.empty()) { sentry_value_t attr = sentry_value_new_attribute(sentry_value_new_string(machineID.c_str()), NULL); sentry_value_set_by_key(tags, "machine_id", attr); } - std::string currentLanguage = common::getLanguage(); + + static std::string currentLanguage = ""; + if (currentLanguage.empty()) + currentLanguage = common::getLanguage(); + if (!currentLanguage.empty()) { sentry_value_t attr = sentry_value_new_attribute(sentry_value_new_string(currentLanguage.c_str()), NULL); sentry_value_set_by_key(tags, "current_language", attr); } - std::string localArea = common::getLocalArea(); + + static std::string localArea = ""; + if (localArea.empty()) + localArea = common::getLocalArea(); + if (!localArea.empty()) { sentry_value_t attr = sentry_value_new_attribute(sentry_value_new_string(localArea.c_str()), NULL);