api.py 25 KB

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