From d4344adadd8732ae156e030bbcc14ffcbdffd526 Mon Sep 17 00:00:00 2001 From: Eduardo Dantas Date: Fri, 8 Nov 2024 21:46:27 -0300 Subject: [PATCH] fix: compilation and crash with async save --- .../players/components/player_storage.cpp | 2 +- src/game/scheduling/save_manager.cpp | 26 ++++++++++--------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/creatures/players/components/player_storage.cpp b/src/creatures/players/components/player_storage.cpp index 392dda58da3..ec78e81d995 100644 --- a/src/creatures/players/components/player_storage.cpp +++ b/src/creatures/players/components/player_storage.cpp @@ -126,7 +126,7 @@ bool PlayerStorage::save() { return true; }; - auto insertModifiedStorageKeys = [playerGUID, playerName, modifiedKeys, storageMap]() mutable { + auto insertModifiedStorageKeys = [this, playerGUID, playerName, modifiedKeys, storageMap]() mutable { getReservedRange(); if (!modifiedKeys.empty()) { DBInsert storageQuery("INSERT INTO `player_storage` (`player_id`, `key`, `value`) VALUES "); diff --git a/src/game/scheduling/save_manager.cpp b/src/game/scheduling/save_manager.cpp index 4e757ff5c22..ab8abbbfae8 100644 --- a/src/game/scheduling/save_manager.cpp +++ b/src/game/scheduling/save_manager.cpp @@ -16,7 +16,7 @@ #include "io/iologindata.hpp" #include "kv/kv.hpp" #include "lib/di/container.hpp" -#include "game/scheduling/task.hpp" +#include "game/scheduling/dispatcher.hpp" SaveManager::SaveManager(ThreadPool &threadPool, KVStore &kvStore, Logger &logger, Game &game) : threadPool(threadPool), kv(kvStore), logger(logger), game(game) { @@ -34,18 +34,19 @@ void SaveManager::saveAll() { Benchmark bm_saveAll; logger.info("Saving server..."); - Benchmark bm_players; const auto async = g_configManager().getBoolean(TOGGLE_SAVE_ASYNC); + Benchmark bm_players; + const auto &players = std::vector>>(game.getPlayers().begin(), game.getPlayers().end()); logger.info("Saving {} players... (Async: {})", players.size(), async ? "Enabled" : "Disabled"); - threadPool.submit_loop(static_cast(0), static_cast(players.size()), [this, &players](const int i) { - const auto &player = players[i].second; - player->loginPosition = player->getPosition(); - doSavePlayer(player); - }) - .wait(); + g_dispatcher().asyncWait(players.size(), [this, &players](size_t i) { + if(const auto& player = players[i].second) { + player->loginPosition = player->getPosition(); + doSavePlayer(player); + } + }); double duration_players = bm_players.duration(); if (duration_players > 1000.0) { @@ -56,10 +57,11 @@ void SaveManager::saveAll() { Benchmark bm_guilds; const auto &guilds = std::vector>>(game.getGuilds().begin(), game.getGuilds().end()); - threadPool.submit_loop(static_cast(0), static_cast(guilds.size()), [this, &guilds](const int i) { - saveGuild(guilds[i].second); - }) - .wait(); + g_dispatcher().asyncWait(guilds.size(), [this, &guilds](size_t i) { + if (const auto& guild = guilds[i].second) { + saveGuild(guild); + } + }); double duration_guilds = bm_guilds.duration(); if (duration_guilds > 1000.0) {