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