From 9e79ad11abbba6f46e42aa3d21593b1dd476d4a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=85=E6=88=8E=E6=B0=8F?= Date: Wed, 7 Feb 2024 22:46:24 +0800 Subject: [PATCH] fix: follow rime/librime#806, migrate to path --- RimeWithWeasel/RimeWithWeasel.cpp | 65 ++++++++++++++++++------------- RimeWithWeasel/WeaselUtility.cpp | 38 ++++++++---------- WeaselDeployer/Configurator.cpp | 21 +++++----- WeaselServer/WeaselServerApp.cpp | 11 +++--- WeaselServer/WeaselServerApp.h | 21 +++++----- include/RimeWithWeasel.h | 8 ++-- include/WeaselUtility.h | 7 ++-- librime | 2 +- 8 files changed, 88 insertions(+), 85 deletions(-) diff --git a/RimeWithWeasel/RimeWithWeasel.cpp b/RimeWithWeasel/RimeWithWeasel.cpp index 914e27878..b71ad9c66 100644 --- a/RimeWithWeasel/RimeWithWeasel.cpp +++ b/RimeWithWeasel/RimeWithWeasel.cpp @@ -4,9 +4,11 @@ #include #include #include + +#include +#include #include #include -#include #define TRANSPARENT_COLOR 0x00000000 #define ARGB2ABGR(value) ((value & 0xff000000) | ((value & 0x000000ff) << 16) | (value & 0x0000ff00) | ((value & 0x00ff0000) >> 16)) @@ -70,10 +72,12 @@ void _RefreshTrayIcon(const UINT session_id, const std::function _Update void RimeWithWeaselHandler::_Setup() { RIME_STRUCT(RimeTraits, weasel_traits); - weasel_traits.shared_data_dir = weasel_shared_data_dir(); - weasel_traits.user_data_dir = weasel_user_data_dir(); + std::string shared_dir = wstring_to_string(WeaselSharedDataPath().wstring(), CP_UTF8); + std::string user_dir = wstring_to_string(WeaselUserDataPath().wstring(), CP_UTF8); + weasel_traits.shared_data_dir = shared_dir.c_str(); + weasel_traits.user_data_dir = user_dir.c_str(); weasel_traits.prebuilt_data_dir = weasel_traits.shared_data_dir; - std::string distribution_name(wstring_to_string(WEASEL_IME_NAME, CP_UTF8)); + std::string distribution_name = wstring_to_string(WEASEL_IME_NAME, CP_UTF8); weasel_traits.distribution_name = distribution_name.c_str(); weasel_traits.distribution_code_name = WEASEL_CODE_NAME; weasel_traits.distribution_version = WEASEL_VERSION; @@ -155,7 +159,7 @@ UINT RimeWithWeaselHandler::AddSession(LPWSTR buffer, EatLine eat) DLOG(INFO) << "Add session: created session_id = " << session_id; _ReadClientInfo(session_id, buffer); - m_session_status_map[session_id] = SesstionStatus(); + m_session_status_map[session_id] = SessionStatus(); m_session_status_map[session_id].style = m_base_style; RIME_STRUCT(RimeStatus, status); @@ -512,23 +516,28 @@ void RimeWithWeaselHandler::_UpdateUI(UINT session_id) m_option_name.clear(); } -void _LoadIconSettingFromSchema(RimeConfig& config, char *buffer, const int& BUF_SIZE, - const char* key1, const char* key2, const std::wstring& user_dir, const std::wstring& shared_dir, std::wstring& value) +std::wstring _LoadIconSettingFromSchema(RimeConfig& config, + const char* key1, + const char* key2, + const boost::filesystem::path& user_dir, + const boost::filesystem::path& shared_dir) { - memset(buffer, '\0', (BUF_SIZE+1)); - if (RimeConfigGetString(&config, key1, buffer, BUF_SIZE) || (key2 != NULL && RimeConfigGetString(&config, key2, buffer, BUF_SIZE))) { - std::wstring tmp = string_to_wstring(buffer, CP_UTF8); - DWORD dwAttrib = GetFileAttributes((user_dir + L"\\" + tmp).c_str()); - if (!(INVALID_FILE_ATTRIBUTES != dwAttrib && 0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY))) { - dwAttrib = GetFileAttributes((shared_dir + L"\\" + tmp).c_str()); - if (!(INVALID_FILE_ATTRIBUTES != dwAttrib && 0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY))) - value = L""; - else - value = (shared_dir + L"\\" + tmp); + const int BUF_SIZE = 255; + char buffer[BUF_SIZE + 1]; + memset(buffer, '\0', (BUF_SIZE + 1)); + if (RimeConfigGetString(&config, key1, buffer, BUF_SIZE) || + (key2 != NULL && RimeConfigGetString(&config, key2, buffer, BUF_SIZE))) { + std::wstring resource = string_to_wstring(buffer, CP_UTF8); + DWORD dwAttrib = GetFileAttributes((user_dir / resource).c_str()); + if (INVALID_FILE_ATTRIBUTES != dwAttrib && 0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)) { + return (user_dir / resource).wstring(); + } + dwAttrib = GetFileAttributes((shared_dir / resource).c_str()); + if (INVALID_FILE_ATTRIBUTES != dwAttrib && 0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)) { + return (shared_dir / resource).wstring(); } - else value = user_dir + L"\\" + tmp; } - else value = L""; + return L""; } void RimeWithWeaselHandler::_LoadSchemaSpecificSettings(UINT session_id, const std::string& schema_id) @@ -541,7 +550,7 @@ void RimeWithWeaselHandler::_LoadSchemaSpecificSettings(UINT session_id, const s _UpdateShowNotifications(&config); m_ui->style() = m_base_style; _UpdateUIStyle(&config, m_ui, false); - SesstionStatus& session_status = m_session_status_map[session_id]; + SessionStatus& session_status = m_session_status_map[session_id]; session_status.style = m_ui->style(); // load schema color style config memset(buffer, '\0', sizeof(buffer)); @@ -583,12 +592,12 @@ void RimeWithWeaselHandler::_LoadSchemaSpecificSettings(UINT session_id, const s } // load schema icon start { - std::wstring user_dir = string_to_wstring(weasel_user_data_dir()); - std::wstring shared_dir = string_to_wstring(weasel_shared_data_dir()); - _LoadIconSettingFromSchema(config, buffer, BUF_SIZE, "schema/icon", "schema/zhung_icon", user_dir, shared_dir, session_status.style.current_zhung_icon); - _LoadIconSettingFromSchema(config, buffer, BUF_SIZE, "schema/ascii_icon", NULL, user_dir, shared_dir, session_status.style.current_ascii_icon); - _LoadIconSettingFromSchema(config, buffer, BUF_SIZE, "schema/full_icon", NULL, user_dir, shared_dir, session_status.style.current_full_icon); - _LoadIconSettingFromSchema(config, buffer, BUF_SIZE, "schema/half_icon", NULL, user_dir, shared_dir, session_status.style.current_half_icon); + auto user_dir = WeaselUserDataPath(); + auto shared_dir = WeaselSharedDataPath(); + session_status.style.current_zhung_icon = _LoadIconSettingFromSchema(config, "schema/icon", "schema/zhung_icon", user_dir, shared_dir); + session_status.style.current_ascii_icon = _LoadIconSettingFromSchema(config, "schema/ascii_icon", NULL, user_dir, shared_dir); + session_status.style.current_full_icon = _LoadIconSettingFromSchema(config, "schema/full_icon", NULL, user_dir, shared_dir); + session_status.style.current_half_icon = _LoadIconSettingFromSchema(config, "schema/half_icon", NULL, user_dir, shared_dir); } // load schema icon end RimeConfigClose(&config); @@ -602,7 +611,7 @@ void RimeWithWeaselHandler::_LoadAppInlinePreeditSet(UINT session_id, bool ignor if(!ignore_app_name && m_last_app_name == app_name) return; m_last_app_name = app_name; - SesstionStatus& session_status = m_session_status_map[session_id]; + SessionStatus& session_status = m_session_status_map[session_id]; bool inline_preedit = session_status.style.inline_preedit; if (!app_name.empty()) { @@ -712,7 +721,7 @@ bool RimeWithWeaselHandler::_Respond(UINT session_id, EatLine eat) std::set actions; std::list messages; - SesstionStatus& session_status = m_session_status_map[session_id]; + SessionStatus& session_status = m_session_status_map[session_id]; RIME_STRUCT(RimeCommit, commit); if (RimeGetCommit(session_id, &commit)) { diff --git a/RimeWithWeasel/WeaselUtility.cpp b/RimeWithWeasel/WeaselUtility.cpp index b0887373d..fc60f8a0e 100644 --- a/RimeWithWeasel/WeaselUtility.cpp +++ b/RimeWithWeasel/WeaselUtility.cpp @@ -1,42 +1,36 @@ #include "stdafx.h" +#include #include +#include -std::wstring WeaselUserDataPath() { - WCHAR path[MAX_PATH] = {0}; +namespace fs = boost::filesystem; + +fs::path WeaselUserDataPath() { + WCHAR _path[MAX_PATH] = {0}; const WCHAR KEY[] = L"Software\\Rime\\Weasel"; HKEY hKey; LSTATUS ret = RegOpenKey(HKEY_CURRENT_USER, KEY, &hKey); if (ret == ERROR_SUCCESS) { - DWORD len = sizeof(path); + DWORD len = sizeof(_path); DWORD type = 0; DWORD data = 0; - ret = RegQueryValueEx(hKey, L"RimeUserDir", NULL, &type, (LPBYTE)path, &len); + ret = RegQueryValueEx(hKey, L"RimeUserDir", NULL, &type, (LPBYTE)_path, &len); RegCloseKey(hKey); - if (ret == ERROR_SUCCESS && type == REG_SZ && path[0]) + if (ret == ERROR_SUCCESS && type == REG_SZ && _path[0]) { - return path; + return fs::path(_path); } } // default location - ExpandEnvironmentStringsW(L"%AppData%\\Rime", path, _countof(path)); - return path; -} - -const char* weasel_shared_data_dir() { - static char path[MAX_PATH] = {0}; - GetModuleFileNameA(NULL, path, _countof(path)); - std::string str_path(path); - size_t k = str_path.find_last_of("/\\"); - strcpy_s(path + k + 1, _countof(path) - (k + 1), "data"); - return path; + ExpandEnvironmentStringsW(L"%AppData%\\Rime", _path, _countof(_path)); + return fs::path(_path); } -const char* weasel_user_data_dir() { - static char path[MAX_PATH] = {0}; - // Windows wants multi-byte file paths in native encoding - WideCharToMultiByte(CP_ACP, 0, WeaselUserDataPath().c_str(), -1, path, _countof(path) - 1, NULL, NULL); - return path; +fs::path WeaselSharedDataPath() { + wchar_t _path[MAX_PATH] = {0}; + GetModuleFileNameW(NULL, _path, _countof(_path)); + return fs::path(_path).remove_filename().append("data"); } std::string GetCustomResource(const char *name, const char *type) diff --git a/WeaselDeployer/Configurator.cpp b/WeaselDeployer/Configurator.cpp index 1ede65533..ec8ef55f0 100644 --- a/WeaselDeployer/Configurator.cpp +++ b/WeaselDeployer/Configurator.cpp @@ -13,17 +13,17 @@ #include #include #pragma warning(default: 4005) +#include #include #include "WeaselDeployer.h" static void CreateFileIfNotExist(std::string filename) { - std::string user_data_dir = weasel_user_data_dir(); - std::wstring filepathw = string_to_wstring(user_data_dir) + L"\\" + string_to_wstring(filename); - DWORD dwAttrib = GetFileAttributes(filepathw.c_str()); + boost::filesystem::path file_path = WeaselUserDataPath() / string_to_wstring(filename, CP_UTF8); + DWORD dwAttrib = GetFileAttributes(file_path.c_str()); if (!(INVALID_FILE_ATTRIBUTES != dwAttrib && 0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY))) { - std::wofstream o(filepathw, std::ios::app); + std::wofstream o(file_path.c_str(), std::ios::app); o.close(); } } @@ -36,14 +36,13 @@ Configurator::Configurator() void Configurator::Initialize() { RIME_STRUCT(RimeTraits, weasel_traits); - weasel_traits.shared_data_dir = weasel_shared_data_dir(); - weasel_traits.user_data_dir = weasel_user_data_dir(); + std::string shared_dir = wstring_to_string(WeaselSharedDataPath().wstring(), CP_UTF8); + std::string user_dir = wstring_to_string(WeaselUserDataPath().wstring(), CP_UTF8); + weasel_traits.shared_data_dir = shared_dir.c_str(); + weasel_traits.user_data_dir = user_dir.c_str(); weasel_traits.prebuilt_data_dir = weasel_traits.shared_data_dir; - const int len = 20; - char utf8_str[len]; - memset(utf8_str, 0, sizeof(utf8_str)); - WideCharToMultiByte(CP_UTF8, 0, WEASEL_IME_NAME, -1, utf8_str, len - 1, NULL, NULL); - weasel_traits.distribution_name = utf8_str; + std::string distribution_name = wstring_to_string(WEASEL_IME_NAME, CP_UTF8); + weasel_traits.distribution_name = distribution_name.c_str(); weasel_traits.distribution_code_name = WEASEL_CODE_NAME; weasel_traits.distribution_version = WEASEL_VERSION; weasel_traits.app_name = "rime.weasel"; diff --git a/WeaselServer/WeaselServerApp.cpp b/WeaselServer/WeaselServerApp.cpp index 851309885..bd30f72ba 100644 --- a/WeaselServer/WeaselServerApp.cpp +++ b/WeaselServer/WeaselServerApp.cpp @@ -1,5 +1,6 @@ #include "stdafx.h" #include "WeaselServerApp.h" +#include WeaselServerApp::WeaselServerApp() : m_handler(std::make_unique(&m_ui)) @@ -44,12 +45,12 @@ int WeaselServerApp::Run() void WeaselServerApp::SetupMenuHandlers() { - std::wstring dir(install_dir()); + boost::filesystem::path dir = install_dir(); m_server.AddMenuHandler(ID_WEASELTRAY_QUIT, [this] { return m_server.Stop() == 0; }); - m_server.AddMenuHandler(ID_WEASELTRAY_DEPLOY, std::bind(execute, dir + L"\\WeaselDeployer.exe", std::wstring(L"/deploy"))); - m_server.AddMenuHandler(ID_WEASELTRAY_SETTINGS, std::bind(execute, dir + L"\\WeaselDeployer.exe", std::wstring())); - m_server.AddMenuHandler(ID_WEASELTRAY_DICT_MANAGEMENT, std::bind(execute, dir + L"\\WeaselDeployer.exe", std::wstring(L"/dict"))); - m_server.AddMenuHandler(ID_WEASELTRAY_SYNC, std::bind(execute, dir + L"\\WeaselDeployer.exe", std::wstring(L"/sync"))); + m_server.AddMenuHandler(ID_WEASELTRAY_DEPLOY, std::bind(execute, dir / L"WeaselDeployer.exe", std::wstring(L"/deploy"))); + m_server.AddMenuHandler(ID_WEASELTRAY_SETTINGS, std::bind(execute, dir / L"WeaselDeployer.exe", std::wstring())); + m_server.AddMenuHandler(ID_WEASELTRAY_DICT_MANAGEMENT, std::bind(execute, dir / L"WeaselDeployer.exe", std::wstring(L"/dict"))); + m_server.AddMenuHandler(ID_WEASELTRAY_SYNC, std::bind(execute, dir / L"WeaselDeployer.exe", std::wstring(L"/sync"))); m_server.AddMenuHandler(ID_WEASELTRAY_WIKI, std::bind(open, L"https://rime.im/docs/")); m_server.AddMenuHandler(ID_WEASELTRAY_HOMEPAGE, std::bind(open, L"https://rime.im/")); m_server.AddMenuHandler(ID_WEASELTRAY_FORUM, std::bind(open, L"https://rime.im/discuss/")); diff --git a/WeaselServer/WeaselServerApp.h b/WeaselServer/WeaselServerApp.h index 6820ea05d..2f030010d 100644 --- a/WeaselServer/WeaselServerApp.h +++ b/WeaselServer/WeaselServerApp.h @@ -5,25 +5,29 @@ #include #include #include -#include +#include #include #include +#include #include "WeaselTrayIcon.h" +namespace fs = boost::filesystem; + class WeaselServerApp { public: - static bool execute(const std::wstring &cmd, const std::wstring &args) + static bool execute(const fs::path &cmd, const std::wstring &args) { return (int)ShellExecuteW(NULL, NULL, cmd.c_str(), args.c_str(), NULL, SW_SHOWNORMAL) > 32; } - static bool explore(const std::wstring &path) + static bool explore(const fs::path &path) { - return (int)ShellExecuteW(NULL, L"open", L"explorer", (L"\"" + path + L"\"").c_str(), NULL, SW_SHOWNORMAL) > 32; + std::wstring quoted_path(L"\"" + path.wstring() + L"\""); + return (int)ShellExecuteW(NULL, L"open", L"explorer", quoted_string.c_str(), NULL, SW_SHOWNORMAL) > 32; } - static bool open(const std::wstring &path) + static bool open(const fs::path &path) { return (int)ShellExecuteW(NULL, L"open", path.c_str(), NULL, NULL, SW_SHOWNORMAL) > 32; } @@ -40,14 +44,11 @@ class WeaselServerApp { return true; } - static std::wstring install_dir() + static fs::path install_dir() { WCHAR exe_path[MAX_PATH] = { 0 }; GetModuleFileNameW(GetModuleHandle(NULL), exe_path, _countof(exe_path)); - std::wstring dir(exe_path); - size_t pos = dir.find_last_of(L"\\"); - dir.resize(pos); - return dir; + return fs::path(exe_path).remove_filename(); } public: diff --git a/include/RimeWithWeasel.h b/include/RimeWithWeasel.h index f816034b8..5f1d543f9 100644 --- a/include/RimeWithWeasel.h +++ b/include/RimeWithWeasel.h @@ -19,14 +19,14 @@ struct CaseInsensitiveCompare { typedef std::map AppOptions; typedef std::map AppOptionsByAppName; -struct SesstionStatus +struct SessionStatus { - SesstionStatus() : style(weasel::UIStyle()), __synced(false) { RIME_STRUCT(RimeStatus, status); } + SessionStatus() : style(weasel::UIStyle()), __synced(false) { RIME_STRUCT(RimeStatus, status); } weasel::UIStyle style; RimeStatus status; bool __synced; }; -typedef std::map SesstionStatusMap; +typedef std::map SessionStatusMap; class RimeWithWeaselHandler : public weasel::RequestHandler { @@ -88,7 +88,7 @@ class RimeWithWeaselHandler : static std::string m_message_value; static std::string m_message_label; static std::string m_option_name; - SesstionStatusMap m_session_status_map; + SessionStatusMap m_session_status_map; bool m_current_dark_mode; bool m_global_ascii_mode; int m_show_notifications_time; diff --git a/include/WeaselUtility.h b/include/WeaselUtility.h index e2fb5b831..eb4f95d74 100644 --- a/include/WeaselUtility.h +++ b/include/WeaselUtility.h @@ -1,4 +1,5 @@ #pragma once +#include #include inline int utf8towcslen(const char* utf8_str, int utf8_len) @@ -27,10 +28,8 @@ inline std::wstring getUsername() { } // data directories -std::wstring WeaselUserDataPath(); - -const char* weasel_shared_data_dir(); -const char* weasel_user_data_dir(); +boost::filesystem::path WeaselUserDataPath(); +boost::filesystem::path WeaselUserDataPath(); inline BOOL IsUserDarkMode() { diff --git a/librime b/librime index a60876745..6546689a2 160000 --- a/librime +++ b/librime @@ -1 +1 @@ -Subproject commit a60876745af20ecc8489ec6997c6c195949b99ac +Subproject commit 6546689a2c9061860a0af5fa1209f9e8ea7d2132