Skip to content

Commit 714c31f

Browse files
committed
BlockScriptSerialization: Re-generate block definition for resource file blocks
Drag & drop resouce files as blocks into canvas has been implemented. This commit focuses on serializing from the resource file blocks to AST nodes and values. The resource file's path is saved as in the arguments "file_path" feild. So, pass the arguments, which may have "file_path" into get_block_definition() to rebuild resource file's block definition. https://phabricator.endlessm.com/T35712
1 parent 2125148 commit 714c31f

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

addons/block_code/serialization/block_script_serialization.gd

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func instantiate_block(block_definition: BlockDefinition) -> Block:
7272

7373

7474
func instantiate_block_by_name(block_name: String) -> Block:
75-
var block_definition := get_block_definition(block_name)
75+
var block_definition := get_block_definition(block_name, {})
7676

7777
if block_definition == null:
7878
push_warning("Cannot find a block definition for %s" % block_name)
@@ -81,12 +81,14 @@ func instantiate_block_by_name(block_name: String) -> Block:
8181
return instantiate_block(block_definition)
8282

8383

84-
func get_block_definition(block_name: String) -> BlockDefinition:
84+
func get_block_definition(block_name: String, arguments: Dictionary) -> BlockDefinition:
8585
var split := block_name.split(":", true, 1)
8686
var block_definition: BlockDefinition
8787

8888
if len(split) > 1:
89-
return _get_parameter_block_definition(split[0], split[1])
89+
block_definition = _get_parameter_block_definition(split[0], split[1])
90+
if block_definition:
91+
return block_definition
9092

9193
block_definition = _get_base_block_definition(block_name)
9294
if block_definition != null:
@@ -96,6 +98,11 @@ func get_block_definition(block_name: String) -> BlockDefinition:
9698
if block_definition != null:
9799
return block_definition
98100

101+
var file_path = arguments.get("file_path", "")
102+
block_definition = _get_resource_block_definition(block_name, file_path)
103+
if block_definition != null:
104+
return block_definition
105+
99106
# FIXME: This is a workaround for old-style output block references.
100107
# These were generated ahead of time using a block name that has
101108
# a "_" before the parameter name. Now, these parameter blocks
@@ -182,6 +189,17 @@ func _get_parent_node_property_info(property_name: String) -> Dictionary:
182189
return {}
183190

184191

192+
func _get_resource_block_definition(block_name: String, file_path: String) -> BlockDefinition:
193+
if not block_name.begins_with("get_resource_"):
194+
return null
195+
196+
if file_path == "":
197+
return null
198+
199+
var file_name = file_path.get_file()
200+
return BlocksCatalog.get_resource_block_definition(file_name)
201+
202+
185203
func _update_block_definitions():
186204
_available_blocks.clear()
187205
_available_blocks.append_array(_get_inherited_block_definitions())
@@ -251,14 +269,15 @@ func _tree_to_ast(tree: BlockSerializationTree) -> BlockAST:
251269

252270
func _block_to_ast_node(node: BlockSerialization) -> BlockAST.ASTNode:
253271
var ast_node := BlockAST.ASTNode.new()
254-
ast_node.data = get_block_definition(node.name)
255272

256273
for arg_name in node.arguments:
257274
var argument = node.arguments[arg_name]
258275
if argument is ValueBlockSerialization:
259276
argument = _value_to_ast_value(argument)
260277
ast_node.arguments[arg_name] = argument
261278

279+
ast_node.data = get_block_definition(node.name, ast_node.arguments)
280+
262281
var children: Array[BlockAST.ASTNode]
263282
for c in node.children:
264283
children.append(_block_to_ast_node(c))
@@ -269,14 +288,15 @@ func _block_to_ast_node(node: BlockSerialization) -> BlockAST.ASTNode:
269288

270289
func _value_to_ast_value(value_node: ValueBlockSerialization) -> BlockAST.ASTValueNode:
271290
var ast_value_node := BlockAST.ASTValueNode.new()
272-
ast_value_node.data = get_block_definition(value_node.name)
273291

274292
for arg_name in value_node.arguments:
275293
var argument = value_node.arguments[arg_name]
276294
if argument is ValueBlockSerialization:
277295
argument = _value_to_ast_value(argument)
278296
ast_value_node.arguments[arg_name] = argument
279297

298+
ast_value_node.data = get_block_definition(value_node.name, ast_value_node.arguments)
299+
280300
return ast_value_node
281301

282302

0 commit comments

Comments
 (0)