From 2454fd57efb8889cdf2e6a3d52e71f5acbb2f3d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Valent=C3=ADn=20Mu=C3=B1oz=20Espinoza?= Date: Sat, 10 Feb 2024 08:18:35 -0300 Subject: [PATCH] feat: mode encrypted worked correctly --- addons/persistence_node/persistence_node.gd | 57 +++++++++++++-------- examples/data_manager_example.tscn | 2 + 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/addons/persistence_node/persistence_node.gd b/addons/persistence_node/persistence_node.gd index 06bbfef..161ccb3 100644 --- a/addons/persistence_node/persistence_node.gd +++ b/addons/persistence_node/persistence_node.gd @@ -46,7 +46,7 @@ const FILE_PATH_EMPTY = '_file_path is empty' @export var file_name := "data" @export var extention_env_production := ".binary" @export var extention_env_development := ".json" -@export var password_env_production:= "" +@export var password_env_production := "" @export_category("Enviroment") @@ -82,35 +82,49 @@ var data := {} : if dir_exists: match mode: Mode.ENV_PRODUCTION: - _file_path = str("user://" + folder_name + "/" + file_name + extention_env_production) - - file = FileAccess.open_encrypted_with_pass( - _file_path, - FileAccess.READ, - password_env_production + _file_path = str( + "user://" + + folder_name + + "/" + + file_name + + extention_env_production ) - _error = FileAccess.get_open_error() - if not is_instance_valid(file): + var file_exists = FileAccess.file_exists(_file_path) + + # open_encrypted_with_pass() doesn't support WRITE_READ mode + # https://github.com/godotengine/godot/issues/32881 + + if file_exists: file = FileAccess.open_encrypted_with_pass( _file_path, - FileAccess.WRITE_READ, + FileAccess.READ, password_env_production ) - _error = FileAccess.get_open_error() + + _can_save_without_warning = true + data = file.get_var(store_objects) + else: + file = FileAccess.open_encrypted_with_pass( + _file_path, + FileAccess.WRITE, + password_env_production + ) + file.store_var(data, store_objects) - var content : Dictionary = file.get_var(store_objects) - file.close() + _error = FileAccess.get_open_error() - _can_save_without_warning = true + if is_instance_valid(file): + file.close() - if not content: - data = {} - else: - data = content - Mode.ENV_DEVELOPMENT: - _file_path = str("user://" + folder_name + "/" + file_name + extention_env_development) + _file_path = str( + "user://" + + folder_name + + "/" + + file_name + + extention_env_development + ) file = FileAccess.open(_file_path, FileAccess.READ) @@ -201,10 +215,9 @@ func delete_file(): _one_charge = true data.clear() - print(typeof(data)) if _error != OK: - _debug_persistence_node(str(CANT_FILE_DELETE, " Error: ", _error)) + _debug_persistence_node(str(CANT_FILE_DELETE, " - ERROR: ", _error)) elif _file_path.is_empty(): _debug_persistence_node(str(FILE_PATH_EMPTY)) else: diff --git a/examples/data_manager_example.tscn b/examples/data_manager_example.tscn index 75bb31e..639cbed 100644 --- a/examples/data_manager_example.tscn +++ b/examples/data_manager_example.tscn @@ -16,11 +16,13 @@ script = ExtResource("1_xe0pl") script = ExtResource("1_kwirt") folder_name = "saves_example" file_name = "character_data" +password_env_production = "312312" [node name="GeneralConfigData" type="Node" parent="."] script = ExtResource("1_kwirt") folder_name = "saves_example" file_name = "general_config_data" +password_env_production = "31312" [node name="Container" type="CenterContainer" parent="."] layout_mode = 1