22 template <
class Filter>
61 using Lock = std::recursive_mutex;
62 using Locker = std::lock_guard<Lock>;
68 std::map<RE::FormID, EventFilterHandleMap>
_regs;
73 template <
class Enable,
class... Args>
76 template <
class... Args>
80 RE::BSScript::is_return_convertible<Args>...>>,
106 const auto targetFormID = a_target->
GetFormID();
107 if (
auto it = this->_regs.find(targetFormID); it != this->_regs.end()) {
108 for (
auto& [eventFilter, handles] : it->second) {
109 if (a_callback(eventFilter.first, eventFilter.second)) {
110 for (
auto& handle : handles) {
112 vm->SendEvent(handle, eventName, args);
122 std::tuple args(
VMArg(std::forward<Args>(a_args))...);
126 task->AddTask([a_target, a_callback, args,
this]()
mutable {
133 template <
class Tuple, std::size_t... I>
136 SendEvent(a_target, a_callback, std::get<I>(std::forward<Tuple>(a_tuple)).Unpack()...);
165 const auto targetFormID = a_target->
GetFormID();
166 if (
auto it = this->_regs.find(targetFormID); it != this->_regs.end()) {
167 for (
auto& [eventFilter, handles] : it->second) {
168 if (a_callback(eventFilter.first, eventFilter.second)) {
169 for (
auto& handle : handles) {
171 vm->SendEvent(handle, eventName, args);
183 task->AddTask([a_target, a_callback,
this]() {
184 SendEvent(a_target, std::move(a_callback));
190 template <
class Filter>
193 _eventName(a_eventName),
197 template <
class Filter>
200 _eventName(a_rhs._eventName),
205 a_rhs.
_lock.unlock();
208 if (
auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr) {
209 for (
auto& reg :
_regs) {
210 for (
auto& keyHandles : reg.second) {
211 for (
auto& handle : keyHandles.second) {
212 policy->PersistHandle(handle);
219 template <
class Filter>
222 _eventName(a_rhs._eventName),
225 Locker locker(a_rhs._lock);
226 _regs = std::move(a_rhs._regs);
230 template <
class Filter>
234 if (
auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr) {
235 for (
auto& reg : _regs) {
236 for (
auto& keyHandles : reg.second) {
237 for (
auto& handle : keyHandles.second) {
238 policy->ReleaseHandle(handle);
245 template <
class Filter>
248 if (
this == &a_rhs) {
262 if (
auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr) {
263 for (
auto& reg : _regs) {
264 for (
auto& keyHandles : reg.second) {
265 for (
auto& handle : keyHandles.second) {
266 policy->PersistHandle(handle);
275 template <
class Filter>
278 if (
this == &a_rhs) {
283 Locker rhsLocker(a_rhs._lock);
287 _eventName = a_rhs._eventName;
289 _regs = std::move(a_rhs._regs);
295 template <
class Filter>
301 const auto formID = reference ? reference->
GetFormID() : 0;
304 return Register(a_form, formID, { a_filter, a_matchFilter },
static_cast<RE::VMTypeID>(a_form->
GetFormType()));
310 template <
class Filter>
313 assert(a_activeEffect);
316 const auto formID = target ? target->
GetFormID() : 0;
325 template <
class Filter>
331 const auto formID = target ? target->
GetFormID() : 0;
340 template <
class Filter>
346 const auto formID = reference ? reference->
GetFormID() : 0;
349 return Unregister(a_form, formID, { a_filter, a_matchFilter },
static_cast<RE::VMTypeID>(a_form->
GetFormType()));
355 template <
class Filter>
358 assert(a_activeEffect);
361 const auto formID = target ? target->
GetFormID() : 0;
370 template <
class Filter>
376 const auto formID = target ? target->
GetFormID() : 0;
385 template <
class Filter>
391 const auto formID = reference ? reference->
GetFormID() : 0;
398 template <
class Filter>
401 assert(a_activeEffect);
404 const auto formID = target ? target->
GetFormID() : 0;
411 template <
class Filter>
417 const auto formID = target ? target->
GetFormID() : 0;
424 template <
class Filter>
428 auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr;
430 log::error(
"Failed to get handle policy!");
435 for (
auto& reg : _regs) {
436 for (
auto& keyHandle : reg.second) {
437 if (
auto result = keyHandle.second.erase(a_handle); result != 0) {
438 policy->ReleaseHandle(a_handle);
444 template <
class Filter>
448 auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr;
450 log::error(
"Failed to get handle policy!");
455 auto it = _regs.find(a_uniqueID);
456 if (it != _regs.end()) {
457 for (
auto& keyHandles : it->second) {
458 for (
auto& handle : keyHandles.second) {
459 policy->ReleaseHandle(handle);
466 template <
class Filter>
470 auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr;
473 for (
auto& reg : _regs) {
474 for (
auto& keyHandle : reg.second) {
475 for (
auto& handle : keyHandle.second) {
476 policy->ReleaseHandle(handle);
484 template <
class Filter>
488 if (!a_intfc->
OpenRecord(a_type, a_version)) {
489 log::error(
"Failed to open record!");
493 return Save(a_intfc);
496 template <
class Filter>
503 const std::size_t numRegs = _regs.size();
505 log::error(
"Failed to save reg count ({})!", numRegs);
508 for (
auto& reg : _regs) {
511 log::error(
"Failed to save handle formID ({:X})", reg.first);
514 std::size_t numUniqueHandle = reg.second.size();
516 log::error(
"Failed to save handle count ({})!", numUniqueHandle);
520 for (
auto& [key, handles] : reg.second) {
522 auto [eventFilter, match] = key;
523 if (!eventFilter.Save(a_intfc)) {
524 log::error(
"Failed to save event filters!");
528 log::error(
"Failed to save reg key as bool ({})!", match);
532 std::size_t numHandles = handles.size();
534 log::error(
"Failed to save handle count ({})!", numHandles);
537 for (
auto& handle : handles) {
539 log::error(
"Failed to save handle ({})", handle);
549 template <
class Filter>
562 std::size_t numKeyHandle;
564 std::size_t numHandles;
567 for (std::size_t i = 0; i < numRegs; ++i) {
570 log::warn(
"Failed to resolve target formID ({:X})", formID);
574 for (std::size_t j = 0; j < numKeyHandle; ++j) {
576 Filter eventFilter{};
577 if (!eventFilter.Load(a_intfc)) {
578 log::error(
"Failed to save event filters!");
586 for (std::size_t k = 0; k < numHandles; ++k) {
589 _regs[formID][curKey].insert(vmHandle);
598 template <
class Filter>
604 template <
class Filter>
609 auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr;
611 log::error(
"Failed to get handle policy!");
615 const auto invalidHandle = policy->EmptyHandle();
616 auto handle = policy->GetHandleForObject(a_typeID, a_object);
617 if (handle == invalidHandle) {
618 log::error(
"Failed to create handle!");
623 auto result = _regs[a_formID][a_filter].insert(handle);
627 policy->PersistHandle(handle);
630 return result.second;
633 template <
class Filter>
638 auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr;
640 log::error(
"Failed to get handle policy!");
644 const auto invalidHandle = policy->EmptyHandle();
645 const auto handle = policy->GetHandleForObject(a_typeID, a_object);
646 if (handle == invalidHandle) {
647 log::error(
"Failed to create handle!");
652 if (
auto formIt = _regs.find(a_formID); formIt != _regs.end()) {
653 if (
auto keyIt = formIt->second.find(a_filter); keyIt != formIt->second.end()) {
654 if (
auto result = keyIt->second.erase(handle); result != 0) {
655 policy->ReleaseHandle(handle);
664 template <
class Filter>
669 auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr;
671 log::error(
"Failed to get handle policy!");
675 const auto invalidHandle = policy->EmptyHandle();
676 const auto handle = policy->GetHandleForObject(a_typeID, a_object);
677 if (handle == invalidHandle) {
678 log::error(
"Failed to create handle!");
683 if (
auto it = _regs.find(a_formID); it != _regs.end()) {
684 for (
auto& keyHandles : it->second) {
685 if (
auto result = keyHandles.second.erase(handle); result != 0) {
686 policy->ReleaseHandle(handle);
693 template <
class Filter,
class... Args>
Definition: ActiveEffect.h:27
static constexpr auto VMTYPEID
Definition: ActiveEffect.h:31
Definition: BGSRefAlias.h:15
static constexpr auto VMTYPEID
Definition: BGSRefAlias.h:19
Actor * GetActorReference() const
static VirtualMachine * GetSingleton()
Definition: TESObjectREFR.h:113
Definition: RegistrationMapUnique.h:32
std::recursive_mutex Lock
Definition: RegistrationMapUnique.h:61
bool Load(SerializationInterface *a_intfc)
Definition: RegistrationMapUnique.h:550
void Clear()
Definition: RegistrationMapUnique.h:467
~RegistrationMapUniqueBase()
Definition: RegistrationMapUnique.h:231
void Revert(SerializationInterface *)
Definition: RegistrationMapUnique.h:599
Lock _lock
Definition: RegistrationMapUnique.h:70
std::map< RE::FormID, EventFilterHandleMap > _regs
Definition: RegistrationMapUnique.h:68
bool Register(RE::TESForm *a_form, const Filter &a_filter, bool a_matchFilter)
Definition: RegistrationMapUnique.h:296
bool Save(SerializationInterface *a_intfc, std::uint32_t a_type, std::uint32_t a_version)
Definition: RegistrationMapUnique.h:485
RegistrationMapUniqueBase()=delete
RegistrationMapUniqueBase & operator=(const RegistrationMapUniqueBase &a_rhs)
Definition: RegistrationMapUnique.h:246
bool Unregister(RE::TESForm *a_form, const Filter &a_filter, bool a_matchFilter)
Definition: RegistrationMapUnique.h:341
void UnregisterAll(const RE::TESForm *a_form)
Definition: RegistrationMapUnique.h:386
std::string _eventName
Definition: RegistrationMapUnique.h:69
std::lock_guard< Lock > Locker
Definition: RegistrationMapUnique.h:62
~RegistrationMapUnique()=default
RegistrationMapUnique(RegistrationMapUnique &&)=default
RegistrationMapUnique & operator=(const RegistrationMapUnique &)=default
RegistrationMapUnique()=delete
void SendEvent(const RE::TESObjectREFR *a_target, PassFilterFunc a_callback, Args... a_args)
Definition: RegistrationMapUnique.h:101
RegistrationMapUnique(const RegistrationMapUnique &)=default
void QueueEvent(RE::TESObjectREFR *a_target, PassFilterFunc a_callback, Args... a_args)
Definition: RegistrationMapUnique.h:120
RegistrationMapUnique & operator=(RegistrationMapUnique &&)=default
RegistrationMapUnique(const std::string_view &a_eventName)
Definition: RegistrationMapUnique.h:92
RegistrationMapUnique & operator=(const RegistrationMapUnique &)=default
RegistrationMapUnique & operator=(RegistrationMapUnique &&)=default
RegistrationMapUnique(RegistrationMapUnique &&)=default
RegistrationMapUnique(const RegistrationMapUnique &)=default
~RegistrationMapUnique()=default
void SendEvent(const RE::TESObjectREFR *a_target, PassFilterFunc a_callback)
Definition: RegistrationMapUnique.h:160
RegistrationMapUnique()=delete
RegistrationMapUnique(const std::string_view &a_eventName)
Definition: RegistrationMapUnique.h:151
void QueueEvent(RE::TESObjectREFR *a_target, PassFilterFunc a_callback)
Definition: RegistrationMapUnique.h:179
Definition: RegistrationMapUnique.h:74
Definition: RegistrationMapUnique.h:24
std::function< bool(const Filter &, bool)> PassFilterFunc
Definition: RegistrationMapUnique.h:29
std::map< EventFilter, std::set< RE::VMHandle > > EventFilterHandleMap
Definition: RegistrationMapUnique.h:27
std::pair< Filter, bool > EventFilter
Definition: RegistrationMapUnique.h:26
Definition: Interfaces.h:82
bool ResolveHandle(RE::VMHandle a_oldHandle, RE::VMHandle &a_newHandle) const
std::uint32_t ReadRecordData(void *a_buf, std::uint32_t a_length) const
bool WriteRecordData(const void *a_buf, std::uint32_t a_length) const
bool ResolveFormID(RE::FormID a_oldFormID, RE::FormID &a_newFormID) const
bool OpenRecord(std::uint32_t a_type, std::uint32_t a_version) const
constexpr REL::ID Save(static_cast< std::uint64_t >(34818))
std::uint32_t FormID
Definition: BSCoreTypes.h:5
std::uint32_t VMTypeID
Definition: BSCoreTypes.h:9
std::uint64_t VMHandle
Definition: BSCoreTypes.h:7
BSScript::IFunctionArguments * MakeFunctionArguments(Args &&... a_args)
Definition: FunctionArguments.h:80
VMArg(T &&) -> VMArg< T >
string(const CharT(&)[N]) -> string< CharT, N - 1 >
typename Impl::EventFilterUnique< Filter >::template RegistrationMapUnique< void, Args... > RegistrationMapUnique
Definition: RegistrationMapUnique.h:694
const TaskInterface * GetTaskInterface() noexcept
Definition: EffectArchetypes.h:65
Definition: RegistrationTraits.h:40