optmize code

This commit is contained in:
arm64v8a
2023-01-02 10:21:47 +09:00
parent 98ce9d64df
commit 6edb3b0d9b
11 changed files with 56 additions and 53 deletions

View File

@@ -34,14 +34,15 @@ include("cmake/fuck_windows/fuck.cmake")
#### default prefix path ####
if (NKR_PACKAGE)
if (NOT NKR_LIBS)
if (NKR_PACKAGE)
list(APPEND NKR_LIBS ${CMAKE_SOURCE_DIR}/libs/deps/package)
else ()
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 ()

View File

@@ -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<int> invaild_profile_id;
profiles = {};
groups = {};
QList<int> 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));

View File

@@ -7,6 +7,7 @@
namespace NekoRay {
class ProfileManager : public JsonStore {
public:
// Manager
QMap<int, QSharedPointer<ProxyEntity>> profiles;
QMap<int, QSharedPointer<Group>> groups;

View File

@@ -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;

View File

@@ -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) {

View File

@@ -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 + "/")

View File

@@ -29,12 +29,12 @@ namespace NekoRay {
class JsonStore {
public:
QMap<QString, QSharedPointer<configItem>> _map;
QList<std::function<void()>> _hooks_after_load;
QList<std::function<void()>> _hooks_before_save;
std::function<void()> callback_after_load = nullptr;
std::function<void()> 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;

View File

@@ -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 + "/")

View File

@@ -414,8 +414,8 @@
</widget>
</item>
<item>
<widget class="QGroupBox" name="horizontalGroupBox_2">
<layout class="QHBoxLayout" name="horizontalLayout_23">
<widget class="QGroupBox" name="horizontalGroupBox_5">
<layout class="QHBoxLayout" name="horizontalLayout_6">
<item>
<widget class="QCheckBox" name="start_minimal">
<property name="sizePolicy">

View File

@@ -167,10 +167,10 @@ void DialogManageRoutes::on_load_save_clicked() {
auto fn = lineEdit->text();
if (!fn.isEmpty()) {
auto r = std::make_unique<NekoRay::Routing>();
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<NekoRay::Routing>();
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();

View File

@@ -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();
}
}