-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
170 lines (139 loc) · 3.86 KB
/
main.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
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
#include <iostream>
#include <string>
#include <vector>
#include "json_spirit/json_spirit.h"
#include "amx/amx.h"
#include "plugincommon.h"
#include "settings.h"
#include "AmxUtils.h"
typedef void(*logprintf_t)(char* format, ...);
logprintf_t logprintf;
extern void *pAMXFunctions;
struct t_JsonData
{
int id;
json_spirit::Object object;
};
std::vector<t_JsonData> json_data;
int json_data_id = 0;
cell AMX_NATIVE_CALL json_decode(AMX* amx, cell* params)
{
std::string json_string = AmxUtils::amx_GetStdString(amx, ¶ms[1]);
json_spirit::Value value;
auto success = json_spirit::read(json_string, value);
if (success == false)
return 0;
json_data_id++;
json_data.push_back({ json_data_id, value.get_obj() });
return json_data_id;
}
cell AMX_NATIVE_CALL json_get_int(AMX* amx, cell* params)
{
int data_id = params[1];
std::string key = AmxUtils::amx_GetStdString(amx, ¶ms[2]);
for (auto &x : json_data)
{
if (x.id == data_id)
{
for (auto &i : x.object)
{
if (i.name_ == key && i.value_.type() == json_spirit::int_type)
{
return i.value_.get_int();
}
}
}
}
return -1;
}
cell AMX_NATIVE_CALL json_get_string(AMX* amx, cell* params)
{
int data_id = params[1];
std::string key = AmxUtils::amx_GetStdString(amx, ¶ms[2]);
for (auto &x : json_data)
{
if (x.id == data_id)
{
for (auto &i : x.object)
{
if (i.name_ == key && i.value_.type() == json_spirit::str_type)
{
cell *addr = NULL;
amx_GetAddr(amx, params[3], &addr);
amx_SetString(addr, i.value_.get_str().c_str(), 0, 0, params[4]);
return 1;
}
}
}
}
return -1;
}
cell AMX_NATIVE_CALL json_get_float(AMX* amx, cell* params)
{
int data_id = params[1];
std::string key = AmxUtils::amx_GetStdString(amx, ¶ms[2]);
for (auto &x : json_data)
{
if (x.id == data_id)
{
for (auto &i : x.object)
{
if (i.name_ == key && i.value_.type() == json_spirit::real_type)
{
const float value = (float)i.value_.get_real();
return amx_ftoc(value);
}
}
}
}
return -1.0;
}
cell AMX_NATIVE_CALL json_delete(AMX* amx, cell* params)
{
int data_id = params[1], index = 0;
for (auto &i : json_data)
{
if (i.id == data_id)
{
json_data.erase(json_data.begin()+index);
return 1;
}
index++;
}
return 0;
}
PLUGIN_EXPORT unsigned int PLUGIN_CALL Supports()
{
return SUPPORTS_VERSION | SUPPORTS_AMX_NATIVES;
}
PLUGIN_EXPORT bool PLUGIN_CALL Load(void **ppData)
{
pAMXFunctions = ppData[PLUGIN_DATA_AMX_EXPORTS];
logprintf = (logprintf_t)ppData[PLUGIN_DATA_LOGPRINTF];
logprintf(" -----------------------------------------------------------------");
logprintf(" SA:MP JSON plugin version %s successfully loaded!", PLUGIN_VERSION);
logprintf(" Developer: Manuel Schnitzer");
logprintf(" -----------------------------------------------------------------");
return true;
}
PLUGIN_EXPORT void PLUGIN_CALL Unload()
{
logprintf(" * SA:MP JSON plugin was unloaded.");
}
extern "C" const AMX_NATIVE_INFO PluginNatives[] =
{
{ "json_decode", json_decode },
{ "json_get_int", json_get_int },
{ "json_get_string", json_get_string },
{ "json_get_float", json_get_float },
{ "json_delete", json_delete },
{ 0, 0 }
};
PLUGIN_EXPORT int PLUGIN_CALL AmxLoad(AMX *amx)
{
return amx_Register(amx, PluginNatives, -1);
}
PLUGIN_EXPORT int PLUGIN_CALL AmxUnload(AMX *amx)
{
return AMX_ERR_NONE;
}