bugfix for loading deleted mods + safe saves
This commit is contained in:
@@ -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<String> {
|
||||
var mods:Array<String> = [];
|
||||
#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<String> {
|
||||
var libs = [];
|
||||
for (i in Paths.assetsTree.libraries) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
@@ -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(_) {
|
||||
|
||||
@@ -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(_) {
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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")));
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
@@ -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<Alphabet>();
|
||||
|
||||
Reference in New Issue
Block a user