api.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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. @staticmethod
  201. def to_str(errc):
  202. match errc:
  203. case ErrorCode.INVALID_SCENE_PATH:
  204. return "invalid_scene_path"
  205. case ErrorCode.NODE_NOT_FOUND:
  206. return "node_not_found"
  207. case ErrorCode.CHILD_NODE_NOT_FOUND:
  208. return "child_node_not_found"
  209. case ErrorCode.PARENT_NODE_NOT_FOUND:
  210. return "parent_node_not_found"
  211. case ErrorCode.PROPERTY_ALREADY_EXISTS:
  212. return "property_already_exists"
  213. case ErrorCode.PROPERTY_NOT_FOUND:
  214. return "property_not_found"
  215. case ErrorCode.PROPERTY_WRONG_TYPE:
  216. return "property_wrong_type"
  217. case ErrorCode.PROPERTY_WRONG_LEN:
  218. return "property_wrong_len"
  219. case ErrorCode.PROPERTY_WRONG_INDEX:
  220. return "property_wrong_index"
  221. case ErrorCode.PROPERTY_OUT_OF_RANGE:
  222. return "property_out_of_range"
  223. case ErrorCode.PROPERTY_NULL_NOT_ALLOWED:
  224. return "property_null_not_allowed"
  225. case ErrorCode.PROPERTY_SEXPR_NOT_ALLOWED:
  226. return "property_sexpr_not_allowed"
  227. case ErrorCode.PROPERTY_IS_BOUNDED:
  228. return "property_is_bounded"
  229. case ErrorCode.PROPERTY_WRONG_ENUM_ITEM:
  230. return "property_wrong_enum_item"
  231. case ErrorCode.SIGNAL_ALREADY_EXISTS:
  232. return "signal_already_exists"
  233. case ErrorCode.SIGNAL_NOT_FOUND:
  234. return "signal_not_found"
  235. case ErrorCode.SLOT_NOT_FOUND:
  236. return "slot_not_found"
  237. case ErrorCode.METHOD_ALREADY_EXISTS:
  238. return "method_already_exists"
  239. case ErrorCode.METHOD_NOT_FOUND:
  240. return "method_not_found"
  241. case ErrorCode.NODES_ARE_LINKED:
  242. return "nodes_are_linked"
  243. case ErrorCode.NODES_NOT_LINKED:
  244. return "nodes_not_linked"
  245. case ErrorCode.NODE_HAS_PARENTS:
  246. return "node_has_parents"
  247. case ErrorCode.NODE_HAS_CHILDREN:
  248. return "node_has_children"
  249. case ErrorCode.NODE_PARENT_NAME_CONFLICT:
  250. return "node_parent_name_conflict"
  251. case ErrorCode.NODE_CHILD_NAME_CONFLICT:
  252. return "node_child_name_conflict"
  253. case ErrorCode.NODE_SIBLING_NAME_CONFLICT:
  254. return "node_sibling_name_conflict"
  255. case ErrorCode.SEXPR_GLOBAL_NOT_FOUND:
  256. return "sexpr_global_not_found"
  257. case ErrorCode.PUBLISHER_DESTROYED:
  258. return "publisher_destroyed"
  259. case ErrorCode.CHANNEL_CLOSED:
  260. return "channel_closed"
  261. case ErrorCode.NODES_ARE_SAME:
  262. return "nodes_are_same"
  263. case ErrorCode.UNEXPECTED_TOKEN:
  264. return "unexpected_token"
  265. case ErrorCode.KVDB_ERR:
  266. return "kvdb_err"
  267. case ErrorCode.SERVICE_FAILED:
  268. return "service_failed"
  269. case ErrorCode.GFX_DUPLICATE_TEXTURE_ID:
  270. return "gfx_duplicate_texture_id"
  271. case ErrorCode.GFX_UNKNOWN_TEXTURE_ID:
  272. return "gfx_unknown_texture_id"
  273. case ErrorCode.GFX_DUPLICATE_BUFFER_ID:
  274. return "gfx_duplicate_buffer_id"
  275. case ErrorCode.GFX_UNKNOWN_BUFFER_ID:
  276. return "gfx_unknown_buffer_id"
  277. case ErrorCode.GFX_DUPLICATE_ANIM_ID:
  278. return "gfx_duplicate_anim_id"
  279. case ErrorCode.GFX_UNKNOWN_ANIM_ID:
  280. return "gfx_unknown_anim_id"
  281. case ErrorCode.CONTACT_NOT_FOUND:
  282. return "contact_not_found"
  283. case ErrorCode.SERIAL_ERR:
  284. return "serial_err"
  285. case ErrorCode.TURSO_ERR:
  286. return "turso_err"
  287. case _:
  288. return "unknown"
  289. def vertex(x, y, r, g, b, a, u, v):
  290. buf = bytearray()
  291. serial.write_f32(buf, x)
  292. serial.write_f32(buf, y)
  293. serial.write_f32(buf, r)
  294. serial.write_f32(buf, g)
  295. serial.write_f32(buf, b)
  296. serial.write_f32(buf, a)
  297. serial.write_f32(buf, u)
  298. serial.write_f32(buf, v)
  299. return buf
  300. def face(idx1, idx2, idx3):
  301. buf = bytearray()
  302. serial.write_u32(buf, idx1)
  303. serial.write_u32(buf, idx2)
  304. serial.write_u32(buf, idx3)
  305. return buf
  306. class Api:
  307. def __init__(self, addr="127.0.0.1", port=9484):
  308. context = zmq.Context()
  309. self.socket = context.socket(zmq.REQ)
  310. #self.socket.setsockopt(zmq.IPV6, True)
  311. self.socket.connect(f"tcp://{addr}:{port}")
  312. def _make_request(self, cmd, payload):
  313. req_cmd = bytearray()
  314. serial.write_u8(req_cmd, cmd)
  315. self.socket.send_multipart([req_cmd, payload])
  316. errc, reply = self.socket.recv_multipart()
  317. errc = int.from_bytes(errc, "little")
  318. cursor = serial.Cursor(reply)
  319. match errc:
  320. case 0:
  321. pass
  322. case ErrorCode.INVALID_SCENE_PATH:
  323. raise exc.InvalidScenePath
  324. case ErrorCode.NODE_NOT_FOUND:
  325. raise exc.NodeNotFound
  326. case ErrorCode.CHILD_NODE_NOT_FOUND:
  327. raise exc.ChildNodeNotFound
  328. case ErrorCode.PARENT_NODE_NOT_FOUND:
  329. raise exc.ParentNodeNotFound
  330. case ErrorCode.PROPERTY_ALREADY_EXISTS:
  331. raise exc.PropertyAlreadyExists
  332. case ErrorCode.PROPERTY_NOT_FOUND:
  333. raise exc.PropertyNotFound
  334. case ErrorCode.PROPERTY_WRONG_TYPE:
  335. raise exc.PropertyWrongType
  336. case ErrorCode.PROPERTY_WRONG_LEN:
  337. raise exc.PropertyWrongLen
  338. case ErrorCode.PROPERTY_WRONG_INDEX:
  339. raise exc.PropertyWrongIndex
  340. case ErrorCode.PROPERTY_OUT_OF_RANGE:
  341. raise exc.PropertyOutOfRange
  342. case ErrorCode.PROPERTY_NULL_NOT_ALLOWED:
  343. raise exc.PropertyNullNotAllowed
  344. case ErrorCode.PROPERTY_SEXPR_NOT_ALLOWED:
  345. raise exc.PropertySExprNotAllowed
  346. case ErrorCode.PROPERTY_IS_BOUNDED:
  347. raise exc.PropertyIsBounded
  348. case ErrorCode.PROPERTY_WRONG_ENUM_ITEM:
  349. raise exc.PropertyWrongEnumItem
  350. case ErrorCode.SIGNAL_ALREADY_EXISTS:
  351. raise exc.SignalAlreadyExists
  352. case ErrorCode.SIGNAL_NOT_FOUND:
  353. raise exc.SignalNotFound
  354. case ErrorCode.SLOT_NOT_FOUND:
  355. raise exc.SlotNotFound
  356. case ErrorCode.METHOD_ALREADY_EXISTS:
  357. raise exc.MethodAlreadyExists
  358. case ErrorCode.METHOD_NOT_FOUND:
  359. raise exc.MethodNotFound
  360. case ErrorCode.NODES_ARE_LINKED:
  361. raise exc.NodesAreLinked
  362. case ErrorCode.NODES_NOT_LINKED:
  363. raise exc.NodesNotLinked
  364. case ErrorCode.NODE_HAS_PARENTS:
  365. raise exc.NodeHasParents
  366. case ErrorCode.NODE_HAS_CHILDREN:
  367. raise exc.NodeHasChildren
  368. case ErrorCode.NODE_PARENT_NAME_CONFLICT:
  369. raise exc.NodeParentNameConflict
  370. case ErrorCode.NODE_CHILD_NAME_CONFLICT:
  371. raise exc.NodeChildNameConflict
  372. case ErrorCode.NODE_SIBLING_NAME_CONFLICT:
  373. raise exc.NodeSiblingNameConflict
  374. case ErrorCode.SEXPR_GLOBAL_NOT_FOUND:
  375. raise exc.SExprGlobalNotFound
  376. case ErrorCode.PUBLISHER_DESTROYED:
  377. raise exc.PublisherDestroyed
  378. case ErrorCode.CHANNEL_CLOSED:
  379. raise exc.ChannelClosed
  380. case ErrorCode.NODES_ARE_SAME:
  381. raise exc.NodesAreSame
  382. case ErrorCode.UNEXPECTED_TOKEN:
  383. raise exc.UnexpectedToken
  384. case ErrorCode.KVDB_ERR:
  385. raise exc.KvdbErr
  386. case ErrorCode.SERVICE_FAILED:
  387. raise exc.ServiceFailed
  388. case ErrorCode.GFX_DUPLICATE_TEXTURE_ID:
  389. raise exc.GfxDuplicateTextureID
  390. case ErrorCode.GFX_UNKNOWN_TEXTURE_ID:
  391. raise exc.GfxUnknownTextureID
  392. case ErrorCode.GFX_DUPLICATE_BUFFER_ID:
  393. raise exc.GfxDuplicateBufferID
  394. case ErrorCode.GFX_UNKNOWN_BUFFER_ID:
  395. raise exc.GfxUnknownBufferID
  396. case ErrorCode.GFX_DUPLICATE_ANIM_ID:
  397. raise exc.GfxDuplicateAnimID
  398. case ErrorCode.GFX_UNKNOWN_ANIM_ID:
  399. raise exc.GfxUnknownAnimID
  400. case ErrorCode.CONTACT_NOT_FOUND:
  401. raise exc.ContactNotFound
  402. case ErrorCode.SERIAL_ERR:
  403. raise exc.SerialErr
  404. case ErrorCode.TURSO_ERR:
  405. raise exc.TursoErr
  406. case _:
  407. raise exc.UnknownError(f"unknown error code: {errc}")
  408. return cursor
  409. def hello(self):
  410. response = self._make_request(Command.HELLO, bytearray())
  411. return serial.decode_str(response)
  412. def get_info(self, node_id):
  413. req = bytearray()
  414. serial.write_u32(req, node_id)
  415. cur = self._make_request(Command.GET_INFO, req)
  416. name = serial.decode_str(cur)
  417. type = serial.read_u8(cur)
  418. return (name, type)
  419. def get_children(self, node_path):
  420. req = bytearray()
  421. serial.encode_str(req, node_path)
  422. cur = self._make_request(Command.GET_CHILDREN, req)
  423. children_len = serial.decode_varint(cur)
  424. children = []
  425. for _ in range(children_len):
  426. child_name = serial.decode_str(cur)
  427. child_id = serial.read_u32(cur)
  428. child_type = serial.read_u8(cur)
  429. children.append((child_name, child_id, child_type))
  430. return children
  431. def get_parents(self, node_id):
  432. req = bytearray()
  433. serial.write_u32(req, node_id)
  434. cur = self._make_request(Command.GET_PARENTS, req)
  435. parents_len = serial.decode_varint(cur)
  436. parents = []
  437. for _ in range(parents_len):
  438. parent_name = serial.decode_str(cur)
  439. parent_id = serial.read_u32(cur)
  440. parent_type = serial.read_u8(cur)
  441. parents.append((parent_name, parent_id, parent_type))
  442. return parents
  443. def get_properties(self, node_path):
  444. req = bytearray()
  445. serial.encode_str(req, node_path)
  446. cur = self._make_request(Command.GET_PROPERTIES, req)
  447. props_len = serial.decode_varint(cur)
  448. props = []
  449. enum_read_fn = lambda cur: serial.decode_arr(cur, serial.decode_str)
  450. def depend_read_fn(cur):
  451. i = serial.read_u32(cur)
  452. local_name = serial.decode_str(cur)
  453. return (i, local_name)
  454. for _ in range(props_len):
  455. prop_name = serial.decode_str(cur)
  456. # We need prop_type below
  457. prop_type = serial.read_u8(cur)
  458. prop_read_fn = lambda cur: Api.read_prop_val(cur, prop_type)
  459. prop = Property(
  460. prop_name,
  461. prop_type,
  462. # subtype
  463. serial.read_u8(cur),
  464. # defaults
  465. #serial.decode_arr(cur, prop_read_fn),
  466. # ui_name
  467. serial.decode_str(cur),
  468. # desc
  469. serial.decode_str(cur),
  470. # is_null_allowed
  471. bool(serial.read_u8(cur)),
  472. # is_expr_allowed
  473. bool(serial.read_u8(cur)),
  474. # array_len
  475. serial.read_u32(cur),
  476. # min_val
  477. serial.decode_opt(cur, prop_read_fn),
  478. # max_val
  479. serial.decode_opt(cur, prop_read_fn),
  480. # enum_items
  481. serial.decode_opt(cur, enum_read_fn),
  482. # depends
  483. serial.decode_arr(cur, depend_read_fn)
  484. )
  485. props.append(prop)
  486. return props
  487. @staticmethod
  488. def read_prop_val(cur, prop_type):
  489. match prop_type:
  490. case PropertyType.NULL:
  491. return None
  492. case PropertyType.BOOL:
  493. return bool(serial.read_u8(cur))
  494. case PropertyType.UINT32:
  495. return serial.read_u32(cur)
  496. case PropertyType.FLOAT32:
  497. return serial.read_f32(cur)
  498. case PropertyType.STR:
  499. return serial.decode_str(cur)
  500. case PropertyType.ENUM:
  501. return serial.decode_str(cur)
  502. case PropertyType.SCENE_NODE_ID:
  503. return serial.read_u32(cur)
  504. case PropertyType.VECTOR_SHAPE:
  505. # Shapes carry no payload on the get path
  506. return "<...>"
  507. case _:
  508. raise Exception("unknown property type returned")
  509. def get_property_value(self, node_path, prop_name):
  510. req = bytearray()
  511. serial.encode_str(req, node_path)
  512. serial.encode_str(req, prop_name)
  513. cur = self._make_request(Command.GET_PROPERTY_VALUE, req)
  514. prop_type = serial.read_u8(cur)
  515. def prop_read_fn(cur):
  516. prop_status = serial.read_u8(cur)
  517. match prop_status:
  518. case PropertyStatus.NULL:
  519. return None
  520. case PropertyStatus.EXPR:
  521. return Expr(serial.decode_str(cur))
  522. case PropertyStatus.UNSET | PropertyStatus.OK:
  523. return Api.read_prop_val(cur, prop_type)
  524. vals = serial.decode_arr(cur, prop_read_fn)
  525. return vals
  526. def add_node(self, node_name, node_type):
  527. req = bytearray()
  528. serial.encode_str(req, node_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_id):
  534. req = bytearray()
  535. serial.write_u32(req, node_id)
  536. self._make_request(Command.REMOVE_NODE, req)
  537. def rename_node(self, node_id, node_name):
  538. req = bytearray()
  539. serial.write_u32(req, node_id)
  540. serial.encode_str(req, node_name)
  541. self._make_request(Command.RENAME_NODE, req)
  542. def scan_dangling(self):
  543. cur = self._make_request(Command.SCAN_DANGLING, bytearray())
  544. dangling_len = serial.decode_varint(cur)
  545. dangling = []
  546. for _ in range(dangling_len):
  547. node_id = serial.read_u32(cur)
  548. dangling.append(node_id)
  549. return dangling
  550. def lookup_node_id(self, node_path):
  551. req = bytearray()
  552. serial.encode_str(req, node_path)
  553. try:
  554. cur = self._make_request(Command.LOOKUP_NODE_ID, req)
  555. except exc.NodeNotFound:
  556. return None
  557. return serial.read_u32(cur)
  558. def add_property(self, node_id, prop):
  559. req = bytearray()
  560. serial.write_u32(req, node_id)
  561. serial.encode_str(req, prop.name)
  562. serial.write_u8(req, int(prop.type))
  563. serial.write_u8(req, int(prop.subtype))
  564. serial.write_u32(req, int(prop.array_len))
  565. def write_defaults(by):
  566. assert prop.defaults is not None
  567. defaults_len = len(prop.defaults)
  568. serial.encode_varint(req, defaults_len)
  569. for default in prop.defaults:
  570. match prop.type:
  571. case PropertyType.UINT32:
  572. serial.write_u32(req, default)
  573. case PropertyType.FLOAT32:
  574. serial.write_f32(req, default)
  575. case PropertyType.STR:
  576. serial.encode_str(req, default)
  577. case _:
  578. raise exc.PropertyWrongType
  579. serial.encode_opt(req, prop.defaults, write_defaults)
  580. serial.encode_str(req, prop.ui_name)
  581. serial.encode_str(req, prop.desc)
  582. serial.write_u8(req, int(prop.is_null_allowed))
  583. serial.write_u8(req, int(prop.is_expr_allowed))
  584. def write_mxx(v, by):
  585. assert v is not None
  586. match prop.type:
  587. case PropertyType.UINT32:
  588. serial.write_u32(req, v)
  589. case PropertyType.FLOAT32:
  590. serial.write_f32(req, v)
  591. case _:
  592. raise exc.PropertyWrongType
  593. write_min = lambda by: write_mxx(prop.min_val, by)
  594. write_max = lambda by: write_mxx(prop.max_val, by)
  595. serial.encode_opt(req, prop.min_val, write_min)
  596. serial.encode_opt(req, prop.max_val, write_max)
  597. serial.encode_varint(req, len(prop.enum_items))
  598. for enum_item in prop.enum_items:
  599. if prop.type != PropertyType.ENUM:
  600. raise exc.PropertyWrongType
  601. serial.encode_str(req, enum_item)
  602. self._make_request(Command.ADD_PROPERTY, req)
  603. def link_node(self, child_id, parent_id):
  604. req = bytearray()
  605. serial.write_u32(req, child_id)
  606. serial.write_u32(req, parent_id)
  607. self._make_request(Command.LINK_NODE, req)
  608. def unlink_node(self, child_id, parent_id):
  609. req = bytearray()
  610. serial.write_u32(req, child_id)
  611. serial.write_u32(req, parent_id)
  612. self._make_request(Command.UNLINK_NODE, req)
  613. def set_property_null(self, node_path, prop_name, i):
  614. req = bytearray()
  615. serial.encode_str(req, node_path)
  616. serial.encode_str(req, prop_name)
  617. serial.write_u32(req, i)
  618. serial.write_u8(req, PropertyType.NULL)
  619. self._make_request(Command.SET_PROPERTY_VALUE, req)
  620. def set_property_bool(self, node_path, prop_name, i, val):
  621. req = bytearray()
  622. serial.encode_str(req, node_path)
  623. serial.encode_str(req, prop_name)
  624. serial.write_u32(req, i)
  625. serial.write_u8(req, PropertyType.BOOL)
  626. serial.write_u8(req, int(val))
  627. self._make_request(Command.SET_PROPERTY_VALUE, req)
  628. def set_property_u32(self, node_path, prop_name, i, val):
  629. req = bytearray()
  630. serial.encode_str(req, node_path)
  631. serial.encode_str(req, prop_name)
  632. serial.write_u32(req, i)
  633. serial.write_u8(req, PropertyType.UINT32)
  634. serial.write_u32(req, val)
  635. self._make_request(Command.SET_PROPERTY_VALUE, req)
  636. def set_property_f32(self, node_path, prop_name, i, val):
  637. req = bytearray()
  638. serial.encode_str(req, node_path)
  639. serial.encode_str(req, prop_name)
  640. serial.write_u32(req, i)
  641. serial.write_u8(req, PropertyType.FLOAT32)
  642. serial.write_f32(req, val)
  643. self._make_request(Command.SET_PROPERTY_VALUE, req)
  644. def set_property_str(self, node_path, prop_name, i, val):
  645. req = bytearray()
  646. serial.encode_str(req, node_path)
  647. serial.encode_str(req, prop_name)
  648. serial.write_u32(req, i)
  649. serial.write_u8(req, PropertyType.STR)
  650. serial.encode_str(req, val)
  651. self._make_request(Command.SET_PROPERTY_VALUE, req)
  652. def set_property_enum(self, node_path, prop_name, i, val):
  653. req = bytearray()
  654. serial.encode_str(req, node_path)
  655. serial.encode_str(req, prop_name)
  656. serial.write_u32(req, i)
  657. serial.write_u8(req, PropertyType.ENUM)
  658. serial.encode_str(req, val)
  659. self._make_request(Command.SET_PROPERTY_VALUE, req)
  660. def set_property_expr(self, node_path, prop_name, i, expr_str):
  661. req = bytearray()
  662. serial.encode_str(req, node_path)
  663. serial.encode_str(req, prop_name)
  664. serial.write_u32(req, i)
  665. serial.write_u8(req, PropertyType.SEXPR)
  666. serial.encode_str(req, expr_str)
  667. self._make_request(Command.SET_PROPERTY_VALUE, req)
  668. def set_property_shape(self, node_path, prop_name, i, verts, indices):
  669. # verts: list of (x_expr, y_expr, [r, g, b, a]) tuples, where the
  670. # coordinate exprs use the same source language as set_property_expr
  671. req = bytearray()
  672. serial.encode_str(req, node_path)
  673. serial.encode_str(req, prop_name)
  674. serial.write_u32(req, i)
  675. serial.write_u8(req, PropertyType.VECTOR_SHAPE)
  676. serial.encode_varint(req, len(verts))
  677. for (x_expr, y_expr, color) in verts:
  678. serial.encode_str(req, x_expr)
  679. serial.encode_str(req, y_expr)
  680. for c in color:
  681. serial.write_f32(req, c)
  682. serial.encode_varint(req, len(indices))
  683. for index in indices:
  684. serial.write_u16(req, index)
  685. self._make_request(Command.SET_PROPERTY_VALUE, req)
  686. def get_signals(self, node_path):
  687. req = bytearray()
  688. serial.encode_str(req, node_path)
  689. cur = self._make_request(Command.GET_SIGNALS, req)
  690. sigs_len = serial.decode_varint(cur)
  691. sigs = []
  692. for _ in range(sigs_len):
  693. sigs.append(serial.decode_str(cur))
  694. return sigs
  695. def register_slot(self, node_path, sig_name, slot_name, user_data):
  696. req = bytearray()
  697. serial.encode_str(req, node_path)
  698. serial.encode_str(req, sig_name)
  699. serial.encode_str(req, slot_name)
  700. serial.encode_varint(req, len(user_data))
  701. req += user_data
  702. cur = self._make_request(Command.REGISTER_SLOT, req)
  703. slot_id = serial.read_u32(cur)
  704. return slot_id
  705. def unregister_slot(self, node_id, sig_name, slot_id):
  706. req = bytearray()
  707. serial.write_u32(req, node_id)
  708. serial.encode_str(req, sig_name)
  709. serial.write_u32(req, slot_id)
  710. self._make_request(Command.UNREGISTER_SLOT, req)
  711. def lookup_slot_id(self, node_id, sig_name, slot_name):
  712. req = bytearray()
  713. serial.write_u32(req, node_id)
  714. serial.encode_str(req, sig_name)
  715. serial.encode_str(req, slot_name)
  716. try:
  717. cur = self._make_request(Command.LOOKUP_SLOT_ID, req)
  718. except exc.SlotNotFound:
  719. return None
  720. return serial.read_u32(cur)
  721. def get_slots(self, node_path, sig_name):
  722. req = bytearray()
  723. serial.encode_str(req, node_path)
  724. serial.encode_str(req, sig_name)
  725. cur = self._make_request(Command.GET_SLOTS, req)
  726. def read_slot(cur):
  727. slot_name = serial.decode_str(cur)
  728. slot_id = serial.read_u32(cur)
  729. return (slot_name, slot_id)
  730. slots = serial.decode_arr(cur, read_slot)
  731. return slots
  732. def get_methods(self, node_path):
  733. req = bytearray()
  734. serial.encode_str(req, node_path)
  735. cur = self._make_request(Command.GET_METHODS, req)
  736. def read_method(cur):
  737. method_name = serial.decode_str(cur)
  738. return method_name
  739. methods = serial.decode_arr(cur, read_method)
  740. return methods
  741. def get_method(self, node_path, method_name):
  742. req = bytearray()
  743. serial.encode_str(req, node_path)
  744. serial.encode_str(req, method_name)
  745. cur = self._make_request(Command.GET_METHOD, req)
  746. def read_arg(cur):
  747. arg_name = serial.decode_str(cur)
  748. arg_desc = serial.decode_str(cur)
  749. arg_type = serial.read_u8(cur)
  750. return (arg_name, arg_desc, arg_type)
  751. args = serial.decode_arr(cur, read_arg)
  752. results = serial.decode_arr(cur, read_arg)
  753. return (args, results)
  754. def call_method(self, node_path, method_name, arg_data):
  755. req = bytearray()
  756. serial.encode_str(req, node_path)
  757. serial.encode_str(req, method_name)
  758. serial.encode_buf(req, arg_data)
  759. cur = self._make_request(Command.CALL_METHOD, req)
  760. result = serial.decode_opt(cur, serial.decode_buf)
  761. return result