fix: reuse identical music files instead of numbered copies

- compare size and SHA-256 before copying a track into the routine folder
- keep the file the current routine already references when content matches
- convert M4A/AAC into a scratch file and dedupe it by content
- extend playback tests for repeated loads and same-name variants
This commit is contained in:
torsten 2026-11-10 20:27:25 +01:00
parent 2edd756920
commit e23520e630
5 changed files with 169 additions and 34 deletions

View file

@ -562,6 +562,52 @@ func _run() -> void:
"playback bar shows the music file"
)
# Loading the very same track again must not create a numbered duplicate.
_check(
controller.load_music_from_path(music_source),
"the same music file can be loaded again"
)
_check(
controller.get_music_file_name() == "test_music.wav",
"reloading identical music keeps the existing file name"
)
_check(
not FileAccess.file_exists("user://routines/test_music 2.wav"),
"reloading identical music creates no numbered duplicate"
)
# A different track with the same name still gets its own numbered copy.
var alt_dir := "user://music_alt"
DirAccess.make_dir_recursive_absolute(
ProjectSettings.globalize_path(alt_dir)
)
var alt_source := alt_dir.path_join("test_music.wav")
_write_test_wav(alt_source, 16000)
_check(
controller.load_music_from_path(alt_source),
"a different track with the same name loads"
)
_check(
controller.get_music_file_name() == "test_music 2.wav",
"a different track is stored under a numbered name"
)
_check(
controller.load_music_from_path(alt_source),
"the numbered track can be loaded again"
)
_check(
controller.get_music_file_name() == "test_music 2.wav",
"reloading the numbered track keeps its name"
)
_check(
controller.load_music_from_path(music_source),
"switching back to the first track works"
)
_check(
controller.get_music_file_name() == "test_music.wav",
"switching back reuses the existing first copy"
)
controller.seek_playback(0.5)
controller.play()
_check(controller.music_player.playing, "music starts with routine playback")
@ -598,9 +644,14 @@ func _run() -> void:
FileAccess.file_exists("user://routines/test_music.wav"),
"clearing keeps the file for other routines"
)
DirAccess.remove_absolute(
ProjectSettings.globalize_path("user://routines/test_music.wav")
)
for leftover: String in [
"user://routines/test_music.wav",
"user://routines/test_music 2.wav",
]:
if FileAccess.file_exists(leftover):
DirAccess.remove_absolute(ProjectSettings.globalize_path(leftover))
DirAccess.remove_absolute(ProjectSettings.globalize_path(alt_source))
DirAccess.remove_absolute(ProjectSettings.globalize_path(alt_dir))
DirAccess.remove_absolute(ProjectSettings.globalize_path(music_source))
scene.queue_free()
@ -613,27 +664,26 @@ func _run() -> void:
_finish()
func _write_test_wav(path: String) -> void:
const SAMPLE_RATE := 8000
const DATA_SIZE := SAMPLE_RATE * 2
func _write_test_wav(path: String, sample_rate: int = 8000) -> void:
var data_size := sample_rate * 2
var file := FileAccess.open(path, FileAccess.WRITE)
if file == null:
return
file.store_buffer("RIFF".to_ascii_buffer())
file.store_32(36 + DATA_SIZE)
file.store_32(36 + data_size)
file.store_buffer("WAVE".to_ascii_buffer())
file.store_buffer("fmt ".to_ascii_buffer())
file.store_32(16)
file.store_16(1)
file.store_16(1)
file.store_32(SAMPLE_RATE)
file.store_32(SAMPLE_RATE * 2)
file.store_32(sample_rate)
file.store_32(sample_rate * 2)
file.store_16(2)
file.store_16(16)
file.store_buffer("data".to_ascii_buffer())
file.store_32(DATA_SIZE)
file.store_32(data_size)
var silence := PackedByteArray()
silence.resize(DATA_SIZE)
silence.resize(data_size)
file.store_buffer(silence)
file.close()