From 69c9030b9ccfdcc7b284d955e8c7cd735502923f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=8D=9A=7ENex?= <87421482+NexIsDumb@users.noreply.github.com> Date: Sun, 17 Mar 2024 18:24:44 +0100 Subject: [PATCH] once again safer savings Co-Authored-By: Ne_Eo --- source/funkin/backend/utils/CoolUtil.hx | 27 +++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/source/funkin/backend/utils/CoolUtil.hx b/source/funkin/backend/utils/CoolUtil.hx index 8bf1a167..66858865 100644 --- a/source/funkin/backend/utils/CoolUtil.hx +++ b/source/funkin/backend/utils/CoolUtil.hx @@ -78,13 +78,13 @@ class CoolUtil } /** - * Safe saves a file and shows a warning box instead of making the program crash + * Safe saves a file (even adding missing folder) and shows a warning box instead of making the program crash * @param path Path to save the file at. * @param data Content of the file to save. */ @:noUsing public static function safeSaveFile(path:String, content:String, showErrorBox:Bool = true) { #if sys - try sys.io.File.saveContent(path, content) + try sys.io.File.saveContent(addMissingFolders(path), content) catch(e) { var errMsg:String = 'Error while trying to save the file: ${Std.string(e).replace('\n', ' ')}'; Logs.traceColored([Logs.logText(errMsg, RED)], ERROR); @@ -93,6 +93,29 @@ class CoolUtil #end } + /** + * Creates eventual missing folders to the specified `path` + * + * WARNING: eventual files in `path` will be considered as folders! Just to make possible folders be named as `songs.json` for example + * + * @param path Path to check. + * @return The initial Path. + */ + @:noUsing public static function addMissingFolders(path:String):String { + #if sys + var folders:Array = path.split("/"); + var currentPath:String = ""; + + for (folder in folders) { + currentPath += folder + "/"; + if (!sys.FileSystem.exists(currentPath)) { + sys.FileSystem.createDirectory(currentPath); + } + } + #end + return path; + } + /** * Shortcut to parse a JSON string * @param str Path to the JSON string