Skip to content

Commit

Permalink
fix(skyrim-platform): fix potential magicEffectApply crash (skyrim-mu…
Browse files Browse the repository at this point in the history
  • Loading branch information
Pospelove authored May 3, 2024
1 parent f5ffd31 commit 4869870
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
1 change: 1 addition & 0 deletions docs/release/dev/sp-fix-magicEffectApply-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed potential crash when loading a save with at least one active `magicEffectApply` listener.
38 changes: 29 additions & 9 deletions skyrim-platform/src/platform_se/skyrim_platform/EventHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -616,19 +616,39 @@ EventResult EventHandler::ProcessEvent(
return EventResult::kContinue;
}

auto e = CopyEventPtr(event);
uint32_t casterId =
event->caster.get() ? event->caster.get()->GetFormID() : 0;
uint32_t targetId =
event->target.get() ? event->target.get()->GetFormID() : 0;
uint32_t effectId = event->magicEffect;

SkyrimPlatform::GetSingleton()->AddUpdateTask([e] {
auto obj = JsValue::Object();
SkyrimPlatform::GetSingleton()->AddUpdateTask(
[casterId, targetId, effectId] {
auto obj = JsValue::Object();

auto effect = RE::TESForm::LookupByID(e->magicEffect);
auto effect = RE::TESForm::LookupByID(effectId);

AddObjProperty(&obj, "effect", effect, "MagicEffect");
AddObjProperty(&obj, "caster", e->caster.get(), "ObjectReference");
AddObjProperty(&obj, "target", e->target.get(), "ObjectReference");
auto casterRefr = RE::TESForm::LookupByID<RE::TESObjectREFR>(casterId);
auto targetRefr = RE::TESForm::LookupByID<RE::TESObjectREFR>(targetId);

SendEvent("magicEffectApply", obj);
});
if (!effect) {
return;
}

if (!casterRefr && casterId != 0) {
return;
}

if (!targetRefr && targetId != 0) {
return;
}

AddObjProperty(&obj, "effect", effect, "MagicEffect");
AddObjProperty(&obj, "caster", casterRefr, "ObjectReference");
AddObjProperty(&obj, "target", targetRefr, "ObjectReference");

SendEvent("magicEffectApply", obj);
});

return EventResult::kContinue;
}
Expand Down

0 comments on commit 4869870

Please sign in to comment.