-
Notifications
You must be signed in to change notification settings - Fork 20
/
util.py
379 lines (305 loc) · 11.3 KB
/
util.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
import logging
import math
from PIL import Image
from _remote import ffi, lib
refs = None
STRUCTTYPES = ffi.list_types()[1]
GLFUNCTIONS = '''
glGetError glEnable glDisable glHint
glTexImage2D glTexParameteri
'''.split()
GL_TEXTURE_2D = 0x0DE1
GL_TEXTURE_BASE_LEVEL = 0x813C
GL_TEXTURE_MAX_LEVEL = 0x813D
GL_TEXTURE_MAG_FILTER = 0x2800
GL_TEXTURE_MIN_FILTER = 0x2801
GL_NEAREST_MIPMAP_NEAREST = 0x2700
GL_NEAREST_MIPMAP_LINEAR = 0x2702
GL_LINEAR_MIPMAP_NEAREST = 0x2701
GL_LINEAR_MIPMAP_LINEAR = 0x2703
GL_UNSIGNED_BYTE = 0x1401
GL_RGBA = 0x1908
# classes ####################################################################
class PlainText(object):
'''wrapper for text drawing'''
def __init__(self, *, size=14, outlineSize=2, font=b'TenbyFive',
color=0xffffff, outlineColor=0x000000, alpha=1.0,
screenCoords=True):
self.size = size
self.outlineSize = outlineSize
self.font = font
self.color = color
self.outlineColor = outlineColor
self._texture = 0
self.text = b''
self._w = ffi.new('int *')
self._h = ffi.new('int *')
self.w = self.h = 0
self.alpha = alpha
self._dirty = True
self.screenCoords = screenCoords
def __setattr__(self, name, val):
if name == 'size' or name == 'outlineSize':
val = int(val)
elif name == 'color' or name == 'outlineColor':
val = (int(val) & 0xffffff) | 0xff000000
elif name == 'font' or name == 'text':
if type(val) != bytes:
if type(val) == str:
val = val.encode('utf-8')
else:
raise AttributeError(
'"{}" must be str or bytes, got {}'.format(
name, str(type(val))))
else: # anything else is not affecting texture - pass through
if name == 'alpha':
val = max(0, min(1, val))
self._cmod = (math.floor(val * 255) << 24) & 0xff000000
object.__setattr__(self, name, val)
return
# check if the texture needs updating
try:
oldval = self.__getattribute__(name)
if oldval != val:
self._dirty = True
except AttributeError:
pass
object.__setattr__(self, name, val)
def updateTexture(self):
self._texture = refs.XDL_CreateTextTexture(
self._texture, self.font, self.size, self.color,
self.text, self.outlineSize, self.outlineColor)
refs.XDL_QueryTexture(self._texture, self._w, self._h)
self.w = self._w[0]
self.h = self._h[0]
def __del__(self):
if self._texture > 0:
refs.XDL_DestroyTexture(self._texture)
def draw(self, x, y, anchorX=0, anchorY=0, angle=0):
if self.size <= 0 or len(self.text) == 0:
return
if self._dirty:
self.updateTexture()
self._dirty = False
if self._texture <= 0:
return
w = self.w
h = self.h
if not self.screenCoords:
x /= refs.scaleX
y /= refs.scaleY
x -= anchorX * w
y -= anchorY * h
rotDegt = round(angle * 10)
cw = refs.canvasW_[0]
ch = refs.canvasH_[0]
refs.canvasW_[0] = refs.windowW
refs.canvasH_[0] = refs.windowH
refs.XDL_DrawTexture(
self._texture, 0, 0, w, h,
round(x), round(y), w, h,
rotDegt, w // 2, h // 2,
0, self._cmod, lib.BLENDMODE_BLEND)
refs.canvasW_[0] = cw
refs.canvasH_[0] = ch
class MultilineText(object):
def __init__(self, *, spacing=15, **kwargs):
self._kwargs = kwargs
self.spacing = spacing
self.children = []
def __setattr__(self, name, val):
if name == '_kwargs' or name == 'children':
pass
elif name == 'spacing':
val = int(val)
if val < 0:
val = 0
elif name == 'text':
lines = val.splitlines()
if len(self.children) < len(lines):
for i in range(len(lines) - len(self.children)):
self.children.append(PlainText(**self._kwargs))
for i in range(len(lines)):
self.children[i].text = lines[i]
self.children = self.children[:len(lines)]
else:
self._kwargs[name] = val
for t in self.children:
t.__setattr__(name, val)
object.__setattr__(self, name, val)
def draw(self, x, y, **kwargs):
for t in self.children:
t.draw(x, y, **kwargs)
y += self.spacing
if y > refs.windowH:
return
class NumberDict(PlainText):
def __init__(self, *, size=16, outlineSize=2, font=b'HemiHeadBold',
color=0xffffff, outlineColor=0x000000, screenCoords=False):
super().__init__(
size=size, outlineSize=outlineSize, font=font,
color=color, outlineColor=outlineColor, screenCoords=screenCoords)
def updateTexture(self):
self._texture = refs.XDL_GetNumberDict(
self.font, self.size, self.color,
self.outlineSize, self.outlineColor)
refs.XDL_QueryTexture(self._texture, self._w, self._h)
self.w = self._w[0]
self.h = self._h[0]
def draw(self, num, x, y, anchorX=0, anchorY=0):
if self.size <= 0:
return
if self._dirty:
self.updateTexture()
self._dirty = False
if self._texture <= 0:
return
_nw = ffi.new('int *')
_nh = ffi.new('int *')
refs.XDL_SizeFromNumberDict(self._texture, num, _nw, _nh)
nw = _nw[0]
nh = _nh[0]
if not self.screenCoords:
x /= refs.scaleX
y /= refs.scaleY
x -= anchorX * nw
y -= anchorY * nh
cw = refs.canvasW_[0]
ch = refs.canvasH_[0]
refs.canvasW_[0] = refs.windowW
refs.canvasH_[0] = refs.windowH
refs.XDL_DrawFromNumberDict(
self._texture, num, nw, nh, round(x), round(y), nw, nh)
refs.canvasW_[0] = cw
refs.canvasH_[0] = ch
def __del__(self):
pass
# functions ##################################################################
def loadGLFunctions():
for name in GLFUNCTIONS:
refs[name] = ffi.cast(
'p' + name, lib.SDL_GL_GetProcAddress(bytes(name, 'utf-8')))
def loadMipmaps(pname, sheet):
if GLFUNCTIONS[0] not in refs:
loadGLFunctions()
maxlevel = refs.config.getint('general', 'mipmap_maxlevel', fallback=0)
if maxlevel < 1:
return
for level in range(1, maxlevel + 1):
fname = 'data/texture/{}.m{}.{}.png'.format(pname, level, sheet)
try:
img = Image.open(fname)
except IOError:
logging.error('could not load: "{}"'.format(fname))
return
pixels = ffi.from_buffer(img.tobytes())
refs.glTexImage2D(
GL_TEXTURE_2D, level, 4, img.width, img.height,
0, GL_RGBA, GL_UNSIGNED_BYTE, pixels)
logging.debug('pack {} sheet {} level {} err {}'.format(
pname, sheet, level, refs.glGetError()))
refs.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, maxlevel)
refs.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_LINEAR)
def updateState():
'''make commonly used data more accessible to plugins'''
if GLFUNCTIONS[0] not in refs:
loadGLFunctions()
if refs.stage[0] == ffi.NULL:
return
# top level children of stage
tops = []
types = []
for t in vec2list(refs.stage[0].asUIElementContainer.children):
clname = getClassName(t)
types.append(clname)
if clname in STRUCTTYPES:
t = ffi.cast('struct {} *'.format(clname), t)
tops.append(t)
refs.tops = tops
refs.topTypes = types
# main menu
if types[0] == 'MainMenu':
refs.MainMenu = tops[0]
else:
refs.MainMenu = ffi.NULL
# game client
refs.GameClient = refs.WorldClient = refs.ClientWorld =\
refs.WorldView = ffi.NULL
if types[0] == 'GameClient':
refs.GameClient = tops[0]
refs.WorldClient = refs.GameClient.worldClient
if refs.WorldClient != ffi.NULL:
refs.WorldView = refs.WorldClient.worldView
refs.ClientWorld = refs.WorldClient.clientWorld
if refs.ClientWorld == ffi.NULL or refs.WorldView == ffi.NULL:
refs.ClientWorld = refs.WorldView = ffi.NULL
# window size and scale
ww_ = ffi.new('int *')
wh_ = ffi.new('int *')
lib.SDL_GetWindowSize(refs.window_[0], ww_, wh_)
ww = ww_[0] or 1 # avoid potential division by zero
wh = wh_[0] or 1
refs.windowW = ww
refs.windowH = wh
refs.scaleX = refs.canvasW_[0] / ww
refs.scaleY = refs.canvasH_[0] / wh
# add more useful things here
def getstr(stdstring):
if stdstring == ffi.NULL:
return '(NULL)'
return ffi.string(stdstring.s, 1000).decode('utf-8', errors='replace')
def veclen(vector, itemtype='void*'):
if vector.start == ffi.NULL or vector.finish == ffi.NULL:
return 0
return (vector.finish - vector.start) // ffi.sizeof(itemtype)
def vec2list(vector, itemtype='void*'):
'''std::vector -> list'''
if vector.start == ffi.NULL or vector.finish == ffi.NULL or\
vector.endOfStorage <= vector.start:
return []
n = (vector.finish - vector.start) // ffi.sizeof(itemtype)
return ffi.unpack(ffi.cast(itemtype + '*', vector.start), n)
def sVecMap2list(svecmap, itemtype='void*'):
'''struct SortedVecMap -> list'''
lst = vec2list(svecmap.vec, 'struct SortedVecElement')
for i in range(len(lst)):
lst[i] = ffi.cast(itemtype, lst[i].obj)
return lst
def worldobjects(subworld):
'''get objects from a member of subclass of SubWorldImpl as a list'''
if subworld == ffi.NULL:
return []
return sVecMap2list(subworld.asSubWorldImpl.objs, 'struct WorldObject *')
def getClassName(obj):
'''
class name of a C++ object (assuming gcc memory layout).
doesn't demangle complicated names
'''
if obj == ffi.NULL:
return 'NULL'
# class pointer is always at [0]
classptr = ffi.cast('void****', obj)[0]
# vtable (1 up) -> type -> name (1 down)
nameptr = classptr[-1][1]
cname = ffi.string(ffi.cast('char*', nameptr), 100)
return cname[1 if len(cname) < 11 else 2:].decode()
def firstChild(obj):
if obj == ffi.NULL:
return ffi.NULL
cv = ffi.cast('struct UIElementContainer *', obj).children
if cv.finish == cv.start or cv.endOfStorage <= cv.start:
return ffi.NULL
return ffi.cast('struct UIElement **', cv.start)[0]
def getUITree(obj, depth=0):
'''printable UI element tree starting from obj'''
if obj == ffi.NULL:
return ' ' * depth + 'NULL'
cname = getClassName(obj)
uiel = ffi.cast('struct UIElement*', obj)
res = ' ' * depth + '{0} ({1.x}, {1.y}) {1.w}x{1.h}'.format(cname, uiel)
if cname in refs.CASTABLE['UIElementContainer']:
uiec = ffi.cast('struct UIElementContainer*', obj)
for elem in vec2list(uiec.children):
res += '\n' + getUITree(elem, depth + 2)
return res