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 std::apply([&](
auto&&... a_copy) {
114 vm->SendEvent(handle, eventName, args);
126 std::tuple args(
VMArg(std::forward<Args>(a_args))...);
130 task->AddTask([a_target, a_callback, args,
this]()
mutable {
137 template <
class Tuple, std::size_t... I>
140 SendEvent(a_target, a_callback, std::get<I>(std::forward<Tuple>(a_tuple)).Unpack()...);
169 const auto targetFormID = a_target->
GetFormID();
170 if (
auto it = this->_regs.find(targetFormID); it != this->_regs.end()) {
171 for (
auto& [eventFilter, handles] : it->second) {
172 if (a_callback(eventFilter.first, eventFilter.second)) {
173 for (
auto& handle : handles) {
175 vm->SendEvent(handle, eventName, args);
187 task->AddTask([a_target, a_callback,
this]() {
188 SendEvent(a_target, std::move(a_callback));
194 template <
class Filter>
197 _eventName(a_eventName),
201 template <
class Filter>
204 _eventName(a_rhs._eventName),
209 a_rhs.
_lock.unlock();
212 if (
auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr) {
213 for (
auto& reg :
_regs) {
214 for (
auto& keyHandles : reg.second) {
215 for (
auto& handle : keyHandles.second) {
216 policy->PersistHandle(handle);
223 template <
class Filter>
226 _eventName(a_rhs._eventName),
229 Locker locker(a_rhs._lock);
230 _regs = std::move(a_rhs._regs);
234 template <
class Filter>
238 if (
auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr) {
239 for (
auto& reg : _regs) {
240 for (
auto& keyHandles : reg.second) {
241 for (
auto& handle : keyHandles.second) {
242 policy->ReleaseHandle(handle);
249 template <
class Filter>
252 if (
this == &a_rhs) {
266 if (
auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr) {
267 for (
auto& reg : _regs) {
268 for (
auto& keyHandles : reg.second) {
269 for (
auto& handle : keyHandles.second) {
270 policy->PersistHandle(handle);
279 template <
class Filter>
282 if (
this == &a_rhs) {
287 Locker rhsLocker(a_rhs._lock);
291 _eventName = a_rhs._eventName;
293 _regs = std::move(a_rhs._regs);
299 template <
class Filter>
305 const auto formID = reference ? reference->
GetFormID() : 0;
308 return Register(a_form, formID, { a_filter, a_matchFilter },
static_cast<RE::VMTypeID>(a_form->
GetFormType()));
314 template <
class Filter>
317 assert(a_activeEffect);
320 const auto formID = target ? target->
GetFormID() : 0;
329 template <
class Filter>
335 const auto formID = target ? target->
GetFormID() : 0;
344 template <
class Filter>
350 const auto formID = reference ? reference->
GetFormID() : 0;
353 return Unregister(a_form, formID, { a_filter, a_matchFilter },
static_cast<RE::VMTypeID>(a_form->
GetFormType()));
359 template <
class Filter>
362 assert(a_activeEffect);
365 const auto formID = target ? target->
GetFormID() : 0;
374 template <
class Filter>
380 const auto formID = target ? target->
GetFormID() : 0;
389 template <
class Filter>
395 const auto formID = reference ? reference->
GetFormID() : 0;
402 template <
class Filter>
405 assert(a_activeEffect);
408 const auto formID = target ? target->
GetFormID() : 0;
415 template <
class Filter>
421 const auto formID = target ? target->
GetFormID() : 0;
428 template <
class Filter>
432 auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr;
434 log::error(
"Failed to get handle policy!");
439 for (
auto& reg : _regs) {
440 for (
auto& keyHandle : reg.second) {
441 if (
auto result = keyHandle.second.erase(a_handle); result != 0) {
442 policy->ReleaseHandle(a_handle);
448 template <
class Filter>
452 auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr;
454 log::error(
"Failed to get handle policy!");
459 auto it = _regs.find(a_uniqueID);
460 if (it != _regs.end()) {
461 for (
auto& keyHandles : it->second) {
462 for (
auto& handle : keyHandles.second) {
463 policy->ReleaseHandle(handle);
470 template <
class Filter>
474 auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr;
477 for (
auto& reg : _regs) {
478 for (
auto& keyHandle : reg.second) {
479 for (
auto& handle : keyHandle.second) {
480 policy->ReleaseHandle(handle);
488 template <
class Filter>
492 if (!a_intfc->
OpenRecord(a_type, a_version)) {
493 log::error(
"Failed to open record!");
497 return Save(a_intfc);
500 template <
class Filter>
507 const std::size_t numRegs = _regs.size();
509 log::error(
"Failed to save reg count ({})!", numRegs);
512 for (
auto& reg : _regs) {
515 log::error(
"Failed to save handle formID ({:X})", reg.first);
518 std::size_t numUniqueHandle = reg.second.size();
520 log::error(
"Failed to save handle count ({})!", numUniqueHandle);
524 for (
auto& [key, handles] : reg.second) {
526 auto [eventFilter, match] = key;
527 if (!eventFilter.Save(a_intfc)) {
528 log::error(
"Failed to save event filters!");
532 log::error(
"Failed to save reg key as bool ({})!", match);
536 std::size_t numHandles = handles.size();
538 log::error(
"Failed to save handle count ({})!", numHandles);
541 for (
auto& handle : handles) {
543 log::error(
"Failed to save handle ({})", handle);
553 template <
class Filter>
566 std::size_t numKeyHandle;
568 std::size_t numHandles;
571 for (std::size_t i = 0; i < numRegs; ++i) {
574 log::warn(
"Failed to resolve target formID ({:X})", formID);
578 for (std::size_t j = 0; j < numKeyHandle; ++j) {
580 Filter eventFilter{};
581 if (!eventFilter.Load(a_intfc)) {
582 log::error(
"Failed to save event filters!");
590 for (std::size_t k = 0; k < numHandles; ++k) {
593 _regs[formID][curKey].insert(vmHandle);
602 template <
class Filter>
608 template <
class Filter>
613 auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr;
615 log::error(
"Failed to get handle policy!");
619 const auto invalidHandle = policy->EmptyHandle();
620 auto handle = policy->GetHandleForObject(a_typeID, a_object);
621 if (handle == invalidHandle) {
622 log::error(
"Failed to create handle!");
627 auto result = _regs[a_formID][a_filter].insert(handle);
631 policy->PersistHandle(handle);
634 return result.second;
637 template <
class Filter>
642 auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr;
644 log::error(
"Failed to get handle policy!");
648 const auto invalidHandle = policy->EmptyHandle();
649 const auto handle = policy->GetHandleForObject(a_typeID, a_object);
650 if (handle == invalidHandle) {
651 log::error(
"Failed to create handle!");
656 if (
auto formIt = _regs.find(a_formID); formIt != _regs.end()) {
657 if (
auto keyIt = formIt->second.find(a_filter); keyIt != formIt->second.end()) {
658 if (
auto result = keyIt->second.erase(handle); result != 0) {
659 policy->ReleaseHandle(handle);
668 template <
class Filter>
673 auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr;
675 log::error(
"Failed to get handle policy!");
679 const auto invalidHandle = policy->EmptyHandle();
680 const auto handle = policy->GetHandleForObject(a_typeID, a_object);
681 if (handle == invalidHandle) {
682 log::error(
"Failed to create handle!");
687 if (
auto it = _regs.find(a_formID); it != _regs.end()) {
688 for (
auto& keyHandles : it->second) {
689 if (
auto result = keyHandles.second.erase(handle); result != 0) {
690 policy->ReleaseHandle(handle);
697 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:114
Definition: RegistrationMapUnique.h:32
std::recursive_mutex Lock
Definition: RegistrationMapUnique.h:61
bool Load(SerializationInterface *a_intfc)
Definition: RegistrationMapUnique.h:554
void Clear()
Definition: RegistrationMapUnique.h:471
~RegistrationMapUniqueBase()
Definition: RegistrationMapUnique.h:235
void Revert(SerializationInterface *)
Definition: RegistrationMapUnique.h:603
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:300
bool Save(SerializationInterface *a_intfc, std::uint32_t a_type, std::uint32_t a_version)
Definition: RegistrationMapUnique.h:489
RegistrationMapUniqueBase()=delete
RegistrationMapUniqueBase & operator=(const RegistrationMapUniqueBase &a_rhs)
Definition: RegistrationMapUnique.h:250
bool Unregister(RE::TESForm *a_form, const Filter &a_filter, bool a_matchFilter)
Definition: RegistrationMapUnique.h:345
void UnregisterAll(const RE::TESForm *a_form)
Definition: RegistrationMapUnique.h:390
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:124
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:164
RegistrationMapUnique()=delete
RegistrationMapUnique(const std::string_view &a_eventName)
Definition: RegistrationMapUnique.h:155
void QueueEvent(RE::TESObjectREFR *a_target, PassFilterFunc a_callback)
Definition: RegistrationMapUnique.h:183
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
auto make_tuple(T1 &&a_first, T2 &&a_second)
Definition: BSTTuple.h:187
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:698
const TaskInterface * GetTaskInterface() noexcept
Definition: EffectArchetypes.h:65
Definition: RegistrationTraits.h:40