ds@1952: /**
ds@1952: * vim: set ts=4 :
ds@1952: * =============================================================================
ds@1952: * SourceMod
ds@1952: * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
ds@1952: * =============================================================================
ds@1952: *
ds@1952: * This program is free software; you can redistribute it and/or modify it under
ds@1952: * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952: * Free Software Foundation.
ds@1952: *
ds@1952: * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
ds@1952: * details.
ds@1952: *
ds@1952: * You should have received a copy of the GNU General Public License along with
ds@1952: * this program. If not, see .
ds@1952: *
ds@1952: * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952: * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952: * by the Valve Corporation. You must obey the GNU General Public License in
ds@1952: * all respects for all other code used. Additionally, AlliedModders LLC grants
ds@1952: * this exception to all derivative works. AlliedModders LLC defines further
ds@1952: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952: * or .
ds@1952: *
ds@1952: * Version: $Id$
ds@1952: */
ds@1952:
ds@1952: #include "sm_globals.h"
ds@1952: #include
ds@1952: #include "HandleSys.h"
ds@1952:
ds@1952: HandleType_t g_TypeSMC = 0;
ds@1952:
ds@1952: class ParseInfo : public ITextListener_SMC
ds@1952: {
ds@1952: public:
ds@1952: ParseInfo()
ds@1952: {
ds@1952: parse_start = NULL;
ds@1952: parse_end = NULL;
ds@1952: new_section = NULL;
ds@1952: key_value = NULL;
ds@1952: end_section = NULL;
ds@1952: raw_line = NULL;
ds@1952: handle = 0;
ds@1952: }
ds@1952: public:
ds@1952: void ReadSMC_ParseStart()
ds@1952: {
ds@1952: if (parse_start)
ds@1952: {
ds@1952: cell_t result;
ds@1952: parse_start->PushCell(handle);
ds@1952: parse_start->Execute(&result);
ds@1952: }
ds@1952: }
ds@1952:
ds@1952: void ReadSMC_ParseEnd(bool halted, bool failed)
ds@1952: {
ds@1952: if (parse_end)
ds@1952: {
ds@1952: cell_t result;
ds@1952: parse_end->PushCell(handle);
ds@1952: parse_end->PushCell(halted ? 1 : 0);
ds@1952: parse_end->PushCell(failed ? 1 : 0);
ds@1952: parse_end->Execute(&result);
ds@1952: }
ds@1952: }
ds@1952:
ds@1952: SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name)
ds@1952: {
ds@1952: cell_t result = SMCResult_Continue;
ds@1952:
ds@1952: if (new_section)
ds@1952: {
ds@1952: new_section->PushCell(handle);
ds@1952: new_section->PushString(name);
ds@1952: new_section->PushCell(1);
ds@1952: new_section->Execute(&result);
ds@1952: }
ds@1952:
ds@1952: return (SMCResult)result;
ds@1952: }
ds@1952:
ds@1952: SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
ds@1952: {
ds@1952: cell_t result = SMCResult_Continue;
ds@1952:
ds@1952: if (key_value)
ds@1952: {
ds@1952: key_value->PushCell(handle);
ds@1952: key_value->PushString(key);
ds@1952: key_value->PushString(value);
ds@1952: key_value->PushCell(1);
ds@1952: key_value->PushCell(1);
ds@1952: key_value->Execute(&result);
ds@1952: }
ds@1952:
ds@1952: return (SMCResult)result;
ds@1952: }
ds@1952:
ds@1952: SMCResult ReadSMC_LeavingSection(const SMCStates *states)
ds@1952: {
ds@1952: cell_t result = SMCResult_Continue;
ds@1952:
ds@1952: if (end_section)
ds@1952: {
ds@1952: end_section->PushCell(handle);
ds@1952: end_section->Execute(&result);
ds@1952: }
ds@1952:
ds@1952: return (SMCResult)result;
ds@1952: }
ds@1952:
ds@1952: SMCResult ReadSMC_RawLine(const SMCStates *states, const char *line)
ds@1952: {
ds@1952: cell_t result = SMCResult_Continue;
ds@1952:
ds@1952: if (raw_line)
ds@1952: {
ds@1952: raw_line->PushCell(handle);
ds@1952: raw_line->PushString(line);
ds@1952: raw_line->PushCell(states->line);
ds@1952: raw_line->Execute(&result);
ds@1952: }
ds@1952:
ds@1952: return (SMCResult)result;
ds@1952: }
ds@1952: public:
ds@1952: IPluginFunction *parse_start;
ds@1952: IPluginFunction *parse_end;
ds@1952: IPluginFunction *new_section;
ds@1952: IPluginFunction *key_value;
ds@1952: IPluginFunction *end_section;
ds@1952: IPluginFunction *raw_line;
ds@1952: Handle_t handle;
ds@1952: };
ds@1952:
ds@1952: class TextParseGlobals :
ds@1952: public SMGlobalClass,
ds@1952: public IHandleTypeDispatch
ds@1952: {
ds@1952: public:
ds@1952: void OnSourceModAllInitialized()
ds@1952: {
ds@1952: HandleAccess sec;
ds@1952:
ds@1952: /* These cannot be cloned, because they are locked to a specific plugin.
ds@1952: * However, we let anyone read them because we don't care.
ds@1952: */
ds@1952: g_HandleSys.InitAccessDefaults(NULL, &sec);
ds@1952: sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY;
ds@1952: sec.access[HandleAccess_Read] = 0;
ds@1952:
ds@1952: g_TypeSMC = g_HandleSys.CreateType("SMCParser", this, 0, NULL, &sec, g_pCoreIdent, NULL);
ds@1952: }
ds@1952:
ds@1952: void OnSourceModShutdown()
ds@1952: {
ds@1952: g_HandleSys.RemoveType(g_TypeSMC, g_pCoreIdent);
ds@1952: }
ds@1952:
ds@1952: void OnHandleDestroy(HandleType_t type, void *object)
ds@1952: {
ds@1952: ParseInfo *parse = (ParseInfo *)object;
ds@1952: delete parse;
ds@1952: }
ds@1952:
ds@1952: bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
ds@1952: {
ds@1952: *pSize = sizeof(ParseInfo);
ds@1952: return true;
ds@1952: }
ds@1952: };
ds@1952:
ds@1952: TextParseGlobals g_TextParseGlobals;
ds@1952:
ds@1952: static cell_t SMC_CreateParser(IPluginContext *pContext, const cell_t *params)
ds@1952: {
ds@1952: ParseInfo *pInfo = new ParseInfo();
ds@1952:
ds@1952: Handle_t hndl = g_HandleSys.CreateHandle(g_TypeSMC, pInfo, pContext->GetIdentity(), g_pCoreIdent, NULL);
ds@1952:
ds@1952: /* Should never happen */
ds@1952: if (!hndl)
ds@1952: {
ds@1952: delete pInfo;
ds@1952: return 0;
ds@1952: }
ds@1952:
ds@1952: pInfo->handle = hndl;
ds@1952:
ds@1952: return hndl;
ds@1952: }
ds@1952:
ds@1952: static cell_t SMC_SetParseStart(IPluginContext *pContext, const cell_t *params)
ds@1952: {
ds@1952: Handle_t hndl = (Handle_t)params[1];
ds@1952: HandleError err;
ds@1952: ParseInfo *parse;
ds@1952:
ds@1952: if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse))
ds@1952: != HandleError_None)
ds@1952: {
ds@1952: return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
ds@1952: }
ds@1952:
ds@1952: parse->parse_start = pContext->GetFunctionById((funcid_t)params[2]);
ds@1952:
ds@1952: return 1;
ds@1952: }
ds@1952:
ds@1952: static cell_t SMC_SetParseEnd(IPluginContext *pContext, const cell_t *params)
ds@1952: {
ds@1952: Handle_t hndl = (Handle_t)params[1];
ds@1952: HandleError err;
ds@1952: ParseInfo *parse;
ds@1952:
ds@1952: if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse))
ds@1952: != HandleError_None)
ds@1952: {
ds@1952: return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
ds@1952: }
ds@1952:
ds@1952: parse->parse_end = pContext->GetFunctionById((funcid_t)params[2]);
ds@1952:
ds@1952: return 1;
ds@1952: }
ds@1952:
ds@1952: static cell_t SMC_SetReaders(IPluginContext *pContext, const cell_t *params)
ds@1952: {
ds@1952: Handle_t hndl = (Handle_t)params[1];
ds@1952: HandleError err;
ds@1952: ParseInfo *parse;
ds@1952:
ds@1952: if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse))
ds@1952: != HandleError_None)
ds@1952: {
ds@1952: return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
ds@1952: }
ds@1952:
ds@1952: parse->new_section = pContext->GetFunctionById((funcid_t)params[2]);
ds@1952: parse->key_value = pContext->GetFunctionById((funcid_t)params[3]);
ds@1952: parse->end_section = pContext->GetFunctionById((funcid_t)params[4]);
ds@1952:
ds@1952: return 1;
ds@1952: }
ds@1952:
ds@1952: static cell_t SMC_SetRawLine(IPluginContext *pContext, const cell_t *params)
ds@1952: {
ds@1952: Handle_t hndl = (Handle_t)params[1];
ds@1952: HandleError err;
ds@1952: ParseInfo *parse;
ds@1952:
ds@1952: if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse))
ds@1952: != HandleError_None)
ds@1952: {
ds@1952: return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
ds@1952: }
ds@1952:
ds@1952: parse->raw_line = pContext->GetFunctionById((funcid_t)params[2]);
ds@1952:
ds@1952: return 1;
ds@1952: }
ds@1952:
ds@1952: static cell_t SMC_ParseFile(IPluginContext *pContext, const cell_t *params)
ds@1952: {
ds@1952: Handle_t hndl = (Handle_t)params[1];
ds@1952: HandleError err;
ds@1952: ParseInfo *parse;
ds@1952:
ds@1952: if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse))
ds@1952: != HandleError_None)
ds@1952: {
ds@1952: return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
ds@1952: }
ds@1952:
ds@1952: char *file;
ds@1952: pContext->LocalToString(params[2], &file);
ds@1952:
ds@1952: char path[PLATFORM_MAX_PATH];
ds@1952: g_SourceMod.BuildPath(Path_Game, path, sizeof(path), "%s", file);
ds@1952:
ds@1952: SMCStates states;
ds@1952: SMCError p_err = textparsers->ParseFile_SMC(path, parse, &states);
ds@1952:
ds@1952: cell_t *c_line, *c_col;
ds@1952: pContext->LocalToPhysAddr(params[3], &c_line);
ds@1952: pContext->LocalToPhysAddr(params[4], &c_col);
ds@1952:
ds@1952: *c_line = states.line;
ds@1952: *c_col = states.col;
ds@1952:
ds@1952: return (cell_t)p_err;
ds@1952: }
ds@1952:
ds@1952: static cell_t SMC_GetErrorString(IPluginContext *pContext, const cell_t *params)
ds@1952: {
ds@1952: const char *str = textparsers->GetSMCErrorString((SMCError)params[1]);
ds@1952:
ds@1952: if (!str)
ds@1952: {
ds@1952: return 0;
ds@1952: }
ds@1952:
ds@1952: pContext->StringToLocal(params[2], params[3], str);
ds@1952:
ds@1952: return 1;
ds@1952: }
ds@1952:
ds@1952: REGISTER_NATIVES(textNatives)
ds@1952: {
ds@1952: {"SMC_CreateParser", SMC_CreateParser},
ds@1952: {"SMC_ParseFile", SMC_ParseFile},
ds@1952: {"SMC_GetErrorString", SMC_GetErrorString},
ds@1952: {"SMC_SetParseStart", SMC_SetParseStart},
ds@1952: {"SMC_SetParseEnd", SMC_SetParseEnd},
ds@1952: {"SMC_SetReaders", SMC_SetReaders},
ds@1952: {"SMC_SetRawLine", SMC_SetRawLine},
ds@1952: {NULL, NULL},
ds@1952: };