Compare commits

...

1 Commits

3 changed files with 17 additions and 9 deletions

View File

@@ -17,6 +17,8 @@ static func run(old_dir: String, new_dir: String) -> int:
if old_dir == new_dir or FileAccess.file_exists(new_dir.path_join(MARKER)) or not DirAccess.dir_exists_absolute(old_dir):
return 0
var res := _copy_missing(old_dir, new_dir)
if res[0] > 0: # нові відкриті файли: хай save.gd ще раз їх зашифрує, а не вважає підробкою
DirAccess.remove_absolute(new_dir.path_join(".protected"))
if res[1]:
var f := FileAccess.open(new_dir.path_join(MARKER), FileAccess.WRITE)
if f:

View File

@@ -13,17 +13,17 @@ const MARKER := "user://.protected" # є — старі незашифрова
static var last_bad := "" # ім'я останнього відкладеного пошкодженого файлу (main.gd показує попередження)
# спершу в .tmp, потім заміна: збій чи вимкнене світло посеред запису не зіпсує старе збереження
static func save_json(path: String, data, protect := true) -> void:
static func save_json(path: String, data, protect := true) -> bool:
var tmp := path + ".tmp"
var f := FileAccess.open_encrypted_with_pass(tmp, FileAccess.WRITE, KEY) if protect else FileAccess.open(tmp, FileAccess.WRITE)
if f == null:
return
return false
var ok := f.store_string(JSON.stringify(data))
f.close()
if ok:
DirAccess.rename_absolute(ProjectSettings.globalize_path(tmp), ProjectSettings.globalize_path(path))
else:
DirAccess.remove_absolute(ProjectSettings.globalize_path(tmp))
if ok and DirAccess.rename_absolute(ProjectSettings.globalize_path(tmp), ProjectSettings.globalize_path(path)) == OK:
return true
DirAccess.remove_absolute(ProjectSettings.globalize_path(tmp))
return false
static func is_encrypted(path: String) -> bool:
var f := FileAccess.open(path, FileAccess.READ)
@@ -35,9 +35,10 @@ static func read_json(path: String, protect := true) -> Variant:
if not FileAccess.file_exists(path):
return null
var f: FileAccess
if not protect or (not is_encrypted(path) and not FileAccess.file_exists(MARKER)):
var enc := is_encrypted(path)
if not protect or (not enc and not FileAccess.file_exists(MARKER)):
f = FileAccess.open(path, FileAccess.READ) # налаштування або старий файл до перетворення
elif is_encrypted(path):
elif enc:
f = FileAccess.open_encrypted_with_pass(path, FileAccess.READ, KEY)
var d = JSON.parse_string(f.get_as_text()) if f else null
f = null
@@ -53,11 +54,14 @@ static func read_json(path: String, protect := true) -> Variant:
static func protect_legacy(paths: Array) -> void:
if FileAccess.file_exists(MARKER):
return
var all_ok := true # запис не вдався (файл тримає антивірус, диск повний) — без позначки, наступний запуск спробує ще
for p in paths:
if FileAccess.file_exists(p) and not is_encrypted(p):
var d = read_json(p)
if d != null:
save_json(p, d)
all_ok = save_json(p, d) and all_ok
if not all_ok:
return
var m := FileAccess.open(MARKER, FileAccess.WRITE)
if m:
m.store_string("saves are encrypted")

View File

@@ -334,7 +334,9 @@ func _run() -> void:
DirAccess.make_dir_recursive_absolute(nd)
for kv in [[od.path_join("save.json"), "old_save"], [od.path_join("settings.json"), "old_set"], [od.path_join("sub/a.json"), "a"], [nd.path_join("settings.json"), "new_set"]]:
FileAccess.open(kv[0], FileAccess.WRITE).store_string(kv[1])
FileAccess.open(nd.path_join(".protected"), FileAccess.WRITE).store_string("x")
ok(M.run(od, nd) == 2, "перенос: скопійовано лише відсутні файли (з підпапками)")
ok(not FileAccess.file_exists(nd.path_join(".protected")), "перенос: скопійовані відкриті файли ще раз шифруються, а не стають підробкою")
ok(FileAccess.get_file_as_string(nd.path_join("save.json")) == "old_save" and FileAccess.get_file_as_string(nd.path_join("sub/a.json")) == "a", "перенос: збереження в новій папці")
ok(FileAccess.get_file_as_string(nd.path_join("settings.json")) == "new_set", "перенос: новіші дані в новій папці не затерто")
ok(FileAccess.file_exists(od.path_join("save.json")), "перенос: стара папка лишилась")