1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/core/Database.h Sun Jul 06 04:49:55 2008 +0000
1.3 @@ -0,0 +1,150 @@
1.4 +/**
1.5 + * vim: set ts=4 :
1.6 + * =============================================================================
1.7 + * SourceMod
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 +#ifndef _INCLUDE_DATABASE_MANAGER_H_
1.36 +#define _INCLUDE_DATABASE_MANAGER_H_
1.37 +
1.38 +#include <IDBDriver.h>
1.39 +#include "sm_globals.h"
1.40 +#include <sh_vector.h>
1.41 +#include <sh_string.h>
1.42 +#include <sh_list.h>
1.43 +#include <ITextParsers.h>
1.44 +#include "sm_memtable.h"
1.45 +#include <IThreader.h>
1.46 +#include "sm_simple_prioqueue.h"
1.47 +#include "PluginSys.h"
1.48 +
1.49 +using namespace SourceHook;
1.50 +
1.51 +struct ConfDbInfo
1.52 +{
1.53 + ConfDbInfo() : name(-1), driver(-1), host(-1), user(-1), pass(-1),
1.54 + database(-1), realDriver(NULL)
1.55 + {
1.56 + }
1.57 + int name;
1.58 + int driver;
1.59 + int host;
1.60 + int user;
1.61 + int pass;
1.62 + int database;
1.63 + IDBDriver *realDriver;
1.64 + DatabaseInfo info;
1.65 +};
1.66 +
1.67 +class DBManager :
1.68 + public IDBManager,
1.69 + public SMGlobalClass,
1.70 + public IHandleTypeDispatch,
1.71 + public ITextListener_SMC,
1.72 + public IThread,
1.73 + public IThreadWorkerCallbacks,
1.74 + public IPluginsListener
1.75 +{
1.76 +public:
1.77 + DBManager();
1.78 +public:
1.79 + const char *GetInterfaceName();
1.80 + unsigned int GetInterfaceVersion();
1.81 +public: //SMGlobalClass
1.82 + void OnSourceModAllInitialized();
1.83 + void OnSourceModLevelChange(const char *mapName);
1.84 + void OnSourceModIdentityDropped(IdentityToken_t *pToken);
1.85 + void OnSourceModShutdown();
1.86 +public: //IHandleTypeDispatch
1.87 + void OnHandleDestroy(HandleType_t type, void *object);
1.88 +public: //IDBManager
1.89 + void AddDriver(IDBDriver *pDrivera);
1.90 + void RemoveDriver(IDBDriver *pDriver);
1.91 + const DatabaseInfo *FindDatabaseConf(const char *name);
1.92 + bool Connect(const char *name, IDBDriver **pdr, IDatabase **pdb, bool persistent, char *error, size_t maxlength);
1.93 + unsigned int GetDriverCount();
1.94 + IDBDriver *GetDriver(unsigned int index);
1.95 + Handle_t CreateHandle(DBHandleType type, void *ptr, IdentityToken_t *pToken);
1.96 + HandleError ReadHandle(Handle_t hndl, DBHandleType type, void **ptr);
1.97 + HandleError ReleaseHandle(Handle_t hndl, DBHandleType type, IdentityToken_t *token);
1.98 + void AddDependency(IExtension *myself, IDBDriver *driver);
1.99 +public: //ITextListener_SMC
1.100 + void ReadSMC_ParseStart();
1.101 + SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name);
1.102 + SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value);
1.103 + SMCResult ReadSMC_LeavingSection(const SMCStates *states);
1.104 + void ReadSMC_ParseEnd(bool halted, bool failed);
1.105 +public: //IThread
1.106 + void RunThread(IThreadHandle *pThread);
1.107 + void OnTerminate(IThreadHandle *pThread, bool cancel);
1.108 +public: //IThreadWorkerCallbacks
1.109 + void OnWorkerStart(IThreadWorker *pWorker);
1.110 + void OnWorkerStop(IThreadWorker *pWorker);
1.111 +public: //IPluginsListener
1.112 + void OnPluginUnloaded(IPlugin *plugin);
1.113 +public:
1.114 + ConfDbInfo *GetDatabaseConf(const char *name);
1.115 + IDBDriver *FindOrLoadDriver(const char *name);
1.116 + IDBDriver *GetDefaultDriver();
1.117 + const char *GetDefaultDriverName();
1.118 + bool AddToThreadQueue(IDBThreadOperation *op, PrioQueueLevel prio);
1.119 + void LockConfig();
1.120 + void UnlockConfig();
1.121 + void RunFrame();
1.122 + inline HandleType_t GetDatabaseType()
1.123 + {
1.124 + return m_DatabaseType;
1.125 + }
1.126 +private:
1.127 + void KillWorkerThread();
1.128 +private:
1.129 + CVector<IDBDriver *> m_drivers;
1.130 +
1.131 + /* Threading stuff */
1.132 + PrioQueue<IDBThreadOperation *> m_OpQueue;
1.133 + Queue<IDBThreadOperation *> m_ThinkQueue;
1.134 + CVector<bool> m_drSafety; /* which drivers are safe? */
1.135 + IThreadWorker *m_pWorker; /* Worker thread object */
1.136 + IMutex *m_pConfigLock; /* Configuration lock */
1.137 + IMutex *m_pQueueLock; /* Queue safety lock */
1.138 + IMutex *m_pThinkLock; /* Think-queue lock */
1.139 +
1.140 + List<ConfDbInfo> m_confs;
1.141 + HandleType_t m_DriverType;
1.142 + HandleType_t m_DatabaseType;
1.143 + String m_DefDriver;
1.144 + BaseStringTable m_StrTab;
1.145 + char m_Filename[PLATFORM_MAX_PATH];
1.146 + unsigned int m_ParseLevel;
1.147 + unsigned int m_ParseState;
1.148 + IDBDriver *m_pDefault;
1.149 +};
1.150 +
1.151 +extern DBManager g_DBMan;
1.152 +
1.153 +#endif //_INCLUDE_DATABASE_MANAGER_H_