fix: harden S4 audio verification

This commit is contained in:
goose
2026-10-01 09:02:23 -04:00
parent 0615f5b89d
commit 4e356ee7b8
6 changed files with 102 additions and 6 deletions
+48 -2
View File
@@ -69,13 +69,59 @@ func _find_first_audio_file(kind: String, name: String) -> String:
directory.list_dir_begin()
var entry := directory.get_next()
while not entry.is_empty():
if not directory.current_is_dir() and entry.get_extension().to_lower() in ADMITTED_EXTENSIONS:
var path := directory_path.path_join(entry)
if not directory.current_is_dir() and _is_loadable_external_audio(path):
directory.list_dir_end()
return directory_path.path_join(entry)
return path
entry = directory.get_next()
directory.list_dir_end()
return ""
func _is_loadable_external_audio(path: String) -> bool:
var extension := path.get_extension().to_lower()
if not extension in ADMITTED_EXTENSIONS:
return false
# Do not call Godot's external loaders unless the bytes identify a codec they load.
# Other contract-admitted extensions (flac/m4a) intentionally have no native loader.
match extension:
"wav":
return _has_wav_signature(path)
"ogg", "oga", "opus":
return _has_prefix(path, PackedByteArray([79, 103, 103, 83])) # OggS
"mp3":
return _has_mp3_signature(path)
_:
return false
func _has_wav_signature(path: String) -> bool:
var file := FileAccess.open(path, FileAccess.READ)
if file == null:
return false
var header := file.get_buffer(12)
file.close()
return header.size() == 12 and header.slice(0, 4) == PackedByteArray([82, 73, 70, 70]) and header.slice(8, 12) == PackedByteArray([87, 65, 86, 69]) # RIFF....WAVE
func _has_prefix(path: String, prefix: PackedByteArray) -> bool:
var file := FileAccess.open(path, FileAccess.READ)
if file == null:
return false
var header := file.get_buffer(prefix.size())
file.close()
return header == prefix
func _has_mp3_signature(path: String) -> bool:
var file := FileAccess.open(path, FileAccess.READ)
if file == null:
return false
var header := file.get_buffer(3)
file.close()
if header.size() < 2:
return false
if header.size() == 3 and header == PackedByteArray([73, 68, 51]): # ID3
return true
# MPEG frame sync: 11 set bits, MPEG version not reserved, layer not reserved.
return header[0] == 255 and (header[1] & 0xE0) == 0xE0 and (header[1] & 0x18) != 0x08 and (header[1] & 0x06) != 0
func _play_ambience(path: String) -> void:
var stream := _load_audio_stream(path)
if stream == null or _ambience_player == null: