From f21369c7cbeb6605a4ef072b954498d8b8940e51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=85=E6=88=8E=E6=B0=8F?= Date: Tue, 6 Feb 2024 12:56:38 +0800 Subject: [PATCH 1/3] follow up librime#806, avoid extra conversion Avoid using `RimeGet*Dir()` from `rime_api`. They return native path on Windows and coding conversion is incurred. When passing path from lua to librime class, use either rime::path object or UTF-8 encoded string. --- src/modules.cc | 9 ++++++--- src/opencc.cc | 19 ++++++++++--------- src/types.cc | 3 ++- src/types_ext.cc | 7 ++++++- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/modules.cc b/src/modules.cc index 2e84459..d9bc3d3 100644 --- a/src/modules.cc +++ b/src/modules.cc @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include "lib/lua_templates.h" #include "lua_gears.h" @@ -17,8 +17,11 @@ static bool file_exists(const char *fname) noexcept { } static void lua_init(lua_State *L) { - const auto user_dir = std::string(RimeGetUserDataDir()); - const auto shared_dir = std::string(RimeGetSharedDataDir()); + // path::string() returns native encoding on Windows + const auto user_dir = + rime::Service::instance().deployer().user_data_dir.string(); + const auto shared_dir = + rime::Service::instance().deployer().shared_data_dir.string(); types_init(L); lua_getglobal(L, "package"); diff --git a/src/opencc.cc b/src/opencc.cc index 17c9b90..c110dd6 100644 --- a/src/opencc.cc +++ b/src/opencc.cc @@ -13,8 +13,8 @@ #include #include #include -#include #include +#include #include "lib/lua_export_type.h" #include "optional.h" @@ -28,8 +28,8 @@ namespace { class Opencc { public: - //static shared_ptr create(const string &config_path); - Opencc(const string& config_path); + //static shared_ptr create(const path &config_path); + Opencc(const path& config_path); bool ConvertWord(const string& text, vector* forms); bool RandomConvertText(const string& text, string* simplified); bool ConvertText(const string& text, string* simplified); @@ -43,9 +43,10 @@ class Opencc { opencc::DictPtr dict_; }; -Opencc::Opencc(const string& config_path) { +Opencc::Opencc(const path& config_path) { opencc::Config config; - converter_ = config.NewFromFile(config_path); + // OpenCC accepts UTF-8 encoded path. + converter_ = config.NewFromFile(config_path.u8string()); const list conversions = converter_->GetConversionChain()->GetConversions(); dict_ = conversions.front()->GetDict(); @@ -116,14 +117,14 @@ namespace OpenccReg { using T = Opencc; optional make(const string &filename) { - string user_path( RimeGetUserDataDir()); - string shared_path(RimeGetSharedDataDir()); + path user_path = Service::instance().deployer().user_data_dir; + path shared_path = Service::instance().deployer().shared_data_dir; try{ - return T(user_path + "/opencc/" + filename); + return T(user_path / "opencc" / filename); } catch(...) { try{ - return T(shared_path + "/opencc/" + filename); + return T(shared_path / "opencc" / filename); } catch(...) { LOG(ERROR) << " [" << user_path << "|" << shared_path << "]/opencc/" diff --git a/src/types.cc b/src/types.cc index 21fdd9f..e2b76d8 100644 --- a/src/types.cc +++ b/src/types.cc @@ -308,7 +308,8 @@ namespace ReverseDbReg { using T = ReverseDb; an make(const string &file) { - an db = New(string(RimeGetUserDataDir()) + "/" + file); + an db = New( + Service::instance().deployer().user_data_dir / file); db->Load(); return db; } diff --git a/src/types_ext.cc b/src/types_ext.cc index a69d033..68d58ab 100644 --- a/src/types_ext.cc +++ b/src/types_ext.cc @@ -263,6 +263,11 @@ namespace UserDbReg{ return {}; } + string file_name(const T& t) { + // returns ANSI encoded string on Windows + return t.file_path().string(); + } + bool Open(T &t) { return t.Open(); } bool Close(T &t) { return t.Close(); } bool OpenReadOnly(T &t) { return t.OpenReadOnly(); } @@ -300,7 +305,7 @@ namespace UserDbReg{ {"read_only",WRAPMEM(T, readonly)}, {"disabled",WRAPMEM(T, disabled)}, {"name", WRAPMEM(T, name)}, - {"file_name", WRAPMEM(T, file_name)}, + {"file_name", WRAP(file_name)}, { NULL, NULL }, }; From df714a59e5f1eef151b621790f185a98285c446b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=85=E6=88=8E=E6=B0=8F?= Date: Tue, 6 Feb 2024 18:04:23 +0800 Subject: [PATCH 2/3] replace RimeApi::get_*_dir with Deployer's getters --- src/types.cc | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/src/types.cc b/src/types.cc index e2b76d8..abefea2 100644 --- a/src/types.cc +++ b/src/types.cc @@ -1,3 +1,4 @@ +#include #include #include #include @@ -15,9 +16,9 @@ #include #include #include +#include #include #include "lua_gears.h" -#include #include #include "lib/lua_export_type.h" @@ -1845,43 +1846,35 @@ namespace KeySequenceReg { namespace RimeApiReg { string get_rime_version() { - RimeApi* rime = rime_get_api(); - return string(rime->get_version()); + return string(rime_get_api()->get_version()); } string get_shared_data_dir() { - RimeApi* rime = rime_get_api(); - return string(rime->get_shared_data_dir()); + return Service::instance().deployer().shared_data_dir.string(); } string get_user_data_dir() { - RimeApi* rime = rime_get_api(); - return string(rime->get_user_data_dir()); + return Service::instance().deployer().user_data_dir.string(); } string get_sync_dir() { - RimeApi* rime = rime_get_api(); - return string(rime->get_sync_dir()); + return Service::instance().deployer().sync_dir.string(); } string get_distribution_name(){ - Deployer &deployer(Service::instance().deployer()); - return deployer.distribution_name; + return Service::instance().deployer().distribution_name; } string get_distribution_code_name(){ - Deployer &deployer(Service::instance().deployer()); - return deployer.distribution_code_name; + return Service::instance().deployer().distribution_code_name; } string get_distribution_version(){ - Deployer &deployer(Service::instance().deployer()); - return deployer.distribution_version; + return Service::instance().deployer().distribution_version; } string get_user_id(){ - Deployer &deployer(Service::instance().deployer()); - return deployer.user_id; + return Service::instance().deployer().user_id; } // boost::regex api From d1e644d3deeb5cf33b7e8099b7318bce8edbe0ec Mon Sep 17 00:00:00 2001 From: Chunhui He Date: Tue, 6 Feb 2024 13:28:36 +0000 Subject: [PATCH 3/3] compat old librime --- src/modules.cc | 36 ++++++++++++++++++++++++---- src/opencc.cc | 56 +++++++++++++++++++++++++++++++------------ src/types.cc | 62 +++++++++++++++++++++++++++++++++++------------- src/types_ext.cc | 14 ++++++++++- 4 files changed, 130 insertions(+), 38 deletions(-) diff --git a/src/modules.cc b/src/modules.cc index d9bc3d3..a4ed5e0 100644 --- a/src/modules.cc +++ b/src/modules.cc @@ -1,6 +1,7 @@ #include #include #include +#include #include #include "lib/lua_templates.h" #include "lua_gears.h" @@ -16,12 +17,37 @@ static bool file_exists(const char *fname) noexcept { return false; } +namespace { +template using void_t = void; + +template +struct COMPAT { + static std::string get_shared_data_dir() { + return std::string(rime_get_api()->get_shared_data_dir()); + } + + static std::string get_user_data_dir() { + return std::string(rime_get_api()->get_user_data_dir()); + } +}; + +template +struct COMPAT().user_data_dir.string())>> { + static std::string get_shared_data_dir() { + // path::string() returns native encoding on Windows + return rime::Service::instance().deployer().shared_data_dir.string(); + } + + static std::string get_user_data_dir() { + return rime::Service::instance().deployer().user_data_dir.string(); + } +}; +} + static void lua_init(lua_State *L) { - // path::string() returns native encoding on Windows - const auto user_dir = - rime::Service::instance().deployer().user_data_dir.string(); - const auto shared_dir = - rime::Service::instance().deployer().shared_data_dir.string(); + + const auto user_dir = COMPAT::get_user_data_dir(); + const auto shared_dir = COMPAT::get_shared_data_dir(); types_init(L); lua_getglobal(L, "package"); diff --git a/src/opencc.cc b/src/opencc.cc index c110dd6..16a5647 100644 --- a/src/opencc.cc +++ b/src/opencc.cc @@ -29,7 +29,7 @@ namespace { class Opencc { public: //static shared_ptr create(const path &config_path); - Opencc(const path& config_path); + Opencc(const string& utf8_config_path); bool ConvertWord(const string& text, vector* forms); bool RandomConvertText(const string& text, string* simplified); bool ConvertText(const string& text, string* simplified); @@ -43,10 +43,10 @@ class Opencc { opencc::DictPtr dict_; }; -Opencc::Opencc(const path& config_path) { +Opencc::Opencc(const string& utf8_config_path) { opencc::Config config; // OpenCC accepts UTF-8 encoded path. - converter_ = config.NewFromFile(config_path.u8string()); + converter_ = config.NewFromFile(utf8_config_path); const list conversions = converter_->GetConversionChain()->GetConversions(); dict_ = conversions.front()->GetDict(); @@ -116,23 +116,49 @@ vector Opencc::convert_word(const string& text){ namespace OpenccReg { using T = Opencc; - optional make(const string &filename) { - path user_path = Service::instance().deployer().user_data_dir; - path shared_path = Service::instance().deployer().shared_data_dir; - try{ - return T(user_path / "opencc" / filename); + template using void_t = void; + + template + struct COMPAT { + static optional make(const string &filename) { + auto user_path = string(rime_get_api()->get_user_data_dir()); + auto shared_path = string(rime_get_api()->get_shared_data_dir()); + try{ + return T(user_path + "/opencc/" + filename); + } + catch(...) { + try{ + return T(shared_path + "/opencc/" + filename); + } + catch(...) { + LOG(ERROR) << " [" << user_path << "|" << shared_path << "]/opencc/" + << filename << ": File not found or InvalidFormat"; + return {}; + } + } } - catch(...) { + }; + + template + struct COMPAT().user_data_dir.string())>> { + static optional make(const string &filename) { + path user_path = Service::instance().deployer().user_data_dir; + path shared_path = Service::instance().deployer().shared_data_dir; try{ - return T(shared_path / "opencc" / filename); + return T((user_path / "opencc" / filename).u8string()); } catch(...) { - LOG(ERROR) << " [" << user_path << "|" << shared_path << "]/opencc/" - << filename << ": File not found or InvalidFormat"; - return {}; + try{ + return T((shared_path / "opencc" / filename).u8string()); + } + catch(...) { + LOG(ERROR) << " [" << user_path << "|" << shared_path << "]/opencc/" + << filename << ": File not found or InvalidFormat"; + return {}; + } } } - } + }; optional> convert_word(T &t,const string &s) { vector res; @@ -142,7 +168,7 @@ namespace OpenccReg { } static const luaL_Reg funcs[] = { - {"Opencc",WRAP(make)}, + {"Opencc",WRAP(COMPAT::make)}, { NULL, NULL }, }; diff --git a/src/types.cc b/src/types.cc index abefea2..1f4fe74 100644 --- a/src/types.cc +++ b/src/types.cc @@ -30,6 +30,47 @@ using namespace rime; namespace { +template using void_t = void; + +template +struct COMPAT { + // fallback version if librime is old + static an new_ReverseDb(const std::string &file) { + return New(std::string(RimeGetUserDataDir()) + "/" + file); + } + + static string get_shared_data_dir() { + return string(rime_get_api()->get_shared_data_dir()); + } + + static string get_user_data_dir() { + return string(rime_get_api()->get_user_data_dir()); + } + + static string get_sync_dir() { + return string(rime_get_api()->get_sync_dir()); + } +}; + +template +struct COMPAT().user_data_dir.string())>> { + static an new_ReverseDb(const std::string &file) { + return New(Service::instance().deployer().user_data_dir / file); + } + + static string get_shared_data_dir() { + return Service::instance().deployer().shared_data_dir.string(); + } + + static string get_user_data_dir() { + return Service::instance().deployer().user_data_dir.string(); + } + + static string get_sync_dir() { + return Service::instance().deployer().sync_dir.string(); + } +}; + //--- wrappers for Segment namespace SegmentReg { using T = Segment; @@ -309,8 +350,7 @@ namespace ReverseDbReg { using T = ReverseDb; an make(const string &file) { - an db = New( - Service::instance().deployer().user_data_dir / file); + an db = COMPAT::new_ReverseDb(file); db->Load(); return db; } @@ -1849,18 +1889,6 @@ namespace RimeApiReg { return string(rime_get_api()->get_version()); } - string get_shared_data_dir() { - return Service::instance().deployer().shared_data_dir.string(); - } - - string get_user_data_dir() { - return Service::instance().deployer().user_data_dir.string(); - } - - string get_sync_dir() { - return Service::instance().deployer().sync_dir.string(); - } - string get_distribution_name(){ return Service::instance().deployer().distribution_name; } @@ -1906,9 +1934,9 @@ namespace RimeApiReg { static const luaL_Reg funcs[]= { { "get_rime_version", WRAP(get_rime_version) }, - { "get_shared_data_dir", WRAP(get_shared_data_dir) }, - { "get_user_data_dir", WRAP(get_user_data_dir) }, - { "get_sync_dir", WRAP(get_sync_dir) }, + { "get_shared_data_dir", WRAP(COMPAT::get_shared_data_dir) }, + { "get_user_data_dir", WRAP(COMPAT::get_user_data_dir) }, + { "get_sync_dir", WRAP(COMPAT::get_sync_dir) }, { "get_distribution_name", WRAP(get_distribution_name) }, { "get_distribution_code_name", WRAP(get_distribution_code_name) }, { "get_distribution_version", WRAP(get_distribution_version) }, diff --git a/src/types_ext.cc b/src/types_ext.cc index 68d58ab..99e351d 100644 --- a/src/types_ext.cc +++ b/src/types_ext.cc @@ -38,6 +38,18 @@ struct COMPAT().name_space())>> { } }; +// fallback version of file_path() if librime is old +template struct void_t1 { using t = int; }; +template().file_name())>::t = 0> +std::string get_UserDb_file_path_string(const T &t) { + return t.file_name(); +} + +template().file_path())>::t = 0> +std::string get_UserDb_file_path_string(const T &t) { + return t.file_path().string(); +} + namespace ProcessorReg{ using T = Processor; @@ -305,7 +317,7 @@ namespace UserDbReg{ {"read_only",WRAPMEM(T, readonly)}, {"disabled",WRAPMEM(T, disabled)}, {"name", WRAPMEM(T, name)}, - {"file_name", WRAP(file_name)}, + {"file_name", WRAP(get_UserDb_file_path_string)}, { NULL, NULL }, };