Skip to content

Commit

Permalink
some new debug
Browse files Browse the repository at this point in the history
  • Loading branch information
nic11 committed Jan 15, 2025
1 parent 94a0d16 commit 5a977ac
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 11 deletions.
36 changes: 30 additions & 6 deletions skymp5-server/cpp/server_guest_lib/EvaluateTemplate.h
Original file line number Diff line number Diff line change
@@ -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 <exception>
#include <sstream>
#include <stdexcept>
#include <vector>

// template <class Callback>
// auto CallbackWrapper(const Callback& callback, const espm::LookupResult& npcLookupResult, const espm::NPC_::Data& npcData, std::string detailedLog) {
// if constexpr (std::tuple_size<typename std::decay<Callback>::type>::value == 2) {
// return callback(npcLookupResult, npcData);
// } else {
// return callback(npcLookupResult, npcData, std::move(detailedLog));
// }
// }

template <uint16_t TemplateFlag, class Callback>
auto EvaluateTemplate(WorldState* worldState, uint32_t baseId,
const std::vector<FormDesc>& templateChain,
const Callback& callback)
{
ANTIGO_CONTEXT_INIT(ctx);

const std::vector<FormDesc> chainDefault = { FormDesc::FromFormId(
baseId, worldState->espmFiles) };
const std::vector<FormDesc>& chain =
Expand Down Expand Up @@ -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";
}
}
}

Expand Down
32 changes: 27 additions & 5 deletions skymp5-server/cpp/server_guest_lib/MpObjectReference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,6 +35,7 @@
#include <map>
#include <numeric>
#include <optional>
#include <stdexcept>

#include "OpenContainerMessage.h"
#include "TeleportMessage.h"
Expand Down Expand Up @@ -1707,17 +1710,24 @@ 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() })) {

if (std::count(scriptNames.begin(), scriptNames.end(),
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();
}
}
}

Expand Down Expand Up @@ -1822,6 +1832,8 @@ std::vector<espm::CONT::ContainerObject> GetOutfitObjects(
WorldState* worldState, const std::vector<FormDesc>& templateChain,
const espm::LookupResult& lookupRes)
{
ANTIGO_CONTEXT_INIT(ctx);

auto& compressedFieldsCache = worldState->GetEspmCache();

std::vector<espm::CONT::ContainerObject> res;
Expand All @@ -1830,8 +1842,14 @@ std::vector<espm::CONT::ContainerObject> GetOutfitObjects(
auto baseId = lookupRes.ToGlobalId(lookupRes.rec->GetId());
auto outfitId = EvaluateTemplate<espm::NPC_::UseInventory>(
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 =
Expand Down Expand Up @@ -1892,6 +1910,8 @@ void MpObjectReference::AddContainerObject(

void MpObjectReference::EnsureBaseContainerAdded(espm::Loader& espm)
{
ANTIGO_CONTEXT_INIT(ctx);

if (ChangeForm().baseContainerAdded) {
return;
}
Expand All @@ -1901,6 +1921,8 @@ void MpObjectReference::EnsureBaseContainerAdded(espm::Loader& espm)
return;
}

ctx.AddMessage("next: AsActor(), templateChain, lookupRes");

auto actor = AsActor();
const std::vector<FormDesc> kEmptyTemplateChain;
const std::vector<FormDesc>& templateChain =
Expand Down
2 changes: 2 additions & 0 deletions skymp5-server/cpp/server_guest_lib/PartOne.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ void PartOne::AttachEspm(espm::Loader* espm)

void PartOne::AttachSaveStorage(std::shared_ptr<ISaveStorage> saveStorage)
{
ANTIGO_CONTEXT_INIT(ctx);

worldState.AttachSaveStorage(saveStorage);

auto start = std::chrono::steady_clock::now();
Expand Down
1 change: 1 addition & 0 deletions skymp5-server/cpp/server_guest_lib/WorldState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ void WorldState::AddForm(std::unique_ptr<MpForm> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "MongoDatabase.h"

#include "JsonUtils.h"
#include "antigo/Context.h"

#ifndef NO_MONGO
# include <bsoncxx/builder/stream/document.hpp>
Expand Down Expand Up @@ -106,6 +107,8 @@ std::vector<std::optional<MpChangeForm>>&& MongoDatabase::UpsertImpl(

void MongoDatabase::Iterate(const IterateCallback& iterateCallback)
{
ANTIGO_CONTEXT_INIT(ctx);

constexpr int kBatchSize = 1001;
mongocxx::options::find findOptions;
findOptions.batch_size(kBatchSize);
Expand Down

0 comments on commit 5a977ac

Please sign in to comment.