@@ -8,6 +8,10 @@ var _connections: Array[Dictionary] : get = get_connections
88var _origin : Vector2 = Vector2 (INF , INF ) : get = get_origin
99
1010
11+ func _init (origin = Vector2 (INF , INF )) -> void :
12+ _origin = origin
13+
14+
1115func get_nodes_info () -> Dictionary [int , Dictionary ]:
1216 return _nodes
1317
@@ -44,7 +48,13 @@ func add_frame(current_id: int, position: Vector2, data: Dictionary) -> void:
4448
4549
4650func add_connections (connections : Array [Dictionary ]) -> void :
47- _connections .append_array (connections )
51+ for connection in connections :
52+ add_connection (connection )
53+
54+
55+ func add_connection (connection : Dictionary ) -> void :
56+ if not _connections .has (connection ):
57+ _connections .append (connection )
4858
4959
5060func get_node_type (id : int ) -> GaeaGraph .NodeType :
@@ -61,3 +71,79 @@ func get_node_data(id: int) -> Dictionary:
6171
6272func get_node_position (id : int ) -> Vector2 :
6373 return _nodes .get (id , {}).get (& "position" , get_origin ())
74+
75+
76+ func serialize () -> String :
77+ var nodes_data : Dictionary [int , Array ] = {}
78+ var connections : Array [String ] = []
79+
80+ for node_id : int in _nodes .keys ():
81+ var node_properties : Dictionary = _nodes .get (node_id , {})
82+ nodes_data .set (node_id , [
83+ node_properties .get (& "type" ),
84+ node_properties .get (& "data" ),
85+ ])
86+
87+ for connection in _connections :
88+ if nodes_data .has (connection .from_node ) and nodes_data .has (connection .to_node ):
89+ connections .append ("%s -%s -%s -%s " % [
90+ connection .get ("from_node" ),
91+ connection .get ("from_port" ),
92+ connection .get ("to_node" ),
93+ connection .get ("to_port" ),
94+ ])
95+
96+ return JSON .stringify ({
97+ "origin" : _origin ,
98+ "nodes" : nodes_data ,
99+ "connections" : connections
100+ }, "" , false )
101+
102+
103+ ## Deserialize a previously serialized GaeaNodesCopy,
104+ ## return a GaeaNodesCopy object or a string as error message.
105+ static func deserialize (serialized : String ) -> Variant :
106+ var data = JSON .parse_string (serialized )
107+
108+ if (
109+ not data is Dictionary
110+ or not data .get ("origin" ) is Vector2
111+ or not data .get ("nodes" ) is Dictionary
112+ or not data .get ("connections" ) is Array
113+ ):
114+ return "Invalid data provided: the data could not be parsed"
115+
116+ var origin : Vector2 = data [0 ]
117+ var deserialized : GaeaNodesCopy = GaeaNodesCopy .new (origin )
118+
119+ var nodes_data : Dictionary = data .get ("nodes" )
120+ for node_id in nodes_data .keys ():
121+ var node_data = nodes_data .get (node_id )
122+ if typeof (node_id ) != TYPE_INT or typeof (node_data ) != TYPE_ARRAY or not node_data [1 ] is Dictionary :
123+ return "Invalid data provided: the data could not be parsed"
124+ match node_data [0 ]:
125+ GaeaGraph .NodeType .NODE :
126+ var uid : String = node_data [1 ].get (& "uid" )
127+ var resource : GaeaNodeResource
128+ if GaeaNodeResource .is_valid_node_resource (uid ).is_empty ():
129+ resource = load (uid ).new ()
130+ else :
131+ resource = GaeaNodeInvalidScript .new ()
132+ resource .load_save_data (node_data [1 ])
133+ deserialized .add_node (node_id , resource , node_data [1 ].get (& "position" , origin ), node_data [1 ])
134+ GaeaGraph .NodeType .FRAME :
135+ deserialized .add_frame (node_id , node_data [1 ].get (& "position" , origin ), node_data [1 ])
136+ _ :
137+ return "Invalid data provided: the data could not be parsed"
138+
139+ var connections : Array = nodes_data .get ("connections" )
140+ @warning_ignore ("integer_division" )
141+ for connection_string : String in connections :
142+ var split_string := connection_string .split ("-" )
143+ deserialized .add_connection ({
144+ "from_node" : int (split_string [0 ]),
145+ "from_port" : int (split_string [1 ]),
146+ "to_node" : int (split_string [2 ]),
147+ "to_port" : int (split_string [3 ])
148+ })
149+ return deserialized
0 commit comments