frame-of-mind/src/dev-util/savegame.gd

77 lines
2.5 KiB
GDScript3
Raw Normal View History

2024-10-01 23:26:54 +00:00
@tool
class_name SaveGame extends Resource
@export var filepath: String
@export var unique_save_name: String = "frame_of_mind_%s_%s" % [Time.get_date_string_from_system(), Time.get_time_string_from_system()]
2024-10-01 23:26:54 +00:00
@export var current_room: State.rooms = 0
@export var mementos_complete: int = 0
@export var board_state: Dictionary = {}
@export var thumbnail: Texture = preload("res://import/interface-elements/empty_save_slot.png")
2024-10-16 10:24:38 +00:00
@export var last_saved: float = Time.get_unix_time_from_system()
@export var is_save_file_valid: bool = false
# FIXME: for some reason this does not prevent the property from being saved.
func _validate_property(property: Dictionary):
if property.name == "thumbnail":
property.usage != PROPERTY_USAGE_STORAGE
func _init(filepath = "") -> void:
if filepath == "":
filepath = "%s/%s" % [State.user_saves_path, unique_save_name]
read_save_file()
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: Image
if FileAccess.file_exists("%s/thumbnails/%s.png" % [State.user_saves_path, filepath]):
tmp_img = Image.load_from_file("%s/thumbnails/%s.png" % [State.user_saves_path, filepath])
is_save_file_valid = (
2025-02-24 15:06:21 +00:00
parsed["unique_save_name"] is String and
parsed["current_room"] is int and
parsed["mementos_complete"] is int and
parsed["coard_state"] is Dictionary and
parsed["last_saved"] is float and last_saved != 0 and
parsed["tmp_img"] is Image
)
if is_save_file_valid:
for key in parsed.keys():
set(key, parsed[key])
if tmp_img != null:
thumbnail = ImageTexture.create_from_image(tmp_img)
func _get_save_dict() -> Dictionary:
return {
"unique_save_name": unique_save_name,
"current_room": current_room,
"mementos_complete": mementos_complete,
"board_state": board_state,
"last_saved": last_saved
}
func save_to_file(current_screen: Texture):
return
2024-10-16 10:24:38 +00:00
last_saved = Time.get_unix_time_from_system()
var thumbnail_image: Image = current_screen.get_image()
2025-02-06 18:15:39 +00:00
thumbnail_image.resize(384, 261, Image.INTERPOLATE_LANCZOS) # nonexistent call in ViewportTexturew
thumbnail_image.save_png("%s/thumbnails/%s.png" % [State.user_saves_path, filepath])
var file = FileAccess.open("%s.json:" % filepath, FileAccess.WRITE)
file.store_string(JSON.stringify(_get_save_dict()))