From 55b72973676e1f71aca276bdf01fcb4c30794eb4 Mon Sep 17 00:00:00 2001 From: gb <741021719@qq.com> Date: Thu, 19 Jan 2023 15:44:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AF=AD=E8=A8=80=E5=8C=85?= =?UTF-8?q?=E6=94=B9=E5=8F=98=E4=BA=8B=E4=BB=B6=E6=B3=A8=E5=86=8C=E5=8F=8A?= =?UTF-8?q?=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app_language.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 3 deletions(-) diff --git a/app_language.cpp b/app_language.cpp index c5044ad..fa1c030 100644 --- a/app_language.cpp +++ b/app_language.cpp @@ -15,6 +15,7 @@ #include #include #include +#include namespace util { @@ -142,7 +143,18 @@ class lang_mgr return std::find(code_pages.begin(), code_pages.end(), cp) != code_pages.end(); } }MAPSTR; + typedef struct _chg_notify + { + void(*notify)(int code_page, void* param); + void* param; + bool operator==(void(*ntfy)(int code_page, void* param)) + { + return notify == ntfy; + } + }CHGNOTIFY; + std::vector notify_; + volatile bool in_changing_; std::vector code_pages_; LANATTR **all_; uint32_t os_cp_; @@ -286,6 +298,11 @@ class lang_mgr return ret; } + void notify_changed(void) + { + for (auto& v : notify_) + v.notify(cur_cp_, v.param); + } #if defined(_WIN32) || defined(_WIN64) static std::string u2a(const wchar_t* u, UINT cp = CP_ACP) @@ -447,7 +464,7 @@ class lang_mgr #endif } - lang_mgr() : os_cp_(lang_mgr::get_os_code_page()), cur_cp_(-1), cur_lang_(""), all_(nullptr) + lang_mgr() : os_cp_(lang_mgr::get_os_code_page()), cur_cp_(-1), cur_lang_(""), all_(nullptr), in_changing_(true) { std::string path(get_module_path()); size_t pos = path.rfind(PATH_SEPERATOR[0]); @@ -482,6 +499,7 @@ class lang_mgr str += v.name.length() + 2; st++; } + in_changing_ = false; } ~lang_mgr() { @@ -520,7 +538,7 @@ public: if (*locale >= '0' && *locale <= '9') cp = atoi(locale); } - end = (char *)";"; + *end = ';'; } return cp; @@ -557,12 +575,30 @@ public: if (it == code_pages_.end()) return ENOENT; - load_language_pak(it->file.c_str(), false); + std::string f(it->file); + + in_changing_ = true; + std::this_thread::sleep_for(std::chrono::milliseconds(50)); + + cp = cur_cp_; + load_language_pak(f.c_str(), false); + in_changing_ = false; + + if (cp != cur_cp_) + notify_changed(); return 0; } const char* get_string(uint32_t id, int* err) { + int cnt = 0; + while (in_changing_) + { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + if (cnt++ > 100) + return ""; + } + if (map_off_[id].size()) { if (err) @@ -582,6 +618,28 @@ public: { return code_pages_.size(); } + void register_language_changed_notify(void(*lan_changed)(int code_page, void* param), bool reg, void* param = nullptr) + { + std::vector::iterator it = std::find(notify_.begin(), notify_.end(), lan_changed); + + if (it == notify_.end()) + { + if (reg) + { + CHGNOTIFY c; + c.notify = lan_changed; + c.param = param; + notify_.push_back(c); + } + } + else + { + if (reg) + it->param = param; + else + notify_.erase(it); + } + } }; lang_mgr* lang_mgr::inst_ = nullptr; @@ -610,4 +668,8 @@ extern "C" { return lang_mgr::instance()->get_string(id, err); } + void register_language_changed_notify(void(*lan_changed)(int code_page, void* param), bool reg, void* param) + { + lang_mgr::instance()->register_language_changed_notify(lan_changed, reg, param); + } }