-
-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathcallback_manager.cpp
More file actions
183 lines (154 loc) · 4.9 KB
/
Copy pathcallback_manager.cpp
File metadata and controls
183 lines (154 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* This file is part of CounterStrikeSharp.
* CounterStrikeSharp is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CounterStrikeSharp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CounterStrikeSharp. If not, see <https://www.gnu.org/licenses/>. *
*/
#include "scripting/callback_manager.h"
#include <algorithm>
#include "core/log.h"
#include "vprof.h"
namespace counterstrikesharp {
ScriptCallback::ScriptCallback(const char* szName) : m_root_context(fxNativeContext{})
{
m_script_context_raw = ScriptContextRaw(m_root_context);
m_name = std::string(szName);
m_profile_name = "ScriptCallback::Execute::" + m_name;
}
ScriptCallback::~ScriptCallback() { m_functions.clear(); }
void ScriptCallback::AddListener(CallbackT fnPluginFunction) { m_functions.push_back(fnPluginFunction); }
bool ScriptCallback::RemoveListener(CallbackT fnPluginFunction)
{
size_t nOriginalSize = m_functions.size();
m_functions.erase(std::ranges::remove(m_functions, fnPluginFunction).begin(), m_functions.end());
return m_functions.size() != nOriginalSize;
}
bool ScriptCallback::IsContextSafe()
{
try
{
auto& Ctx = ScriptContext();
Ctx.GetResult<void*>();
return true;
}
catch (...)
{
CSSHARP_CORE_WARN("Context is invalid (exception during access)");
return false;
}
}
void ScriptCallback::Execute(bool bResetContext)
{
if (!IsContextSafe())
{
ScriptContext().ThrowNativeError("ScriptCallback::Execute aborted due to invalid context");
CSSHARP_CORE_WARN("ScriptCallback::Execute aborted due to invalid context (callback: '{}')", m_name);
return;
}
VPROF_BUDGET(m_profile_name.c_str(), "CS# Script Callbacks");
for (size_t nI = 0; nI < m_functions.size(); ++nI)
{
if (auto fnMethodToCall = m_functions[nI])
{
try
{
fnMethodToCall(&ScriptContextStruct());
}
catch (...)
{
ScriptContext().ThrowNativeError("Exception in callback execution");
CSSHARP_CORE_ERROR("Exception thrown inside callback '{}', index {}", m_name, nI);
}
}
else
{
ScriptContext().ThrowNativeError("Null listener in callback");
CSSHARP_CORE_ERROR("Null function pointer in callback '{}', index {}", m_name, nI);
}
}
if (bResetContext)
{
Reset();
}
}
void ScriptCallback::Reset() { ScriptContext().Reset(); }
CallbackManager::CallbackManager() = default;
ScriptCallback* CallbackManager::CreateCallback(const char* szName)
{
CSSHARP_CORE_TRACE("Creating callback {0}", szName);
auto* pCallback = new ScriptCallback(szName);
m_managed.push_back(pCallback);
return pCallback;
}
ScriptCallback* CallbackManager::FindCallback(const char* szName)
{
for (auto* pMarshal : m_managed)
{
if (strcmp(pMarshal->GetName().c_str(), szName) == 0)
{
return pMarshal;
}
}
return nullptr;
}
void CallbackManager::ReleaseCallback(ScriptCallback* pCallback)
{
auto I = std::ranges::remove_if(m_managed, [pCallback](const ScriptCallback* pI) {
return pCallback == pI;
}).begin();
if (I != m_managed.end()) m_managed.erase(I, m_managed.end());
delete pCallback;
}
bool CallbackManager::TryAddFunction(const char* szName, CallbackT fnCallable)
{
if (auto* pCallback = FindCallback(szName))
{
pCallback->AddListener(fnCallable);
return true;
}
return false;
}
bool CallbackManager::TryRemoveFunction(const char* szName, CallbackT fnCallable)
{
if (auto* pCallback = FindCallback(szName))
{
return pCallback->RemoveListener(fnCallable);
}
return false;
}
void CallbackManager::PrintCallbackDebug()
{
CSSHARP_CORE_INFO("----CALLBACKS----");
for (auto* pCallback : m_managed)
{
CSSHARP_CORE_INFO("{0} ({0})\n", pCallback->GetName().c_str(), 1);
}
}
CallbackPair::CallbackPair()
{
pre = globals::callbackManager.CreateCallback("");
post = globals::callbackManager.CreateCallback("");
}
CallbackPair::CallbackPair(bool bNoCallbacks)
{
if (!bNoCallbacks)
{
pre = globals::callbackManager.CreateCallback("");
post = globals::callbackManager.CreateCallback("");
}
}
CallbackPair::~CallbackPair()
{
globals::callbackManager.ReleaseCallback(pre);
globals::callbackManager.ReleaseCallback(post);
}
} // namespace counterstrikesharp