Skip to content

Commit af8ce39

Browse files
committed
MOAR safety checks!
1 parent d839049 commit af8ce39

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

addons/dialogic/Resources/TimelineResourceSaver.gd

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func _save(resource: Resource, path: String = '', flags: int = 0) -> int:
2626
return ERR_INVALID_DATA
2727
# Do not do this if the timeline's not in a ready state, so it doesn't accidentally save it blank
2828
elif !resource.events_processed:
29-
print('[Dialogic] Beginning saving timeline. Safety checks will be performed before writing...')
29+
print('[Dialogic] Beginning saving timeline. Safety checks will be performed before writing, and temporary file will be created and removed if saving is successful...')
3030

3131
#prepare everything before writing, we will only open the file if it's successfuly prepared, as that will clear the file contents
3232
#var result = events_to_text(resource.events)
@@ -47,12 +47,37 @@ func _save(resource: Resource, path: String = '', flags: int = 0) -> int:
4747
indent += 1
4848
if indent < 0:
4949
indent = 0
50-
result += "\t".repeat(indent)+"\n"
50+
#result += "\t".repeat(indent)+"\n"
51+
result += "\n"
5152

5253
if (len(result) > 0):
53-
var file := FileAccess.open(path, FileAccess.WRITE)
54+
var file := FileAccess.open(path.replace(".dtl", ".tmp"), FileAccess.WRITE)
5455
file.store_string(result)
55-
print('[Dialogic] Completed saving timeline "' , path, '"')
56+
file = null
57+
58+
var dir = DirAccess.open("res://")
59+
if dir.file_exists(path.replace(".dtl", ".tmp")):
60+
file = FileAccess.open(path.replace(".dtl", ".tmp"), FileAccess.READ)
61+
var check_length = file.get_length()
62+
if check_length > 0:
63+
var check_result = file.get_as_text()
64+
if result == check_result:
65+
dir.remove(path)
66+
dir.rename(path.replace(".dtl", ".tmp"), path)
67+
print('[Dialogic] Completed saving timeline "' , path, '"')
68+
else:
69+
printerr("[Dialogic] " + path + ": Temporary timeline file contents do not match what was written! Temporary file was saved as .tmp extension, please check to see if it matches your timeline, and rename to .dtl manually.")
70+
return ERR_INVALID_DATA
71+
else:
72+
printerr("[Dialogic] " + path + ": Temporary timeline file is empty! Timeline was not saved!")
73+
dir.remove(path.replace(".dtl", ".tmp"))
74+
return ERR_INVALID_DATA
75+
else:
76+
printerr("[Dialogic] " + path + ": Temporary timeline file failed to create! Timeline was not saved!")
77+
return ERR_INVALID_DATA
78+
79+
80+
5681
else:
5782
printerr("[Dialogic] " + path + ": Timeline failed to convert to text for saving! Timeline was not saved!")
5883
return ERR_INVALID_DATA

0 commit comments

Comments
 (0)