Fix crash in LoadMerch by clearing/placing refs in main thread

This commit is contained in:
Tyler Hallada 2020-10-16 20:19:48 -04:00
parent b3dd9e240d
commit 404f2d9ae5

View File

@ -109,19 +109,21 @@ bool LoadMerchandiseImpl(
return false;
}
// Since this method is running asyncronously in a thread, set up a callback on the trigger ref that will receive an event with the result
SKSE::RegistrationMap<bool> regMap = SKSE::RegistrationMap<bool>();
regMap.Register(toggle_ref, RE::BSFixedString("OnLoadMerchandise"));
RE::TESObjectREFR * merchant_chest = merchant_shelf->GetLinkedRef(chest_keyword);
if (!merchant_chest) {
logger::error("LoadMerchandise merchant_chest is null!");
regMap.SendEvent(false);
regMap.Unregister(toggle_ref);
return false;
}
FFIResult<MerchRecordVec> result = get_merchandise_list(api_url.c_str(), api_key.c_str(), merchandise_list_id);
// Placing the refs must be done on the main thread otherwise disabling & deleting refs in ClearMerchandiseImpl causes a crash
auto task = SKSE::GetTaskInterface();
task->AddTask([result, merchant_chest, merchant_shelf, placeholder_static, shelf_keyword, item_keyword, prev_keyword, next_keyword, page, toggle_ref, cell, data_handler, a_vm, extra_linked_ref_vtbl, MoveTo_Native, PlaceAtMe_Native]() {
// Since this method is running asyncronously in a thread, set up a callback on the trigger ref that will receive an event with the result
SKSE::RegistrationMap<bool> regMap = SKSE::RegistrationMap<bool>();
regMap.Register(toggle_ref, RE::BSFixedString("OnLoadMerchandise"));
if (result.IsOk()) {
logger::info("LoadMerchandise get_merchandise_list result OK");
MerchRecordVec vec = result.AsOk();
@ -135,6 +137,7 @@ bool LoadMerchandiseImpl(
return true;
}
ClearMerchandiseImpl(merchant_chest, merchant_shelf, placeholder_static, shelf_keyword, item_keyword);
logger::info(FMT_STRING("LoadMerchandise current shelf page is: {:d}"), merchant_shelf->extraList.GetCount());
@ -356,13 +359,13 @@ bool LoadMerchandiseImpl(
next_ref->SetDisplayName("(No next page)", true);
}
else {
next_ref->SetDisplayName(fmt::format("Advance to page %d", page + 1).c_str(), true);
next_ref->SetDisplayName(fmt::format("Advance to page {:d}", page + 1).c_str(), true);
}
if (page == 1) {
prev_ref->SetDisplayName("(No previous page)", true);
}
else {
prev_ref->SetDisplayName(fmt::format("Back to page %d", page - 1).c_str(), true);
prev_ref->SetDisplayName(fmt::format("Back to page {:d}", page - 1).c_str(), true);
}
// auto messaging = SKSE::GetModCallbackEventSource();
// const SKSE::ModCallbackEvent event = { RE::BSFixedString("BazaarRealm_LoadMerchandiseDone"), RE::BSFixedString(""), 0, nullptr };
@ -376,9 +379,9 @@ bool LoadMerchandiseImpl(
regMap.Unregister(toggle_ref);
return false;
}
regMap.SendEvent(true);
regMap.Unregister(toggle_ref);
});
return true;
}