-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtranslator.py
354 lines (253 loc) · 8.55 KB
/
translator.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
''' __ ___________
_______/ |\_ _____/__ ______
/ ___/\ __\ __)_\ \/ / _ \
\___ \ | | | \\ ( <_> )
/____ > |__|/_______ / \_/ \____/
\/ \/
This code has no licence, feel free to do whatever you want with it.
'''
import cod2_default_element_settings
def log(msg, state = 'INFO', out = ''):
print state.upper() + ': ' + msg
if out:
out.tx.insert('end', state.upper() + ': ' + msg + '\n')
def _calculateTabs(value):
TABMAX = 7
numOfTabs = ( TABMAX*4 - len(value) ) / 4
if (len(value)%4 !=0): numOfTabs += 1
return '\t'*numOfTabs
def getSortedDic(dic, key = 'order'):
arr = []
for i in dic:
arr.append(dic[i])
return sorted(arr, key = lambda x: x['order'])
def _writeProperties(element, indent):
toWrite = ''
for property in cod2_default_element_settings.elementOrder:
if property not in element['properties']:
continue
value = element['properties'][property][0]
flags = element['properties'][property][1]
if 'DNIIN' in flags and not value:
continue
if 'DNII-' in flags and (value == ' ' or value == 'MENU_FALSE' or value =='0' or value == '-'):
continue
if 'CL' in flags:
value = ''
if 'S' in flags:
value = '"' + value + '"'
if 'BW' in flags:
value = '{ ' + value + ' }'
toWrite += indent+property+ _calculateTabs(property) +value+'\n'
return toWrite
def exportAsMenu(Menus, saveto = 'C:/users/stevo/desktop/test.menu'):
# Variable init
toWrite = ''
# Includes
toWrite += '#include "ui_mp/menudef.h"\n'
toWrite += '\n'
# Local definitions
#toWrite += cod2_default_element_settings.getCusotmDefs()
# Header part
toWrite += '\n{\n'
for menu in Menus:
# Menu header
toWrite += '\tmenuDef\n'
toWrite += '\t{\n'
# Menu properties
toWrite += _writeProperties(menu, '\t'*2)
# ExecKey
if menu['execKey']:
toWrite += '\n'
for key in menu['execKey']:
toWrite += '\t\texecKey '+key+' '+menu['execKey'][key] +'\n'
# Seperate menu and elements
toWrite += '\n\n'
# Get sorted elements (by order they were created)
elements = getSortedDic(menu['elements'])
# Elements
for element in elements:
# Element header
toWrite += '\t\titemDef\n'
toWrite += '\t\t{\n'
# Element properties
toWrite += _writeProperties(element, '\t'*3)
toWrite += '\t\t}\n'
toWrite += '\n'
toWrite += '\t}\n\n'
toWrite += '}\n'
file = open(saveto, 'wb')
file.write(toWrite)
file.close()
# -------------------------------------------------------------------------------------------------------------
def getSegments(data):
segments, segment = [], ''
inside = 0
for i in data:
if i == '"':
inside = 1-inside
if i == '\t' or (i == ' ' and not inside):
if segment != '':
if '//' in segment:
segment = ''
break
segments.append(segment)
segment = ''
else:
segment += i
if segment != '':
segments.append(segment)
return segments
def processData(data):
data = data.split('\n')
newData = []
for line in data:
# Separete in new lines
segments = getSegments(line)
if segments:
newData.append(segments)
# all in one
#segments = getSegments(line)
#for segment in segments:
# if segment:
# newData.append(segment)
return newData
def processInclude(data, out=''):
for i in range(len(data)):
item = data[i][0]
if item == '#include':
path = data[i][1].replace('"','')
data.pop(i)
log('Loading #include file: ' + path, out = out)
path = 'Data/cod2_menus/' + path
file = open(path, 'rb')
filedata = file.read().replace('\r', '')
while '/*' in filedata: filedata = filedata.split('/*', 1)[0] + filedata.split('/*', 1)[1].split('*/', 1)[1]
filedata = processData(filedata)
file.close()
for segment in filedata[::-1]:
data.insert(i, segment)
break
return data
def loadItemDef(GUI, data, inx, out=''):
bracketNum = 0
bracketValue = ''
lastItem = ''
elementID = GUI.elementManager.createItemElement()
element = GUI.elementManager.elements[elementID]
element['properties']['text'][2].var.set('')
for i in range(inx, len(data)):
item = data[i][0]
value = ' '.join(data[i][1:])
if '{' not in value: value = value.replace('"','')
if value in cod2_default_element_settings.globalDefinitions['custom']:
value = cod2_default_element_settings.globalDefinitions['custom'][value]
if item == '{':
bracketNum += 1
elif item == '}':
bracketNum -= 1
if bracketNum == 1 and bracketValue != '':
if lastItem in element['properties']:
element['properties'][lastItem][2].var.set(bracketValue)
element['properties'][lastItem][0] = bracketValue
bracketValue = ''
lastItem = item
elif bracketNum == 2:
bracketValue += item + ' ' + value + ' '
elif item in element['properties'] and bracketNum == 1:
if item == 'decoration': value = 'MENU_TRUE'
element['properties'][item][2].var.set(value)
element['properties'][item][0] = value
lastItem = item
else:
lastItem = item
log('Unknown item Property: ' + item, 'warning', out=out)
if not bracketNum:
break
if element['properties'].has_key('rect'):
element['properties']['rect'][0] = cod2_default_element_settings.getMultipleValuesFromKey(element['properties']['rect'][2].var.get())
while element['properties']['rect'][0].count(' ') < 5:
element['properties']['rect'][0] += ' 0'
element['properties']['rect'][2].var.set(element['properties']['rect'][0])
if element['properties'].has_key('type') and element['properties']['type'][0] == 'ITEM_TYPE_SLIDER':
GUI.elementManager.initSlider(element)
GUI.elementManager.calculateCords(element, updateImage = True)
return i
def loadMenuDef(GUI, data, inx, out = ''):
bracketNum, skip = 0, 0
bracketValue = ''
lastItem = ''
menu = GUI.MenuManager.createMenu()
GUI.nb.select(menu['id'])
GUI.MenuManager.selectMenu(menu)
for i in range(inx, len(data)):
if i < skip:
continue
item = cod2_default_element_settings.fixItem(data[i][0])
value = ' '.join(data[i][1:]).replace('"', '')
if item == '{':
bracketNum += 1
elif item == '}':
bracketNum -= 1
if bracketNum == 1 and bracketValue != '':
if lastItem in menu['properties']:
menu['properties'][lastItem][0] = bracketValue
bracketValue = ''
lastItem = item
elif bracketNum == 2:
bracketValue += item + ' ' + value + ' '
elif item in menu['properties'] and bracketNum == 1:
#log('Menu property: '+ item+ ', value: '+ value)
menu['properties'][item][0] = value
lastItem = item
elif item.lower() == 'itemdef':
skip = loadItemDef(GUI, data, i+1, out=out) +1
elif item.lower() == 'execkey':
key = data[i][1]
value = ' '.join(data[i][2:]).replace('"', '')
if '{' not in value or '}' not in value:
log('Faulty or muli-lined execKey: ' + value)
menu['execKey'][key] = value
else:
lastItem = item
log('Unknown Menu Property: ' + item, 'warning', out=out)
if not bracketNum:
break
GUI.MenuManager.updateTabName(menu['id'], menu['properties']['name'][0])
menu['name'] = menu['properties']['name'][0]
def importMenuFile(GUI, filePath = 'C:/users/stevo/desktop/ingame.menu', out = ''):
log('Import started ', out = out)
try:
file = open(filePath, 'rb')
data = file.read().replace('\r', '')
file.close()
log('File has been read', 'success', out = out)
except Exception, err:
log('Following error occured while reading file: ' + str(err), 'critical', out = out)
return -1
try:
while '/*' in data: data = data.split('/*', 1)[0] + data.split('/*', 1)[1].split('*/', 1)[1]
data = processData(data)
while '#include' in [item for sublist in data for item in sublist]:
data = processInclude(data, out=out)
log('All "#include" have been imported', 'success', out = out)
except Exception, err:
log('Following error occured while processing include: ' + str(err), 'critical', out = out)
return -2
try:
for i in range(len(data)):
for i2 in range(len(data[i])):
item = data[i][i2].lower()
if item == '#define':
variable = data[i][1]
value = ' '.join(data[i][2:])
if cod2_default_element_settings.getValueFromKey(variable) == variable:
cod2_default_element_settings.globalDefinitions['custom'][variable] = value
if item == 'menudef':
log('New menu has been found', out = out)
loadMenuDef(GUI, data, i+1, out=out)
except Exception, err:
log('Following error occured while processing file: ' + str(err), 'critical', out = out)
return -3
return 0