Skip to content

Commit

Permalink
Merge pull request #6339 from Goober5000/empty_sounds_fix
Browse files Browse the repository at this point in the history
further refinement of sound fixes
  • Loading branch information
Goober5000 authored Sep 9, 2024
2 parents 854b013 + ee9770d commit 45d9926
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion code/gamesnd/gamesnd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,11 @@ void parse_gamesnd_new(game_snd* gs, bool no_create)
}
}

bool gamesnd_is_placeholder(const game_snd& gs)
{
return gs.sound_entries.empty() || gs.sound_entries[0].filename[0] == '\0';
}

void gamesnd_parse_entry(game_snd *gs, bool &orig_no_create, SCP_vector<game_snd> *lookupVector, size_t lookupVectorMaxIndexableSize, bool (*is_reserved_index)(int))
{
SCP_string name;
Expand Down Expand Up @@ -1015,7 +1020,7 @@ void gamesnd_parse_entry(game_snd *gs, bool &orig_no_create, SCP_vector<game_snd
auto existing_gs = &lookupVector->at(vectorIndex);

// if the existing sound was an empty or placeholder sound, replace it, don't warn
if (existing_gs->sound_entries.empty() || existing_gs->sound_entries[0].filename[0] == '\0')
if (gamesnd_is_placeholder(*existing_gs))
{
gs = existing_gs;
orig_no_create = true; // prevent sound from being appended in parse_sound_table
Expand All @@ -1026,6 +1031,26 @@ void gamesnd_parse_entry(game_snd *gs, bool &orig_no_create, SCP_vector<game_snd
error_display(0, "Duplicate sound name \"%s\" found!", name.c_str());
}
}
else if (lookupVector)
{
// see if there is an empty or placeholder sound that we can replace
int i = 0;
for (const auto& ii : *lookupVector)
{
if (gamesnd_is_placeholder(ii) && !is_reserved_index(i))
{
vectorIndex = i;
gs = &lookupVector->at(vectorIndex);
orig_no_create = true; // prevent sound from being appended in parse_sound_table
// (leave no_create as false because we are creating a new sound and we need all the fields to be filled out)

// a placeholder can have a single entry with an empty filename, so remove that if it exists
gs->sound_entries.clear();
break;
}
i++;
}
}

gs->name = std::move(name);
}
Expand Down Expand Up @@ -1461,6 +1486,17 @@ void gamesnd_parse_soundstbl(bool first_stage)
// these vectors should be the same size
while (Snds_iface_handle.size() < Snds_iface.size())
Snds_iface_handle.push_back(sound_handle::invalid());

// mark any placeholder sounds as invalid
// (most places in the code already check for this, but it is possible for sexps and scripts to attempt to play an empty sound)
for (auto& snd : Snds)
if (gamesnd_is_placeholder(snd))
snd.flags |= GAME_SND_NOT_VALID;

// ditto for interface sounds
for (auto& snd : Snds_iface)
if (gamesnd_is_placeholder(snd))
snd.flags |= GAME_SND_NOT_VALID;
}
else
{
Expand Down

0 comments on commit 45d9926

Please sign in to comment.