Skip to content

Commit 4cf76bc

Browse files
committed
apkmetainfo2json
1 parent c81e9c6 commit 4cf76bc

File tree

10 files changed

+233
-1
lines changed

10 files changed

+233
-1
lines changed
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
QT -= gui
2+
3+
CONFIG += c++11 console
4+
CONFIG -= app_bundle
5+
6+
# You can make your code fail to compile if it uses deprecated APIs.
7+
# In order to do so, uncomment the following line.
8+
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
9+
10+
SOURCES += \
11+
apkmetainfo.cpp \
12+
main.cpp
13+
14+
# Default rules for deployment.
15+
qnx: target.path = /tmp/$${TARGET}/bin
16+
else: unix:!android: target.path = /opt/$${TARGET}/bin
17+
!isEmpty(target.path): INSTALLS += target
18+
19+
HEADERS += \
20+
apkmetainfo.h

Qt_ApkMetaInfo2Json/apkmetainfo.cpp

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
#include "apkmetainfo.h"
2+
3+
#include <QDebug>
4+
#include <QJsonArray>
5+
#include <QTextCodec>
6+
7+
/////////////////////////////////////////////////////////
8+
/// run exe
9+
ApkMetaInfo::ApkMetaInfo(QObject *parent) : QObject(parent)
10+
{
11+
connect(&m_exe, &QProcess::readyReadStandardOutput, this,
12+
[=]() { m_output.append(m_exe.readAllStandardOutput()); });
13+
connect(&m_exe, &QProcess::errorOccurred, this,
14+
[=]() { emit sigFinished({}); });
15+
connect(&m_exe,
16+
QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this,
17+
[=]() {
18+
m_output.append(m_exe.readAllStandardOutput());
19+
20+
if (QTextCodec *codec = QTextCodec::codecForName(m_output)) {
21+
parse(codec->toUnicode(m_output));
22+
}
23+
else {
24+
parse(m_output);
25+
}
26+
});
27+
}
28+
29+
void ApkMetaInfo::run(const QString &p)
30+
{
31+
m_exe.start("aapt.exe", QStringList{"dump", "badging", p});
32+
if (!m_exe.waitForStarted()) {
33+
qDebug() << m_exe.errorString();
34+
m_exe.kill();
35+
emit sigFinished({});
36+
}
37+
}
38+
39+
//////////////////////////////////////////////////////////////////////
40+
/// parse data
41+
void ApkMetaInfo::parseLine(const QString &pre,
42+
const QString &str,
43+
QJsonObject &obj)
44+
{
45+
// str
46+
// "package: name='com.github.kr328.clash' versionCode='203022'
47+
// versionName='2.3.22' compileSdkVersion='30'
48+
// compileSdkVersionCodename='11'"
49+
50+
const auto parts
51+
= QString(str).remove(pre + ":").split(" ", Qt::SkipEmptyParts);
52+
for (const auto &j : parts) {
53+
auto pair = j.split("=");
54+
if (pair.size() == 2) {
55+
obj[pair[0]] = pair[1].remove("'");
56+
}
57+
else {
58+
qDebug() << "err" << pair << str;
59+
}
60+
}
61+
}
62+
63+
void ApkMetaInfo::parseLine(const QString &pre,
64+
const QString &str,
65+
QJsonArray &array)
66+
{
67+
const auto parts
68+
= QString(str).remove(pre + ":").split(" ", Qt::SkipEmptyParts);
69+
for (const auto &j : parts) {
70+
auto pair = j.split("=");
71+
if (pair.size() == 2) {
72+
array.append(pair[1].remove("'"));
73+
}
74+
else {
75+
qDebug() << "err" << pair << str;
76+
}
77+
}
78+
}
79+
80+
void ApkMetaInfo::parseLineShort(const QString & /*pre*/,
81+
const QString &str,
82+
QJsonObject &obj)
83+
{
84+
// str
85+
// ""application-icon-160:'r/n_.xml'"
86+
const auto parts = str.split(":", Qt::SkipEmptyParts);
87+
if (parts.size() == 2) {
88+
obj[parts.value(0)] = parts.value(1).remove("'").trimmed();
89+
}
90+
else {
91+
qDebug() << "err" << parts;
92+
}
93+
}
94+
95+
void ApkMetaInfo::parse(const QString &data)
96+
{
97+
const QString pre_package = "package";
98+
const QString pre_sdk_v = "sdkVersion";
99+
const QString pre_sdk_t_v = "targetSdkVersion";
100+
const QString pre_up = "uses-permission";
101+
const QString pre_il = "install-location";
102+
const QString pre_al = "application-label";
103+
const QString pre_nc = "native-code";
104+
105+
QJsonObject obj_package;
106+
QJsonArray obj_up;
107+
QJsonArray obj_other;
108+
QString line;
109+
110+
const auto list = data.split('\n', Qt::SkipEmptyParts);
111+
for (const auto &i : list) {
112+
line = i.trimmed();
113+
114+
if (line.startsWith(pre_package)) {
115+
parseLine(pre_package, line, obj_package);
116+
}
117+
else if (line.startsWith(pre_sdk_v)) {
118+
parseLineShort(pre_sdk_v, line, obj_package);
119+
}
120+
else if (line.startsWith(pre_sdk_t_v)) {
121+
parseLineShort(pre_sdk_t_v, line, obj_package);
122+
}
123+
else if (line.startsWith(pre_il)) {
124+
parseLineShort(pre_il, line, obj_package);
125+
}
126+
else if (line.startsWith(pre_al + "-") || line.startsWith(pre_al)) {
127+
if (line.startsWith(pre_al + "-")) {
128+
continue;
129+
}
130+
parseLineShort(pre_al, line, obj_package);
131+
}
132+
else if (line.startsWith(pre_nc)) {
133+
parseLineShort(pre_nc, line, obj_package);
134+
}
135+
else if (line.startsWith(pre_up)) {
136+
parseLine(pre_up, line, obj_up);
137+
}
138+
else {
139+
obj_other.append(line);
140+
}
141+
}
142+
143+
QJsonObject obj_root;
144+
obj_root["Package"] = obj_package;
145+
obj_root["Uses Permission"] = obj_up;
146+
// should label it any character after "U"
147+
obj_root["X"] = obj_other;
148+
emit sigFinished(obj_root);
149+
}

Qt_ApkMetaInfo2Json/apkmetainfo.h

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef APKMETAINFO_H
2+
#define APKMETAINFO_H
3+
4+
#include <QJsonObject>
5+
#include <QObject>
6+
#include <QProcess>
7+
8+
class ApkMetaInfo : public QObject {
9+
Q_OBJECT
10+
public:
11+
explicit ApkMetaInfo(QObject *parent = nullptr);
12+
13+
void run(const QString &p);
14+
Q_SIGNAL void sigFinished(const QJsonObject &s);
15+
16+
private:
17+
void parse(const QString &data);
18+
void parseLineShort(const QString &, const QString &str, QJsonObject &obj);
19+
void parseLine(const QString &pre, const QString &str, QJsonArray &array);
20+
void parseLine(const QString &pre, const QString &str, QJsonObject &obj);
21+
22+
QByteArray m_output;
23+
QProcess m_exe;
24+
};
25+
26+
#endif // APKMETAINFO_H

Qt_ApkMetaInfo2Json/main.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <QCoreApplication>
2+
#include <QFile>
3+
#include <QFileInfo>
4+
#include <QJsonDocument>
5+
6+
#include "apkmetainfo.h"
7+
8+
int main(int argc, char* argv[])
9+
{
10+
QCoreApplication a(argc, argv);
11+
QStringList arguments = a.arguments();
12+
if (arguments.length() != 3) {
13+
return -1;
14+
}
15+
16+
ApkMetaInfo apk;
17+
apk.connect(&apk, &ApkMetaInfo::sigFinished, [&](const QJsonObject& obj) {
18+
if (obj.isEmpty()) {
19+
a.quit();
20+
return;
21+
}
22+
QJsonDocument doc(obj);
23+
24+
QString output = arguments[2];
25+
if (QFileInfo(output).suffix().isEmpty()) {
26+
output.append(".json");
27+
}
28+
QFile fo(output);
29+
fo.open(fo.WriteOnly | fo.Text | fo.Truncate);
30+
fo.write(doc.toJson());
31+
fo.close();
32+
a.quit();
33+
});
34+
apk.run(arguments[1]);
35+
36+
return a.exec();
37+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

Qt Torrent2Json/main.cpp renamed to Qt_Torrent2Json/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ int main(int argc, char* argv[])
3939
return -1;
4040
}
4141

42-
QJsonObject root_obj;
4342
QFile f(arguments[1]);
4443
if (!f.open(f.ReadOnly)) {
4544
qDebug() << f.errorString();
@@ -53,6 +52,7 @@ int main(int argc, char* argv[])
5352
qDebug() << info.errorString();
5453
return -1;
5554
}
55+
QJsonObject root_obj;
5656
root_obj["file"] = f.fileName();
5757
addStr(root_obj, "announceUrl", info.announceUrl());
5858
qDebug() << "announceList" << info.announceList();
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)