-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.cpp
61 lines (46 loc) · 1.5 KB
/
extension.cpp
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
#include <src/jansson.h>
#include <src/natives.h>
#include "extension.h"
CJanssonExtension g_JanssonExtension;
nJansson::IJansson* pJansson;
SMEXT_LINK(&g_JanssonExtension)
void CJanssonExtension::OnHandleTypeDispatchDestroy(const char* type, SourceMod::IHandleTypeDispatch* pDispatch) {
if(strcmp(type, "Json") == 0)
delete (CJsonHandler*) pDispatch;
}
bool CJanssonExtension::SDK_OnLoad(char *error, size_t maxlength, bool late)
{
m_pJsonType = new nJansson::CHandleType (
"Json",
new CJsonHandler(),
OnHandleTypeDispatchDestroy,
0,
nullptr,
nullptr,
myself->GetIdentity()
);
sharesys->AddNatives(myself, JSON_NATIVES);
sharesys->AddNatives(myself, JSON_OBJECT_NATIVES);
sharesys->AddNatives(myself, JSON_ARRAY_NATIVES);
sharesys->RegisterLibrary(myself, "jansson");
if(pJansson != nullptr)
delete (nJansson::Jansson *)pJansson;
pJansson = new nJansson::Jansson();
if(!pJansson->types()->add(m_pJsonType))
{
sprintf(error, "Failed on type '%s' registration", m_pJsonType->name());
return false;
}
return sharesys->AddInterface(myself, pJansson);
}
void CJanssonExtension::SDK_OnUnload()
{
delete m_pJsonType;
delete (nJansson::Jansson*) pJansson;
}
void CJanssonExtension::SDK_OnAllLoaded() {
// pass
}
void CJsonHandler::OnHandleDestroy(SourceMod::HandleType_t type, void *object) {
pJansson->close(static_cast<nJansson::IJson *>(object));
}