1.1 --- a/core/smn_textparse.cpp Fri Mar 28 06:21:34 2008 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,319 +0,0 @@
1.4 -/**
1.5 - * vim: set ts=4 :
1.6 - * =============================================================================
1.7 - * SourceMod
1.8 - * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
1.9 - * =============================================================================
1.10 - *
1.11 - * This program is free software; you can redistribute it and/or modify it under
1.12 - * the terms of the GNU General Public License, version 3.0, as published by the
1.13 - * Free Software Foundation.
1.14 - *
1.15 - * This program is distributed in the hope that it will be useful, but WITHOUT
1.16 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.18 - * details.
1.19 - *
1.20 - * You should have received a copy of the GNU General Public License along with
1.21 - * this program. If not, see <http://www.gnu.org/licenses/>.
1.22 - *
1.23 - * As a special exception, AlliedModders LLC gives you permission to link the
1.24 - * code of this program (as well as its derivative works) to "Half-Life 2," the
1.25 - * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
1.26 - * by the Valve Corporation. You must obey the GNU General Public License in
1.27 - * all respects for all other code used. Additionally, AlliedModders LLC grants
1.28 - * this exception to all derivative works. AlliedModders LLC defines further
1.29 - * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
1.30 - * or <http://www.sourcemod.net/license.php>.
1.31 - *
1.32 - * Version: $Id$
1.33 - */
1.34 -
1.35 -#include "sm_globals.h"
1.36 -#include <ITextParsers.h>
1.37 -#include "HandleSys.h"
1.38 -
1.39 -HandleType_t g_TypeSMC = 0;
1.40 -
1.41 -class ParseInfo : public ITextListener_SMC
1.42 -{
1.43 -public:
1.44 - ParseInfo()
1.45 - {
1.46 - parse_start = NULL;
1.47 - parse_end = NULL;
1.48 - new_section = NULL;
1.49 - key_value = NULL;
1.50 - end_section = NULL;
1.51 - raw_line = NULL;
1.52 - handle = 0;
1.53 - }
1.54 -public:
1.55 - void ReadSMC_ParseStart()
1.56 - {
1.57 - if (parse_start)
1.58 - {
1.59 - cell_t result;
1.60 - parse_start->PushCell(handle);
1.61 - parse_start->Execute(&result);
1.62 - }
1.63 - }
1.64 -
1.65 - void ReadSMC_ParseEnd(bool halted, bool failed)
1.66 - {
1.67 - if (parse_end)
1.68 - {
1.69 - cell_t result;
1.70 - parse_end->PushCell(handle);
1.71 - parse_end->PushCell(halted ? 1 : 0);
1.72 - parse_end->PushCell(failed ? 1 : 0);
1.73 - parse_end->Execute(&result);
1.74 - }
1.75 - }
1.76 -
1.77 - SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name)
1.78 - {
1.79 - cell_t result = SMCResult_Continue;
1.80 -
1.81 - if (new_section)
1.82 - {
1.83 - new_section->PushCell(handle);
1.84 - new_section->PushString(name);
1.85 - new_section->PushCell(1);
1.86 - new_section->Execute(&result);
1.87 - }
1.88 -
1.89 - return (SMCResult)result;
1.90 - }
1.91 -
1.92 - SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
1.93 - {
1.94 - cell_t result = SMCResult_Continue;
1.95 -
1.96 - if (key_value)
1.97 - {
1.98 - key_value->PushCell(handle);
1.99 - key_value->PushString(key);
1.100 - key_value->PushString(value);
1.101 - key_value->PushCell(1);
1.102 - key_value->PushCell(1);
1.103 - key_value->Execute(&result);
1.104 - }
1.105 -
1.106 - return (SMCResult)result;
1.107 - }
1.108 -
1.109 - SMCResult ReadSMC_LeavingSection(const SMCStates *states)
1.110 - {
1.111 - cell_t result = SMCResult_Continue;
1.112 -
1.113 - if (end_section)
1.114 - {
1.115 - end_section->PushCell(handle);
1.116 - end_section->Execute(&result);
1.117 - }
1.118 -
1.119 - return (SMCResult)result;
1.120 - }
1.121 -
1.122 - SMCResult ReadSMC_RawLine(const SMCStates *states, const char *line)
1.123 - {
1.124 - cell_t result = SMCResult_Continue;
1.125 -
1.126 - if (raw_line)
1.127 - {
1.128 - raw_line->PushCell(handle);
1.129 - raw_line->PushString(line);
1.130 - raw_line->PushCell(states->line);
1.131 - raw_line->Execute(&result);
1.132 - }
1.133 -
1.134 - return (SMCResult)result;
1.135 - }
1.136 -public:
1.137 - IPluginFunction *parse_start;
1.138 - IPluginFunction *parse_end;
1.139 - IPluginFunction *new_section;
1.140 - IPluginFunction *key_value;
1.141 - IPluginFunction *end_section;
1.142 - IPluginFunction *raw_line;
1.143 - Handle_t handle;
1.144 -};
1.145 -
1.146 -class TextParseGlobals :
1.147 - public SMGlobalClass,
1.148 - public IHandleTypeDispatch
1.149 -{
1.150 -public:
1.151 - void OnSourceModAllInitialized()
1.152 - {
1.153 - HandleAccess sec;
1.154 -
1.155 - /* These cannot be cloned, because they are locked to a specific plugin.
1.156 - * However, we let anyone read them because we don't care.
1.157 - */
1.158 - g_HandleSys.InitAccessDefaults(NULL, &sec);
1.159 - sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY;
1.160 - sec.access[HandleAccess_Read] = 0;
1.161 -
1.162 - g_TypeSMC = g_HandleSys.CreateType("SMCParser", this, 0, NULL, &sec, g_pCoreIdent, NULL);
1.163 - }
1.164 -
1.165 - void OnSourceModShutdown()
1.166 - {
1.167 - g_HandleSys.RemoveType(g_TypeSMC, g_pCoreIdent);
1.168 - }
1.169 -
1.170 - void OnHandleDestroy(HandleType_t type, void *object)
1.171 - {
1.172 - ParseInfo *parse = (ParseInfo *)object;
1.173 - delete parse;
1.174 - }
1.175 -};
1.176 -
1.177 -TextParseGlobals g_TextParseGlobals;
1.178 -
1.179 -static cell_t SMC_CreateParser(IPluginContext *pContext, const cell_t *params)
1.180 -{
1.181 - ParseInfo *pInfo = new ParseInfo();
1.182 -
1.183 - Handle_t hndl = g_HandleSys.CreateHandle(g_TypeSMC, pInfo, pContext->GetIdentity(), g_pCoreIdent, NULL);
1.184 -
1.185 - /* Should never happen */
1.186 - if (!hndl)
1.187 - {
1.188 - delete pInfo;
1.189 - return 0;
1.190 - }
1.191 -
1.192 - pInfo->handle = hndl;
1.193 -
1.194 - return hndl;
1.195 -}
1.196 -
1.197 -static cell_t SMC_SetParseStart(IPluginContext *pContext, const cell_t *params)
1.198 -{
1.199 - Handle_t hndl = (Handle_t)params[1];
1.200 - HandleError err;
1.201 - ParseInfo *parse;
1.202 -
1.203 - if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse))
1.204 - != HandleError_None)
1.205 - {
1.206 - return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
1.207 - }
1.208 -
1.209 - parse->parse_start = pContext->GetFunctionById((funcid_t)params[2]);
1.210 -
1.211 - return 1;
1.212 -}
1.213 -
1.214 -static cell_t SMC_SetParseEnd(IPluginContext *pContext, const cell_t *params)
1.215 -{
1.216 - Handle_t hndl = (Handle_t)params[1];
1.217 - HandleError err;
1.218 - ParseInfo *parse;
1.219 -
1.220 - if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse))
1.221 - != HandleError_None)
1.222 - {
1.223 - return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
1.224 - }
1.225 -
1.226 - parse->parse_end = pContext->GetFunctionById((funcid_t)params[2]);
1.227 -
1.228 - return 1;
1.229 -}
1.230 -
1.231 -static cell_t SMC_SetReaders(IPluginContext *pContext, const cell_t *params)
1.232 -{
1.233 - Handle_t hndl = (Handle_t)params[1];
1.234 - HandleError err;
1.235 - ParseInfo *parse;
1.236 -
1.237 - if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse))
1.238 - != HandleError_None)
1.239 - {
1.240 - return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
1.241 - }
1.242 -
1.243 - parse->new_section = pContext->GetFunctionById((funcid_t)params[2]);
1.244 - parse->key_value = pContext->GetFunctionById((funcid_t)params[3]);
1.245 - parse->end_section = pContext->GetFunctionById((funcid_t)params[4]);
1.246 -
1.247 - return 1;
1.248 -}
1.249 -
1.250 -static cell_t SMC_SetRawLine(IPluginContext *pContext, const cell_t *params)
1.251 -{
1.252 - Handle_t hndl = (Handle_t)params[1];
1.253 - HandleError err;
1.254 - ParseInfo *parse;
1.255 -
1.256 - if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse))
1.257 - != HandleError_None)
1.258 - {
1.259 - return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
1.260 - }
1.261 -
1.262 - parse->raw_line = pContext->GetFunctionById((funcid_t)params[2]);
1.263 -
1.264 - return 1;
1.265 -}
1.266 -
1.267 -static cell_t SMC_ParseFile(IPluginContext *pContext, const cell_t *params)
1.268 -{
1.269 - Handle_t hndl = (Handle_t)params[1];
1.270 - HandleError err;
1.271 - ParseInfo *parse;
1.272 -
1.273 - if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse))
1.274 - != HandleError_None)
1.275 - {
1.276 - return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
1.277 - }
1.278 -
1.279 - char *file;
1.280 - pContext->LocalToString(params[2], &file);
1.281 -
1.282 - char path[PLATFORM_MAX_PATH];
1.283 - g_SourceMod.BuildPath(Path_Game, path, sizeof(path), "%s", file);
1.284 -
1.285 - SMCStates states;
1.286 - SMCError p_err = textparsers->ParseFile_SMC(path, parse, &states);
1.287 -
1.288 - cell_t *c_line, *c_col;
1.289 - pContext->LocalToPhysAddr(params[3], &c_line);
1.290 - pContext->LocalToPhysAddr(params[4], &c_col);
1.291 -
1.292 - *c_line = states.line;
1.293 - *c_col = states.col;
1.294 -
1.295 - return (cell_t)p_err;
1.296 -}
1.297 -
1.298 -static cell_t SMC_GetErrorString(IPluginContext *pContext, const cell_t *params)
1.299 -{
1.300 - const char *str = textparsers->GetSMCErrorString((SMCError)params[1]);
1.301 -
1.302 - if (!str)
1.303 - {
1.304 - return 0;
1.305 - }
1.306 -
1.307 - pContext->StringToLocal(params[2], params[3], str);
1.308 -
1.309 - return 1;
1.310 -}
1.311 -
1.312 -REGISTER_NATIVES(textNatives)
1.313 -{
1.314 - {"SMC_CreateParser", SMC_CreateParser},
1.315 - {"SMC_ParseFile", SMC_ParseFile},
1.316 - {"SMC_GetErrorString", SMC_GetErrorString},
1.317 - {"SMC_SetParseStart", SMC_SetParseStart},
1.318 - {"SMC_SetParseEnd", SMC_SetParseEnd},
1.319 - {"SMC_SetReaders", SMC_SetReaders},
1.320 - {"SMC_SetRawLine", SMC_SetRawLine},
1.321 - {NULL, NULL},
1.322 -};