From 477ed2fcdcda256746c6098fc646c814d2f6c543 Mon Sep 17 00:00:00 2001 From: klingbolt Date: Tue, 23 Jul 2024 20:05:57 -0400 Subject: [PATCH 1/5] Save the current data to the map files --- Spatial.gd | 71 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 30 deletions(-) diff --git a/Spatial.gd b/Spatial.gd index e7cc839d..a3479d0d 100644 --- a/Spatial.gd +++ b/Spatial.gd @@ -9,6 +9,7 @@ var map_is_open: bool var compass_is_top_right: bool var edit_panel_open: bool = false +var unsaved_changes: bool = false setget set_unsaved_changes # This is the path to the texture that will be used for the next created 3d-path # object or icon object in the UI @@ -389,11 +390,14 @@ func reset_3D_minimap_masks(category: Spatial): var waypoint_data = Waypoint.Waypoint.new() -var marker_file_dir = "user://protobins/" +#We save the marker data in this directory when the files are have been split +#by Map ID. All changes made by the editor are automatically saved in these +#files prior to export. +var unsaved_markers_dir = "user://protobins/" var marker_file_path = "" -func load_waypoint_markers(map_id): - self.marker_file_path = self.marker_file_dir + String(map_id) + ".bin" +func load_waypoint_markers(map_id_to_load: int): + self.marker_file_path = self.unsaved_markers_dir + String(map_id_to_load) + ".bin" self.waypoint_data = Waypoint.Waypoint.new() clear_map_markers() init_category_tree() @@ -537,7 +541,7 @@ func _waypoint_categories_to_godot_nodes(item: TreeItem, waypoint_category: Wayp if texture_id == null: print("Warning: No texture found in " , category_name) continue - var full_texture_path = self.marker_file_dir + self.waypoint_data.get_textures()[texture_id].get_filepath() + var full_texture_path = self.unsaved_markers_dir + self.waypoint_data.get_textures()[texture_id].get_filepath() gen_new_trail(full_texture_path, trail, category_item) @@ -546,7 +550,7 @@ func _waypoint_categories_to_godot_nodes(item: TreeItem, waypoint_category: Wayp if texture_id == null: print("Warning: No texture found in " , category_name) continue - var full_texture_path = self.marker_file_dir + self.waypoint_data.get_textures()[texture_id].get_filepath() + var full_texture_path = self.unsaved_markers_dir + self.waypoint_data.get_textures()[texture_id].get_filepath() gen_new_icon(full_texture_path, icon, category_item) for category_child in waypoint_category.get_children(): @@ -621,31 +625,30 @@ func gen_new_icon(texture_path: String, waypoint_icon: Waypoint.Icon, category_i var category_data = category_item.get_metadata(0) category_data.category3d.add_icon(new_icon) -# This function take all of the currently rendered objects and converts it into -# the data format that is saved/loaded from. -func data_from_renderview(): - var icons_data = [] - var paths_data = [] - - for icon in $Icons.get_children(): - icons_data.append({ - "position": [icon.translation.x, icon.translation.y, -icon.translation.z], - "texture": icon.texture_path - }) - - for path in $Paths.get_children(): - #print(path) - var points = [] - for point in range(path.get_point_count()): - var point_position:Vector3 = path.get_point_position(point) - points.append([point_position.x, point_position.y, -point_position.z]) - paths_data.append({ - "points": points, - "texture": path.texture_path - }) - - var data_out = {"icons": icons_data, "paths": paths_data} - return data_out +################################################################################ +# Section of functions for saving data to file +################################################################################ +func set_unsaved_changes(value): + if self.unsaved_changes != value: + unsaved_changes = value + update_burrito_icon() + +func update_burrito_icon(): + if self.unsaved_changes: + #TODO: Determine if this is the best color and alpha value to use + $Control/GlobalMenuButton/TextureRect.modulate = ColorN("red", 1) + $Control/GlobalMenuButton/main_menu_toggle.hint_tooltip = "Unsaved Data" + else: + $Control/GlobalMenuButton/TextureRect.modulate = ColorN("white", 0.44) + $Control/GlobalMenuButton/main_menu_toggle.hint_tooltip = "" + +func save_current_map_data(): + var packed_bytes = self.waypoint_data.to_bytes() + var file = File.new() + file.open(self.marker_file_path, file.WRITE) + file.store_buffer(packed_bytes) + set_unsaved_changes(false) + ################################################################################ # Adjustment and gizmo functions @@ -764,6 +767,7 @@ func set_icon_position(new_position: Vector3, waypoint_icon: Waypoint.Icon, icon position.set_y(new_position.y) position.set_z(new_position.z) icon.set_position(new_position) + set_unsaved_changes(true) func remove_icon(waypoint_icon: Waypoint.Icon, icon: Sprite3D): if icon.waypoint != waypoint_icon: @@ -849,14 +853,19 @@ func new_trail_point_after(waypoint_trail: Waypoint.Trail, trail3d: Spatial, tra func refresh_trail3d_points(trail3d: Spatial): trail3d.refresh_mesh() + set_unsaved_changes(true) func refresh_trail2d_points(trail2d: Line2D): trail2d.refresh_points() + set_unsaved_changes(true) ################################################################################ # Signal Functions ################################################################################ +func _on_SaveTrail_pressed(): + save_current_map_data() + func _on_main_menu_toggle_pressed(): $Control/Dialogs/MainMenu.show() set_maximal_mouse_block() @@ -1011,6 +1020,8 @@ func _on_ReverseTrailDirection_pressed(): func _on_ExitButton_pressed(): + if self.unsaved_changes: + save_current_map_data() exit_burrito() From e0212162ff23d6c58dafda5843fbb138498e6289 Mon Sep 17 00:00:00 2001 From: klingbolt Date: Tue, 23 Jul 2024 22:00:33 -0400 Subject: [PATCH 2/5] Added a check to save if the map changes --- Spatial.gd | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Spatial.gd b/Spatial.gd index a3479d0d..37709a4a 100644 --- a/Spatial.gd +++ b/Spatial.gd @@ -341,6 +341,8 @@ func decode_context_packet(spb: StreamPeerBuffer): if self.map_id != old_map_id: print("New Map") print("Saving Old Map") + if self.unsaved_changes: + save_current_map_data() print("Loading New Map") load_waypoint_markers(self.map_id) From 5d0832d1833abbf45bf8fc54f1dbea0a9e2e8f8c Mon Sep 17 00:00:00 2001 From: klingbolt Date: Tue, 30 Jul 2024 23:13:23 -0400 Subject: [PATCH 3/5] Added hashing functionality and moved import function to allow simpler calling of hashing functions --- Spatial.gd | 86 +++++++++++++++++++++++++++++++++++----------------- Spatial.tscn | 6 ++-- 2 files changed, 61 insertions(+), 31 deletions(-) diff --git a/Spatial.gd b/Spatial.gd index 37709a4a..0b06b83a 100644 --- a/Spatial.gd +++ b/Spatial.gd @@ -9,7 +9,6 @@ var map_is_open: bool var compass_is_top_right: bool var edit_panel_open: bool = false -var unsaved_changes: bool = false setget set_unsaved_changes # This is the path to the texture that will be used for the next created 3d-path # object or icon object in the UI @@ -57,6 +56,7 @@ const gizmo_scene = preload("res://Gizmo/PointEdit.tscn") # Scripts containing code used by this scene const CategoryData = preload("res://CategoryData.gd") const Waypoint = preload("res://waypoint.gd") +const FileHandler = preload("res://FileHandler.gd") ##########Node Connections########### onready var markers_ui := $Control/Dialogs/CategoriesDialog/MarkersUI as Tree @@ -340,9 +340,11 @@ func decode_context_packet(spb: StreamPeerBuffer): if self.map_id != old_map_id: print("New Map") - print("Saving Old Map") - if self.unsaved_changes: - save_current_map_data() + if old_map_id != 0: + if not read_hash(old_map_id) == get_hash(self.waypoint_data.to_bytes()): + print("Saving Old Map") + save_map_data(old_map_id) + print("not same") print("Loading New Map") load_waypoint_markers(self.map_id) @@ -395,7 +397,7 @@ var waypoint_data = Waypoint.Waypoint.new() #We save the marker data in this directory when the files are have been split #by Map ID. All changes made by the editor are automatically saved in these #files prior to export. -var unsaved_markers_dir = "user://protobins/" +var unsaved_markers_dir = "user://protobin_by_map_id/" var marker_file_path = "" func load_waypoint_markers(map_id_to_load: int): @@ -630,27 +632,46 @@ func gen_new_icon(texture_path: String, waypoint_icon: Waypoint.Icon, category_i ################################################################################ # Section of functions for saving data to file ################################################################################ -func set_unsaved_changes(value): - if self.unsaved_changes != value: - unsaved_changes = value - update_burrito_icon() - -func update_burrito_icon(): - if self.unsaved_changes: - #TODO: Determine if this is the best color and alpha value to use - $Control/GlobalMenuButton/TextureRect.modulate = ColorN("red", 1) - $Control/GlobalMenuButton/main_menu_toggle.hint_tooltip = "Unsaved Data" - else: - $Control/GlobalMenuButton/TextureRect.modulate = ColorN("white", 0.44) - $Control/GlobalMenuButton/main_menu_toggle.hint_tooltip = "" - -func save_current_map_data(): +func save_map_data(map_id: int): var packed_bytes = self.waypoint_data.to_bytes() var file = File.new() file.open(self.marker_file_path, file.WRITE) file.store_buffer(packed_bytes) - set_unsaved_changes(false) +func get_hash(data: PoolByteArray) -> String: + var ctx = HashingContext.new() + ctx.start(HashingContext.HASH_MD5) + ctx.update(data) + var res: PoolByteArray = ctx.finish() + return res.hex_encode() + +const FILE_HASH_PATH: String = "user://file_hash.json" + +# Save all hashes +func save_hashes(): + var file = File.new() + var data = {} + var dir = Directory.new() + dir.open(self.unsaved_markers_dir) + dir.list_dir_begin() + var file_name = dir.get_next() + while file_name != "": + if dir.file_exists(file_name): + file.open(file_name, File.READ) + var hash_data = get_hash(file.get_buffer(file.get_len())) + data[file_name.get_basename()] = hash_data + file_name = dir.get_next() + file.open(FILE_HASH_PATH, File.WRITE) + file.store_string(JSON.print(data)) + +func read_hash(map_id: int) -> String: + var file = File.new() + if not file.file_exists(FILE_HASH_PATH): + return "" + var data = {} + file.open(FILE_HASH_PATH, File.READ) + data = JSON.parse(file.get_as_text()).result + return data.get(String(map_id), "") ################################################################################ # Adjustment and gizmo functions @@ -769,7 +790,6 @@ func set_icon_position(new_position: Vector3, waypoint_icon: Waypoint.Icon, icon position.set_y(new_position.y) position.set_z(new_position.z) icon.set_position(new_position) - set_unsaved_changes(true) func remove_icon(waypoint_icon: Waypoint.Icon, icon: Sprite3D): if icon.waypoint != waypoint_icon: @@ -855,18 +875,16 @@ func new_trail_point_after(waypoint_trail: Waypoint.Trail, trail3d: Spatial, tra func refresh_trail3d_points(trail3d: Spatial): trail3d.refresh_mesh() - set_unsaved_changes(true) func refresh_trail2d_points(trail2d: Line2D): trail2d.refresh_points() - set_unsaved_changes(true) ################################################################################ # Signal Functions ################################################################################ func _on_SaveTrail_pressed(): - save_current_map_data() + save_map_data(self.map_id) func _on_main_menu_toggle_pressed(): $Control/Dialogs/MainMenu.show() @@ -1022,8 +1040,8 @@ func _on_ReverseTrailDirection_pressed(): func _on_ExitButton_pressed(): - if self.unsaved_changes: - save_current_map_data() + if not read_hash(self.map_id) == get_hash(self.waypoint_data.to_bytes()): + save_map_data(self.map_id) exit_burrito() @@ -1050,3 +1068,17 @@ func _on_MarkersUI_item_edited(): func _on_ImportPath_pressed(): $Control/Dialogs/ImportPackDialog.show() + + +func _on_ImportPackDialog_dir_selected(dir): + var user_data_dir = str(OS.get_user_data_dir()) + var args: PoolStringArray = [ + "--input-taco-path", dir, + # TODO: This line is not working as intended and needs to be investigated + #"--input-waypoint-path", user_data_dir.plus_file("protobin"), + "--output-waypoint-path", user_data_dir.plus_file("protobin"), + "--output-split-waypoint-path", ProjectSettings.globalize_path(self.unsaved_markers_dir) + ] + FileHandler.call_xml_converter(args) + save_hashes() + load_waypoint_markers(self.map_id) diff --git a/Spatial.tscn b/Spatial.tscn index 22a1adfc..d67e2225 100644 --- a/Spatial.tscn +++ b/Spatial.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=19 format=2] +[gd_scene load_steps=18 format=2] [ext_resource path="res://Spatial.gd" type="Script" id=1] [ext_resource path="res://shaders/range_indicators.shader" type="Shader" id=2] @@ -12,7 +12,6 @@ [ext_resource path="res://icon_new_point.png" type="Texture" id=11] [ext_resource path="res://SettingsDialog.gd" type="Script" id=12] [ext_resource path="res://Category3D.gd" type="Script" id=13] -[ext_resource path="res://ImportPackDialog.gd" type="Script" id=14] [ext_resource path="res://Category2D.gd" type="Script" id=15] [sub_resource type="Shader" id=1] @@ -193,7 +192,6 @@ mode = 2 access = 2 current_dir = "" current_path = "" -script = ExtResource( 14 ) __meta__ = { "_edit_use_anchors_": false } @@ -886,7 +884,7 @@ material/0 = SubResource( 4 ) [connection signal="pressed" from="Control/GlobalMenuButton/EditorQuckPanel/HBoxContainer/NewTrailPoint" to="." method="_on_NewTrailPoint_pressed"] [connection signal="pressed" from="Control/GlobalMenuButton/EditorQuckPanel/HBoxContainer/NewIcon" to="." method="_on_NewIcon_pressed"] [connection signal="pressed" from="Control/GlobalMenuButton/EditorQuckPanel/HBoxContainer/AdjustPoints" to="." method="_on_AdjustNodesButton_pressed"] -[connection signal="dir_selected" from="Control/Dialogs/ImportPackDialog" to="Control/Dialogs/ImportPackDialog" method="_on_FileDialog_dir_selected"] +[connection signal="dir_selected" from="Control/Dialogs/ImportPackDialog" to="." method="_on_ImportPackDialog_dir_selected"] [connection signal="hide" from="Control/Dialogs/ImportPackDialog" to="." method="_on_Dialog_hide"] [connection signal="hide" from="Control/Dialogs/MainMenu" to="." method="_on_Dialog_hide"] [connection signal="pressed" from="Control/Dialogs/MainMenu/ScrollContainer/VBoxContainer/LoadTrail" to="." method="_on_LoadTrail_pressed"] From 981cf43ff90425bd00c1964d2ae6a2d57231465e Mon Sep 17 00:00:00 2001 From: klingbolt Date: Tue, 30 Jul 2024 23:15:52 -0400 Subject: [PATCH 4/5] added spaced in comments --- Spatial.gd | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Spatial.gd b/Spatial.gd index 0b06b83a..fbc1b684 100644 --- a/Spatial.gd +++ b/Spatial.gd @@ -394,9 +394,9 @@ func reset_3D_minimap_masks(category: Spatial): var waypoint_data = Waypoint.Waypoint.new() -#We save the marker data in this directory when the files are have been split -#by Map ID. All changes made by the editor are automatically saved in these -#files prior to export. +# We save the marker data in this directory when the files are have been split +# by Map ID. All changes made by the editor are automatically saved in these +# files prior to export. var unsaved_markers_dir = "user://protobin_by_map_id/" var marker_file_path = "" @@ -1075,7 +1075,7 @@ func _on_ImportPackDialog_dir_selected(dir): var args: PoolStringArray = [ "--input-taco-path", dir, # TODO: This line is not working as intended and needs to be investigated - #"--input-waypoint-path", user_data_dir.plus_file("protobin"), + # "--input-waypoint-path", user_data_dir.plus_file("protobin"), "--output-waypoint-path", user_data_dir.plus_file("protobin"), "--output-split-waypoint-path", ProjectSettings.globalize_path(self.unsaved_markers_dir) ] From d4944b7bd67dd5cad12b10a84312aa2df10ec72d Mon Sep 17 00:00:00 2001 From: klingbolt Date: Wed, 31 Jul 2024 23:32:02 -0400 Subject: [PATCH 5/5] addressing code review --- Spatial.gd | 32 +++++++++++++------------------- Spatial.tscn | 1 - 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/Spatial.gd b/Spatial.gd index fbc1b684..12e82926 100644 --- a/Spatial.gd +++ b/Spatial.gd @@ -58,6 +58,9 @@ const CategoryData = preload("res://CategoryData.gd") const Waypoint = preload("res://waypoint.gd") const FileHandler = preload("res://FileHandler.gd") +# File path for the the json that contains a hash of the data files +const HASH_BY_MAP_ID_FILEPATH: String = "user://hash_by_map_id.json" + ##########Node Connections########### onready var markers_ui := $Control/Dialogs/CategoriesDialog/MarkersUI as Tree onready var markers_3d := $Markers3D as Spatial @@ -340,11 +343,9 @@ func decode_context_packet(spb: StreamPeerBuffer): if self.map_id != old_map_id: print("New Map") - if old_map_id != 0: - if not read_hash(old_map_id) == get_hash(self.waypoint_data.to_bytes()): - print("Saving Old Map") - save_map_data(old_map_id) - print("not same") + if old_map_id != 0 and not read_hash(old_map_id) == make_hash(self.waypoint_data.to_bytes()): + print("Saving Old Map") + save_map_data(old_map_id) print("Loading New Map") load_waypoint_markers(self.map_id) @@ -638,14 +639,13 @@ func save_map_data(map_id: int): file.open(self.marker_file_path, file.WRITE) file.store_buffer(packed_bytes) -func get_hash(data: PoolByteArray) -> String: +func make_hash(data: PoolByteArray) -> String: var ctx = HashingContext.new() ctx.start(HashingContext.HASH_MD5) ctx.update(data) var res: PoolByteArray = ctx.finish() return res.hex_encode() -const FILE_HASH_PATH: String = "user://file_hash.json" # Save all hashes func save_hashes(): @@ -658,20 +658,17 @@ func save_hashes(): while file_name != "": if dir.file_exists(file_name): file.open(file_name, File.READ) - var hash_data = get_hash(file.get_buffer(file.get_len())) - data[file_name.get_basename()] = hash_data + data[file_name.get_basename()] = make_hash(file.get_buffer(file.get_len())) file_name = dir.get_next() - file.open(FILE_HASH_PATH, File.WRITE) + file.open(HASH_BY_MAP_ID_FILEPATH, File.WRITE) file.store_string(JSON.print(data)) func read_hash(map_id: int) -> String: var file = File.new() - if not file.file_exists(FILE_HASH_PATH): + if not file.file_exists(HASH_BY_MAP_ID_FILEPATH): return "" - var data = {} - file.open(FILE_HASH_PATH, File.READ) - data = JSON.parse(file.get_as_text()).result - return data.get(String(map_id), "") + file.open(HASH_BY_MAP_ID_FILEPATH, File.READ) + return JSON.parse(file.get_as_text()).result.get(String(map_id), "") ################################################################################ # Adjustment and gizmo functions @@ -883,9 +880,6 @@ func refresh_trail2d_points(trail2d: Line2D): ################################################################################ # Signal Functions ################################################################################ -func _on_SaveTrail_pressed(): - save_map_data(self.map_id) - func _on_main_menu_toggle_pressed(): $Control/Dialogs/MainMenu.show() set_maximal_mouse_block() @@ -1040,7 +1034,7 @@ func _on_ReverseTrailDirection_pressed(): func _on_ExitButton_pressed(): - if not read_hash(self.map_id) == get_hash(self.waypoint_data.to_bytes()): + if not read_hash(self.map_id) == make_hash(self.waypoint_data.to_bytes()): save_map_data(self.map_id) exit_burrito() diff --git a/Spatial.tscn b/Spatial.tscn index d67e2225..3483cd77 100644 --- a/Spatial.tscn +++ b/Spatial.tscn @@ -911,7 +911,6 @@ material/0 = SubResource( 4 ) [connection signal="value_changed" from="Control/Dialogs/RangesDialog/GridContainer/SpinBox7" to="Control/Dialogs/RangesDialog" method="on_change"] [connection signal="file_selected" from="Control/Dialogs/TexturePathOpen" to="." method="_on_TexturePathOpen_file_selected"] [connection signal="hide" from="Control/Dialogs/TexturePathOpen" to="." method="_on_Dialog_hide"] -[connection signal="file_selected" from="Control/Dialogs/SaveDialog" to="." method="_on_SaveDialog_file_selected"] [connection signal="hide" from="Control/Dialogs/SaveDialog" to="." method="_on_Dialog_hide"] [connection signal="hide" from="Control/Dialogs/NodeEditorDialog" to="." method="_on_NodeEditorDialog_hide"] [connection signal="pressed" from="Control/Dialogs/NodeEditorDialog/ScrollContainer/VBoxContainer/DeleteNode" to="." method="_on_DeleteNode_pressed"]