-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpp.py
197 lines (170 loc) · 7.39 KB
/
mpp.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
# Copyright 2017 Adam Beckmeyer
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
import sys
from gyr import server, matrix_objects, exceptions
from pyvirtualdisplay.smartdisplay import SmartDisplay
from easyprocess import EasyProcess
import time
import json
from io import BytesIO
handler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter("%(asctime)s : %(levelname)s : %(name)s : %(message)s")
handler.setFormatter(formatter)
logger = logging.getLogger()
logger.addHandler(handler)
logger.setLevel(logging.INFO)
# Must start display before we can use Xlib via pynput
disp = SmartDisplay(visible=False, size=(240, 160))
disp.start()
logger.info("xvfb display started")
from pynput.keyboard import Key, Controller
logger.info("pynput imported and working")
# Have to start pulse here because mgba is being a weenie
pulse = EasyProcess("pulseaudio")
pulse.start()
logger.info("pulseaudio started")
with open("config.json") as f:
config = json.load(f)
if config["debug"]:
logger.setLevel(logging.DEBUG)
application = server.Application(config["hs_address"], config["token"])
class MPPServer:
def __init__(self, application, config):
self.config = config
self.config["room_alias"] = ("#" + self.config["local_room_alias"] +
":" + self.config["hs_name"])
self.config["user_id"] = ("@" + self.config["local_user_id"] +
":" + self.config["hs_name"])
self.disp = disp
self.keyboard = Controller()
self.api = application.Api()
try:
# when creating a room with a room alias, only local part of alias is used
self.room_id = self.api.create_room(alias=self.config["local_room_alias"],
is_public=True)
logger.info("new room created: " + self.room_id)
except exceptions.MatrixError:
# if it already exists just get the room_id
self.room_id = self.api.get_room_id(self.config["room_alias"])
logger.info("using existing room: " + self.room_id)
self._start_mgba()
def _start_mgba(self):
self.pkmn = EasyProcess("mgba -s 2 -b " +
self.config["bios_location"] + " " +
self.config["rom_location"])
self.pkmn.start()
logger.info("mgba started")
self.ts = time.time()
self.save_timers = [[index, time.time(), l, f] for index, (l, f) in enumerate(((50, Key.f1), (1000, Key.f2), (10000, Key.f3), (50000, Key.f4), (100000, Key.f5)))]
time.sleep(5)
self._load()
self.send_screenshot()
def __del__(self):
self.pkmn.stop()
self.disp.stop()
def send_screenshot(self):
if self.ts + 2 < time.time():
self._send()
self.ts = time.time()
def _send(self):
img = self.disp.grab()
f = BytesIO()
try:
img.save(f, format="JPEG", quality=50, optimize=True)
mxc = self.api.media_upload(f.getvalue(), "image/jpeg")["content_uri"]
file_name = str(int(self.ts)) + ".jpg"
self.api.send_content(self.room_id, mxc, file_name, "m.image")
logger.debug("sent screenshot to " + self.room_id)
except AttributeError:
self.api.send_notice(self.room_id, "Error in capturing screenshot")
logger.error("could not capture screenshot from display")
def _save(self):
for i, t, l, f in self.save_timers:
if t + l < time.time():
with self.keyboard.pressed(Key.shift):
self._press_key(f)
logger.info("saved state to " + str(f) + " after " + str(l) + " seconds")
self.save_timers[i][1] = time.time()
def _load(self):
self._press_key(Key.f1)
logger.debug("loaded gamestate from f1")
def room_handler(self, room_alias):
# Only room created is #matrixplayspokemon
logger.info("homeserver asked for " + room_alias)
return room_alias == self.config["room_alias"]
def user_handler(self, mxid):
# Only user is as_user.mxid
logger.info("homeserver asked for " + mxid)
return mxid == self.config["user_id"]
def transaction_handler(self, event_stream):
for event in event_stream:
if (event.id == self.room_id and
event.type == "m.room.message" and
event.content["msgtype"] == "m.text"):
content = event.content["body"].lower()
if content == "a":
self._press_key("x")
elif content == "b":
self._press_key("z")
elif content == "l":
self._press_key("a")
elif content == "r":
self._press_key("s")
elif content == "up":
self._press_key(Key.up)
elif content == "down":
self._press_key(Key.down)
elif content == "left":
self._press_key(Key.left)
elif content == "right":
self._press_key(Key.right)
elif content == "start":
self._press_key(Key.enter)
elif content == "select":
self._press_key(Key.backspace)
elif content == "dump" and self.config["debug"]:
logger.debug("received dump command")
self._dump()
elif content == "save" and self.config["debug"]:
logger.debug("received save command")
with self.keyboard.pressed(Key.shift):
self._press_key(Key.f1)
logger.debug("saved current gamestate to f1")
elif content == "load" and self.config["debug"]:
logger.debug("received load command")
self._load()
logger.debug("handled " + event.type +
#" from " + event.mxid +
" in " + event.id)
self.send_screenshot()
self._save()
return True
def _press_key(self, key):
self.keyboard.press(key)
# make sure the key is pressed long enought to register with mgba
time.sleep(0.05)
self.keyboard.release(key)
logger.debug("pressed key: " + str(key))
def _dump(self):
self.pkmn.stop()
logger.warning("mgba stdout: " + self.pkmn.stdout)
logger.warning("mgba stderr: " + self.pkmn.stderr)
self._start_mgba()
mpp = MPPServer(application, config)
logger.info("setup complete")
application.add_handlers(room_handler=mpp.room_handler,
transaction_handler=mpp.transaction_handler,
user_handler=mpp.user_handler,)