core/Database.h
author Phong Dang <teame06@gmail.com>
Wed Aug 01 07:29:40 2007 +0000 (2007-08-01)
changeset 1241 2e8046793bc9
parent 1231 17badaf66ce4
child 1330 ab94316bb52b
permissions -rw-r--r--
Added check to make sure player are alive to perform sm_slap, sm_burn, sm_slay.
     1 /**
     2  * vim: set ts=4 :
     3  * ================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * ================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or 
     9  * modify it under the terms of the GNU General Public License, 
    10  * version 3.0, as published by the Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful,
    13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15  * GNU General Public License for more details.
    16  *
    17  * You should have received a copy of the GNU General Public License
    18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to 
    21  * link the code of this program (as well as its derivative works) to 
    22  * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any 
    23  * Game MODs that run on software by the Valve Corporation.  You must 
    24  * obey the GNU General Public License in all respects for all other 
    25  * code used. Additionally, AlliedModders LLC grants this exception 
    26  * to all derivative works. AlliedModders LLC defines further 
    27  * exceptions, found in LICENSE.txt (as of this writing, version 
    28  * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
    29  *
    30  * Version: $Id$
    31  */
    32 
    33 #ifndef _INCLUDE_DATABASE_MANAGER_H_
    34 #define _INCLUDE_DATABASE_MANAGER_H_
    35 
    36 #include <IDBDriver.h>
    37 #include "sm_globals.h"
    38 #include <sh_vector.h>
    39 #include <sh_string.h>
    40 #include <sh_list.h>
    41 #include <ITextParsers.h>
    42 #include "sm_memtable.h"
    43 #include <IThreader.h>
    44 #include "sm_simple_prioqueue.h"
    45 #include "PluginSys.h"
    46 
    47 using namespace SourceHook;
    48 
    49 struct ConfDbInfo
    50 {
    51 	ConfDbInfo() : name(-1), driver(-1), host(-1), user(-1), pass(-1), 
    52 		database(-1), realDriver(NULL)
    53 	{
    54 	}
    55 	int name;
    56 	int driver;
    57 	int host;
    58 	int user;
    59 	int pass;
    60 	int database;
    61 	IDBDriver *realDriver;
    62 	DatabaseInfo info;
    63 };
    64 
    65 class DBManager : 
    66 	public IDBManager,
    67 	public SMGlobalClass,
    68 	public IHandleTypeDispatch,
    69 	public ITextListener_SMC,
    70 	public IThread,
    71 	public IThreadWorkerCallbacks,
    72 	public IPluginsListener
    73 {
    74 public:
    75 	DBManager();
    76 public:
    77 	const char *GetInterfaceName();
    78 	unsigned int GetInterfaceVersion();
    79 public: //SMGlobalClass
    80 	void OnSourceModAllInitialized();
    81 	void OnSourceModLevelChange(const char *mapName);
    82 	void OnSourceModIdentityDropped(IdentityToken_t *pToken);
    83 	void OnSourceModShutdown();
    84 public: //IHandleTypeDispatch
    85 	void OnHandleDestroy(HandleType_t type, void *object);
    86 public: //IDBManager
    87 	void AddDriver(IDBDriver *pDrivera);
    88 	void RemoveDriver(IDBDriver *pDriver);
    89 	const DatabaseInfo *FindDatabaseConf(const char *name);
    90 	bool Connect(const char *name, IDBDriver **pdr, IDatabase **pdb, bool persistent, char *error, size_t maxlength);
    91 	unsigned int GetDriverCount();
    92 	IDBDriver *GetDriver(unsigned int index);
    93 	Handle_t CreateHandle(DBHandleType type, void *ptr, IdentityToken_t *pToken);
    94 	HandleError ReadHandle(Handle_t hndl, DBHandleType type, void **ptr);
    95 	HandleError ReleaseHandle(Handle_t hndl, DBHandleType type, IdentityToken_t *token);
    96 public: //ITextListener_SMC
    97 	void ReadSMC_ParseStart();
    98 	SMCParseResult ReadSMC_NewSection(const char *name, bool opt_quotes);
    99 	SMCParseResult ReadSMC_KeyValue(const char *key, const char *value, bool key_quotes, bool value_quotes);
   100 	SMCParseResult ReadSMC_LeavingSection();
   101 	void ReadSMC_ParseEnd(bool halted, bool failed);
   102 public: //IThread
   103 	void RunThread(IThreadHandle *pThread);
   104 	void OnTerminate(IThreadHandle *pThread, bool cancel);
   105 public: //IThreadWorkerCallbacks
   106 	void OnWorkerStart(IThreadWorker *pWorker);
   107 	void OnWorkerStop(IThreadWorker *pWorker);
   108 public: //IPluginsListener
   109 	void OnPluginUnloaded(IPlugin *plugin);
   110 public:
   111 	ConfDbInfo *GetDatabaseConf(const char *name);
   112 	IDBDriver *FindOrLoadDriver(const char *name);
   113 	IDBDriver *GetDefaultDriver();
   114 	const char *GetDefaultDriverName();
   115 	bool AddToThreadQueue(IDBThreadOperation *op, PrioQueueLevel prio);
   116 	void LockConfig();
   117 	void UnlockConfig();
   118 	void RunFrame();
   119 	inline HandleType_t GetDatabaseType()
   120 	{
   121 		return m_DatabaseType;
   122 	}
   123 private:
   124 	void KillWorkerThread();
   125 private:
   126 	CVector<IDBDriver *> m_drivers;
   127 
   128 	/* Threading stuff */
   129 	PrioQueue<IDBThreadOperation *> m_OpQueue;
   130 	Queue<IDBThreadOperation *> m_ThinkQueue;
   131 	CVector<bool> m_drSafety;			/* which drivers are safe? */
   132 	IThreadWorker *m_pWorker;			/* Worker thread object */
   133 	IMutex *m_pConfigLock;				/* Configuration lock */
   134 	IMutex *m_pQueueLock;				/* Queue safety lock */
   135 	IMutex *m_pThinkLock;				/* Think-queue lock */
   136 
   137 	List<ConfDbInfo> m_confs;
   138 	HandleType_t m_DriverType;
   139 	HandleType_t m_DatabaseType;
   140 	String m_DefDriver;
   141 	BaseStringTable m_StrTab;
   142 	char m_Filename[PLATFORM_MAX_PATH];
   143 	unsigned int m_ParseLevel;
   144 	unsigned int m_ParseState;
   145 	IDBDriver *m_pDefault;
   146 };
   147 
   148 extern DBManager g_DBMan;
   149 
   150 #endif //_INCLUDE_DATABASE_MANAGER_H_