Skip to content

Commit

Permalink
Prevent game to assert when trying to play music with invalid params
Browse files Browse the repository at this point in the history
  • Loading branch information
Kolfering committed Sep 28, 2024
1 parent a55ecbb commit 7c4bbfd
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
3 changes: 1 addition & 2 deletions Source_Files/Lua/lua_music.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ static int Lua_Music_Volume_Set(lua_State* L)

static bool Lua_Music_Valid(int16 index)
{
auto slot = index >= 0 ? Music::instance()->GetSlot(index + Music::reserved_music_slots) : nullptr;
return slot && slot->IsInit();
return index >= 0 ? Music::instance()->GetSlot(index + Music::reserved_music_slots) : nullptr;
}

static int Lua_MusicManager_New(lua_State* L)
Expand Down
6 changes: 3 additions & 3 deletions Source_Files/Sound/Music.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ void Music::Idle()
for (int i = 0; i < music_slots.size(); i++) {

auto& slot = music_slots.at(i);
if (slot->IsInit() && slot->IsFading()) {
if (slot->Playing() && slot->IsFading()) {
auto volumeResult = slot->ComputeFadingVolume();
bool fadeIn = volumeResult.first;
float vol = fadeIn ? std::min(volumeResult.second, slot->GetLimitFadeVolume()) : std::max(volumeResult.second, slot->GetLimitFadeVolume());
Expand Down Expand Up @@ -207,13 +207,13 @@ bool Music::Slot::SetParameters(bool loop, float volume)

void Music::StandardSlot::Play()
{
if (!OpenALManager::Get() || Playing()) return;
if (!OpenALManager::Get() || !IsInit() || Playing()) return;
musicPlayer = OpenALManager::Get()->PlayMusic(decoder, parameters);
}

void Music::DynamicSlot::Play()
{
if (!OpenALManager::Get() || Playing()) return;
if (!OpenALManager::Get() || !IsInit() || Playing()) return;
musicPlayer = OpenALManager::Get()->PlayDynamicMusic(dynamic_music_presets, default_preset_index, default_segment_index, parameters);
}

Expand Down
4 changes: 2 additions & 2 deletions Source_Files/Sound/Music.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Music
MusicParameters parameters;
public:
void Fade(float limitVolume, short duration, bool stopOnNoVolume = true);
bool Playing() const { return IsInit() && musicPlayer && musicPlayer->IsActive(); }
bool Playing() const { return musicPlayer && musicPlayer->IsActive(); }
void Pause();
virtual void Close();
virtual bool IsInit() const = 0;
Expand Down Expand Up @@ -88,7 +88,7 @@ class Music

class DynamicSlot : public Slot {
public:
bool IsInit() const override { return true; };
bool IsInit() const override { return IsSegmentIndexValid(default_preset_index, default_segment_index); };
void Close() override;
void Play() override;
int LoadTrack(FileSpecifier* file);
Expand Down

0 comments on commit 7c4bbfd

Please sign in to comment.