-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathguiparser.py
371 lines (339 loc) · 15.4 KB
/
guiparser.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
"""
SWTOR GUI Parser
A class that uses XML parsing to parse the SWTOR GUI files in the GUIProfiles directory, generated by SWTOR. Individual
elements can be retrieved, as long as their name is known from the point of initialization.
Written by RedFantom for the GSF Parser
Copyright (c) RedFantom 2017
Available under the license as in LICENSE
"""
import xml.etree.cElementTree as et
import os
import sys
try:
import Tkinter as tk
import tkMessageBox as messagebox
except ImportError:
import tkinter as tk
from tkinter import messagebox
def get_screen_resolution():
"""
Cross-platform way to get the screen resolution using Tkinter. Alternative methods include using GetSystemMetrics
with win32api for Windows and screen_width()/screen_height() from the gtk.gdk module on Linux. For now, it seems
best to use a cross-platform option
:return: tuple, (width, height), such as (1920, 1080)
"""
root = tk.Tk()
width = root.winfo_screenwidth()
height = root.winfo_screenheight()
root.destroy()
return width, height
def get_swtor_directory():
"""
Returns the absolute path to the directory that contains the SWTOR temporary files
:return: str
"""
if sys.platform == "win32":
import tempfile
path = os.path.abspath(os.path.join(tempfile.gettempdir(), "..", "SWTOR"))
if not os.path.exists(path):
raise ValueError("SWTOR directory not found. Is SWTOR installed?")
return path
else:
raise NotImplementedError
def get_gui_profiles():
"""
Returns a list of all GUI profiles available in the SWTOR directory
:return: list
"""
return [item.replace(".xml", "") for item in
os.listdir(os.path.join(get_swtor_directory(), "swtor", "settings", "GUIProfiles"))]
def get_player_guiname(player_name):
"""
Returns the GUI Profile name for a certain player name. Does not work if there are multiple characters with the same
name on the same account used on the same computer and also doesn't work if the name is misspelled. Credit for
finding this reference to the GUI state files in the SWTOR settings files goes to Ion.
:param player_name: name of the player
:return: GUI profile name, not XML file
"""
if not isinstance(player_name, str):
raise ValueError("player_name is not of str type: {0}".format(player_name))
if player_name == "":
raise ValueError("player_name is an empty str")
dir = os.path.join(get_swtor_directory(), "swtor", "settings")
if not os.path.exists(dir):
messagebox.showerror("Error", "SWTOR settings path not found. Is SWTOR correctly installed?")
raise ValueError("SWTOR settings path not found")
correct_file = None
for file_name in os.listdir(dir):
if not file_name.endswith(".ini"):
continue
elif player_name in file_name:
correct_file = file_name
break
if not correct_file:
raise ValueError("Could not find a player settings file with name: {0}".format(player_name))
with open(os.path.join(dir, correct_file)) as settings_file:
lines = settings_file.readlines()
gui_profile = None
for line in lines:
elements = line.split("=")
if elements[0] == "GUI_Current_Profile ":
gui_profile = str(elements[1])
break
if not gui_profile:
raise ValueError("Could not find GUI_Current_Profile in settings file {0}".format(correct_file))
return gui_profile[1:]
class GUIParser(object):
"""
Parses an SWTOR GUI profile by first reading the file into an ElementTree and then allowing the user to retrieve
data values from the profile by providing tuples or directly calculating coordinates the user needs. Example piece
of GSF GUI profile section:
<FreeFlightPlayerStatusEffects>
<anchorAlignment Type="3" Value="2.000000" />
<anchorXOffset Type="3" Value="25.000000" />
<anchorYOffset Type="3" Value="-200.000000" />
<scale Type="3" Value="1.000000" />
<enabled Type="2" Value="1" />
<alpha Type="3" Value="100.000000" />
</FreeFlightPlayerStatusEffects>
All GSF GUI elements provide the attributes anchorAlignment, anchorXOffset, anchorYOffset, scale, enabled and alpha
The amount of GSF GUI elements is, luckily, quite limited, a list of items is available in the class' __init__
function.
The alignment of the GUI element works as follows:
- The anchorXOffset and anchorYOffset are in pixels
- The offsets are counted from one out of nine points on the screen, to the same respective point on the GUI element
- The points are these:
X Y
* 1: Left top
* 2: Left bottom
* 3: Left center
* 4: Right top
* 5: Right bottom
* 6: Right center
* 7: Center top
* 8: Center bottom
* 9: Center center
So, if the anchorXOffset is 50, the anchorYOffset is 0 and the anchor is 8, then the bottom center of the GUI
element is 50 pixels to the right from the bottom center of the screen
All the credit for this incredibly useful information goes to Ion, who has written a SWTOR UI layout generator
that you can find here: https://github.com/ion1/swtor-ui
Important Note: Not all GUI elements in the SWTOR XML files have the same capitalization in their elements. Such as
the different variants AnchorXOffset, anchorXOffset and even anchorOffsetX! Please check what forrmat your option
uses before using it in this class.
"""
debug = True
def __init__(self, file_name, target_items):
"""
Initializes the class by reading the XML file and setting things up for access by the user
:param file_name: a GUI profile file_name, either an absolute path or a plain file_name
"""
file_name = os.path.basename(file_name)
if ".xml" not in file_name:
file_name += ".xml"
if not os.path.exists(file_name):
file_name = os.path.join(get_swtor_directory(), "swtor", "settings", "GUIProfiles", file_name)
if not os.path.exists(file_name):
raise ValueError("file_name specified is not valid: {0}".format(file_name))
self.file_name = file_name
self.tree = et.parse(file_name)
self.root = self.tree.getroot()
self.gui_elements = {}
self.target_items = [item for item in target_items]
self.target_sizes = target_items
for item in self.target_items:
self.gui_elements[item] = self.root.find(item)
if not self.gui_elements[item]:
raise ValueError("Could not find {0} in GUI profile".format(item))
resolution = get_screen_resolution()
self.anchor_dictionary = self.get_anchor_dictionary(resolution)
return
def __getitem__(self, key):
"""
Returns the requested data to the user
:param key: (element, item, attribute)
:return: value in str type
"""
if not isinstance(key, tuple):
raise ValueError("key requested not a tuple: {0}".format(key))
if not len(key) is 3:
raise ValueError("key requested not with len 3: {0}".format(key))
return self.gui_elements[key[0]].find(key[1]).get(key[2])
def __setitem__(self, key, value):
raise ValueError("Manipulating GUI profiles is not supported")
@staticmethod
def get_anchor_dictionary(resolution):
"""
Get a dictionary of the absolute pixel points for each of the nine anchor points in the docstring of this class
by performing the required calculations.
:param resolution: (width, height) tuple
:return: anchor_point dict
"""
x_left = 0
x_center = int(round(resolution[0] / 2, 0)) - 5
x_right = resolution[0] - 5
y_top = 0
y_center = int(round(resolution[1] / 2, 0))
y_bottom = resolution[1]
anchor_points = {
1: (x_left, y_top),
2: (x_left, y_bottom),
3: (x_left, y_center),
4: (x_right, y_top),
5: (x_right, y_bottom),
6: (x_right, y_center),
7: (x_center, y_top),
8: (x_center, y_bottom),
9: (x_center, y_center)
}
return anchor_points
@staticmethod
def get_element_absolute_coordinates(anchor_points, anchor, x_offset, y_offset):
"""
Get absolute screen pixel coordinates for an element by name
:param anchor_points: dictionary anchor_points
:param anchor: anchor number (must be in anchor_points dict)
:param x_offset: int offset
:param y_offset: int offset
:return: (x, y) tuple
"""
return anchor_points[anchor][0] + x_offset, anchor_points[anchor][1] + y_offset
@staticmethod
def get_item_value(element, name):
"""
Get an int value from an element
:param element: XML parser element
:param name: sub-element name
:return: int value
"""
return int(round(float(element.find(name).get("Value")), 0))
@staticmethod
def get_element_scale(element):
"""
As the scale is a float value, not an int, the normal class method can't be used for this item
:param element: element object
:return: float
"""
return round(float(element.find("scale").get("Value")), 3)
@staticmethod
def get_scale_corrected_value(value, scale):
return int(round(value * scale, 0))
def get_essential_element_values(self, element):
"""
Get the essential element values for a GSF GUI element (for the position)
:param element: XML parser element
:return: anchor number, x_offset int, y_offset int, alpha percentage
"""
x_offset = self.get_item_value(element, "anchorXOffset")
y_offset = self.get_item_value(element, "anchorYOffset")
alpha = self.get_item_value(element, "anchorAlignment")
return x_offset, y_offset, alpha
def get_element_object(self, element_name):
"""
Check the element name passed as argument and return an appropriate element object
:param element_name: str name
:return: element object
"""
if element_name not in self.gui_elements:
raise ValueError("element requested that was not in target_items initializer argument: {0}".
format(element_name))
return self.gui_elements[element_name]
def get_element_anchor(self, element):
"""
Get the element anchor number
:param element: element object
:return: anchor int
"""
return self.get_item_value(element, "anchorAlignment")
def get_box_coordinates(self, element_name):
"""
Get a tuple with the box coordinates for an element
:param element_name: name of the element
:return: (x, y, x+~, y+~)
"""
element = self.get_element_object(element_name)
x_offset, y_offset, alpha = self.get_essential_element_values(element)
scale = self.get_element_scale(element)
anchor = self.get_element_anchor(element)
self.debug_print("element_name, x_offset, y_offset, alpha, scale, anchor: {0}, {1}, {2}, {3}, {4}, {5}".format(
element_name, x_offset, y_offset, alpha, scale, anchor))
if anchor is 1:
# Anchor left top
x_one = self.anchor_dictionary[anchor][0] + x_offset
y_one = self.anchor_dictionary[anchor][1] + y_offset
elif anchor is 2:
# Anchor left bottom
x_one = self.anchor_dictionary[anchor][0] + x_offset
y_one = self.anchor_dictionary[anchor][1] + y_offset - self.get_scale_corrected_value(
self.target_sizes[element_name][1], scale)
elif anchor is 3:
# Anchor left center
x_one = self.anchor_dictionary[anchor][0] + x_offset
y_one = self.anchor_dictionary[anchor][1] + y_offset - self.get_scale_corrected_value(
self.target_sizes[element_name][1] * .5, scale)
elif anchor is 4:
# Anchor right top
x_one = self.anchor_dictionary[anchor][0] + x_offset - self.get_scale_corrected_value(
self.target_sizes[element_name][0], scale)
y_one = self.anchor_dictionary[anchor][1] + y_offset
elif anchor is 5:
# Anchor right bottom
x_one = self.anchor_dictionary[anchor][0] + x_offset - self.get_scale_corrected_value(
self.target_sizes[element_name][0], scale)
y_one = self.anchor_dictionary[anchor][1] + y_offset - self.get_scale_corrected_value(
self.target_sizes[element_name][1], scale)
elif anchor is 6:
# Anchor right center
x_one = self.anchor_dictionary[anchor][0] + x_offset - self.get_scale_corrected_value(
self.target_sizes[element_name][0], scale)
y_one = self.anchor_dictionary[anchor][1] + y_offset - self.get_scale_corrected_value(
self.target_sizes[element_name][1] * 0.5, scale)
elif anchor is 7:
# Anchor center top
x_one = self.anchor_dictionary[anchor][0] + x_offset - self.get_scale_corrected_value(
self.target_sizes[element_name][0] * 0.5, scale)
y_one = self.anchor_dictionary[anchor][1] + y_offset
elif anchor is 8:
# Anchor center bottom
x_one = self.anchor_dictionary[anchor][0] + x_offset - self.get_scale_corrected_value(
self.target_sizes[element_name][0] * 0.5, scale)
y_one = self.anchor_dictionary[anchor][1] + y_offset - self.get_scale_corrected_value(
self.target_sizes[element_name][1], scale)
elif anchor is 9:
# Anchor center center
x_one = self.anchor_dictionary[anchor][0] + x_offset - self.get_scale_corrected_value(
self.target_sizes[element_name][0] * 0.5, scale)
y_one = self.anchor_dictionary[anchor][1] + y_offset - self.get_scale_corrected_value(
self.target_sizes[element_name][1] * 0.5, scale)
else:
raise ValueError("Invalid anchor value found: {0}".format(anchor))
x_two = x_one + self.get_scale_corrected_value(self.target_sizes[element_name][0], scale)
y_two = y_one + self.get_scale_corrected_value(self.target_sizes[element_name][1], scale)
return x_one, y_one, x_two, y_two
def get_max_min_coordinates(self, output=False):
"""
Originally for reverse engineering only
:param output: if True prints results
:return: (x_min, x_max, y_min, y_max), all float
"""
x_values = []
y_values = []
for child in self.root:
valid = False
for item in child:
if item.tag != "anchorXOffset":
continue
else:
valid = True
break
if not valid:
continue
x_values.append(float(child.find("anchorXOffset").get("Value")))
y_values.append(float(child.find("anchorYOffset").get("Value")))
if output:
print("Minimum X Value: ", min(x_values), "\nMaximum X Value: ", max(x_values))
print("Minimum Y Value: ", min(y_values), "\nMaximum Y Value: ", max(y_values))
return min(x_values), max(x_values), min(y_values), max(y_values)
def debug_print(self, line):
if self.debug:
print(line)
return