#include "common/pch.h" #include "ConfigUtil.h" #include #include #include "../common/JsonUtil.h" #include "../common/TimeUtil.h" bool fileExists(const std::string& filename) { std::ifstream file(filename); return file.good(); // Èç¹ûÎļþ³É¹¦´ò¿ª£¬Ôò·µ»Ø true } string ConfigUtil::getConfigPath() { wchar_t* appDataPath = nullptr; CString path; // »ñÈ¡APPDataĿ¼·¾¶ if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, NULL, &appDataPath))) { std::wcout << L"APPDataĿ¼·¾¶: " << appDataPath << std::endl; path.Append(appDataPath); CoTaskMemFree(appDataPath); // ÊÍ·ÅÄÚ´æ } else { std::wcerr << L"ÎÞ·¨»ñÈ¡APPDataĿ¼·¾¶" << std::endl; return "config.cfg"; } path.Append(L"\\Ðü¸¡¶¢ÅÌ"); // ÅжÏÎļþ¼ÐÊÇ·ñ´æÔÚ DWORD fileAttributes = GetFileAttributesW(path); if (fileAttributes == INVALID_FILE_ATTRIBUTES) { CreateDirectoryW(path, NULL); } string fpath = StringUtil::cstring2String(path); fpath.append("\\config.cfg"); return fpath.c_str(); } void ConfigUtil::readConfig(libconfig::Config& config) { string strConfPath = getConfigPath(); if (!fileExists(strConfPath)) { std::ofstream file(strConfPath); file.is_open(); } //½â¶ÁÅäÖÃÎļþ try { config.readFile(strConfPath.c_str()); } catch (const libconfig::FileIOException& fioex) { std::cerr << "I/O exception while reading the file." << std::endl; } catch (const libconfig::ParseException& pex) { std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine() << " - " << pex.getError() << std::endl; } } void ConfigUtil::writeConfig(libconfig::Config& config) { config.writeFile(getConfigPath().c_str()); } int ConfigUtil::readIntConfig(const char* key) { libconfig::Config mConfig; readConfig(mConfig); libconfig::Setting& root = mConfig.getRoot(); if (root.exists(key)) { int val; root.lookupValue(key, val); return val; } throw string("ÉÐδ»ñÈ¡µ½ÄÚÈÝ"); } string ConfigUtil::readStringConfig(const char* key) { libconfig::Config mConfig; readConfig(mConfig); libconfig::Setting& root = mConfig.getRoot(); if (root.exists(key)) { //string val; const char* val; root.lookupValue(key, val); return string(val); } throw string("ÉÐδ»ñÈ¡µ½ÄÚÈÝ"); } void ConfigUtil::setIntConfig(const char* key, int val) { libconfig::Config mConfig; readConfig(mConfig); libconfig::Setting& root = mConfig.getRoot(); if (root.exists(key)) { root[key] = val; } else { root.add(key, libconfig::Setting::TypeInt) = val; } writeConfig(mConfig); } void ConfigUtil::setStringConfig(const char* key, string val) { libconfig::Config mConfig; readConfig(mConfig); libconfig::Setting& root = mConfig.getRoot(); if (root.exists(key)) { root[key] = val.c_str(); } else { root.add(key, libconfig::Setting::TypeString) = val.c_str(); } writeConfig(mConfig); } list ConfigUtil::getKeys() { libconfig::Config mConfig; readConfig(mConfig); libconfig::Setting& root = mConfig.getRoot(); list keys; for (auto iter = root.begin(); iter != root.end(); ++iter) { keys.push_back(iter->getName()); } return keys; } void ConfigUtil::delKey(string key) { libconfig::Config mConfig; readConfig(mConfig); libconfig::Setting& root = mConfig.getRoot(); root.remove(key.c_str()); writeConfig(mConfig); } bool ConfigUtil::isTradeRefresh() { try { return readIntConfig("trade_refresh") > 0; } catch (...) { } return false; } bool ConfigUtil::isGroupRefresh() { try { return readIntConfig("group_refresh") > 0; } catch (...) { } return false; } bool ConfigUtil::isAutoFocus() { try { return readIntConfig("auto_focus") > 0; } catch (...) { } return false; } bool ConfigUtil::isTradeQuickKey() { try { return readIntConfig("trade_quick_key") > 0; } catch (...) { } return false; } int* ConfigUtil::getSellRuleDialogShowPos() { int* pos = (int*)malloc(sizeof(int) * 2); pos[0] = 0; pos[1] = 0; try { string str = readStringConfig("sell_rule_show_pos"); // ·Ö¸ô×Ö·û´® int split_index = str.find(","); int x = stoi(str.substr(0, split_index)); int y = stoi(str.substr(split_index + 1, str.length() - (split_index + 1))); pos[0] = x; pos[1] = y; } catch (...) { } return pos; } void ConfigUtil::setTradeRefresh(bool enable) { setIntConfig("trade_refresh", enable ? 1 : 0); } void ConfigUtil::setGroupRefresh(bool enable) { setIntConfig("group_refresh", enable ? 1 : 0); } void ConfigUtil::setAutoFocus(bool enable) { setIntConfig("auto_focus", enable ? 1 : 0); } void ConfigUtil::setTradeQuickKey(bool enable) { setIntConfig("trade_quick_key", enable ? 1 : 0); } void ConfigUtil::setSellRuleDialogShowPos(int x, int y) { string pos = ""; pos.append(to_string(x)).append(",").append(to_string(y)); setStringConfig("sell_rule_show_pos", pos); } MyPoint ConfigUtil::getWindowPos() { // Ò³ÂëλÖà try { string data = readStringConfig("window_pos"); rapidjson::Document root = JsonUtil::parseUTF8(data); return MyPoint({ root[0].GetInt(), root[1].GetInt() }); } catch (...) { } return MyPoint({ 0,0 }); } void ConfigUtil::setWindowPos(int x, int y) { string st = "["; st.append(std::to_string(x)).append(",").append(std::to_string(y)).append("]"); setStringConfig("window_pos", st); } int ConfigUtil::getThsAutoRefreshTimeSpace() { try { return readIntConfig("ths_auto_refresh_time_space"); } catch (...) { } return 0; } void ConfigUtil::setThsAutoRefreshTimeSpace(int ms) { setIntConfig("ths_auto_refresh_time_space", ms); } list ConfigUtil::getVolumesSetting() { try { list volumeList; string result = readStringConfig("volume_settings"); auto doc = JsonUtil::parseUTF8(result); auto array = doc.GetArray(); for (int i = 0; i < array.Size(); i++) { volumeList.push_back(array[i].GetInt()); } return volumeList; } catch (...) { } return list(); } void ConfigUtil::setVolumesSetting(list volumes) { string st = "["; int index = 0; for (list::iterator e = volumes.begin(); e != volumes.end(); ++e) { index++; st.append(to_string(*e)); if (index != volumes.size()) { st.append(","); } } st.append("]"); setStringConfig("volume_settings", st); } void ConfigUtil::setCodeNames(map codeNameMap) { // ½«mapתΪjson rapidjson::StringBuffer buf; rapidjson::PrettyWriter writer(buf); writer.StartObject(); for (map::iterator el = codeNameMap.begin(); el != codeNameMap.end(); el++) { string code = (*el).first; CString name = (*el).second; writer.Key(code.c_str()); writer.String(StringUtil::cstring2String(name).c_str()); } writer.EndObject(); const char* json_content = buf.GetString(); string key = "code_name_map-"; key.append(TimeUtil::getNowTime("%Y%m%d")); setStringConfig(key.c_str(), json_content); // ɾ³ý¹ýÆÚµÄ´úÂëÊý¾Ý list kyes = getKeys(); for (auto e = kyes.begin(); e != kyes.end(); ++e) { string k = *e; if (!k._Equal(key) && k.find("code_name_map-") == 0) { delKey(k); } } } map ConfigUtil::getCodeNames() { map m; try { string key = "code_name_map-"; key.append(TimeUtil::getNowTime("%Y%m%d")); string result = readStringConfig(key.c_str()); if (!result.empty()) { auto doc = JsonUtil::parseUTF8(result); auto root = doc.GetObjectW(); for (auto e = root.MemberBegin(); e != root.MemberEnd(); ++e) { string code = (*e).name.GetString(); string name = (*e).value.GetString(); m[code] = CString(name.c_str()); } } } catch (...) { } return m; } void ConfigUtil::setBuyMoney(int money) { setIntConfig("buy_money", money); } int ConfigUtil::getBuyMoney() { try { return readIntConfig("buy_money"); } catch (...) { // ĬÈÏ2w return 20000; } } void ConfigUtil::setSellMoney(int money) { setIntConfig("sell_default_money", money); } int ConfigUtil::getSellMoney() { try { return readIntConfig("sell_default_money"); } catch (...) { // ĬÈÏ5k return 5000; } } void ConfigUtil::setOCRWindowParams(OCRWindowParams params) { string st = "["; st.append(to_string(params.top)); st.append(","); st.append(to_string(params.width)); st.append(","); st.append(to_string(params.height)); st.append(","); st.append(to_string(params.scale)); st.append("]"); setStringConfig("ocr_window_params", st); } OCRWindowParams ConfigUtil::getOCRWindowParams() { string result = readStringConfig("ocr_window_params"); if (result.empty()) { throw string("û»ñÈ¡µ½ÅäÖÃÎļþ"); } auto doc = JsonUtil::parseUTF8(result); auto array = doc.GetArray(); OCRWindowParams params = { array[0].GetInt(),array[1].GetInt(),array[2].GetInt(),array[3].GetFloat() }; return params; } void ConfigUtil::setEnableOCRCode(bool enable) { setIntConfig("ocr_ths_code", enable ? 1 : 0); } bool ConfigUtil::getEnableOCRCode() { try { return readIntConfig("ocr_ths_code") > 0; } catch (...) { return FALSE; } return false; }