aboutsummaryrefslogtreecommitdiff
path: root/game/src/Autoload/SaveManager.gd
diff options
context:
space:
mode:
author Spartan322 <Megacake1234@gmail.com>2023-04-23 22:01:49 +0200
committer Spartan322 <Megacake1234@gmail.com>2023-05-12 13:32:21 +0200
commit8ab166ae0bd784145c3706aba0c3f3326cbbaac1 (patch)
treee4511c51eb4ceaefd92a7ad8a00f42910eb83811 /game/src/Autoload/SaveManager.gd
parent398ba6f05ae1bb13e59790436bf0da299e5a399d (diff)
Add SaveManager autoload singleton
Globally handles save finding, creation, removal, changes, naming, and flushing Globally handles the session tags for the active save Add SaveLoadMenu to GameSession Add Start Date Panel and Save Panel loading to LobbyMenu Includes session tag filter Add LobbyPanelButton and SavePanelButton scene and scripts Add SaveResource to handle saving and loading individual save files Add localization for SaveLoadMenu Expand localization for additions to LobbyMenu Add ButtonContainer variation type to default theme Fulfill: UI-82, UI-83, UI-84, UI-85, UI-87, UI-89, UI-90, UI-91, UI-92, UI-94 UIFUN-82, UIFUN-83, UIFUN-84, UIFUN-86, UIFUN-87, UIFUN-89 FS-28
Diffstat (limited to 'game/src/Autoload/SaveManager.gd')
-rw-r--r--game/src/Autoload/SaveManager.gd55
1 files changed, 55 insertions, 0 deletions
diff --git a/game/src/Autoload/SaveManager.gd b/game/src/Autoload/SaveManager.gd
new file mode 100644
index 0000000..c653b2c
--- /dev/null
+++ b/game/src/Autoload/SaveManager.gd
@@ -0,0 +1,55 @@
+extends Node
+
+# Requirements
+# * FS-28
+const save_directory_setting := &"openvic2/data/saves_directory"
+
+var current_save : SaveResource
+var current_session_tag : StringName
+
+var _save_dictionary : Dictionary = {}
+var _dirty_save : SaveResource
+
+func _ready():
+ var saves_dir_path : String = ProjectSettings.get_setting_with_override(save_directory_setting)
+ assert(saves_dir_path != null, "'%s' setting could not be found." % save_directory_setting)
+
+ DirAccess.make_dir_recursive_absolute(saves_dir_path)
+ var saves_dir := DirAccess.open(saves_dir_path)
+ for file in saves_dir.get_files():
+ var save := SaveResource.new()
+ save.load_save(saves_dir_path.path_join(file))
+ add_or_replace_save(save, true)
+
+func get_save_file_name(save_name : StringName, session_tag : StringName = current_session_tag) -> StringName:
+ return ("%s - %s" % [save_name, session_tag]).validate_filename()
+
+func make_new_save(save_name : String, session_tag : StringName = current_session_tag) -> SaveResource:
+ var file_name := get_save_file_name(save_name, session_tag) + ".tres"
+ var new_save := SaveResource.new()
+ new_save.set_file_path(save_name, ProjectSettings.get_setting_with_override(save_directory_setting).path_join(file_name))
+ print(new_save.file_path)
+ new_save.session_tag = session_tag
+ return new_save
+
+func has_save(save_name : StringName, session_tag : StringName = current_session_tag) -> bool:
+ return _save_dictionary.has(get_save_file_name(save_name, session_tag))
+
+func add_or_replace_save(save : SaveResource, ignore_dirty : bool = false) -> void:
+ var binded_func := _on_save_deleted_or_moved.bind(save)
+ save.deleted.connect(binded_func)
+ save.trash_moved.connect(binded_func)
+ _save_dictionary[get_save_file_name(save.save_name, save.session_tag)] = save
+ if not ignore_dirty:
+ _dirty_save = save
+
+func delete_save(save : SaveResource) -> void:
+ save.delete()
+
+func flush_save() -> void:
+ if _dirty_save == null: return
+ _dirty_save.flush_save()
+ _dirty_save = null
+
+func _on_save_deleted_or_moved(save : SaveResource) -> void:
+ _save_dictionary.erase(get_save_file_name(save.save_name, save.session_tag))