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