core/ShareSys.h
author David Anderson <dvander@alliedmods.net>
Sun Nov 16 16:06:18 2008 -0800 (2008-11-16)
changeset 2411 8dfaa3d730b5
parent 2289 core/systems/ShareSys.h@44045ead61cd
child 2545 9cb1a1fefc2c
permissions -rw-r--r--
Removed pointless "systems" folder from core.
dvander@370
     1
/**
ds@653
     2
 * vim: set ts=4 :
ds@1330
     3
 * =============================================================================
dvander@1231
     4
 * SourceMod
ds@1947
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1330
     6
 * =============================================================================
dvander@370
     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@370
    28
 *
dvander@370
    29
 * Version: $Id$
dvander@370
    30
 */
dvander@370
    31
dvander@229
    32
#ifndef _INCLUDE_SOURCEMOD_SHARESYSTEM_H_
dvander@229
    33
#define _INCLUDE_SOURCEMOD_SHARESYSTEM_H_
dvander@229
    34
dvander@229
    35
#include <IShareSys.h>
dvander@229
    36
#include <IHandleSys.h>
dvander@229
    37
#include <sh_list.h>
dvander@2086
    38
#include <sm_trie_tpl.h>
dvander@229
    39
#include "sm_globals.h"
dvander@229
    40
#include "sourcemod.h"
dvander@229
    41
dvander@229
    42
using namespace SourceHook;
dvander@229
    43
dvander@229
    44
namespace SourceMod
dvander@229
    45
{
dvander@229
    46
	struct IdentityToken_t
dvander@229
    47
	{
dvander@229
    48
		Handle_t ident;
dvander@868
    49
		void *ptr;
dvander@868
    50
		IdentityType_t type;
dvander@229
    51
	};
dvander@229
    52
};
dvander@229
    53
dvander@229
    54
struct IfaceInfo
dvander@229
    55
{
dvander@1456
    56
	bool operator ==(const IfaceInfo &info)
dvander@1456
    57
	{
dvander@1456
    58
		return (info.iface == iface && info.owner == owner);
dvander@1456
    59
	}
dvander@229
    60
	SMInterface *iface;
dvander@304
    61
	IExtension *owner;
dvander@229
    62
};
dvander@229
    63
dvander@2086
    64
class CNativeOwner;
dvander@2086
    65
class CPlugin;
dvander@2086
    66
struct NativeEntry;
dvander@2086
    67
dvander@2086
    68
struct ReplaceNative
dvander@2086
    69
{
dvander@2086
    70
	CNativeOwner *owner;
dvander@2086
    71
	SPVM_NATIVE_FUNC func;
dvander@2086
    72
};
dvander@2086
    73
dvander@2086
    74
struct FakeNative
dvander@2086
    75
{
dvander@2086
    76
	char name[64];
dvander@2086
    77
	IPluginContext *ctx;
dvander@2086
    78
	IPluginFunction *call;
dvander@2086
    79
};
dvander@2086
    80
dvander@2086
    81
struct NativeEntry
dvander@2086
    82
{
dvander@2086
    83
	CNativeOwner *owner;
dvander@2086
    84
	SPVM_NATIVE_FUNC func;
dvander@2086
    85
	const char *name;
dvander@2086
    86
	ReplaceNative replacement;
dvander@2086
    87
	FakeNative *fake;
dvander@2086
    88
};
dvander@2086
    89
dvander@229
    90
class ShareSystem : 
dvander@229
    91
	public IShareSys,
dvander@229
    92
	public SMGlobalClass,
dvander@229
    93
	public IHandleTypeDispatch
dvander@229
    94
{
dvander@2086
    95
	friend class CNativeOwner;
dvander@229
    96
public:
dvander@229
    97
	ShareSystem();
dvander@229
    98
public: //IShareSys
dvander@304
    99
	bool AddInterface(IExtension *myself, SMInterface *pIface);
dvander@229
   100
	bool RequestInterface(const char *iface_name, 
dvander@229
   101
		unsigned int iface_vers,
ds@741
   102
		IExtension *myself,
dvander@229
   103
		SMInterface **pIface);
dvander@312
   104
	void AddNatives(IExtension *myself, const sp_nativeinfo_t *natives);
dvander@229
   105
	IdentityType_t CreateIdentType(const char *name);
dvander@229
   106
	IdentityType_t FindIdentType(const char *name);
dvander@868
   107
	IdentityToken_t *CreateIdentity(IdentityType_t type, void *ptr);
dvander@229
   108
	void DestroyIdentType(IdentityType_t type);
dvander@229
   109
	void DestroyIdentity(IdentityToken_t *identity);
dvander@324
   110
	void AddDependency(IExtension *myself, const char *filename, bool require, bool autoload);
dvander@1572
   111
	void RegisterLibrary(IExtension *myself, const char *name);
dvander@1689
   112
	void OverrideNatives(IExtension *myself, const sp_nativeinfo_t *natives);
dvander@229
   113
public: //SMGlobalClass
dvander@229
   114
	/* Pre-empt in case anything tries to register idents early */
dvander@1770
   115
	void Initialize();
dvander@229
   116
	void OnSourceModShutdown();
dvander@229
   117
public: //IHandleTypeDispatch
dvander@229
   118
	void OnHandleDestroy(HandleType_t type, void *object);
dvander@229
   119
public:
dvander@229
   120
	IdentityToken_t *CreateCoreIdentity();
dvander@304
   121
	void RemoveInterfaces(IExtension *pExtension);
dvander@229
   122
public:
dvander@229
   123
	inline IdentityToken_t *GetIdentRoot()
dvander@229
   124
	{
dvander@229
   125
		return &m_IdentRoot;
dvander@229
   126
	}
dvander@2086
   127
public:
dvander@2086
   128
	void BindNativesToPlugin(CPlugin *pPlugin, bool bCoreOnly);
dvander@2086
   129
	void BindNativeToPlugin(CPlugin *pPlugin, NativeEntry *pEntry);
dvander@2086
   130
	NativeEntry *AddFakeNative(IPluginFunction *pFunc, const char *name, SPVM_FAKENATIVE_FUNC func);
dvander@2086
   131
private:
dvander@2086
   132
	NativeEntry *AddNativeToCache(CNativeOwner *pOwner, const sp_nativeinfo_t *ntv);
dvander@2086
   133
	void ClearNativeFromCache(CNativeOwner *pOwner, const char *name);
dvander@2086
   134
	NativeEntry *FindNative(const char *name);
dvander@2086
   135
	void BindNativeToPlugin(CPlugin *pPlugin, 
dvander@2086
   136
		sp_native_t *ntv, 
dvander@2086
   137
		uint32_t index, 
dvander@2086
   138
		NativeEntry *pEntry);
dvander@229
   139
private:
dvander@229
   140
	List<IfaceInfo> m_Interfaces;
dvander@229
   141
	HandleType_t m_TypeRoot;
dvander@229
   142
	IdentityToken_t m_IdentRoot;
dvander@229
   143
	HandleType_t m_IfaceType;
dvander@229
   144
	IdentityType_t m_CoreType;
dvander@2086
   145
	KTrie<NativeEntry *> m_NtvCache;
dvander@229
   146
};
dvander@229
   147
dvander@229
   148
extern ShareSystem g_ShareSys;
dvander@229
   149
dvander@229
   150
#endif //_INCLUDE_SOURCEMOD_SHARESYSTEM_H_