|
ds@1952
|
1 |
/** |
|
ds@1952
|
2 |
* vim: set ts=4 : |
|
ds@1952
|
3 |
* ============================================================================= |
|
ds@1952
|
4 |
* SourceMod |
|
ds@1952
|
5 |
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. |
|
ds@1952
|
6 |
* ============================================================================= |
|
ds@1952
|
7 |
* |
|
ds@1952
|
8 |
* This program is free software; you can redistribute it and/or modify it under |
|
ds@1952
|
9 |
* the terms of the GNU General Public License, version 3.0, as published by the |
|
ds@1952
|
10 |
* Free Software Foundation. |
|
ds@1952
|
11 |
* |
|
ds@1952
|
12 |
* This program is distributed in the hope that it will be useful, but WITHOUT |
|
ds@1952
|
13 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
ds@1952
|
14 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
|
ds@1952
|
15 |
* details. |
|
ds@1952
|
16 |
* |
|
ds@1952
|
17 |
* You should have received a copy of the GNU General Public License along with |
|
ds@1952
|
18 |
* this program. If not, see <http://www.gnu.org/licenses/>. |
|
ds@1952
|
19 |
* |
|
ds@1952
|
20 |
* As a special exception, AlliedModders LLC gives you permission to link the |
|
ds@1952
|
21 |
* code of this program (as well as its derivative works) to "Half-Life 2," the |
|
ds@1952
|
22 |
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software |
|
ds@1952
|
23 |
* by the Valve Corporation. You must obey the GNU General Public License in |
|
ds@1952
|
24 |
* all respects for all other code used. Additionally, AlliedModders LLC grants |
|
ds@1952
|
25 |
* this exception to all derivative works. AlliedModders LLC defines further |
|
ds@1952
|
26 |
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), |
|
ds@1952
|
27 |
* or <http://www.sourcemod.net/license.php>. |
|
ds@1952
|
28 |
* |
|
ds@1952
|
29 |
* Version: $Id$ |
|
ds@1952
|
30 |
*/ |
|
ds@1952
|
31 |
|
|
ds@1952
|
32 |
#include "sm_globals.h" |
|
ds@1952
|
33 |
#include <ITextParsers.h> |
|
ds@1952
|
34 |
#include "HandleSys.h" |
|
ds@1952
|
35 |
|
|
ds@1952
|
36 |
HandleType_t g_TypeSMC = 0; |
|
ds@1952
|
37 |
|
|
ds@1952
|
38 |
class ParseInfo : public ITextListener_SMC |
|
ds@1952
|
39 |
{ |
|
ds@1952
|
40 |
public: |
|
ds@1952
|
41 |
ParseInfo() |
|
ds@1952
|
42 |
{ |
|
ds@1952
|
43 |
parse_start = NULL; |
|
ds@1952
|
44 |
parse_end = NULL; |
|
ds@1952
|
45 |
new_section = NULL; |
|
ds@1952
|
46 |
key_value = NULL; |
|
ds@1952
|
47 |
end_section = NULL; |
|
ds@1952
|
48 |
raw_line = NULL; |
|
ds@1952
|
49 |
handle = 0; |
|
ds@1952
|
50 |
} |
|
ds@1952
|
51 |
public: |
|
ds@1952
|
52 |
void ReadSMC_ParseStart() |
|
ds@1952
|
53 |
{ |
|
ds@1952
|
54 |
if (parse_start) |
|
ds@1952
|
55 |
{ |
|
ds@1952
|
56 |
cell_t result; |
|
ds@1952
|
57 |
parse_start->PushCell(handle); |
|
ds@1952
|
58 |
parse_start->Execute(&result); |
|
ds@1952
|
59 |
} |
|
ds@1952
|
60 |
} |
|
ds@1952
|
61 |
|
|
ds@1952
|
62 |
void ReadSMC_ParseEnd(bool halted, bool failed) |
|
ds@1952
|
63 |
{ |
|
ds@1952
|
64 |
if (parse_end) |
|
ds@1952
|
65 |
{ |
|
ds@1952
|
66 |
cell_t result; |
|
ds@1952
|
67 |
parse_end->PushCell(handle); |
|
ds@1952
|
68 |
parse_end->PushCell(halted ? 1 : 0); |
|
ds@1952
|
69 |
parse_end->PushCell(failed ? 1 : 0); |
|
ds@1952
|
70 |
parse_end->Execute(&result); |
|
ds@1952
|
71 |
} |
|
ds@1952
|
72 |
} |
|
ds@1952
|
73 |
|
|
ds@1952
|
74 |
SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name) |
|
ds@1952
|
75 |
{ |
|
ds@1952
|
76 |
cell_t result = SMCResult_Continue; |
|
ds@1952
|
77 |
|
|
ds@1952
|
78 |
if (new_section) |
|
ds@1952
|
79 |
{ |
|
ds@1952
|
80 |
new_section->PushCell(handle); |
|
ds@1952
|
81 |
new_section->PushString(name); |
|
ds@1952
|
82 |
new_section->PushCell(1); |
|
ds@1952
|
83 |
new_section->Execute(&result); |
|
ds@1952
|
84 |
} |
|
ds@1952
|
85 |
|
|
ds@1952
|
86 |
return (SMCResult)result; |
|
ds@1952
|
87 |
} |
|
ds@1952
|
88 |
|
|
ds@1952
|
89 |
SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value) |
|
ds@1952
|
90 |
{ |
|
ds@1952
|
91 |
cell_t result = SMCResult_Continue; |
|
ds@1952
|
92 |
|
|
ds@1952
|
93 |
if (key_value) |
|
ds@1952
|
94 |
{ |
|
ds@1952
|
95 |
key_value->PushCell(handle); |
|
ds@1952
|
96 |
key_value->PushString(key); |
|
ds@1952
|
97 |
key_value->PushString(value); |
|
ds@1952
|
98 |
key_value->PushCell(1); |
|
ds@1952
|
99 |
key_value->PushCell(1); |
|
ds@1952
|
100 |
key_value->Execute(&result); |
|
ds@1952
|
101 |
} |
|
ds@1952
|
102 |
|
|
ds@1952
|
103 |
return (SMCResult)result; |
|
ds@1952
|
104 |
} |
|
ds@1952
|
105 |
|
|
ds@1952
|
106 |
SMCResult ReadSMC_LeavingSection(const SMCStates *states) |
|
ds@1952
|
107 |
{ |
|
ds@1952
|
108 |
cell_t result = SMCResult_Continue; |
|
ds@1952
|
109 |
|
|
ds@1952
|
110 |
if (end_section) |
|
ds@1952
|
111 |
{ |
|
ds@1952
|
112 |
end_section->PushCell(handle); |
|
ds@1952
|
113 |
end_section->Execute(&result); |
|
ds@1952
|
114 |
} |
|
ds@1952
|
115 |
|
|
ds@1952
|
116 |
return (SMCResult)result; |
|
ds@1952
|
117 |
} |
|
ds@1952
|
118 |
|
|
ds@1952
|
119 |
SMCResult ReadSMC_RawLine(const SMCStates *states, const char *line) |
|
ds@1952
|
120 |
{ |
|
ds@1952
|
121 |
cell_t result = SMCResult_Continue; |
|
ds@1952
|
122 |
|
|
ds@1952
|
123 |
if (raw_line) |
|
ds@1952
|
124 |
{ |
|
ds@1952
|
125 |
raw_line->PushCell(handle); |
|
ds@1952
|
126 |
raw_line->PushString(line); |
|
ds@1952
|
127 |
raw_line->PushCell(states->line); |
|
ds@1952
|
128 |
raw_line->Execute(&result); |
|
ds@1952
|
129 |
} |
|
ds@1952
|
130 |
|
|
ds@1952
|
131 |
return (SMCResult)result; |
|
ds@1952
|
132 |
} |
|
ds@1952
|
133 |
public: |
|
ds@1952
|
134 |
IPluginFunction *parse_start; |
|
ds@1952
|
135 |
IPluginFunction *parse_end; |
|
ds@1952
|
136 |
IPluginFunction *new_section; |
|
ds@1952
|
137 |
IPluginFunction *key_value; |
|
ds@1952
|
138 |
IPluginFunction *end_section; |
|
ds@1952
|
139 |
IPluginFunction *raw_line; |
|
ds@1952
|
140 |
Handle_t handle; |
|
ds@1952
|
141 |
}; |
|
ds@1952
|
142 |
|
|
ds@1952
|
143 |
class TextParseGlobals : |
|
ds@1952
|
144 |
public SMGlobalClass, |
|
ds@1952
|
145 |
public IHandleTypeDispatch |
|
ds@1952
|
146 |
{ |
|
ds@1952
|
147 |
public: |
|
ds@1952
|
148 |
void OnSourceModAllInitialized() |
|
ds@1952
|
149 |
{ |
|
ds@1952
|
150 |
HandleAccess sec; |
|
ds@1952
|
151 |
|
|
ds@1952
|
152 |
/* These cannot be cloned, because they are locked to a specific plugin. |
|
ds@1952
|
153 |
* However, we let anyone read them because we don't care. |
|
ds@1952
|
154 |
*/ |
|
ds@1952
|
155 |
g_HandleSys.InitAccessDefaults(NULL, &sec); |
|
ds@1952
|
156 |
sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY; |
|
ds@1952
|
157 |
sec.access[HandleAccess_Read] = 0; |
|
ds@1952
|
158 |
|
|
ds@1952
|
159 |
g_TypeSMC = g_HandleSys.CreateType("SMCParser", this, 0, NULL, &sec, g_pCoreIdent, NULL); |
|
ds@1952
|
160 |
} |
|
ds@1952
|
161 |
|
|
ds@1952
|
162 |
void OnSourceModShutdown() |
|
ds@1952
|
163 |
{ |
|
ds@1952
|
164 |
g_HandleSys.RemoveType(g_TypeSMC, g_pCoreIdent); |
|
ds@1952
|
165 |
} |
|
ds@1952
|
166 |
|
|
ds@1952
|
167 |
void OnHandleDestroy(HandleType_t type, void *object) |
|
ds@1952
|
168 |
{ |
|
ds@1952
|
169 |
ParseInfo *parse = (ParseInfo *)object; |
|
ds@1952
|
170 |
delete parse; |
|
ds@1952
|
171 |
} |
|
ds@1952
|
172 |
|
|
ds@1952
|
173 |
bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize) |
|
ds@1952
|
174 |
{ |
|
ds@1952
|
175 |
*pSize = sizeof(ParseInfo); |
|
ds@1952
|
176 |
return true; |
|
ds@1952
|
177 |
} |
|
ds@1952
|
178 |
}; |
|
ds@1952
|
179 |
|
|
ds@1952
|
180 |
TextParseGlobals g_TextParseGlobals; |
|
ds@1952
|
181 |
|
|
ds@1952
|
182 |
static cell_t SMC_CreateParser(IPluginContext *pContext, const cell_t *params) |
|
ds@1952
|
183 |
{ |
|
ds@1952
|
184 |
ParseInfo *pInfo = new ParseInfo(); |
|
ds@1952
|
185 |
|
|
ds@1952
|
186 |
Handle_t hndl = g_HandleSys.CreateHandle(g_TypeSMC, pInfo, pContext->GetIdentity(), g_pCoreIdent, NULL); |
|
ds@1952
|
187 |
|
|
ds@1952
|
188 |
/* Should never happen */ |
|
ds@1952
|
189 |
if (!hndl) |
|
ds@1952
|
190 |
{ |
|
ds@1952
|
191 |
delete pInfo; |
|
ds@1952
|
192 |
return 0; |
|
ds@1952
|
193 |
} |
|
ds@1952
|
194 |
|
|
ds@1952
|
195 |
pInfo->handle = hndl; |
|
ds@1952
|
196 |
|
|
ds@1952
|
197 |
return hndl; |
|
ds@1952
|
198 |
} |
|
ds@1952
|
199 |
|
|
ds@1952
|
200 |
static cell_t SMC_SetParseStart(IPluginContext *pContext, const cell_t *params) |
|
ds@1952
|
201 |
{ |
|
ds@1952
|
202 |
Handle_t hndl = (Handle_t)params[1]; |
|
ds@1952
|
203 |
HandleError err; |
|
ds@1952
|
204 |
ParseInfo *parse; |
|
ds@1952
|
205 |
|
|
ds@1952
|
206 |
if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse)) |
|
ds@1952
|
207 |
!= HandleError_None) |
|
ds@1952
|
208 |
{ |
|
ds@1952
|
209 |
return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err); |
|
ds@1952
|
210 |
} |
|
ds@1952
|
211 |
|
|
ds@1952
|
212 |
parse->parse_start = pContext->GetFunctionById((funcid_t)params[2]); |
|
ds@1952
|
213 |
|
|
ds@1952
|
214 |
return 1; |
|
ds@1952
|
215 |
} |
|
ds@1952
|
216 |
|
|
ds@1952
|
217 |
static cell_t SMC_SetParseEnd(IPluginContext *pContext, const cell_t *params) |
|
ds@1952
|
218 |
{ |
|
ds@1952
|
219 |
Handle_t hndl = (Handle_t)params[1]; |
|
ds@1952
|
220 |
HandleError err; |
|
ds@1952
|
221 |
ParseInfo *parse; |
|
ds@1952
|
222 |
|
|
ds@1952
|
223 |
if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse)) |
|
ds@1952
|
224 |
!= HandleError_None) |
|
ds@1952
|
225 |
{ |
|
ds@1952
|
226 |
return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err); |
|
ds@1952
|
227 |
} |
|
ds@1952
|
228 |
|
|
ds@1952
|
229 |
parse->parse_end = pContext->GetFunctionById((funcid_t)params[2]); |
|
ds@1952
|
230 |
|
|
ds@1952
|
231 |
return 1; |
|
ds@1952
|
232 |
} |
|
ds@1952
|
233 |
|
|
ds@1952
|
234 |
static cell_t SMC_SetReaders(IPluginContext *pContext, const cell_t *params) |
|
ds@1952
|
235 |
{ |
|
ds@1952
|
236 |
Handle_t hndl = (Handle_t)params[1]; |
|
ds@1952
|
237 |
HandleError err; |
|
ds@1952
|
238 |
ParseInfo *parse; |
|
ds@1952
|
239 |
|
|
ds@1952
|
240 |
if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse)) |
|
ds@1952
|
241 |
!= HandleError_None) |
|
ds@1952
|
242 |
{ |
|
ds@1952
|
243 |
return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err); |
|
ds@1952
|
244 |
} |
|
ds@1952
|
245 |
|
|
ds@1952
|
246 |
parse->new_section = pContext->GetFunctionById((funcid_t)params[2]); |
|
ds@1952
|
247 |
parse->key_value = pContext->GetFunctionById((funcid_t)params[3]); |
|
ds@1952
|
248 |
parse->end_section = pContext->GetFunctionById((funcid_t)params[4]); |
|
ds@1952
|
249 |
|
|
ds@1952
|
250 |
return 1; |
|
ds@1952
|
251 |
} |
|
ds@1952
|
252 |
|
|
ds@1952
|
253 |
static cell_t SMC_SetRawLine(IPluginContext *pContext, const cell_t *params) |
|
ds@1952
|
254 |
{ |
|
ds@1952
|
255 |
Handle_t hndl = (Handle_t)params[1]; |
|
ds@1952
|
256 |
HandleError err; |
|
ds@1952
|
257 |
ParseInfo *parse; |
|
ds@1952
|
258 |
|
|
ds@1952
|
259 |
if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse)) |
|
ds@1952
|
260 |
!= HandleError_None) |
|
ds@1952
|
261 |
{ |
|
ds@1952
|
262 |
return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err); |
|
ds@1952
|
263 |
} |
|
ds@1952
|
264 |
|
|
ds@1952
|
265 |
parse->raw_line = pContext->GetFunctionById((funcid_t)params[2]); |
|
ds@1952
|
266 |
|
|
ds@1952
|
267 |
return 1; |
|
ds@1952
|
268 |
} |
|
ds@1952
|
269 |
|
|
ds@1952
|
270 |
static cell_t SMC_ParseFile(IPluginContext *pContext, const cell_t *params) |
|
ds@1952
|
271 |
{ |
|
ds@1952
|
272 |
Handle_t hndl = (Handle_t)params[1]; |
|
ds@1952
|
273 |
HandleError err; |
|
ds@1952
|
274 |
ParseInfo *parse; |
|
ds@1952
|
275 |
|
|
ds@1952
|
276 |
if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse)) |
|
ds@1952
|
277 |
!= HandleError_None) |
|
ds@1952
|
278 |
{ |
|
ds@1952
|
279 |
return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err); |
|
ds@1952
|
280 |
} |
|
ds@1952
|
281 |
|
|
ds@1952
|
282 |
char *file; |
|
ds@1952
|
283 |
pContext->LocalToString(params[2], &file); |
|
ds@1952
|
284 |
|
|
ds@1952
|
285 |
char path[PLATFORM_MAX_PATH]; |
|
ds@1952
|
286 |
g_SourceMod.BuildPath(Path_Game, path, sizeof(path), "%s", file); |
|
ds@1952
|
287 |
|
|
ds@1952
|
288 |
SMCStates states; |
|
ds@1952
|
289 |
SMCError p_err = textparsers->ParseFile_SMC(path, parse, &states); |
|
ds@1952
|
290 |
|
|
ds@1952
|
291 |
cell_t *c_line, *c_col; |
|
ds@1952
|
292 |
pContext->LocalToPhysAddr(params[3], &c_line); |
|
ds@1952
|
293 |
pContext->LocalToPhysAddr(params[4], &c_col); |
|
ds@1952
|
294 |
|
|
ds@1952
|
295 |
*c_line = states.line; |
|
ds@1952
|
296 |
*c_col = states.col; |
|
ds@1952
|
297 |
|
|
ds@1952
|
298 |
return (cell_t)p_err; |
|
ds@1952
|
299 |
} |
|
ds@1952
|
300 |
|
|
ds@1952
|
301 |
static cell_t SMC_GetErrorString(IPluginContext *pContext, const cell_t *params) |
|
ds@1952
|
302 |
{ |
|
ds@1952
|
303 |
const char *str = textparsers->GetSMCErrorString((SMCError)params[1]); |
|
ds@1952
|
304 |
|
|
ds@1952
|
305 |
if (!str) |
|
ds@1952
|
306 |
{ |
|
ds@1952
|
307 |
return 0; |
|
ds@1952
|
308 |
} |
|
ds@1952
|
309 |
|
|
ds@1952
|
310 |
pContext->StringToLocal(params[2], params[3], str); |
|
ds@1952
|
311 |
|
|
ds@1952
|
312 |
return 1; |
|
ds@1952
|
313 |
} |
|
ds@1952
|
314 |
|
|
ds@1952
|
315 |
REGISTER_NATIVES(textNatives) |
|
ds@1952
|
316 |
{ |
|
ds@1952
|
317 |
{"SMC_CreateParser", SMC_CreateParser}, |
|
ds@1952
|
318 |
{"SMC_ParseFile", SMC_ParseFile}, |
|
ds@1952
|
319 |
{"SMC_GetErrorString", SMC_GetErrorString}, |
|
ds@1952
|
320 |
{"SMC_SetParseStart", SMC_SetParseStart}, |
|
ds@1952
|
321 |
{"SMC_SetParseEnd", SMC_SetParseEnd}, |
|
ds@1952
|
322 |
{"SMC_SetReaders", SMC_SetReaders}, |
|
ds@1952
|
323 |
{"SMC_SetRawLine", SMC_SetRawLine}, |
|
ds@1952
|
324 |
{NULL, NULL}, |
|
ds@1952
|
325 |
}; |