forked from keysmashes/edmcoverlay2
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathedmcoverlay.py
More file actions
283 lines (249 loc) · 7.05 KB
/
Copy pathedmcoverlay.py
File metadata and controls
283 lines (249 loc) · 7.05 KB
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
import queue
import threading
from _logger import logger
import secrets
import inspect
import json
import _config_vars
from typing import Optional
from persistent_socket import PersistentSocket
SERVER_IP = "127.0.0.1"
SERVER_PORT = 5010
_SHUTDOWN_SIGNAL = object()
class OverlayImpl:
"""Implementation of communication with binary overlay server.
Note, it is NOT singltone so each plugin will have own copy.
If plugin breaks protocol, it will be disconnected just
while others remain connected.
"""
_config: Optional[_config_vars.ConfigVars] = None
def __init__(
self,
server: str = SERVER_IP,
port: int = SERVER_PORT,
):
self._host = server
self._port = port
self._connection = PersistentSocket(server, port)
self._queue = queue.Queue()
self._worker_thread = threading.Thread(
target=self._send_worker,
name=f"OverlayWorker-{secrets.token_hex(2)}",
daemon=True,
)
self._worker_thread.start()
@staticmethod
def setConfig(config: _config_vars.ConfigVars):
OverlayImpl._config = config
def _stop(self):
logger.info("Sending self-stop/exit request to the binary.")
self.send_command("exit")
self._queue.put(_SHUTDOWN_SIGNAL)
def _send_worker(self):
"""Background thread to send data to binary overlay."""
while True:
data = self._queue.get()
if data is _SHUTDOWN_SIGNAL:
self._connection.close()
break
try:
bstr = data.encode("utf-8")
ok = self._connection.send(str(len(bstr)).encode("utf-8") + b"#" + bstr)
if not ok:
logger.warning("Overlay Worker: Could not send data to binary.")
except Exception as e:
logger.error(f"Overlay Worker thread error: {e}")
finally:
self._queue.task_done()
def _send_raw_text(self, inpstr: str):
self._queue.put(inpstr)
return None
def _send2bin(self, owner: str, msg: dict):
if self._config is not None:
if "font_size" not in msg and "shape" not in msg and "svg" not in msg:
font = msg.get("size", "normal")
msg["font_size"] = self._config.getFontSize(owner, font)
if "vector_font_size" not in msg and "vector" in msg:
font = msg.get("size", "normal")
msg["vector_font_size"] = self._config.getFontSize(owner, font)
self._send_raw_text(json.dumps(msg))
def send_command(self, command: str):
self._send_raw_text(json.dumps({"command": command}))
def send_message(
self,
owner: str,
msgid: str,
text: str,
color: str,
x: int,
y: int,
ttl: int = 4,
size: str = "normal",
):
msg = {
"text": text,
"color": color,
"x": x,
"y": y,
"ttl": ttl,
"size": size,
"id": msgid,
}
self._send2bin(owner, msg)
def send_shape(
self,
owner: str,
shapeid: str,
shape: str,
color: str,
fill: str,
x: int,
y: int,
w: int,
h: int,
ttl: int,
):
msg = {
"shape": shape,
"color": color,
"fill": fill,
"x": x,
"y": y,
"w": w,
"h": h,
"ttl": ttl,
"id": shapeid,
}
self._send2bin(owner, msg)
def send_svg(
self,
owner: str,
svgid: str,
svg: str,
css: str,
font_file: str,
x: int,
y: int,
ttl: int,
):
msg = {
"svg": svg,
"css": css,
"x": x,
"y": y,
"ttl": ttl,
"id": svgid,
"font_file": font_file,
}
self._send2bin(owner, msg)
def connect(self):
return self._connection.connect()
class Overlay:
"""Main class to communicated with binary overlay.
Plugin must create a copy of this class to draw overlay messages.
Each new copy will establish new TCP connection.
"""
__caller_path: str = ""
__token: str = ""
def __init__(self) -> None:
self.__token = secrets.token_hex(4)
self.__overlay = OverlayImpl()
callFrames = inspect.getouterframes(inspect.currentframe())
self.__caller_path = callFrames[1].filename
logger.debug('\tOverlay() is created for plugin: "%s"\n', self.__caller_path)
def send_raw(self, msg: dict):
if "msgid" in msg:
msg["msgid"] = self.__token + str(msg["msgid"])
if "shapeid" in msg:
msg["shapeid"] = self.__token + str(msg["shapeid"])
if "svgid" in msg:
msg["svgid"] = self.__token + str(msg["svgid"])
if "id" in msg:
msg["id"] = self.__token + msg["id"]
return self.__overlay._send2bin(self.__caller_path, msg)
def send_command(self, command: str):
self.__overlay.send_command(command)
def send_message(
self,
msgid: str,
text: str,
color: str,
x: int,
y: int,
ttl: int = 4,
size="normal",
):
return self.__overlay.send_message(
self.__caller_path,
self.__token + msgid,
text,
color,
x,
y,
ttl=ttl,
size=size,
)
def send_shape(
self,
shapeid: str,
shape: str,
color: str,
fill: str,
x: int,
y: int,
w: int,
h: int,
ttl: int,
):
return self.__overlay.send_shape(
owner=self.__caller_path,
shapeid=self.__token + shapeid,
shape=shape,
color=color,
fill=fill,
x=x,
y=y,
w=w,
h=h,
ttl=ttl,
)
def send_svg(
self,
svgid: str,
svg: str,
x: int,
y: int,
ttl: int,
css: str = "",
font_file: str = "",
):
return self.__overlay.send_svg(
owner=self.__caller_path,
svgid=self.__token + svgid,
svg=svg,
css=css,
font_file=font_file,
x=x,
y=y,
ttl=ttl,
)
def connect(self) -> bool:
return self.__overlay.connect()
def is_multiline_supported(self) -> bool:
"""
Returns:
bool: true if this plugin can draw multilined text direct, separated with \n.
"""
return True
def is_svg_supported(self) -> bool:
"""
Returns:
bool: true if this plugin can render SVG direct.
"""
return True
def is_emoji_supported(self) -> bool:
"""
Returns:
bool: true if this plugin can render emoji in texts.
"""
return True