-
Notifications
You must be signed in to change notification settings - Fork 2
/
lrffile.py
224 lines (189 loc) · 6.94 KB
/
lrffile.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
import os
import time
import win32gui
import tkFileDialog
import Tkinter
from capturer import client_capture
import pipes
import subprocess
import config
from itertools import islice
import re
import json
import zlib
import win32con
import gzip
import sys
from analyze import LOLGameData
from win32con import *
from win32api import *
from win32ui import *
from win32gui import GetClassName
from win32event import *
import sys
import capturer
import webbrowser
import win32com.client
# Captions (titles) of popup windows to confirm
PopupNames = (
'Close Exisitng Match?',
'Newer Version',
"Fix Exe?"
)
_called_directly = False
def GetWindowText( Window ):
"""
Get text of all 'Static' elements of windows and return
concatenated.
"""
Child, Text = None, ''
while 1:
try: Child = FindWindowEx( Window, Child, 'Static', None )
except: break
Text += '\n\t'.join( Child.GetWindowText().split( '\r' ) )
return Text
def FindControl( Window, CName = 'OK', CType = 'Button' ):
"""
Find control with name CName in Window
@arg Window: Top level window
@type Window: PyCWnd
@arg CName: Control Name
@type CName: string
@arg CType: Control class
@type CType: string
@return Control
@rtype: PyCwnd
"""
return FindWindowEx( Window, None, CType, CName )
def getLRFMetadata(fileh):
head=list(islice(fileh,1))
jsonstr = re.search("(\{.*\})", str(head))
if(not jsonstr):
return False
else:
jsonstr = jsonstr.group(1)
data = json.loads(jsonstr)
return data
def flushPrint(str):
print str
sys.stdout.flush()
def analyseLRFFile(filename = None, savefile = None):
window_title = config.LOL_WINDOW_TITLE
toplist, winlist, childlist = [], [], []
def enum_cb(hwnd, results):
winlist.append((hwnd, win32gui.GetWindowText(hwnd)))
def find_windows_with_name(window_title):
win32gui.EnumWindows(enum_cb, toplist)
return [(hwnd, title) for hwnd, title in winlist if window_title in title.lower()]
if(len(find_windows_with_name(window_title)) == 0):
if(filename == None):
filename = promptOpenFile()
if(not filename):
print "Aborting."
return
lrfmeta = getLRFMetadata(open(filename))
if(not lrfmeta):
print "Could not read LRF file"
return False
if(lrfmeta['clientVersion'] not in config.LOL_VALID_CLIENT_VERSIONS):
print "Invalid client version of replay. Replay client version is "+lrfmeta['clientVersion']+". Acceptable versions: "+", ".join(config.LOL_VALID_CLIENT_VERSIONS)
return False
subprocess.Popen([config.LOLREPLAY_PATH, filename])
alert_box_count = 0
while(1):
for name in PopupNames:
try: window = FindWindow(None, name)
except: continue
# Extract text information from Popup (for logging).
message = GetWindowText(window) # Get message text before window disappears
for i in range(0, 5):
window.SetFocus()
time.sleep(0.1)
shell = win32com.client.Dispatch("WScript.Shell")
shell.AppActivate(name)
if(name == "Newer Version"):
print "Newer version dialog box. Clicking 'No'. "
capturer.sendkey(39, 0.5)
time.sleep(1)
capturer.sendkey(13, 0.5)
alert_box_count += 1
elif(name == "Fix Exe?"):
print "Fix exe dialog box detected. Clicking 'No'."
capturer.sendkey(39, 0.5)
time.sleep(1)
capturer.sendkey(13, 0.5)
alert_box_count += 1
elif(name == 'Close Exisitng Match?'):
print "Close Exisitng Match dialog box"
time.sleep(1)
capturer.sendkey(13, 0.5)
alert_box_count += 1
if(alert_box_count > 10):
print "Error: Could not focus message box window."
return False
if(len(find_windows_with_name(window_title)) == 0):
time.sleep(1)
continue
else:
break
flushPrint("League of Legends client window detected. Waiting 10 seconds for loading screen to appear...")
time.sleep(10)
else:
lrfmeta = {}
if(not filename):
filename = "tmp.lrf"
print "Analysis will be saved to output/"+os.path.splitext(os.path.basename(filename))[0]+".lra"
flushPrint("Beginning client capture.")
output = client_capture(lrfmeta)
if(output == False):
flushPrint("Client capture failed.")
return False
flushPrint("Client capture completed.")
hwnd = find_windows_with_name(window_title)[0][0]
# Close the LOL Client Window
win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)
if(not savefile):
savefile = "output/"+os.path.splitext(os.path.basename(filename))[0]+".lra"
output['lra_version'] = "0.1"
output['lrf_meta'] = lrfmeta
if(savefile):
savefileh = gzip.open(savefile, "wb")
jsonstring = json.dumps(output)
savefileh.write(jsonstring)
savefileh = open(savefile+".txt", "w")
print >> savefileh, jsonstring
savefileh.close()
data = LOLGameData(savefile)
print str(len(data.data['history'])) + " data points loaded."
flushPrint("Generating analysis files...")
data.generateAnalysisFile()
# Save html file
basename = (".".join(savefile.split(".")[0:-1])).split("/")[-1]
fh = open("analysis/analysis.html", "rb")
analysis_raw = fh.read()
jsonh = open("output/"+basename+".json", "rb");
new_html = analysis_raw.replace("<!-- DATA_555 -->", "<script type='text/javascript'>_data_defined = true; data="+jsonh.read()+"</script>");
jsonh.close()
fh.close()
analysis_fh = open("analysis/"+basename+".html", "wb")
analysis_fh.write(new_html)
analysis_fh.close()
flushPrint("Analysis complete. View analysis page at "+os.path.dirname(os.path.realpath(__file__)) + "/analysis/"+basename+".html")
flushPrint("Done.")
if(_called_directly == True):
webbrowser.open(os.path.dirname(os.path.realpath(__file__)) + "/analysis/"+basename+".html",new=2)
return True
def promptOpenFile():
root = Tkinter.Tk()
root.withdraw()
file_path = tkFileDialog.askopenfilename()
print "Opening "+file_path
return file_path
if __name__ == "__main__":
_called_directly = True
if(len(sys.argv) == 2):
analyseLRFFile(sys.argv[1])
elif(len(sys.argv) > 2):
analyseLRFFile(sys.argv[1], sys.argv[2])
else:
analyseLRFFile()