2
2
class_name BlockScriptSerialization
3
3
extends Resource
4
4
5
+ const ASTList = preload ("res://addons/block_code/code_generation/ast_list.gd" )
6
+ const BlockAST = preload ("res://addons/block_code/code_generation/block_ast.gd" )
5
7
const BlocksCatalog = preload ("res://addons/block_code/code_generation/blocks_catalog.gd" )
6
8
const BlockCategory = preload ("res://addons/block_code/ui/picker/categories/block_category.gd" )
7
9
const BlockDefinition = preload ("res://addons/block_code/code_generation/block_definition.gd" )
10
+ const BlockSerialization = preload ("res://addons/block_code/serialization/block_serialization.gd" )
8
11
const BlockSerializationTree = preload ("res://addons/block_code/serialization/block_serialization_tree.gd" )
12
+ const ValueBlockSerialization = preload ("res://addons/block_code/serialization/value_block_serialization.gd" )
9
13
const VariableDefinition = preload ("res://addons/block_code/code_generation/variable_definition.gd" )
10
14
11
15
@export var script_inherits : String
12
16
@export var block_serialization_trees : Array [BlockSerializationTree ]
13
- @export var variables : Array [VariableDefinition ]
17
+ @export var variables : Array [VariableDefinition ]:
18
+ set = _set_variables
14
19
@export var generated_script : String
15
20
@export var version : int
16
21
22
+ var _var_block_definitions : Dictionary # String, BlockDefinition
23
+
17
24
18
25
func _init (
19
26
p_script_inherits : String = "" , p_block_serialization_trees : Array [BlockSerializationTree ] = [], p_variables : Array [VariableDefinition ] = [], p_generated_script : String = "" , p_version = 0
@@ -25,6 +32,24 @@ func _init(
25
32
version = p_version
26
33
27
34
35
+ func _set_variables (value ):
36
+ variables = value
37
+ _refresh_var_block_definitions ()
38
+
39
+
40
+ func _refresh_var_block_definitions ():
41
+ _var_block_definitions .clear ()
42
+ for block_def in BlocksCatalog .get_variable_block_definitions (variables ):
43
+ _var_block_definitions [block_def .name ] = block_def
44
+
45
+
46
+ func _get_block (block_name : StringName ) -> BlockDefinition :
47
+ var block_def : BlockDefinition = _var_block_definitions .get (block_name )
48
+ if block_def == null :
49
+ block_def = BlocksCatalog .get_block (block_name )
50
+ return block_def
51
+
52
+
28
53
func get_definitions () -> Array [BlockDefinition ]:
29
54
for class_dict in ProjectSettings .get_global_class_list ():
30
55
if class_dict .class == script_inherits :
@@ -44,3 +69,86 @@ func get_categories() -> Array[BlockCategory]:
44
69
return script .get_custom_categories ()
45
70
46
71
return []
72
+
73
+
74
+ func generate_ast_list () -> ASTList :
75
+ var a
10000
st_list := ASTList .new ()
76
+ for tree in block_serialization_trees :
77
+ var ast : BlockAST = _tree_to_ast (tree )
78
+ ast_list .append (ast , tree .canvas_position )
79
+ return ast_list
80
+
81
+
82
+ func _tree_to_ast (tree : BlockSerializationTree ) -> BlockAST:
83
+ var ast : BlockAST = BlockAST .new ()
84
+ ast .root = _block_to_ast_node (tree .root )
85
+ return ast
86
+
87
+
88
+ func _block_to_ast_node (node : BlockSerialization ) -> BlockAST.ASTNode :
89
+ var ast_node := BlockAST .ASTN ode.new ()
90
+ ast_node .data = _get_block (node .name )
91
+
92
+ for arg_name in node .arguments :
93
+ var argument = node .arguments [arg_name ]
94
+ if argument is ValueBlockSerialization :
95
+ argument = _value_to_ast_value (argument )
96
+ ast_node .arguments [arg_name ] = argument
97
+
98
+ var children : Array [BlockAST .ASTNode ]
99
+ for c in node .children :
100
+ children .append (_block_to_ast_node (c ))
101
+ ast_node .children = children
102
+
103
+ return ast_node
104
+
105
+
106
+ func _value_to_ast_value (value_node : ValueBlockSerialization ) -> BlockAST.ASTValueNode :
107
+ var ast_value_node := BlockAST .ASTV alueNode.new ()
108
+ ast_value_node .data = _get_block (value_node .name )
109
+
110
+ for arg_name in value_node .arguments :
111
+ var argument = value_node .arguments [arg_name ]
112
+ if argument is ValueBlockSerialization :
113
+ argument = _value_to_ast_value (argument )
114
+ ast_value_node .arguments [arg_name ] = argument
115
+
116
+ return ast_value_node
117
+
118
+
119
+ func update_from_ast_list (ast_list : ASTList ):
120
+ var trees : Array [BlockSerializationTree ]
121
+ for ast_pair in ast_list .array :
122
+ var root := _block_from_ast_node (ast_pair .ast .root )
123
+ var tree := BlockSerializationTree .new (root , ast_pair .canvas_position )
124
+ trees .append (tree )
125
+ block_serialization_trees = trees
126
+
127
+
128
+ func _block_from_ast_node (ast_node : BlockAST .ASTNode ) -> BlockSerialization :
129
+ var block := BlockSerialization .new (ast_node .data .name )
130
+
131
+ for arg_name in ast_node .arguments :
132
+ var argument = ast_node .arguments [arg_name ]
133
+ if argument is BlockAST .ASTValueNode :
134
+ argument = _value_from_ast_value (argument )
135
+ block .arguments [arg_name ] = argument
136
+
137
+ var children : Array [BlockSerialization ]
138
+ for c in ast_node .children :
139
+ children .append (_block_from_ast_node (c ))
140
+ block .children = children
141
+
142
+ return block
143
+
144
+
145
+ func _value_from_ast_value (ast_node : BlockAST .ASTValueNode ) -> ValueBlockSerialization :
146
+ var value := ValueBlockSerialization .new (ast_node .data .name )
147
+
148
+ for arg_name in ast_node .arguments :
149
+ var argument = ast_node .arguments [arg_name ]
150
+ if argument is BlockAST .ASTValueNode :
151
+ argument = _value_from_ast_value (argument )
152
+ value .arguments [arg_name ] = argument
153
+
154
+ return value
0 commit comments