Skip to content

Commit 96a7da3

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 6b7c9d2 commit 96a7da3

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

addons/block_code/serialization/block_script_serialization.gd

Lines changed: 24 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,16 @@ 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 block_name != "get_resource_file_path":
194+
return null
195+
196+
if file_path.is_empty() or not FileAccess.file_exists(file_path) or not ResourceLoader.exists(file_path):
197+
return null
198+
199+
return BlocksCatalog.get_resource_block_definition(file_path)
200+
201+
185202
func _update_block_definitions():
186203
_available_blocks.clear()
187204
_available_blocks.append_array(_get_inherited_block_definitions())
@@ -251,14 +268,15 @@ func _tree_to_ast(tree: BlockSerializationTree) -> BlockAST:
251268

252269
func _block_to_ast_node(node: BlockSerialization) -> BlockAST.ASTNode:
253270
var ast_node := BlockAST.ASTNode.new()
254-
ast_node.data = get_block_definition(node.name)
255271

256272
for arg_name in node.arguments:
257273
var argument = node.arguments[arg_name]
258274
if argument is ValueBlockSerialization:
259275
argument = _value_to_ast_value(argument)
260276
ast_node.arguments[arg_name] = argument
261277

278+
ast_node.data = get_block_definition(node.name, ast_node.arguments)
279+
262280
var children: Array[BlockAST.ASTNode]
263281
for c in node.children:
264282
children.append(_block_to_ast_node(c))
@@ -269,14 +287,15 @@ func _block_to_ast_node(node: BlockSerialization) -> BlockAST.ASTNode:
269287

270288
func _value_to_ast_value(value_node: ValueBlockSerialization) -> BlockAST.ASTValueNode:
271289
var ast_value_node := BlockAST.ASTValueNode.new()
272-
ast_value_node.data = get_block_definition(value_node.name)
273290

274291
for arg_name in value_node.arguments:
275292
var argument = value_node.arguments[arg_name]
276293
if argument is ValueBlockSerialization:
277294
argument = _value_to_ast_value(argument)
278295
ast_value_node.arguments[arg_name] = argument
279296

297+
ast_value_node.data = get_block_definition(value_node.name, ast_value_node.arguments)
298+
280299
return ast_value_node
281300

282301

0 commit comments

Comments
 (0)