core/logic/Database.h
author Nicholas Hastings <skamonkey@gmail.com>
Wed Apr 23 21:40:50 2014 -0400 (2014-04-23)
changeset 4332 75c299312bec
parent 4138 da768ad428cf
permissions -rw-r--r--
Update CS:GO gamedata.
     1 /**
     2  * vim: set ts=4 sw=4 tw=99 noet :
     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_DATABASE_MANAGER_H_
    33 #define _INCLUDE_DATABASE_MANAGER_H_
    34 
    35 #include <IDBDriver.h>
    36 #include "common_logic.h"
    37 #include <sh_vector.h>
    38 #include <sh_string.h>
    39 #include <sh_list.h>
    40 #include <ITextParsers.h>
    41 #include <IThreader.h>
    42 #include <IPluginSys.h>
    43 #include <am-thread-utils.h>
    44 #include "sm_simple_prioqueue.h"
    45 
    46 using namespace SourceHook;
    47 
    48 struct ConfDbInfo
    49 {
    50 	ConfDbInfo() : realDriver(NULL)
    51 	{
    52 	}
    53 	String name;
    54 	String driver;
    55 	String host;
    56 	String user;
    57 	String pass;
    58 	String database;
    59 	IDBDriver *realDriver;
    60 	DatabaseInfo info;
    61 };
    62 
    63 class DBManager : 
    64 	public IDBManager,
    65 	public SMGlobalClass,
    66 	public IHandleTypeDispatch,
    67 	public ITextListener_SMC,
    68 	public IPluginsListener,
    69 	public ke::IRunnable
    70 {
    71 public:
    72 	DBManager();
    73 public:
    74 	const char *GetInterfaceName();
    75 	unsigned int GetInterfaceVersion();
    76 public: //SMGlobalClass
    77 	void OnSourceModAllInitialized();
    78 	void OnSourceModLevelChange(const char *mapName);
    79 	void OnSourceModIdentityDropped(IdentityToken_t *pToken);
    80 	void OnSourceModShutdown();
    81 public: //IHandleTypeDispatch
    82 	void OnHandleDestroy(HandleType_t type, void *object);
    83 public: //IDBManager
    84 	void AddDriver(IDBDriver *pDrivera);
    85 	void RemoveDriver(IDBDriver *pDriver);
    86 	const DatabaseInfo *FindDatabaseConf(const char *name);
    87 	bool Connect(const char *name, IDBDriver **pdr, IDatabase **pdb, bool persistent, char *error, size_t maxlength);
    88 	unsigned int GetDriverCount();
    89 	IDBDriver *GetDriver(unsigned int index);
    90 	Handle_t CreateHandle(DBHandleType type, void *ptr, IdentityToken_t *pToken);
    91 	HandleError ReadHandle(Handle_t hndl, DBHandleType type, void **ptr);
    92 	HandleError ReleaseHandle(Handle_t hndl, DBHandleType type, IdentityToken_t *token);
    93 	void AddDependency(IExtension *myself, IDBDriver *driver);
    94 public: //ITextListener_SMC
    95 	void ReadSMC_ParseStart();
    96 	SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name);
    97 	SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value);
    98 	SMCResult ReadSMC_LeavingSection(const SMCStates *states);
    99 	void ReadSMC_ParseEnd(bool halted, bool failed);
   100 public: //ke::IRunnable
   101 	void Run();
   102 	void ThreadMain();
   103 public: //IPluginsListener
   104 	void OnPluginUnloaded(IPlugin *plugin);
   105 public:
   106 	ConfDbInfo *GetDatabaseConf(const char *name);
   107 	IDBDriver *FindOrLoadDriver(const char *name);
   108 	IDBDriver *GetDefaultDriver();
   109 	const char *GetDefaultDriverName();
   110 	bool AddToThreadQueue(IDBThreadOperation *op, PrioQueueLevel prio);
   111 	void LockConfig();
   112 	void UnlockConfig();
   113 	void RunFrame();
   114 	inline HandleType_t GetDatabaseType()
   115 	{
   116 		return m_DatabaseType;
   117 	}
   118 private:
   119 	void ClearConfigs();
   120 	void KillWorkerThread();
   121 private:
   122 	CVector<IDBDriver *> m_drivers;
   123 
   124 	/* Threading stuff */
   125 	PrioQueue<IDBThreadOperation *> m_OpQueue;
   126 	Queue<IDBThreadOperation *> m_ThinkQueue;
   127 	CVector<bool> m_drSafety;			/* which drivers are safe? */
   128 	ke::AutoPtr<ke::Thread> m_Worker;
   129 	ke::ConditionVariable m_QueueEvent;
   130 	ke::Mutex m_ConfigLock;
   131 	ke::Mutex m_ThinkLock;
   132 	bool m_Terminate;
   133 
   134 	List<ConfDbInfo *> m_confs;
   135 	HandleType_t m_DriverType;
   136 	HandleType_t m_DatabaseType;
   137 	String m_DefDriver;
   138 	char m_Filename[PLATFORM_MAX_PATH];
   139 	unsigned int m_ParseLevel;
   140 	unsigned int m_ParseState;
   141 	IDBDriver *m_pDefault;
   142 };
   143 
   144 extern DBManager g_DBMan;
   145 
   146 #endif //_INCLUDE_DATABASE_MANAGER_H_