forked from tellomichmich/PokeNoxBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.py
364 lines (316 loc) · 11.3 KB
/
Utils.py
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
358
359
360
361
362
363
364
import json
import time
import io
import random
import sys
import os.path
import base64
import datetime
import PokemonIVCalculator
import math
import fileinput
import traceback
from PIL import Image, ImageOps, ImageDraw, ImageChops, ImageFilter
from math import ceil, radians, cos, sin, asin, sqrt
import re
import win32ui
import subprocess
#Rotate a python list
def rotate_list(l,n):
return l[-n:] + l[:-n]
def geo_distance(lat1, lon1, lat2, lon2):
lat1, lon1, lat2, lon2 = map(radians, [lat1, lon1, lat2, lon2])
dlat = lat2 - lat1
dlon = lon2 - lon1
a = sin(dlon/2)**2 + cos(lon1) * cos(lon2) * sin(dlat/2)**2
c = 2 * asin(sqrt(a))
r = 6371
return (c * r)*1000
def geo_walk_to(speed, lat1, lon1, alt1, lat2, lon2, alt2):
new_lat = lat1
new_lon = lon1
new_alt = alt1
travel_geo_points = []
dist = geo_distance(lat1, lon1, lat2, lon2)
steps = (float(dist))/(float(speed))
if steps >= 1:
dLat = (lat2 - lat1) / steps
dLon = (lon2 - lon1) / steps
dAlt = (alt2 - alt1) / steps
for i in range(0, int(steps)):
new_lat = lat1 + (dLat * (i+1))
new_lon = lon1 + (dLon * (i+1))
new_alt = alt1 + (dAlt * (i+1))
#print "%f, %f, %f" % (new_lat, new_lon, new_alt)
travel_geo_points.append([new_lat, new_lon, new_alt])
#else:
#print "%f, %f, %f" % (new_lat, new_lon, new_alt)
# travel_geo_points.append([new_lat, new_lon, new_alt])
return travel_geo_points
def geo_point_from_kml(kml_filename, speed):
geo_points = []
f = open(kml_filename)
for line in f:
if re.match("[-+]?[0-9]*\.?[0-9]+,[-+]?[0-9]*\.?[0-9]+,[-+]?[0-9]*\.?[0-9]+", line):
geo_point = line.strip().split(',')
geo_points.append([float(geo_point[0]), float(geo_point[1]), float(geo_point[2])])
f.close()
loop_geo_points = []
last_geo_point = None
for cur_geo_point in geo_points:
if not last_geo_point is None:
loop_geo_points = loop_geo_points + geo_walk_to(speed, last_geo_point[0], last_geo_point[1], last_geo_point[2], cur_geo_point[0], cur_geo_point[1], cur_geo_point[2])
last_geo_point = cur_geo_point
loop_geo_points = loop_geo_points + geo_walk_to(speed, last_geo_point[0], last_geo_point[1], last_geo_point[2], geo_points[0][0], geo_points[0][1], geo_points[0][2])
return loop_geo_points
def Tap(x, y):
Command = "bin\\adb.exe shell input tap %d %d" % (x, y)
os.system(Command)
def Swipe(x1, y1, x2, y2):
Command = "bin\\adb.exe shell input swipe %d %d %d %d " % (x1, y1, x2, y2)
os.system(Command)
def SwipeTime(x1, y1, x2, y2, t):
Command = "bin\\adb.exe shell input swipe %d %d %d %d %d" % (x1, y1, x2, y2, t)
os.system(Command)
def KeyEscap():
Command = "bin\\adb.exe shell input keyevent 4"
os.system(Command)
def TakePngScreenshot():
while True:
try:
Command = "bin\\adb.exe shell \"screencap -p | busybox base64\""
PngScreenshotData = os.popen(Command).read()
PngScreenshotData = base64.b64decode(PngScreenshotData)
break;
except KeyboardInterrupt:
sys.exit(0)
except:
print "[!] Failed to get screen"
return PngScreenshotData
def IsColorInCeil(ColorToCheck, RefColor, Ceil):
if (RefColor[0]+(255*Ceil)) > ColorToCheck[0] > (RefColor[0]-(255*Ceil)):
if (RefColor[1]+(255*Ceil)) > ColorToCheck[1] > (RefColor[1]-(255*Ceil)):
if (RefColor[2]+(255*Ceil)) > ColorToCheck[2] > (RefColor[2]-(255*Ceil)):
return True
return False
def RemoveColor(img, Color, Ceil):
pixdata = img.load()
for y in xrange(img.size[1]):
for x in xrange(img.size[0]):
if IsColorInCeil(pixdata[x, y], Color, Ceil):
pixdata[x, y] = (255, 255, 255)
def RemoveBlue(img, Limit):
pixdata = img.load()
for y in xrange(img.size[1]):
for x in xrange(img.size[0]):
if pixdata[x, y][2] > Limit:
pixdata[x, y] = (255, 255, 255)
def RemoveInSquare(img, x, y, xs, ys):
pixdata = img.load()
for yr in xrange(img.size[1]):
for xr in xrange(img.size[0]):
if (x+xs >= xr >= x) and (y+ys >= yr >= y):
pixdata[xr, yr] = (255, 255, 255)
def RemoveNotInSquare(img, x, y, xs, ys):
pixdata = img.load()
for yr in xrange(img.size[1]):
for xr in xrange(img.size[0]):
if not((x+xs >= xr >= x) and (y+ys >= yr >= y)):
pixdata[xr, yr] = (255, 255, 255)
def GetMeanColor(img, x, y, size=10):
MeanColor = [0, 0, 0]
pixdata = img.load()
for xr in range(x-(size/2), x+(size/2)):
for yr in range(y-(size/2), y+(size/2)):
MeanColor[0] = MeanColor[0] + pixdata[xr, yr][0]
MeanColor[1] = MeanColor[1] + pixdata[xr, yr][1]
MeanColor[2] = MeanColor[2] + pixdata[xr, yr][2]
MeanColor[0] = MeanColor[0]/(size**2)
MeanColor[1] = MeanColor[1]/(size**2)
MeanColor[2] = MeanColor[2]/(size**2)
return MeanColor
def GetImgFromScreenShot():
Screenshot = TakePngScreenshot()
img = Image.open(io.BytesIO(Screenshot))
img = img.convert("RGB")
return img
def HighContrast(img, Limit=126):
pixdata1 = img.load()
for xr in xrange(img.size[0]):
for yr in xrange(img.size[1]):
if pixdata1[xr, yr] >= Limit:
pixdata1[xr, yr] = 255
else:
pixdata1[xr, yr] = 0
def OnlyPureWhite(img1):
pixdata1 = img1.load()
output = img1.copy()
pixdataout = output.load()
for xr in xrange(img1.size[0]):
for yr in xrange(img1.size[1]):
if pixdata1[xr, yr] != (255, 255, 255):
pixdataout[xr, yr] = (255, 255, 255)
else:
pixdataout[xr, yr] = (0, 0, 0)
return output
def GetImgFromFile(File):
img = Image.open(File)
img = img.convert("RGB")
return img
#END OF UTILS
def BlackOrWhite(img):
BlackCount = 0.0
pixdata = img.load()
for xr in xrange(img.size[0]):
for yr in xrange(img.size[1]):
if pixdata[xr, yr] != (255, 255, 255):
pixdata[xr, yr] = (0, 0, 0)
BlackCount += 1
return (BlackCount/(img.size[0]*img.size[1]))*100
def RemoveColorList(img, ColorList):
pixdata = img.load()
for x in xrange(img.size[0]):
for y in xrange(img.size[1]):
if pixdata[x, y] in ColorList:
pixdata[x, y] = (255, 255, 255)
def random_lat_long_delta():
return ((random.random() * 0.00001) - 0.000005) * 3
def ImgToString(img, CharSet=None):
img.save("tmp\\ocr.png")
Command = "bin\\tesseract.exe --tessdata-dir bin\\tessdata tmp\\ocr.png tmp\\ocr "
if CharSet != None:
Command += "-c tessedit_char_whitelist="+CharSet+" "
Command += "-psm 7 "
Command += "> nul 2>&1"
#print Command
os.system(Command)
#TODO: Remove this, as we psm 7
#Get the largest line in txt
with open("tmp\\ocr.txt") as f:
content = f.read().splitlines()
OutputLine = ""
for line in content:
line = line.strip()
if len(line) > len(OutputLine):
OutputLine = line
return OutputLine
#Todo: Take a while...
def ZoomOut():
ZoomOutFix()
Command = "bin\\adb.exe push bin\\Zoomout.txt /sdcard/Zoomout.txt"
os.system(Command)
Command = "bin\\adb.exe shell sh /sdcard/Zoomout.txt"
os.system(Command)
#Check the correct input
def GetEvent():
Command = "bin\\adb.exe shell cat /proc/bus/input/devices >tmp\\inputs.log"
os.system(Command)
a = 'N: Name="Android Input"'
line_num = 0
x = 0
with open("tmp\\inputs.log") as f:
content = f.read().splitlines()
for line in content:
line_num += 1
if a in line:
x = line_num+4
elif line_num == x:
result = line.replace(line[:36], '')
return result
#Change the file whit the correct event number
def ZoomOutFix():
x = GetEvent()
array = []
array.append("#!/bin/sh")
with open("bin\\Zoomout.txt") as f:
content = f.read().splitlines()
for line in content:
if "event" in line:
line = re.sub(r"event[0-9]", x, line)
array.append(line)
f.close()
array.append("exit")
g = open("bin\\Zoomout.txt", 'w')
g.write("\n".join(array))
g.close()
#Check Nox process is running
def IsNoxRunning():
try:
if win32ui.FindWindow(None, "Nox App Player"):
return True
except:
return False
def StartNoxProcess(NoxPath):
try:
WARNING_LOG("Starting Nox...")
process = subprocess.Popen(NoxPath, shell=True, stdout=subprocess.PIPE)
except:
ERROR_LOG("The program can't run Nox")
def KillNoxProcess():
try:
if IsNoxRunning():
os.system("taskkill /im Nox.exe /f")
except:
ERROR_LOG("The program could not be killed")
#The process is closed if it has reached the maximum number of consecutive errors
TimeOutValue = 0
TimeOutValueMax = 20
def CallTimeOut():
if TimeOutValue == TimeOutValueMax:
KillNoxProcess()
TimeOutValue = 0
else:
TimeOutValue += 1
ERROR_LOG("TimeOutValue: "+str(TimeOutValue)+" of "+str(TimeOutValueMax))
#Reset the value if consecutive errors no longer occur
def ResetTimeOut():
TimeOutValue = 0
def GetTimeOutValue():
return TimeOutValue
def GetTimeOutValueMax():
return TimeOutValueMax
def LevenshteinDistance(first, second):
"""Find the Levenshtein distance between two strings."""
if len(first) > len(second):
first, second = second, first
if len(second) == 0:
return len(first)
first_length = len(first) + 1
second_length = len(second) + 1
distance_matrix = [[0] * second_length for x in range(first_length)]
for i in range(first_length):
distance_matrix[i][0] = i
for j in range(second_length):
distance_matrix[0][j]=j
for i in xrange(1, first_length):
for j in range(1, second_length):
deletion = distance_matrix[i-1][j] + 1
insertion = distance_matrix[i][j-1] + 1
substitution = distance_matrix[i-1][j-1]
if first[i-1] != second[j-1]:
substitution += 1
distance_matrix[i][j] = min(insertion, deletion, substitution)
return distance_matrix[first_length-1][second_length-1]
def ERROR_LOG(String):
print("\033[0;91m"+"[#] "+String+"\033[00m")
def WARNING_LOG(String):
print("\033[0;93m"+"[!] "+String+"\033[00m")
def COOL_LOG(String):
print("\033[0;92m"+"[*] "+String+"\033[00m")
def DEBUG_LOG(String):
print("\033[0;94m"+"[@] "+String+"\033[00m")
def INFO_LOG(String):
print("[+] "+String)
def WriteAppend(Filename, Content):
f = open(Filename, "a")
f.write(Content)
f.close()
def DiffImgPercent(img1, img2):
pixdata1 = img1.load()
pixdata2 = img2.load()
DiffCount = 0
for xr in xrange(img1.size[0]):
for yr in xrange(img1.size[1]):
if pixdata1[xr, yr] != pixdata2[xr, yr]:
DiffCount += 1
return (DiffCount*1.0) / (img1.size[0]*img1.size[1])