Skip to content

Commit 659328b

Browse files
committed
Add some error handling for uninitialised songs
1 parent 23c268f commit 659328b

File tree

5 files changed

+22
-48
lines changed

5 files changed

+22
-48
lines changed

source/mikolka/compatibility/freeplay/FreeplayHelpers.hx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ class FreeplayHelpers
109109
try
110110
{
111111
PlayState.SONG = Song.loadFromJson(poop, songLowercase);
112+
if(PlayState.SONG == null) throw "Song parsing failed!";
112113
PlayState.isStoryMode = false;
113114
PlayState.storyDifficulty = diffId;
114115

source/mikolka/compatibility/ui/StoryModeHooks.hx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class StoryModeHooks {
8585

8686

8787
Song.loadFromJson(PlayState.storyPlaylist[0].toLowerCase() + diffic, PlayState.storyPlaylist[0].toLowerCase());
88+
if(PlayState.SONG == null) throw "Song parsing failed!";
8889
PlayState.campaignScore = 0;
8990
PlayState.campaignMisses = 0;
9091
return true;

source/states/PlayState.hx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,8 @@ class PlayState extends MusicBeatState
335335
persistentUpdate = true;
336336
persistentDraw = true;
337337

338+
if(SONG == null) throw "SONG value uninitialised!";
339+
338340
Conductor.mapBPMChanges(SONG);
339341
Conductor.bpm = SONG.bpm;
340342

source/states/editors/DialogueCharacterEditorState.hx

Lines changed: 17 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package states.editors;
22

3+
import haxe.io.Path;
4+
import states.editors.content.FileDialogHandler;
35
import mikolka.stages.cutscenes.dialogueBox.DialogueCharacter.DialogueCharacterFile;
46
import mikolka.stages.cutscenes.dialogueBox.DialogueCharacter.DialogueAnimArray;
57
import openfl.net.FileReference;
@@ -674,34 +676,23 @@ class DialogueCharacterEditorState extends MusicBeatState implements PsychUIEven
674676
else ClientPrefs.toggleVolumeKeys(false);
675677
}
676678

677-
var _file:FileReference = null;
679+
var _file:FileDialogHandler = null;
678680
function loadCharacter() {
679681
var jsonFilter:FileFilter = new FileFilter('JSON', 'json');
680-
_file = new FileReference();
681-
_file.addEventListener(#if desktop Event.SELECT #else Event.COMPLETE #end, onLoadComplete);
682-
_file.addEventListener(Event.CANCEL, onLoadCancel);
683-
_file.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
684-
_file.browse([#if !mac jsonFilter #end]);
682+
_file = new FileDialogHandler();
683+
_file.open(null,"Open dialog portrait character",[jsonFilter],onLoadComplete,onLoadCancel,onLoadError);
685684
}
686685

687-
function onLoadComplete(_):Void
686+
function onLoadComplete():Void
688687
{
689-
_file.removeEventListener(#if desktop Event.SELECT #else Event.COMPLETE #end, onLoadComplete);
690-
_file.removeEventListener(Event.CANCEL, onLoadCancel);
691-
_file.removeEventListener(IOErrorEvent.IO_ERROR, onLoadError);
692-
693-
#if sys
694-
var fullPath:String = null;
695-
@:privateAccess
696-
if(_file.__path != null) fullPath = _file.__path;
697-
698-
if(fullPath != null) {
699-
var rawJson:String = NativeFileSystem.getContent(fullPath);
688+
if(_file.data != null) {
689+
var rawJson:String = _file.data;
700690
if(rawJson != null) {
701691
var loadedChar:DialogueCharacterFile = cast Json.parse(rawJson);
702692
if(loadedChar.dialogue_pos != null) //Make sure it's really a dialogue character
703693
{
704-
var cutName:String = _file.name.substr(0, _file.name.length - 5);
694+
var cutName:String = Path.withoutDirectory(_file.path);
695+
cutName = cutName.substr(0, cutName.length - 5);
705696
trace("Successfully loaded file: " + cutName);
706697
character.jsonFile = loadedChar;
707698
reloadCharacter();
@@ -719,31 +710,22 @@ class DialogueCharacterEditorState extends MusicBeatState implements PsychUIEven
719710
}
720711
}
721712
_file = null;
722-
#else
723-
trace("File couldn't be loaded! You aren't on Desktop, are you?");
724-
#end
725713
}
726714

727715
/**
728716
* Called when the save file dialog is cancelled.
729717
*/
730-
function onLoadCancel(_):Void
718+
function onLoadCancel():Void
731719
{
732-
_file.removeEventListener(#if desktop Event.SELECT #else Event.COMPLETE #end, onLoadComplete);
733-
_file.removeEventListener(Event.CANCEL, onLoadCancel);
734-
_file.removeEventListener(IOErrorEvent.IO_ERROR, onLoadError);
735720
_file = null;
736721
trace("Cancelled file loading.");
737722
}
738723

739724
/**
740725
* Called if there is an error while saving the gameplay recording.
741726
*/
742-
function onLoadError(_):Void
727+
function onLoadError():Void
743728
{
744-
_file.removeEventListener(#if desktop Event.SELECT #else Event.COMPLETE #end, onLoadComplete);
745-
_file.removeEventListener(Event.CANCEL, onLoadCancel);
746-
_file.removeEventListener(IOErrorEvent.IO_ERROR, onLoadError);
747729
_file = null;
748730
trace("Problem loading file");
749731
}
@@ -759,43 +741,31 @@ class DialogueCharacterEditorState extends MusicBeatState implements PsychUIEven
759741
unsavedProgress = false;
760742
StorageUtil.saveContent('$characterName.json', data);
761743
#else
762-
_file = new FileReference();
763-
_file.addEventListener(#if desktop Event.SELECT #else Event.COMPLETE #end, onSaveComplete);
764-
_file.addEventListener(Event.CANCEL, onSaveCancel);
765-
_file.addEventListener(IOErrorEvent.IO_ERROR, onSaveError);
766-
_file.save(data, characterName + ".json");
744+
_file = new FileDialogHandler();
745+
_file.save(characterName + ".json",data,onSaveComplete,onSaveCancel,onSaveError);
767746
#end
768747
}
769748
}
770749

771-
function onSaveComplete(_):Void
750+
function onSaveComplete():Void
772751
{
773-
_file.removeEventListener(#if desktop Event.SELECT #else Event.COMPLETE #end, onSaveComplete);
774-
_file.removeEventListener(Event.CANCEL, onSaveCancel);
775-
_file.removeEventListener(IOErrorEvent.IO_ERROR, onSaveError);
776752
_file = null;
777753
FlxG.log.notice("Successfully saved file.");
778754
}
779755

780756
/**
781757
* Called when the save file dialog is cancelled.
782758
*/
783-
function onSaveCancel(_):Void
759+
function onSaveCancel():Void
784760
{
785-
_file.removeEventListener(#if desktop Event.SELECT #else Event.COMPLETE #end, onSaveComplete);
786-
_file.removeEventListener(Event.CANCEL, onSaveCancel);
787-
_file.removeEventListener(IOErrorEvent.IO_ERROR, onSaveError);
788761
_file = null;
789762
}
790763

791764
/**
792765
* Called if there is an error while saving the gameplay recording.
793766
*/
794-
function onSaveError(_):Void
767+
function onSaveError():Void
795768
{
796-
_file.removeEventListener(#if desktop Event.SELECT #else Event.COMPLETE #end, onSaveComplete);
797-
_file.removeEventListener(Event.CANCEL, onSaveCancel);
798-
_file.removeEventListener(IOErrorEvent.IO_ERROR, onSaveError);
799769
_file = null;
800770
FlxG.log.error("Problem saving file");
801771
}

source/states/editors/NoteSplashEditorState.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ class NoteSplashEditorState extends MusicBeatState
906906
#if MODS_ALLOWED
907907
if (txtLoaded.__path != null)
908908
{
909-
try txt = NativeFileSystem.getContent(txtLoaded.__path) catch (e) txt = null;
909+
try txt = File.getContent(txtLoaded.__path) catch (e) txt = null;
910910
file = txtLoaded.__path;
911911
file = file.substring(0, file.length - 4) + ".json";
912912
}

0 commit comments

Comments
 (0)