extensions/structs/extension.cpp
changeset 2224 676c3b826f13
child 2385 2265a1fd35c9
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/extensions/structs/extension.cpp	Fri Aug 08 00:17:37 2008 +0000
     1.3 @@ -0,0 +1,285 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * =============================================================================
     1.7 + * SourceMod Sample Extension
     1.8 + * Copyright (C) 2004-2008 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 "extension.h"
    1.36 +
    1.37 +/**
    1.38 + * @file extension.cpp
    1.39 + * @brief Implement extension code here.
    1.40 + */
    1.41 +
    1.42 +Structs g_Structs;		/**< Global singleton for extension's main interface */
    1.43 +
    1.44 +SMEXT_LINK(&g_Structs);
    1.45 +
    1.46 +HandleType_t g_StructHandle = 0;
    1.47 +StructHandler g_StructHandler;
    1.48 +
    1.49 +IGameConfig *conf;
    1.50 +
    1.51 +size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
    1.52 +{
    1.53 +	va_list ap;
    1.54 +	va_start(ap, fmt);
    1.55 +	size_t len = vsnprintf(buffer, maxlength, fmt, ap);
    1.56 +	va_end(ap);
    1.57 +
    1.58 +	if (len >= maxlength)
    1.59 +	{
    1.60 +		buffer[maxlength - 1] = '\0';
    1.61 +		return (maxlength - 1);
    1.62 +	}
    1.63 +	else
    1.64 +	{
    1.65 +		return len;
    1.66 +	}
    1.67 +}
    1.68 +
    1.69 +void Structs::SDK_OnAllLoaded()
    1.70 +{
    1.71 +	g_StructHandle = handlesys->CreateType("Struct", 
    1.72 +		&g_StructHandler, 
    1.73 +		0, 
    1.74 +		NULL, 
    1.75 +		NULL, 
    1.76 +		myself->GetIdentity(), 
    1.77 +		NULL);
    1.78 +
    1.79 +	gameconfs->AddUserConfigHook("Structs", this);
    1.80 +
    1.81 +	sharesys->AddNatives(myself, MyNatives);
    1.82 +
    1.83 +
    1.84 +	char error[100];
    1.85 +	if (!gameconfs->LoadGameConfigFile("structs", &conf, error, sizeof(error)))
    1.86 +	{
    1.87 +		g_pSM->LogError(myself, "Parsing Failed!");
    1.88 +	}
    1.89 +}
    1.90 +
    1.91 +void Structs::SDK_OnUnload()
    1.92 +{
    1.93 +	handlesys->RemoveType(g_StructHandle, myself->GetIdentity());
    1.94 +
    1.95 +	gameconfs->RemoveUserConfigHook("Structs", this);
    1.96 +}
    1.97 +
    1.98 +bool Structs::SDK_OnLoad( char *error, size_t maxlength, bool late )
    1.99 +{
   1.100 +	sharesys->AddInterface(myself, &g_StructManager);
   1.101 +
   1.102 +	m_typeLookup.insert("int", Member_Int32);
   1.103 +	m_typeLookup.insert("int*", Member_Int32Pointer);
   1.104 +	m_typeLookup.insert("float", Member_Float);
   1.105 +	m_typeLookup.insert("float*", Member_FloatPointer);
   1.106 +	m_typeLookup.insert("char", Member_Char);
   1.107 +	m_typeLookup.insert("char*", Member_CharPointer);
   1.108 +	m_typeLookup.insert("Vector", Member_Vector);
   1.109 +	m_typeLookup.insert("Vector*", Member_VectorPointer);
   1.110 +	m_typeLookup.insert("ent", Member_EHandle);
   1.111 +	m_typeLookup.insert("ent*", Member_EHandlePointer);
   1.112 +
   1.113 +	return true;
   1.114 +}
   1.115 +
   1.116 +void Structs::ReadSMC_ParseStart()
   1.117 +{
   1.118 +	m_bInStruct = false;
   1.119 +	m_currentStruct = NULL;
   1.120 +	m_bInMember = false;
   1.121 +	m_currentMember = NULL;
   1.122 +}
   1.123 +
   1.124 +SourceMod::SMCResult Structs::ReadSMC_NewSection( const SMCStates *states, const char *name )
   1.125 +{
   1.126 +	if (!m_bInStruct)
   1.127 +	{
   1.128 +		m_currentStruct = new StructInfo();
   1.129 +		UTIL_Format(m_currentStruct->name, sizeof(m_currentStruct->name), "%s", name);
   1.130 +		m_bInStruct = true;
   1.131 +
   1.132 +		return SMCResult_Continue;
   1.133 +	}
   1.134 +
   1.135 +	if (!m_bInMember)
   1.136 +	{
   1.137 +		m_bInMember = true;
   1.138 +		m_currentMember = new MemberInfo();
   1.139 +		UTIL_Format(m_currentMember->name, sizeof(m_currentMember->name), "%s", name);
   1.140 +
   1.141 +		return SMCResult_Continue;
   1.142 +	}
   1.143 +
   1.144 +	g_pSM->LogMessage(myself, "Cannot nest within a member: line: %i col: %i", states->line, states->col);
   1.145 +
   1.146 +	return SMCResult_HaltFail;
   1.147 +}
   1.148 +
   1.149 +SourceMod::SMCResult Structs::ReadSMC_KeyValue( const SMCStates *states, const char *key, const char *value )
   1.150 +{
   1.151 +	if (!m_bInStruct)
   1.152 +	{
   1.153 +		//hrm..
   1.154 +		g_pSM->LogMessage(myself, "Unknown value %s: line: %i col: %i", key, states->line, states->col);
   1.155 +		return SMCResult_HaltFail;
   1.156 +	}
   1.157 +
   1.158 +	if (m_bInMember)
   1.159 +	{
   1.160 +		if (strcmp(key, "type") == 0)
   1.161 +		{
   1.162 +			MemberType *pType = m_typeLookup.retrieve(value);
   1.163 +
   1.164 +			if (pType == NULL)
   1.165 +			{
   1.166 +				//invalid type
   1.167 +				g_pSM->LogMessage(myself, "Invalid Type: line: %i col: %i", states->line, states->col);
   1.168 +				return SMCResult_HaltFail;
   1.169 +			}
   1.170 +
   1.171 +			/* Note all 'int' types are assumed to be 32bit for the moment */
   1.172 +			m_currentMember->type = *pType;
   1.173 +		}
   1.174 +		else if (strcmp(key, "size") == 0)
   1.175 +		{
   1.176 +			m_currentMember->size = atoi(value);
   1.177 +		}
   1.178 +#if defined WIN32
   1.179 +		else if (strcmp(key, "windows") == 0)
   1.180 +#else
   1.181 +		else if (strcmp(key, "linux") == 0)
   1.182 +#endif
   1.183 +		{
   1.184 +			m_currentMember->offset = atoi(value);
   1.185 +		}
   1.186 +	}
   1.187 +	else
   1.188 +	{
   1.189 +		if (strcmp(key, "size") == 0)
   1.190 +		{
   1.191 +			m_currentStruct->size = atoi(value);
   1.192 +		}
   1.193 +	}
   1.194 +
   1.195 +	return SMCResult_Continue;
   1.196 +}
   1.197 +
   1.198 +SourceMod::SMCResult Structs::ReadSMC_LeavingSection( const SMCStates *states )
   1.199 +{
   1.200 +	if (m_bInMember)
   1.201 +	{
   1.202 +		/** Check for missing or invalid parameters */
   1.203 +		if (m_currentMember->type == Member_Unknown || m_currentMember->offset == -1)
   1.204 +		{
   1.205 +			g_pSM->LogMessage(myself, "Missing offset or type: line: %i col: %i", states->line, states->col);
   1.206 +			return SMCResult_HaltFail;
   1.207 +		}
   1.208 +
   1.209 +		if (m_currentMember->size == -1)
   1.210 +		{
   1.211 +			switch (m_currentMember->type)
   1.212 +			{
   1.213 +				case Member_Float:
   1.214 +				case Member_FloatPointer:
   1.215 +				case Member_Vector:
   1.216 +				case Member_VectorPointer:
   1.217 +				case Member_EHandle:
   1.218 +				case Member_EHandlePointer:
   1.219 +				{
   1.220 +					break;
   1.221 +				}
   1.222 +
   1.223 +				default:
   1.224 +				{
   1.225 +					g_pSM->LogMessage(myself, "Missing size: line: %i col: %i", states->line, states->col);
   1.226 +					return SMCResult_HaltFail;
   1.227 +				}
   1.228 +			}
   1.229 +		}
   1.230 +
   1.231 +		/* Work out the int size */
   1.232 +		if (m_currentMember->type == Member_Int32)
   1.233 +		{
   1.234 +			if (m_currentMember->size == 1)
   1.235 +			{
   1.236 +				m_currentMember->type = Member_Int8;
   1.237 +			} 
   1.238 +			else if (m_currentMember->size == 2)
   1.239 +			{
   1.240 +				m_currentMember->type = Member_Int16;
   1.241 +			} 
   1.242 +			else if (m_currentMember->size == 4)
   1.243 +			{
   1.244 +				m_currentMember->type = Member_Int32;
   1.245 +			} 
   1.246 +			else
   1.247 +			{
   1.248 +				g_pSM->LogMessage(myself, "Invalid int size %i: line: %i col: %i", m_currentMember->size, states->line, states->col);
   1.249 +				return SMCResult_HaltFail;
   1.250 +			}
   1.251 +
   1.252 +		} 
   1.253 +		else if (m_currentMember->type == Member_Int32)
   1.254 +		{
   1.255 +			if (m_currentMember->size == 1)
   1.256 +			{
   1.257 +				m_currentMember->type = Member_Int8Pointer;
   1.258 +			} 
   1.259 +			else if (m_currentMember->size == 2)
   1.260 +			{
   1.261 +				m_currentMember->type = Member_Int16Pointer;
   1.262 +			} 
   1.263 +			else if (m_currentMember->size == 4)
   1.264 +			{
   1.265 +				m_currentMember->type = Member_Int32Pointer;
   1.266 +			} 
   1.267 +			else
   1.268 +			{
   1.269 +				g_pSM->LogMessage(myself, "Invalid int size %i: line: %i col: %i", m_currentMember->size, states->line, states->col);
   1.270 +				return SMCResult_HaltFail;
   1.271 +			}
   1.272 +		}
   1.273 +
   1.274 +		m_currentStruct->AddMember(m_currentMember->name, m_currentMember);
   1.275 +
   1.276 +		m_bInMember = false;
   1.277 +		m_currentMember = NULL;
   1.278 +
   1.279 +		return SMCResult_Continue;
   1.280 +	}
   1.281 +
   1.282 +	g_StructManager.AddStruct(m_currentStruct->name, m_currentStruct);
   1.283 +
   1.284 +	m_bInStruct = false;
   1.285 +	m_currentStruct = NULL;
   1.286 +
   1.287 +	return SMCResult_Continue;
   1.288 +}