-
Notifications
You must be signed in to change notification settings - Fork 0
/
hoifileparser.cpp
97 lines (90 loc) · 3.39 KB
/
hoifileparser.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
#include "hoifileparser.h"
#include "datetimeformat.h"
QRegularExpression HoiFileParser::pattern = QRegularExpression(
"\\s*player=\"(.*)\""
"\\s*ideology=(.*)"
"\\s*date=\"(.*)\""
"\\s*difficulty=\"(.*)\""
"\\s*version=\"(.*)\""
"\\s*ironman=\"(.*)\"\\s*");
QProcess* HoiFileParser::save(HoiSaveNode *saveNode)
{
QString srcPath = saveNode->srcPath();
QString dstPath = saveNode->filePath();
qDebug() << srcPath;
if(QFileInfo::exists(dstPath)) {
return nullptr;
}
QDir saveFolder = QFileInfo(dstPath).dir();
if(!saveFolder.exists()) {
qDebug() << "mkdir :" << saveFolder.mkpath(saveFolder.path());
}
QString copyPath = HoiFileParser::baseDir(srcPath) + "/" + QFileInfo(srcPath).fileName();
qDebug() << copyPath;
if (!QFileInfo::exists(copyPath)) {
return nullptr;
}
QProcess* process = new QProcess(QCoreApplication::instance());
process->startCommand(QString("%1/7z/7za.exe a -tzip \"%2\" -ir!\"%3\"").arg(
QCoreApplication::applicationDirPath(),
dstPath, copyPath
));
return process;
// QObject::connect(process, &QProcess::finished, process, [p = process]() {
// qDebug() << p->readAllStandardOutput();
// });
}
QProcess* HoiFileParser::restore(HoiSaveNode *saveNode)
{
QString srcPath = saveNode->srcPath();
QString dstPath = saveNode->filePath();
QProcess *process = new QProcess(QCoreApplication::instance());
QString cmd = QString("%1/7z/7za.exe e \"%2\" -o\"%3\" -y").arg(
QCoreApplication::applicationDirPath(),
dstPath, QFileInfo(srcPath).dir().path()
);
qInfo() << "run command:" << cmd;
process->startCommand(cmd);
return process;
}
HoiSaveNode* HoiFileParser::parse(const QString &filePath)
{
HoiSaveNode *savenode = new HoiSaveNode();
savenode->setSrcPath(filePath);
QString copyPath = HoiFileParser::baseDir(filePath) + "/" + QFileInfo(filePath).fileName();
if(QFileInfo::exists(copyPath)) {
// 不能直接覆盖文件,真滴坑
QFile::remove(copyPath);
}
qInfo() << "copy file to copy path:" << QFile::copy(filePath, copyPath);
QFile copyfile(copyPath);
copyfile.open(QFile::OpenModeFlag::ReadOnly);
QByteArray content = copyfile.read(512);
copyfile.close();
QRegularExpressionMatch match = pattern.match(QString(content));
if(match.hasMatch()) {
savenode->setPlayer(match.captured(1));
savenode->setIdeology(match.captured(2));
savenode->setDate(match.captured(3));
savenode->setDifficulty(match.captured(4));
savenode->setVersion(match.captured(5));
savenode->setIronman(match.captured(6));
}
savenode->setFilePath(
QString("%1/%2_%3.zip").arg(
HoiFileParser::baseDir(filePath),
savenode->date(),
savenode->updateTime().toString(DATETIME_NORMAL_FORMAT)
));
return savenode;
}
QString HoiFileParser::baseDir(const QString &path)
{
return QString("%1/save_games/%2").arg(
QCoreApplication::applicationDirPath(),
QFileInfo(path).fileName()
);
}
HoiFileParser::HoiFileParser()
{
}