core/logic/smn_textparse.cpp
author Ruben Gonzalez <drifter01620@gmail.com>
Thu May 22 09:49:27 2014 -0400 (2014-05-22)
changeset 4378 568f4497ef4a
parent 3955 763d59fe6d77
permissions -rw-r--r--
Update CS:GO team score gamedata.
     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 "common_logic.h"
    33 #include <ITextParsers.h>
    34 #include <ISourceMod.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 		handlesys->InitAccessDefaults(NULL, &sec);
   156 		sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY;
   157 		sec.access[HandleAccess_Read] = 0;
   158 
   159 		g_TypeSMC = handlesys->CreateType("SMCParser", this, 0, NULL, &sec, g_pCoreIdent, NULL);
   160 	}
   161 
   162 	void OnSourceModShutdown()
   163 	{
   164 		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 = 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 	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
   206 
   207 	if ((err=handlesys->ReadHandle(hndl, g_TypeSMC, &sec, (void **)&parse))
   208 		!= HandleError_None)
   209 	{
   210 		return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
   211 	}
   212 
   213 	parse->parse_start = pContext->GetFunctionById((funcid_t)params[2]);
   214 
   215 	return 1;
   216 }
   217 
   218 static cell_t SMC_SetParseEnd(IPluginContext *pContext, const cell_t *params)
   219 {
   220 	Handle_t hndl = (Handle_t)params[1];
   221 	HandleError err;
   222 	ParseInfo *parse;
   223 	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
   224 
   225 	if ((err=handlesys->ReadHandle(hndl, g_TypeSMC, &sec, (void **)&parse))
   226 		!= HandleError_None)
   227 	{
   228 		return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
   229 	}
   230 
   231 	parse->parse_end = pContext->GetFunctionById((funcid_t)params[2]);
   232 
   233 	return 1;
   234 }
   235 
   236 static cell_t SMC_SetReaders(IPluginContext *pContext, const cell_t *params)
   237 {
   238 	Handle_t hndl = (Handle_t)params[1];
   239 	HandleError err;
   240 	ParseInfo *parse;
   241 	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
   242 
   243 	if ((err=handlesys->ReadHandle(hndl, g_TypeSMC, &sec, (void **)&parse))
   244 		!= HandleError_None)
   245 	{
   246 		return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
   247 	}
   248 
   249 	parse->new_section = pContext->GetFunctionById((funcid_t)params[2]);
   250 	parse->key_value = pContext->GetFunctionById((funcid_t)params[3]);
   251 	parse->end_section = pContext->GetFunctionById((funcid_t)params[4]);
   252 
   253 	return 1;
   254 }
   255 
   256 static cell_t SMC_SetRawLine(IPluginContext *pContext, const cell_t *params)
   257 {
   258 	Handle_t hndl = (Handle_t)params[1];
   259 	HandleError err;
   260 	ParseInfo *parse;
   261 	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
   262 
   263 	if ((err=handlesys->ReadHandle(hndl, g_TypeSMC, &sec, (void **)&parse))
   264 		!= HandleError_None)
   265 	{
   266 		return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
   267 	}
   268 
   269 	parse->raw_line = pContext->GetFunctionById((funcid_t)params[2]);
   270 
   271 	return 1;
   272 }
   273 
   274 static cell_t SMC_ParseFile(IPluginContext *pContext, const cell_t *params)
   275 {
   276 	Handle_t hndl = (Handle_t)params[1];
   277 	HandleError err;
   278 	ParseInfo *parse;
   279 	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
   280 
   281 	if ((err=handlesys->ReadHandle(hndl, g_TypeSMC, &sec, (void **)&parse))
   282 		!= HandleError_None)
   283 	{
   284 		return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
   285 	}
   286 
   287 	char *file;
   288 	pContext->LocalToString(params[2], &file);
   289 
   290 	char path[PLATFORM_MAX_PATH];
   291 	g_pSM->BuildPath(Path_Game, path, sizeof(path), "%s", file);
   292 
   293 	SMCStates states;
   294 	SMCError p_err = textparsers->ParseFile_SMC(path, parse, &states);
   295 
   296 	cell_t *c_line, *c_col;
   297 	pContext->LocalToPhysAddr(params[3], &c_line);
   298 	pContext->LocalToPhysAddr(params[4], &c_col);
   299 
   300 	*c_line = states.line;
   301 	*c_col = states.col;
   302 
   303 	return (cell_t)p_err;
   304 }
   305 
   306 static cell_t SMC_GetErrorString(IPluginContext *pContext, const cell_t *params)
   307 {
   308 	const char *str = textparsers->GetSMCErrorString((SMCError)params[1]);
   309 
   310 	if (!str)
   311 	{
   312 		return 0;
   313 	}
   314 
   315 	pContext->StringToLocal(params[2], params[3], str);
   316 
   317 	return 1;
   318 }
   319 
   320 REGISTER_NATIVES(textNatives)
   321 {
   322 	{"SMC_CreateParser",			SMC_CreateParser},
   323 	{"SMC_ParseFile",				SMC_ParseFile},
   324 	{"SMC_GetErrorString",			SMC_GetErrorString},
   325 	{"SMC_SetParseStart",			SMC_SetParseStart},
   326 	{"SMC_SetParseEnd",				SMC_SetParseEnd},
   327 	{"SMC_SetReaders",				SMC_SetReaders},
   328 	{"SMC_SetRawLine",				SMC_SetRawLine},
   329 	{NULL,							NULL},
   330 };