From 6edb3b0d9ba1bf8c26243f09e69c1a80b997643b Mon Sep 17 00:00:00 2001 From: arm64v8a <48624112+arm64v8a@users.noreply.github.com> Date: Mon, 2 Jan 2023 10:21:47 +0900 Subject: [PATCH] optmize code --- CMakeLists.txt | 13 +++++++------ db/Database.cpp | 22 ++++++++++++---------- db/Database.hpp | 1 + db/ProxyEntity.hpp | 2 +- main/NekoRay.cpp | 20 ++++++-------------- main/NekoRay.hpp | 12 ++++++++++++ main/NekoRay_ConfigItem.hpp | 10 +++++----- main/NekoRay_DataStore.hpp | 12 +++--------- ui/dialog_basic_settings.ui | 4 ++-- ui/dialog_manage_routes.cpp | 6 +++--- ui/mainwindow.cpp | 7 ++++--- 11 files changed, 56 insertions(+), 53 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 16f6ac3..d8b67a6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,14 +34,15 @@ include("cmake/fuck_windows/fuck.cmake") #### default prefix path #### -if (NKR_PACKAGE) - list(APPEND NKR_LIBS ${CMAKE_SOURCE_DIR}/libs/deps/package) -else () - list(APPEND NKR_LIBS ${CMAKE_SOURCE_DIR}/libs/deps/built) +if (NOT NKR_LIBS) + if (NKR_PACKAGE) + list(APPEND NKR_LIBS ${CMAKE_SOURCE_DIR}/libs/deps/package) + else () + list(APPEND NKR_LIBS ${CMAKE_SOURCE_DIR}/libs/deps/built) + endif () endif () -if (NKR_DISABLE_LIBS) -else () +if (NOT NKR_DISABLE_LIBS) list(APPEND CMAKE_PREFIX_PATH ${NKR_LIBS}) endif () diff --git a/db/Database.cpp b/db/Database.cpp index b0cfd30..b61db7a 100644 --- a/db/Database.cpp +++ b/db/Database.cpp @@ -10,19 +10,21 @@ namespace NekoRay { ProfileManager *profileManager = new ProfileManager(); ProfileManager::ProfileManager() : JsonStore("groups/pm.json") { - _hooks_after_load.push_back([=]() { LoadManager(); }); - _hooks_before_save.push_back([=]() { SaveManager(); }); + callback_after_load = [this]() { LoadManager(); }; + callback_before_save = [this]() { SaveManager(); }; _add(new configItem("profiles", &_profiles, itemType::integerList)); _add(new configItem("groups", &_groups, itemType::integerList)); } void ProfileManager::LoadManager() { - QList invaild_profile_id; + profiles = {}; + groups = {}; + QList invalidProfileId; for (auto id: _profiles) { auto ent = LoadProxyEntity(QString("profiles/%1.json").arg(id)); if (ent == nullptr || ent->bean == nullptr || ent->bean->version == -114514) { - // clear invaild profile - invaild_profile_id << id; + // clear invalid profile + invalidProfileId << id; continue; } profiles[id] = ent; @@ -30,7 +32,7 @@ namespace NekoRay { for (auto id: _groups) { groups[id] = LoadGroup(QString("groups/%1.json").arg(id)); } - for (auto id: invaild_profile_id) { + for (auto id: invalidProfileId) { DeleteProfile(id); } } @@ -55,8 +57,7 @@ namespace NekoRay { } if (validType) { - // 加载前设置好 fn - ent->load_control_force = true; + ent->load_control_must = true; ent->fn = jsonPath; ent->Load(); } @@ -101,8 +102,9 @@ namespace NekoRay { // ProxyEntity - ProxyEntity::ProxyEntity(fmt::AbstractBean *bean, QString _type) { - type = std::move(_type); + ProxyEntity::ProxyEntity(fmt::AbstractBean *bean, const QString &type_) { + if (type_ != nullptr) this->type = type_; + _add(new configItem("type", &type, itemType::string)); _add(new configItem("id", &id, itemType::integer)); _add(new configItem("gid", &gid, itemType::integer)); diff --git a/db/Database.hpp b/db/Database.hpp index f415416..9e2ea36 100644 --- a/db/Database.hpp +++ b/db/Database.hpp @@ -7,6 +7,7 @@ namespace NekoRay { class ProfileManager : public JsonStore { public: + // Manager QMap> profiles; QMap> groups; diff --git a/db/ProxyEntity.hpp b/db/ProxyEntity.hpp index e927aa0..9195408 100644 --- a/db/ProxyEntity.hpp +++ b/db/ProxyEntity.hpp @@ -34,7 +34,7 @@ namespace NekoRay { // Cache QString full_test_report; - ProxyEntity(fmt::AbstractBean *bean, QString _type); + ProxyEntity(fmt::AbstractBean *bean, const QString &type_); [[nodiscard]] QString DisplayLatency() const; diff --git a/main/NekoRay.cpp b/main/NekoRay.cpp index 83d1ff2..4d6e3c9 100644 --- a/main/NekoRay.cpp +++ b/main/NekoRay.cpp @@ -109,7 +109,7 @@ namespace NekoRay { _add(new configItem("custom", &this->custom, itemType::string)); } - QString Routing::toString() const { + QString Routing::DisplayRouting() const { return QString("[Proxy] %1\n[Proxy] %2\n[Direct] %3\n[Direct] %4\n[Block] %5\n[Block] %6") .arg(SplitLines(proxy_domain).join(",")) .arg(SplitLines(proxy_ip).join(",")) @@ -219,9 +219,6 @@ namespace NekoRay { void JsonStore::FromJson(QJsonObject object) { for (const auto &key: object.keys()) { if (_map.count(key) == 0) { - if (debug_verbose) { - qDebug() << QString("unknown key\n%1\n%2").arg(key, QJsonObject2QString(object, false)); - } continue; } @@ -273,16 +270,12 @@ namespace NekoRay { if (value.type() != QJsonValue::Object) { continue; } - if (load_control_no_jsonStore) - continue; ((JsonStore *) item->ptr)->FromJson(value.toObject()); break; } } - for (const auto &hook: _hooks_after_load) { - hook(); - } + if (callback_after_load != nullptr) callback_after_load(); } void JsonStore::FromJsonBytes(const QByteArray &data) { @@ -290,7 +283,7 @@ namespace NekoRay { auto document = QJsonDocument::fromJson(data, &error); if (error.error != error.NoError) { - if (debug_verbose) qDebug() << "QJsonParseError" << error.errorString(); + qDebug() << "QJsonParseError" << error.errorString(); return; } @@ -298,9 +291,7 @@ namespace NekoRay { } bool JsonStore::Save() { - for (const auto &hook: _hooks_before_save) { - hook(); - } + if (callback_before_save != nullptr) callback_before_save(); auto save_content = ToJsonBytes(); auto changed = last_save_content != save_content; @@ -319,8 +310,9 @@ namespace NekoRay { QFile file; file.setFileName(fn); - if (!file.exists() && !load_control_force) + if (!file.exists() && !load_control_must) { return false; + } bool ok = file.open(QIODevice::ReadOnly); if (!ok) { diff --git a/main/NekoRay.hpp b/main/NekoRay.hpp index c2bf117..282c37b 100644 --- a/main/NekoRay.hpp +++ b/main/NekoRay.hpp @@ -4,3 +4,15 @@ #include "NekoRay_Utils.hpp" #include "NekoRay_ConfigItem.hpp" #include "NekoRay_DataStore.hpp" + +// Switch core support + +namespace NekoRay { + inline int coreType = NekoRay::CoreType::V2RAY; + + QString FindCoreAsset(const QString &name); +} // namespace NekoRay + +#define IS_NEKO_BOX (NekoRay::coreType == NekoRay::CoreType::SING_BOX) +#define ROUTES_PREFIX_NAME QString(IS_NEKO_BOX ? "routes_box" : "routes") +#define ROUTES_PREFIX QString(ROUTES_PREFIX_NAME + "/") diff --git a/main/NekoRay_ConfigItem.hpp b/main/NekoRay_ConfigItem.hpp index c6e6dca..d3c437c 100644 --- a/main/NekoRay_ConfigItem.hpp +++ b/main/NekoRay_ConfigItem.hpp @@ -29,12 +29,12 @@ namespace NekoRay { class JsonStore { public: QMap> _map; - QList> _hooks_after_load; - QList> _hooks_before_save; + + std::function callback_after_load = nullptr; + std::function callback_before_save = nullptr; + QString fn; - bool debug_verbose = false; - bool load_control_force = false; - bool load_control_no_jsonStore = false; //不加载 json object + bool load_control_must = false; // must load from file bool save_control_compact = false; QByteArray last_save_content; diff --git a/main/NekoRay_DataStore.hpp b/main/NekoRay_DataStore.hpp index 55e9dc2..9da8d7c 100644 --- a/main/NekoRay_DataStore.hpp +++ b/main/NekoRay_DataStore.hpp @@ -2,8 +2,6 @@ namespace NekoRay { - QString FindCoreAsset(const QString &name); - class Routing : public JsonStore { public: QString direct_ip; @@ -16,7 +14,7 @@ namespace NekoRay { explicit Routing(int preset = 0); - [[nodiscard]] QString toString() const; + [[nodiscard]] QString DisplayRouting() const; static QStringList List(); @@ -134,6 +132,8 @@ namespace NekoRay { // Other Core ExtraCore *extraCore = new ExtraCore; + // Methods + DataStore(); void UpdateStartedId(int id); @@ -141,10 +141,4 @@ namespace NekoRay { extern DataStore *dataStore; - inline int coreType = NekoRay::CoreType::V2RAY; - } // namespace NekoRay - -#define IS_NEKO_BOX (NekoRay::coreType == NekoRay::CoreType::SING_BOX) -#define ROUTES_PREFIX_NAME QString(IS_NEKO_BOX ? "routes_box" : "routes") -#define ROUTES_PREFIX QString(ROUTES_PREFIX_NAME + "/") diff --git a/ui/dialog_basic_settings.ui b/ui/dialog_basic_settings.ui index 2140597..25447b2 100644 --- a/ui/dialog_basic_settings.ui +++ b/ui/dialog_basic_settings.ui @@ -414,8 +414,8 @@ - - + + diff --git a/ui/dialog_manage_routes.cpp b/ui/dialog_manage_routes.cpp index 8ce4b1a..82c01ce 100644 --- a/ui/dialog_manage_routes.cpp +++ b/ui/dialog_manage_routes.cpp @@ -167,10 +167,10 @@ void DialogManageRoutes::on_load_save_clicked() { auto fn = lineEdit->text(); if (!fn.isEmpty()) { auto r = std::make_unique(); - r->load_control_force = true; + r->load_control_must = true; r->fn = ROUTES_PREFIX + fn; if (r->Load()) { - if (QMessageBox::question(nullptr, software_name, tr("Load routing: %1").arg(fn) + "\n" + r->toString()) == QMessageBox::Yes) { + if (QMessageBox::question(nullptr, software_name, tr("Load routing: %1").arg(fn) + "\n" + r->DisplayRouting()) == QMessageBox::Yes) { REFRESH_ACTIVE_ROUTING(fn, r) w->accept(); } @@ -183,7 +183,7 @@ void DialogManageRoutes::on_load_save_clicked() { auto r = std::make_unique(); SAVE_TO_ROUTING(r) r->fn = ROUTES_PREFIX + fn; - if (QMessageBox::question(nullptr, software_name, tr("Save routing: %1").arg(fn) + "\n" + r->toString()) == QMessageBox::Yes) { + if (QMessageBox::question(nullptr, software_name, tr("Save routing: %1").arg(fn) + "\n" + r->DisplayRouting()) == QMessageBox::Yes) { r->Save(); REFRESH_ACTIVE_ROUTING(fn, r) w->accept(); diff --git a/ui/mainwindow.cpp b/ui/mainwindow.cpp index 9113bb4..4454c60 100644 --- a/ui/mainwindow.cpp +++ b/ui/mainwindow.cpp @@ -290,10 +290,10 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi auto fn = a->text(); if (!fn.isEmpty()) { NekoRay::Routing r; - r.load_control_force = true; + r.load_control_must = true; r.fn = ROUTES_PREFIX + fn; if (r.Load()) { - if (QMessageBox::question(GetMessageBoxParent(), software_name, tr("Load routing and apply: %1").arg(fn) + "\n" + r.toString()) == QMessageBox::Yes) { + if (QMessageBox::question(GetMessageBoxParent(), software_name, tr("Load routing and apply: %1").arg(fn) + "\n" + r.DisplayRouting()) == QMessageBox::Yes) { NekoRay::Routing::SetToActive(fn); if (NekoRay::dataStore->started_id >= 0) { neko_start(NekoRay::dataStore->started_id); @@ -965,7 +965,8 @@ void MainWindow::on_menu_profile_debug_info_triggered() { if (btn == 1) { QDesktopServices::openUrl(QUrl::fromLocalFile(QFileInfo(QString("profiles/%1.json").arg(ents.first()->id)).absoluteFilePath())); } else if (btn == 2) { - ents.first()->Load(); + NekoRay::dataStore->Load(); + NekoRay::profileManager->Load(); refresh_proxy_list(); } }