ArcdpsExtension
 
Loading...
Searching...
No Matches
EventSequencer.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <cstdlib>
6#include <functional>
7#include <mutex>
8#include <set>
9#include <thread>
10
11namespace ArcdpsExtension {
13 public:
14 typedef std::function<uintptr_t(cbtevent* ev, ag* src, ag* dst, const char* skillname, uint64_t id, uint64_t revision)> CallbackSignature;
15
16 explicit EventSequencer(const CallbackSignature& pCallback);
17 virtual ~EventSequencer();
18
19 // delete copy and move
20 EventSequencer(const EventSequencer& pOther) = delete;
21 EventSequencer(EventSequencer&& pOther) noexcept = delete;
22 EventSequencer& operator=(const EventSequencer& pOther) = delete;
23 EventSequencer& operator=(EventSequencer&& pOther) noexcept = delete;
24
25 struct Event {
26 struct : cbtevent {
27 bool Present = false;
28 } Ev;
29
30 struct : ag {
31 std::string NameStorage;
32 bool Present = false;
34
35 struct : ag {
36 std::string NameStorage;
37 bool Present = false;
39
40 const char* Skillname; // Skill names are guaranteed to be valid for the lifetime of the process so copying pointer is fine
41 uint64_t Id;
42 uint64_t Revision;
43
44 std::strong_ordering operator<=>(const Event& pOther) const {
45 return Id <=> pOther.Id;
46 }
47
50 Id(pId),
52 if (pEv) {
53 *static_cast<cbtevent*>(&Ev) = *pEv;
54 Ev.Present = true;
55 }
56 if (pSrc) {
57 *static_cast<ag*>(&Source) = *pSrc;
58 Source.Present = true;
59 if (Source.name) {
60 Source.NameStorage = pSrc->name;
61 }
62 }
63 if (pDst) {
64 *static_cast<ag*>(&Destination) = *pDst;
65 Destination.Present = true;
66 if (Destination.name) {
67 Destination.NameStorage = pDst->name;
68 }
69 }
70 }
71 };
72
73 void ProcessEvent(cbtevent* pEv, ag* pSrc, ag* pDst, const char* pSkillname, uint64_t pId, uint64_t pRevision);
74
75 [[nodiscard]] bool EventsPending() const;
80 void Reset();
81
82 void Shutdown();
83
84 private:
85 const CallbackSignature mCallback;
86 std::multiset<Event> mElements;
87 std::mutex mElementsMutex;
88 std::jthread mThread;
89 uint64_t mNextId = 2; // Events start with ID 2 for some reason (it is always like that and no plans to change)
90 uint64_t mLastId = 2;
91 bool mThreadRunning = false;
92
93 template<bool First = true>
94 void Runner() {
95 std::unique_lock guard(mElementsMutex);
96
97 if (!mElements.empty() && mElements.begin()->Id == mNextId) {
98 mThreadRunning = true;
99 auto item = mElements.extract(mElements.begin());
100 guard.unlock();
101 EventInternal(item.value());
102 mThreadRunning = false;
103 Runner<false>();
104
105 if constexpr (First) {
106 ++mNextId;
107 }
108 }
109 }
110
111 void EventInternal(Event& pElem);
112 };
113} // namespace ArcdpsExtension
Definition EventSequencer.h:12
void ProcessEvent(cbtevent *pEv, ag *pSrc, ag *pDst, const char *pSkillname, uint64_t pId, uint64_t pRevision)
Definition EventSequencer.cpp:6
virtual ~EventSequencer()
Definition EventSequencer.cpp:49
EventSequencer(EventSequencer &&pOther) noexcept=delete
bool EventsPending() const
Definition EventSequencer.cpp:23
EventSequencer & operator=(EventSequencer &&pOther) noexcept=delete
EventSequencer(const EventSequencer &pOther)=delete
void Shutdown()
Definition EventSequencer.cpp:78
std::function< uintptr_t(cbtevent *ev, ag *src, ag *dst, const char *skillname, uint64_t id, uint64_t revision)> CallbackSignature
Definition EventSequencer.h:14
void Reset()
Definition EventSequencer.cpp:27
EventSequencer & operator=(const EventSequencer &pOther)=delete
Definition ArcdpsExtension.h:10
Definition EventSequencer.h:25
uint64_t Id
Definition EventSequencer.h:41
std::strong_ordering operator<=>(const Event &pOther) const
Definition EventSequencer.h:44
const char * Skillname
Definition EventSequencer.h:40
std::string NameStorage
Definition EventSequencer.h:31
Event(cbtevent *pEv, ag *pSrc, ag *pDst, const char *pSkillname, uint64_t pId, uint64_t pRevision)
Definition EventSequencer.h:48
bool Present
Definition EventSequencer.h:27
uint64_t Revision
Definition EventSequencer.h:42
Definition arcdps_structs_slim.h:320
const char * name
Definition arcdps_structs_slim.h:321
Definition arcdps_structs_slim.h:289