2 * ===============================================================
3 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
4 * ===============================================================
6 * This file is not open source and may not be copied without explicit
7 * written permission of AlliedModders LLC. This file may not be redistributed
8 * in whole or significant part.
9 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
14 #include "sm_globals.h"
15 #include "TextParsers.h"
16 #include "HandleSys.h"
18 HandleType_t g_TypeSMC = 0;
20 class ParseInfo : public ITextListener_SMC
34 void ReadSMC_ParseStart()
39 parse_start->PushCell(handle);
40 parse_start->Execute(&result);
44 void ReadSMC_ParseEnd(bool halted, bool failed)
49 parse_start->PushCell(handle);
50 parse_end->PushCell(halted ? 1 : 0);
51 parse_end->PushCell(failed ? 1 : 0);
52 parse_end->Execute(&result);
56 SMCParseResult ReadSMC_NewSection(const char *name, bool opt_quotes)
58 cell_t result = SMCParse_Continue;
62 new_section->PushCell(handle);
63 new_section->PushString(name);
64 new_section->PushCell(opt_quotes ? 1 : 0);
65 new_section->Execute(&result);
68 return (SMCParseResult)result;
71 SMCParseResult ReadSMC_KeyValue(const char *key, const char *value, bool key_quotes, bool value_quotes)
73 cell_t result = SMCParse_Continue;
77 key_value->PushCell(handle);
78 key_value->PushString(key);
79 key_value->PushString(value);
80 key_value->PushCell(key_quotes ? 1 : 0);
81 key_value->PushCell(value_quotes ? 1 : 0);
82 key_value->Execute(&result);
85 return (SMCParseResult)result;
88 SMCParseResult ReadSMC_LeavingSection()
90 cell_t result = SMCParse_Continue;
94 end_section->PushCell(handle);
95 end_section->Execute(&result);
98 return (SMCParseResult)result;
101 SMCParseResult ReadSMC_RawLine(const char *line, unsigned int curline)
103 cell_t result = SMCParse_Continue;
107 raw_line->PushCell(handle);
108 raw_line->PushString(line);
109 raw_line->PushCell(curline);
110 raw_line->Execute(&result);
113 return (SMCParseResult)result;
116 IPluginFunction *parse_start;
117 IPluginFunction *parse_end;
118 IPluginFunction *new_section;
119 IPluginFunction *key_value;
120 IPluginFunction *end_section;
121 IPluginFunction *raw_line;
125 class TextParseGlobals :
126 public SMGlobalClass,
127 public IHandleTypeDispatch
130 void OnSourceModAllInitialized()
134 /* These cannot be cloned, because they are locked to a specific plugin.
135 * However, we let anyone read them because we don't care.
137 g_HandleSys.InitAccessDefaults(NULL, &sec);
138 sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY;
139 sec.access[HandleAccess_Read] = 0;
141 g_TypeSMC = g_HandleSys.CreateType("SMCParser", this, 0, NULL, &sec, g_pCoreIdent, NULL);
144 void OnSourceModShutdown()
146 g_HandleSys.RemoveType(g_TypeSMC, g_pCoreIdent);
149 void OnHandleDestroy(HandleType_t type, void *object)
151 ParseInfo *parse = (ParseInfo *)object;
156 TextParseGlobals g_TextParseGlobals;
158 static cell_t SMC_CreateParser(IPluginContext *pContext, const cell_t *params)
160 ParseInfo *pInfo = new ParseInfo();
162 Handle_t hndl = g_HandleSys.CreateHandle(g_TypeSMC, pInfo, pContext->GetIdentity(), g_pCoreIdent, NULL);
164 /* Should never happen */
171 pInfo->handle = hndl;
176 static cell_t SMC_SetParseStart(IPluginContext *pContext, const cell_t *params)
178 Handle_t hndl = (Handle_t)params[1];
182 if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse))
185 return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
188 parse->parse_start = pContext->GetFunctionById((funcid_t)params[2]);
193 static cell_t SMC_SetParseEnd(IPluginContext *pContext, const cell_t *params)
195 Handle_t hndl = (Handle_t)params[1];
199 if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse))
202 return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
205 parse->parse_end = pContext->GetFunctionById((funcid_t)params[2]);
210 static cell_t SMC_SetReaders(IPluginContext *pContext, const cell_t *params)
212 Handle_t hndl = (Handle_t)params[1];
216 if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse))
219 return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
222 parse->new_section = pContext->GetFunctionById((funcid_t)params[2]);
223 parse->key_value = pContext->GetFunctionById((funcid_t)params[3]);
224 parse->end_section = pContext->GetFunctionById((funcid_t)params[4]);
229 static cell_t SMC_SetRawLine(IPluginContext *pContext, const cell_t *params)
231 Handle_t hndl = (Handle_t)params[1];
235 if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse))
238 return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
241 parse->raw_line = pContext->GetFunctionById((funcid_t)params[2]);
246 static cell_t SMC_ParseFile(IPluginContext *pContext, const cell_t *params)
248 Handle_t hndl = (Handle_t)params[1];
252 if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse))
255 return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
259 pContext->LocalToString(params[2], &file);
261 char path[PLATFORM_MAX_PATH];
262 g_SourceMod.BuildPath(Path_Game, path, sizeof(path), "%s", file);
264 unsigned int line = 0, col = 0;
265 SMCParseError p_err = g_TextParser.ParseFile_SMC(path, parse, &line, &col);
267 cell_t *c_line, *c_col;
268 pContext->LocalToPhysAddr(params[3], &c_line);
269 pContext->LocalToPhysAddr(params[4], &c_col);
274 return (cell_t)p_err;
277 static cell_t SMC_GetErrorString(IPluginContext *pContext, const cell_t *params)
279 const char *str = g_TextParser.GetSMCErrorString((SMCParseError)params[1]);
286 pContext->StringToLocal(params[2], params[3], str);
291 REGISTER_NATIVES(textNatives)
293 {"SMC_CreateParser", SMC_CreateParser},
294 {"SMC_ParseFile", SMC_ParseFile},
295 {"SMC_GetErrorString", SMC_GetErrorString},
296 {"SMC_SetParseStart", SMC_SetParseStart},
297 {"SMC_SetParseEnd", SMC_SetParseEnd},
298 {"SMC_SetReaders", SMC_SetReaders},
299 {"SMC_SetRawLine", SMC_SetRawLine},