tools/gdc/GameConfigs.h
author Nicholas Hastings <skamonkey@gmail.com>
Tue Jul 03 07:51:12 2012 -0400 (2012-07-03)
changeset 3573 440c51204096
parent 3129 76208f2e5ef0
permissions -rw-r--r--
Fixed clientprefs prefab menu double free crash (bug 5374, r=asherkin).
     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 #ifndef _INCLUDE_SOURCEMOD_CGAMECONFIGS_H_
    33 #define _INCLUDE_SOURCEMOD_CGAMECONFIGS_H_
    34 
    35 #include <IGameConfigs.h>
    36 #include <ITextParsers.h>
    37 //#include <sh_list.h>
    38 //#include "sm_trie.h"
    39 //#include "sm_globals.h"
    40 //#include "sm_memtable.h"
    41 //#include "sm_trie_tpl.h"
    42 //#include "ThreadSupport.h"
    43 #include <list>
    44 #include <map>
    45 #include "gdc.h"
    46 #include <limits.h>
    47 #include <string>
    48 
    49 #define PLATFORM_MAX_PATH PATH_MAX
    50 
    51 using std::map;
    52 using std::list;
    53 using namespace SourceMod;
    54 //using namespace SourceHook;
    55 
    56 extern char *game;
    57 extern char *engine;
    58 
    59 #define PARSE_ERROR -1
    60 struct cmp_str 
    61 {
    62 	bool operator()(char const *a, char const *b) 
    63 	{
    64 		return std::strcmp(a, b) < 0;
    65 	}
    66 };
    67 
    68 struct Offset
    69 {
    70 	char *name;
    71 	char *symbol;
    72 	int lin;
    73 	int win;
    74 
    75 	Offset()
    76 	{
    77 		name = symbol = NULL;
    78 		lin = win = -1;
    79 	}
    80 
    81 	~Offset()
    82 	{
    83 		delete name;
    84 		delete symbol;
    85 	}
    86 
    87 	void reset()
    88 	{
    89 		delete name;
    90 		delete symbol;
    91 		lin = win = -1;
    92 	}
    93 
    94 	void setName(const char *src)
    95 	{
    96 		delete name;
    97 		name = new char[strlen(src)+1];
    98 		strcpy(name, src);
    99 	}
   100 
   101 	void setSymbol(const char *src)
   102 	{
   103 		delete symbol;
   104 		symbol = new char[strlen(src)+1];
   105 		strcpy(symbol, src);
   106 	}
   107 
   108 	void setLin(int offs)
   109 	{
   110 		lin = offs;
   111 	}
   112 
   113 	void setWin(int offs)
   114 	{
   115 		win = offs;
   116 	}
   117 };
   118 
   119 enum Library
   120 {
   121 	Engine,
   122 	Server
   123 };
   124 
   125 struct Sig
   126 {
   127 	char *name;
   128 	char *win;
   129 	char *lin;
   130 	Library lib;
   131 
   132 	Sig()
   133 	{
   134 		name = win = lin = NULL;
   135 		lib = (Library) -1;
   136 	}
   137 
   138 	~Sig()
   139 	{
   140 		delete name;
   141 		delete win;
   142 		delete lin;
   143 	}
   144 
   145 	void reset()
   146 	{
   147 		delete name;
   148 		delete win;
   149 		delete lin;
   150 		lib = (Library) -1;
   151 	}
   152 
   153 	void setName(const char *src)
   154 	{
   155 		delete name;
   156 		name = new char[strlen(src)+1];
   157 		strcpy(name, src);
   158 	}
   159 
   160 	void setWin(const char *src)
   161 	{
   162 		delete win;
   163 		win = new char[strlen(src)+1];
   164 		strcpy(win, src);
   165 	}
   166 
   167 	void setLin(const char *src)
   168 	{
   169 		delete lin;
   170 		lin = new char[strlen(src)+1];
   171 		strcpy(lin, src);
   172 	}
   173 
   174 	void setLib(const char *src)
   175 	{
   176 		if (stricmp(src, "server") == 0) lib = Server;
   177 		else if (stricmp(src, "engine") == 0) lib = Engine;
   178 		else lib = (Library)-1;
   179 	}
   180 };
   181 
   182 class SendProp;
   183 
   184 class CGameConfig : 
   185 	public ITextListener_SMC//,
   186 //	public IGameConfig
   187 {
   188 public:
   189 	CGameConfig();//const char *file);//, const char *game, const char *engine);
   190 	~CGameConfig();
   191 public:
   192 	bool Reparse(char *error, size_t maxlength);
   193 	bool EnterFile(const char *file, char *error, size_t maxlength);
   194 	list<Offset> GetOffsets();
   195 	list<Sig> GetSigs();
   196 public: //ITextListener_SMC
   197 	SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name);
   198 	SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value);
   199 	SMCResult ReadSMC_LeavingSection(const SMCStates *states);
   200 public: //IGameConfig
   201 	const char *GetKeyValue(const char *key);
   202 	bool GetOffset(const char *key, int *value);
   203 	SendProp *GetSendProp(const char *key);
   204 	bool GetMemSig(const char *key, void **addr);
   205 public:
   206 //	void IncRefCount();
   207 //	unsigned int DecRefCount();
   208 private:
   209 //	bool DoesGameMatch(const char *value);
   210 //	bool DoesEngineMatch(const char *value);
   211 public:
   212 //	BaseStringTable *m_pStrings;
   213 	char m_File[PLATFORM_MAX_PATH];
   214 	char m_CurFile[PLATFORM_MAX_PATH];
   215 	list<Offset> m_Offsets;
   216 	list<Sig> m_Sigs;
   217 	map<const char*,const char*,cmp_str> m_Keys;
   218 	unsigned int m_RefCount;
   219 	/* Parse states */
   220 	int m_ParseState;
   221 	unsigned int m_IgnoreLevel;
   222 	char m_Class[64];
   223 	char m_Prop[64];
   224 	char m_offset[64];
   225 	char m_Game[256];
   226 	char m_gdcGame[256];
   227 	char m_gdcEngine[256];
   228 	bool bShouldBeReadingDefault;
   229 	bool had_game;
   230 	bool matched_game;
   231 	bool had_engine;
   232 	bool matched_engine;
   233 
   234 	/* Custom Sections */
   235 	unsigned int m_CustomLevel;
   236 	ITextListener_SMC *m_CustomHandler;
   237 };
   238 
   239 #endif //_INCLUDE_SOURCEMOD_CGAMECONFIGS_H_