When writing a file, make sure that its parent directory exists

Fixes #13.
This commit is contained in:
blinry 2021-02-05 16:20:52 +01:00
parent a938ba206f
commit d780520d51

View file

@ -55,6 +55,15 @@ func read_file(path, fallback_string=null):
func write_file(path, content): func write_file(path, content):
if debug_file_io: if debug_file_io:
print("writing " + path) print("writing " + path)
# Create the base directory of the file, if it doesn't exist.
var parts = Array(path.split("/"))
parts.pop_back()
var dirname = PoolStringArray(parts).join("/")
var dir = Directory.new()
if not dir.dir_exists(path):
dir.make_dir_recursive(dirname)
var file = File.new() var file = File.new()
file.open(path, File.WRITE) file.open(path, File.WRITE)
file.store_string(content) file.store_string(content)