1

Always run SyncCellStorage as a task

This commit is contained in:
Eddoursul 2022-07-16 18:19:30 +02:00
parent 1fedd07be1
commit deaac1c723
3 changed files with 88 additions and 85 deletions

Binary file not shown.

View File

@ -269,9 +269,7 @@ namespace ArtifactTracker
g_bBookShelf = false;
std::thread([]() {
std::this_thread::sleep_for(std::chrono::milliseconds(1200));
SKSE::GetTaskInterface()->AddTask([&]() {
ArtifactTracker::SyncCellStorage();
});
ArtifactTracker::SyncCellStorage();
}).detach();
} else {
SyncCellStorage();
@ -377,8 +375,8 @@ namespace ArtifactTracker
log::info("Adding new storage in {}", cell->GetName());
#endif
SKSE::GetTaskInterface()->AddTask([&]() {
cellStorage = RE::PlayerCharacter::GetSingleton()->PlaceObjectAtMe(g_cellContainer, true).get();
SKSE::GetTaskInterface()->AddTask([]() {
const auto cellStorage = RE::PlayerCharacter::GetSingleton()->PlaceObjectAtMe(g_cellContainer, true).get();
if (cellStorage) {
cellStorage->Disable();
@ -406,83 +404,86 @@ namespace ArtifactTracker
log::info("Running SyncCellStorage");
#endif
std::unordered_set<RE::FormID> cellItems;
const RE::FormID ignoreFormID = a_ignoreRef ? a_ignoreRef->formID : NULL;
const auto cell = g_cellStorage->GetParentCell();
const auto inv = g_cellStorage->GetInventory();
SKSE::GetTaskInterface()->AddTask([ignoreFormID]() {
std::unordered_set<RE::FormID> cellItems;
cell->ForEachReference([&](RE::TESObjectREFR& a_ref) {
if (a_ignoreRef && a_ignoreRef->formID == a_ref.formID) {
return true;
}
const auto cell = g_cellStorage->GetParentCell();
const auto inv = g_cellStorage->GetInventory();
const auto baseObj = a_ref.GetBaseObject();
if (IsValidContainer(&a_ref)) {
if (g_cellContainer == baseObj || baseObj->formID == 0xDC9E7 || g_persistentMap.contains(a_ref.formID)) { // skip persistent and PlayerBookShelfContainer
cell->ForEachReference([&](RE::TESObjectREFR& a_ref) {
if (ignoreFormID && ignoreFormID == a_ref.formID) {
return true;
}
const auto contInv = a_ref.GetInventory([&](RE::TESBoundObject& a_object) -> bool {
return !cellItems.contains(a_object.formID) && IsArtifact(&a_object);
});
const auto baseObj = a_ref.GetBaseObject();
for (const auto& [item, data] : contInv) {
if (data.first > 0) {
cellItems.insert(item->formID);
if (inv.find(item) == inv.end()) {
g_cellStorage->AddObjectToContainer(item, nullptr, 1, nullptr);
if (IsValidContainer(&a_ref)) {
if (g_cellContainer == baseObj || baseObj->formID == 0xDC9E7 || g_persistentMap.contains(a_ref.formID)) { // skip persistent and PlayerBookShelfContainer
return true;
}
const auto contInv = a_ref.GetInventory([&](RE::TESBoundObject& a_object) -> bool {
return !cellItems.contains(a_object.formID) && IsArtifact(&a_object);
});
for (const auto& [item, data] : contInv) {
if (data.first > 0) {
cellItems.insert(item->formID);
if (inv.find(item) == inv.end()) {
g_cellStorage->AddObjectToContainer(item, nullptr, 1, nullptr);
}
if (!g_listStored->HasForm(item)) {
ListRemoveItem(g_listNew, item);
ListRemoveItem(g_listFound, item);
g_listStored->AddForm(item);
}
}
if (!g_listStored->HasForm(item)) {
}
return true;
}
if (a_ref.IsDisabled() || a_ref.IsMarkedForDeletion() || cellItems.contains(baseObj->formID) || !IsArtifact(baseObj)) {
return true;
}
cellItems.insert(baseObj->formID);
if (inv.find(baseObj) == inv.end()) {
g_cellStorage->AddObjectToContainer(baseObj, nullptr, 1, nullptr);
}
if (!g_listStored->HasForm(baseObj)) {
ListRemoveItem(g_listNew, baseObj);
ListRemoveItem(g_listFound, baseObj);
g_listStored->AddForm(baseObj);
}
return true;
});
for (const auto& [item, data] : inv) {
const auto& [count, entry] = data;
if (count > 0 && !cellItems.contains(item->formID)) {
g_cellStorage->RemoveItem(item, count, RE::ITEM_REMOVE_REASON::kRemove, nullptr, nullptr);
if (!RefListHasItem(g_persistentStorage, item->formID)) {
ListRemoveItem(g_listStored, item);
if (GetItemCount(RE::PlayerCharacter::GetSingleton(), item) || FollowersHaveItem(item)) {
ListRemoveItem(g_listNew, item);
g_listFound->AddForm(item);
} else {
ListRemoveItem(g_listFound, item);
g_listStored->AddForm(item);
g_listNew->AddForm(item);
}
}
}
return true;
}
if (a_ref.IsDisabled() || a_ref.IsMarkedForDeletion() || cellItems.contains(baseObj->formID) || !IsArtifact(baseObj)) {
return true;
}
cellItems.insert(baseObj->formID);
if (inv.find(baseObj) == inv.end()) {
g_cellStorage->AddObjectToContainer(baseObj, nullptr, 1, nullptr);
}
if (!g_listStored->HasForm(baseObj)) {
ListRemoveItem(g_listNew, baseObj);
ListRemoveItem(g_listFound, baseObj);
g_listStored->AddForm(baseObj);
}
return true;
cellItems.clear();
});
for (const auto& [item, data] : inv) {
const auto& [count, entry] = data;
if (count > 0 && !cellItems.contains(item->formID)) {
g_cellStorage->RemoveItem(item, count, RE::ITEM_REMOVE_REASON::kRemove, nullptr, nullptr);
if (!RefListHasItem(g_persistentStorage, item->formID)) {
ListRemoveItem(g_listStored, item);
if (GetItemCount(RE::PlayerCharacter::GetSingleton(), item) || FollowersHaveItem(item)) {
ListRemoveItem(g_listNew, item);
g_listFound->AddForm(item);
} else {
ListRemoveItem(g_listFound, item);
g_listNew->AddForm(item);
}
}
}
}
cellItems.clear();
}
void OnContainerChanged(const RE::TESContainerChangedEvent* a_event, RE::TESForm* form)
@ -759,7 +760,7 @@ namespace ArtifactTracker
g_iFollowerIndex = iCurrentFollowers;
std::thread([]() {
std::this_thread::sleep_for(std::chrono::milliseconds(3000)); // wait for followers to load into the new cell
SKSE::GetTaskInterface()->AddTask([&]() {
SKSE::GetTaskInterface()->AddTask([]() {
RescanFoundArtifacts();
});
}).detach();

View File

@ -84,22 +84,26 @@ auto EventListener::ProcessEvent(
SKSE::log::info("MenuOpenCloseEvent");
#endif
if (a_event->opening) {
RE::BSInputDeviceManager::GetSingleton()->AddEventSink(EventListener::GetSingleton());
} else {
RE::BSInputDeviceManager::GetSingleton()->RemoveEventSink(EventListener::GetSingleton());
if (ArtifactTracker::g_bTakeAll && ArtifactTracker::g_bNotifyNewArtifact && ArtifactTracker::g_bTakeAllCount > 0) {
//RE::DebugNotification(fmt::format("{} new artifact(s) acquired", ArtifactTracker::g_bTakeAllCount).c_str());
RE::BSTSmartPointer<RE::BSScript::IStackCallbackFunctor> stackCallback;
RE::BSScript::Internal::VirtualMachine::GetSingleton()->DispatchStaticCall("ETR_NewArtifactsCombinedNotification", "Show", RE::MakeFunctionArguments<std::uint32_t>(std::move(ArtifactTracker::g_bTakeAllCount)), stackCallback);
}
ArtifactTracker::g_bTakeAll = false;
ArtifactTracker::g_bTakeAllCount = 0;
}
bool bOpening = a_event->opening;
if (ArtifactTracker::IsHome()) {
ArtifactTracker::SetContainerMode(a_event->opening);
}
SKSE::GetTaskInterface()->AddTask([bOpening]() {
if (bOpening) {
RE::BSInputDeviceManager::GetSingleton()->AddEventSink(EventListener::GetSingleton());
} else {
RE::BSInputDeviceManager::GetSingleton()->RemoveEventSink(EventListener::GetSingleton());
if (ArtifactTracker::g_bTakeAll && ArtifactTracker::g_bNotifyNewArtifact && ArtifactTracker::g_bTakeAllCount > 0) {
//RE::DebugNotification(fmt::format("{} new artifact(s) acquired", ArtifactTracker::g_bTakeAllCount).c_str());
RE::BSTSmartPointer<RE::BSScript::IStackCallbackFunctor> stackCallback;
RE::BSScript::Internal::VirtualMachine::GetSingleton()->DispatchStaticCall("ETR_NewArtifactsCombinedNotification", "Show", RE::MakeFunctionArguments<std::uint32_t>(std::move(ArtifactTracker::g_bTakeAllCount)), stackCallback);
}
ArtifactTracker::g_bTakeAll = false;
ArtifactTracker::g_bTakeAllCount = 0;
}
if (ArtifactTracker::IsHome()) {
ArtifactTracker::SetContainerMode(bOpening);
}
});
}
return RE::BSEventNotifyControl::kContinue;
@ -116,9 +120,7 @@ auto EventListener::ProcessEvent(
if (ref && ArtifactTracker::IsArtifact(ref->GetBaseObject())) {
std::thread([]() {
std::this_thread::sleep_for(std::chrono::milliseconds(200));
SKSE::GetTaskInterface()->AddTask([&]() {
ArtifactTracker::SyncCellStorage();
});
ArtifactTracker::SyncCellStorage();
}).detach();
}
}