Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server: avoid re-use of recent ParticleSpawner and Sound IDs #14045

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2171,12 +2171,18 @@ void Server::SendPlayerSpeed(session_t peer_id, const v3f &added_vel)

inline s32 Server::nextSoundId()
{
s32 ret = m_next_sound_id;
if (m_next_sound_id == INT32_MAX)
m_next_sound_id = 0; // signed overflow is undefined
else
m_next_sound_id++;
return ret;
s32 free_id = m_playing_sounds_id_last_used;
while (free_id == 0 || m_playing_sounds.find(free_id) != m_playing_sounds.end()) {
if (free_id == INT32_MAX)
free_id = 0; // signed overflow is undefined
else
free_id++;

if (free_id == m_playing_sounds_id_last_used)
return 0;
}
m_playing_sounds_id_last_used = free_id;
return free_id;
}

s32 Server::playSound(ServerPlayingSound &params, bool ephemeral)
Expand Down Expand Up @@ -2232,6 +2238,8 @@ s32 Server::playSound(ServerPlayingSound &params, bool ephemeral)

// old clients will still use this, so pick a reserved ID (-1)
const s32 id = ephemeral ? -1 : nextSoundId();
if (id == 0)
return 0;

float gain = params.gain * params.spec.gain;
NetworkPacket pkt(TOCLIENT_PLAY_SOUND, 0);
Expand Down
2 changes: 1 addition & 1 deletion src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ class Server : public con::PeerHandler, public MapEventReceiver,
Sounds
*/
std::unordered_map<s32, ServerPlayingSound> m_playing_sounds;
s32 m_next_sound_id = 0; // positive values only
s32 m_playing_sounds_id_last_used = 0; // positive values only
s32 nextSoundId();

ModStorageDatabase *m_mod_storage_database = nullptr;
Expand Down
18 changes: 9 additions & 9 deletions src/serverenvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1637,16 +1637,16 @@ u32 ServerEnvironment::addParticleSpawner(float exptime)
// Timers with lifetime 0 do not expire
float time = exptime > 0.f ? exptime : PARTICLE_SPAWNER_NO_EXPIRY;

u32 id = 0;
for (;;) { // look for unused particlespawner id
id++;
std::unordered_map<u32, float>::iterator f = m_particle_spawners.find(id);
if (f == m_particle_spawners.end()) {
m_particle_spawners[id] = time;
break;
}
u32 free_id = m_particle_spawners_id_last_used;
while (free_id == 0 || m_particle_spawners.find(free_id) != m_particle_spawners.end()) {
if (free_id == m_particle_spawners_id_last_used)
return 0; // full
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is zero a valid id?
I suppose not, would be good to use std::optional<u32> for the return one day

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I currently do not see any indicator that 0 is a reserved ID - neither on client- nor server-side. However, it will at least be more consistent other the ID assignments like in ActiveObjectMgr where 0 is reserved.

free_id++;
}
return id;

m_particle_spawners_id_last_used = free_id;
m_particle_spawners[free_id] = time;
return free_id;
}

u32 ServerEnvironment::addParticleSpawner(float exptime, u16 attached_id)
Expand Down
1 change: 1 addition & 0 deletions src/serverenvironment.h
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ class ServerEnvironment final : public Environment
// Particles
IntervalLimiter m_particle_management_interval;
std::unordered_map<u32, float> m_particle_spawners;
u32 m_particle_spawners_id_last_used = 0;
std::unordered_map<u32, u16> m_particle_spawner_attachments;

// Environment metrics
Expand Down