core/logic/ShareSys.h
author Kyle Sanderson <kyle.leet@gmail.com>
Mon Aug 12 00:42:56 2013 +0100 (2013-08-12)
changeset 3994 4bc73bac0f4c
parent 3872 4c93f74cae51
child 4067 cc438da4a30c
permissions -rw-r--r--
Added helper stock to check if a command exists. (bug 5510, r=asherkin)
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@3872
    39
#include "common_logic.h"
dvander@229
    40
dvander@229
    41
using namespace SourceHook;
dvander@229
    42
dvander@229
    43
namespace SourceMod
dvander@229
    44
{
dvander@229
    45
	struct IdentityToken_t
dvander@229
    46
	{
dvander@229
    47
		Handle_t ident;
dvander@868
    48
		void *ptr;
dvander@868
    49
		IdentityType_t type;
dvander@229
    50
	};
dvander@229
    51
};
dvander@229
    52
dvander@229
    53
struct IfaceInfo
dvander@229
    54
{
dvander@1456
    55
	bool operator ==(const IfaceInfo &info)
dvander@1456
    56
	{
dvander@1456
    57
		return (info.iface == iface && info.owner == owner);
dvander@1456
    58
	}
dvander@229
    59
	SMInterface *iface;
dvander@304
    60
	IExtension *owner;
dvander@229
    61
};
dvander@229
    62
dvander@2086
    63
class CNativeOwner;
dvander@3872
    64
struct NativeEntry;
dvander@2086
    65
class CPlugin;
dvander@2086
    66
dvander@2086
    67
struct ReplaceNative
dvander@2086
    68
{
dvander@2086
    69
	CNativeOwner *owner;
dvander@2086
    70
	SPVM_NATIVE_FUNC func;
dvander@2086
    71
};
dvander@2086
    72
dvander@2086
    73
struct FakeNative
dvander@2086
    74
{
dvander@2086
    75
	char name[64];
dvander@2086
    76
	IPluginContext *ctx;
dvander@2086
    77
	IPluginFunction *call;
dvander@2086
    78
};
dvander@2086
    79
dvander@2086
    80
struct NativeEntry
dvander@2086
    81
{
dvander@2086
    82
	CNativeOwner *owner;
dvander@2086
    83
	SPVM_NATIVE_FUNC func;
dvander@2086
    84
	const char *name;
dvander@2086
    85
	ReplaceNative replacement;
dvander@2086
    86
	FakeNative *fake;
dvander@2086
    87
};
dvander@2086
    88
dvander@2841
    89
struct Capability
dvander@2841
    90
{
dvander@2841
    91
	IExtension *ext;
dvander@2841
    92
	IFeatureProvider *provider;
dvander@2841
    93
};
dvander@2841
    94
dvander@2841
    95
class ShareSystem :
dvander@229
    96
	public IShareSys,
dvander@229
    97
	public SMGlobalClass,
dvander@229
    98
	public IHandleTypeDispatch
dvander@229
    99
{
dvander@2086
   100
	friend class CNativeOwner;
dvander@229
   101
public:
dvander@229
   102
	ShareSystem();
dvander@229
   103
public: //IShareSys
dvander@304
   104
	bool AddInterface(IExtension *myself, SMInterface *pIface);
dvander@229
   105
	bool RequestInterface(const char *iface_name, 
dvander@229
   106
		unsigned int iface_vers,
ds@741
   107
		IExtension *myself,
dvander@229
   108
		SMInterface **pIface);
dvander@312
   109
	void AddNatives(IExtension *myself, const sp_nativeinfo_t *natives);
dvander@229
   110
	IdentityType_t CreateIdentType(const char *name);
dvander@229
   111
	IdentityType_t FindIdentType(const char *name);
dvander@868
   112
	IdentityToken_t *CreateIdentity(IdentityType_t type, void *ptr);
dvander@229
   113
	void DestroyIdentType(IdentityType_t type);
dvander@229
   114
	void DestroyIdentity(IdentityToken_t *identity);
dvander@324
   115
	void AddDependency(IExtension *myself, const char *filename, bool require, bool autoload);
dvander@1572
   116
	void RegisterLibrary(IExtension *myself, const char *name);
dvander@1689
   117
	void OverrideNatives(IExtension *myself, const sp_nativeinfo_t *natives);
dvander@2841
   118
	void AddCapabilityProvider(IExtension *myself, IFeatureProvider *provider,
dvander@2841
   119
		                       const char *name);
dvander@2841
   120
	void DropCapabilityProvider(IExtension *myself, IFeatureProvider *provider,
dvander@2841
   121
		                        const char *name);
dvander@229
   122
public: //SMGlobalClass
dvander@229
   123
	/* Pre-empt in case anything tries to register idents early */
dvander@1770
   124
	void Initialize();
dvander@229
   125
	void OnSourceModShutdown();
dvander@229
   126
public: //IHandleTypeDispatch
dvander@229
   127
	void OnHandleDestroy(HandleType_t type, void *object);
dvander@229
   128
public:
dvander@229
   129
	IdentityToken_t *CreateCoreIdentity();
dvander@304
   130
	void RemoveInterfaces(IExtension *pExtension);
dvander@2841
   131
	FeatureStatus TestFeature(IPluginRuntime *pRuntime, FeatureType feature, const char *name);
dvander@2841
   132
	FeatureStatus TestNative(IPluginRuntime *pRuntime, const char *name);
dvander@2841
   133
	FeatureStatus TestCap(const char *name);
dvander@229
   134
public:
dvander@229
   135
	inline IdentityToken_t *GetIdentRoot()
dvander@229
   136
	{
dvander@229
   137
		return &m_IdentRoot;
dvander@229
   138
	}
dvander@2086
   139
public:
dvander@2086
   140
	void BindNativesToPlugin(CPlugin *pPlugin, bool bCoreOnly);
dvander@2086
   141
	void BindNativeToPlugin(CPlugin *pPlugin, NativeEntry *pEntry);
dvander@2086
   142
	NativeEntry *AddFakeNative(IPluginFunction *pFunc, const char *name, SPVM_FAKENATIVE_FUNC func);
dvander@2545
   143
	NativeEntry *FindNative(const char *name);
dvander@2086
   144
private:
dvander@2086
   145
	NativeEntry *AddNativeToCache(CNativeOwner *pOwner, const sp_nativeinfo_t *ntv);
dvander@2086
   146
	void ClearNativeFromCache(CNativeOwner *pOwner, const char *name);
dvander@2086
   147
	void BindNativeToPlugin(CPlugin *pPlugin, 
dvander@2086
   148
		sp_native_t *ntv, 
dvander@2086
   149
		uint32_t index, 
dvander@2086
   150
		NativeEntry *pEntry);
dvander@229
   151
private:
dvander@229
   152
	List<IfaceInfo> m_Interfaces;
dvander@229
   153
	HandleType_t m_TypeRoot;
dvander@229
   154
	IdentityToken_t m_IdentRoot;
dvander@229
   155
	HandleType_t m_IfaceType;
dvander@229
   156
	IdentityType_t m_CoreType;
dvander@2086
   157
	KTrie<NativeEntry *> m_NtvCache;
dvander@2841
   158
	KTrie<Capability> m_caps;
dvander@229
   159
};
dvander@229
   160
dvander@229
   161
extern ShareSystem g_ShareSys;
dvander@229
   162
dvander@229
   163
#endif //_INCLUDE_SOURCEMOD_SHARESYSTEM_H_