diff --git a/src/main.cpp b/src/main.cpp index d95e061..e183341 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,75 +5,61 @@ #include "utils/strings.h" #include "utils/files.h" #include "utils/log.h" -#include +#include #include -// 去除字符串首尾空白 -static std::string trim(const std::string &s) { - auto start = s.find_first_not_of(" \t\r\n"); - if (start == std::string::npos) return ""; - auto end = s.find_last_not_of(" \t\r\n"); - return s.substr(start, end - start + 1); -} - -// 解析config.ini,将键值对填入Config +// 解析config.ini,使用Windows系统API GetPrivateProfileStringW static bool parse_config(const std::wstring &path, Config &cfg, std::string *error) { - std::ifstream file(path); - if (!file.is_open()) { - if (error) *error = "Cannot open config file: " + utils::wstr_to_str(path); + if (GetFileAttributesW(path.c_str()) == INVALID_FILE_ATTRIBUTES) { + *error = "Config file not found: " + utils::wstr_to_str(path); return false; } - std::string line; - while (std::getline(file, line)) { - line = trim(line); - if (line.empty() || line[0] == ';' || line[0] == '#') continue; - if (line[0] == '[') continue; // 跳过section头 + wchar_t buf[512]; - size_t eq = line.find('='); - if (eq == std::string::npos) continue; + // [CONFIG] section + auto read_w = [&](const wchar_t *key) -> std::wstring { + DWORD len = GetPrivateProfileStringW(L"CONFIG", key, L"", buf, _countof(buf), path.c_str()); + return {buf, len}; + }; - std::string key = trim(line.substr(0, eq)); - std::string val = trim(line.substr(eq + 1)); + cfg.icf_path = read_w(L"ICF_PATH"); + cfg.image_dir = read_w(L"IMAGE_DIR"); + cfg.overlay_dir = read_w(L"OVERLAY_DIR"); - if (key == "ICF_PATH") { - cfg.icf_path = utils::to_wstring(val); - } else if (key == "IMAGE_DIR") { - cfg.image_dir = utils::to_wstring(val); - } else if (key == "OVERLAY_DIR") { - cfg.overlay_dir = utils::to_wstring(val); - } else if (key == "MOUNT_LETTER") { - if (!val.empty()) { - char ch = val[0]; - if (ch >= 'a' && ch <= 'z') ch -= 'a' - 'A'; - cfg.mount_letter = static_cast(ch); - } - } else if (key == "APP_LINK") { - cfg.app_link = utils::to_wstring(val); - } else if (key == "OPT_LINK") { - cfg.opt_link = utils::to_wstring(val); - } else if (key == "APP_KEY") { - cfg.app_key = val; - } else if (key == "APP_IV") { - cfg.app_iv = val; - } else if (key == "OPT_KEY") { - cfg.opt_key = val; - } else if (key == "OPT_IV") { - cfg.opt_iv = val; - } + std::wstring letter = read_w(L"MOUNT_LETTER"); + if (!letter.empty()) { + wchar_t ch = letter[0]; + if (ch >= L'a' && ch <= L'z') ch -= L'a' - L'A'; + cfg.mount_letter = ch; } + + cfg.app_link = read_w(L"APP_LINK"); + cfg.opt_link = read_w(L"OPT_LINK"); + + // [KEYS] section + auto read_a = [&](const wchar_t *key) -> std::string { + DWORD len = GetPrivateProfileStringW(L"KEYS", key, L"", buf, _countof(buf), path.c_str()); + return utils::wstr_to_str(std::wstring(buf, len)); + }; + + cfg.app_key = read_a(L"APP_KEY"); + cfg.app_iv = read_a(L"APP_IV"); + cfg.opt_key = read_a(L"OPT_KEY"); + cfg.opt_iv = read_a(L"OPT_IV"); + return true; } static void print_usage() { - LOG_INFO( + std::fputs( + "SEGA MOUNT\n" "Usage:\n" - " sega_mount -m [config.ini] 挂载\n" - " sega_mount -u [config.ini] 卸载\n" - "\n" - " config.ini默认读取exe所在目录,也可在命令行指定路径\n" - " Warn: UNMOUNT maybe unstable" + " sega_mount -m [config.ini] Mount\n" + " sega_mount -u [config.ini] Unmount\n", + stdout ); + std::fflush(stdout); } int main(int argc, char *argv[]) {