This repository has been archived by the owner on Sep 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pini.cpp
306 lines (198 loc) · 7.33 KB
/
Pini.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "IniFile.h"
#include "SDK/amx/amx.h"
#include "SDK/plugincommon.h"
typedef void(*logprintf_t)(char* format, ...);
logprintf_t logprintf;
void **ppPluginData;
extern void *pAMXFunctions;
CIniFile *pIniFiles[PINI_MAX_OPENED_FILES];
int iIniSlots[PINI_MAX_OPENED_FILES];
PLUGIN_EXPORT unsigned int PLUGIN_CALL Supports() {
return SUPPORTS_VERSION | SUPPORTS_AMX_NATIVES;
}
int set_amxstring(AMX *amx, cell amx_addr, const char *source, int max) {
cell* dest = (cell *)(amx->base + (int)(((AMX_HEADER *)amx->base)->dat + amx_addr));
cell* start = dest;
while (max--&&*source)
*dest++ = (cell)*source++;
*dest = 0;
return dest - start;
}
// native OpenIniFile(&PINI:Handle, filename[], readwrite = PINI_READWRITE);
static cell AMX_NATIVE_CALL n_OpenIniFile(AMX* amx, cell* params) {
char *szFileName;
amx_StrParam(amx, params[2], szFileName);
int rw = params[3];
for (int i = 0; i < PINI_MAX_OPENED_FILES; i++) {
if (iIniSlots[i] == 0) {
int err = PINI_NO_ERROR;
pIniFiles[i] = new CIniFile(szFileName, rw, err);
if (err != PINI_NO_ERROR) {
cell* cptr;
amx_GetAddr(amx, params[1], &cptr);
*cptr = (cell)(0xFF);
iIniSlots[i] = 0;
delete pIniFiles[i];
}
else {
cell* cptr;
amx_GetAddr(amx, params[1], &cptr);
*cptr = (cell)(i);
iIniSlots[i] = 1;
}
return err;
}
}
return PINI_OPEN_LIMIT;
}
// native GetIniKeysCount(PINI:Handle);
static cell AMX_NATIVE_CALL n_GetIniKeysCount(AMX* amx, cell* params) {
int handle = params[1];
if (handle < 0 || handle >= PINI_MAX_OPENED_FILES) return -1;
if (pIniFiles[handle] == nullptr) return -1;
if (iIniSlots[handle] == 0) return -1;
return pIniFiles[handle]->GetIniKeysCount();
}
// native GetIniKeyExists(PINI:Handle, const key[]);
static cell AMX_NATIVE_CALL n_GetIniKeyExists(AMX* amx, cell* params) {
int handle = params[1];
if (handle < 0 || handle >= PINI_MAX_OPENED_FILES) return -1;
if (pIniFiles[handle] == nullptr) return -1;
if (iIniSlots[handle] == 0) return -1;
char *szKey;
amx_StrParam(amx, params[2], szKey);
return pIniFiles[handle]->GetIniKeyExists(szKey);
}
// native GetIniInt(PINI:Handle, const key[], &value);
static cell AMX_NATIVE_CALL n_GetIniInt(AMX* amx, cell* params) {
int handle = params[1];
if (handle < 0 || handle >= PINI_MAX_OPENED_FILES) return PINI_INVALID_HANDLE;
if (pIniFiles[handle] == nullptr) return PINI_INVALID_HANDLE;
if (iIniSlots[handle] == 0) return PINI_INVALID_HANDLE;
char *szKey;
amx_StrParam(amx, params[2], szKey);
int iRetVal;
int ret = pIniFiles[handle]->GetIniInt(szKey, iRetVal);
if (ret != PINI_NO_ERROR) return ret;
cell* cptr;
amx_GetAddr(amx, params[3], &cptr);
*cptr = (cell)(iRetVal);
return ret;
}
// native GetIniFloat(PINI:Handle, const key[], &Float:value);
static cell AMX_NATIVE_CALL n_GetIniFloat(AMX* amx, cell* params) {
int handle = params[1];
if (handle < 0 || handle >= PINI_MAX_OPENED_FILES) return PINI_INVALID_HANDLE;
if (pIniFiles[handle] == nullptr) return PINI_INVALID_HANDLE;
if (iIniSlots[handle] == 0) return PINI_INVALID_HANDLE;
char *szKey;
amx_StrParam(amx, params[2], szKey);
float fRetVal;
int ret = pIniFiles[handle]->GetIniFloat(szKey, fRetVal);
if (ret != PINI_NO_ERROR) return ret;
cell* cptr;
amx_GetAddr(amx, params[3], &cptr);
*cptr = amx_ftoc(fRetVal);
return ret;
}
// native GetIniString(PINI:Handle, const key[], value[], destsize = sizeof(value));
static cell AMX_NATIVE_CALL n_GetIniString(AMX* amx, cell* params) {
int handle = params[1];
if (handle < 0 || handle >= PINI_MAX_OPENED_FILES) return PINI_INVALID_HANDLE;
if (pIniFiles[handle] == nullptr) return PINI_INVALID_HANDLE;
if (iIniSlots[handle] == 0) return PINI_INVALID_HANDLE;
char *szKey;
amx_StrParam(amx, params[2], szKey);
const char *szValue;
int ret = pIniFiles[handle]->GetIniString(szKey, szValue);
if (ret != PINI_NO_ERROR) return ret;
set_amxstring(amx, params[3], szValue, params[4]);
return PINI_NO_ERROR;
}
// native SetIniInt(PINI:Handle, const key[], value);
static cell AMX_NATIVE_CALL n_SetIniInt(AMX* amx, cell* params) {
int handle = params[1];
if (handle < 0 || handle >= PINI_MAX_OPENED_FILES) return PINI_INVALID_HANDLE;
if (pIniFiles[handle] == nullptr) return PINI_INVALID_HANDLE;
if (iIniSlots[handle] == 0) return PINI_INVALID_HANDLE;
char *szKey;
amx_StrParam(amx, params[2], szKey);
int ret = pIniFiles[handle]->SetIniInt(szKey, params[3]);
return ret;
}
// native SetIniFloat(PINI:Handle, const key[], Float:value);
static cell AMX_NATIVE_CALL n_SetIniFloat(AMX* amx, cell* params) {
int handle = params[1];
if (handle < 0 || handle >= PINI_MAX_OPENED_FILES) return PINI_INVALID_HANDLE;
if (pIniFiles[handle] == nullptr) return PINI_INVALID_HANDLE;
if (iIniSlots[handle] == 0) return PINI_INVALID_HANDLE;
char *szKey;
amx_StrParam(amx, params[2], szKey);
int ret = pIniFiles[handle]->SetIniFloat(szKey, amx_ctof(params[3]));
return ret;
}
// native SetIniString(PINI:Handle, const key[], value[]);
static cell AMX_NATIVE_CALL n_SetIniString(AMX* amx, cell* params) {
int handle = params[1];
if (handle < 0 || handle >= PINI_MAX_OPENED_FILES) return PINI_INVALID_HANDLE;
if (pIniFiles[handle] == nullptr) return PINI_INVALID_HANDLE;
if (iIniSlots[handle] == 0) return PINI_INVALID_HANDLE;
char *szKey;
char *szValue;
amx_StrParam(amx, params[2], szKey);
amx_StrParam(amx, params[3], szValue);
int ret = pIniFiles[handle]->SetIniString(szKey, szValue);
return ret;
}
// native CloseIniFile(PINI:Handle);
static cell AMX_NATIVE_CALL n_CloseIniFile(AMX* amx, cell* params) {
int handle = params[1];
if (handle < 0 || handle >= PINI_MAX_OPENED_FILES) return PINI_INVALID_HANDLE;
if (pIniFiles[handle] == nullptr) return PINI_INVALID_HANDLE;
if (iIniSlots[handle] == 0) return PINI_INVALID_HANDLE;
delete pIniFiles[handle];
iIniSlots[handle] = 0;
return PINI_NO_ERROR;
}
// native RemoveIniKey(PINI:Handle, const key[]);
static cell AMX_NATIVE_CALL n_RemoveIniKey(AMX* amx, cell* params) {
int handle = params[1];
if (handle < 0 || handle >= PINI_MAX_OPENED_FILES) return PINI_INVALID_HANDLE;
if (pIniFiles[handle] == nullptr) return PINI_INVALID_HANDLE;
if (iIniSlots[handle] == 0) return PINI_INVALID_HANDLE;
char *szKey;
amx_StrParam(amx, params[2], szKey);
return pIniFiles[handle]->RemoveIniKey(szKey);
}
PLUGIN_EXPORT bool PLUGIN_CALL Load( void **ppData ) {
memset(iIniSlots, 0, sizeof(iIniSlots));
pAMXFunctions = ppData[PLUGIN_DATA_AMX_EXPORTS];
logprintf = (logprintf_t)ppData[PLUGIN_DATA_LOGPRINTF];
logprintf(" Pini plugin by povargek loaded! (http://vk.com/povargek)\n");
return true;
}
PLUGIN_EXPORT void PLUGIN_CALL Unload() {
logprintf(" Pini plugin by povargek unloaded! (http://vk.com/povargek)\n");
}
AMX_NATIVE_INFO pluginNatives[] = {
{ "OpenIniFile", n_OpenIniFile },
{ "GetIniKeysCount", n_GetIniKeysCount },
{ "GetIniKeyExists", n_GetIniKeyExists },
{ "GetIniInt", n_GetIniInt },
{ "GetIniFloat", n_GetIniFloat },
{ "GetIniString", n_GetIniString },
{ "SetIniInt", n_SetIniInt },
{ "SetIniFloat", n_SetIniFloat },
{ "SetIniString", n_SetIniString },
{ "CloseIniFile", n_CloseIniFile },
{ "RemoveIniKey", n_RemoveIniKey },
{ 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;
}
PLUGIN_EXPORT void PLUGIN_CALL ProcessTick() {
}