Core/Misc: Various dynspawn cleanup and refactors split off from pooling rewrite:

- Map::RemoveRespawnTime(SpawnObjectType, LowType, doRespawn) split into Map::Respawn and Map::RemoveRespawnTime, without the extra boolean
- Map::RemoveRespawnTime(RespawnInfo*) merged into Map::DeleteRespawnInfo(RespawnInfo*) and is now private
- Map::DeleteRespawnInfo(void) renamed to Map::UnloadAllRespawnInfos to properly describe what it does
- Map::ProcessRespawns now actually saves the delayed respawn time to DB if the respawn was delayed
- Map::AddRespawnInfo now takes const reference, and returns success as a boolean
- Map::AddRespawnInfo no longer offers an unused "replace" parameter
- Map::DeleteRespawnInfo no longer offers a variety of unused private overloads
- Map::SaveRespawnTime no longer offers a tantalizing writeDB parameter. Parameter is now called "startup" to properly describe what it does.
- Map::SaveRespawnInfoDB now takes RespawnInfo reference instead of all the various fields. Still public because compatibility mode. QQ.
- Map::GetWorldObjectBySpawnId sanitized
- Map::GetXRespawnTime methods sanitized to all go through Map::GetRespawnTime

----------

Core/DB: Unify `creature_respawn` and `gameobject_respawn` into a single `respawn` table
This commit is contained in:
Treeston authored and Ovahlord committed 2019-08-18 12:47:10 +02:00
1 parent f9184f026b
commit 90540d684b
15 files changed
+184 -236

No files matched your search

+63 -102
View File
@@ -76,7 +76,7 @@ Map::~Map()
// Delete all waiting spawns, else there will be a memory leak
// This doesn't delete from database.
DeleteRespawnInfo();
UnloadAllRespawnInfos();
while (!i_worldObjects.empty())
{
@@ -3178,9 +3178,12 @@ void Map::Respawn(RespawnInfo* info, SQLTransaction dbTrans)
if (!CheckRespawn(info))
{
if (info->respawnTime)
SaveRespawnTime(info->type, info->spawnId, info->entry, info->respawnTime, info->gridId, true, true, dbTrans);
{
_respawnTimes.decrease(info->handle);
SaveRespawnInfoDB(*info, dbTrans);
}
else
RemoveRespawnTime(info);
DeleteRespawnInfo(info, dbTrans);
return;
}
@@ -3188,23 +3191,13 @@ void Map::Respawn(RespawnInfo* info, SQLTransaction dbTrans)
SpawnObjectType const type = info->type;
uint32 const gridId = info->gridId;
ObjectGuid::LowType const spawnId = info->spawnId;
RemoveRespawnTime(info);
DeleteRespawnInfo(info, dbTrans);
DoRespawn(type, spawnId, gridId);
}
void Map::Respawn(std::vector<RespawnInfo*>& respawnData, SQLTransaction dbTrans)
bool Map::AddRespawnInfo(RespawnInfo const& info)
{
SQLTransaction trans = dbTrans ? dbTrans : CharacterDatabase.BeginTransaction();
for (RespawnInfo* info : respawnData)
Respawn(info, trans);
if (!dbTrans)
CharacterDatabase.CommitTransaction(trans);
}
void Map::AddRespawnInfo(RespawnInfo& info, bool replace)
{
if (!info.spawnId)
return;
ASSERT(info.spawnId, "Attempt to schedule respawn with zero spawnid (type %u)", uint32(info.type));
RespawnInfoMap& bySpawnIdMap = GetRespawnMapForType(info.type);
@@ -3212,13 +3205,10 @@ void Map::AddRespawnInfo(RespawnInfo& info, bool replace)
if (it != bySpawnIdMap.end()) // spawnid already has a respawn scheduled
{
RespawnInfo* const existing = it->second;
if (replace || info.respawnTime < existing->respawnTime) // delete existing in this case
if (info.respawnTime < existing->respawnTime) // delete existing in this case
DeleteRespawnInfo(existing);
else // don't delete existing, instead replace respawn time so caller saves the correct time
{
info.respawnTime = existing->respawnTime;
return;
}
else
return false;
}
// if we get to this point, we should insert the respawninfo (there either was no prior entry, or it was deleted already)
@@ -3226,6 +3216,7 @@ void Map::AddRespawnInfo(RespawnInfo& info, bool replace)
ri->handle = _respawnTimes.push(ri);
bool success = bySpawnIdMap.emplace(ri->spawnId, ri).second;
ASSERT(success, "Insertion of respawn info with id (%u,%u) into spawn id map failed - state desync.", uint32(ri->type), ri->spawnId);
return true;
}
static void PushRespawnInfoFrom(std::vector<RespawnInfo*>& data, RespawnInfoMap const& map)
@@ -3252,7 +3243,7 @@ RespawnInfo* Map::GetRespawnInfo(SpawnObjectType type, ObjectGuid::LowType spawn
return it->second;
}
void Map::DeleteRespawnInfo() // delete everything
void Map::UnloadAllRespawnInfos() // delete everything from memory
{
for (RespawnInfo* info : _respawnTimes)
delete info;
@@ -3261,7 +3252,7 @@ void Map::DeleteRespawnInfo() // delete everything
_gameObjectRespawnTimesBySpawnId.clear();
}
void Map::DeleteRespawnInfo(RespawnInfo* info)
void Map::DeleteRespawnInfo(RespawnInfo* info, SQLTransaction dbTrans)
{
// Delete from all relevant containers to ensure consistency
ASSERT(info);
@@ -3270,48 +3261,21 @@ void Map::DeleteRespawnInfo(RespawnInfo* info)
size_t const n = GetRespawnMapForType(info->type).erase(info->spawnId);
ASSERT(n == 1, "Respawn stores inconsistent for map %u, spawnid %u (type %u)", GetId(), info->spawnId, uint32(info->type));
//respawn heap
// respawn heap
_respawnTimes.erase(info->handle);
// database
PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_RESPAWN);
stmt->setUInt16(0, info->type);
stmt->setUInt32(1, info->spawnId);
stmt->setUInt16(2, GetId());
stmt->setUInt32(3, GetInstanceId());
CharacterDatabase.ExecuteOrAppend(dbTrans, stmt);
// then cleanup the object
delete info;
}
void Map::RemoveRespawnTime(RespawnInfo* info, bool doRespawn, SQLTransaction dbTrans)
{
PreparedStatement* stmt;
switch (info->type)
{
case SPAWN_TYPE_CREATURE:
stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_CREATURE_RESPAWN);
break;
case SPAWN_TYPE_GAMEOBJECT:
stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_GO_RESPAWN);
break;
default:
ASSERT(false, "Invalid respawninfo type %u for spawnid %u map %u", uint32(info->type), info->spawnId, GetId());
return;
}
stmt->setUInt32(0, info->spawnId);
stmt->setUInt16(1, GetId());
stmt->setUInt32(2, GetInstanceId());
CharacterDatabase.ExecuteOrAppend(dbTrans, stmt);
if (doRespawn)
Respawn(info);
else
DeleteRespawnInfo(info);
}
void Map::RemoveRespawnTime(std::vector<RespawnInfo*>& respawnData, bool doRespawn, SQLTransaction dbTrans)
{
SQLTransaction trans = dbTrans ? dbTrans : CharacterDatabase.BeginTransaction();
for (RespawnInfo* info : respawnData)
RemoveRespawnTime(info, doRespawn, trans);
if (!dbTrans)
CharacterDatabase.CommitTransaction(trans);
}
void Map::ProcessRespawns()
{
time_t now = time(nullptr);
@@ -3338,6 +3302,7 @@ void Map::ProcessRespawns()
{
ASSERT(now < next->respawnTime); // infinite loop guard
_respawnTimes.decrease(next->handle);
SaveRespawnInfoDB(*next);
}
}
}
@@ -3418,7 +3383,7 @@ bool Map::SpawnGroupSpawn(uint32 groupId, bool ignoreRespawn, bool force, std::v
continue;
// we need to remove the respawn time, otherwise we'd end up double spawning
RemoveRespawnTime(data->type, data->spawnId, false);
RemoveRespawnTime(data->type, data->spawnId);
}
// don't spawn if the grid isn't loaded (will be handled in grid loader)
@@ -4436,12 +4401,15 @@ void Map::UpdateIteratorBack(Player* player)
m_mapRefIter = m_mapRefIter->nocheck_prev();
}
void Map::SaveRespawnTime(SpawnObjectType type, ObjectGuid::LowType spawnId, uint32 entry, time_t respawnTime, uint32 gridId, bool writeDB, bool replace, SQLTransaction dbTrans)
void Map::SaveRespawnTime(SpawnObjectType type, ObjectGuid::LowType spawnId, uint32 entry, time_t respawnTime, uint32 gridId, SQLTransaction dbTrans, bool startup)
{
if (!spawnId)
return;
if (!respawnTime)
{
// Delete only
RemoveRespawnTime(type, spawnId, false, dbTrans);
RemoveRespawnTime(type, spawnId, dbTrans);
return;
}
@@ -4451,26 +4419,31 @@ void Map::SaveRespawnTime(SpawnObjectType type, ObjectGuid::LowType spawnId, uin
ri.entry = entry;
ri.respawnTime = respawnTime;
ri.gridId = gridId;
AddRespawnInfo(ri, replace);
bool success = AddRespawnInfo(ri);
if (writeDB)
SaveRespawnTimeDB(type, spawnId, ri.respawnTime, dbTrans); // might be different from original respawn time if we didn't replace
if (startup)
{
if (!success)
TC_LOG_ERROR("maps", "Attempt to load saved respawn %" PRIu64 " for (%u,%u) failed - duplicate respawn? Skipped.", respawnTime, uint32(type), spawnId);
}
else if (success)
SaveRespawnInfoDB(ri, dbTrans);
}
void Map::SaveRespawnTimeDB(SpawnObjectType type, ObjectGuid::LowType spawnId, time_t respawnTime, SQLTransaction dbTrans)
void Map::SaveRespawnInfoDB(RespawnInfo const& info, SQLTransaction dbTrans)
{
// Just here for support of compatibility mode
PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement((type == SPAWN_TYPE_GAMEOBJECT) ? CHAR_REP_GO_RESPAWN : CHAR_REP_CREATURE_RESPAWN);
stmt->setUInt32(0, spawnId);
stmt->setUInt64(1, uint64(respawnTime));
stmt->setUInt16(2, GetId());
stmt->setUInt32(3, GetInstanceId());
PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_REP_RESPAWN);
stmt->setUInt16(0, info.type);
stmt->setUInt32(1, info.spawnId);
stmt->setUInt64(2, uint64(info.respawnTime));
stmt->setUInt16(3, GetId());
stmt->setUInt32(4, GetInstanceId());
CharacterDatabase.ExecuteOrAppend(dbTrans, stmt);
}
void Map::LoadRespawnTimes()
{
PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_SEL_CREATURE_RESPAWNS);
PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_SEL_RESPAWNS);
stmt->setUInt16(0, GetId());
stmt->setUInt32(1, GetInstanceId());
if (PreparedQueryResult result = CharacterDatabase.Query(stmt))
@@ -4478,41 +4451,29 @@ void Map::LoadRespawnTimes()
do
{
Field* fields = result->Fetch();
ObjectGuid::LowType loguid = fields[0].GetUInt32();
uint64 respawnTime = fields[1].GetUInt64();
SpawnObjectType type = SpawnObjectType(fields[0].GetUInt16());
ObjectGuid::LowType spawnId = fields[1].GetUInt32();
uint64 respawnTime = fields[2].GetUInt64();
if (CreatureData const* cdata = sObjectMgr->GetCreatureData(loguid))
SaveRespawnTime(SPAWN_TYPE_CREATURE, loguid, cdata->id, time_t(respawnTime), Trinity::ComputeGridCoord(cdata->spawnPoint.GetPositionX(), cdata->spawnPoint.GetPositionY()).GetId(), false);
} while (result->NextRow());
}
stmt = CharacterDatabase.GetPreparedStatement(CHAR_SEL_GO_RESPAWNS);
stmt->setUInt16(0, GetId());
stmt->setUInt32(1, GetInstanceId());
if (PreparedQueryResult result = CharacterDatabase.Query(stmt))
{
do
{
Field* fields = result->Fetch();
ObjectGuid::LowType loguid = fields[0].GetUInt32();
uint64 respawnTime = fields[1].GetUInt64();
if (GameObjectData const* godata = sObjectMgr->GetGameObjectData(loguid))
SaveRespawnTime(SPAWN_TYPE_GAMEOBJECT, loguid, godata->id, time_t(respawnTime), GetZoneId(PhasingHandler::GetEmptyPhaseShift(), godata->spawnPoint), Trinity::ComputeGridCoord(godata->spawnPoint.GetPositionX(), godata->spawnPoint.GetPositionY()).GetId(), false);
if (type < SPAWN_TYPE_MAX)
{
if (SpawnData const* data = sObjectMgr->GetSpawnData(type, spawnId))
SaveRespawnTime(type, spawnId, data->id, time_t(respawnTime), Trinity::ComputeGridCoord(data->spawnPoint.GetPositionX(), data->spawnPoint.GetPositionY()).GetId(), nullptr, true);
else
TC_LOG_ERROR("maps", "Loading saved respawn time of %" PRIu64 " for spawnid (%u,%u) - spawn does not exist, ignoring", respawnTime, uint32(type), spawnId);
}
else
{
TC_LOG_ERROR("maps", "Loading saved respawn time of %" PRIu64 " for spawnid (%u,%u) - invalid spawn type, ignoring", respawnTime, uint32(type), spawnId);
}
} while (result->NextRow());
}
}
void Map::DeleteRespawnTimesInDB(uint16 mapId, uint32 instanceId)
/*static*/ void Map::DeleteRespawnTimesInDB(uint16 mapId, uint32 instanceId)
{
PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_CREATURE_RESPAWN_BY_INSTANCE);
stmt->setUInt16(0, mapId);
stmt->setUInt32(1, instanceId);
CharacterDatabase.Execute(stmt);
stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_GO_RESPAWN_BY_INSTANCE);
PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_ALL_RESPAWNS);
stmt->setUInt16(0, mapId);
stmt->setUInt32(1, instanceId);
CharacterDatabase.Execute(stmt);