Skip to content

Add version to block script data #122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion addons/block_code/block_script_data/block_script_data.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ extends Resource
@export var script_inherits: String
@export var block_trees: SerializedBlockTreeNodeArray
@export var generated_script: String
@export var version: int


func _init(p_script_inherits: String = "", p_block_trees: SerializedBlockTreeNodeArray = null, p_generated_script: String = ""):
func _init(p_script_inherits: String = "", p_block_trees: SerializedBlockTreeNodeArray = null, p_generated_script: String = "", p_version = 0):
script_inherits = p_script_inherits
block_trees = p_block_trees
generated_script = p_generated_script
version = p_version
2 changes: 2 additions & 0 deletions addons/block_code/ui/constants.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
extends Object

const CURRENT_DATA_VERSION = 0

const KNOB_X = 10.0
const KNOB_W = 20.0
const KNOB_H = 5.0
Expand Down
13 changes: 13 additions & 0 deletions addons/block_code/ui/main_panel.gd
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ extends Control
@onready var _icon_collapse := EditorInterface.get_editor_theme().get_icon("Back", "EditorIcons")
@onready var _icon_expand := EditorInterface.get_editor_theme().get_icon("Forward", "EditorIcons")

const Constants = preload("res://addons/block_code/ui/constants.gd")

var _current_block_code_node: BlockCode
var _block_code_nodes: Array
var _collapsed: bool = false
Expand Down Expand Up @@ -82,6 +84,14 @@ func _on_delete_dialog_confirmed(block_code_node: BlockCode):
undo_redo.commit_action()


func _try_migration():
var version: int = _current_block_code_node.block_script.version
if version == Constants.CURRENT_DATA_VERSION:
# No migration needed.
return
push_warning("Migration not implemented from %d to %d" % [version, Constants.CURRENT_DATA_VERSION])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is good enough for now, but in the future we might want to beef this up. If the stored data version is less than the current data version, migrate it. If the stored data version is greater than the current data version, then someone has already migrated the data to a newer plugin version and is now trying to run an older version of the plugin. Since the older plugin version by definition doesn't know about the newer data format, I think the only thing to do there would be to refuse to load the script and bring up some kind of error message.



func switch_scene(scene_root: Node):
_title_bar.scene_selected(scene_root)

Expand All @@ -90,6 +100,8 @@ func switch_block_code_node(block_code_node: BlockCode):
var block_script: BlockScriptData = block_code_node.block_script if block_code_node else null
_current_block_code_node = block_code_node
_delete_node_button.disabled = _current_block_code_node == null
if _current_block_code_node != null:
_try_migration()
_picker.bsd_selected(block_script)
_title_bar.bsd_selected(block_script)
_block_canvas.bsd_selected(block_script)
Expand Down Expand Up @@ -129,6 +141,7 @@ func save_script():
var generated_script = _block_canvas.generate_script_from_current_window(block_script.script_inherits)
block_script.block_trees = block_trees
block_script.generated_script = generated_script
block_script.version = Constants.CURRENT_DATA_VERSION
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we arbitrarily update the version in the saved script if it hasn't been migrated? This is probably fine for now but eventually I'd expect save_script to bail if _current_block_code_node.version != Constants.CURRENT_DATA_VERSION. Maybe generate_script_from_current_window is the one doing the heavy lifting there and should return null if it can't understand the blocks on screen.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is TBD. I guess we'll try a migration and if that fails the blocks shouldn't be editable at all, can't be saved.


undo_redo.add_do_property(_current_block_code_node.block_script, "block_trees", block_trees)
undo_redo.add_do_property(_current_block_code_node.block_script, "generated_script", generated_script)
Expand Down
Loading