core/logic/GameConfigs.h
author Kyle Sanderson <kyle.leet@gmail.com>
Mon Aug 12 00:42:56 2013 +0100 (2013-08-12)
changeset 3994 4bc73bac0f4c
parent 3961 0dc477c29792
child 4038 92fba8db1d33
permissions -rw-r--r--
Added helper stock to check if a command exists. (bug 5510, 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 "common_logic.h"
    36 #include <IGameConfigs.h>
    37 #include <ITextParsers.h>
    38 #include <sh_list.h>
    39 #include "sm_memtable.h"
    40 #include <sm_trie_tpl.h>
    41 
    42 using namespace SourceMod;
    43 using namespace SourceHook;
    44 
    45 class SendProp;
    46 
    47 class CGameConfig : 
    48 	public ITextListener_SMC,
    49 	public IGameConfig
    50 {
    51 	friend class GameConfigManager;
    52 public:
    53 	CGameConfig(const char *file, const char *engine = NULL);
    54 	~CGameConfig();
    55 public:
    56 	bool Reparse(char *error, size_t maxlength);
    57 	bool EnterFile(const char *file, char *error, size_t maxlength);
    58 	void SetBaseEngine(const char *engine);
    59 	void SetParseEngine(const char *engine);
    60 public: //ITextListener_SMC
    61 	SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name);
    62 	SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value);
    63 	SMCResult ReadSMC_LeavingSection(const SMCStates *states);
    64 public: //IGameConfig
    65 	const char *GetKeyValue(const char *key);
    66 	bool GetOffset(const char *key, int *value);
    67 	SendProp *GetSendProp(const char *key);
    68 	bool GetMemSig(const char *key, void **addr);
    69 	bool GetAddress(const char *key, void **addr);
    70 public:
    71 	void IncRefCount();
    72 	unsigned int DecRefCount();
    73 private:
    74 	BaseStringTable *m_pStrings;
    75 	char m_File[PLATFORM_MAX_PATH];
    76 	char m_CurFile[PLATFORM_MAX_PATH];
    77 	KTrie<int> m_Offsets;
    78 	KTrie<SendProp *> m_Props;
    79 	KTrie<int> m_Keys;
    80 	KTrie<void *> m_Sigs;
    81 	unsigned int m_RefCount;
    82 	/* Parse states */
    83 	int m_ParseState;
    84 	unsigned int m_IgnoreLevel;
    85 	char m_Class[64];
    86 	char m_Prop[64];
    87 	char m_offset[64];
    88 	char m_Game[256];
    89 	bool bShouldBeReadingDefault;
    90 	bool had_game;
    91 	bool matched_game;
    92 	bool had_engine;
    93 	bool matched_engine;
    94 
    95 	/* Custom Sections */
    96 	unsigned int m_CustomLevel;
    97 	ITextListener_SMC *m_CustomHandler;
    98 
    99 	/* Support for reading Addresses */
   100 	struct AddressConf
   101 	{
   102 		char signatureName[64];
   103 		int readCount;
   104 		int read[8];
   105 
   106 		AddressConf(char *sigName, unsigned sigLength, unsigned readCount, int *read);
   107 
   108 		AddressConf() {}
   109 	};
   110 
   111 	char m_Address[64];
   112 	char m_AddressSignature[64];
   113 	int m_AddressReadCount;
   114 	int m_AddressRead[8];
   115 	KTrie<AddressConf> *m_pAddresses;
   116 	const char *m_pEngine;
   117 	const char *m_pBaseEngine;
   118 };
   119 
   120 class GameConfigManager : 
   121 	public IGameConfigManager,
   122 	public SMGlobalClass
   123 {
   124 public:
   125 	GameConfigManager();
   126 	~GameConfigManager();
   127 public: //IGameConfigManager
   128 	bool LoadGameConfigFile(const char *file, IGameConfig **pConfig, char *error, size_t maxlength);
   129 	void CloseGameConfigFile(IGameConfig *cfg);
   130 	IGameConfig *ReadHandle(Handle_t hndl,
   131 		IdentityToken_t *ident,
   132 		HandleError *err);
   133 	void AddUserConfigHook(const char *sectionname, ITextListener_SMC *listener);
   134 	void RemoveUserConfigHook(const char *sectionname, ITextListener_SMC *listener);
   135 	void AcquireLock();
   136 	void ReleaseLock();
   137 public: //SMGlobalClass
   138 	void OnSourceModStartup(bool late);
   139 	void OnSourceModAllInitialized();
   140 	void OnSourceModAllShutdown();
   141 private:
   142 	List<CGameConfig *> m_cfgs;
   143 	KTrie<CGameConfig *> m_Lookup;
   144 public:
   145 	KTrie<ITextListener_SMC *> m_customHandlers;
   146 };
   147 
   148 extern GameConfigManager g_GameConfigs;
   149 extern IGameConfig *g_pGameConf;
   150 
   151 #endif //_INCLUDE_SOURCEMOD_CGAMECONFIGS_H_