api.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. import zmq
  2. from collections import namedtuple
  3. from . import serial, exc
  4. class Expr(str):
  5. """String rendering of an expr-bound property value, sent over netdebug.
  6. Subclasses str so it formats naturally, but stays isinstance-distinct
  7. from plain str property values."""
  8. pass
  9. Property = namedtuple("Property", [
  10. "name",
  11. "type",
  12. "subtype",
  13. #"defaults",
  14. "ui_name",
  15. "desc",
  16. "is_null_allowed",
  17. "is_expr_allowed",
  18. "array_len",
  19. "min_val",
  20. "max_val",
  21. "enum_items",
  22. "depends"
  23. ])
  24. class Command:
  25. HELLO = 0
  26. ADD_NODE = 1
  27. REMOVE_NODE = 9
  28. RENAME_NODE = 23
  29. SCAN_DANGLING = 24
  30. LOOKUP_NODE_ID = 12
  31. ADD_PROPERTY = 11
  32. LINK_NODE = 2
  33. UNLINK_NODE = 8
  34. GET_INFO = 19
  35. GET_CHILDREN = 4
  36. GET_PARENTS = 5
  37. GET_PROPERTIES = 3
  38. GET_PROPERTY_VALUE = 6
  39. SET_PROPERTY_VALUE = 7
  40. GET_SIGNALS = 14
  41. REGISTER_SLOT = 15
  42. UNREGISTER_SLOT = 16
  43. LOOKUP_SLOT_ID = 17
  44. GET_SLOTS = 18
  45. GET_METHODS = 20
  46. GET_METHOD = 21
  47. CALL_METHOD = 22
  48. class SceneNodeType:
  49. NULL = 0
  50. ROOT = 1
  51. WINDOW = 2
  52. WINDOW_INPUT = 3
  53. KEYBOARD = 4
  54. MOUSE = 5
  55. LAYER = 6
  56. OBJECT = 7
  57. VECTOR_ART = 8
  58. TEXT = 9
  59. TEXTURE = 10
  60. FONTS = 11
  61. FONT = 12
  62. EDIT = 14
  63. IMAGE = 15
  64. BUTTON = 16
  65. SHORTCUT = 17
  66. GESTURE = 18
  67. EMOJI_PICKER = 19
  68. SETTING = 21
  69. MENU = 22
  70. TOKEN_TABLE = 23
  71. TEXT_SCRAMBLE = 24
  72. CHAT_VIEW = 25
  73. PRIV_MSG_NODE = 26
  74. DATE_MSG_NODE = 27
  75. FILE_MSG_NODE = 28
  76. SLIDER = 29
  77. DROPDOWN = 30
  78. PLUGIN_ROOT = 100
  79. PLUGIN = 101
  80. NODE_TYPE_NAMES = {
  81. getattr(SceneNodeType, name): name.lower()
  82. for name in dir(SceneNodeType)
  83. if name.isupper()
  84. }
  85. class PropertyType:
  86. NULL = 0
  87. BOOL = 1
  88. UINT32 = 2
  89. FLOAT32 = 3
  90. STR = 4
  91. ENUM = 5
  92. SCENE_NODE_ID = 7
  93. SEXPR = 8
  94. VECTOR_SHAPE = 9
  95. @staticmethod
  96. def to_str(prop_type):
  97. match prop_type:
  98. case PropertyType.NULL:
  99. return "null"
  100. case PropertyType.BOOL:
  101. return "bool"
  102. case PropertyType.UINT32:
  103. return "uint32"
  104. case PropertyType.FLOAT32:
  105. return "float32"
  106. case PropertyType.STR:
  107. return "str"
  108. case PropertyType.ENUM:
  109. return "enum"
  110. case PropertyType.SCENE_NODE_ID:
  111. return "scene_node_id"
  112. case PropertyType.SEXPR:
  113. return "sexpr"
  114. case PropertyType.VECTOR_SHAPE:
  115. return "vector_shape"
  116. class PropertySubType:
  117. NULL = 0
  118. COLOR = 1
  119. PIXEL = 2
  120. RESOURCE_ID = 3
  121. LOCALE = 4
  122. FLAG = 5
  123. @staticmethod
  124. def to_str(prop_type):
  125. match prop_type:
  126. case PropertySubType.NULL:
  127. return "null"
  128. case PropertySubType.COLOR:
  129. return "color"
  130. case PropertySubType.PIXEL:
  131. return "pixel"
  132. case PropertySubType.RESOURCE_ID:
  133. return "resource_id"
  134. case PropertySubType.LOCALE:
  135. return "locale"
  136. case PropertySubType.FLAG:
  137. return "flag"
  138. class CallArgType:
  139. UINT32 = 0
  140. UINT64 = 1
  141. FLOAT32 = 2
  142. BOOL = 3
  143. STR = 4
  144. HASH = 5
  145. @staticmethod
  146. def to_str(arg_type):
  147. match arg_type:
  148. case CallArgType.UINT32:
  149. return "uint32"
  150. case CallArgType.UINT64:
  151. return "uint64"
  152. case CallArgType.FLOAT32:
  153. return "float32"
  154. case CallArgType.BOOL:
  155. return "bool"
  156. case CallArgType.STR:
  157. return "str"
  158. case CallArgType.HASH:
  159. return "hash"
  160. case _:
  161. return "unknown"
  162. class PropertyStatus:
  163. OK = 0
  164. UNSET = 1
  165. NULL = 2
  166. EXPR = 3
  167. class ErrorCode:
  168. INVALID_SCENE_PATH = 1
  169. NODE_NOT_FOUND = 2
  170. CHILD_NODE_NOT_FOUND = 3
  171. PARENT_NODE_NOT_FOUND = 4
  172. PROPERTY_ALREADY_EXISTS = 5
  173. PROPERTY_NOT_FOUND = 6
  174. PROPERTY_WRONG_TYPE = 7
  175. PROPERTY_WRONG_LEN = 9
  176. PROPERTY_WRONG_INDEX = 10
  177. PROPERTY_OUT_OF_RANGE = 11
  178. PROPERTY_NULL_NOT_ALLOWED = 12
  179. PROPERTY_SEXPR_NOT_ALLOWED = 13
  180. PROPERTY_IS_BOUNDED = 14
  181. PROPERTY_WRONG_ENUM_ITEM = 15
  182. SIGNAL_ALREADY_EXISTS = 16
  183. SIGNAL_NOT_FOUND = 17
  184. SLOT_NOT_FOUND = 18
  185. METHOD_ALREADY_EXISTS = 19
  186. METHOD_NOT_FOUND = 20
  187. NODES_ARE_LINKED = 21
  188. NODES_NOT_LINKED = 22
  189. NODE_HAS_PARENTS = 23
  190. NODE_HAS_CHILDREN = 24
  191. NODE_PARENT_NAME_CONFLICT = 25
  192. NODE_CHILD_NAME_CONFLICT = 26
  193. NODE_SIBLING_NAME_CONFLICT = 27
  194. SEXPR_GLOBAL_NOT_FOUND = 32
  195. PUBLISHER_DESTROYED = 34
  196. CHANNEL_CLOSED = 36
  197. NODES_ARE_SAME = 37
  198. UNEXPECTED_TOKEN = 38
  199. KVDB_ERR = 39
  200. SERVICE_FAILED = 40
  201. GFX_DUPLICATE_TEXTURE_ID = 41
  202. GFX_UNKNOWN_TEXTURE_ID = 42
  203. GFX_DUPLICATE_BUFFER_ID = 43
  204. GFX_UNKNOWN_BUFFER_ID = 44
  205. GFX_DUPLICATE_ANIM_ID = 45
  206. GFX_UNKNOWN_ANIM_ID = 46
  207. CONTACT_NOT_FOUND = 47
  208. SERIAL_ERR = 48
  209. TURSO_ERR = 49
  210. UNSUPPORTED_NODE_TYPE = 50
  211. NODE_NOT_REMOVABLE = 51
  212. @staticmethod
  213. def to_str(errc):
  214. match errc:
  215. case ErrorCode.INVALID_SCENE_PATH:
  216. return "invalid_scene_path"
  217. case ErrorCode.NODE_NOT_FOUND:
  218. return "node_not_found"
  219. case ErrorCode.CHILD_NODE_NOT_FOUND:
  220. return "child_node_not_found"
  221. case ErrorCode.PARENT_NODE_NOT_FOUND:
  222. return "parent_node_not_found"
  223. case ErrorCode.PROPERTY_ALREADY_EXISTS:
  224. return "property_already_exists"
  225. case ErrorCode.PROPERTY_NOT_FOUND:
  226. return "property_not_found"
  227. case ErrorCode.PROPERTY_WRONG_TYPE:
  228. return "property_wrong_type"
  229. case ErrorCode.PROPERTY_WRONG_LEN:
  230. return "property_wrong_len"
  231. case ErrorCode.PROPERTY_WRONG_INDEX:
  232. return "property_wrong_index"
  233. case ErrorCode.PROPERTY_OUT_OF_RANGE:
  234. return "property_out_of_range"
  235. case ErrorCode.PROPERTY_NULL_NOT_ALLOWED:
  236. return "property_null_not_allowed"
  237. case ErrorCode.PROPERTY_SEXPR_NOT_ALLOWED:
  238. return "property_sexpr_not_allowed"
  239. case ErrorCode.PROPERTY_IS_BOUNDED:
  240. return "property_is_bounded"
  241. case ErrorCode.PROPERTY_WRONG_ENUM_ITEM:
  242. return "property_wrong_enum_item"
  243. case ErrorCode.SIGNAL_ALREADY_EXISTS:
  244. return "signal_already_exists"
  245. case ErrorCode.SIGNAL_NOT_FOUND:
  246. return "signal_not_found"
  247. case ErrorCode.SLOT_NOT_FOUND:
  248. return "slot_not_found"
  249. case ErrorCode.METHOD_ALREADY_EXISTS:
  250. return "method_already_exists"
  251. case ErrorCode.METHOD_NOT_FOUND:
  252. return "method_not_found"
  253. case ErrorCode.NODES_ARE_LINKED:
  254. return "nodes_are_linked"
  255. case ErrorCode.NODES_NOT_LINKED:
  256. return "nodes_not_linked"
  257. case ErrorCode.NODE_HAS_PARENTS:
  258. return "node_has_parents"
  259. case ErrorCode.NODE_HAS_CHILDREN:
  260. return "node_has_children"
  261. case ErrorCode.NODE_PARENT_NAME_CONFLICT:
  262. return "node_parent_name_conflict"
  263. case ErrorCode.NODE_CHILD_NAME_CONFLICT:
  264. return "node_child_name_conflict"
  265. case ErrorCode.NODE_SIBLING_NAME_CONFLICT:
  266. return "node_sibling_name_conflict"
  267. case ErrorCode.SEXPR_GLOBAL_NOT_FOUND:
  268. return "sexpr_global_not_found"
  269. case ErrorCode.PUBLISHER_DESTROYED:
  270. return "publisher_destroyed"
  271. case ErrorCode.CHANNEL_CLOSED:
  272. return "channel_closed"
  273. case ErrorCode.NODES_ARE_SAME:
  274. return "nodes_are_same"
  275. case ErrorCode.UNEXPECTED_TOKEN:
  276. return "unexpected_token"
  277. case ErrorCode.KVDB_ERR:
  278. return "kvdb_err"
  279. case ErrorCode.SERVICE_FAILED:
  280. return "service_failed"
  281. case ErrorCode.GFX_DUPLICATE_TEXTURE_ID:
  282. return "gfx_duplicate_texture_id"
  283. case ErrorCode.GFX_UNKNOWN_TEXTURE_ID:
  284. return "gfx_unknown_texture_id"
  285. case ErrorCode.GFX_DUPLICATE_BUFFER_ID:
  286. return "gfx_duplicate_buffer_id"
  287. case ErrorCode.GFX_UNKNOWN_BUFFER_ID:
  288. return "gfx_unknown_buffer_id"
  289. case ErrorCode.GFX_DUPLICATE_ANIM_ID:
  290. return "gfx_duplicate_anim_id"
  291. case ErrorCode.GFX_UNKNOWN_ANIM_ID:
  292. return "gfx_unknown_anim_id"
  293. case ErrorCode.CONTACT_NOT_FOUND:
  294. return "contact_not_found"
  295. case ErrorCode.SERIAL_ERR:
  296. return "serial_err"
  297. case ErrorCode.TURSO_ERR:
  298. return "turso_err"
  299. case ErrorCode.UNSUPPORTED_NODE_TYPE:
  300. return "unsupported_node_type"
  301. case ErrorCode.NODE_NOT_REMOVABLE:
  302. return "node_not_removable"
  303. case _:
  304. return "unknown"
  305. class Api:
  306. def __init__(self, addr="127.0.0.1", port=9484):
  307. self.addr = addr
  308. self.port = port
  309. self.context = zmq.Context()
  310. self.socket = self._make_socket()
  311. def _make_socket(self):
  312. socket = self.context.socket(zmq.REQ)
  313. #self.socket.setsockopt(zmq.IPV6, True)
  314. # Fail fast with zmq.error.Again when no app is listening, so the
  315. # CLI can report the endpoint it tried instead of hanging forever.
  316. socket.setsockopt(zmq.RCVTIMEO, 3000)
  317. # Discard undelivered messages at exit instead of blocking on
  318. # context teardown when no app ever answered.
  319. socket.setsockopt(zmq.LINGER, 0)
  320. socket.connect(f"tcp://{self.addr}:{self.port}")
  321. return socket
  322. def _reset_socket(self):
  323. # A REQ socket whose reply timed out is stuck mid-request and
  324. # rejects further sends; replace it so later requests work.
  325. try:
  326. self.socket.close(linger=0)
  327. except zmq.error.ZMQError:
  328. pass
  329. self.socket = self._make_socket()
  330. def _make_request(self, cmd, payload):
  331. req_cmd = bytearray()
  332. serial.write_u8(req_cmd, cmd)
  333. try:
  334. self.socket.send_multipart([req_cmd, payload])
  335. errc, reply = self.socket.recv_multipart()
  336. except zmq.error.ZMQError:
  337. self._reset_socket()
  338. raise
  339. errc = int.from_bytes(errc, "little")
  340. cursor = serial.Cursor(reply)
  341. match errc:
  342. case 0:
  343. pass
  344. case ErrorCode.INVALID_SCENE_PATH:
  345. raise exc.InvalidScenePath
  346. case ErrorCode.NODE_NOT_FOUND:
  347. raise exc.NodeNotFound
  348. case ErrorCode.CHILD_NODE_NOT_FOUND:
  349. raise exc.ChildNodeNotFound
  350. case ErrorCode.PARENT_NODE_NOT_FOUND:
  351. raise exc.ParentNodeNotFound
  352. case ErrorCode.PROPERTY_ALREADY_EXISTS:
  353. raise exc.PropertyAlreadyExists
  354. case ErrorCode.PROPERTY_NOT_FOUND:
  355. raise exc.PropertyNotFound
  356. case ErrorCode.PROPERTY_WRONG_TYPE:
  357. raise exc.PropertyWrongType
  358. case ErrorCode.PROPERTY_WRONG_LEN:
  359. raise exc.PropertyWrongLen
  360. case ErrorCode.PROPERTY_WRONG_INDEX:
  361. raise exc.PropertyWrongIndex
  362. case ErrorCode.PROPERTY_OUT_OF_RANGE:
  363. raise exc.PropertyOutOfRange
  364. case ErrorCode.PROPERTY_NULL_NOT_ALLOWED:
  365. raise exc.PropertyNullNotAllowed
  366. case ErrorCode.PROPERTY_SEXPR_NOT_ALLOWED:
  367. raise exc.PropertySExprNotAllowed
  368. case ErrorCode.PROPERTY_IS_BOUNDED:
  369. raise exc.PropertyIsBounded
  370. case ErrorCode.PROPERTY_WRONG_ENUM_ITEM:
  371. raise exc.PropertyWrongEnumItem
  372. case ErrorCode.SIGNAL_ALREADY_EXISTS:
  373. raise exc.SignalAlreadyExists
  374. case ErrorCode.SIGNAL_NOT_FOUND:
  375. raise exc.SignalNotFound
  376. case ErrorCode.SLOT_NOT_FOUND:
  377. raise exc.SlotNotFound
  378. case ErrorCode.METHOD_ALREADY_EXISTS:
  379. raise exc.MethodAlreadyExists
  380. case ErrorCode.METHOD_NOT_FOUND:
  381. raise exc.MethodNotFound
  382. case ErrorCode.NODES_ARE_LINKED:
  383. raise exc.NodesAreLinked
  384. case ErrorCode.NODES_NOT_LINKED:
  385. raise exc.NodesNotLinked
  386. case ErrorCode.NODE_HAS_PARENTS:
  387. raise exc.NodeHasParents
  388. case ErrorCode.NODE_HAS_CHILDREN:
  389. raise exc.NodeHasChildren
  390. case ErrorCode.NODE_PARENT_NAME_CONFLICT:
  391. raise exc.NodeParentNameConflict
  392. case ErrorCode.NODE_CHILD_NAME_CONFLICT:
  393. raise exc.NodeChildNameConflict
  394. case ErrorCode.NODE_SIBLING_NAME_CONFLICT:
  395. raise exc.NodeSiblingNameConflict
  396. case ErrorCode.SEXPR_GLOBAL_NOT_FOUND:
  397. raise exc.SExprGlobalNotFound
  398. case ErrorCode.PUBLISHER_DESTROYED:
  399. raise exc.PublisherDestroyed
  400. case ErrorCode.CHANNEL_CLOSED:
  401. raise exc.ChannelClosed
  402. case ErrorCode.NODES_ARE_SAME:
  403. raise exc.NodesAreSame
  404. case ErrorCode.UNEXPECTED_TOKEN:
  405. raise exc.UnexpectedToken
  406. case ErrorCode.KVDB_ERR:
  407. raise exc.KvdbErr
  408. case ErrorCode.SERVICE_FAILED:
  409. raise exc.ServiceFailed
  410. case ErrorCode.GFX_DUPLICATE_TEXTURE_ID:
  411. raise exc.GfxDuplicateTextureID
  412. case ErrorCode.GFX_UNKNOWN_TEXTURE_ID:
  413. raise exc.GfxUnknownTextureID
  414. case ErrorCode.GFX_DUPLICATE_BUFFER_ID:
  415. raise exc.GfxDuplicateBufferID
  416. case ErrorCode.GFX_UNKNOWN_BUFFER_ID:
  417. raise exc.GfxUnknownBufferID
  418. case ErrorCode.GFX_DUPLICATE_ANIM_ID:
  419. raise exc.GfxDuplicateAnimID
  420. case ErrorCode.GFX_UNKNOWN_ANIM_ID:
  421. raise exc.GfxUnknownAnimID
  422. case ErrorCode.CONTACT_NOT_FOUND:
  423. raise exc.ContactNotFound
  424. case ErrorCode.SERIAL_ERR:
  425. raise exc.SerialErr
  426. case ErrorCode.TURSO_ERR:
  427. raise exc.TursoErr
  428. case ErrorCode.UNSUPPORTED_NODE_TYPE:
  429. raise exc.UnsupportedNodeType
  430. case ErrorCode.NODE_NOT_REMOVABLE:
  431. raise exc.NodeNotRemovable
  432. case _:
  433. raise exc.UnknownError(f"unknown error code: {errc}")
  434. return cursor
  435. def hello(self):
  436. response = self._make_request(Command.HELLO, bytearray())
  437. return serial.decode_str(response)
  438. def get_children(self, node_path):
  439. req = bytearray()
  440. serial.encode_str(req, node_path)
  441. cur = self._make_request(Command.GET_CHILDREN, req)
  442. children_len = serial.decode_varint(cur)
  443. children = []
  444. for _ in range(children_len):
  445. child_name = serial.decode_str(cur)
  446. child_id = serial.read_u32(cur)
  447. child_type = serial.read_u8(cur)
  448. children.append((child_name, child_id, child_type))
  449. return children
  450. def get_properties(self, node_path):
  451. req = bytearray()
  452. serial.encode_str(req, node_path)
  453. cur = self._make_request(Command.GET_PROPERTIES, req)
  454. props_len = serial.decode_varint(cur)
  455. props = []
  456. enum_read_fn = lambda cur: serial.decode_arr(cur, serial.decode_str)
  457. def depend_read_fn(cur):
  458. i = serial.read_u32(cur)
  459. local_name = serial.decode_str(cur)
  460. return (i, local_name)
  461. for _ in range(props_len):
  462. prop_name = serial.decode_str(cur)
  463. # We need prop_type below
  464. prop_type = serial.read_u8(cur)
  465. prop_read_fn = lambda cur: Api.read_prop_val(cur, prop_type)
  466. prop = Property(
  467. prop_name,
  468. prop_type,
  469. # subtype
  470. serial.read_u8(cur),
  471. # defaults
  472. #serial.decode_arr(cur, prop_read_fn),
  473. # ui_name
  474. serial.decode_str(cur),
  475. # desc
  476. serial.decode_str(cur),
  477. # is_null_allowed
  478. bool(serial.read_u8(cur)),
  479. # is_expr_allowed
  480. bool(serial.read_u8(cur)),
  481. # array_len
  482. serial.read_u32(cur),
  483. # min_val
  484. serial.decode_opt(cur, prop_read_fn),
  485. # max_val
  486. serial.decode_opt(cur, prop_read_fn),
  487. # enum_items
  488. serial.decode_opt(cur, enum_read_fn),
  489. # depends
  490. serial.decode_arr(cur, depend_read_fn)
  491. )
  492. props.append(prop)
  493. return props
  494. @staticmethod
  495. def read_prop_val(cur, prop_type):
  496. match prop_type:
  497. case PropertyType.NULL:
  498. return None
  499. case PropertyType.BOOL:
  500. return bool(serial.read_u8(cur))
  501. case PropertyType.UINT32:
  502. return serial.read_u32(cur)
  503. case PropertyType.FLOAT32:
  504. return serial.read_f32(cur)
  505. case PropertyType.STR:
  506. return serial.decode_str(cur)
  507. case PropertyType.ENUM:
  508. return serial.decode_str(cur)
  509. case PropertyType.SCENE_NODE_ID:
  510. return serial.read_u32(cur)
  511. case PropertyType.VECTOR_SHAPE:
  512. # Shapes carry no payload on the get path
  513. return "<...>"
  514. case _:
  515. raise Exception("unknown property type returned")
  516. def get_property_value_full(self, node_path, prop_name):
  517. req = bytearray()
  518. serial.encode_str(req, node_path)
  519. serial.encode_str(req, prop_name)
  520. cur = self._make_request(Command.GET_PROPERTY_VALUE, req)
  521. prop_type = serial.read_u8(cur)
  522. def prop_read_fn(cur):
  523. prop_status = serial.read_u8(cur)
  524. match prop_status:
  525. case PropertyStatus.NULL:
  526. return (PropertyStatus.NULL, None)
  527. case PropertyStatus.EXPR:
  528. return (PropertyStatus.EXPR, Expr(serial.decode_str(cur)))
  529. case PropertyStatus.UNSET | PropertyStatus.OK:
  530. return (prop_status, Api.read_prop_val(cur, prop_type))
  531. case _:
  532. raise Exception(f"unknown property status returned: {prop_status} (desynced stream?)")
  533. return serial.decode_arr(cur, prop_read_fn)
  534. def get_property_value(self, node_path, prop_name):
  535. vals = self.get_property_value_full(node_path, prop_name)
  536. return [val for (_, val) in vals]
  537. def add_node(self, parent_path, name, node_type):
  538. req = bytearray()
  539. serial.encode_str(req, parent_path)
  540. serial.encode_str(req, name)
  541. serial.write_u8(req, int(node_type))
  542. cur = self._make_request(Command.ADD_NODE, req)
  543. node_id = serial.read_u32(cur)
  544. return node_id
  545. def remove_node(self, node_path):
  546. req = bytearray()
  547. serial.encode_str(req, node_path)
  548. self._make_request(Command.REMOVE_NODE, req)
  549. def set_property_null(self, node_path, prop_name, i):
  550. req = bytearray()
  551. serial.encode_str(req, node_path)
  552. serial.encode_str(req, prop_name)
  553. serial.write_u32(req, i)
  554. serial.write_u8(req, PropertyType.NULL)
  555. self._make_request(Command.SET_PROPERTY_VALUE, req)
  556. def set_property_bool(self, node_path, prop_name, i, val):
  557. req = bytearray()
  558. serial.encode_str(req, node_path)
  559. serial.encode_str(req, prop_name)
  560. serial.write_u32(req, i)
  561. serial.write_u8(req, PropertyType.BOOL)
  562. serial.write_u8(req, int(val))
  563. self._make_request(Command.SET_PROPERTY_VALUE, req)
  564. def set_property_u32(self, node_path, prop_name, i, val):
  565. req = bytearray()
  566. serial.encode_str(req, node_path)
  567. serial.encode_str(req, prop_name)
  568. serial.write_u32(req, i)
  569. serial.write_u8(req, PropertyType.UINT32)
  570. serial.write_u32(req, val)
  571. self._make_request(Command.SET_PROPERTY_VALUE, req)
  572. def set_property_f32(self, node_path, prop_name, i, val):
  573. req = bytearray()
  574. serial.encode_str(req, node_path)
  575. serial.encode_str(req, prop_name)
  576. serial.write_u32(req, i)
  577. serial.write_u8(req, PropertyType.FLOAT32)
  578. serial.write_f32(req, val)
  579. self._make_request(Command.SET_PROPERTY_VALUE, req)
  580. def set_property_str(self, node_path, prop_name, i, val):
  581. req = bytearray()
  582. serial.encode_str(req, node_path)
  583. serial.encode_str(req, prop_name)
  584. serial.write_u32(req, i)
  585. serial.write_u8(req, PropertyType.STR)
  586. serial.encode_str(req, val)
  587. self._make_request(Command.SET_PROPERTY_VALUE, req)
  588. def set_property_enum(self, node_path, prop_name, i, val):
  589. req = bytearray()
  590. serial.encode_str(req, node_path)
  591. serial.encode_str(req, prop_name)
  592. serial.write_u32(req, i)
  593. serial.write_u8(req, PropertyType.ENUM)
  594. serial.encode_str(req, val)
  595. self._make_request(Command.SET_PROPERTY_VALUE, req)
  596. def set_property_node_id(self, node_path, prop_name, i, val):
  597. req = bytearray()
  598. serial.encode_str(req, node_path)
  599. serial.encode_str(req, prop_name)
  600. serial.write_u32(req, i)
  601. serial.write_u8(req, PropertyType.SCENE_NODE_ID)
  602. serial.write_u32(req, val)
  603. self._make_request(Command.SET_PROPERTY_VALUE, req)
  604. def set_property_expr(self, node_path, prop_name, i, expr_str):
  605. req = bytearray()
  606. serial.encode_str(req, node_path)
  607. serial.encode_str(req, prop_name)
  608. serial.write_u32(req, i)
  609. serial.write_u8(req, PropertyType.SEXPR)
  610. serial.encode_str(req, expr_str)
  611. self._make_request(Command.SET_PROPERTY_VALUE, req)
  612. def set_property_shape(self, node_path, prop_name, i, verts, indices):
  613. # verts: list of (x_expr, y_expr, [r, g, b, a]) tuples, where the
  614. # coordinate exprs use the same source language as set_property_expr
  615. req = bytearray()
  616. serial.encode_str(req, node_path)
  617. serial.encode_str(req, prop_name)
  618. serial.write_u32(req, i)
  619. serial.write_u8(req, PropertyType.VECTOR_SHAPE)
  620. serial.encode_varint(req, len(verts))
  621. for (x_expr, y_expr, color) in verts:
  622. serial.encode_str(req, x_expr)
  623. serial.encode_str(req, y_expr)
  624. for c in color:
  625. serial.write_f32(req, c)
  626. serial.encode_varint(req, len(indices))
  627. for index in indices:
  628. serial.write_u16(req, index)
  629. self._make_request(Command.SET_PROPERTY_VALUE, req)
  630. def get_signals(self, node_path):
  631. req = bytearray()
  632. serial.encode_str(req, node_path)
  633. cur = self._make_request(Command.GET_SIGNALS, req)
  634. sigs_len = serial.decode_varint(cur)
  635. sigs = []
  636. for _ in range(sigs_len):
  637. sigs.append(serial.decode_str(cur))
  638. return sigs
  639. def register_slot(self, node_path, sig_name, slot_name, user_data):
  640. req = bytearray()
  641. serial.encode_str(req, node_path)
  642. serial.encode_str(req, sig_name)
  643. serial.encode_str(req, slot_name)
  644. serial.encode_varint(req, len(user_data))
  645. req += user_data
  646. cur = self._make_request(Command.REGISTER_SLOT, req)
  647. slot_id = serial.read_u32(cur)
  648. return slot_id
  649. def get_slots(self, node_path, sig_name):
  650. req = bytearray()
  651. serial.encode_str(req, node_path)
  652. serial.encode_str(req, sig_name)
  653. cur = self._make_request(Command.GET_SLOTS, req)
  654. def read_slot(cur):
  655. slot_name = serial.decode_str(cur)
  656. slot_id = serial.read_u32(cur)
  657. return (slot_name, slot_id)
  658. slots = serial.decode_arr(cur, read_slot)
  659. return slots
  660. def get_methods(self, node_path):
  661. req = bytearray()
  662. serial.encode_str(req, node_path)
  663. cur = self._make_request(Command.GET_METHODS, req)
  664. def read_method(cur):
  665. method_name = serial.decode_str(cur)
  666. return method_name
  667. methods = serial.decode_arr(cur, read_method)
  668. return methods
  669. def get_method(self, node_path, method_name):
  670. req = bytearray()
  671. serial.encode_str(req, node_path)
  672. serial.encode_str(req, method_name)
  673. cur = self._make_request(Command.GET_METHOD, req)
  674. def read_arg(cur):
  675. arg_name = serial.decode_str(cur)
  676. arg_desc = serial.decode_str(cur)
  677. arg_type = serial.read_u8(cur)
  678. return (arg_name, arg_desc, arg_type)
  679. args = serial.decode_arr(cur, read_arg)
  680. results = serial.decode_opt(cur, lambda cur: serial.decode_arr(cur, read_arg))
  681. return (args, results)
  682. def call_method(self, node_path, method_name, arg_data):
  683. req = bytearray()
  684. serial.encode_str(req, node_path)
  685. serial.encode_str(req, method_name)
  686. serial.encode_buf(req, arg_data)
  687. cur = self._make_request(Command.CALL_METHOD, req)
  688. result = serial.decode_opt(cur, serial.decode_buf)
  689. return result