fix: keep external audio discovery diagnostic only
This commit is contained in:
@@ -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`. 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.
|
||||
Admitted file extensions are `.mp3`, `.ogg`, `.oga`, `.wav`, `.flac`, `.m4a`, and `.opus`. `AudioLibrary` discovers the first admitted regular file in each requested directory and exposes that path only as diagnostics (`get_last_ambience_path` / `get_last_sfx_path`). **This prototype never loads or plays external audio:** it does not invoke `AudioStream*.load_from_file`, `ResourceLoader`, or another Godot loader on files below these roots, regardless of extension or apparent validity. A nominal or truncated `.wav` is therefore equally non-playable (`has_playable_ambience()` / `has_playable_sfx()` return `false`) and cannot affect gameplay. Directory discovery is deliberately best-effort and error-free; it is not a promise that the discovered file can be read or played. The game still selects `exploration` at startup, changes to `combat` for a combat result, and dispatches audio requests for player attacks, enemy attacks, and death without blocking those actions.
|
||||
|
||||
Example:
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
# S4 round 2 — external audio ruling
|
||||
|
||||
## Decision
|
||||
|
||||
The S4 prototype may discover admitted audio filenames under the configured ASCII external roots, but it must never ask Godot to read, decode, import, or play those files. This is a safety boundary, not a codec-validation problem: a superficial signature cannot establish that a file is complete or safe for a downstream loader.
|
||||
|
||||
## Implementation
|
||||
|
||||
- `AudioLibrary` retains best-effort, error-free directory discovery for the documented roots and admitted extensions.
|
||||
- The last discovered ambience/SFX paths remain diagnostic-only public getters.
|
||||
- All byte-signature checks and `FileAccess` header reads were removed.
|
||||
- All `AudioStream*.load_from_file` calls, stream players, and external-load helper paths were removed.
|
||||
- `has_playable_ambience()` and `has_playable_sfx()` are public diagnostics and currently always return `false`.
|
||||
- `set_ambience_context` and `play_sfx` still accept their supported requests and cannot block or change game-state resolution.
|
||||
|
||||
## Verification coverage
|
||||
|
||||
Unit and E2E coverage create both a structurally nominal WAV and a truncated/invalid WAV in the external layout. They assert discovery remains diagnostic-only, neither file is playable through the public API, and combat still resolves normally. The README now states that discovery does not promise readability or playback.
|
||||
@@ -10,39 +10,21 @@ var current_ambience_context := ""
|
||||
var _audio_home_override := ""
|
||||
var _last_ambience_path := ""
|
||||
var _last_sfx_path := ""
|
||||
var _ambience_player: AudioStreamPlayer
|
||||
var _sfx_player: AudioStreamPlayer
|
||||
|
||||
func _init(audio_home_override: String = "") -> void:
|
||||
_audio_home_override = audio_home_override
|
||||
|
||||
func _ready() -> void:
|
||||
_ambience_player = AudioStreamPlayer.new()
|
||||
_sfx_player = AudioStreamPlayer.new()
|
||||
add_child(_ambience_player)
|
||||
add_child(_sfx_player)
|
||||
|
||||
func set_ambience_context(context: String) -> void:
|
||||
if not context in AMBIENCE_CONTEXTS:
|
||||
return
|
||||
current_ambience_context = context
|
||||
_last_ambience_path = ""
|
||||
var path := _find_first_audio_file("audio", current_ambience_context)
|
||||
if path.is_empty():
|
||||
_stop_ambience()
|
||||
return
|
||||
_last_ambience_path = path
|
||||
_play_ambience(path)
|
||||
_last_ambience_path = _find_first_audio_file("audio", current_ambience_context)
|
||||
|
||||
func play_sfx(event: String) -> void:
|
||||
_last_sfx_path = ""
|
||||
if not event in SFX_EVENTS:
|
||||
return
|
||||
var path := _find_first_audio_file("sfx", event)
|
||||
if path.is_empty():
|
||||
return
|
||||
_last_sfx_path = path
|
||||
_play_sfx(path)
|
||||
_last_sfx_path = _find_first_audio_file("sfx", event)
|
||||
|
||||
func get_last_ambience_path() -> String:
|
||||
return _last_ambience_path
|
||||
@@ -50,6 +32,12 @@ func get_last_ambience_path() -> String:
|
||||
func get_last_sfx_path() -> String:
|
||||
return _last_sfx_path
|
||||
|
||||
func has_playable_ambience() -> bool:
|
||||
return false
|
||||
|
||||
func has_playable_sfx() -> bool:
|
||||
return false
|
||||
|
||||
func _audio_roots() -> Array[String]:
|
||||
if not _audio_home_override.is_empty():
|
||||
return [_audio_home_override]
|
||||
@@ -69,86 +57,9 @@ func _find_first_audio_file(kind: String, name: String) -> String:
|
||||
directory.list_dir_begin()
|
||||
var entry := directory.get_next()
|
||||
while not entry.is_empty():
|
||||
var path := directory_path.path_join(entry)
|
||||
if not directory.current_is_dir() and _is_loadable_external_audio(path):
|
||||
if not directory.current_is_dir() and entry.get_extension().to_lower() in ADMITTED_EXTENSIONS:
|
||||
directory.list_dir_end()
|
||||
return path
|
||||
return directory_path.path_join(entry)
|
||||
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:
|
||||
return
|
||||
_ambience_player.stream = stream
|
||||
_ambience_player.play()
|
||||
|
||||
func _play_sfx(path: String) -> void:
|
||||
var stream := _load_audio_stream(path)
|
||||
if stream == null or _sfx_player == null:
|
||||
return
|
||||
_sfx_player.stream = stream
|
||||
_sfx_player.play()
|
||||
|
||||
func _load_audio_stream(path: String) -> AudioStream:
|
||||
# External roots are not imported Godot resources. Only use Godot-native loaders
|
||||
# that can accept a filesystem path; unavailable codecs fail closed.
|
||||
match path.get_extension().to_lower():
|
||||
"wav":
|
||||
return AudioStreamWAV.load_from_file(path)
|
||||
"ogg", "oga", "opus":
|
||||
return AudioStreamOggVorbis.load_from_file(path)
|
||||
"mp3":
|
||||
return AudioStreamMP3.load_from_file(path)
|
||||
_:
|
||||
return null
|
||||
|
||||
func _stop_ambience() -> void:
|
||||
if _ambience_player != null:
|
||||
_ambience_player.stop()
|
||||
|
||||
@@ -8,7 +8,7 @@ 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")
|
||||
empty_audio_home = ProjectSettings.globalize_path("user://s4-empty-audio-home-%s" % Time.get_ticks_usec())
|
||||
DirAccess.make_dir_recursive_absolute(empty_audio_home)
|
||||
OS.set_environment("DCW_AUDIO_HOME", empty_audio_home)
|
||||
game = GameScene.instantiate()
|
||||
@@ -38,3 +38,37 @@ func test_real_game_resolves_combat_with_empty_audio_home() -> void:
|
||||
assert_eq(audio.current_ambience_context, "combat")
|
||||
assert_eq(audio.get_last_ambience_path(), "")
|
||||
assert_eq(audio.get_last_sfx_path(), "")
|
||||
assert_false(audio.has_playable_ambience())
|
||||
assert_false(audio.has_playable_sfx())
|
||||
|
||||
func test_external_nominal_and_invalid_wavs_do_not_affect_combat() -> void:
|
||||
var audio = game.get_node("AudioLibrary")
|
||||
var nominal_path := empty_audio_home.path_join("audio/combat/silence.wav")
|
||||
var invalid_path := empty_audio_home.path_join("sfx/attack_given/truncated.wav")
|
||||
DirAccess.make_dir_recursive_absolute(nominal_path.get_base_dir())
|
||||
DirAccess.make_dir_recursive_absolute(invalid_path.get_base_dir())
|
||||
var nominal_file := FileAccess.open(nominal_path, FileAccess.WRITE)
|
||||
nominal_file.store_buffer(PackedByteArray([
|
||||
82, 73, 70, 70, 36, 0, 0, 0, 87, 65, 86, 69, 102, 109, 116, 32,
|
||||
16, 0, 0, 0, 1, 0, 1, 0, 68, 172, 0, 0, 136, 88, 1, 0,
|
||||
2, 0, 16, 0, 100, 97, 116, 97, 0, 0, 0, 0,
|
||||
]))
|
||||
nominal_file.close()
|
||||
var invalid_file := FileAccess.open(invalid_path, FileAccess.WRITE)
|
||||
invalid_file.store_string("truncated WAV")
|
||||
invalid_file.close()
|
||||
|
||||
game.submit_test_action(Vector2i.RIGHT)
|
||||
var first_melee := game.submit_test_action(Vector2i.RIGHT)
|
||||
var lethal_melee := game.submit_test_action(Vector2i.RIGHT)
|
||||
|
||||
assert_eq(first_melee[0].kind, &"melee")
|
||||
assert_eq(lethal_melee[1].kind, &"enemy_defeated")
|
||||
assert_false(game.get_game_state().enemies.has(&"goblin"))
|
||||
# Discovery remains diagnostic-only after combat; no external stream is playable.
|
||||
audio.set_ambience_context("combat")
|
||||
audio.play_sfx("attack_given")
|
||||
assert_eq(audio.get_last_ambience_path(), nominal_path)
|
||||
assert_eq(audio.get_last_sfx_path(), invalid_path)
|
||||
assert_false(audio.has_playable_ambience())
|
||||
assert_false(audio.has_playable_sfx())
|
||||
|
||||
@@ -12,6 +12,8 @@ func test_missing_audio_directories_are_silent_for_known_requests() -> void:
|
||||
assert_eq(library.current_ambience_context, "exploration")
|
||||
assert_eq(library.get_last_ambience_path(), "")
|
||||
assert_eq(library.get_last_sfx_path(), "")
|
||||
assert_false(library.has_playable_ambience())
|
||||
assert_false(library.has_playable_sfx())
|
||||
|
||||
func test_unknown_requests_are_silent() -> void:
|
||||
var library := AudioLibrary.new("res://tests/fixtures/missing-dcwascii")
|
||||
@@ -24,11 +26,22 @@ func test_unknown_requests_are_silent() -> void:
|
||||
assert_eq(library.current_ambience_context, "exploration")
|
||||
assert_eq(library.get_last_ambience_path(), "")
|
||||
assert_eq(library.get_last_sfx_path(), "")
|
||||
assert_false(library.has_playable_ambience())
|
||||
assert_false(library.has_playable_sfx())
|
||||
|
||||
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")
|
||||
func test_nominal_and_invalid_wavs_are_discovered_but_never_playable() -> void:
|
||||
var root := ProjectSettings.globalize_path("user://s4-external-audio-library")
|
||||
var nominal_path := root.path_join("audio/exploration/silence.wav")
|
||||
var invalid_path := root.path_join("sfx/attack_given/not-a-wave.wav")
|
||||
DirAccess.make_dir_recursive_absolute(nominal_path.get_base_dir())
|
||||
DirAccess.make_dir_recursive_absolute(invalid_path.get_base_dir())
|
||||
var nominal_file := FileAccess.open(nominal_path, FileAccess.WRITE)
|
||||
nominal_file.store_buffer(PackedByteArray([
|
||||
82, 73, 70, 70, 36, 0, 0, 0, 87, 65, 86, 69, 102, 109, 116, 32,
|
||||
16, 0, 0, 0, 1, 0, 1, 0, 68, 172, 0, 0, 136, 88, 1, 0,
|
||||
2, 0, 16, 0, 100, 97, 116, 97, 0, 0, 0, 0,
|
||||
]))
|
||||
nominal_file.close()
|
||||
var invalid_file := FileAccess.open(invalid_path, FileAccess.WRITE)
|
||||
invalid_file.store_string("this is not a WAV file")
|
||||
invalid_file.close()
|
||||
@@ -38,25 +51,7 @@ func test_invalid_wav_is_not_selected_or_played_and_does_not_block_requests() ->
|
||||
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")
|
||||
DirAccess.make_dir_recursive_absolute(sample_path.get_base_dir())
|
||||
var sample := FileAccess.open(sample_path, FileAccess.WRITE)
|
||||
sample.store_buffer(PackedByteArray([
|
||||
82, 73, 70, 70, 36, 0, 0, 0, 87, 65, 86, 69, 102, 109, 116, 32,
|
||||
16, 0, 0, 0, 1, 0, 1, 0, 68, 172, 0, 0, 136, 88, 1, 0,
|
||||
2, 0, 16, 0, 100, 97, 116, 97, 0, 0, 0, 0,
|
||||
]))
|
||||
sample.close()
|
||||
var library := AudioLibrary.new(root)
|
||||
add_child_autofree(library)
|
||||
|
||||
library.set_ambience_context("exploration")
|
||||
|
||||
assert_eq(library.get_last_ambience_path(), sample_path)
|
||||
assert_eq(library.get_last_ambience_path(), nominal_path)
|
||||
assert_eq(library.get_last_sfx_path(), invalid_path)
|
||||
assert_false(library.has_playable_ambience())
|
||||
assert_false(library.has_playable_sfx())
|
||||
|
||||
Reference in New Issue
Block a user