-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMemoryWatcher.py
143 lines (107 loc) · 3.85 KB
/
MemoryWatcher.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
import binascii
import zmq
import socket
import os
import platform
def chunk(l, n):
return [l[i:i + n] for i in range(0, len(l), n)]
def parseMessage(message):
lines = message.splitlines()
assert (len(lines) % 2 == 0)
diffs = chunk(lines, 2)
for diff in diffs:
diff[1] = binascii.unhexlify(diff[1].zfill(8))
return diffs
class MemoryWatcherZMQ:
"""Reads and parses game memory changes.
Pass the location of the socket to the constructor, then either manually
call next() on this class to get a single change, or else use it like a
normal iterator.
"""
def __init__(self, path, _id):
self.path = path
self.id = _id
self.messages = None
self.exited = False
self.windows = platform.system() == 'Windows'
#write_with_folder(self.path, '5555')
context = zmq.Context()
self.alert_socket = context.socket(zmq.PUSH)
self.alert_socket.connect("tcp://127.0.0.1:7555")
if self.windows:
self.socket = context.socket(zmq.PULL)
self.socket.bind("tcp://127.0.0.1:%d" % 5555)
else :
self.socket = context.socket(zmq.REP)
self.socket.bind("ipc://" + path)
self.socket.setsockopt(zmq.RCVTIMEO, 10000)
self.socket.setsockopt(zmq.LINGER, 0)
def __exit__(self, *args):
"""Closes the socket."""
pass
def unbind(self):
self.socket.bind("ipc://" + self.path)
def __iter__(self):
"""Iterate over this class in the usual way to get memory changes."""
return self
def __next__(self):
"""Returns the next (address, value) tuple, or None on timeout.
address is the string provided by dolphin, set in Locations.txt.
value is a four-byte string suitable for interpretation with struct.
"""
return self.get_messages()
def get_messages(self):
if self.messages is None:
try:
message = self.socket.recv()
message = message.decode('utf-8')
self.messages = parseMessage(message)
except zmq.ZMQError as e:
if not self.exited:
self.alert_socket.send_pyobj(self.id)
return self.messages
def advance(self):
if not self.windows:
try:
self.socket.send(b'')
except zmq.ZMQError:
pass
self.messages = None
class MemoryWatcher:
"""Reads and parses game memory changes.
Pass the location of the socket to the constructor, then either manually
call next() on this class to get a single change, or else use it like a
normal iterator.
"""
def __init__(self, path):
try:
os.unlink(path)
except OSError:
pass
self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
self.socket.settimeout(1)
self.socket.bind(path)
def __exit__(self, *args):
"""Closes the socket."""
pass
def unbind(self):
self.socket.close()
def __iter__(self):
"""Iterate over this class in the usual way to get memory changes."""
return self
def __next__(self):
"""Returns the next (address, value) tuple, or None on timeout.
address is the string provided by dolphin, set in Locations.txt.
value is a four-byte string suitable for interpretation with struct.
"""
return self.get_messages()
def get_messages(self):
try:
message = self.socket.recv(1024).decode('utf-8')
message = message.strip('\x00')
messages = parseMessage(message)
except socket.timeout:
return []
return messages
def advance(self):
pass