Skip to content

Commit b323968

Browse files
committed
Avoid invalid song state when loading/importing broken files
1 parent 2214736 commit b323968

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

globals/IOManager.gd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func load_ceol_song_safe() -> void:
144144

145145
func _load_ceol_song_confirmed(path: String) -> bool:
146146
var loaded_song: Song = SongLoader.load(path)
147-
if not loaded_song:
147+
if not loaded_song || not loaded_song.is_valid_song():
148148
Controller.update_status("FAILED TO LOAD SONG", Controller.StatusLevel.ERROR)
149149
return false
150150

@@ -298,7 +298,7 @@ func _import_song_confirmed(import_config: ImportMasterPopup.ImportConfig) -> vo
298298

299299
func _import_mid_song(import_config: ImportMasterPopup.ImportConfig) -> void:
300300
var imported_song := MidiImporter.import(import_config)
301-
if not imported_song:
301+
if not imported_song || not imported_song.is_valid_song():
302302
Controller.update_status("FAILED TO IMPORT SONG", Controller.StatusLevel.ERROR)
303303
return
304304

io/SongLoader.gd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ static func load(path: String) -> Song:
3030
if reader.get_version() == 3:
3131
return _load_v3(reader)
3232

33-
printerr("SongLoader: The song file at '%s' has unsupported version %d, an empty song is created instead." % [ path, reader.get_version() ])
34-
return Song.create_default_song()
33+
printerr("SongLoader: The song file at '%s' has unsupported version %d." % [ path, reader.get_version() ])
34+
return null
3535

3636

3737
# Original release; due to a bug it never saved the instrument volume.

objects/Song.gd

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,17 @@ static func create_default_song() -> Song:
102102
return song
103103

104104

105+
func is_valid_song() -> bool:
106+
# A valid song must have at least one instrument and one pattern.
107+
# We automatically create one of each whenever the last one is
108+
# deleted. Loaded and imported songs are expected to have at least
109+
# one as well.
110+
if instruments.is_empty() || patterns.is_empty():
111+
return false
112+
113+
return true
114+
115+
105116
func get_safe_filename(extension: String = FILE_EXTENSION) -> String:
106117
if filename.is_empty():
107118
return "%s.%s" % [ FILE_DEFAULT_NAME, extension ]

0 commit comments

Comments
 (0)