diff --git a/skymp5-server/cpp/server_guest_lib/EvaluateTemplate.h b/skymp5-server/cpp/server_guest_lib/EvaluateTemplate.h index 6c71ac3ad6..1912ac1be7 100644 --- a/skymp5-server/cpp/server_guest_lib/EvaluateTemplate.h +++ b/skymp5-server/cpp/server_guest_lib/EvaluateTemplate.h @@ -1,16 +1,33 @@ #pragma once #include "FormDesc.h" #include "WorldState.h" +#include "antigo/Context.h" +#include "antigo/ExecutionData.h" +#include "antigo/ResolvedContext.h" +#include "libespm/LookupResult.h" +#include "libespm/NPC_.h" #include "libespm/espm.h" +#include #include #include #include +// template +// auto CallbackWrapper(const Callback& callback, const espm::LookupResult& npcLookupResult, const espm::NPC_::Data& npcData, std::string detailedLog) { +// if constexpr (std::tuple_size::type>::value == 2) { +// return callback(npcLookupResult, npcData); +// } else { +// return callback(npcLookupResult, npcData, std::move(detailedLog)); +// } +// } + template auto EvaluateTemplate(WorldState* worldState, uint32_t baseId, const std::vector& templateChain, const Callback& callback) { + ANTIGO_CONTEXT_INIT(ctx); + const std::vector chainDefault = { FormDesc::FromFormId( baseId, worldState->espmFiles) }; const std::vector& chain = @@ -40,12 +57,19 @@ auto EvaluateTemplate(WorldState* worldState, uint32_t baseId, detailedLog << "Variable npcData: baseTemplate=" << npcData.baseTemplate << ", templateDataFlags=" << npcData.templateDataFlags << "\n"; - if (npcData.baseTemplate == 0) { - return callback(npcLookupResult, npcData); - } - - if (!(npcData.templateDataFlags & TemplateFlag)) { - return callback(npcLookupResult, npcData); + try { + if (npcData.baseTemplate == 0) { + return callback(npcLookupResult, npcData); + } + + if (!(npcData.templateDataFlags & TemplateFlag)) { + return callback(npcLookupResult, npcData); + } + } catch (const std::exception& e) { + detailedLog << "Callback failed: " << e.what() << "\n"; + while (Antigo::HasExceptionWitness()) { + detailedLog << Antigo::PopExceptionWitness().ToString() << "\n"; + } } } diff --git a/skymp5-server/cpp/server_guest_lib/MpObjectReference.cpp b/skymp5-server/cpp/server_guest_lib/MpObjectReference.cpp index 2aa405682f..355033c1ef 100644 --- a/skymp5-server/cpp/server_guest_lib/MpObjectReference.cpp +++ b/skymp5-server/cpp/server_guest_lib/MpObjectReference.cpp @@ -24,6 +24,8 @@ #include "libespm/CompressedFieldsCache.h" #include "libespm/Convert.h" #include "libespm/GroupUtils.h" +#include "libespm/LookupResult.h" +#include "libespm/NPC_.h" #include "libespm/Utils.h" #include "papyrus-vm/Reader.h" #include "papyrus-vm/Utils.h" // stricmp @@ -33,6 +35,7 @@ #include #include #include +#include #include "OpenContainerMessage.h" #include "TeleportMessage.h" @@ -1707,6 +1710,10 @@ void MpObjectReference::InitScripts() auto& scriptsInStorage = GetParent()->GetScriptStorage()->ListScripts(false); for (auto& script : scriptData->scripts) { + ANTIGO_CONTEXT_INIT(ctx); + auto g = ctx.AddLambdaWithRef([&script]() { return script.scriptName; }); + g.Arm(); + if (scriptsInStorage.count( { script.scriptName.begin(), script.scriptName.end() })) { @@ -1714,10 +1721,13 @@ void MpObjectReference::InitScripts() script.scriptName) == 0) { scriptNames.push_back(script.scriptName); } - } else if (auto wst = GetParent()) - wst->logger->warn("Script '{}' not found in the script storage", - script.scriptName); + } else { + if (auto wst = GetParent()) { + wst->logger->warn("Script '{}' not found in the script storage", + script.scriptName); + } ctx.Resolve().Print(); + } } } @@ -1822,6 +1832,8 @@ std::vector GetOutfitObjects( WorldState* worldState, const std::vector& templateChain, const espm::LookupResult& lookupRes) { + ANTIGO_CONTEXT_INIT(ctx); + auto& compressedFieldsCache = worldState->GetEspmCache(); std::vector res; @@ -1830,8 +1842,14 @@ std::vector GetOutfitObjects( auto baseId = lookupRes.ToGlobalId(lookupRes.rec->GetId()); auto outfitId = EvaluateTemplate( worldState, baseId, templateChain, - [](const auto& npcLookupRes, const auto& npcData) { - return npcLookupRes.ToGlobalId(npcData.defaultOutfitId); + [](const espm::LookupResult& npcLookupRes, const espm::NPC_::Data& npcData) { + ANTIGO_CONTEXT_INIT(ctx); + auto result = npcLookupRes.ToGlobalId(npcData.defaultOutfitId); + if (result == 0) { + ctx.AddMessage("evaluated NPC template - outfitId is 0"); + throw std::runtime_error("failed NPC template eval"); // this will make context log everything + } + return result; }); auto outfitLookupRes = @@ -1892,6 +1910,8 @@ void MpObjectReference::AddContainerObject( void MpObjectReference::EnsureBaseContainerAdded(espm::Loader& espm) { + ANTIGO_CONTEXT_INIT(ctx); + if (ChangeForm().baseContainerAdded) { return; } @@ -1901,6 +1921,8 @@ void MpObjectReference::EnsureBaseContainerAdded(espm::Loader& espm) return; } + ctx.AddMessage("next: AsActor(), templateChain, lookupRes"); + auto actor = AsActor(); const std::vector kEmptyTemplateChain; const std::vector& templateChain = diff --git a/skymp5-server/cpp/server_guest_lib/PartOne.cpp b/skymp5-server/cpp/server_guest_lib/PartOne.cpp index 78fcf65c33..e2a575865f 100644 --- a/skymp5-server/cpp/server_guest_lib/PartOne.cpp +++ b/skymp5-server/cpp/server_guest_lib/PartOne.cpp @@ -284,6 +284,8 @@ void PartOne::AttachEspm(espm::Loader* espm) void PartOne::AttachSaveStorage(std::shared_ptr saveStorage) { + ANTIGO_CONTEXT_INIT(ctx); + worldState.AttachSaveStorage(saveStorage); auto start = std::chrono::steady_clock::now(); diff --git a/skymp5-server/cpp/server_guest_lib/WorldState.cpp b/skymp5-server/cpp/server_guest_lib/WorldState.cpp index d2a1354c2a..442d791b59 100644 --- a/skymp5-server/cpp/server_guest_lib/WorldState.cpp +++ b/skymp5-server/cpp/server_guest_lib/WorldState.cpp @@ -101,6 +101,7 @@ void WorldState::AddForm(std::unique_ptr form, uint32_t formId, const MpChangeForm* optionalChangeFormToApply) { ANTIGO_CONTEXT_INIT(ctx); + ctx.AddMessage("next: formId, skipChecks, optionalChangeFormToApply"); ctx.AddUnsigned(formId); ctx.AddUnsigned(skipChecks); ctx.AddPtr(optionalChangeFormToApply); diff --git a/skymp5-server/cpp/server_guest_lib/database_drivers/MongoDatabase.cpp b/skymp5-server/cpp/server_guest_lib/database_drivers/MongoDatabase.cpp index b067d07da4..4b3f8b7b55 100644 --- a/skymp5-server/cpp/server_guest_lib/database_drivers/MongoDatabase.cpp +++ b/skymp5-server/cpp/server_guest_lib/database_drivers/MongoDatabase.cpp @@ -1,6 +1,7 @@ #include "MongoDatabase.h" #include "JsonUtils.h" +#include "antigo/Context.h" #ifndef NO_MONGO # include @@ -106,6 +107,8 @@ std::vector>&& MongoDatabase::UpsertImpl( void MongoDatabase::Iterate(const IterateCallback& iterateCallback) { + ANTIGO_CONTEXT_INIT(ctx); + constexpr int kBatchSize = 1001; mongocxx::options::find findOptions; findOptions.batch_size(kBatchSize);