From b2968c55da6b73250d809b42af202ffb622044d9 Mon Sep 17 00:00:00 2001 From: Kp Date: Sun, 25 Jul 2021 23:00:56 +0000 Subject: [PATCH] Improve error reporting for read_sndfile Move error reporting down into read_sndfile, so that individual failure paths can have specific error messages. --- common/main/piggy.h | 2 +- similar/main/mission.cpp | 2 +- similar/main/piggy.cpp | 20 ++++++++++++-------- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/common/main/piggy.h b/common/main/piggy.h index da6a00015..f3669e7ba 100644 --- a/common/main/piggy.h +++ b/common/main/piggy.h @@ -152,7 +152,7 @@ void load_d1_bitmap_replacements(); */ grs_bitmap *read_extra_bitmap_d1_pig(const char *name, grs_bitmap &out); int read_hamfile(); -int read_sndfile(); +void read_sndfile(int required); #endif } diff --git a/similar/main/mission.cpp b/similar/main/mission.cpp index 8eecd748e..4bee28736 100644 --- a/similar/main/mission.cpp +++ b/similar/main/mission.cpp @@ -894,7 +894,7 @@ int load_mission_ham() { // re-read sounds in case mission has custom .sXX Num_sound_files = 0; - read_sndfile(); + read_sndfile(0); piggy_read_sounds(); } diff --git a/similar/main/piggy.cpp b/similar/main/piggy.cpp index 29b5ecede..eeaeef187 100644 --- a/similar/main/piggy.cpp +++ b/similar/main/piggy.cpp @@ -1075,7 +1075,7 @@ int read_hamfile() return 1; } -int read_sndfile() +void read_sndfile(const int required) { int snd_id,snd_version; int N_sounds; @@ -1087,15 +1087,22 @@ int read_sndfile() char temp_name_read[16]; int sbytes = 0; - auto snd_fp = PHYSFSX_openReadBuffered(DEFAULT_SNDFILE); + const auto filename = DEFAULT_SNDFILE; + auto snd_fp = PHYSFSX_openReadBuffered(filename); if (!snd_fp) - return 0; + { + if (required) + Error("Cannot open sound file: %s\n", filename); + return; + } //make sure soundfile is valid type file & is up-to-date snd_id = PHYSFSX_readInt(snd_fp); snd_version = PHYSFSX_readInt(snd_fp); if (snd_id != SNDFILE_ID || snd_version != SNDFILE_VERSION) { - return 0; + if (required) + Error("Cannot load sound file: expected (id=%.8lx version=%.8x), found (id=%.8x version=%.8x) in \"%s\"", SNDFILE_ID, SNDFILE_VERSION, snd_id, snd_version, filename); + return; } N_sounds = PHYSFSX_readInt(snd_fp); @@ -1117,7 +1124,6 @@ int read_sndfile() sbytes += sndh.length; } SoundBits = std::make_unique(sbytes + 16); - return 1; } int properties_init(void) @@ -1159,9 +1165,7 @@ int properties_init(void) if (Piggy_hamfile_version >= 3) { - snd_ok = read_sndfile(); - if (!snd_ok) - Error("Cannot open sound file: %s\n", DEFAULT_SNDFILE); + read_sndfile(1); } return (ham_ok && snd_ok); //read ok