Skip to content

Commit

Permalink
BlockScriptSerialization: Re-generate block definition for resource f…
Browse files Browse the repository at this point in the history
…ile 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
  • Loading branch information
starnight committed Nov 27, 2024
1 parent 6b7c9d2 commit 96a7da3
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions addons/block_code/serialization/block_script_serialization.gd
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func instantiate_block(block_definition: BlockDefinition) -> Block:


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

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


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

if len(split) > 1:
return _get_parameter_block_definition(split[0], split[1])
block_definition = _get_parameter_block_definition(split[0], split[1])
if block_definition:
return block_definition

block_definition = _get_base_block_definition(block_name)
if block_definition != null:
Expand All @@ -96,6 +98,11 @@ func get_block_definition(block_name: String) -> BlockDefinition:
if block_definition != null:
return block_definition

var file_path = arguments.get("file_path", "")
block_definition = _get_resource_block_definition(block_name, file_path)
if block_definition != null:
return block_definition

# FIXME: This is a workaround for old-style output block references.
# These were generated ahead of time using a block name that has
# a "_" before the parameter name. Now, these parameter blocks
Expand Down Expand Up @@ -182,6 +189,16 @@ func _get_parent_node_property_info(property_name: String) -> Dictionary:
return {}


func _get_resource_block_definition(block_name: String, file_path: String) -> BlockDefinition:
if block_name != "get_resource_file_path":
return null

if file_path.is_empty() or not FileAccess.file_exists(file_path) or not ResourceLoader.exists(file_path):
return null

return BlocksCatalog.get_resource_block_definition(file_path)


func _update_block_definitions():
_available_blocks.clear()
_available_blocks.append_array(_get_inherited_block_definitions())
Expand Down Expand Up @@ -251,14 +268,15 @@ func _tree_to_ast(tree: BlockSerializationTree) -> BlockAST:

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

for arg_name in node.arguments:
var argument = node.arguments[arg_name]
if argument is ValueBlockSerialization:
argument = _value_to_ast_value(argument)
ast_node.arguments[arg_name] = argument

ast_node.data = get_block_definition(node.name, ast_node.arguments)

var children: Array[BlockAST.ASTNode]
for c in node.children:
children.append(_block_to_ast_node(c))
Expand All @@ -269,14 +287,15 @@ func _block_to_ast_node(node: BlockSerialization) -> BlockAST.ASTNode:

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

for arg_name in value_node.arguments:
var argument = value_node.arguments[arg_name]
if argument is ValueBlockSerialization:
argument = _value_to_ast_value(argument)
ast_value_node.arguments[arg_name] = argument

ast_value_node.data = get_block_definition(value_node.name, ast_value_node.arguments)

return ast_value_node


Expand Down

0 comments on commit 96a7da3

Please sign in to comment.