Skip to content

Commit

Permalink
Reversed linked items tree that is logged after lookup.
Browse files Browse the repository at this point in the history
  • Loading branch information
adya committed Mar 24, 2024
1 parent 1926498 commit a9840f0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 35 deletions.
15 changes: 9 additions & 6 deletions SPID/include/LinkedDistribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ namespace LinkedDistribution
/// <param name="rawLinkedDistribution">A raw linked item entries that should be processed.</param>
void LookupLinkedItems(RE::TESDataHandler* const dataHandler, INI::LinkedItemsVec& rawLinkedItems = INI::linkedItems);

void LogLinkedItemsLookup();

/// <summary>
/// Calculates DistributionSet for each linked form and calls a callback for each of them.
/// </summary>
Expand All @@ -72,12 +74,6 @@ namespace LinkedDistribution
/// <param name="callback">A callback to be called with each DistributionSet. This is supposed to do the actual distribution.</param>
void ForEachLinkedDistributionSet(const std::set<RE::TESForm*>& linkedForms, std::function<void(DistributionSet&)> callback);

/// <summary>
/// Iterates over each type of LinkedForms and calls a callback with each of them.
/// </summary>
template <typename Func, typename... Args>
void ForEachLinkedForms(Func&& func, const Args&&... args);

private:
template <class Form>
DataVec<Form>& LinkedFormsForForm(RE::TESForm* form, LinkedForms<Form>& linkedForms) const;
Expand All @@ -92,6 +88,13 @@ namespace LinkedDistribution
LinkedForms<RE::BGSKeyword> keywords{ RECORD::kKeyword };
LinkedForms<RE::TESFaction> factions{ RECORD::kFaction };
LinkedForms<RE::TESObjectARMO> skins{ RECORD::kSkin };

/// <summary>
/// Iterates over each type of LinkedForms and calls a callback with each of them.
/// </summary>
template <typename Func, typename... Args>
void ForEachLinkedForms(Func&& func, const Args&&... args);

};

#pragma region Implementation
Expand Down
44 changes: 44 additions & 0 deletions SPID/src/LinkedDistribution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,50 @@ namespace LinkedDistribution
ForEachLinkedForms([&]<typename Form>(LinkedForms<Form>& forms) {
std::erase_if(forms.forms, [](const auto& pair) { return pair.second.empty(); });
});

// Clear INI once lookup is done
rawLinkedItems.clear();

// Clear logger's buffer to free some memory :)
buffered_logger::clear();
}

void Manager::LogLinkedItemsLookup()
{
logger::info("{:*^50}", "LINKED ITEMS");

ForEachLinkedForms([]<typename Form>(const LinkedForms<Form>& linkedForms) {
if (linkedForms.GetForms().empty()) {
return;
}

std::unordered_map<RE::TESForm*, std::vector<RE::TESForm*>> map{};

// Iterate through the original map
for (const auto& pair : linkedForms.GetForms()) {
const auto key = pair.first;
const DataVec<Form>& values = pair.second;

for (const auto& value : values) {
map[value.form].push_back(key);
}
}

const auto& recordName = RECORD::add[linkedForms.GetType()];
logger::info("Linked {}s: ", recordName);

for (const auto& [form, linkedItems] : map) {
logger::info("\t{}", describe(form));

const auto lastItemIndex = linkedItems.size() - 1;
for (int i = 0; i < lastItemIndex; ++i) {
const auto& linkedItem = linkedItems[i];
logger::info("\t├─── {}", describe(linkedItem));
}
const auto& lastLinkedItem = linkedItems[lastItemIndex];
logger::info("\t└─── {}", describe(lastLinkedItem));
}
});
}

void Manager::ForEachLinkedDistributionSet(const std::set<RE::TESForm*>& targetForms, std::function<void(DistributionSet&)> performDistribution)
Expand Down
30 changes: 1 addition & 29 deletions SPID/src/LookupForms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,35 +85,7 @@ void LookupLinkedItems(RE::TESDataHandler* const dataHandler)

void LogLinkedItemsLookup()
{
using namespace LinkedDistribution;

logger::info("{:*^50}", "LINKED ITEMS");

Manager::GetSingleton()->ForEachLinkedForms([]<typename Form>(const LinkedForms<Form>& linkedForms) {
if (linkedForms.GetForms().empty()) {
return;
}
const auto& recordName = RECORD::add[linkedForms.GetType()];
logger::info("Linked {}s: ", recordName);

for (const auto& [form, linkedItems] : linkedForms.GetForms()) {
logger::info("\t{}", describe(form));

const auto lastItemIndex = linkedItems.size() - 1;
for (int i = 0; i < lastItemIndex; ++i) {
const auto& linkedItem = linkedItems[i];
logger::info("\t├─── {}", describe(linkedItem.form));
}
const auto& lastLinkedItem = linkedItems[lastItemIndex];
logger::info("\t└─── {}", describe(lastLinkedItem.form));
}
});

// Clear INI once lookup is done
LinkedDistribution::INI::linkedItems.clear();

// Clear logger's buffer to free some memory :)
buffered_logger::clear();
LinkedDistribution::Manager::GetSingleton()->LogLinkedItemsLookup();
}

bool Lookup::LookupForms()
Expand Down

0 comments on commit a9840f0

Please sign in to comment.