|
|
@ -2,7 +2,10 @@
|
|
|
|
# This file is the BSON serializer for the Godot Engine,
|
|
|
|
# This file is the BSON serializer for the Godot Engine,
|
|
|
|
# published under the MIT license. https://opensource.org/license/MIT
|
|
|
|
# published under the MIT license. https://opensource.org/license/MIT
|
|
|
|
|
|
|
|
|
|
|
|
class_name BSON
|
|
|
|
extends Node
|
|
|
|
|
|
|
|
# Unfortunately, this has to be a node in order to be a singleton.
|
|
|
|
|
|
|
|
# I'd rather BSON wasn't in the scenetree, but it seems like that's
|
|
|
|
|
|
|
|
# the only way to do this. Hopefully this will change in the future.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static func to_bson(data: Dictionary) -> PackedByteArray:
|
|
|
|
static func to_bson(data: Dictionary) -> PackedByteArray:
|
|
|
@ -30,6 +33,8 @@ static func get_byte_type(value: Variant) -> int:
|
|
|
|
return 0x03
|
|
|
|
return 0x03
|
|
|
|
TYPE_BOOL:
|
|
|
|
TYPE_BOOL:
|
|
|
|
return 0x08
|
|
|
|
return 0x08
|
|
|
|
|
|
|
|
TYPE_NIL:
|
|
|
|
|
|
|
|
return 0x0a
|
|
|
|
_:
|
|
|
|
_:
|
|
|
|
push_error("BSON serialization error: Unsupported type: ", typeof(value))
|
|
|
|
push_error("BSON serialization error: Unsupported type: ", typeof(value))
|
|
|
|
return 0x00
|
|
|
|
return 0x00
|
|
|
@ -186,6 +191,9 @@ class Deserializer:
|
|
|
|
func get_bool() -> bool:
|
|
|
|
func get_bool() -> bool:
|
|
|
|
return (get_int8() == 1)
|
|
|
|
return (get_int8() == 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func get_null() -> void:
|
|
|
|
|
|
|
|
read_position += 1
|
|
|
|
|
|
|
|
|
|
|
|
func read_dictionary() -> Dictionary:
|
|
|
|
func read_dictionary() -> Dictionary:
|
|
|
|
var object = {}
|
|
|
|
var object = {}
|
|
|
|
|
|
|
|
|
|
|
@ -212,6 +220,9 @@ class Deserializer:
|
|
|
|
0x08: object[key] = get_bool()
|
|
|
|
0x08: object[key] = get_bool()
|
|
|
|
0x04: object[key] = read_array()
|
|
|
|
0x04: object[key] = read_array()
|
|
|
|
0x03: object[key] = read_dictionary()
|
|
|
|
0x03: object[key] = read_dictionary()
|
|
|
|
|
|
|
|
0x0a:
|
|
|
|
|
|
|
|
object[key] = null
|
|
|
|
|
|
|
|
get_null()
|
|
|
|
_:
|
|
|
|
_:
|
|
|
|
push_error("BSON deserialization error: Unsupported type "
|
|
|
|
push_error("BSON deserialization error: Unsupported type "
|
|
|
|
+ str(type)
|
|
|
|
+ str(type)
|
|
|
@ -259,6 +270,9 @@ class Deserializer:
|
|
|
|
0x08: array.append(get_bool())
|
|
|
|
0x08: array.append(get_bool())
|
|
|
|
0x04: array.append(read_array())
|
|
|
|
0x04: array.append(read_array())
|
|
|
|
0x03: array.append(read_dictionary())
|
|
|
|
0x03: array.append(read_dictionary())
|
|
|
|
|
|
|
|
0x0a:
|
|
|
|
|
|
|
|
array.append(null)
|
|
|
|
|
|
|
|
get_null()
|
|
|
|
_: push_error("BSON deserialization error: Unsupported type: " + str(type))
|
|
|
|
_: push_error("BSON deserialization error: Unsupported type: " + str(type))
|
|
|
|
if iter > expected_size:
|
|
|
|
if iter > expected_size:
|
|
|
|
push_warning("BSON deserialization warning: Array is the wrong length."
|
|
|
|
push_warning("BSON deserialization warning: Array is the wrong length."
|
|
|
|