binary_serialization_api.rst 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 | transform2d |
  36. +--------+--------------------------+
  37. | 9 | plane |
  38. +--------+--------------------------+
  39. | 10 | quat |
  40. +--------+--------------------------+
  41. | 11 | aabb |
  42. +--------+--------------------------+
  43. | 12 | basis |
  44. +--------+--------------------------+
  45. | 13 | transform |
  46. +--------+--------------------------+
  47. | 14 | color |
  48. +--------+--------------------------+
  49. | 15 | node path |
  50. +--------+--------------------------+
  51. | 16 | rid |
  52. +--------+--------------------------+
  53. | 17 | object |
  54. +--------+--------------------------+
  55. | 18 | dictionary |
  56. +--------+--------------------------+
  57. | 19 | array |
  58. +--------+--------------------------+
  59. | 20 | raw array |
  60. +--------+--------------------------+
  61. | 21 | int array |
  62. +--------+--------------------------+
  63. | 22 | real array |
  64. +--------+--------------------------+
  65. | 23 | string array |
  66. +--------+--------------------------+
  67. | 24 | vector2 array |
  68. +--------+--------------------------+
  69. | 25 | vector3 array |
  70. +--------+--------------------------+
  71. | 26 | color array |
  72. +--------+--------------------------+
  73. | 27 | max |
  74. +--------+--------------------------+
  75. Following this is the actual packet contents, which varies for each type
  76. of packet:
  77. 0: null
  78. ~~~~~~~
  79. 1: :ref:`bool<class_bool>`
  80. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  81. +----------+-------+-----------+---------------------------+
  82. | Offset | Len | Type | Description |
  83. +==========+=======+===========+===========================+
  84. | 4 | 4 | Integer | 0 for False, 1 for True |
  85. +----------+-------+-----------+---------------------------+
  86. 2: :ref:`int<class_int>`
  87. ~~~~~~~~~~~~~~~~~~~~~~~~
  88. +----------+-------+-----------+--------------------------+
  89. | Offset | Len | Type | Description |
  90. +==========+=======+===========+==========================+
  91. | 4 | 4 | Integer | Signed, 32-Bit Integer |
  92. +----------+-------+-----------+--------------------------+
  93. 3: :ref:`float<class_float>`/real
  94. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  95. +----------+-------+---------+-------------------------+
  96. | Offset | Len | Type | Description |
  97. +==========+=======+=========+=========================+
  98. | 4 | 4 | Float | IEE 754 32-Bits Float |
  99. +----------+-------+---------+-------------------------+
  100. 4: :ref:`String<class_string>`
  101. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  102. +----------+-------+-----------+----------------------------+
  103. | Offset | Len | Type | Description |
  104. +==========+=======+===========+============================+
  105. | 4 | 4 | Integer | String Length (in Bytes) |
  106. +----------+-------+-----------+----------------------------+
  107. | 8 | X | Bytes | UTF-8 Encoded String |
  108. +----------+-------+-----------+----------------------------+
  109. This field is padded to 4 bytes.
  110. 5: :ref:`Vector2<class_vector2>`
  111. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  112. +----------+-------+---------+----------------+
  113. | Offset | Len | Type | Description |
  114. +==========+=======+=========+================+
  115. | 4 | 4 | Float | X Coordinate |
  116. +----------+-------+---------+----------------+
  117. | 8 | 4 | Float | Y Coordinate |
  118. +----------+-------+---------+----------------+
  119. 6: :ref:`Rect2<class_rect2>`
  120. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  121. +----------+-------+---------+----------------+
  122. | Offset | Len | Type | Description |
  123. +==========+=======+=========+================+
  124. | 4 | 4 | Float | X Coordinate |
  125. +----------+-------+---------+----------------+
  126. | 8 | 4 | Float | Y Coordinate |
  127. +----------+-------+---------+----------------+
  128. | 12 | 4 | Float | X Size |
  129. +----------+-------+---------+----------------+
  130. | 16 | 4 | Float | Y Size |
  131. +----------+-------+---------+----------------+
  132. 7: :ref:`Vector3<class_vector3>`
  133. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  134. +----------+-------+---------+----------------+
  135. | Offset | Len | Type | Description |
  136. +==========+=======+=========+================+
  137. | 4 | 4 | Float | X Coordinate |
  138. +----------+-------+---------+----------------+
  139. | 8 | 4 | Float | Y Coordinate |
  140. +----------+-------+---------+----------------+
  141. | 12 | 4 | Float | Z Coordinate |
  142. +----------+-------+---------+----------------+
  143. 8: :ref:`Transform2D<class_transform2d>`
  144. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  145. +----------+-------+---------+---------------+
  146. | Offset | Len | Type | Description |
  147. +==========+=======+=========+===============+
  148. | 4 | 4 | Float | [0][0] |
  149. +----------+-------+---------+---------------+
  150. | 8 | 4 | Float | [0][1] |
  151. +----------+-------+---------+---------------+
  152. | 12 | 4 | Float | [1][0] |
  153. +----------+-------+---------+---------------+
  154. | 16 | 4 | Float | [1][1] |
  155. +----------+-------+---------+---------------+
  156. | 20 | 4 | Float | [2][0] |
  157. +----------+-------+---------+---------------+
  158. | 24 | 4 | Float | [2][1] |
  159. +----------+-------+---------+---------------+
  160. 9: :ref:`Plane<class_plane>`
  161. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  162. +----------+-------+---------+---------------+
  163. | Offset | Len | Type | Description |
  164. +==========+=======+=========+===============+
  165. | 4 | 4 | Float | Normal X |
  166. +----------+-------+---------+---------------+
  167. | 8 | 4 | Float | Normal Y |
  168. +----------+-------+---------+---------------+
  169. | 12 | 4 | Float | Normal Z |
  170. +----------+-------+---------+---------------+
  171. | 16 | 4 | Float | Distance |
  172. +----------+-------+---------+---------------+
  173. 10: :ref:`Quat<class_quat>`
  174. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  175. +----------+-------+---------+---------------+
  176. | Offset | Len | Type | Description |
  177. +==========+=======+=========+===============+
  178. | 4 | 4 | Float | Imaginary X |
  179. +----------+-------+---------+---------------+
  180. | 8 | 4 | Float | Imaginary Y |
  181. +----------+-------+---------+---------------+
  182. | 12 | 4 | Float | Imaginary Z |
  183. +----------+-------+---------+---------------+
  184. | 16 | 4 | Float | Real W |
  185. +----------+-------+---------+---------------+
  186. 11: :ref:`AABB<class_aabb>`
  187. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  188. +----------+-------+---------+----------------+
  189. | Offset | Len | Type | Description |
  190. +==========+=======+=========+================+
  191. | 4 | 4 | Float | X Coordinate |
  192. +----------+-------+---------+----------------+
  193. | 8 | 4 | Float | Y Coordinate |
  194. +----------+-------+---------+----------------+
  195. | 12 | 4 | Float | Z Coordinate |
  196. +----------+-------+---------+----------------+
  197. | 16 | 4 | Float | X Size |
  198. +----------+-------+---------+----------------+
  199. | 20 | 4 | Float | Y Size |
  200. +----------+-------+---------+----------------+
  201. | 24 | 4 | Float | Z Size |
  202. +----------+-------+---------+----------------+
  203. 12: :ref:`Basis<class_basis>`
  204. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  205. +----------+-------+---------+---------------+
  206. | Offset | Len | Type | Description |
  207. +==========+=======+=========+===============+
  208. | 4 | 4 | Float | [0][0] |
  209. +----------+-------+---------+---------------+
  210. | 8 | 4 | Float | [0][1] |
  211. +----------+-------+---------+---------------+
  212. | 12 | 4 | Float | [0][2] |
  213. +----------+-------+---------+---------------+
  214. | 16 | 4 | Float | [1][0] |
  215. +----------+-------+---------+---------------+
  216. | 20 | 4 | Float | [1][1] |
  217. +----------+-------+---------+---------------+
  218. | 24 | 4 | Float | [1][2] |
  219. +----------+-------+---------+---------------+
  220. | 28 | 4 | Float | [2][0] |
  221. +----------+-------+---------+---------------+
  222. | 32 | 4 | Float | [2][1] |
  223. +----------+-------+---------+---------------+
  224. | 36 | 4 | Float | [2][2] |
  225. +----------+-------+---------+---------------+
  226. 13: :ref:`Transform<class_transform>`
  227. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  228. +----------+-------+---------+---------------+
  229. | Offset | Len | Type | Description |
  230. +==========+=======+=========+===============+
  231. | 4 | 4 | Float | [0][0] |
  232. +----------+-------+---------+---------------+
  233. | 8 | 4 | Float | [0][1] |
  234. +----------+-------+---------+---------------+
  235. | 12 | 4 | Float | [0][2] |
  236. +----------+-------+---------+---------------+
  237. | 16 | 4 | Float | [1][0] |
  238. +----------+-------+---------+---------------+
  239. | 20 | 4 | Float | [1][1] |
  240. +----------+-------+---------+---------------+
  241. | 24 | 4 | Float | [1][2] |
  242. +----------+-------+---------+---------------+
  243. | 28 | 4 | Float | [2][0] |
  244. +----------+-------+---------+---------------+
  245. | 32 | 4 | Float | [2][1] |
  246. +----------+-------+---------+---------------+
  247. | 36 | 4 | Float | [2][2] |
  248. +----------+-------+---------+---------------+
  249. | 40 | 4 | Float | [3][0] |
  250. +----------+-------+---------+---------------+
  251. | 44 | 4 | Float | [3][1] |
  252. +----------+-------+---------+---------------+
  253. | 48 | 4 | Float | [3][2] |
  254. +----------+-------+---------+---------------+
  255. 14: :ref:`Color<class_color>`
  256. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  257. +----------+-------+---------+----------------+
  258. | Offset | Len | Type | Description |
  259. +==========+=======+=========+================+
  260. | 4 | 4 | Float | Red (0..1) |
  261. +----------+-------+---------+----------------+
  262. | 8 | 4 | Float | Green (0..1) |
  263. +----------+-------+---------+----------------+
  264. | 12 | 4 | Float | Blue (0..1) |
  265. +----------+-------+---------+----------------+
  266. | 16 | 4 | Float | Alpha (0..1) |
  267. +----------+-------+---------+----------------+
  268. 15: :ref:`NodePath<class_nodepath>`
  269. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  270. +----------+-------+-----------+-----------------------------------------------------------------------------------------+
  271. | Offset | Len | Type | Description |
  272. +==========+=======+===========+=========================================================================================+
  273. | 4 | 4 | Integer | String Length, or New Format (val&0x80000000!=0 and NameCount=val&0x7FFFFFFF) |
  274. +----------+-------+-----------+-----------------------------------------------------------------------------------------+
  275. For old format:
  276. ^^^^^^^^^^^^^^^
  277. +----------+-------+---------+------------------------+
  278. | Offset | Len | Type | Description |
  279. +==========+=======+=========+========================+
  280. | 8 | X | Bytes | UTF-8 Encoded String |
  281. +----------+-------+---------+------------------------+
  282. Padded to 4 bytes.
  283. For new format:
  284. ^^^^^^^^^^^^^^^
  285. +----------+-------+-----------+-------------------------------------+
  286. | Offset | Len | Type | Description |
  287. +==========+=======+===========+=====================================+
  288. | 4 | 4 | Integer | Sub-Name Count |
  289. +----------+-------+-----------+-------------------------------------+
  290. | 8 | 4 | Integer | Flags (absolute: val&1 != 0 ) |
  291. +----------+-------+-----------+-------------------------------------+
  292. For each Name and Sub-Name
  293. +----------+-------+-----------+------------------------+
  294. | Offset | Len | Type | Description |
  295. +==========+=======+===========+========================+
  296. | X+0 | 4 | Integer | String Length |
  297. +----------+-------+-----------+------------------------+
  298. | X+4 | X | Bytes | UTF-8 Encoded String |
  299. +----------+-------+-----------+------------------------+
  300. Every name string is padded to 4 bytes.
  301. 16: :ref:`RID<class_rid>` (unsupported)
  302. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  303. 17: :ref:`Object<class_object>` (unsupported)
  304. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  305. 18: :ref:`Dictionary<class_dictionary>`
  306. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  307. +----------+-------+-----------+---------------------------------------------------------------------+
  308. | Offset | Len | Type | Description |
  309. +==========+=======+===========+=====================================================================+
  310. | 4 | 4 | Integer | val&0x7FFFFFFF = elements, val&0x80000000 = shared (bool) |
  311. +----------+-------+-----------+---------------------------------------------------------------------+
  312. Then what follows is, for amount of "elements", pairs of key and value,
  313. one after the other, using this same format.
  314. 19: :ref:`Array<class_array>`
  315. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  316. +----------+-------+-----------+---------------------------------------------------------------------+
  317. | Offset | Len | Type | Description |
  318. +==========+=======+===========+=====================================================================+
  319. | 4 | 4 | Integer | val&0x7FFFFFFF = elements, val&0x80000000 = shared (bool) |
  320. +----------+-------+-----------+---------------------------------------------------------------------+
  321. Then what follows is, for amount of "elements", values one after the
  322. other, using this same format.
  323. 20: :ref:`PoolByteArray<class_poolbytearray>`
  324. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  325. +---------------+-------+-----------+------------------------+
  326. | Offset | Len | Type | Description |
  327. +===============+=======+===========+========================+
  328. | 4 | 4 | Integer | Array Length (Bytes) |
  329. +---------------+-------+-----------+------------------------+
  330. | 8..8+length | 1 | Byte | Byte (0..255) |
  331. +---------------+-------+-----------+------------------------+
  332. The array data is padded to 4 bytes.
  333. 21: :ref:`PoolIntArray<class_poolintarray>`
  334. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  335. +------------------+-------+-----------+---------------------------+
  336. | Offset | Len | Type | Description |
  337. +==================+=======+===========+===========================+
  338. | 4 | 4 | Integer | Array Length (Integers) |
  339. +------------------+-------+-----------+---------------------------+
  340. | 8..8+length\*4 | 4 | Integer | 32 Bits Signed Integer |
  341. +------------------+-------+-----------+---------------------------+
  342. 22: :ref:`PoolRealArray<class_poolrealarray>`
  343. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  344. +------------------+-------+-----------+---------------------------+
  345. | Offset | Len | Type | Description |
  346. +==================+=======+===========+===========================+
  347. | 4 | 4 |Integer | Array Length (Floats) |
  348. +------------------+-------+-----------+---------------------------+
  349. | 8..8+length\*4 | 4 |Integer | 32 Bits IEE 754 Float |
  350. +------------------+-------+-----------+---------------------------+
  351. 23: :ref:`PoolStringArray<class_poolstringarray>`
  352. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  353. +----------+-------+-----------+--------------------------+
  354. | Offset | Len | Type | Description |
  355. +==========+=======+===========+==========================+
  356. | 4 | 4 | Integer | Array Length (Strings) |
  357. +----------+-------+-----------+--------------------------+
  358. For each String:
  359. +----------+-------+-----------+------------------------+
  360. | Offset | Len | Type | Description |
  361. +==========+=======+===========+========================+
  362. | X+0 | 4 | Integer | String Length |
  363. +----------+-------+-----------+------------------------+
  364. | X+4 | X | Bytes | UTF-8 Encoded String |
  365. +----------+-------+-----------+------------------------+
  366. Every string is padded to 4 bytes.
  367. 24: :ref:`PoolVector2Array<class_poolvector2array>`
  368. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  369. +-------------------+-------+-----------+----------------+
  370. | Offset | Len | Type | Description |
  371. +===================+=======+===========+================+
  372. | 4 | 4 | Integer | Array Length |
  373. +-------------------+-------+-----------+----------------+
  374. | 8..8+length\*8 | 4 | Float | X Coordinate |
  375. +-------------------+-------+-----------+----------------+
  376. | 8..12+length\*8 | 4 | Float | Y Coordinate |
  377. +-------------------+-------+-----------+----------------+
  378. 25: :ref:`PoolVector3Array<class_poolvector3array>`
  379. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  380. +--------------------+-------+-----------+----------------+
  381. | Offset | Len | Type | Description |
  382. +====================+=======+===========+================+
  383. | 4 | 4 | Integer | Array Length |
  384. +--------------------+-------+-----------+----------------+
  385. | 8..8+length\*12 | 4 | Float | X Coordinate |
  386. +--------------------+-------+-----------+----------------+
  387. | 8..12+length\*12 | 4 | Float | Y Coordinate |
  388. +--------------------+-------+-----------+----------------+
  389. | 8..16+length\*12 | 4 | Float | Z Coordinate |
  390. +--------------------+-------+-----------+----------------+
  391. 26: :ref:`PoolColorArray<class_poolcolorarray>`
  392. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  393. +--------------------+-------+-----------+----------------+
  394. | Offset | Len | Type | Description |
  395. +====================+=======+===========+================+
  396. | 4 | 4 | Integer | Array Length |
  397. +--------------------+-------+-----------+----------------+
  398. | 8..8+length\*16 | 4 | Float | Red (0..1) |
  399. +--------------------+-------+-----------+----------------+
  400. | 8..12+length\*16 | 4 | Float | Green (0..1) |
  401. +--------------------+-------+-----------+----------------+
  402. | 8..16+length\*16 | 4 | Float | Blue (0..1) |
  403. +--------------------+-------+-----------+----------------+
  404. | 8..20+length\*16 | 4 | Float | Alpha (0..1) |
  405. +--------------------+-------+-----------+----------------+