-
Notifications
You must be signed in to change notification settings - Fork 19
/
UnrealDebuggerIntegration.py
399 lines (353 loc) · 18.9 KB
/
UnrealDebuggerIntegration.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#------------------------------------------------------------------------------
# UnrealScriptIDE UnrealDebugger integration
#------------------------------------------------------------------------------
#
# -installs the debugger automatically when you build with a debug configuration. Preserves other installed debugger.
# -adds a command to uninstall the debugger and restore your old debugger.
# -set breakpoint inside Sublime:
#
# (TODO):
# sync opened files
# sync breakpoints: current_compile_settings
#
# (c) Florian Zinggeler
#------------------------------------------------------------------------------
import sublime
import sublime_plugin
import os
import threading
import shutil
from xml.etree import ElementTree
def get_paths(open_folder_arr, b_64bit=False):
# search open folders for the Src directory
udk_exe_path = ""
for folder in open_folder_arr:
if "\Development\Src" in folder:
udk_exe_path = folder
break
if udk_exe_path == "":
raise Exception("Src folder not found! Add the Src folder as a project.")
# Removing "Development\Src" (this is probably not how it's done correctly):
udk_path = udk_exe_path[:-15]
if b_64bit:
install_dir = udk_path + "Binaries\\Win64\\"
source_dir = sublime.packages_path() + "\\UnrealScriptIDE\\Debugger\\UnrealDebugger 64 bits"
else:
install_dir = udk_path + "Binaries\\Win32\\"
source_dir = sublime.packages_path() + "\\UnrealScriptIDE\\Debugger\\UnrealDebugger 32 bits"
return [install_dir, source_dir]
class UnrealManageBreakpointsCommand(sublime_plugin.TextCommand):
"""
Opens a dialog to manage all breakpoints.
"""
def run(self, edit):
open_folder_arr = self.view.window().folders()
install_dir_64, src_dir = get_paths(open_folder_arr, True)
self.breakpoints_xml = install_dir_64[:-6] + "UScriptIDE_Breakpoints.xml"
if os.path.exists(self.breakpoints_xml):
self.xml_tree = ElementTree.parse(self.breakpoints_xml)
self.breakpoints = self.xml_tree.find("Breakpoints")
points = self.get_breakpoints(self.breakpoints)
self.display_points(points)
def get_breakpoints(self, breakpoints):
dic = breakpoints.find("Dictionary")
points, self.points = [], []
for item in dic:
k = item.find("key")
s = k.find("string")
points.append([s.text.title(), " |"])
current_file = s.text.title().replace(".", "\\Classes\\") + ".uc"
self.points.append(current_file + ":1:0")
v = item.find("value")
arr = v.find("ArrayOfBreakpoint")
for b in arr:
l = b.find("LineNo").text
e = b.find("Enabled").text
points.append((" [o]" if e == "true" else " [x]") + "\tLine number " + l)
self.points.append(current_file + ":" + l + ":0")
if len(arr) == 0:
points.pop()
self.points.pop()
return points
def display_points(self, points):
points = [["Deactivate all Breakpoints"], ["Activate all Breakpoints"],
["Remove all Breakpoints", "______________________________________________________________________"]] + points
self.view.window().show_quick_panel(points, self.on_click_point)
def on_click_point(self, num):
if num == -1:
pass
elif num == 0:
# deactivate all
d = self.breakpoints.find('Dictionary')
for item in d:
for p in item[1][0]:
p[2].text, p[3].text = "false", "false"
self.xml_tree.write(self.breakpoints_xml)
for view in sublime.active_window().views():
active = view.get_regions("breakpoint_enabled")
deactivated = view.get_regions("breakpoint_deactivated")
view.add_regions("breakpoint_deactivated", active + deactivated, "breakpoint", "dirty_circle_light", sublime.HIDDEN | sublime.PERSISTENT)
view.add_regions("breakpoint_enabled", [], "breakpoint", "circle", sublime.HIDDEN | sublime.PERSISTENT)
elif num == 1:
# activate all
d = self.breakpoints.find('Dictionary')
for item in d:
for p in item[1][0]:
p[2].text, p[3].text = "true", "true"
self.xml_tree.write(self.breakpoints_xml)
for view in sublime.active_window().views():
active = view.get_regions("breakpoint_enabled")
deactivated = view.get_regions("breakpoint_deactivated")
view.add_regions("breakpoint_deactivated", [], "breakpoint", "dirty_circle_light", sublime.HIDDEN | sublime.PERSISTENT)
view.add_regions("breakpoint_enabled", active + deactivated, "breakpoint", "circle", sublime.HIDDEN | sublime.PERSISTENT)
elif num == 2:
# delete all
d = self.breakpoints.find('Dictionary')
for item in d:
item[1][0].clear()
self.xml_tree.write(self.breakpoints_xml)
for view in sublime.active_window().views():
view.add_regions("breakpoint_deactivated", [], "breakpoint", "dirty_circle_light", sublime.HIDDEN | sublime.PERSISTENT)
view.add_regions("breakpoint_enabled", [], "breakpoint", "circle", sublime.HIDDEN | sublime.PERSISTENT)
else:
print(self.points)
open_folder_arr = self.view.window().folders()
inst_dir, source_dir = get_paths(open_folder_arr, True)
file_name = inst_dir[:-15] + "Development\\Src\\" + self.points[num - 3]
window = sublime.active_window()
window.open_file(file_name, sublime.ENCODED_POSITION | sublime.TRANSIENT)
window.open_file(file_name, sublime.ENCODED_POSITION | sublime.TRANSIENT)
# Installs the debugger. if there is already a debugger installed, creates a backup
class UnrealInstallDebuggerCommand(sublime_plugin.TextCommand):
def run(self, edit, b_64bit=False):
open_folder_arr = self.view.window().folders() # Gets all opened folders in the Sublime Text editor.
self.install_dir, self.source_dir = get_paths(open_folder_arr, b_64bit)
self.install_debugger()
# copies all needed files and if needed creates a backup of DebuggerInterface.dll
def install_debugger(self):
for filename in os.listdir(self.source_dir):
try:
if os.path.getsize(os.path.join(self.source_dir, filename)) != os.path.getsize(os.path.join(self.install_dir, filename)):
# backup
if filename == "DebuggerInterface.dll":
if not os.path.exists(os.path.join(self.install_dir, "DebuggerInterface.dll-old-UScriptIDE")):
print("Another debugger was found. Creating backup")
os.rename(os.path.join(self.install_dir, filename), os.path.join(self.install_dir, "DebuggerInterface.dll-old-UScriptIDE"))
# install
print("install newer version", filename)
self.install(os.path.join(self.source_dir, filename), os.path.join(self.install_dir, filename))
except os.error:
# install
print("fresh install", filename)
self.install(os.path.join(self.source_dir, filename), os.path.join(self.install_dir, filename))
self.view.window().run_command("unreal_load_breakpoints", {"b_set_breakpoints": True})
# copy file or directory
def install(self, src, dst):
if os.path.isdir(src):
try:
shutil.copytree(src, dst)
except FileExistsError:
print("cannot copy file: ", src, " file already exists!")
else:
shutil.copy(src, dst)
# Uninstalls the debugger and install your old debugger again
class UnrealUninstallDebuggerCommand(sublime_plugin.TextCommand):
def run(self, edit):
open_folder_arr = self.view.window().folders() # Gets all opened folders in the Sublime Text editor.
self.install_dir_32, self.source_dir_32 = get_paths(open_folder_arr, False)
self.install_dir_64, self.source_dir_64 = get_paths(open_folder_arr, True)
self.uninstall(self.source_dir_32, self.install_dir_32)
self.uninstall(self.source_dir_64, self.install_dir_64)
# deletes all debugger files and restores the backup
def uninstall(self, src, uninst):
print("uninstall debugger from ", uninst)
for f in os.listdir(src):
uninst_f_path = os.path.join(uninst, f)
if os.path.exists(uninst_f_path):
if os.path.isfile(uninst_f_path):
os.unlink(uninst_f_path)
else:
shutil.rmtree(uninst_f_path)
backup = os.path.join(uninst, "DebuggerInterface.dll-old-UScriptIDE")
if os.path.exists(backup):
if not os.path.exists(backup[:-15]):
print("old debugger found, reinstalling the debugger")
os.rename(backup, backup[:-15])
# loads breakpoints from the master file
class UnrealLoadBreakpointsCommand(sublime_plugin.TextCommand):
def run(self, edit, b_set_breakpoints=False):
self.filename = self.view.file_name()
if self.filename:
open_folder_arr = self.view.window().folders()
install_dir_64, src_dir = get_paths(open_folder_arr, True)
install_dir_32, src_dir = get_paths(open_folder_arr, False)
self.breakpoints_xml = install_dir_64[:-6] + "UScriptIDE_Breakpoints.xml"
if os.path.exists(self.breakpoints_xml):
xml_tree = ElementTree.parse(self.breakpoints_xml)
breakpoints = xml_tree.find("Breakpoints")
if b_set_breakpoints:
print("set breakpoints")
self.set_breakpoints(breakpoints, install_dir_32 + "UnrealDebugger.project")
self.set_breakpoints(breakpoints, install_dir_64 + "UnrealDebugger.project")
else:
print("load breakpoints")
LoadBreakpoints(breakpoints, self).start()
# self.load_breakpoints(breakpoints)
def call_toggle_breakpoint(self, b):
p = self.view.text_point(int(b.find("LineNo").text)-1, 0)
point = self.view.line(p)
self.view.window().run_command("unreal_toggle_breakpoint", {"b_deactivate": b.find("Enabled").text != "true", "breakpoint_a": point.a, "breakpoint_b": point.b})
# copies the breakpoints from the master file to The Debugger files
def set_breakpoints(self, breakpoints, debugger_file):
if os.path.exists(debugger_file):
debugger_tree = ElementTree.parse(debugger_file)
break_on_first_line = debugger_tree.find("BreakOnFirstLine")
if break_on_first_line is not None:
s = sublime.load_settings('UnrealScriptIDE.sublime-settings')
break_on_first_line.text = "false" if not s.get('break_on_first_line') else "true"
else:
print("not found!!, ", break_on_first_line)
debugger_breakpoints = debugger_tree.find("Breakpoints")
dic = breakpoints.find("Dictionary")
dic_debug = debugger_breakpoints.find("Dictionary")
dic_debug.clear()
for item in dic:
dic_debug.append(item)
debugger_tree.write(debugger_file)
else:
shutil.copy(self.breakpoints_xml, debugger_file)
# a separate thread for loading breakpoints, as this can take a moment.
class LoadBreakpoints(threading.Thread):
def __init__(self, breakpoints, main_thread):
self.breakpoints = breakpoints
self.main_thread = main_thread
threading.Thread.__init__(self)
def run(self): # gets called when the thread is created
self.load_breakpoints(self.breakpoints)
# loads the saved breakpoints from the master file to the current view
def load_breakpoints(self, breakpoints):
dic = breakpoints.find("Dictionary")
current_file = self.main_thread.filename.split('\\')[-1].split('.')[0].lower()
for item in dic:
k = item.find("key")
s = k.find("string")
if s.text.split('.')[-1].lower() == current_file:
print("load breakpoints for file ", current_file)
v = item.find("value")
arr = v.find("ArrayOfBreakpoint")
for b in arr:
# p = self.main_thread.view.text_point(int(b.find("LineNo").text)-1, 0)
# point = self.main_thread.view.line(p)
sublime.set_timeout(lambda: self.main_thread.call_toggle_breakpoint(b), 1)
# Toggles a breakpoint visually. Also saves the breakpoints to the master file.
class UnrealToggleBreakpointCommand(sublime_plugin.TextCommand):
def run(self, edit, b_deactivate=False, breakpoint_a=None, breakpoint_b=None):
old_breakpoints = self.view.get_regions("breakpoint_enabled")
old_breakpoints_deactivated = self.view.get_regions("breakpoint_deactivated")
if breakpoint_a and breakpoint_b:
print(breakpoint_a, breakpoint_b, b_deactivate)
breakpoint = [sublime.Region(breakpoint_a, breakpoint_b)]
if breakpoint[0] not in old_breakpoints and breakpoint[0] not in old_breakpoints_deactivated:
if not b_deactivate:
self.view.add_regions("breakpoint_enabled", old_breakpoints + breakpoint, "breakpoint", "circle", sublime.HIDDEN | sublime.PERSISTENT)
else:
self.view.add_regions("breakpoint_deactivated", old_breakpoints_deactivated + breakpoint, "breakpoint", "dirty_circle_light", sublime.HIDDEN | sublime.PERSISTENT)
else:
selections = [s for s in self.view.sel()]
selections = self.clean_selection(selections)
if not b_deactivate:
new_breakpoints = self.create_new_breakpoints(old_breakpoints, selections)
new_breakpoints_deactivated = self.list_minus_list(old_breakpoints_deactivated, new_breakpoints)
else:
new_breakpoints_deactivated = self.create_new_deactivated_breakpoints(old_breakpoints, selections)
new_breakpoints_deactivated += old_breakpoints_deactivated
new_breakpoints = self.list_minus_list(old_breakpoints, new_breakpoints_deactivated)
self.view.add_regions("breakpoint_deactivated", new_breakpoints_deactivated, "breakpoint", "dirty_circle_light", sublime.HIDDEN | sublime.PERSISTENT) # dirty_indicator, dirty_circle, circle
self.view.add_regions("breakpoint_enabled", new_breakpoints, "breakpoint", "circle", sublime.HIDDEN | sublime.PERSISTENT)
self.save_breakpoints(new_breakpoints, new_breakpoints_deactivated)
# this saves the currently only visual breakpoints to the master file
def save_breakpoints(self, breakpoints, breakpoints_deactivated):
self.current_file = self.view.file_name().split('\\')
self.current_file = self.current_file[-3] + "." + self.current_file[-1].split('.')[0]
breakpoints_formatted = []
for b in breakpoints:
line, c = self.view.rowcol(b.a)
breakpoints_formatted.append((line + 1, True))
for b in breakpoints_deactivated:
line, c = self.view.rowcol(b.a)
breakpoints_formatted.append((line + 1, False))
print(breakpoints_formatted)
open_folder_arr = self.view.window().folders()
install_dir_64, src_dir = get_paths(open_folder_arr, True)
breakpoints_xml = install_dir_64[:-6] + "UScriptIDE_Breakpoints.xml"
if os.path.exists(breakpoints_xml):
print("append breakpoints to existing xml")
xml_tree = ElementTree.parse(breakpoints_xml)
bs = xml_tree.find("Breakpoints")
d = bs.find('Dictionary')
b_found_item = False
for item in d:
if item[0][0].text == self.current_file.upper():
item[1][0].clear()
b_found_item = True
self.add_breakpoints_to_array(breakpoints_formatted, item[1][0])
if not b_found_item:
arr = self.add_item_to_dictionary(d)
self.add_breakpoints_to_array(breakpoints_formatted, arr)
xml_tree.write(breakpoints_xml)
else:
print("create new xml with breakpoints")
root = ElementTree.Element('Project')
bs = ElementTree.SubElement(root, "Breakpoints")
d = ElementTree.SubElement(bs, "Dictionary")
arr = self.add_item_to_dictionary(d)
self.add_breakpoints_to_array(breakpoints_formatted, arr)
ElementTree.ElementTree(root).write(breakpoints_xml)
def add_item_to_dictionary(self, Dictionary):
i = ElementTree.SubElement(Dictionary, "item")
k = ElementTree.SubElement(i, "key")
s = ElementTree.SubElement(k, "string")
s.text = self.current_file.upper()
v = ElementTree.SubElement(i, "value")
return ElementTree.SubElement(v, "ArrayOfBreakpoint")
def add_breakpoints_to_array(self, breakpoints_formatted, ArrayOfBreakpoint):
for point in breakpoints_formatted:
b = ElementTree.SubElement(ArrayOfBreakpoint, "Breakpoint")
cn = ElementTree.SubElement(b, "ClassName")
cn.text = self.current_file
ln = ElementTree.SubElement(b, "LineNo")
ln.text = str(point[0])
h = ElementTree.SubElement(b, "Healthy")
h.text = str(point[1]).lower()
e = ElementTree.SubElement(b, "Enabled")
e.text = str(point[1]).lower()
# this will return the new breakpoints. If a breakpoint in new_candidates is already in old_breakpoints it will be removed.
def create_new_breakpoints(self, old_breakpoints, new_candidates):
new_breakpoints = []
for b in old_breakpoints:
if b in new_candidates:
new_candidates.remove(b)
else:
new_breakpoints.append(b)
new_breakpoints += new_candidates
return new_breakpoints
# returns all matching breakpoints
def create_new_deactivated_breakpoints(self, old_breakpoints, new_candidates):
new_breakpoints = []
for b in old_breakpoints:
if b in new_candidates:
new_breakpoints.append(b)
return new_breakpoints
def list_minus_list(self, a, b):
for x in b:
if x in a:
a.remove(x)
return a
def clean_selection(self, selection):
new_candidates_cleaned = []
for s in selection:
new_candidates_cleaned += self.view.split_by_newlines(s)
selection = []
for r in new_candidates_cleaned:
selection.append(self.view.line(r))
return selection