Skip to content

Commit

Permalink
fix: follow rime/librime#806, migrate to path
Browse files Browse the repository at this point in the history
  • Loading branch information
lotem committed Feb 7, 2024
1 parent 2c69049 commit cc3ea78
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 77 deletions.
57 changes: 33 additions & 24 deletions RimeWithWeasel/RimeWithWeasel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
#include <StringAlgorithm.hpp>
#include <WeaselUtility.h>
#include <WeaselVersion.h>

#include <boost/filesystem/filesystem.hpp>
#include <map>
#include <regex>
#include <rime_api.h>
#include <map>

#define TRANSPARENT_COLOR 0x00000000
#define ARGB2ABGR(value) ((value & 0xff000000) | ((value & 0x000000ff) << 16) | (value & 0x0000ff00) | ((value & 0x00ff0000) >> 16))
Expand Down Expand Up @@ -70,10 +72,12 @@ void _RefreshTrayIcon(const UINT session_id, const std::function<void()> _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;
Expand Down Expand Up @@ -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)
void _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)
Expand Down Expand Up @@ -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 = WeaselSharedDataDir();
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);
Expand Down
38 changes: 16 additions & 22 deletions RimeWithWeasel/WeaselUtility.cpp
Original file line number Diff line number Diff line change
@@ -1,42 +1,36 @@
#include "stdafx.h"
#include <boost/filesystem/filesystem.hpp>
#include <string>
#include <WeaselUtility.h>

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)
Expand Down
21 changes: 10 additions & 11 deletions WeaselDeployer/Configurator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
#include <rime_api.h>
#include <rime_levers_api.h>
#pragma warning(default: 4005)
#include <boost/filesystem/filesystem.hpp>
#include <fstream>
#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();
}
}
Expand All @@ -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";
Expand Down
11 changes: 6 additions & 5 deletions WeaselServer/WeaselServerApp.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "stdafx.h"
#include "WeaselServerApp.h"
#include <boost/filesystem/filesystem.hpp>

WeaselServerApp::WeaselServerApp()
: m_handler(std::make_unique<RimeWithWeaselHandler>(&m_ui))
Expand Down Expand Up @@ -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/"));
Expand Down
21 changes: 11 additions & 10 deletions WeaselServer/WeaselServerApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,29 @@
#include <WeaselUI.h>
#include <RimeWithWeasel.h>
#include <WeaselUtility.h>
#include <winsparkle.h>
#include <boost/filesystem/filesystem.hpp>
#include <functional>
#include <memory>
#include <winsparkle.h>

#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;
}
Expand All @@ -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:
Expand Down
7 changes: 3 additions & 4 deletions include/WeaselUtility.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include <boost/filesystem/filesystem.hpp>
#include <string>

inline int utf8towcslen(const char* utf8_str, int utf8_len)
Expand Down Expand Up @@ -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()
{
Expand Down
2 changes: 1 addition & 1 deletion librime
Submodule librime updated 136 files

0 comments on commit cc3ea78

Please sign in to comment.