-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
357 lines (296 loc) · 12.4 KB
/
mainwindow.cpp
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include "mainwindow.h"
#include <QtWidgets>
#include <QMainWindow>
#include <iostream>
#include <fstream>
#include <string>
MainWindow::MainWindow()
{
MainWindow::state = EDMWstate::unloaded;
// Set layout in QWidget
QWidget *window = new QWidget();
// Set layout
QGridLayout *layout = new QGridLayout(window);
QLabel *infolabel = new QLabel;
infolabel->setText("Select the file to patch (Empires_DMW.exe)");
layout->addWidget(infolabel,0,0,1,2);
QPushButton *loadBtn = new QPushButton;
loadBtn->setText("open File");
layout->addWidget(loadBtn,1,0,1,2);
fileLabel = new QLabel;
fileLabel->setText("No file loaded");
layout->addWidget(fileLabel,2,0,1,2);
patchBtn = new QPushButton;
patchBtn->setText("patch loaded File");
layout->addWidget(patchBtn,3,0,1,2);
// Zoom Input
QLabel *zoomEditInfo = new QLabel;
zoomEditInfo->setText("Zoom Scaling (default: 1.0)");
layout->addWidget(zoomEditInfo,4,0,1,1);
zoomEdit = new QLineEdit;
//zoomEdit->setText(QString::fromStdString(std::to_string(-20.5)));
zoomEdit->setText(QString::number(-20.5));
layout->addWidget(zoomEdit,4,1,1,1);
// Cull/Fog Input
QLabel *cullEditInfo = new QLabel;
cullEditInfo->setText("Cull, (default: 26.29)");
layout->addWidget(cullEditInfo,5,0,1,1);
cullEdit = new QLineEdit;
//cullEdit->setText(QString::fromStdString(std::to_string(35.0)));
cullEdit->setText(QString::number(35.0));
layout->addWidget(cullEdit,5,1,1,1);
// Save Button
saveBtn = new QPushButton;
saveBtn->setText("Write values");
layout->addWidget(saveBtn,6,0,1,2);
saveDefaultBtn = new QPushButton;
saveDefaultBtn->setText("Write default values");
layout->addWidget(saveDefaultBtn,7,0,1,2);
// Set QWidget as the central layout of the main window
setCentralWidget(window);
setWindowFlags(Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint );
setUnifiedTitleAndToolBarOnMac(true);
connect( patchBtn, SIGNAL(clicked()),this,SLOT(patchFileClicked()));
connect( loadBtn, SIGNAL(clicked()),this,SLOT(openFileClicked()));
connect( saveBtn, SIGNAL(clicked()),this,SLOT(saveClicked()));
connect( saveDefaultBtn, SIGNAL(clicked()),this,SLOT(saveDefaultClicked()));
updateByState();
}
void MainWindow::updateByState(){
QString stateStringEDMW;
float zoomValue = patchedDefaultZoomScaling;
float cullValue = patchedDefaultCull;
switch (state) {
case unloaded:
patchBtn->setEnabled(false);
patchBtn->setText("patch loaded File");
zoomEdit->setEnabled(false);
cullEdit->setEnabled(false);
saveBtn->setEnabled(false);
saveDefaultBtn->setEnabled(false);
fileLabel->setText("No file loaded");
zoomEdit->setText(QString::number(zoomValue));
cullEdit->setText(QString::number(cullValue));
break;
case loadedUnpatched:
patchBtn->setEnabled(true);
patchBtn->setText("patch loaded File");
zoomEdit->setEnabled(false);
cullEdit->setEnabled(false);
saveBtn->setEnabled(true);
saveDefaultBtn->setEnabled(false);
stateStringEDMW = QString("File loaded (unpatched):\n");
stateStringEDMW.append(path);
fileLabel->setText(stateStringEDMW);
zoomValue = patchedDefaultZoomScaling;
cullValue = patchedDefaultCull;
zoomEdit->setText(QString::number(zoomValue));
cullEdit->setText(QString::number(cullValue));
break;
case loadedPatched:
patchBtn->setEnabled(true);
patchBtn->setText("unpatch loaded File");
zoomEdit->setEnabled(true);
cullEdit->setEnabled(true);
saveBtn->setEnabled(true);
saveDefaultBtn->setEnabled(true);
stateStringEDMW = QString("File loaded (patched):\n");
stateStringEDMW.append(path);
fileLabel->setText(stateStringEDMW);
zoomValue = *(reinterpret_cast<float*>(data.data()+EDMWPatchedZoomScalingPos));
cullValue = *(reinterpret_cast<float*>(data.data()+EDMWCullPos));
zoomEdit->setText(QString::number(zoomValue));
cullEdit->setText(QString::number(cullValue));
break;
default:
std::cerr << "Unknown state" << std::endl;
break;
}
}
void MainWindow::patchFileClicked(){
uint32_t *imgSize = nullptr;
uint16_t *segSize = nullptr;
uint8_t *segName = nullptr;
uint32_t *segVirtualSize = nullptr;
uint32_t *segVirtualAddress = nullptr;
uint32_t *segSizeOfRawData = nullptr;
uint32_t *segPointerToRawData = nullptr;
uint8_t *segCharacteristics = nullptr;
uint8_t *redirectSection = nullptr;
uint8_t *zoomCodeSection = nullptr;
float *dataZoomScaling = nullptr;
float *dataCull = nullptr;
// prepare data vector
if (state == loadedUnpatched){
// Increase / resize data vector
data.insert(data.end(), EDMWPatchedSize - EDMWUnpatchedSize, 0x0);
}
if (state == loadedPatched){
// Shrink / resize data vector
data.erase(data.begin()+EDMWUnpatchedSize, data.end());
}
// assign shared data pointers
imgSize = reinterpret_cast<uint32_t*>(data.data()+EDMWImageSizePos);
segSize = reinterpret_cast<uint16_t*>(data.data()+EDMWSegmentSizePos);
segName = reinterpret_cast<uint8_t*>(data.data()+EDMWSegmentNamePos);
segVirtualSize = reinterpret_cast<uint32_t*>(data.data()+EDMWVirtualSizePos);
segVirtualAddress = reinterpret_cast<uint32_t*>(data.data()+EDMWVirtualAddressPos);
segSizeOfRawData = reinterpret_cast<uint32_t*>(data.data()+EDMWSizeOfRawDataPos);
segPointerToRawData = reinterpret_cast<uint32_t*>(data.data()+EDMWPointerToRawDataPos);
segCharacteristics = reinterpret_cast<uint8_t*>(data.data()+EDMWCharacteristicsPos);
redirectSection = reinterpret_cast<uint8_t*>(data.data()+EDMWRedirectSectionPos);
// Reset rear cull factor
dataCull = reinterpret_cast<float*>(data.data()+EDMWCullPos);
*dataCull = patchedDefaultCull;
if (state == loadedUnpatched) {
// assign data pointers for patched setup
dataZoomScaling = reinterpret_cast<float*>(data.data()+EDMWPatchedZoomScalingPos);
zoomCodeSection = reinterpret_cast<uint8_t*>(data.data()+EDMWPatchedZoomCodeSectionPos);
// Patch header
*imgSize = EDMWPatchedImageSize;
*segSize = EDMWPatchedSegmentSize;
std::copy(std::begin(EDMWPatchedSegmentName), std::end(EDMWPatchedSegmentName), segName);
*segVirtualSize = EDMWPatchedVirtualSize;
*segVirtualAddress = EDMWPatchedVirtualAddress;
*segSizeOfRawData = EDMWPatchedSizeOfRawData;
*segPointerToRawData = EDMWPatchedPointerToRawData;
std::copy(std::begin(EDMWPatchedCharacteristics), std::end(EDMWPatchedCharacteristics), segCharacteristics);
// Modify render path
std::copy(std::begin(EDMWPatchedRedirectSection),std::end(EDMWPatchedRedirectSection), redirectSection);
// Append additional zoom scaling code
std::copy(std::begin(EDMWPatchedZoomCodeSection),std::end(EDMWPatchedZoomCodeSection), zoomCodeSection);
// Set default zoom / cull values
*dataZoomScaling = patchedDefaultZoomScaling;
*dataCull = patchedDefaultCull;
state = loadedPatched;
} else if (state == loadedPatched){
// Unpatch header
*imgSize = EDMWUnpatchedImageSize;;
*segSize = EDMWUnpatchedSegmentSize;
std::copy(std::begin(EDMWUnpatchedSegmentName), std::end(EDMWUnpatchedSegmentName), segName);
*segVirtualSize = EDMWUnpatchedVirtualSize;
*segVirtualAddress = EDMWUnpatchedVirtualAddress;
*segSizeOfRawData = EDMWUnpatchedSizeOfRawData;
*segPointerToRawData = EDMWUnpatchedPointerToRawData;
std::copy(std::begin(EDMWUnpatchedCharacteristics), std::end(EDMWUnpatchedCharacteristics), segCharacteristics);
// Restore render path
std::copy(std::begin(EDMWUnpatchedRedirectSection),std::end(EDMWUnpatchedRedirectSection), redirectSection);
// Reset rear cull factor
*dataCull = patchedDefaultCull;
state = loadedUnpatched;
}
updateByState();
}
void MainWindow::openFileClicked() {
QString filepath = QFileDialog::getOpenFileName();
std::cout << "Try to open file from: " << filepath.toStdString() << std::endl;
std::ifstream inputFile (filepath.toStdString(),std::ios::binary);
if(inputFile.good()){
// read file
std::vector<char> const file(
(std::istreambuf_iterator<char>(inputFile)),
(std::istreambuf_iterator<char>())
);
MainWindow::path = filepath;
MainWindow::data = std::vector<uint8_t>(file.begin(), file.end());
if (file.size() == EDMWUnpatchedSize) {
std::cout << "File is: Empires Dawn of the Modern World (unpatched)" << std::endl;
MainWindow::state = EDMWstate::loadedUnpatched;
} else if (file.size() == EDMWPatchedSize) {
std::cout << "File is: Empires Dawn of the Modern World (patched)" << std::endl;
MainWindow::state = EDMWstate::loadedPatched;
} else {
std::cout << "Unknown file with size " << std::to_string(file.size()) << std::endl;
printErrorMessage("Unknown file! A file of the following size is expected: 0x479000, 0x479FFF bytes");
state = EDMWstate::unloaded;
}
} else {
printErrorMessage("Error while reading the file");
state = EDMWstate::unloaded;
}
inputFile.close();
updateByState();
}
void MainWindow::saveClicked() {
float zoomValue = patchedDefaultZoomScaling;
float cullValue = patchedDefaultCull;
bool validZoom = true;
bool validCull = true;
if (state == loadedPatched) {
// check text fields
try {
zoomValue = std::stof(zoomEdit->text().toStdString());
} catch (...) {
validZoom = false;
}
try {
cullValue = std::stof(cullEdit->text().toStdString());
} catch (...) {
validCull = false;
}
}
if (validZoom && validCull) {
overwriteDataArray(zoomValue,cullValue);
// Write data array to file
writeFile();
} else {
QString err = "Invalid values.";
if (!validZoom)
err.append("\nZoom value is invalid");
if (!validCull)
err.append("\nCull value is invalid");
printErrorMessage(err);
}
updateByState();
}
void MainWindow::saveDefaultClicked(){
overwriteDataArray(patchedDefaultZoomScaling,patchedDefaultCull);
// Write data array to file
writeFile();
updateByState();
}
void MainWindow::overwriteDataArray(float zoom, float cull){
float *zoomPtr = nullptr;
float *cullPtr = nullptr;
// find float positions in std::vector data
cullPtr = reinterpret_cast<float*>(data.data()+EDMWCullPos);
*cullPtr = cull;
// Write zoom scaling only if data is patched!
if (state == loadedPatched) {
zoomPtr = reinterpret_cast<float*>(data.data()+EDMWPatchedZoomScalingPos);
*zoomPtr = zoom;
}
}
void MainWindow::writeFile(){
// try to write file
std::ofstream outFile (path.toStdString(),std::ios::out | std::ios::binary);
if (outFile.good()){
outFile.write(reinterpret_cast<char*>(data.data()),data.size());
outFile.flush();
printInfoMessage("File was successfully patched!");
} else {
printErrorMessage("Error during file writing.","You can try to run this program as run as administrator.");
}
outFile.close();
}
void MainWindow::printErrorMessage(QString msg,QString detailMsg){
QMessageBox errMsg;
errMsg.setIcon(QMessageBox::Warning);
if (detailMsg != nullptr)
errMsg.setInformativeText(detailMsg);
errMsg.setText(msg);
errMsg.exec();
}
void MainWindow::printInfoMessage(QString msg,QString detailMsg){
QMessageBox infoMsg;
infoMsg.setIcon(QMessageBox::Information);
if (detailMsg != nullptr)
infoMsg.setInformativeText(detailMsg);
infoMsg.setText(msg);
infoMsg.exec();
}
void MainWindow::closeEvent(QCloseEvent *event)
{
event->accept();
//event->ignore();
}