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
+1 -1
View File
@@ -33,7 +33,7 @@ Audio is optional presentation. Its absence, unknown requests, missing folders,
└── death/
```
Admitted file extensions are `.mp3`, `.ogg`, `.oga`, `.wav`, `.flac`, `.m4a`, and `.opus`. Godot-native playback is attempted for compatible `.wav`, `.ogg`/`.oga`/`.opus`, and `.mp3` files; other admitted extensions are discovered but silently skipped if no native loader is available. The game selects `exploration` at startup, changes to `combat` for a combat result, and dispatches SFX for player attacks, enemy attacks, and death.
Admitted file extensions are `.mp3`, `.ogg`, `.oga`, `.wav`, `.flac`, `.m4a`, and `.opus`. Before invoking an external Godot loader, `AudioLibrary` fails closed unless a `.wav` has a `RIFF`/`WAVE` signature, an `.ogg`/`.oga`/`.opus` has `OggS`, or an `.mp3` has `ID3` or a valid MPEG sync word. `.flac` and `.m4a` remain silent because this library has no native external loader for them. Thus extension-only or malformed external files are never handed to `load_from_file`. The game selects `exploration` at startup, changes to `combat` for a combat result, and dispatches SFX for player attacks, enemy attacks, and death.
Example:
+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:
+12 -1
View File
@@ -5,4 +5,15 @@ if [[ ! -x "$GODOT_BIN" ]]; then
GODOT_BIN="$(command -v godot4 || command -v godot || true)"
fi
: "${GODOT_BIN:?Set GODOT_BIN or install godot4/godot}"
exec "$GODOT_BIN" --headless --path . -s res://addons/gut/gut_cmdln.gd -gdir=res://tests/e2e -gexit
log_path="$(mktemp)"
trap 'rm -f "$log_path"' EXIT
set +e
"$GODOT_BIN" --headless --path . -s res://addons/gut/gut_cmdln.gd -gdir=res://tests/e2e -gexit 2>&1 | tee "$log_path"
godot_status=${PIPESTATUS[0]}
set -e
if grep -Eq 'SCRIPT ERROR|Parse Error' "$log_path"; then
echo "E2E tests failed: Godot reported a script or parse error" >&2
exit 1
fi
exit "$godot_status"
+12 -1
View File
@@ -5,4 +5,15 @@ if [[ ! -x "$GODOT_BIN" ]]; then
GODOT_BIN="$(command -v godot4 || command -v godot || true)"
fi
: "${GODOT_BIN:?Set GODOT_BIN or install godot4/godot}"
exec "$GODOT_BIN" --headless --path . -s res://addons/gut/gut_cmdln.gd -gdir=res://tests/unit -gexit
log_path="$(mktemp)"
trap 'rm -f "$log_path"' EXIT
set +e
"$GODOT_BIN" --headless --path . -s res://addons/gut/gut_cmdln.gd -gdir=res://tests/unit -gexit 2>&1 | tee "$log_path"
godot_status=${PIPESTATUS[0]}
set -e
if grep -Eq 'SCRIPT ERROR|Parse Error' "$log_path"; then
echo "unit tests failed: Godot reported a script or parse error" >&2
exit 1
fi
exit "$godot_status"
+11 -1
View File
@@ -1,19 +1,29 @@
extends GutTest
const GameScene = preload("res://scenes/Game.tscn")
const AudioLibrary = preload("res://scripts/infrastructure/audio_library.gd")
var empty_audio_home := ""
var previous_audio_home := ""
var game: Game
func before_each() -> void:
previous_audio_home = OS.get_environment("DCW_AUDIO_HOME")
empty_audio_home = ProjectSettings.globalize_path("user://s4-empty-audio-home")
DirAccess.make_dir_recursive_absolute(empty_audio_home)
OS.set_environment("DCW_AUDIO_HOME", empty_audio_home)
game = GameScene.instantiate()
# Replace the scene library before ready to isolate both production search roots.
var scene_audio := game.get_node("AudioLibrary")
var isolated_audio := AudioLibrary.new(empty_audio_home)
isolated_audio.name = "AudioLibrary"
game.remove_child(scene_audio)
scene_audio.free()
game.add_child(isolated_audio)
add_child_autofree(game)
await get_tree().process_frame
func after_each() -> void:
OS.set_environment("DCW_AUDIO_HOME", "")
OS.set_environment("DCW_AUDIO_HOME", previous_audio_home)
func test_real_game_resolves_combat_with_empty_audio_home() -> void:
var audio = game.get_node("AudioLibrary")
+18
View File
@@ -25,6 +25,24 @@ func test_unknown_requests_are_silent() -> void:
assert_eq(library.get_last_ambience_path(), "")
assert_eq(library.get_last_sfx_path(), "")
func test_invalid_wav_is_not_selected_or_played_and_does_not_block_requests() -> void:
var root := ProjectSettings.globalize_path("user://s4-invalid-audio-library")
var invalid_path := root.path_join("audio/exploration/not-a-wave.wav")
DirAccess.make_dir_recursive_absolute(invalid_path.get_base_dir())
var invalid_file := FileAccess.open(invalid_path, FileAccess.WRITE)
invalid_file.store_string("this is not a WAV file")
invalid_file.close()
var library := AudioLibrary.new(root)
add_child_autofree(library)
library.set_ambience_context("exploration")
library.play_sfx("attack_given")
assert_eq(library.get_last_ambience_path(), "")
assert_eq(library.get_last_sfx_path(), "")
assert_false(library._ambience_player.playing)
assert_false(library._sfx_player.playing)
func test_admitted_wav_file_is_discovered() -> void:
var root := ProjectSettings.globalize_path("user://s4-audio-library")
var sample_path := root.path_join("audio/exploration/silence.wav")