fix(Core/Weather): Improve weather system thread safety (#22772)

Co-authored-by: Shauren <shauren.trinity@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Takenbacon
2025-09-29 07:43:30 -07:00
committed by GitHub
parent 5f17121117
commit 22f93eaca6
20 changed files with 186 additions and 247 deletions

View File

@@ -20,16 +20,16 @@
*/
#include "Weather.h"
#include "Map.h"
#include "MiscPackets.h"
#include "Player.h"
#include "ScriptMgr.h"
#include "Util.h"
#include "World.h"
#include "WorldSessionMgr.h"
/// Create the Weather object
Weather::Weather(uint32 zone, WeatherData const* weatherChances)
: m_zone(zone), m_weatherChances(weatherChances)
Weather::Weather(Map* map, uint32 zone, WeatherData const* weatherChances)
: m_map(map), m_zone(zone), m_weatherChances(weatherChances)
{
m_timer.SetInterval(sWorld->getIntConfig(CONFIG_INTERVAL_CHANGEWEATHER));
m_type = WEATHER_TYPE_FINE;
@@ -190,6 +190,12 @@ void Weather::SendWeatherUpdateToPlayer(Player* player)
player->SendDirectMessage(weather.Write());
}
void Weather::SendFineWeatherUpdateToPlayer(Player* player)
{
WorldPackets::Misc::Weather weather(WEATHER_STATE_FINE);
player->SendDirectMessage(weather.Write());
}
/// Send the new weather to all players in the zone
bool Weather::UpdateWeather()
{
@@ -204,7 +210,7 @@ bool Weather::UpdateWeather()
WorldPackets::Misc::Weather weather(state, m_grade);
//- Returns false if there were no players found to update
if (!sWorldSessionMgr->SendZoneMessage(m_zone, weather.Write()))
if (!m_map->SendZoneMessage(m_zone, weather.Write()))
return false;
///- Log the event

View File

@@ -25,6 +25,7 @@
#include "SharedDefines.h"
#include "Timer.h"
class Map;
class Player;
#define WEATHER_SEASONS 4
@@ -63,7 +64,7 @@ enum WeatherState : uint32
class Weather
{
public:
Weather(uint32 zone, WeatherData const* weatherChances);
Weather(Map* map, uint32 zone, WeatherData const* weatherChances);
~Weather() = default;
bool Update(uint32 diff);
@@ -71,6 +72,7 @@ public:
bool UpdateWeather();
void SendWeatherUpdateToPlayer(Player* player);
static void SendFineWeatherUpdateToPlayer(Player* player);
void SetWeather(WeatherType type, float grade);
/// For which zone is this weather?
@@ -79,6 +81,7 @@ public:
private:
[[nodiscard]] WeatherState GetWeatherState() const;
Map* m_map;
uint32 m_zone;
WeatherType m_type;
float m_grade;

View File

@@ -20,63 +20,25 @@
*/
#include "WeatherMgr.h"
#include "Containers.h"
#include "DatabaseEnv.h"
#include "Log.h"
#include "MiscPackets.h"
#include "ObjectMgr.h"
#include "Player.h"
#include "QueryResult.h"
#include "Timer.h"
#include "Weather.h"
#include "WorldSession.h"
namespace WeatherMgr
{
namespace
{
typedef std::unordered_map<uint32, std::unique_ptr<Weather>> WeatherMap;
typedef std::unordered_map<uint32, WeatherData> WeatherZoneMap;
WeatherMap m_weathers;
WeatherZoneMap mWeatherZoneMap;
WeatherData const* GetWeatherData(uint32 zone_id)
{
WeatherZoneMap::const_iterator itr = mWeatherZoneMap.find(zone_id);
return (itr != mWeatherZoneMap.end()) ? &itr->second : nullptr;
}
std::unordered_map<uint32, WeatherData> _weatherData;
}
/// Find a Weather object by the given zoneid
Weather* FindWeather(uint32 id)
WeatherData const* GetWeatherData(uint32 zone_id)
{
WeatherMap::const_iterator itr = m_weathers.find(id);
return (itr != m_weathers.end()) ? itr->second.get() : 0;
}
/// Remove a Weather object for the given zoneid
void RemoveWeather(uint32 id)
{
// not called at the moment. Kept for completeness
WeatherMap::iterator itr = m_weathers.find(id);
if (itr != m_weathers.end())
m_weathers.erase(itr);
}
/// Add a Weather object to the list
Weather* AddWeather(uint32 zone_id)
{
WeatherData const* weatherChances = GetWeatherData(zone_id);
// zone does not have weather, ignore
if (!weatherChances)
return nullptr;
Weather* w = new Weather(zone_id, weatherChances);
m_weathers[w->GetZone()].reset(w);
w->ReGenerate();
w->UpdateWeather();
return w;
return Acore::Containers::MapGetValuePtr(_weatherData, zone_id);
}
void LoadWeatherData()
@@ -105,7 +67,7 @@ namespace WeatherMgr
uint32 zone_id = fields[0].Get<uint32>();
WeatherData& wzc = mWeatherZoneMap[zone_id];
WeatherData& wzc = _weatherData[zone_id];
for (uint8 season = 0; season < WEATHER_SEASONS; ++season)
{
@@ -140,27 +102,4 @@ namespace WeatherMgr
LOG_INFO("server.loading", ">> Loaded {} Weather Definitions in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
LOG_INFO("server.loading", " ");
}
void SendFineWeatherUpdateToPlayer(Player* player)
{
WorldPackets::Misc::Weather weather(WEATHER_STATE_FINE);
player->SendDirectMessage(weather.Write());
}
void Update(uint32 diff)
{
///- Send an update signal to Weather objects
WeatherMap::iterator itr, next;
for (itr = m_weathers.begin(); itr != m_weathers.end(); itr = next)
{
next = itr;
++next;
///- and remove Weather objects for zones with no player
// As interval > WorldTick
if (!itr->second->Update(diff))
m_weathers.erase(itr);
}
}
} // namespace

View File

@@ -25,19 +25,12 @@
#include "Define.h"
class Weather;
class Player;
struct WeatherData;
namespace WeatherMgr
{
void LoadWeatherData();
Weather* FindWeather(uint32 id);
Weather* AddWeather(uint32 zone_id);
void RemoveWeather(uint32 zone_id);
void SendFineWeatherUpdateToPlayer(Player* player);
void Update(uint32 diff);
WeatherData const* GetWeatherData(uint32 zone_id);
}
#endif