core/Database.h
author Nicholas Hastings <skamonkey@gmail.com>
Fri Aug 30 14:15:02 2013 -0400 (2013-08-30)
changeset 4102 0eb66c45a884
parent 4031 852cf4415d12
child 4111 34b8688e3bec
permissions -rw-r--r--
Reserved slot fixes for OB games when SourceTV or Replay are enabled (bug 5499, r=drifter).
dvander@667
     1
/**
dvander@4027
     2
 * vim: set ts=4 sw=4 tw=99 noet :
ds@1330
     3
 * =============================================================================
dvander@1231
     4
 * SourceMod
ds@1947
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1330
     6
 * =============================================================================
dvander@667
     7
 *
ds@1330
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1330
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1330
    10
 * Free Software Foundation.
dvander@1231
    11
 * 
ds@1330
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1330
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1330
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1330
    15
 * details.
dvander@1231
    16
 *
ds@1330
    17
 * You should have received a copy of the GNU General Public License along with
ds@1330
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
dvander@1231
    19
 *
ds@1330
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1330
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1330
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1330
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1330
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1330
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1330
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1330
    27
 * or <http://www.sourcemod.net/license.php>.
dvander@667
    28
 *
dvander@667
    29
 * Version: $Id$
dvander@667
    30
 */
dvander@667
    31
dvander@666
    32
#ifndef _INCLUDE_DATABASE_MANAGER_H_
dvander@666
    33
#define _INCLUDE_DATABASE_MANAGER_H_
dvander@666
    34
dvander@666
    35
#include <IDBDriver.h>
dvander@666
    36
#include "sm_globals.h"
dvander@666
    37
#include <sh_vector.h>
dvander@868
    38
#include <sh_string.h>
dvander@868
    39
#include <sh_list.h>
dvander@868
    40
#include <ITextParsers.h>
dvander@868
    41
#include "sm_memtable.h"
dvander@1096
    42
#include <IThreader.h>
dvander@3872
    43
#include <IPluginSys.h>
dvander@4031
    44
#include <am-thread-utils.h>
dvander@1096
    45
#include "sm_simple_prioqueue.h"
dvander@666
    46
dvander@666
    47
using namespace SourceHook;
dvander@666
    48
dvander@868
    49
struct ConfDbInfo
dvander@868
    50
{
fyrenmoo@3139
    51
	ConfDbInfo() : realDriver(NULL)
dvander@868
    52
	{
dvander@868
    53
	}
fyrenmoo@3139
    54
	String name;
fyrenmoo@3139
    55
	String driver;
fyrenmoo@3139
    56
	String host;
fyrenmoo@3139
    57
	String user;
fyrenmoo@3139
    58
	String pass;
fyrenmoo@3139
    59
	String database;
dvander@868
    60
	IDBDriver *realDriver;
dvander@868
    61
	DatabaseInfo info;
dvander@868
    62
};
dvander@868
    63
dvander@666
    64
class DBManager : 
dvander@666
    65
	public IDBManager,
dvander@666
    66
	public SMGlobalClass,
dvander@868
    67
	public IHandleTypeDispatch,
dvander@1096
    68
	public ITextListener_SMC,
dvander@4027
    69
	public IPluginsListener,
dvander@4027
    70
	public ke::IRunnable
dvander@666
    71
{
dvander@858
    72
public:
dvander@868
    73
	DBManager();
dvander@868
    74
public:
dvander@666
    75
	const char *GetInterfaceName();
dvander@666
    76
	unsigned int GetInterfaceVersion();
dvander@666
    77
public: //SMGlobalClass
dvander@666
    78
	void OnSourceModAllInitialized();
dvander@868
    79
	void OnSourceModLevelChange(const char *mapName);
dvander@1157
    80
	void OnSourceModIdentityDropped(IdentityToken_t *pToken);
dvander@858
    81
	void OnSourceModShutdown();
dvander@666
    82
public: //IHandleTypeDispatch
dvander@666
    83
	void OnHandleDestroy(HandleType_t type, void *object);
dvander@666
    84
public: //IDBManager
dvander@858
    85
	void AddDriver(IDBDriver *pDrivera);
dvander@666
    86
	void RemoveDriver(IDBDriver *pDriver);
dvander@666
    87
	const DatabaseInfo *FindDatabaseConf(const char *name);
dvander@858
    88
	bool Connect(const char *name, IDBDriver **pdr, IDatabase **pdb, bool persistent, char *error, size_t maxlength);
dvander@666
    89
	unsigned int GetDriverCount();
dvander@858
    90
	IDBDriver *GetDriver(unsigned int index);
dvander@858
    91
	Handle_t CreateHandle(DBHandleType type, void *ptr, IdentityToken_t *pToken);
dvander@858
    92
	HandleError ReadHandle(Handle_t hndl, DBHandleType type, void **ptr);
dvander@858
    93
	HandleError ReleaseHandle(Handle_t hndl, DBHandleType type, IdentityToken_t *token);
dvander@2134
    94
	void AddDependency(IExtension *myself, IDBDriver *driver);
dvander@868
    95
public: //ITextListener_SMC
dvander@868
    96
	void ReadSMC_ParseStart();
dvander@1656
    97
	SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name);
dvander@1656
    98
	SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value);
dvander@1656
    99
	SMCResult ReadSMC_LeavingSection(const SMCStates *states);
dvander@868
   100
	void ReadSMC_ParseEnd(bool halted, bool failed);
dvander@4027
   101
public: //ke::IRunnable
dvander@4027
   102
	void Run();
dvander@4027
   103
	void ThreadMain();
dvander@1096
   104
public: //IPluginsListener
dvander@1096
   105
	void OnPluginUnloaded(IPlugin *plugin);
dvander@868
   106
public:
dvander@868
   107
	ConfDbInfo *GetDatabaseConf(const char *name);
dvander@868
   108
	IDBDriver *FindOrLoadDriver(const char *name);
dvander@868
   109
	IDBDriver *GetDefaultDriver();
dvander@1096
   110
	const char *GetDefaultDriverName();
dvander@1096
   111
	bool AddToThreadQueue(IDBThreadOperation *op, PrioQueueLevel prio);
dvander@1096
   112
	void LockConfig();
dvander@1096
   113
	void UnlockConfig();
dvander@1096
   114
	void RunFrame();
dvander@1096
   115
	inline HandleType_t GetDatabaseType()
dvander@1096
   116
	{
dvander@1096
   117
		return m_DatabaseType;
dvander@1096
   118
	}
dvander@1096
   119
private:
dvander@3140
   120
	void ClearConfigs();
dvander@1096
   121
	void KillWorkerThread();
dvander@666
   122
private:
dvander@858
   123
	CVector<IDBDriver *> m_drivers;
dvander@1096
   124
dvander@1096
   125
	/* Threading stuff */
dvander@1096
   126
	PrioQueue<IDBThreadOperation *> m_OpQueue;
dvander@1096
   127
	Queue<IDBThreadOperation *> m_ThinkQueue;
dvander@1096
   128
	CVector<bool> m_drSafety;			/* which drivers are safe? */
dvander@4027
   129
	ke::AutoPtr<ke::Thread> m_Worker;
dvander@4027
   130
	ke::ConditionVariable m_QueueEvent;
dvander@4027
   131
	ke::Mutex m_ConfigLock;
dvander@4027
   132
	ke::Mutex m_ThinkLock;
dvander@4027
   133
	bool m_Terminate;
dvander@1096
   134
dvander@3140
   135
	List<ConfDbInfo *> m_confs;
dvander@666
   136
	HandleType_t m_DriverType;
dvander@666
   137
	HandleType_t m_DatabaseType;
dvander@868
   138
	String m_DefDriver;
dvander@868
   139
	char m_Filename[PLATFORM_MAX_PATH];
dvander@868
   140
	unsigned int m_ParseLevel;
dvander@868
   141
	unsigned int m_ParseState;
dvander@868
   142
	IDBDriver *m_pDefault;
dvander@666
   143
};
dvander@666
   144
dvander@666
   145
extern DBManager g_DBMan;
dvander@666
   146
dvander@666
   147
#endif //_INCLUDE_DATABASE_MANAGER_H_