From efcc3bf694d225e4012fcd6dca6533842d395960 Mon Sep 17 00:00:00 2001 From: betalars Date: Thu, 3 Oct 2024 01:10:35 +0200 Subject: [PATCH] implement loading and saving raw data to save file resource --- src/dev-util/savegame.gd | 65 ++++++++++++++++++- .../main menu/save_game_display.gd | 2 +- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/src/dev-util/savegame.gd b/src/dev-util/savegame.gd index 449458b..96804ff 100644 --- a/src/dev-util/savegame.gd +++ b/src/dev-util/savegame.gd @@ -1,8 +1,69 @@ @tool class_name SaveGame extends Resource +@export var filepath: String +@export var save_slot_id: int +@export var unique_save_name: String = "frame_of_mind_save_slot_%d_%s_%s" % [save_slot_id, Time.get_date_string_from_system(), Time.get_time_string_from_system()] @export var current_room: State.rooms = 0 @export var mementos_complete: int = 0 -@export var board_state: PackedScene -@export var thumbnail: Texture2D = preload("res://import/interface-elements/empty_save_slot.png") +@export var board_state: Dictionary +@export var thumbnail: Image = Image.load_from_file("res://import/interface-elements/empty_save_slot.png") @export var last_saved: Dictionary = Time.get_datetime_dict_from_system() + +@export var is_save_file_valid: bool = false + +func _init(filepath = "") -> void: + if filepath == "": + filepath = "usr://saves/%s" % unique_save_name + + read_save_file() + +func _get_save_dict() -> Dictionary: + return { + "save_slot_id": save_slot_id, + "unique_save_name": unique_save_name, + "current_room": current_room, + "mementos_complete": mementos_complete, + "board_state": board_state, + "last_saved": last_saved + } + + +func read_save_file(): + if FileAccess.file_exists("%s.json:" % filepath): + + + var file = FileAccess.open("%s.json:" % filepath, FileAccess.READ) + var raw_json = FileAccess.get_file_as_string("%s.json:" % filepath) + file.close() + var parsed: Dictionary = JSON.parse_string(raw_json) + + var tmp_img + if FileAccess.file_exists("%s.png" % filepath): + tmp_img = Image.load_from_file("%s.png" % filepath) + + is_save_file_valid = ( + save_slot_id is int and + unique_save_name is String and + current_room is int and + mementos_complete is int and + board_state is Dictionary and + last_saved is Dictionary and + Time.get_unix_time_from_datetime_dict(last_saved) != 0 and + tmp_img is Image + ) + + if is_save_file_valid: + for key in parsed.keys(): + set(key, parsed[key]) + + thumbnail = tmp_img + +func save_file(current_screen: ImageTexture): + thumbnail = current_screen.get_image() + current_screen.resize(384, 261, Image.INTERPOLATE_LANCZOS) + + thumbnail.save_png("%s.png" % filepath) + + var file = FileAccess.open("%s.json:" % filepath, FileAccess.WRITE) + file.store_string(JSON.stringify(_get_save_dict())) diff --git a/src/logic-scenes/main menu/save_game_display.gd b/src/logic-scenes/main menu/save_game_display.gd index 85ba426..91f5007 100644 --- a/src/logic-scenes/main menu/save_game_display.gd +++ b/src/logic-scenes/main menu/save_game_display.gd @@ -6,7 +6,7 @@ func _init(save: SaveGame, id: int) -> void: add_child(base_container) var texture:= TextureRect.new() - texture.texture = save.thumbnail + texture.texture = ImageTexture.create_from_image(save.thumbnail) texture.size_flags_vertical = Control.SIZE_SHRINK_CENTER base_container.add_child(texture)