Skip to content

Commit

Permalink
feat: mode encrypted worked correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
MatiasVME committed Feb 10, 2024
1 parent 98b5ffd commit 2454fd5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
57 changes: 35 additions & 22 deletions addons/persistence_node/persistence_node.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions examples/data_manager_example.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2454fd5

Please sign in to comment.