Skip to content

Commit

Permalink
fix: task context is being sent empty (opentibiabr#2871)
Browse files Browse the repository at this point in the history
Solve the problem where the message "[Task::Task] is sent: the task
context cannot be empty!" because 'DispatcherContext::addEvent' and
'DispatcherContext::tryAddEvent' methods are not receiving the "context"
parameter.
  • Loading branch information
luanluciano93 authored Sep 24, 2024
1 parent a399c26 commit a444901
Show file tree
Hide file tree
Showing 18 changed files with 247 additions and 167 deletions.
2 changes: 1 addition & 1 deletion src/canary_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ int CanaryServer::run() {

loaderStatus.notify_one();
},
"CanaryServer::run"
__FUNCTION__
);

loaderStatus.wait(LoaderStatus::LOADING);
Expand Down
10 changes: 6 additions & 4 deletions src/creatures/combat/condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2095,10 +2095,12 @@ bool ConditionFeared::executeCondition(std::shared_ptr<Creature> creature, int32
}

if (getFleePath(creature, currentPos, listDir)) {
g_dispatcher().addEvent([id = creature->getID(), listDir] {
g_game().forcePlayerAutoWalk(id, listDir);
},
"ConditionFeared::executeCondition");
g_dispatcher().addEvent(
[id = creature->getID(), listDir] {
g_game().forcePlayerAutoWalk(id, listDir);
},
__FUNCTION__
);

g_logger().debug("[ConditionFeared::executeCondition] Walking Scheduled");
}
Expand Down
44 changes: 26 additions & 18 deletions src/creatures/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,17 +251,23 @@ void Creature::addEventWalk(bool firstStep) {
return;
}

g_dispatcher().context().tryAddEvent([ticks, self = getCreature()]() {
// Take first step right away, but still queue the next
if (ticks == 1) {
g_game().checkCreatureWalk(self->getID());
}
g_dispatcher().context().tryAddEvent(
[ticks, self = getCreature()]() {
// Take first step right away, but still queue the next
if (ticks == 1) {
g_game().checkCreatureWalk(self->getID());
}

self->eventWalk = g_dispatcher().scheduleEvent(
static_cast<uint32_t>(ticks),
[creatureId = self->getID()] { g_game().checkCreatureWalk(creatureId); }, "Game::checkCreatureWalk"
);
});
self->eventWalk = g_dispatcher().scheduleEvent(
static_cast<uint32_t>(ticks),
[creatureId = self->getID()] {
g_game().checkCreatureWalk(creatureId);
},
"Game::checkCreatureWalk"
);
},
"Game::checkCreatureWalk"
);
}

void Creature::stopEventWalk() {
Expand Down Expand Up @@ -600,7 +606,7 @@ void Creature::onCreatureMove(const std::shared_ptr<Creature> &creature, const s
if (followCreature && (creature == getCreature() || creature == followCreature)) {
if (hasFollowPath) {
isUpdatingPath = true;
g_dispatcher().addEvent([creatureId = getID()] { g_game().updateCreatureWalk(creatureId); }, "Game::updateCreatureWalk");
g_dispatcher().addEvent([creatureId = getID()] { g_game().updateCreatureWalk(creatureId); }, __FUNCTION__);
}

if (newPos.z != oldPos.z || !canSee(followCreature->getPosition())) {
Expand All @@ -615,7 +621,7 @@ void Creature::onCreatureMove(const std::shared_ptr<Creature> &creature, const s
} else {
if (hasExtraSwing()) {
// our target is moving lets see if we can get in hit
g_dispatcher().addEvent([creatureId = getID()] { g_game().checkCreatureAttack(creatureId); }, "Game::checkCreatureAttack");
g_dispatcher().addEvent([creatureId = getID()] { g_game().checkCreatureAttack(creatureId); }, __FUNCTION__);
}

if (newTile->getZoneType() != oldTile->getZoneType()) {
Expand Down Expand Up @@ -825,10 +831,12 @@ bool Creature::dropCorpse(std::shared_ptr<Creature> lastHitCreature, std::shared
auto isReachable = g_game().map.getPathMatching(player->getPosition(), dirList, FrozenPathingConditionCall(corpse->getPosition()), fpp);

if (player->checkAutoLoot(monster->isRewardBoss()) && isReachable) {
g_dispatcher().addEvent([player, corpseContainer, corpsePosition = corpse->getPosition()] {
g_game().playerQuickLootCorpse(player, corpseContainer, corpsePosition);
},
"Game::playerQuickLootCorpse");
g_dispatcher().addEvent(
[player, corpseContainer, corpsePosition = corpse->getPosition()] {
g_game().playerQuickLootCorpse(player, corpseContainer, corpsePosition);
},
__FUNCTION__
);
}
}
}
Expand Down Expand Up @@ -872,7 +880,7 @@ void Creature::changeHealth(int32_t healthChange, bool sendHealthChange /* = tru
g_game().addCreatureHealth(static_self_cast<Creature>());
}
if (health <= 0) {
g_dispatcher().addEvent([creatureId = getID()] { g_game().executeDeath(creatureId); }, "Game::executeDeath");
g_dispatcher().addEvent([creatureId = getID()] { g_game().executeDeath(creatureId); }, __FUNCTION__);
}
}

Expand Down Expand Up @@ -1061,7 +1069,7 @@ void Creature::goToFollowCreature_async(std::function<void()> &&onComplete) {
});

if (onComplete) {
g_dispatcher().context().addEvent(std::move(onComplete));
g_dispatcher().context().addEvent(std::move(onComplete), __FUNCTION__);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/creatures/monsters/monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ bool Monster::selectTarget(const std::shared_ptr<Creature> &creature) {

if (isHostile() || isSummon()) {
if (setAttackedCreature(creature)) {
g_dispatcher().addEvent([creatureId = getID()] { g_game().checkCreatureAttack(creatureId); }, "Game::checkCreatureAttack");
g_dispatcher().addEvent([creatureId = getID()] { g_game().checkCreatureAttack(creatureId); }, __FUNCTION__);
}
}
return setFollowCreature(creature);
Expand Down
2 changes: 1 addition & 1 deletion src/creatures/monsters/spawns/spawn_monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ void SpawnMonster::startup(bool delayed) {
continue;
}
if (delayed) {
g_dispatcher().addEvent([this, spawnMonsterId, &sb, mType] { scheduleSpawn(spawnMonsterId, sb, mType, 0, true); }, "SpawnMonster::startup");
g_dispatcher().addEvent([this, spawnMonsterId, &sb, mType] { scheduleSpawn(spawnMonsterId, sb, mType, 0, true); }, __FUNCTION__);
} else {
scheduleSpawn(spawnMonsterId, sb, mType, 0, true);
}
Expand Down
14 changes: 7 additions & 7 deletions src/creatures/players/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1946,7 +1946,7 @@ void Player::onCreatureMove(const std::shared_ptr<Creature> &creature, const std
const auto &followCreature = getFollowCreature();
if (hasFollowPath && (creature == followCreature || (creature.get() == this && followCreature))) {
isUpdatingPath = false;
g_dispatcher().addEvent([creatureId = getID()] { g_game().updateCreatureWalk(creatureId); }, "Game::updateCreatureWalk");
g_dispatcher().addEvent([creatureId = getID()] { g_game().updateCreatureWalk(creatureId); }, __FUNCTION__);
}

if (creature != getPlayer()) {
Expand Down Expand Up @@ -4283,7 +4283,7 @@ bool Player::updateSaleShopList(std::shared_ptr<Item> item) {
return true;
}

g_dispatcher().addEvent([creatureId = getID()] { g_game().updatePlayerSaleItems(creatureId); }, "Game::updatePlayerSaleItems");
g_dispatcher().addEvent([creatureId = getID()] { g_game().updatePlayerSaleItems(creatureId); }, __FUNCTION__);
scheduledSaleUpdate = true;
return true;
}
Expand Down Expand Up @@ -4354,7 +4354,7 @@ bool Player::setAttackedCreature(std::shared_ptr<Creature> creature) {
}

if (creature) {
g_dispatcher().addEvent([creatureId = getID()] { g_game().checkCreatureAttack(creatureId); }, "Game::checkCreatureAttack");
g_dispatcher().addEvent([creatureId = getID()] { g_game().checkCreatureAttack(creatureId); }, __FUNCTION__);
}
return true;
}
Expand Down Expand Up @@ -4416,7 +4416,8 @@ void Player::doAttacking(uint32_t) {

const auto &task = createPlayerTask(
std::max<uint32_t>(SCHEDULER_MINTICKS, delay),
[playerId = getID()] { g_game().checkCreatureAttack(playerId); }, "Game::checkCreatureAttack"
[playerId = getID()] { g_game().checkCreatureAttack(playerId); },
__FUNCTION__
);

if (!classicSpeed) {
Expand Down Expand Up @@ -6785,7 +6786,7 @@ void Player::triggerTranscendance() {
player->sendBasicData();
}
},
"Player::triggerTranscendance"
__FUNCTION__
);
g_dispatcher().scheduleEvent(task);

Expand Down Expand Up @@ -7858,8 +7859,7 @@ bool Player::canAutoWalk(const Position &toPosition, const std::function<void()>
std::vector<Direction> listDir;
if (getPathTo(toPosition, listDir, 0, 1, true, true)) {
g_dispatcher().addEvent([creatureId = getID(), listDir] { g_game().playerAutoWalk(creatureId, listDir); }, __FUNCTION__);

std::shared_ptr<Task> task = createPlayerTask(delay, function, __FUNCTION__);
const auto &task = createPlayerTask(delay, function, __FUNCTION__);
setNextWalkActionTask(task);
return true;
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/database/databasetasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void DatabaseTasks::execute(const std::string &query, std::function<void(DBResul
threadPool.detach_task([this, query, callback]() {
bool success = db.executeQuery(query);
if (callback != nullptr) {
g_dispatcher().addEvent([callback, success]() { callback(nullptr, success); }, "DatabaseTasks::execute");
g_dispatcher().addEvent([callback, success]() { callback(nullptr, success); }, __FUNCTION__);
}
});
}
Expand All @@ -35,7 +35,7 @@ void DatabaseTasks::store(const std::string &query, std::function<void(DBResult_
threadPool.detach_task([this, query, callback]() {
DBResult_ptr result = db.storeQuery(query);
if (callback != nullptr) {
g_dispatcher().addEvent([callback, result]() { callback(result, true); }, "DatabaseTasks::store");
g_dispatcher().addEvent([callback, result]() { callback(result, true); }, __FUNCTION__);
}
});
}
Loading

0 comments on commit a444901

Please sign in to comment.