CommonLibSSE (powerof3)
RegistrationMap.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "RE/B/BGSBaseAlias.h"
4 #include "RE/B/BSFixedString.h"
7 #include "RE/T/TESForm.h"
8 #include "RE/T/TypeTraits.h"
9 #include "RE/V/VirtualMachine.h"
10 
11 #include "SKSE/API.h"
13 #include "SKSE/Interfaces.h"
14 
15 namespace SKSE
16 {
17  namespace Impl
18  {
20  {
21  public:
26 
29 
30  bool Register(const RE::TESForm* a_form, RE::BSFixedString a_callback);
31  bool Register(const RE::BGSBaseAlias* a_alias, RE::BSFixedString a_callback);
32  bool Unregister(const RE::TESForm* a_form);
33  bool Unregister(const RE::BGSBaseAlias* a_alias);
34  void Clear();
35  bool Save(SerializationInterface* a_intfc, std::uint32_t a_type, std::uint32_t a_version);
36  bool Save(SerializationInterface* a_intfc);
37  bool Load(SerializationInterface* a_intfc);
38 
39  protected:
40  using Lock = std::recursive_mutex;
41  using Locker = std::lock_guard<Lock>;
43 
44  bool Register(const void* a_object, RE::BSFixedString a_callback, RE::VMTypeID a_typeID);
45  bool Unregister(const void* a_object, RE::VMTypeID a_typeID);
46 
47  std::map<RE::VMHandle, EventName> _regs;
48  mutable Lock _lock;
49  };
50 
51  template <class Enable, class... Args>
53 
54  template <class... Args>
56  std::enable_if_t<
57  std::conjunction_v<
58  RE::BSScript::is_return_convertible<Args>...>>,
59  Args...> :
60  public RegistrationMapBase
61  {
62  public:
63  RegistrationMap() = default;
64  RegistrationMap(const RegistrationMap&) = default;
66 
67  ~RegistrationMap() = default;
68 
69  RegistrationMap& operator=(const RegistrationMap& a_rhs) = default;
71 
72  inline void SendEvent(Args... a_args)
73  {
75  for (auto& reg : _regs) {
76  auto args = RE::MakeFunctionArguments(std::forward<Args>(a_args)...);
77  vm->SendEvent(reg.first, reg.second.c_str(), args);
78  }
79  }
80 
81  inline void QueueEvent(Args... a_args)
82  {
83  std::tuple args(VMArg(std::forward<Args>(a_args))...);
84  auto task = GetTaskInterface();
85  assert(task);
86  if (task) {
87  task->AddTask([args, this]() mutable {
88  SendEvent_Tuple(std::move(args), index_sequence_for_tuple<decltype(args)>{});
89  });
90  }
91  }
92 
93  private:
94  template <class Tuple, std::size_t... I>
95  inline void SendEvent_Tuple(Tuple&& a_tuple, std::index_sequence<I...>)
96  {
97  SendEvent(std::get<I>(std::forward<Tuple>(a_tuple)).Unpack()...);
98  }
99  };
100 
101  template <>
103  {
104  public:
105  RegistrationMap() = default;
106  RegistrationMap(const RegistrationMap&) = default;
108 
109  ~RegistrationMap() = default;
110 
113 
114  inline void SendEvent()
115  {
117  if (vm) {
118  for (auto& reg : _regs) {
119  auto args = RE::MakeFunctionArguments();
120  vm->SendEvent(reg.first, reg.second.c_str(), args);
121  }
122  }
123  }
124 
125  inline void QueueEvent()
126  {
127  auto task = GetTaskInterface();
128  assert(task);
129  task->AddTask([this]() {
130  SendEvent();
131  });
132  }
133  };
134  }
135 
136  template <class... Args>
137  using RegistrationMap = Impl::RegistrationMap<void, Args...>;
138 }
Definition: BGSBaseAlias.h:12
static VirtualMachine * GetSingleton()
Definition: TESForm.h:34
Definition: RegistrationMap.h:20
RegistrationMapBase & operator=(const RegistrationMapBase &a_rhs)
bool Register(const void *a_object, RE::BSFixedString a_callback, RE::VMTypeID a_typeID)
bool Save(SerializationInterface *a_intfc, std::uint32_t a_type, std::uint32_t a_version)
bool Unregister(const RE::BGSBaseAlias *a_alias)
Lock _lock
Definition: RegistrationMap.h:48
RegistrationMapBase(const RegistrationMapBase &a_rhs)
std::string EventName
Definition: RegistrationMap.h:42
std::map< RE::VMHandle, EventName > _regs
Definition: RegistrationMap.h:47
RegistrationMapBase(RegistrationMapBase &&a_rhs)
RegistrationMapBase & operator=(RegistrationMapBase &&a_rhs)
bool Register(const RE::TESForm *a_form, RE::BSFixedString a_callback)
bool Unregister(const void *a_object, RE::VMTypeID a_typeID)
bool Unregister(const RE::TESForm *a_form)
bool Save(SerializationInterface *a_intfc)
bool Load(SerializationInterface *a_intfc)
std::recursive_mutex Lock
Definition: RegistrationMap.h:40
std::lock_guard< Lock > Locker
Definition: RegistrationMap.h:41
bool Register(const RE::BGSBaseAlias *a_alias, RE::BSFixedString a_callback)
RegistrationMap(const RegistrationMap &)=default
RegistrationMap & operator=(const RegistrationMap &)=default
RegistrationMap & operator=(RegistrationMap &&)=default
RegistrationMap(RegistrationMap &&)=default
void SendEvent()
Definition: RegistrationMap.h:114
void QueueEvent()
Definition: RegistrationMap.h:125
Definition: RegistrationMap.h:52
Definition: Interfaces.h:71
std::uint32_t VMTypeID
Definition: BSCoreTypes.h:9
BSScript::IFunctionArguments * MakeFunctionArguments(Args &&... a_args)
Definition: FunctionArguments.h:80
VMArg(T &&) -> VMArg< T >
string(const CharT(&)[N]) -> string< CharT, N - 1 >
Definition: API.h:14
const TaskInterface * GetTaskInterface() noexcept
Definition: NiBinaryStream.h:94
Definition: RegistrationTraits.h:40