core/logic/ShareSys.h
author David Anderson <dvander@alliedmods.net>
Sun Aug 25 11:59:48 2013 -0700 (2013-08-25)
changeset 4069 336e32fe098f
parent 4067 cc438da4a30c
child 4106 a39557bde4ac
permissions -rw-r--r--
Switch HalfLife2 off KTrie (bug 5884 part 7, r=ds).
dvander@370
     1
/**
dvander@4067
     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@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@4067
    38
#include <sm_stringhashmap.h>
dvander@4067
    39
#include <sm_namehashset.h>
dvander@3872
    40
#include "common_logic.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@3872
    65
struct NativeEntry;
dvander@2086
    66
class CPlugin;
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@4067
    88
dvander@4067
    89
	static inline bool matches(const char *name, const NativeEntry *entry)
dvander@4067
    90
	{
dvander@4067
    91
		return strcmp(name, entry->name) == 0;
dvander@4067
    92
	}
dvander@2086
    93
};
dvander@2086
    94
dvander@2841
    95
struct Capability
dvander@2841
    96
{
dvander@2841
    97
	IExtension *ext;
dvander@2841
    98
	IFeatureProvider *provider;
dvander@2841
    99
};
dvander@2841
   100
dvander@2841
   101
class ShareSystem :
dvander@229
   102
	public IShareSys,
dvander@229
   103
	public SMGlobalClass,
dvander@229
   104
	public IHandleTypeDispatch
dvander@229
   105
{
dvander@2086
   106
	friend class CNativeOwner;
dvander@229
   107
public:
dvander@229
   108
	ShareSystem();
dvander@229
   109
public: //IShareSys
dvander@304
   110
	bool AddInterface(IExtension *myself, SMInterface *pIface);
dvander@229
   111
	bool RequestInterface(const char *iface_name, 
dvander@229
   112
		unsigned int iface_vers,
ds@741
   113
		IExtension *myself,
dvander@229
   114
		SMInterface **pIface);
dvander@312
   115
	void AddNatives(IExtension *myself, const sp_nativeinfo_t *natives);
dvander@229
   116
	IdentityType_t CreateIdentType(const char *name);
dvander@229
   117
	IdentityType_t FindIdentType(const char *name);
dvander@868
   118
	IdentityToken_t *CreateIdentity(IdentityType_t type, void *ptr);
dvander@229
   119
	void DestroyIdentType(IdentityType_t type);
dvander@229
   120
	void DestroyIdentity(IdentityToken_t *identity);
dvander@324
   121
	void AddDependency(IExtension *myself, const char *filename, bool require, bool autoload);
dvander@1572
   122
	void RegisterLibrary(IExtension *myself, const char *name);
dvander@1689
   123
	void OverrideNatives(IExtension *myself, const sp_nativeinfo_t *natives);
dvander@2841
   124
	void AddCapabilityProvider(IExtension *myself, IFeatureProvider *provider,
dvander@2841
   125
		                       const char *name);
dvander@2841
   126
	void DropCapabilityProvider(IExtension *myself, IFeatureProvider *provider,
dvander@2841
   127
		                        const char *name);
dvander@229
   128
public: //SMGlobalClass
dvander@229
   129
	/* Pre-empt in case anything tries to register idents early */
dvander@1770
   130
	void Initialize();
dvander@229
   131
	void OnSourceModShutdown();
dvander@229
   132
public: //IHandleTypeDispatch
dvander@229
   133
	void OnHandleDestroy(HandleType_t type, void *object);
dvander@229
   134
public:
dvander@229
   135
	IdentityToken_t *CreateCoreIdentity();
dvander@304
   136
	void RemoveInterfaces(IExtension *pExtension);
dvander@2841
   137
	FeatureStatus TestFeature(IPluginRuntime *pRuntime, FeatureType feature, const char *name);
dvander@2841
   138
	FeatureStatus TestNative(IPluginRuntime *pRuntime, const char *name);
dvander@2841
   139
	FeatureStatus TestCap(const char *name);
dvander@229
   140
public:
dvander@229
   141
	inline IdentityToken_t *GetIdentRoot()
dvander@229
   142
	{
dvander@229
   143
		return &m_IdentRoot;
dvander@229
   144
	}
dvander@2086
   145
public:
dvander@2086
   146
	void BindNativesToPlugin(CPlugin *pPlugin, bool bCoreOnly);
dvander@2086
   147
	void BindNativeToPlugin(CPlugin *pPlugin, NativeEntry *pEntry);
dvander@2086
   148
	NativeEntry *AddFakeNative(IPluginFunction *pFunc, const char *name, SPVM_FAKENATIVE_FUNC func);
dvander@2545
   149
	NativeEntry *FindNative(const char *name);
dvander@2086
   150
private:
dvander@2086
   151
	NativeEntry *AddNativeToCache(CNativeOwner *pOwner, const sp_nativeinfo_t *ntv);
dvander@2086
   152
	void ClearNativeFromCache(CNativeOwner *pOwner, const char *name);
dvander@2086
   153
	void BindNativeToPlugin(CPlugin *pPlugin, 
dvander@2086
   154
		sp_native_t *ntv, 
dvander@2086
   155
		uint32_t index, 
dvander@2086
   156
		NativeEntry *pEntry);
dvander@229
   157
private:
dvander@229
   158
	List<IfaceInfo> m_Interfaces;
dvander@229
   159
	HandleType_t m_TypeRoot;
dvander@229
   160
	IdentityToken_t m_IdentRoot;
dvander@229
   161
	HandleType_t m_IfaceType;
dvander@229
   162
	IdentityType_t m_CoreType;
dvander@4067
   163
	NameHashSet<NativeEntry *> m_NtvCache;
dvander@4067
   164
	StringHashMap<Capability> m_caps;
dvander@229
   165
};
dvander@229
   166
dvander@229
   167
extern ShareSystem g_ShareSys;
dvander@229
   168
dvander@229
   169
#endif //_INCLUDE_SOURCEMOD_SHARESYSTEM_H_