-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.cpp
139 lines (115 loc) · 4 KB
/
main.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
/*
Copyright (C) 2019 Tasos Sahanidis <[email protected]>
This program 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 2 of the License, or
(at your option) any later version.
This program 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, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <QCoreApplication>
#include <QResource>
#include <QDir>
#include <QDebug>
#include <QTextStream>
#if QT_VERSION < QT_VERSION_CHECK(5, 5, 0)
#define qInfo qDebug
#endif
QTextStream cout(stdout);
void copyFiles(QString dest, QTextStream &qrc, const QString &source = ":/mnt")
{
if(!dest.endsWith('/'))
dest.append('/');
QDir dir(source);
QFileInfoList list = dir.entryInfoList();
int listcount = list.count();
for(int i = 0; i < listcount; i++)
{
QFileInfo file = list.at(i);
// Strip out ":/mnt"
cout << file.absoluteFilePath().midRef(5) << endl;
QString destFileName = dest + file.fileName();
if(file.isDir())
{
{
QDir targetDir(dest);
targetDir.mkpath(file.fileName());
}
copyFiles(dest + file.fileName(), qrc, file.absoluteFilePath());
}
else
{
qrc << "<file>" << file.absoluteFilePath().midRef(6) << "</file>" << endl;
if(!QFile::copy(file.absoluteFilePath(), destFileName))
{
qInfo() << "Could not write" << destFileName;
continue;
}
// We need to set the permissions (644) as they are read only by default
if(!QFile::setPermissions(destFileName, QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ReadGroup | QFileDevice::ReadOther))
qInfo() << "Couldn't set permissions for" << destFileName;
}
}
}
int main(int argc, char *argv[])
{
if(argc < 2)
{
qInfo() << "Usage: ./qresExtract <resource.bin> [output path]";
return 1;
}
QString fileStr = QString::fromLocal8Bit(argv[1]);
QString fileName;
{
QFile file(fileStr);
if(!file.exists())
{
qInfo() << "No such file exists.";
return 1;
}
fileName = QFileInfo(file).baseName();
if(!QResource::registerResource(fileStr, "/mnt"))
{
qInfo() << "Could not load" << fileStr;
qInfo() << "This may be because your version of Qt doesn't support this qres version";
if(file.open(QIODevice::ReadOnly))
{
file.seek(0x7);
uchar ver = file.read(1)[0];
qInfo() << "qres version of this file is" << ver;
if(ver == 3)
qInfo() << "Version 3 requires at least Qt 5.13. You are using Qt" << QT_VERSION_STR;
}
return 1;
}
}
QString outPath(".");
if(argc > 2)
outPath = QString::fromLocal8Bit(argv[2]);
if(!outPath.endsWith('/'))
outPath.append('/');
outPath.append(fileName);
// Needed so that we can open the qrc
{
QDir().mkpath(outPath);
}
QFile qrcFile(outPath + "/" + fileName + ".qrc");
if(!qrcFile.open(QIODevice::WriteOnly))
{
qInfo() << "Unable to open" << qrcFile.fileName();
return 1;
}
QTextStream qrc(&qrcFile);
qrc << "<!DOCTYPE RCC><RCC version=\"1.0\">" << endl;
qrc << "<qresource>" << endl;
copyFiles(outPath, qrc);
qrc << "</qresource>" << endl;
qrc << "</RCC>" << endl;
qrcFile.close();
return 0;
}