binary_serialization_api.rst 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. .. _doc_binary_serialization_api:
  2. Binary serialization API
  3. ========================
  4. Introduction
  5. ------------
  6. Godot has a simple serialization API based on Variant. It's used for
  7. converting data types to an array of bytes efficiently. This API is used
  8. in the functions ``get_var`` and ``store_var`` of :ref:`class_File`
  9. as well as the packet APIs for :ref:`class_PacketPeer`. This format
  10. is not used for binary scenes and resources.
  11. Packet specification
  12. --------------------
  13. The packet is designed to be always padded to 4 bytes. All values are
  14. little endian encoded. All packets have a 4 byte header representing an
  15. integer, specifying the type of data:
  16. +--------+--------------------------+
  17. | Type | Value |
  18. +========+==========================+
  19. | 0 | null |
  20. +--------+--------------------------+
  21. | 1 | bool |
  22. +--------+--------------------------+
  23. | 2 | integer |
  24. +--------+--------------------------+
  25. | 3 | float |
  26. +--------+--------------------------+
  27. | 4 | string |
  28. +--------+--------------------------+
  29. | 5 | vector2 |
  30. +--------+--------------------------+
  31. | 6 | rect2 |
  32. +--------+--------------------------+
  33. | 7 | vector3 |
  34. +--------+--------------------------+
  35. | 8 | matrix32 |
  36. +--------+--------------------------+
  37. | 9 | plane |
  38. +--------+--------------------------+
  39. | 10 | quaternion |
  40. +--------+--------------------------+
  41. | 11 | aabb (rect3) |
  42. +--------+--------------------------+
  43. | 12 | matrix3x3 |
  44. +--------+--------------------------+
  45. | 13 | transform (matrix 4x3) |
  46. +--------+--------------------------+
  47. | 14 | color |
  48. +--------+--------------------------+
  49. | 15 | image |
  50. +--------+--------------------------+
  51. | 16 | node path |
  52. +--------+--------------------------+
  53. | 17 | rid (unsupported) |
  54. +--------+--------------------------+
  55. | 18 | object (unsupported) |
  56. +--------+--------------------------+
  57. | 19 | input event |
  58. +--------+--------------------------+
  59. | 20 | dictionary |
  60. +--------+--------------------------+
  61. | 21 | array |
  62. +--------+--------------------------+
  63. | 22 | ByteArray |
  64. +--------+--------------------------+
  65. | 23 | IntArray |
  66. +--------+--------------------------+
  67. | 24 | FloatArray |
  68. +--------+--------------------------+
  69. | 25 | StringArray |
  70. +--------+--------------------------+
  71. | 26 | Vector2Array |
  72. +--------+--------------------------+
  73. | 27 | Vector3Array |
  74. +--------+--------------------------+
  75. | 28 | ColorArray |
  76. +--------+--------------------------+
  77. Following this is the actual packet contents, which varies for each type
  78. of packet:
  79. 0: null
  80. ~~~~~~~
  81. 1: bool
  82. ~~~~~~~
  83. +----------+-------+-----------+---------------------------+
  84. | Offset | Len | Type | Description |
  85. +==========+=======+===========+===========================+
  86. | 4 | 4 | Integer | 0 for False, 1 for True |
  87. +----------+-------+-----------+---------------------------+
  88. 2: integer
  89. ~~~~~~~~~~
  90. +----------+-------+-----------+--------------------------+
  91. | Offset | Len | Type | Description |
  92. +==========+=======+===========+==========================+
  93. | 4 | 4 | Integer | Signed, 32-Bit Integer |
  94. +----------+-------+-----------+--------------------------+
  95. 3: float
  96. ~~~~~~~~
  97. +----------+-------+---------+-------------------------+
  98. | Offset | Len | Type | Description |
  99. +==========+=======+=========+=========================+
  100. | 4 | 4 | Float | IEE 754 32-Bits Float |
  101. +----------+-------+---------+-------------------------+
  102. 4: string
  103. ~~~~~~~~~
  104. +----------+-------+-----------+----------------------------+
  105. | Offset | Len | Type | Description |
  106. +==========+=======+===========+============================+
  107. | 4 | 4 | Integer | String Length (in Bytes) |
  108. +----------+-------+-----------+----------------------------+
  109. | 8 | X | Bytes | UTF-8 Encoded String |
  110. +----------+-------+-----------+----------------------------+
  111. This field is padded to 4 bytes.
  112. 5: vector2
  113. ~~~~~~~~~~
  114. +----------+-------+---------+----------------+
  115. | Offset | Len | Type | Description |
  116. +==========+=======+=========+================+
  117. | 4 | 4 | Float | X Coordinate |
  118. +----------+-------+---------+----------------+
  119. | 8 | 4 | Float | Y Coordinate |
  120. +----------+-------+---------+----------------+
  121. 6: rect2
  122. ~~~~~~~~
  123. +----------+-------+---------+----------------+
  124. | Offset | Len | Type | Description |
  125. +==========+=======+=========+================+
  126. | 4 | 4 | Float | X Coordinate |
  127. +----------+-------+---------+----------------+
  128. | 8 | 4 | Float | Y Coordinate |
  129. +----------+-------+---------+----------------+
  130. | 12 | 4 | Float | X Size |
  131. +----------+-------+---------+----------------+
  132. | 16 | 4 | Float | Y Size |
  133. +----------+-------+---------+----------------+
  134. 7: vector3
  135. ~~~~~~~~~~
  136. +----------+-------+---------+----------------+
  137. | Offset | Len | Type | Description |
  138. +==========+=======+=========+================+
  139. | 4 | 4 | Float | X Coordinate |
  140. +----------+-------+---------+----------------+
  141. | 8 | 4 | Float | Y Coordinate |
  142. +----------+-------+---------+----------------+
  143. | 12 | 4 | Float | Z Coordinate |
  144. +----------+-------+---------+----------------+
  145. 8: matrix32
  146. ~~~~~~~~~~~
  147. +----------+-------+---------+---------------+
  148. | Offset | Len | Type | Description |
  149. +==========+=======+=========+===============+
  150. | 4 | 4 | Float | [0][0] |
  151. +----------+-------+---------+---------------+
  152. | 8 | 4 | Float | [0][1] |
  153. +----------+-------+---------+---------------+
  154. | 12 | 4 | Float | [1][0] |
  155. +----------+-------+---------+---------------+
  156. | 16 | 4 | Float | [1][1] |
  157. +----------+-------+---------+---------------+
  158. | 20 | 4 | Float | [2][0] |
  159. +----------+-------+---------+---------------+
  160. | 24 | 4 | Float | [2][1] |
  161. +----------+-------+---------+---------------+
  162. 9: plane
  163. ~~~~~~~~
  164. +----------+-------+---------+---------------+
  165. | Offset | Len | Type | Description |
  166. +==========+=======+=========+===============+
  167. | 4 | 4 | Float | Normal X |
  168. +----------+-------+---------+---------------+
  169. | 8 | 4 | Float | Normal Y |
  170. +----------+-------+---------+---------------+
  171. | 12 | 4 | Float | Normal Z |
  172. +----------+-------+---------+---------------+
  173. | 16 | 4 | Float | Distance |
  174. +----------+-------+---------+---------------+
  175. 10: quaternion
  176. ~~~~~~~~~~~~~~
  177. +----------+-------+---------+---------------+
  178. | Offset | Len | Type | Description |
  179. +==========+=======+=========+===============+
  180. | 4 | 4 | Float | Imaginary X |
  181. +----------+-------+---------+---------------+
  182. | 8 | 4 | Float | Imaginary Y |
  183. +----------+-------+---------+---------------+
  184. | 12 | 4 | Float | Imaginary Z |
  185. +----------+-------+---------+---------------+
  186. | 16 | 4 | Float | Real W |
  187. +----------+-------+---------+---------------+
  188. 11: aabb (rect3)
  189. ~~~~~~~~~~~~~~~~
  190. +----------+-------+---------+----------------+
  191. | Offset | Len | Type | Description |
  192. +==========+=======+=========+================+
  193. | 4 | 4 | Float | X Coordinate |
  194. +----------+-------+---------+----------------+
  195. | 8 | 4 | Float | Y Coordinate |
  196. +----------+-------+---------+----------------+
  197. | 12 | 4 | Float | Z Coordinate |
  198. +----------+-------+---------+----------------+
  199. | 16 | 4 | Float | X Size |
  200. +----------+-------+---------+----------------+
  201. | 20 | 4 | Float | Y Size |
  202. +----------+-------+---------+----------------+
  203. | 24 | 4 | Float | Z Size |
  204. +----------+-------+---------+----------------+
  205. 12: matrix3x3
  206. ~~~~~~~~~~~~~
  207. +----------+-------+---------+---------------+
  208. | Offset | Len | Type | Description |
  209. +==========+=======+=========+===============+
  210. | 4 | 4 | Float | [0][0] |
  211. +----------+-------+---------+---------------+
  212. | 8 | 4 | Float | [0][1] |
  213. +----------+-------+---------+---------------+
  214. | 12 | 4 | Float | [0][2] |
  215. +----------+-------+---------+---------------+
  216. | 16 | 4 | Float | [1][0] |
  217. +----------+-------+---------+---------------+
  218. | 20 | 4 | Float | [1][1] |
  219. +----------+-------+---------+---------------+
  220. | 24 | 4 | Float | [1][2] |
  221. +----------+-------+---------+---------------+
  222. | 28 | 4 | Float | [2][0] |
  223. +----------+-------+---------+---------------+
  224. | 32 | 4 | Float | [2][1] |
  225. +----------+-------+---------+---------------+
  226. | 36 | 4 | Float | [2][2] |
  227. +----------+-------+---------+---------------+
  228. 13: transform (matrix 4x3)
  229. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  230. +----------+-------+---------+---------------+
  231. | Offset | Len | Type | Description |
  232. +==========+=======+=========+===============+
  233. | 4 | 4 | Float | [0][0] |
  234. +----------+-------+---------+---------------+
  235. | 8 | 4 | Float | [0][1] |
  236. +----------+-------+---------+---------------+
  237. | 12 | 4 | Float | [0][2] |
  238. +----------+-------+---------+---------------+
  239. | 16 | 4 | Float | [1][0] |
  240. +----------+-------+---------+---------------+
  241. | 20 | 4 | Float | [1][1] |
  242. +----------+-------+---------+---------------+
  243. | 24 | 4 | Float | [1][2] |
  244. +----------+-------+---------+---------------+
  245. | 28 | 4 | Float | [2][0] |
  246. +----------+-------+---------+---------------+
  247. | 32 | 4 | Float | [2][1] |
  248. +----------+-------+---------+---------------+
  249. | 36 | 4 | Float | [2][2] |
  250. +----------+-------+---------+---------------+
  251. | 40 | 4 | Float | [3][0] |
  252. +----------+-------+---------+---------------+
  253. | 44 | 4 | Float | [3][1] |
  254. +----------+-------+---------+---------------+
  255. | 48 | 4 | Float | [3][2] |
  256. +----------+-------+---------+---------------+
  257. 14: color
  258. ~~~~~~~~~
  259. +----------+-------+---------+----------------+
  260. | Offset | Len | Type | Description |
  261. +==========+=======+=========+================+
  262. | 4 | 4 | Float | Red (0..1) |
  263. +----------+-------+---------+----------------+
  264. | 8 | 4 | Float | Green (0..1) |
  265. +----------+-------+---------+----------------+
  266. | 12 | 4 | Float | Blue (0..1) |
  267. +----------+-------+---------+----------------+
  268. | 16 | 4 | Float | Alpha (0..1) |
  269. +----------+-------+---------+----------------+
  270. 15: image
  271. ~~~~~~~~~
  272. +---------------------+-------+-----------+--------------------------------------------------+
  273. | Offset | Len | Type | Description |
  274. +=====================+=======+===========+==================================================+
  275. | 4 | 4 | Integer | Format (see FORMAT\_\* in "Image":class_image |
  276. +---------------------+-------+-----------+--------------------------------------------------+
  277. | 8 | 4 | Integer | Mip-Maps (0 means no mip-maps). |
  278. +---------------------+-------+-----------+--------------------------------------------------+
  279. | 12 | 4 | Integer | Width (Pixels) |
  280. +---------------------+-------+-----------+--------------------------------------------------+
  281. | 16 | 4 | Integer | Height (Pixels) |
  282. +---------------------+-------+-----------+--------------------------------------------------+
  283. | 20 | 4 | Integer | Data Length |
  284. +---------------------+-------+-----------+--------------------------------------------------+
  285. | 24..24+DataLength | 1 | Byte | Image Data |
  286. +---------------------+-------+-----------+--------------------------------------------------+
  287. This field is padded to 4 bytes.
  288. 16: node path
  289. ~~~~~~~~~~~~~
  290. +----------+-------+-----------+-----------------------------------------------------------------------------------------+
  291. | Offset | Len | Type | Description |
  292. +==========+=======+===========+=========================================================================================+
  293. | 4 | 4 | Integer | String Length, or New Format (val&0x80000000!=0 and NameCount=val&0x7FFFFFFF) |
  294. +----------+-------+-----------+-----------------------------------------------------------------------------------------+
  295. For old format:
  296. ^^^^^^^^^^^^^^^
  297. +----------+-------+---------+------------------------+
  298. | Offset | Len | Type | Description |
  299. +==========+=======+=========+========================+
  300. | 8 | X | Bytes | UTF-8 Encoded String |
  301. +----------+-------+---------+------------------------+
  302. Padded to 4 bytes.
  303. For new format:
  304. ^^^^^^^^^^^^^^^
  305. +----------+-------+-----------+-------------------------------------+
  306. | Offset | Len | Type | Description |
  307. +==========+=======+===========+=====================================+
  308. | 4 | 4 | Integer | Sub-Name Count |
  309. +----------+-------+-----------+-------------------------------------+
  310. | 8 | 4 | Integer | Flags (absolute: val&1 != 0 ) |
  311. +----------+-------+-----------+-------------------------------------+
  312. For each Name and Sub-Name
  313. +----------+-------+-----------+------------------------+
  314. | Offset | Len | Type | Description |
  315. +==========+=======+===========+========================+
  316. | X+0 | 4 | Integer | String Length |
  317. +----------+-------+-----------+------------------------+
  318. | X+4 | X | Bytes | UTF-8 Encoded String |
  319. +----------+-------+-----------+------------------------+
  320. Every name string is padded to 4 bytes.
  321. 17: rid (unsupported)
  322. ~~~~~~~~~~~~~~~~~~~~~
  323. 18: object (unsupported)
  324. ~~~~~~~~~~~~~~~~~~~~~~~~
  325. 19: input event
  326. ~~~~~~~~~~~~~~~
  327. 20: dictionary
  328. ~~~~~~~~~~~~~~
  329. +----------+-------+-----------+---------------------------------------------------------------------+
  330. | Offset | Len | Type | Description |
  331. +==========+=======+===========+=====================================================================+
  332. | 4 | 4 | Integer | val&0x7FFFFFFF = elements, val&0x80000000 = shared (bool) |
  333. +----------+-------+-----------+---------------------------------------------------------------------+
  334. Then what follows is, for amount of "elements", pairs of key and value,
  335. one after the other, using this same format.
  336. 21: array
  337. ~~~~~~~~~
  338. +----------+-------+-----------+---------------------------------------------------------------------+
  339. | Offset | Len | Type | Description |
  340. +==========+=======+===========+=====================================================================+
  341. | 4 | 4 | Integer | val&0x7FFFFFFF = elements, val&0x80000000 = shared (bool) |
  342. +----------+-------+-----------+---------------------------------------------------------------------+
  343. Then what follows is, for amount of "elements", values one after the
  344. other, using this same format.
  345. 22: :ref:`class_ByteArray`
  346. ~~~~~~~~~~~~~~
  347. +---------------+-------+-----------+------------------------+
  348. | Offset | Len | Type | Description |
  349. +===============+=======+===========+========================+
  350. | 4 | 4 | Integer | Array Length (Bytes) |
  351. +---------------+-------+-----------+------------------------+
  352. | 8..8+length | 1 | Byte | Byte (0..255) |
  353. +---------------+-------+-----------+------------------------+
  354. The array data is padded to 4 bytes.
  355. 23: :ref:`class_IntArray`
  356. ~~~~~~~~~~~~~
  357. +------------------+-------+-----------+---------------------------+
  358. | Offset | Len | Type | Description |
  359. +==================+=======+===========+===========================+
  360. | 4 | 4 | Integer | Array Length (Integers) |
  361. +------------------+-------+-----------+---------------------------+
  362. | 8..8+length\*4 | 4 | Integer | 32 Bits Signed Integer |
  363. +------------------+-------+-----------+---------------------------+
  364. 24: :ref:`class_FloatArray`
  365. ~~~~~~~~~~~~~~~
  366. +------------------+-------+-----------+---------------------------+
  367. | Offset | Len | Type | Description |
  368. +==================+=======+===========+===========================+
  369. | 4 | 4 |Integer | Array Length (Floats) |
  370. +------------------+-------+-----------+---------------------------+
  371. | 8..8+length\*4 | 4 |Integer | 32 Bits IEE 754 Float |
  372. +------------------+-------+-----------+---------------------------+
  373. 25: :ref:`class_StringArray`
  374. ~~~~~~~~~~~~~~~~
  375. +----------+-------+-----------+--------------------------+
  376. | Offset | Len | Type | Description |
  377. +==========+=======+===========+==========================+
  378. | 4 | 4 | Integer | Array Length (Strings) |
  379. +----------+-------+-----------+--------------------------+
  380. For each String:
  381. +----------+-------+-----------+------------------------+
  382. | Offset | Len | Type | Description |
  383. +==========+=======+===========+========================+
  384. | X+0 | 4 | Integer | String Length |
  385. +----------+-------+-----------+------------------------+
  386. | X+4 | X | Bytes | UTF-8 Encoded String |
  387. +----------+-------+-----------+------------------------+
  388. Every string is is padded to 4 bytes.
  389. 26: :ref:`class_Vector2Array`
  390. ~~~~~~~~~~~~~~~~~
  391. +-------------------+-------+-----------+----------------+
  392. | Offset | Len | Type | Description |
  393. +===================+=======+===========+================+
  394. | 4 | 4 | Integer | Array Length |
  395. +-------------------+-------+-----------+----------------+
  396. | 8..8+length\*8 | 4 | Float | X Coordinate |
  397. +-------------------+-------+-----------+----------------+
  398. | 8..12+length\*8 | 4 | Float | Y Coordinate |
  399. +-------------------+-------+-----------+----------------+
  400. 27: :ref:`class_Vector3Array`
  401. ~~~~~~~~~~~~~~~~~
  402. +--------------------+-------+-----------+----------------+
  403. | Offset | Len | Type | Description |
  404. +====================+=======+===========+================+
  405. | 4 | 4 | Integer | Array Length |
  406. +--------------------+-------+-----------+----------------+
  407. | 8..8+length\*12 | 4 | Float | X Coordinate |
  408. +--------------------+-------+-----------+----------------+
  409. | 8..12+length\*12 | 4 | Float | Y Coordinate |
  410. +--------------------+-------+-----------+----------------+
  411. | 8..16+length\*12 | 4 | Float | Z Coordinate |
  412. +--------------------+-------+-----------+----------------+
  413. 28: :ref:`class_ColorArray`
  414. ~~~~~~~~~~~~~~~
  415. +--------------------+-------+-----------+----------------+
  416. | Offset | Len | Type | Description |
  417. +====================+=======+===========+================+
  418. | 4 | 4 | Integer | Array Length |
  419. +--------------------+-------+-----------+----------------+
  420. | 8..8+length\*16 | 4 | Float | Red (0..1) |
  421. +--------------------+-------+-----------+----------------+
  422. | 8..12+length\*16 | 4 | Float | Green (0..1) |
  423. +--------------------+-------+-----------+----------------+
  424. | 8..16+length\*16 | 4 | Float | Blue (0..1) |
  425. +--------------------+-------+-----------+----------------+
  426. | 8..20+length\*16 | 4 | Float | Alpha (0..1) |
  427. +--------------------+-------+-----------+----------------+