forked from StarryPy/StarryPy3k
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.py
474 lines (387 loc) · 12.3 KB
/
utilities.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
"""
StarryPy Utilities
Provides a collection of commonly used utility objects, functions and classes
that can be utilized. Boilerplate = bad.
Original authors: AMorporkian
Updated for release: kharidiron
"""
import asyncio
import collections
import io
import re
import zlib
import dbm
from enum import IntEnum
from pathlib import Path
from types import FunctionType
from shelve import Shelf, _ClosedDict
from pickle import Pickler, Unpickler
path = Path(__file__).parent
# Enums
class State(IntEnum):
DISCONNECTED = 0
VERSION_SENT = 1
CLIENT_CONNECT_RECEIVED = 2
HANDSHAKE_CHALLENGE_SENT = 3
HANDSHAKE_RESPONSE_RECEIVED = 4
CONNECT_RESPONSE_SENT = 5
CONNECTED = 6
CONNECTED_WITH_HEARTBEAT = 7
class Direction(IntEnum):
TO_CLIENT = 0
TO_SERVER = 1
class WarpType(IntEnum):
TO_WORLD = 1
TO_PLAYER = 2
TO_ALIAS = 3
class WarpWorldType(IntEnum):
CELESTIAL_WORLD = 1
PLAYER_WORLD = 2
UNIQUE_WORLD = 3
MISSION_WORLD = 4
class WarpAliasType(IntEnum):
RETURN = 0
ORBITED = 1
SHIP = 2
class ChatSendMode(IntEnum):
UNIVERSE = 0
LOCAL = 1
PARTY = 2
class ChatReceiveMode(IntEnum):
LOCAL = 0
PARTY = 1
BROADCAST = 2
WHISPER = 3
COMMAND_RESULT = 4
RADIO_MESSAGE = 5
WORLD = 6
class SystemLocationType(IntEnum):
SYSTEM = 0
COORDINATE = 1
ORBIT = 2
UUID = 3
LOCATION = 4
class DamageType(IntEnum):
NO_DAMAGE = 0 # Assumed
DAMAGE = 1
IGNORES_DEF = 2
KNOCKBACK = 3
ENVIRONMENT = 4
class DamageHitType(IntEnum):
NORMAL = 0
STRONG = 1
WEAK = 2
SHIELD = 3
KILL = 4
class EntityInteractionType(IntEnum):
NOMINAL = 0
OPEN_CONTAINER_UI = 1
GO_PRONE = 2
OPEN_CRAFTING_UI = 3
OPEN_NPC_UI = 6
OPEN_SAIL_UI = 7
OPEN_TELEPORTER_UI = 8
OPEN_SCRIPTED_UI = 10
OPEN_SPECIAL_UI = 11
OPEN_CREW_UI = 12
class EntitySpawnType(IntEnum):
PLANT = 0
OBJECT = 1
VEHICLE = 2
ITEM_DROP = 3
PLANT_DROP = 4
PROJECTILE = 5
STAGEHAND = 6
MONSTER = 7
NPC = 8
PLAYER = 9
# Useful things
def recursive_dictionary_update(d, u):
"""
Given two dictionaries, update the first one with new values provided by
the second. Works for nested dictionary sets.
:param d: First Dictionary, to base off of.
:param u: Second Dictionary, to provide updated values.
:return: Dictionary. Merged dictionary with bias towards the second.
"""
for k, v in u.items():
if isinstance(v, collections.Mapping):
r = recursive_dictionary_update(d.get(k, {}), v)
d[k] = r
else:
d[k] = u[k]
return d
class DotDict(dict):
"""
Custom dictionary format that allows member access by using dot notation:
eg - dict.key.subkey
"""
def __init__(self, d, **kwargs):
super().__init__(**kwargs)
for k, v in d.items():
if isinstance(v, collections.Mapping):
v = DotDict(v)
self[k] = v
def __getattr__(self, item):
try:
return super().__getitem__(item)
except KeyError as e:
raise AttributeError(str(e)) from None
def __setattr__(self, key, value):
if isinstance(value, collections.Mapping):
value = DotDict(value)
super().__setitem__(key, value)
__delattr__ = dict.__delitem__
@asyncio.coroutine
def detect_overrides(cls, obj):
"""
For each active plugin, check if it wield a packet hook. If it does, add
make a not of it. Hand back all hooks for a specific packet type when done.
"""
res = set()
for key, value in cls.__dict__.items():
if isinstance(value, classmethod):
value = getattr(cls, key).__func__
if isinstance(value, (FunctionType, classmethod)):
meth = getattr(obj, key)
if meth.__func__ is not value:
res.add(key)
return res
class BiDict(dict):
"""
A case-insensitive bidirectional dictionary that supports integers.
"""
def __init__(self, d, **kwargs):
super().__init__(**kwargs)
for k, v in d.items():
self[k] = v
def __setitem__(self, key, value):
if key in self:
del self[key]
if value in self:
del self[value]
super().__setitem__(str(key), str(value))
super().__setitem__(str(value), str(key))
def __getitem__(self, item):
if isinstance(item, int):
key = str(item)
else:
key = item
res = super().__getitem__(key)
if res.isdigit():
res = int(res)
return res
def __delitem__(self, key):
super().__delitem__(self[key])
super().__delitem__(key)
class AsyncBytesIO(io.BytesIO):
"""
This class just wraps a normal BytesIO.read() in a coroutine to make it
easier to interface with functions designed to work on coroutines without
having to monkey around with a type check and extra futures.
"""
@asyncio.coroutine
def read(self, *args, **kwargs):
return super().read(*args, **kwargs)
class Cupboard(Shelf):
"""
Custom Shelf implementation that only pickles values at save-time.
Increases save/load times, decreases get/set item times.
More suitable for use as a savable dictionary.
"""
def __init__(self, filename, flag='c', protocol=None, keyencoding='utf-8'):
self.db = filename
self.flag = flag
self.dict = {}
with dbm.open(self.db, self.flag) as db:
for k in db.keys():
v = io.BytesIO(db[k])
self.dict[k] = Unpickler(v).load()
Shelf.__init__(self, self.dict, protocol, False, keyencoding)
def __getitem__(self, key):
return self.dict[key.encode(self.keyencoding)]
def __setitem__(self, key, value):
self.dict[key.encode(self.keyencoding)] = value
def __delitem__(self, key):
del self.dict[key.encode(self.keyencoding)]
def sync(self):
res = {}
with dbm.open(self.db, self.flag) as db:
for k, v in self.dict.items():
f = io.BytesIO()
p = Pickler(f, protocol=self._protocol)
p.dump(v)
db[k] = f.getvalue()
try:
db.sync()
except AttributeError:
pass
def close(self):
try:
self.sync()
finally:
try:
self.dict = _ClosedDict()
except:
self.dict = None
@asyncio.coroutine
def read_vlq(bytestream):
"""
Give a bytestream, extract the leading Variable Length Quantity (VLQ).
We have to do this as a stream, since we don't know how long a VLQ is
until we observe its end.
"""
d = b""
v = 0
while True:
tmp = yield from bytestream.readexactly(1)
d += tmp
tmp = ord(tmp)
v <<= 7
v |= tmp & 0x7f
if tmp & 0x80 == 0:
break
return v, d
@asyncio.coroutine
def read_signed_vlq(reader):
"""
Manipulate the read-in VLQ to account for signed-ness.
"""
v, d = yield from read_vlq(reader)
if (v & 1) == 0x00:
return v >> 1, d
else:
return -((v >> 1) + 1), d
def extractor(*args):
"""
Extracts quoted arguments and puts them as a single argument in the
passed iterator.
"""
# It's not elegant, but it's the best way to do it as far as I can tell.
# My regex-fu isn't strong though, so if someone can come up with a
# better way, great.
x = re.split(r"(?:([^\"]\S*)|\"(.+?)(?<!\\)\")\s*", " ".join(*args))
x = [word.replace("\\\"", "\"") if word is not None else None for word in x]
return [x for x in filter(None, x)]
@asyncio.coroutine
def read_packet(reader, direction):
"""
Given an interface to read from (reader) read the next packet that comes
in. Determine the packet's type, decode its contents, and track the
direction it is flowing. Store this all in a packet object, and return it
for further processing down the line.
:param reader: Stream from which to read the packet.
:param direction: Destination for the packet (SERVER or CLIENT).
:return: Dictionary. Contains both raw and decoded versions of the packet.
"""
p = {}
compressed = False
packet_type = (yield from reader.readexactly(1))
packet_size, packet_size_data = yield from read_signed_vlq(reader)
if packet_size < 0:
packet_size = abs(packet_size)
compressed = True
data = yield from reader.readexactly(packet_size)
p['type'] = ord(packet_type)
p['size'] = packet_size
p['compressed'] = compressed
if not compressed:
p['data'] = data
else:
try:
zobj = zlib.decompressobj()
p['data'] = zobj.decompress(data)
except zlib.error as e:
raise asyncio.IncompleteReadError
p['original_data'] = packet_type + packet_size_data + data
p['direction'] = direction
return p
def get_syntax(command, fn, command_prefix):
"""
Read back the syntax argument provided in a command's wrapper. Return it
in a printable format.
:param command: Command being called.
:param fn: Function which the command is wrapped around.
:param command_prefix: Prefix used for commands in chat.
:return: String. Syntax details of the target command.
"""
return "Syntax: {}{} {!s}".format(
command_prefix,
command,
fn.syntax)
def send_message(connection, *messages, **kwargs):
"""
Sends a message to a connected player.
:param connection: The connection to send the message to.
:param messages: The message(s) to send.
:return: A Future for the message(s) being sent.
"""
return asyncio.ensure_future(connection.send_message(*messages, **kwargs))
def broadcast(connection, *messages, **kwargs):
"""
Sends a message to all connected players.
:param connection: The connection from which the message came.
:param messages: The message(s) to send.
:return: A Future for the message(s) being sent.
"""
return asyncio.ensure_future(
connection.factory.broadcast(*messages, **kwargs))
def link_plugin_if_available(self, plugin):
if plugin in self.factory.plugin_manager.list_plugins().keys():
self.logger.debug("{} available.".format(plugin))
self.plugins[plugin] = \
self.factory.plugin_manager.list_plugins()[plugin]
return True
else:
return False
class Command:
"""
Defines a decorator that encapsulates a chat command. Provides a common
interface for all commands, including roles, documentation, usage syntax,
and aliases.
"""
def __init__(self, *aliases, role=None, roles=None, perm=None, doc=None,
syntax=None, priority=0):
if syntax is None:
syntax = ()
if isinstance(syntax, str):
syntax = (syntax,)
if doc is None:
doc = ""
self.perm = perm
self.syntax = syntax
self.human_syntax = " ".join(syntax)
self.doc = doc
self.aliases = aliases
self.priority = priority
def __call__(self, f):
"""
Whenever a command is called, its handling gets done here.
:param f: The function the Command decorator is wrapping.
:return: The now-wrapped command, with all the trappings.
"""
def wrapped(s, data, connection):
try:
if self.perm is not None:
if not connection.player.perm_check(self.perm):
raise PermissionError
return f(s, data, connection)
except PermissionError:
send_message(connection,
"You don't have permissions to do that.")
wrapped._command = True
wrapped._aliases = self.aliases
wrapped.__doc__ = self.doc
wrapped.perm = self.perm
wrapped.syntax = self.human_syntax
wrapped.priority = self.priority
# f.roles = self.roles
# f.syntax = self.human_syntax
# f.__doc__ = self.doc
return wrapped
class StorageMixin:
"""
Convenience class for adding access to a player's server-based storage.
"""
def __init__(self):
self.storage = self.plugins.player_manager.get_storage(self)