-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPQHex.cpp
More file actions
190 lines (157 loc) · 5.99 KB
/
PQHex.cpp
File metadata and controls
190 lines (157 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* This file is part of PQHex.
* Copyright (C) 2025 kylon
*
* PQHex is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PQHex is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QFileDialog>
#include "PQHex.h"
#include "./ui_pqhex.h"
#include "tabs/NatureTab.h"
PQHex::PQHex(QWidget *parent): QMainWindow(parent), ui(new Ui::PQHex) {
ui->setupUi(this);
setWindowTitle("PQHex");
setWindowIcon(QIcon(":/ico/pqhex"));
QVBoxLayout *lyt = new QVBoxLayout();
QHBoxLayout *actionsLyt = new QHBoxLayout();
PQH::UI::NatureTab *natureTab = new PQH::UI::NatureTab();
tabWidget = new QTabWidget();
mainTab = new PQH::UI::MainTab();
logTab = new PQH::UI::LogTab();
loadBtn = new QPushButton("Load");
loadDecryptedBtn = new QPushButton("Load decrypted");
saveBtn = new QPushButton("Save");
saveDecryptedBtn = new QPushButton("Save decrypted");
tabWidget->addTab(mainTab, "Main");
tabWidget->addTab(natureTab, "Nature Table");
tabWidget->addTab(logTab, "Logs");
saveBtn->setEnabled(false);
saveDecryptedBtn->setEnabled(false);
logTabIdx = tabWidget->indexOf(logTab);
actionsLyt->addWidget(loadBtn);
actionsLyt->addWidget(loadDecryptedBtn);
actionsLyt->addWidget(saveBtn);
actionsLyt->addWidget(saveDecryptedBtn);
lyt->addWidget(tabWidget);
lyt->addLayout(actionsLyt);
ui->centralwidget->setLayout(lyt);
QObject::connect(loadBtn, &QPushButton::clicked, this, &PQHex::onLoadBtnClicked);
QObject::connect(loadDecryptedBtn, &QPushButton::clicked, this, &PQHex::onLoadDecryptedBtnClicked);
QObject::connect(saveBtn, &QPushButton::clicked, this, &PQHex::onSaveBtnClicked);
QObject::connect(saveDecryptedBtn, &QPushButton::clicked, this, &PQHex::onSaveDecryptedBtnClicked);
QObject::connect(tabWidget, &QTabWidget::currentChanged, this, &PQHex::onTabChanged);
QObject::connect(mainTab, &PQH::UI::MainTab::ingridientsSaved, this, &PQHex::onIngridientsSaved);
QObject::connect(mainTab, &PQH::UI::MainTab::playerNameChanged, this, &PQHex::onPlayerNameChanged);
QObject::connect(mainTab, &PQH::UI::MainTab::stoneEdited, this, &PQHex::onStoneEdited);
QObject::connect(mainTab, &PQH::UI::MainTab::goodsSaved, this, &PQHex::onGoodsSaved);
}
PQHex::~PQHex() {
delete ui;
}
void PQHex::load(const bool encrypted) {
const QString fsave = QFileDialog::getOpenFileName(this, "Load savedata");
PQH::Error ret;
if (fsave.isEmpty())
return;
pqsave = std::make_unique<PQH::Save>();
ret = encrypted ? pqsave->read(fsave) : pqsave->readDecrypted(fsave);
if (ret != PQH::Error::NONE) {
writeLog(getErrorString(ret));
} else {
mainTab->setData(pqsave->getCheckData(), pqsave->getSerializeData());
saveDecryptedBtn->setEnabled(true);
saveBtn->setEnabled(true);
}
}
void PQHex::writeLog(const QString &msg) {
logTab->write(msg);
if (tabWidget->currentIndex() == logTabIdx)
return;
++unreadLogs;
tabWidget->setTabText(logTabIdx, QString("Logs (%1)").arg(unreadLogs));
}
QString PQHex::getErrorString(const PQH::Error e) const {
switch (e) {
case PQH::Error::SAVE_FILE_READ:
return "Failed to read save file";
case PQH::Error::WRITE_FILE_OPEN:
return "Cannot write to file";
case PQH::Error::DEC_HEAD:
return "Failed to decrypt head";
case PQH::Error::ENC_HEAD:
return "Failed to encrypt head";
case PQH::Error::DEC_BODY:
return "Failed to decrypt body";
case PQH::Error::ENC_BODY:
return "Failed to encrypt body";
case PQH::Error::CALC_CHECKSUM:
return "Failed to calculate checksum";
case PQH::Error::CHECKSUM_MISMATCH:
return "Checksum error, file may be corrupt";
case PQH::Error::NONE:
return "Success";
default:
return "Unknown error";
}
}
void PQHex::onLoadBtnClicked() {
load(true);
}
void PQHex::onLoadDecryptedBtnClicked() {
load(false);
}
void PQHex::onSaveBtnClicked() {
const QString savef = QFileDialog::getSaveFileName(this, "Encrypted savedata", "user");
PQH::Error ret;
if (savef.isEmpty())
return;
ret = pqsave->write(savef);
if (ret != PQH::Error::NONE)
writeLog(getErrorString(ret));
else
writeLog("Save: success");
}
void PQHex::onSaveDecryptedBtnClicked() {
const QString savef = QFileDialog::getSaveFileName(this, "Decrypted savedata", "user_dec");
PQH::Error ret;
if (savef.isEmpty())
return;
ret = pqsave->writeDecrypted(savef);
if (ret != PQH::Error::NONE)
writeLog(getErrorString(ret));
else
writeLog("Save decrypted: success");
}
void PQHex::onTabChanged(const int idx) {
if (idx == logTabIdx && unreadLogs) {
tabWidget->setTabText(logTabIdx, "Logs");
unreadLogs = 0;
}
}
void PQHex::onIngridientsSaved(const QList<int> &counts) const {
pqsave->patchIngridients(counts);
mainTab->setData(pqsave->getCheckData(), pqsave->getSerializeData());
}
void PQHex::onPlayerNameChanged(const QString &name) const {
pqsave->patchPlayerName(name);
mainTab->setData(pqsave->getCheckData(), pqsave->getSerializeData());
}
void PQHex::onStoneEdited(const int key) const {
pqsave->patchStone(key);
mainTab->setData(pqsave->getCheckData(), pqsave->getSerializeData());
}
void PQHex::onGoodsSaved() const {
pqsave->patchGoods();
mainTab->setData(pqsave->getCheckData(), pqsave->getSerializeData());
}