-
Notifications
You must be signed in to change notification settings - Fork 1
/
jumpcontroller.cpp
239 lines (218 loc) · 6.38 KB
/
jumpcontroller.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
#include "jumpcontroller.h"
#include <QString>
#include <QObject>
#include <QDebug>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <QTime>
#include <QtGlobal>
JumpController::JumpController()
{
getScreenshotProcess.setProcessChannelMode(QProcess::MergedChannels);
QObject::connect(&getScreenshotProcess,SIGNAL(finished(int)),
this,SLOT(getImageFromStdOutput()));
QObject::connect(&jumpProcess,SIGNAL(finished(int)),
this,SLOT(jumpActionFinishedEvent()));
}
bool JumpController::initializeAdbService(QString path)
{
adbFilePath = path;
return initialize();
}
bool JumpController::refreshAdbService(QString path)
{
return initializeAdbService(path);
}
bool JumpController::refreshAdbService()
{
if(isAdbServiceInitializated)
{
return initialize();
}
else
{
emit sendJumpControllerMessage(QString("Adb path is invalid!"));
return false;
}
}
bool JumpController::restartAdbService(QString path)
{
killAdbService(path);
return initializeAdbService(path);
}
bool JumpController::restartAdbService()
{
if(isAdbPathValid)
{
killAdbService(adbFilePath);
return initialize();
}
else
{
emit sendJumpControllerMessage(QString("Adb path is invalid!"));
return false;
}
}
void JumpController::startAutoJumpLoop()
{
if(isAdbPathValid && isAdbServiceInitializated && isDetectedDevice)
{
QObject::connect(&jumpProcess,SIGNAL(finished(int)),
this,SLOT(jumpActionFinishedLoopEvent()));
QObject::connect(&timerJumpInterval,SIGNAL(timeout()),
this,SLOT(timerJumpIntervalTimeoutEvent()));
getMatScreenshotImage();
}
else
{
sendJumpControllerMessage(QString("Adb error or no device!"));
}
}
void JumpController::stopAutoJumpLoop()
{
timerJumpInterval.stop();
if(jumpProcess.state() == QProcess::Running)
jumpProcess.terminate();
if(getScreenshotProcess.state() == QProcess::Running)
getScreenshotProcess.terminate();
QObject::disconnect(&jumpProcess,SIGNAL(finished(int)),
this,SLOT(jumpActionFinishedLoopEvent()));
QObject::disconnect(&timerJumpInterval,SIGNAL(timeout()),
this,SLOT(timerJumpIntervalTimeoutEvent()));
}
void JumpController::getMatScreenshotImage()
{
if(isAdbServiceInitializated && isDetectedDevice)
{
emit sendJumpControllerMessage(QString("Getting Screenshot..."));
cmd = adbFilePath + " shell screencap -p";
// it takes too much time
getScreenshotProcess.start(cmd);
}
}
void JumpController::setCheatMode(bool flag)
{
isCheatMode = flag;
}
bool JumpController::isAdbServiceInitializatedFlag() const
{
return isAdbServiceInitializated;
}
bool JumpController::isDetectedDeviceFlag() const
{
return isDetectedDevice;
}
bool JumpController::initialize()
{
adbProcess.start(adbFilePath + " devices");
adbProcess.waitForFinished();
adbProcess.start(adbFilePath + " devices");
if(!adbProcess.waitForFinished())
{
emit sendJumpControllerMessage(QString("Adb error!"));
isAdbServiceInitializated = false;
return false;
}
else
{
QString output =QString::fromLocal8Bit(adbProcess.readAllStandardOutput());
output = output.simplified();
if(output.startsWith("List of devices attached"))
{
isAdbPathValid = true;
isAdbServiceInitializated = true;
if(output.mid(25).isEmpty() || output.mid(25).isNull())
{
emit sendJumpControllerMessage(QString("No Devices!"));
isDetectedDevice = false;
}
else
{
emit sendJumpControllerMessage(QString("Find: " + output.mid(25)));
isDetectedDevice = true;
}
return true;
}
else
{
emit sendJumpControllerMessage(QString("Adb error!"));
isAdbPathValid = false;
isAdbServiceInitializated = false;
return false;
}
}
}
void JumpController::jumpAction(int pressTime)
{
if(pressTime > 0)
{
if(!isCheatMode)
cmd = adbFilePath + " shell input swipe 200 200 200 200 " +
QString::number(pressTime);
else
{
qsrand(QTime::currentTime().second());
int x,y;
x = 250+qrand()%100;
y = 1600+qrand()%100;
cmd = adbFilePath + " shell input swipe "+
QString::number(x)+" "+
QString::number(y)+" "+
QString::number(x)+" "+
QString::number(y)+" "+
QString::number(pressTime);
}
qDebug() << cmd;
jumpProcess.start(cmd);
emit sendJumpControllerMessage(QString("Jumpping..."));
}
else
emit sendJumpControllerMessage(QString("Jump Time invalid!"));
}
void JumpController::killAdbService(QString path)
{
adbProcess.start(path + "kill-server");
adbProcess.waitForFinished();
}
void JumpController::killAdbService()
{
if(isAdbPathValid)
{
adbProcess.start(adbFilePath + "kill-server");
adbProcess.waitForFinished();
emit sendJumpControllerMessage(QString("Kill adb service successful!"));
}
else
emit sendJumpControllerMessage(QString("Adb path is invalid!"));
}
void JumpController::getImageFromStdOutput()
{
QByteArray data;
data = getScreenshotProcess.readAll();
getScreenshotProcess.terminate();
data = data.replace("\r\r\n","\n");
// to Mat
std::vector<uchar> buffer(data.begin(),data.end());
matScreenshot = cv::imdecode(buffer,CV_LOAD_IMAGE_COLOR);
if(matScreenshot.data != NULL)
{
emit sendMatScreenshotImage(matScreenshot);
emit sendJumpControllerMessage(QString("Standby."));
}
}
void JumpController::jumpActionFinishedEvent()
{
emit sendJumpControllerMessage(QString("Standby."));
}
void JumpController::jumpActionFinishedLoopEvent()
{
timerJumpInterval.setInterval(jumpInterval);
timerJumpInterval.start();
emit sendJumpControllerMessage(QString("Wait for jump finished."));
}
void JumpController::timerJumpIntervalTimeoutEvent()
{
timerJumpInterval.stop();
getMatScreenshotImage();
}