Skip to content

Commit 45d9926

Browse files
authored
Merge pull request #6339 from Goober5000/empty_sounds_fix
further refinement of sound fixes
2 parents 854b013 + ee9770d commit 45d9926

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

code/gamesnd/gamesnd.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,11 @@ void parse_gamesnd_new(game_snd* gs, bool no_create)
960960
}
961961
}
962962

963+
bool gamesnd_is_placeholder(const game_snd& gs)
964+
{
965+
return gs.sound_entries.empty() || gs.sound_entries[0].filename[0] == '\0';
966+
}
967+
963968
void gamesnd_parse_entry(game_snd *gs, bool &orig_no_create, SCP_vector<game_snd> *lookupVector, size_t lookupVectorMaxIndexableSize, bool (*is_reserved_index)(int))
964969
{
965970
SCP_string name;
@@ -1015,7 +1020,7 @@ void gamesnd_parse_entry(game_snd *gs, bool &orig_no_create, SCP_vector<game_snd
10151020
auto existing_gs = &lookupVector->at(vectorIndex);
10161021

10171022
// if the existing sound was an empty or placeholder sound, replace it, don't warn
1018-
if (existing_gs->sound_entries.empty() || existing_gs->sound_entries[0].filename[0] == '\0')
1023+
if (gamesnd_is_placeholder(*existing_gs))
10191024
{
10201025
gs = existing_gs;
10211026
orig_no_create = true; // prevent sound from being appended in parse_sound_table
@@ -1026,6 +1031,26 @@ void gamesnd_parse_entry(game_snd *gs, bool &orig_no_create, SCP_vector<game_snd
10261031
error_display(0, "Duplicate sound name \"%s\" found!", name.c_str());
10271032
}
10281033
}
1034+
else if (lookupVector)
1035+
{
1036+
// see if there is an empty or placeholder sound that we can replace
1037+
int i = 0;
1038+
for (const auto& ii : *lookupVector)
1039+
{
1040+
if (gamesnd_is_placeholder(ii) && !is_reserved_index(i))
1041+
{
1042+
vectorIndex = i;
1043+
gs = &lookupVector->at(vectorIndex);
1044+
orig_no_create = true; // prevent sound from being appended in parse_sound_table
1045+
// (leave no_create as false because we are creating a new sound and we need all the fields to be filled out)
1046+
1047+
// a placeholder can have a single entry with an empty filename, so remove that if it exists
1048+
gs->sound_entries.clear();
1049+
break;
1050+
}
1051+
i++;
1052+
}
1053+
}
10291054

10301055
gs->name = std::move(name);
10311056
}
@@ -1461,6 +1486,17 @@ void gamesnd_parse_soundstbl(bool first_stage)
14611486
// these vectors should be the same size
14621487
while (Snds_iface_handle.size() < Snds_iface.size())
14631488
Snds_iface_handle.push_back(sound_handle::invalid());
1489+
1490+
// mark any placeholder sounds as invalid
1491+
// (most places in the code already check for this, but it is possible for sexps and scripts to attempt to play an empty sound)
1492+
for (auto& snd : Snds)
1493+
if (gamesnd_is_placeholder(snd))
1494+
snd.flags |= GAME_SND_NOT_VALID;
1495+
1496+
// ditto for interface sounds
1497+
for (auto& snd : Snds_iface)
1498+
if (gamesnd_is_placeholder(snd))
1499+
snd.flags |= GAME_SND_NOT_VALID;
14641500
}
14651501
else
14661502
{

0 commit comments

Comments
 (0)