From 3de8fcc8040db23cd4e82c3e52a72204f2e8e68e 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:00:18 +0100 Subject: [PATCH] bugfix for loading deleted mods + safe saves --- source/funkin/backend/assets/ModsFolder.hx | 23 +++++++++++++++++-- source/funkin/backend/chart/Chart.hx | 4 ++-- source/funkin/backend/system/Main.hx | 2 +- source/funkin/backend/utils/CoolUtil.hx | 16 +++++++++++++ source/funkin/editors/SaveSubstate.hx | 16 ++----------- .../editors/character/CharacterEditor.hx | 6 ++--- source/funkin/editors/charter/Charter.hx | 12 +++++----- .../editors/charter/CharterSelection.hx | 8 +++---- source/funkin/menus/MainMenuState.hx | 4 +--- source/funkin/menus/ModSwitchMenu.hx | 14 +---------- 10 files changed, 56 insertions(+), 49 deletions(-) diff --git a/source/funkin/backend/assets/ModsFolder.hx b/source/funkin/backend/assets/ModsFolder.hx index a88377ad..cd1704ed 100644 --- a/source/funkin/backend/assets/ModsFolder.hx +++ b/source/funkin/backend/assets/ModsFolder.hx @@ -51,10 +51,11 @@ class ModsFolder { */ private static var __firstTime:Bool = true; /** - * Initialises `mods` folder by adding callbacks and such. + * Initialises `mods` folder. */ public static function init() { - + if(!getModsList().contains(Options.lastLoadedMod)) + Options.lastLoadedMod = null; } /** @@ -89,6 +90,24 @@ class ModsFolder { #end } + public static function getModsList():Array { + var mods:Array = []; + #if MOD_SUPPORT + for(modFolder in FileSystem.readDirectory(modsPath)) { + if (FileSystem.isDirectory('${modsPath}${modFolder}')) { + mods.push(modFolder); + } else { + var ext = Path.extension(modFolder).toLowerCase(); + switch(ext) { + case 'zip': + // is a zip mod!! + mods.push(Path.withoutExtension(modFolder)); + } + } + } + #end + return mods; + } public static function getLoadedMods():Array { var libs = []; for (i in Paths.assetsTree.libraries) { diff --git a/source/funkin/backend/chart/Chart.hx b/source/funkin/backend/chart/Chart.hx index d18640bb..e7a2c2a4 100644 --- a/source/funkin/backend/chart/Chart.hx +++ b/source/funkin/backend/chart/Chart.hx @@ -206,11 +206,11 @@ class Chart { var chartPath = '${songFolderPath}/charts/${difficulty.trim()}.json'; var metaPath = '${songFolderPath}/meta.json'; - File.saveContent(chartPath, Json.stringify(filteredChart, null, saveSettings.prettyPrint == true ? "\t" : null)); + CoolUtil.safeSaveFile(chartPath, Json.stringify(filteredChart, null, saveSettings.prettyPrint == true ? "\t" : null)); // idk how null reacts to it so better be sure if (saveSettings.overrideExistingMeta == true || !FileSystem.exists(metaPath)) - File.saveContent(metaPath, Json.stringify(meta, null, saveSettings.prettyPrint == true ? "\t" : null)); + CoolUtil.safeSaveFile(metaPath, Json.stringify(meta, null, saveSettings.prettyPrint == true ? "\t" : null)); #end return filteredChart; } diff --git a/source/funkin/backend/system/Main.hx b/source/funkin/backend/system/Main.hx index 87bd26d8..d04d4f3d 100644 --- a/source/funkin/backend/system/Main.hx +++ b/source/funkin/backend/system/Main.hx @@ -120,7 +120,6 @@ class Main extends Sprite ShaderResizeFix.init(); Logs.init(); Paths.init(); - ModsFolder.init(); DiscordUtil.init(); #if GLOBAL_SCRIPT funkin.backend.scripting.GlobalScript.init(); @@ -159,6 +158,7 @@ class Main extends Sprite FlxG.mouse.useSystemCursor = true; + ModsFolder.init(); #if MOD_SUPPORT ModsFolder.switchMod(modToLoad.getDefault(Options.lastLoadedMod)); #end diff --git a/source/funkin/backend/utils/CoolUtil.hx b/source/funkin/backend/utils/CoolUtil.hx index 261e85f7..8bf1a167 100644 --- a/source/funkin/backend/utils/CoolUtil.hx +++ b/source/funkin/backend/utils/CoolUtil.hx @@ -77,6 +77,22 @@ class CoolUtil #end } + /** + * Safe saves a file 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) + catch(e) { + var errMsg:String = 'Error while trying to save the file: ${Std.string(e).replace('\n', ' ')}'; + Logs.traceColored([Logs.logText(errMsg, RED)], ERROR); + if(showErrorBox) funkin.backend.utils.NativeAPI.showMessageBox("Codename Engine Warning", errMsg, MSG_WARNING); + } + #end + } + /** * Shortcut to parse a JSON string * @param str Path to the JSON string diff --git a/source/funkin/editors/SaveSubstate.hx b/source/funkin/editors/SaveSubstate.hx index 6250050a..7202cf37 100644 --- a/source/funkin/editors/SaveSubstate.hx +++ b/source/funkin/editors/SaveSubstate.hx @@ -1,8 +1,5 @@ package funkin.editors; -#if desktop -import sys.io.File; -#end import haxe.io.Path; import lime.ui.FileDialog; @@ -30,13 +27,9 @@ class SaveSubstate extends MusicBeatSubstate { super.create(); var fileDialog = new FileDialog(); - fileDialog.onCancel.add(function() { - close(); - }); + fileDialog.onCancel.add(function() close()); fileDialog.onSelect.add(function(str) { - #if desktop - File.saveContent(str, data); - #end + CoolUtil.safeSaveFile(str, data); close(); }); fileDialog.browse(SAVE, options.saveExt.getDefault(Path.extension(options.defaultSaveFile)), options.defaultSaveFile); @@ -46,11 +39,6 @@ class SaveSubstate extends MusicBeatSubstate { super.update(elapsed); parent.persistentUpdate = false; } - - private function onError(_) { - // TODO: error handling - close(); - } } typedef SaveSubstateData = { diff --git a/source/funkin/editors/character/CharacterEditor.hx b/source/funkin/editors/character/CharacterEditor.hx index 3fb76ed0..8c5cf9c7 100644 --- a/source/funkin/editors/character/CharacterEditor.hx +++ b/source/funkin/editors/character/CharacterEditor.hx @@ -328,14 +328,14 @@ class CharacterEditor extends UIState { function _file_save(_) { #if sys - sys.io.File.saveContent( + CoolUtil.safeSaveFile( '${Paths.getAssetsRoot()}/data/characters/${character.curCharacter}.xml', buildCharacter() ); undos.save(); - return; - #end + #else _file_saveas(_); + #end } function _file_saveas(_) { diff --git a/source/funkin/editors/charter/Charter.hx b/source/funkin/editors/charter/Charter.hx index 376488a6..8b08b4b8 100644 --- a/source/funkin/editors/charter/Charter.hx +++ b/source/funkin/editors/charter/Charter.hx @@ -1165,13 +1165,13 @@ class Charter extends UIState { function _file_meta_save(_) { #if sys - sys.io.File.saveContent( + CoolUtil.safeSaveFile( '${Paths.getAssetsRoot()}/songs/${__song.toLowerCase()}/meta.json', Json.stringify(PlayState.SONG.meta == null ? {} : PlayState.SONG.meta, null, "\t") ); - return; - #end + #else _file_meta_saveas(_); + #end } function _file_meta_saveas(_) { @@ -1182,13 +1182,13 @@ class Charter extends UIState { function _file_events_save(_) { #if sys - sys.io.File.saveContent( + CoolUtil.safeSaveFile( '${Paths.getAssetsRoot()}/songs/${__song.toLowerCase()}/events.json', Json.stringify({events: PlayState.SONG.events == null ? [] : PlayState.SONG.events}) ); - return; - #end + #else _file_events_saveas(_); + #end } function _file_events_saveas(_) { diff --git a/source/funkin/editors/charter/CharterSelection.hx b/source/funkin/editors/charter/CharterSelection.hx index f9a1ea5e..b0957997 100644 --- a/source/funkin/editors/charter/CharterSelection.hx +++ b/source/funkin/editors/charter/CharterSelection.hx @@ -107,7 +107,7 @@ class CharterSelection extends EditorTreeMenu { sys.FileSystem.createDirectory('$songFolder/charts'); // Save Files - sys.io.File.saveContent('$songFolder/meta.json', Json.stringify(creation.meta, "\t")); + CoolUtil.safeSaveFile('$songFolder/meta.json', Json.stringify(creation.meta, "\t")); if (creation.instBytes != null) sys.io.File.saveBytes('$songFolder/song/Inst.${Paths.SOUND_EXT}', creation.instBytes); if (creation.voicesBytes != null) sys.io.File.saveBytes('$songFolder/song/Voices.${Paths.SOUND_EXT}', creation.voicesBytes); #end @@ -145,9 +145,7 @@ class CharterSelection extends EditorTreeMenu { var songFolder:String = '${Paths.getAssetsRoot()}/songs/${curSong.name}'; // Save Files - #if sys - sys.io.File.saveContent('$songFolder/charts/${name}.json', Json.stringify(data, "\t")); - #end + CoolUtil.safeSaveFile('$songFolder/charts/${name}.json', Json.stringify(data, "\t")); // Add to List curSong.difficulties.push(name); @@ -160,7 +158,7 @@ class CharterSelection extends EditorTreeMenu { var meta = Json.parse(sys.io.File.getContent('$songFolder/meta.json')); if (meta.difficulties != null && !meta.difficulties.contains(name)) { meta.difficulties.push(name); - sys.io.File.saveContent('$songFolder/meta.json', Json.stringify(meta)); + CoolUtil.safeSaveFile('$songFolder/meta.json', Json.stringify(meta)); } } } \ No newline at end of file diff --git a/source/funkin/menus/MainMenuState.hx b/source/funkin/menus/MainMenuState.hx index 7744564b..03e3bd8b 100644 --- a/source/funkin/menus/MainMenuState.hx +++ b/source/funkin/menus/MainMenuState.hx @@ -101,9 +101,7 @@ class MainMenuState extends MusicBeatState if (FlxG.keys.justPressed.SEVEN) FlxG.switchState(new funkin.desktop.DesktopMain()); if (FlxG.keys.justPressed.EIGHT) { - #if sys - sys.io.File.saveContent("chart.json", Json.stringify(funkin.backend.chart.Chart.parse("dadbattle", "hard"))); - #end + CoolUtil.safeSaveFile("chart.json", Json.stringify(funkin.backend.chart.Chart.parse("dadbattle", "hard"))); } */ } diff --git a/source/funkin/menus/ModSwitchMenu.hx b/source/funkin/menus/ModSwitchMenu.hx index 51509ebc..1b23b9d2 100644 --- a/source/funkin/menus/ModSwitchMenu.hx +++ b/source/funkin/menus/ModSwitchMenu.hx @@ -22,19 +22,7 @@ class ModSwitchMenu extends MusicBeatSubstate { bg.alpha = 0; FlxTween.tween(bg, {alpha: 0.5}, 0.25, {ease: FlxEase.cubeOut}); - for(modFolder in FileSystem.readDirectory(ModsFolder.modsPath)) { - if (FileSystem.isDirectory('${ModsFolder.modsPath}${modFolder}')) { - mods.push(modFolder); - } else { - var ext = Path.extension(modFolder).toLowerCase(); - switch(ext) { - case 'zip': - // is a zip mod!! - mods.push(Path.withoutExtension(modFolder)); - } - } - } - + mods = ModsFolder.getModsList(); mods.push(null); alphabets = new FlxTypedGroup();