-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsync.py
172 lines (124 loc) · 5.05 KB
/
sync.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
# Copyright (c) 2021 lampysprites
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from __future__ import annotations
from aiohttp.web_ws import WebSocketResponse
import bpy
import asyncio
import aiohttp
from aiohttp import web
from time import time
from . import async_loop
from . import util
from .messaging import encode
from .addon import addon
class Server():
def __init__(self, host="", port=0):
self.host = host
self.port = port
self._ws = None
self._server = None
self._site = None
self._start_time = 0
def send(self, msg, binary=True):
if self._ws is not None:
if binary:
asyncio.ensure_future(self._ws.send_bytes(msg, False))
else:
asyncio.ensure_future(self._ws.send_str(msg, False))
@property
def connected(self):
return self._ws is not None and not self._ws.closed
def start(self):
started = False
self._start_time = int(time())
async def _start_a(self):
nonlocal started
self._server = web.Server(self._receive)
runner = web.ServerRunner(self._server)
await runner.setup()
self._site = web.TCPSite(runner, self.host, self.port)
await self._site.start()
started = True
async_loop.ensure_async_loop()
stop = asyncio.wait_for(_start_a(self), timeout=5.0)
try:
asyncio.get_event_loop().run_until_complete(stop)
util.refresh()
except asyncio.TimeoutError:
raise RuntimeError(f"Could not start server at {self.host}:{self.port}")
def stop(self):
async def _stop_a():
if self._ws is not None: # no connections happened
await self._ws.close()
await self._site.stop()
await self._site._runner.cleanup()
await self._server.shutdown()
asyncio.ensure_future(_stop_a())
async_loop.erase_async_loop()
util.refresh()
async def _receive(self, request) -> WebSocketResponse:
self._ws = web.WebSocketResponse(max_msg_size=0)
await self._ws.prepare(request)
# client connected
imgs = tuple(util.image_name(img) for img in bpy.data.images)
await self._ws.send_bytes(encode.texture_list(imgs), False)
bpy.ops.pribambase.report(message_type='INFO', message="Aseprite connected")
util.refresh()
async for msg in self._ws:
if msg.type == aiohttp.WSMsgType.BINARY:
await addon.handlers.process(msg.data)
elif msg.type == aiohttp.WSMsgType.ERROR:
bpy.ops.pribambase.report(message_type='ERROR', message=f"Connection closed with exception {self._ws.exception()}")
# client disconnected
bpy.ops.pribambase.report(message_type='INFO', message="Aseprite disconnected")
util.refresh()
return self._ws
class SB_OT_serv_start(bpy.types.Operator):
bl_idname = "pribambase.start_server"
bl_label = "Open Connection"
bl_description = "Begin accepting connections from Aseprite"
@classmethod
def poll(self, ctx):
return not addon.server_up
def execute(self, context):
addon.start_server()
return {'FINISHED'}
class SB_OT_serv_stop(bpy.types.Operator):
bl_idname = "pribambase.stop_server"
bl_label = "Close Connection"
bl_description = "Shut down Aseprite link"
@classmethod
def poll(self, ctx):
return addon.server_up
def execute(self, context):
addon.stop_server()
return {"FINISHED"}
class SB_OT_texture_list(bpy.types.Operator):
bl_idname = "pribambase.texture_list"
bl_label = "Update Texture List"
bl_description = "Update Aseprite about which textures are used in the blendfile"
@classmethod
def poll(self, ctx):
return addon.server_up
def execute(self, context):
images = (util.image_name(img) for img in bpy.data.images)
msg = encode.texture_list(images)
addon.server.send(msg)
return {'FINISHED'}