-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelogger.py
executable file
·318 lines (246 loc) · 7.7 KB
/
relogger.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
#!/usr/bin/env python3.5
# relogger.py: automatically log into EverQuest
import conf
import cv2
import tempfile
import os
import subprocess
import time
import re
import shlex
import conf
import os.path
import logging
_LOGGER = logging.getLogger("relogger")
WAIT_DURATION = 60
EULA_BUTTON = cv2.imread("snips/eulabutton.png")
LOGO = cv2.imread("snips/logo.png")
LOGIN_BUTTON = cv2.imread("snips/login_button.png")
LOGIN_BUTTON2 = cv2.imread("snips/login_button2.png")
USERNAME_LABEL = cv2.imread("snips/username.png")
R99SERVER1 = cv2.imread("snips/r99server1.png")
R99SERVER2 = cv2.imread("snips/r99server2.png")
PLAYEQ_BUTTON = cv2.imread("snips/playeq.png")
PLAYEQ_BUTTON2 = cv2.imread("snips/playeq2.png")
ENTER_WORLD_BUTTON = cv2.imread("snips/enterworld.png")
ENTER_WORLD_BUTTON2 = cv2.imread("snips/enterworld2.png")
DONE_LOADING = cv2.imread("snips/doneloading.png")
def _w(arr):
"""Width of an array"""
return arr.shape[1]
def _h(arr):
"""Height of an array"""
return arr.shape[0]
def _capture():
"""Grab a screenshot of EverQuest"""
fixup()
tf = tempfile.mktemp(".png")
try:
cp = subprocess.run(["/usr/bin/import","-window", "EverQuest", tf], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
if cp.returncode != 0 or b"unavailable" in cp.stdout:
raise StateError("Couldn't capture screenshot")
return cv2.imread(tf)
finally:
os.unlink(tf)
def capture():
for i in range(WAIT_DURATION):
img = _capture()
if len(img.shape) == 3 and img.shape[0] >= 600 and img.shape[1] >= 600 and img.dtype == EULA_BUTTON.dtype:
return img
time.sleep(1)
else:
raise StateError("Couldn't capture screenshot")
def start_eq():
"""Start EverQuest"""
pd = os.getcwd()
try:
os.chdir(conf.EQ_DIR)
return subprocess.Popen(["/usr/bin/wine", "./eqgame.exe", "patchme"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
finally:
os.chdir(pd)
class CommandError(Exception):
pass
class StateError(Exception):
pass
def xdo(cmd):
cp = subprocess.run(["/usr/bin/xdotool"]+shlex.split(cmd), stdout=subprocess.PIPE)
if cp.returncode != 0:
raise CommandError("Failed run of xdotool")
else:
return str(cp.stdout, "utf8")
_WINDLOC_RE = re.compile(r"\s*Position: (-?[0-9]+),(-?[0-9]+).*")
_GEOMETRY_RE = re.compile(r"\s*Geometry: ([0-9]+)x([0-9]+).*")
def _geometry():
"""Get the EQ window location"""
lines = xdo("search --name EverQuest getwindowgeometry").splitlines()
loc = None
size = None
for line in lines:
m = _WINDLOC_RE.match(line)
if m:
loc = int(m.group(1)), int(m.group(2))
m = _GEOMETRY_RE.match(line)
if m:
size = int(m.group(1)), int(m.group(2))
if loc is None or size is None:
raise CommandError("Couldn't find EverQuest window: %r" % lines)
return loc, size
def geometry():
for i in range(10):
try:
return _geometry()
except:
_LOGGER.debug("Failed to retrieve geometry", exc_info=True)
time.sleep(1)
else:
raise CommandError("Couldn't find EverQuest window")
def click(x, y, button=1):
"""Click the mouse in the EQ window"""
loc, size = geometry()
xdo("mousemove %d %d click %d" % (x + loc[0], y + loc[1], button))
def clickslow(x,y, button=1):
loc, size = geometry()
xdo("mousemove %d %d mousedown %d" % (x + loc[0], y+loc[1], button))
time.sleep(0.1)
xdo("mouseup %d" % button)
def key(text, repeat=1):
xdo("key --delay=75 --repeat=%d %s" %(repeat, shlex.quote(text)))
def type(text):
xdo("type --delay=200 "+shlex.quote(text))
def fixup():
loc, size = geometry()
if loc[0] < 0 or loc[1] <0:
xdo("search --name EverQuest windowmove 100 100")
def prepare():
fixup()
click(10, 10)
xdo("search --name EverQuest windowmap windowraise windowfocus")
time.sleep(0.2)
def find_in(haystack, needle):
"""Find the location of a snip within a screenshot"""
if _w(needle) > _w(haystack) or _h(needle) > _h(haystack):
return None, None
res = cv2.matchTemplate(haystack, needle, cv2.TM_CCOEFF_NORMED)
minval, maxval, minloc, maxloc = cv2.minMaxLoc(res)
if maxval < 0.8:
return None, None
else:
return (maxloc[0]+_w(needle)//2, maxloc[1]+_h(needle)//2)
def wait_for(*imgs):
for i in range(WAIT_DURATION):
prepare()
for img in imgs:
x, y = find_in(capture(), img)
if x is not None:
return x, y
else:
time.sleep(1)
continue
break
else:
raise StateError("Got into bad state")
def click_on(*imgs, slow=False):
"""Find a button matching the given image and click on it"""
wait_for(*imgs)
for i in range(WAIT_DURATION):
prepare()
for img in imgs:
x, y = find_in(capture(), img)
if x is not None:
break
else:
break
if slow:
clickslow(x,y)
else:
click(x, y)
time.sleep(1)
else:
raise StateError("Got into bad state")
def pass_logo_screen():
"""Click past the logo screen"""
while True:
prepare()
x, y = find_in(capture(), LOGO)
if x is None:
break
click(100, 100)
time.sleep(1)
def enter_login():
prepare()
x, y = wait_for(USERNAME_LABEL)
prepare()
for i in range(10):
clickslow(x, y+25)
time.sleep(0.25)
time.sleep(0.1)
key("BackSpace", 16)
key("Delete", 16)
time.sleep(0.1)
type(conf.ACCOUNT)
time.sleep(0.1)
key("Tab")
time.sleep(0.1)
type(conf.PASSWORD)
time.sleep(0.1)
key("Return")
def select_server():
prepare()
x, y = wait_for(R99SERVER1, R99SERVER2)
click(x, y)
wait_for(R99SERVER1)
click_on(PLAYEQ_BUTTON, PLAYEQ_BUTTON2)
def launch_eq():
_LOGGER.info("Starting EQ")
p = start_eq()
try:
for i in range(WAIT_DURATION):
try:
prepare()
capture()
break
except:
_LOGGER.debug("EQ isn't yet started", exc_info=True)
time.sleep(1)
else:
raise StateError("EQ never started")
_LOGGER.info("Clicking EULA button")
click_on(EULA_BUTTON)
_LOGGER.info("Passing logo screen")
pass_logo_screen()
_LOGGER.info("Clicking login button")
click_on(LOGIN_BUTTON, LOGIN_BUTTON2)
_LOGGER.info("Entering username/password")
enter_login()
_LOGGER.info("Selecting server")
select_server()
_LOGGER.info("Waiting for character select")
wait_for(ENTER_WORLD_BUTTON, ENTER_WORLD_BUTTON2)
time.sleep(10)
_LOGGER.info("Entering world")
click_on(ENTER_WORLD_BUTTON, ENTER_WORLD_BUTTON2, slow=True)
time.sleep(10)
_LOGGER.info("Waiting for world to load")
wait_for(DONE_LOADING)
return p
except:
_LOGGER.fatal("Trouble, aborting login attempt", exc_info=True)
raise
def launch_bot():
_LOGGER.info("Starting bot")
os.system("./botmain.py")
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO, format="[%(asctime)s] %(levelname)s | %(name)s | %(msg)s")
while True:
try:
os.system("killall eqgame.exe")
time.sleep(5)
p = launch_eq()
try:
launch_bot()
finally:
p.kill()
except:
_LOGGER.error("Session ended, starting over in 10 seconds", exc_info=True)
time.sleep(10)
continue