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