20 template <
class Filter>
53 using Lock = std::recursive_mutex;
54 using Locker = std::lock_guard<Lock>;
63 std::map<Filter, std::set<RE::VMHandle>>
_regs;
68 template <
class Enable,
class... Args>
71 template <
class... Args>
75 RE::BSScript::is_return_convertible<Args>...>>,
96 inline void SendEvent(Filter a_filter, Args... a_args)
101 if (
auto it = this->_regs.find(a_filter); it != this->_regs.end()) {
102 for (
auto& handle : it->second) {
104 std::apply([&](
auto&&... a_copy) {
106 vm->SendEvent(handle, eventName, args);
116 std::tuple args(
VMArg(std::forward<Args>(a_args))...);
120 task->AddTask([a_filter, args,
this]()
mutable {
127 template <
class Tuple, std::size_t... I>
128 inline void SendEvent_Tuple(Filter a_filter, Tuple&& a_tuple, std::index_sequence<I...>)
130 SendEvent(a_filter, std::get<I>(std::forward<Tuple>(a_tuple)).Unpack()...);
159 if (
auto it = this->_regs.find(a_filter); it != this->_regs.end()) {
160 for (
auto& handle : it->second) {
162 vm->SendEvent(handle, eventName, args);
172 task->AddTask([a_filter,
this]() {
173 SendEvent(std::move(a_filter));
179 template <
class Filter>
182 _eventName(a_eventName),
186 template <
class Filter>
189 _eventName(a_rhs._eventName),
194 a_rhs.
_lock.unlock();
197 if (
auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr) {
198 for (
auto& reg :
_regs) {
199 for (
auto& handle : reg.second) {
200 policy->PersistHandle(handle);
206 template <
class Filter>
209 _eventName(a_rhs._eventName),
212 Locker locker(a_rhs._lock);
213 _regs = std::move(a_rhs._regs);
217 template <
class Filter>
221 if (
auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr) {
222 for (
auto& reg : _regs) {
223 for (
auto& handle : reg.second) {
224 policy->ReleaseHandle(handle);
230 template <
class Filter>
233 if (
this == &a_rhs) {
247 if (
auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr) {
248 for (
auto& reg : _regs) {
249 for (
auto& handle : reg.second) {
250 policy->PersistHandle(handle);
258 template <
class Filter>
261 if (
this == &a_rhs) {
266 Locker rhsLocker(a_rhs._lock);
270 _eventName = a_rhs._eventName;
272 _regs = std::move(a_rhs._regs);
278 template <
class Filter>
285 template <
class Filter>
289 return Register(a_alias, std::move(a_filter), a_alias->
GetVMTypeID());
292 template <
class Filter>
295 assert(a_activeEffect);
299 template <
class Filter>
306 template <
class Filter>
310 return Unregister(a_alias, std::move(a_filter), a_alias->
GetVMTypeID());
313 template <
class Filter>
316 assert(a_activeEffect);
320 template <
class Filter>
327 template <
class Filter>
334 template <
class Filter>
337 assert(a_activeEffect);
341 template <
class Filter>
345 auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr;
347 log::error(
"Failed to get handle policy!");
352 for (
auto& reg : _regs) {
353 if (
auto result = reg.second.erase(a_handle); result != 0) {
354 policy->ReleaseHandle(a_handle);
359 template <
class Filter>
363 auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr;
366 for (
auto& reg : _regs) {
367 for (
auto& handle : reg.second) {
368 policy->ReleaseHandle(handle);
375 template <
class Filter>
379 if (!a_intfc->
OpenRecord(a_type, a_version)) {
380 log::error(
"Failed to open record!");
384 return Save(a_intfc);
387 template <
class Filter>
390 if constexpr (std::is_same_v<std::string, Filter>) {
391 std::size_t length = a_filter.length() + 1;
401 template <
class Filter>
408 const std::size_t numRegs = _regs.size();
410 log::error(
"Failed to save reg count ({})!", numRegs);
414 for (
auto& reg : _regs) {
416 if (!SaveFilter(a_intfc, reg.first)) {
420 std::size_t numHandles = reg.second.size();
422 log::error(
"Failed to save handle count ({})!", numHandles);
426 for (
auto& handle : reg.second) {
428 log::error(
"Failed to save handle ({})", handle);
437 template <
class Filter>
440 if constexpr (std::is_same_v<std::string, Filter>) {
441 std::size_t length = 0;
445 a_filter.reserve(length);
446 return a_intfc->
ReadRecordData(a_filter.data(),
static_cast<std::uint32_t
>(length));
447 }
else if constexpr (std::is_same_v<RE::FormID, Filter>) {
457 template <
class Filter>
469 std::size_t numHandles;
473 for (std::size_t i = 0; i < numRegs; ++i) {
474 if (!LoadFilter(a_intfc, filter)) {
478 for (std::size_t j = 0; j < numHandles; ++j) {
481 _regs[filter].insert(handle);
489 template <
class Filter>
495 template <
class Filter>
500 auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr;
502 log::error(
"Failed to get handle policy!");
506 const auto invalidHandle = policy->EmptyHandle();
507 auto handle = policy->GetHandleForObject(a_typeID, a_object);
508 if (handle == invalidHandle) {
509 log::error(
"Failed to create handle!");
514 auto result = _regs[a_filter].insert(handle);
518 policy->PersistHandle(handle);
521 return result.second;
524 template <
class Filter>
529 auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr;
531 log::error(
"Failed to get handle policy!");
535 const auto invalidHandle = policy->EmptyHandle();
536 const auto handle = policy->GetHandleForObject(a_typeID, a_object);
537 if (handle == invalidHandle) {
538 log::error(
"Failed to create handle!");
543 if (
auto it = _regs.find(a_filter); it != _regs.end()) {
544 if (
auto result = it->second.erase(handle); result != 0) {
545 policy->ReleaseHandle(handle);
553 template <
class Filter>
558 auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr;
560 log::error(
"Failed to get handle policy!");
564 const auto invalidHandle = policy->EmptyHandle();
565 const auto handle = policy->GetHandleForObject(a_typeID, a_object);
566 if (handle == invalidHandle) {
567 log::error(
"Failed to create handle!");
572 for (
auto& reg : _regs) {
573 if (
auto result = reg.second.erase(handle); result != 0) {
574 policy->ReleaseHandle(handle);
580 template <
class Filter,
class... Args>
Definition: ActiveEffect.h:27
static constexpr auto VMTYPEID
Definition: ActiveEffect.h:31
Definition: BGSBaseAlias.h:12
VMTypeID GetVMTypeID() const
static VirtualMachine * GetSingleton()
Definition: RegistrationMap.h:25
bool Load(SerializationInterface *a_intfc)
Definition: RegistrationMap.h:458
RegistrationMapBase & operator=(const RegistrationMapBase &a_rhs)
Definition: RegistrationMap.h:231
RegistrationMapBase()=delete
bool LoadFilter(SerializationInterface *a_intfc, Filter &a_filter)
Definition: RegistrationMap.h:438
bool Unregister(const RE::TESForm *a_form, Filter a_filter)
Definition: RegistrationMap.h:300
void Revert(SerializationInterface *)
Definition: RegistrationMap.h:490
~RegistrationMapBase()
Definition: RegistrationMap.h:218
std::string _eventName
Definition: RegistrationMap.h:64
std::lock_guard< Lock > Locker
Definition: RegistrationMap.h:54
Lock _lock
Definition: RegistrationMap.h:65
std::map< Filter, std::set< RE::VMHandle > > _regs
Definition: RegistrationMap.h:63
bool Register(const RE::TESForm *a_form, Filter a_filter)
Definition: RegistrationMap.h:279
bool SaveFilter(SerializationInterface *a_intfc, Filter a_filter)
Definition: RegistrationMap.h:388
void Clear()
Definition: RegistrationMap.h:360
std::recursive_mutex Lock
Definition: RegistrationMap.h:53
void UnregisterAll(const RE::TESForm *a_form)
Definition: RegistrationMap.h:321
bool Save(SerializationInterface *a_intfc, std::uint32_t a_type, std::uint32_t a_version)
Definition: RegistrationMap.h:376
~RegistrationMap()=default
void SendEvent(Filter a_filter, Args... a_args)
Definition: RegistrationMap.h:96
void QueueEvent(Filter a_filter, Args... a_args)
Definition: RegistrationMap.h:114
RegistrationMap(RegistrationMap &&)=default
RegistrationMap(const RegistrationMap &)=default
RegistrationMap & operator=(const RegistrationMap &)=default
RegistrationMap & operator=(RegistrationMap &&)=default
RegistrationMap(const std::string_view &a_eventName)
Definition: RegistrationMap.h:87
~RegistrationMap()=default
RegistrationMap & operator=(const RegistrationMap &)=default
void QueueEvent(Filter a_filter)
Definition: RegistrationMap.h:168
RegistrationMap(const RegistrationMap &)=default
RegistrationMap & operator=(RegistrationMap &&)=default
void SendEvent(Filter a_filter)
Definition: RegistrationMap.h:154
RegistrationMap(RegistrationMap &&)=default
RegistrationMap(const std::string_view &a_eventName)
Definition: RegistrationMap.h:145
Definition: RegistrationMap.h:69
Definition: RegistrationMap.h:22
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))
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::EventFilter< Filter >::template RegistrationMap< void, Args... > RegistrationMap
Definition: RegistrationMap.h:581
const TaskInterface * GetTaskInterface() noexcept
Definition: EffectArchetypes.h:65
Definition: RegistrationTraits.h:40