Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
8253403746
|
|||
a2c493b81e
|
|||
295d2cd838
|
|||
457af2015b
|
|||
701077fa88
|
|||
c77584fd0d
|
1
BSON Examples/dunk.gd.uid
Normal file
1
BSON Examples/dunk.gd.uid
Normal file
@ -0,0 +1 @@
|
|||||||
|
uid://ct7fwu1uxvpmt
|
@ -1,6 +1,6 @@
|
|||||||
[gd_scene load_steps=2 format=3 uid="uid://ckdfb5ggwslbk"]
|
[gd_scene load_steps=2 format=3 uid="uid://ckdfb5ggwslbk"]
|
||||||
|
|
||||||
[ext_resource type="Script" path="res://BSON Examples/dunk.gd" id="1_p38ab"]
|
[ext_resource type="Script" uid="uid://ct7fwu1uxvpmt" path="res://BSON Examples/dunk.gd" id="1_p38ab"]
|
||||||
|
|
||||||
[node name="Dunk" type="Control" node_paths=PackedStringArray("JSONEditor", "OutputLabel", "CopyPopup")]
|
[node name="Dunk" type="Control" node_paths=PackedStringArray("JSONEditor", "OutputLabel", "CopyPopup")]
|
||||||
layout_mode = 3
|
layout_mode = 3
|
||||||
@ -57,7 +57,8 @@ text = "{
|
|||||||
true,
|
true,
|
||||||
false,
|
false,
|
||||||
3.14159265,
|
3.14159265,
|
||||||
\"baz\"
|
\"baz\",
|
||||||
|
null
|
||||||
]
|
]
|
||||||
}"
|
}"
|
||||||
middle_mouse_paste_enabled = false
|
middle_mouse_paste_enabled = false
|
||||||
@ -116,7 +117,6 @@ horizontal_alignment = 1
|
|||||||
autowrap_mode = 3
|
autowrap_mode = 3
|
||||||
|
|
||||||
[node name="CopyPopup" type="PopupPanel" parent="."]
|
[node name="CopyPopup" type="PopupPanel" parent="."]
|
||||||
transparent_bg = true
|
|
||||||
position = Vector2i(492, 586)
|
position = Vector2i(492, 586)
|
||||||
size = Vector2i(164, 31)
|
size = Vector2i(164, 31)
|
||||||
|
|
||||||
|
@ -19,17 +19,19 @@ static func get_byte_type(value: Variant) -> int:
|
|||||||
return 0x02
|
return 0x02
|
||||||
TYPE_INT:
|
TYPE_INT:
|
||||||
if abs(value as int) < 2147483647: # 32 bit signed integer limit
|
if abs(value as int) < 2147483647: # 32 bit signed integer limit
|
||||||
return 0x10
|
return 0x10 # 32 bit signed int
|
||||||
else:
|
else:
|
||||||
return 0x12
|
return 0x12 # 64 bit signed int
|
||||||
TYPE_FLOAT:
|
TYPE_FLOAT:
|
||||||
return 0x01
|
return 0x01 # Double
|
||||||
TYPE_ARRAY:
|
TYPE_ARRAY:
|
||||||
return 0x04
|
return 0x04
|
||||||
TYPE_DICTIONARY:
|
TYPE_DICTIONARY:
|
||||||
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
|
||||||
@ -71,7 +73,7 @@ static func dictionary_to_bytes(dict: Dictionary) -> PackedByteArray:
|
|||||||
buffer.append(get_byte_type(dict[key]))
|
buffer.append(get_byte_type(dict[key]))
|
||||||
var key_string_bytes := key.to_utf8_buffer()
|
var key_string_bytes := key.to_utf8_buffer()
|
||||||
buffer.append_array(key_string_bytes)
|
buffer.append_array(key_string_bytes)
|
||||||
buffer.append(0x00)
|
buffer.append(0x00) # Index string null terminator
|
||||||
buffer.append_array(serialize_variant(dict[key]))
|
buffer.append_array(serialize_variant(dict[key]))
|
||||||
|
|
||||||
return buffer
|
return buffer
|
||||||
@ -82,10 +84,10 @@ static func array_to_bytes(array: Array[Variant]) -> PackedByteArray:
|
|||||||
for index: int in range(array.size()):
|
for index: int in range(array.size()):
|
||||||
buffer.append(get_byte_type(array[index]))
|
buffer.append(get_byte_type(array[index]))
|
||||||
# For whatever reason, BSON wants array indexes to be strings. This makes no sense.
|
# For whatever reason, BSON wants array indexes to be strings. This makes no sense.
|
||||||
|
# The string indexes are literally just the number but in ASCII/UTF8.
|
||||||
var s_index := str(index)
|
var s_index := str(index)
|
||||||
buffer.append_array(s_index.to_utf8_buffer())
|
buffer.append_array(s_index.to_utf8_buffer())
|
||||||
buffer.append(0x00)
|
buffer.append(0x00) # Index string null terminator
|
||||||
|
|
||||||
buffer.append_array(serialize_variant(array[index]))
|
buffer.append_array(serialize_variant(array[index]))
|
||||||
|
|
||||||
return buffer
|
return buffer
|
||||||
@ -124,6 +126,8 @@ static func serialize_variant(data: Variant) -> PackedByteArray:
|
|||||||
|
|
||||||
|
|
||||||
static func from_bson(data: PackedByteArray) -> Dictionary:
|
static func from_bson(data: PackedByteArray) -> Dictionary:
|
||||||
|
if data[-1] != 0x00:
|
||||||
|
push_error("BSON deserialization error: Document is not null terminated. It is likely that the provided buffer is not BSON.")
|
||||||
return Deserializer.new(data).read_dictionary()
|
return Deserializer.new(data).read_dictionary()
|
||||||
|
|
||||||
|
|
||||||
@ -173,7 +177,7 @@ class Deserializer:
|
|||||||
var b_char := get_int8()
|
var b_char := get_int8()
|
||||||
if b_char == 0x00: break
|
if b_char == 0x00: break
|
||||||
s_value += char(b_char)
|
s_value += char(b_char)
|
||||||
if expected_size != iter: # Check if the string is terminated with 0x00
|
if expected_size != iter:
|
||||||
push_error("BSON deserialization error: String was the wrong size."
|
push_error("BSON deserialization error: String was the wrong size."
|
||||||
+ " Position: "
|
+ " Position: "
|
||||||
+ str(read_position - iter)
|
+ str(read_position - iter)
|
||||||
@ -212,6 +216,7 @@ 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
|
||||||
_:
|
_:
|
||||||
push_error("BSON deserialization error: Unsupported type "
|
push_error("BSON deserialization error: Unsupported type "
|
||||||
+ str(type)
|
+ str(type)
|
||||||
@ -259,6 +264,7 @@ 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)
|
||||||
_: 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."
|
||||||
|
1
addons/bson/bson.gd.uid
Normal file
1
addons/bson/bson.gd.uid
Normal file
@ -0,0 +1 @@
|
|||||||
|
uid://tbtkc5qan4gw
|
@ -1,7 +0,0 @@
|
|||||||
[plugin]
|
|
||||||
|
|
||||||
name="BSON"
|
|
||||||
description="A BSON class to serialize and deserialize BSON in GDScript."
|
|
||||||
author="Colormatic Studios"
|
|
||||||
version=""
|
|
||||||
script="plugin.gd"
|
|
@ -1,20 +0,0 @@
|
|||||||
@tool
|
|
||||||
extends EditorPlugin
|
|
||||||
|
|
||||||
|
|
||||||
const AUTOLOAD_NAME = "BSON"
|
|
||||||
|
|
||||||
|
|
||||||
#func _enter_tree() -> void:
|
|
||||||
# Initialization of the plugin goes here.
|
|
||||||
#pass
|
|
||||||
|
|
||||||
#func _exit_tree() -> void:
|
|
||||||
# Clean-up of the plugin goes here.
|
|
||||||
#pass
|
|
||||||
|
|
||||||
func _enable_plugin() -> void:
|
|
||||||
add_autoload_singleton(AUTOLOAD_NAME, "res://addons/bson/bson.gd")
|
|
||||||
|
|
||||||
func _disable_plugin() -> void:
|
|
||||||
remove_autoload_singleton(AUTOLOAD_NAME)
|
|
@ -12,9 +12,5 @@ config_version=5
|
|||||||
|
|
||||||
config/name="BSON for Godot"
|
config/name="BSON for Godot"
|
||||||
run/main_scene="res://BSON Examples/dunk.tscn"
|
run/main_scene="res://BSON Examples/dunk.tscn"
|
||||||
config/features=PackedStringArray("4.3", "Forward Plus")
|
config/features=PackedStringArray("4.4", "Forward Plus")
|
||||||
config/icon="res://icon.svg"
|
config/icon="res://icon.svg"
|
||||||
|
|
||||||
[editor_plugins]
|
|
||||||
|
|
||||||
enabled=PackedStringArray("res://addons/bson/plugin.cfg")
|
|
||||||
|
Reference in New Issue
Block a user