From 28d11c0d9f3604474ec4a4b22e6a930bbf074379 Mon Sep 17 00:00:00 2001 From: fanlumaster <1730976608@qq.com> Date: Fri, 30 Jan 2026 12:27:23 +0800 Subject: [PATCH] [config]: Read toml config file from appdata location --- server/CMakeLists.txt | 9 ++++-- server/README.md | 5 +-- server/assets/config/config.toml | 8 +++++ server/scripts/config_files/CMakeLists.txt | 9 ++++-- server/scripts/prepare_env.py | 2 +- server/src/config/ime_config.cpp | 36 ++++++++++++++++++++++ server/src/config/ime_config.h | 5 +++ server/src/main.cpp | 4 +++ server/src/utils/common_utils.cpp | 6 ++++ server/src/utils/common_utils.h | 1 + server/vcpkg.json | 3 +- 11 files changed, 80 insertions(+), 8 deletions(-) create mode 100644 server/src/config/ime_config.cpp create mode 100644 server/src/config/ime_config.h diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index 6f409f04..e593cdda 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -40,8 +40,8 @@ include_directories("./MetasequoiaImeEngine/utfcpp/source/") set(MY_EXECUTABLE_NAME "MetasequoiaImeServer") set(SRC_FILES - "./src/webview2/windows_webview2.cpp" - "./src/webview2/windows_webview2.h" + "./src/config/ime_config.cpp" + "./src/config/ime_config.h" "./src/defines/defines.h" "./src/global/globals.h" "./src/ipc/event_listener.cpp" @@ -60,6 +60,8 @@ set(SRC_FILES "./src/utils/webview_utils.h" "./src/utils/window_utils.cpp" "./src/utils/window_utils.h" + "./src/webview2/windows_webview2.cpp" + "./src/webview2/windows_webview2.h" "./src/window/ime_windows.cpp" "./src/window/ime_windows.h" "./src/window/window_hook.cpp" @@ -103,4 +105,7 @@ set_target_properties(${PROJECT_NAME} PROPERTIES VS_GLOBAL_WebView2LoaderPrefere set_target_properties(${PROJECT_NAME} PROPERTIES VS_USER_PROPS "${CMAKE_SOURCE_DIR}/MetasequoiaImeServer.props") set_target_properties(${PROJECT_NAME} PROPERTIES VS_PACKAGE_REFERENCES "Microsoft.Web.WebView2_1.0.3240.44;Microsoft.Windows.ImplementationLibrary_1.0.240803.1") +# Tomlplusplus and other head-only libraries +target_include_directories(${PROJECT_NAME} PRIVATE "${CMAKE_SOURCE_DIR}/build/vcpkg_installed/x64-windows/include") + target_link_libraries(${PROJECT_NAME} PRIVATE unofficial::sqlite3::sqlite3 fmt::fmt spdlog::spdlog Boost::locale Boost::json) \ No newline at end of file diff --git a/server/README.md b/server/README.md index 2e2aa1a6..746e939d 100644 --- a/server/README.md +++ b/server/README.md @@ -6,7 +6,7 @@ This is the server end of [MetasequoiaIME](https://github.com/fanlumaster/Metase ### Prerequisites -- Visual Studio 2022 +- Visual Studio 2026 - CMake - vcpkg - Nuget @@ -15,7 +15,7 @@ This is the server end of [MetasequoiaIME](https://github.com/fanlumaster/Metase Make sure vcpkg, Nuget and Boost are installed by **Scoop**. -### Build steps +### Build steps(For Dev) **First**, build IME dictonary and prepare assets, @@ -43,6 +43,7 @@ Prepare environment, cd MetasequoiaImeServer python .\scripts\prepare_env.py Copy-Item -Path .\assets\tables\* -Destination $env:LOCALAPPDATA\MetasequoiaImeTsf +New-Item -ItemType SymbolicLink -Path "$env:LOCALAPPDATA\MetasequoiaImeTsf\config.toml" -Target ".\assets\config\config.toml" ``` Then, build and run, diff --git a/server/assets/config/config.toml b/server/assets/config/config.toml index e69de29b..e6f942ed 100644 --- a/server/assets/config/config.toml +++ b/server/assets/config/config.toml @@ -0,0 +1,8 @@ +[general] +page_size = 8 +enable_emoji = true + +[candidate_window] +font = "Noto Sans SC" +font_size = 14 +follow_cursor = true \ No newline at end of file diff --git a/server/scripts/config_files/CMakeLists.txt b/server/scripts/config_files/CMakeLists.txt index 358ddf09..e78bed97 100644 --- a/server/scripts/config_files/CMakeLists.txt +++ b/server/scripts/config_files/CMakeLists.txt @@ -40,8 +40,8 @@ include_directories("./MetasequoiaImeEngine/utfcpp/source/") set(MY_EXECUTABLE_NAME "MetasequoiaImeServer") set(SRC_FILES - "./src/webview2/windows_webview2.cpp" - "./src/webview2/windows_webview2.h" + "./src/config/ime_config.cpp" + "./src/config/ime_config.h" "./src/defines/defines.h" "./src/global/globals.h" "./src/ipc/event_listener.cpp" @@ -60,6 +60,8 @@ set(SRC_FILES "./src/utils/webview_utils.h" "./src/utils/window_utils.cpp" "./src/utils/window_utils.h" + "./src/webview2/windows_webview2.cpp" + "./src/webview2/windows_webview2.h" "./src/window/ime_windows.cpp" "./src/window/ime_windows.h" "./src/window/window_hook.cpp" @@ -103,4 +105,7 @@ set_target_properties(${PROJECT_NAME} PROPERTIES VS_GLOBAL_WebView2LoaderPrefere set_target_properties(${PROJECT_NAME} PROPERTIES VS_USER_PROPS "${CMAKE_SOURCE_DIR}/MetasequoiaImeServer.props") set_target_properties(${PROJECT_NAME} PROPERTIES VS_PACKAGE_REFERENCES "Microsoft.Web.WebView2_1.0.3240.44;Microsoft.Windows.ImplementationLibrary_1.0.240803.1") +# Tomlplusplus and other head-only libraries +target_include_directories(${PROJECT_NAME} PRIVATE "${CMAKE_SOURCE_DIR}/build/vcpkg_installed/x64-windows/include") + target_link_libraries(${PROJECT_NAME} PRIVATE unofficial::sqlite3::sqlite3 fmt::fmt spdlog::spdlog Boost::locale Boost::json) \ No newline at end of file diff --git a/server/scripts/prepare_env.py b/server/scripts/prepare_env.py index 50dac017..e6cb4d37 100644 --- a/server/scripts/prepare_env.py +++ b/server/scripts/prepare_env.py @@ -73,7 +73,7 @@ with open(dot_clangd_output_file, "w", encoding="utf-8") as f: f.writelines(lines) # -# project_root/tests/CMakeLists.txt +# project_root/CMakeLists.txt # CMakeLists_file = os.path.join( MetasequoiaImeServer_root_path, "scripts", "config_files", "CMakeLists.txt" diff --git a/server/src/config/ime_config.cpp b/server/src/config/ime_config.cpp new file mode 100644 index 00000000..c7082640 --- /dev/null +++ b/server/src/config/ime_config.cpp @@ -0,0 +1,36 @@ +#include "ime_config.h" +#include +#include +#include +#include "utils/common_utils.h" + +void InitImeConfig() +{ + try + { + + std::filesystem::path config_path = std::filesystem::path(CommonUtils::get_ime_data_path()) / "config.toml"; + + if (!std::filesystem::exists(config_path)) + { + std::filesystem::create_directories(config_path.parent_path()); + // TODO: 写入默认配置 + } + + auto tbl = toml::parse_file(config_path.string()); + + int page_size = tbl["general"]["page_size"].value_or(0); + + std::string font = tbl["candidate_window"]["font"].value_or("default"); + + bool follow_cursor = tbl["candidate_window"]["follow_cursor"].value_or(false); + + OutputDebugString(fmt::format(L"page_size = {}\n", page_size).c_str()); + OutputDebugString(fmt::format(L"font = {}\n", string_to_wstring(font)).c_str()); + OutputDebugString(fmt::format(L"follow_cursor = {}\n", follow_cursor).c_str()); + } + catch (const toml::parse_error &err) + { + OutputDebugString(fmt::format(L"TOML error\n").c_str()); + } +} diff --git a/server/src/config/ime_config.h b/server/src/config/ime_config.h new file mode 100644 index 00000000..a8c5d981 --- /dev/null +++ b/server/src/config/ime_config.h @@ -0,0 +1,5 @@ +#pragma once + +#include + +void InitImeConfig(); \ No newline at end of file diff --git a/server/src/main.cpp b/server/src/main.cpp index 2d4cbabd..2545c253 100644 --- a/server/src/main.cpp +++ b/server/src/main.cpp @@ -3,6 +3,7 @@ #include "window/ime_windows.h" #include #include "ipc/ipc.h" +#include "config/ime_config.h" int CALLBACK WinMain(_In_ HINSTANCE hInstance, _In_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow) { @@ -13,6 +14,9 @@ int CALLBACK WinMain(_In_ HINSTANCE hInstance, _In_ HINSTANCE hPrevInstance, _In InitializeSpdLog(); // #endif + // Initialize config + InitImeConfig(); + ::InitIpc(); ::InitNamedPipe(); g_dictQuery = std::make_shared(); diff --git a/server/src/utils/common_utils.cpp b/server/src/utils/common_utils.cpp index 7931405e..3a0ab7ca 100644 --- a/server/src/utils/common_utils.cpp +++ b/server/src/utils/common_utils.cpp @@ -1,5 +1,6 @@ #include "common_utils.h" #include +#include "global/globals.h" using namespace std; @@ -80,6 +81,11 @@ string get_local_appdata_path() return localAppDataDirStr.empty() ? "" : localAppDataDirStr; } +string get_ime_data_path() +{ + return get_local_appdata_path() + "\\" + wstring_to_string(GlobalIme::AppName); +} + string get_username() { char *localAppDataDir = nullptr; diff --git a/server/src/utils/common_utils.h b/server/src/utils/common_utils.h index 83153f19..d4084397 100644 --- a/server/src/utils/common_utils.h +++ b/server/src/utils/common_utils.h @@ -13,6 +13,7 @@ void SendImeInputs(std::wstring words); namespace CommonUtils { std::string get_local_appdata_path(); +std::string get_ime_data_path(); std::string get_username(); std::vector cvt_str_to_vector(std::wstring text); } // namespace CommonUtils \ No newline at end of file diff --git a/server/vcpkg.json b/server/vcpkg.json index 6e07fff9..04b6b80a 100644 --- a/server/vcpkg.json +++ b/server/vcpkg.json @@ -2,6 +2,7 @@ "dependencies": [ "fmt", "spdlog", - "sqlite3" + "sqlite3", + "tomlplusplus" ] }