core/smn_textparse.cpp
author Scott Ehlert <ds@alliedmods.net>
Wed Jun 06 20:42:46 2007 +0000 (2007-06-06)
changeset 891 30343d215489
parent 747 7a3ec26230b1
child 1231 17badaf66ce4
permissions -rw-r--r--
Changed a few natives/stocks that should have used IsClientInGame() rather than IsClientConnected()
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5  * ===============================================================
     6  *
     7  * This file is not open source and may not be copied without explicit
     8  * written permission of AlliedModders LLC.  This file may not be redistributed 
     9  * in whole or significant part.
    10  * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    11  *
    12  * Version: $Id$
    13  */
    14 
    15 #include "sm_globals.h"
    16 #include "TextParsers.h"
    17 #include "HandleSys.h"
    18 
    19 HandleType_t g_TypeSMC = 0;
    20 
    21 class ParseInfo : public ITextListener_SMC
    22 {
    23 public:
    24 	ParseInfo()
    25 	{
    26 		parse_start = NULL;
    27 		parse_end = NULL;
    28 		new_section = NULL;
    29 		key_value = NULL;
    30 		end_section = NULL;
    31 		raw_line = NULL;
    32 		handle = 0;
    33 	}
    34 public:
    35 	void ReadSMC_ParseStart()
    36 	{
    37 		if (parse_start)
    38 		{
    39 			cell_t result;
    40 			parse_start->PushCell(handle);
    41 			parse_start->Execute(&result);
    42 		}
    43 	}
    44 
    45 	void ReadSMC_ParseEnd(bool halted, bool failed)
    46 	{
    47 		if (parse_end)
    48 		{
    49 			cell_t result;
    50 			parse_end->PushCell(handle);
    51 			parse_end->PushCell(halted ? 1 : 0);
    52 			parse_end->PushCell(failed ? 1 : 0);
    53 			parse_end->Execute(&result);
    54 		}
    55 	}
    56 
    57 	SMCParseResult ReadSMC_NewSection(const char *name, bool opt_quotes)
    58 	{
    59 		cell_t result = SMCParse_Continue;
    60 
    61 		if (new_section)
    62 		{
    63 			new_section->PushCell(handle);
    64 			new_section->PushString(name);
    65 			new_section->PushCell(opt_quotes ? 1 : 0);
    66 			new_section->Execute(&result);
    67 		}
    68 
    69 		return (SMCParseResult)result;
    70 	}
    71 
    72 	SMCParseResult ReadSMC_KeyValue(const char *key, const char *value, bool key_quotes, bool value_quotes)
    73 	{
    74 		cell_t result = SMCParse_Continue;
    75 
    76 		if (key_value)
    77 		{
    78 			key_value->PushCell(handle);
    79 			key_value->PushString(key);
    80 			key_value->PushString(value);
    81 			key_value->PushCell(key_quotes ? 1 : 0);
    82 			key_value->PushCell(value_quotes ? 1 : 0);
    83 			key_value->Execute(&result);
    84 		}
    85 
    86 		return (SMCParseResult)result;
    87 	}
    88 
    89 	SMCParseResult ReadSMC_LeavingSection()
    90 	{
    91 		cell_t result = SMCParse_Continue;
    92 
    93 		if (end_section)
    94 		{
    95 			end_section->PushCell(handle);
    96 			end_section->Execute(&result);
    97 		}
    98 
    99 		return (SMCParseResult)result;
   100 	}
   101 
   102 	SMCParseResult ReadSMC_RawLine(const char *line, unsigned int curline)
   103 	{
   104 		cell_t result = SMCParse_Continue;
   105 
   106 		if (raw_line)
   107 		{
   108 			raw_line->PushCell(handle);
   109 			raw_line->PushString(line);
   110 			raw_line->PushCell(curline);
   111 			raw_line->Execute(&result);
   112 		}
   113 
   114 		return (SMCParseResult)result;
   115 	}
   116 public:
   117 	IPluginFunction *parse_start;
   118 	IPluginFunction *parse_end;
   119 	IPluginFunction *new_section;
   120 	IPluginFunction *key_value;
   121 	IPluginFunction *end_section;
   122 	IPluginFunction *raw_line;
   123 	Handle_t handle;
   124 };
   125 
   126 class TextParseGlobals : 
   127 	public SMGlobalClass,
   128 	public IHandleTypeDispatch
   129 {
   130 public:
   131 	void OnSourceModAllInitialized()
   132 	{
   133 		HandleAccess sec;
   134 
   135 		/* These cannot be cloned, because they are locked to a specific plugin.
   136 		 * However, we let anyone read them because we don't care.
   137 		 */
   138 		g_HandleSys.InitAccessDefaults(NULL, &sec);
   139 		sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY;
   140 		sec.access[HandleAccess_Read] = 0;
   141 
   142 		g_TypeSMC = g_HandleSys.CreateType("SMCParser", this, 0, NULL, &sec, g_pCoreIdent, NULL);
   143 	}
   144 
   145 	void OnSourceModShutdown()
   146 	{
   147 		g_HandleSys.RemoveType(g_TypeSMC, g_pCoreIdent);
   148 	}
   149 
   150 	void OnHandleDestroy(HandleType_t type, void *object)
   151 	{
   152 		ParseInfo *parse = (ParseInfo *)object;
   153 		delete parse;
   154 	}
   155 };
   156 
   157 TextParseGlobals g_TextParseGlobals;
   158 
   159 static cell_t SMC_CreateParser(IPluginContext *pContext, const cell_t *params)
   160 {
   161 	ParseInfo *pInfo = new ParseInfo();
   162 
   163 	Handle_t hndl = g_HandleSys.CreateHandle(g_TypeSMC, pInfo, pContext->GetIdentity(), g_pCoreIdent, NULL);
   164 
   165 	/* Should never happen */
   166 	if (!hndl)
   167 	{
   168 		delete pInfo;
   169 		return 0;
   170 	}
   171 
   172 	pInfo->handle = hndl;
   173 
   174 	return hndl;
   175 }
   176 
   177 static cell_t SMC_SetParseStart(IPluginContext *pContext, const cell_t *params)
   178 {
   179 	Handle_t hndl = (Handle_t)params[1];
   180 	HandleError err;
   181 	ParseInfo *parse;
   182 
   183 	if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse))
   184 		!= HandleError_None)
   185 	{
   186 		return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
   187 	}
   188 
   189 	parse->parse_start = pContext->GetFunctionById((funcid_t)params[2]);
   190 
   191 	return 1;
   192 }
   193 
   194 static cell_t SMC_SetParseEnd(IPluginContext *pContext, const cell_t *params)
   195 {
   196 	Handle_t hndl = (Handle_t)params[1];
   197 	HandleError err;
   198 	ParseInfo *parse;
   199 
   200 	if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse))
   201 		!= HandleError_None)
   202 	{
   203 		return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
   204 	}
   205 
   206 	parse->parse_end = pContext->GetFunctionById((funcid_t)params[2]);
   207 
   208 	return 1;
   209 }
   210 
   211 static cell_t SMC_SetReaders(IPluginContext *pContext, const cell_t *params)
   212 {
   213 	Handle_t hndl = (Handle_t)params[1];
   214 	HandleError err;
   215 	ParseInfo *parse;
   216 
   217 	if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse))
   218 		!= HandleError_None)
   219 	{
   220 		return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
   221 	}
   222 
   223 	parse->new_section = pContext->GetFunctionById((funcid_t)params[2]);
   224 	parse->key_value = pContext->GetFunctionById((funcid_t)params[3]);
   225 	parse->end_section = pContext->GetFunctionById((funcid_t)params[4]);
   226 
   227 	return 1;
   228 }
   229 
   230 static cell_t SMC_SetRawLine(IPluginContext *pContext, const cell_t *params)
   231 {
   232 	Handle_t hndl = (Handle_t)params[1];
   233 	HandleError err;
   234 	ParseInfo *parse;
   235 
   236 	if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse))
   237 		!= HandleError_None)
   238 	{
   239 		return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
   240 	}
   241 
   242 	parse->raw_line = pContext->GetFunctionById((funcid_t)params[2]);
   243 
   244 	return 1;
   245 }
   246 
   247 static cell_t SMC_ParseFile(IPluginContext *pContext, const cell_t *params)
   248 {
   249 	Handle_t hndl = (Handle_t)params[1];
   250 	HandleError err;
   251 	ParseInfo *parse;
   252 
   253 	if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse))
   254 		!= HandleError_None)
   255 	{
   256 		return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
   257 	}
   258 
   259 	char *file;
   260 	pContext->LocalToString(params[2], &file);
   261 
   262 	char path[PLATFORM_MAX_PATH];
   263 	g_SourceMod.BuildPath(Path_Game, path, sizeof(path), "%s", file);
   264 
   265 	unsigned int line = 0, col = 0;
   266 	SMCParseError p_err = g_TextParser.ParseFile_SMC(path, parse, &line, &col);
   267 
   268 	cell_t *c_line, *c_col;
   269 	pContext->LocalToPhysAddr(params[3], &c_line);
   270 	pContext->LocalToPhysAddr(params[4], &c_col);
   271 
   272 	*c_line = line;
   273 	*c_col = col;
   274 
   275 	return (cell_t)p_err;
   276 }
   277 
   278 static cell_t SMC_GetErrorString(IPluginContext *pContext, const cell_t *params)
   279 {
   280 	const char *str = g_TextParser.GetSMCErrorString((SMCParseError)params[1]);
   281 
   282 	if (!str)
   283 	{
   284 		return 0;
   285 	}
   286 
   287 	pContext->StringToLocal(params[2], params[3], str);
   288 
   289 	return 1;
   290 }
   291 
   292 REGISTER_NATIVES(textNatives)
   293 {
   294 	{"SMC_CreateParser",			SMC_CreateParser},
   295 	{"SMC_ParseFile",				SMC_ParseFile},
   296 	{"SMC_GetErrorString",			SMC_GetErrorString},
   297 	{"SMC_SetParseStart",			SMC_SetParseStart},
   298 	{"SMC_SetParseEnd",				SMC_SetParseEnd},
   299 	{"SMC_SetReaders",				SMC_SetReaders},
   300 	{"SMC_SetRawLine",				SMC_SetRawLine},
   301 	{NULL,							NULL},
   302 };