mirror of https://github.com/OpenTTD/OpenTTD
Fix ab1a4c6c: Crash if the "No Music" set is loaded because there is no current set_index. (#11511)
Add a helper function to get the current set_index which which checks it is in range.pull/11517/head
parent
27082f9efa
commit
6f35f3274f
|
@ -87,6 +87,7 @@ struct MusicSystem {
|
||||||
void PlaylistClear();
|
void PlaylistClear();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
uint GetSetIndex();
|
||||||
void SetPositionBySetIndex(uint set_index);
|
void SetPositionBySetIndex(uint set_index);
|
||||||
void ChangePlaylistPosition(int ofs);
|
void ChangePlaylistPosition(int ofs);
|
||||||
int playlist_position;
|
int playlist_position;
|
||||||
|
@ -191,6 +192,17 @@ void MusicSystem::SetPositionBySetIndex(uint set_index)
|
||||||
if (it != std::end(this->active_playlist)) this->playlist_position = std::distance(std::begin(this->active_playlist), it);
|
if (it != std::end(this->active_playlist)) this->playlist_position = std::distance(std::begin(this->active_playlist), it);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get set index from current playlist position.
|
||||||
|
* @return current set index, or UINT_MAX if nothing is selected.
|
||||||
|
*/
|
||||||
|
uint MusicSystem::GetSetIndex()
|
||||||
|
{
|
||||||
|
return static_cast<size_t>(this->playlist_position) < this->active_playlist.size()
|
||||||
|
? this->active_playlist[this->playlist_position].set_index
|
||||||
|
: UINT_MAX;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enable shuffle mode.
|
* Enable shuffle mode.
|
||||||
*/
|
*/
|
||||||
|
@ -198,7 +210,7 @@ void MusicSystem::Shuffle()
|
||||||
{
|
{
|
||||||
_settings_client.music.shuffle = true;
|
_settings_client.music.shuffle = true;
|
||||||
|
|
||||||
uint set_index = this->active_playlist[this->playlist_position].set_index;
|
uint set_index = this->GetSetIndex();
|
||||||
this->active_playlist = this->displayed_playlist;
|
this->active_playlist = this->displayed_playlist;
|
||||||
for (size_t i = 0; i < this->active_playlist.size(); i++) {
|
for (size_t i = 0; i < this->active_playlist.size(); i++) {
|
||||||
size_t shuffle_index = InteractiveRandom() % (this->active_playlist.size() - i);
|
size_t shuffle_index = InteractiveRandom() % (this->active_playlist.size() - i);
|
||||||
|
@ -217,7 +229,7 @@ void MusicSystem::Unshuffle()
|
||||||
{
|
{
|
||||||
_settings_client.music.shuffle = false;
|
_settings_client.music.shuffle = false;
|
||||||
|
|
||||||
uint set_index = this->active_playlist[this->playlist_position].set_index;
|
uint set_index = this->GetSetIndex();
|
||||||
this->active_playlist = this->displayed_playlist;
|
this->active_playlist = this->displayed_playlist;
|
||||||
this->SetPositionBySetIndex(set_index);
|
this->SetPositionBySetIndex(set_index);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue