Skip to content

Commit

Permalink
compat old librime
Browse files Browse the repository at this point in the history
  • Loading branch information
hchunhui committed Feb 6, 2024
1 parent df714a5 commit 8edb8b8
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 38 deletions.
36 changes: 31 additions & 5 deletions src/modules.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <cstdio>
#include <rime/common.h>
#include <rime/registry.h>
#include <rime_api.h>
#include <rime/service.h>
#include "lib/lua_templates.h"
#include "lua_gears.h"
Expand All @@ -16,12 +17,37 @@ static bool file_exists(const char *fname) noexcept {
return false;
}

namespace {
template<typename> using void_t = void;

template<typename T, typename = void>
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<typename T>
struct COMPAT<T, void_t<decltype(std::declval<T>().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<rime::Deployer>::get_user_data_dir();
const auto shared_dir = COMPAT<rime::Deployer>::get_shared_data_dir();

types_init(L);
lua_getglobal(L, "package");
Expand Down
56 changes: 41 additions & 15 deletions src/opencc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace {
class Opencc {
public:
//static shared_ptr<Opencc> create(const path &config_path);
Opencc(const path& config_path);
Opencc(const string& utf8_config_path);
bool ConvertWord(const string& text, vector<string>* forms);
bool RandomConvertText(const string& text, string* simplified);
bool ConvertText(const string& text, string* simplified);
Expand All @@ -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<opencc::ConversionPtr> conversions =
converter_->GetConversionChain()->GetConversions();
dict_ = conversions.front()->GetDict();
Expand Down Expand Up @@ -116,23 +116,49 @@ vector<string> Opencc::convert_word(const string& text){
namespace OpenccReg {
using T = Opencc;

optional<T> 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<typename> using void_t = void;

template<typename U, typename = void>
struct COMPAT {
static optional<T> 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<typename U>
struct COMPAT<U, void_t<decltype(std::declval<U>().user_data_dir.string())>> {
static optional<T> 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<vector<string>> convert_word(T &t,const string &s) {
vector<string> res;
Expand All @@ -142,7 +168,7 @@ namespace OpenccReg {
}

static const luaL_Reg funcs[] = {
{"Opencc",WRAP(make)},
{"Opencc",WRAP(COMPAT<Deployer>::make)},
{ NULL, NULL },
};

Expand Down
62 changes: 45 additions & 17 deletions src/types.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,47 @@ using namespace rime;

namespace {

template<typename> using void_t = void;

template<typename T, typename = void>
struct COMPAT {
// fallback version if librime is old
static an<ReverseDb> new_ReverseDb(const std::string &file) {
return New<ReverseDb>(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_data_dir());
}
};

template<typename T>
struct COMPAT<T, void_t<decltype(std::declval<T>().user_data_dir.string())>> {
static an<ReverseDb> new_ReverseDb(const std::string &file) {
return New<ReverseDb>(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;
Expand Down Expand Up @@ -309,8 +350,7 @@ namespace ReverseDbReg {
using T = ReverseDb;

an<T> make(const string &file) {
an<T> db = New<ReverseDb>(
Service::instance().deployer().user_data_dir / file);
an<T> db = COMPAT<Deployer>::new_ReverseDb(file);
db->Load();
return db;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<Deployer>::get_shared_data_dir) },
{ "get_user_data_dir", WRAP(COMPAT<Deployer>::get_user_data_dir) },
{ "get_sync_dir", WRAP(COMPAT<Deployer>::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) },
Expand Down
14 changes: 13 additions & 1 deletion src/types_ext.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ struct COMPAT<T, void_t<decltype(std::declval<T>().name_space())>> {
}
};

// fallback version of file_path() if librime is old
template<typename> struct void_t1 { using t = int; };
template<typename T, typename void_t1<decltype(std::declval<T>().file_name())>::t = 0>
std::string get_UserDb_file_path_string(const T &t) {
return t.file_name();
}

template<typename T, typename void_t1<decltype(std::declval<T>().file_path())>::t = 0>
std::string get_UserDb_file_path_string(const T &t) {
return t.file_path().string();
}

namespace ProcessorReg{
using T = Processor;

Expand Down Expand Up @@ -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<T>)},
{ NULL, NULL },
};

Expand Down

0 comments on commit 8edb8b8

Please sign in to comment.