Skip to content

Commit

Permalink
Merge pull request #323 from klingbolt/save_data
Browse files Browse the repository at this point in the history
Save the current data to the map files
  • Loading branch information
AsherGlick authored Aug 1, 2024
2 parents 69eaab4 + d4944b7 commit 0b55fb7
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 36 deletions.
101 changes: 70 additions & 31 deletions Spatial.gd
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ 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")

# 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
Expand Down Expand Up @@ -339,7 +343,9 @@ func decode_context_packet(spb: StreamPeerBuffer):

if self.map_id != old_map_id:
print("New Map")
print("Saving Old Map")
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)

Expand Down Expand Up @@ -389,11 +395,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://protobin_by_map_id/"
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()
Expand Down Expand Up @@ -537,7 +546,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)


Expand All @@ -546,7 +555,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():
Expand Down Expand Up @@ -621,31 +630,45 @@ 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 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)

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()


# 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)
data[file_name.get_basename()] = make_hash(file.get_buffer(file.get_len()))
file_name = dir.get_next()
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(HASH_BY_MAP_ID_FILEPATH):
return ""
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
Expand Down Expand Up @@ -1011,6 +1034,8 @@ func _on_ReverseTrailDirection_pressed():


func _on_ExitButton_pressed():
if not read_hash(self.map_id) == make_hash(self.waypoint_data.to_bytes()):
save_map_data(self.map_id)
exit_burrito()


Expand All @@ -1037,3 +1062,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)
7 changes: 2 additions & 5 deletions Spatial.tscn
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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]
Expand Down Expand Up @@ -193,7 +192,6 @@ mode = 2
access = 2
current_dir = ""
current_path = ""
script = ExtResource( 14 )
__meta__ = {
"_edit_use_anchors_": false
}
Expand Down Expand Up @@ -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"]
Expand All @@ -913,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"]
Expand Down

0 comments on commit 0b55fb7

Please sign in to comment.