sample_mm/sample_mm.cpp
changeset 497 e6abd02a6a95
parent 379 e8a3f2617a3e
child 507 ac946e22482b
     1.1 --- a/sample_mm/sample_mm.cpp	Sun Feb 17 22:03:10 2008 +0000
     1.2 +++ b/sample_mm/sample_mm.cpp	Sun Feb 17 22:56:20 2008 +0000
     1.3 @@ -29,10 +29,10 @@
     1.4  SH_DECL_HOOK5(IServerGameClients, ClientConnect, SH_NOATTRIB, 0, bool, edict_t *, const char*, const char *, char *, int);
     1.5  SH_DECL_HOOK2(IGameEventManager2, FireEvent, SH_NOATTRIB, 0, bool, IGameEvent *, bool);
     1.6  
     1.7 -#if defined ORANGEBOX_BUILD
     1.8 +#if defined ENGINE_ORANGEBOX
     1.9  SH_DECL_HOOK2_void(IServerGameClients, NetworkIDValidated, SH_NOATTRIB, 0, const char *, const char *);
    1.10  SH_DECL_HOOK2_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *, const CCommand &);
    1.11 -#else
    1.12 +#elif defined ENGINE_ORIGINAL
    1.13  SH_DECL_HOOK1_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *);
    1.14  #endif
    1.15  
    1.16 @@ -45,6 +45,21 @@
    1.17  IServerPluginCallbacks *vsp_callbacks = NULL;
    1.18  IPlayerInfoManager *playerinfomanager = NULL;
    1.19  
    1.20 +ConVar sample_cvar("sample_cvar", "42", 0);
    1.21 +
    1.22 +/** 
    1.23 + * Something like this is needed to register cvars/CON_COMMANDs.
    1.24 + */
    1.25 +class BaseAccessor : public IConCommandBaseAccessor
    1.26 +{
    1.27 +public:
    1.28 +	bool RegisterConCommandBase(ConCommandBase *pCommandBase)
    1.29 +	{
    1.30 +		/* Always call META_REGCVAR instead of going through the engine. */
    1.31 +		return META_REGCVAR(pCommandBase);
    1.32 +	}
    1.33 +} s_BaseAccessor;
    1.34 +
    1.35  PLUGIN_EXPOSE(StubPlugin, g_StubPlugin);
    1.36  bool StubPlugin::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool late)
    1.37  {
    1.38 @@ -59,40 +74,60 @@
    1.39  
    1.40  	META_LOG(g_PLAPI, "Starting plugin.");
    1.41  
    1.42 -	/* Load the VSP listener.  Most people won't need this. */
    1.43 +	/* Load the VSP listener.  This is usually needed for IServerPluginHelpers. */
    1.44 +#if defined METAMOD_PLAPI_VERSION
    1.45  	if ((vsp_callbacks = ismm->GetVSPInfo(NULL)) == NULL)
    1.46 +#endif
    1.47  	{
    1.48  		ismm->AddListener(this, this);
    1.49  		ismm->EnableVSPListener();
    1.50  	}
    1.51  
    1.52 -	m_hooks.push_back(SH_ADD_HOOK(IServerGameDLL, LevelInit, server, SH_MEMBER(this, &StubPlugin::Hook_LevelInit), true));
    1.53 -	m_hooks.push_back(SH_ADD_HOOK(IServerGameDLL, ServerActivate, server, SH_MEMBER(this, &StubPlugin::Hook_ServerActivate), true));
    1.54 -	m_hooks.push_back(SH_ADD_HOOK(IServerGameDLL, GameFrame, server, SH_MEMBER(this, &StubPlugin::Hook_GameFrame), true));
    1.55 -	m_hooks.push_back(SH_ADD_HOOK(IServerGameDLL, LevelShutdown, server, SH_MEMBER(this, &StubPlugin::Hook_LevelShutdown), false));
    1.56 -	m_hooks.push_back(SH_ADD_HOOK(IServerGameClients, ClientActive, gameclients, SH_MEMBER(this, &StubPlugin::Hook_ClientActive), true));
    1.57 -	m_hooks.push_back(SH_ADD_HOOK(IServerGameClients, ClientDisconnect, gameclients, SH_MEMBER(this, &StubPlugin::Hook_ClientDisconnect), true));
    1.58 -	m_hooks.push_back(SH_ADD_HOOK(IServerGameClients, ClientPutInServer, gameclients, SH_MEMBER(this, &StubPlugin::Hook_ClientPutInServer), true));
    1.59 -	m_hooks.push_back(SH_ADD_HOOK(IServerGameClients, SetCommandClient, gameclients, SH_MEMBER(this, &StubPlugin::Hook_SetCommandClient), true));
    1.60 -	m_hooks.push_back(SH_ADD_HOOK(IServerGameClients, ClientSettingsChanged, gameclients, SH_MEMBER(this, &StubPlugin::Hook_ClientSettingsChanged), false));
    1.61 -	m_hooks.push_back(SH_ADD_HOOK(IServerGameClients, ClientConnect, gameclients, SH_MEMBER(this, &StubPlugin::Hook_ClientConnect), false));
    1.62 -	m_hooks.push_back(SH_ADD_HOOK(IServerGameClients, ClientCommand, gameclients, SH_MEMBER(this, &StubPlugin::Hook_ClientCommand), false));
    1.63 +	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, server, this, &StubPlugin::Hook_LevelInit, true);
    1.64 +	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, ServerActivate, server, this, &StubPlugin::Hook_ServerActivate, true);
    1.65 +	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, GameFrame, server, this, &StubPlugin::Hook_GameFrame, true);
    1.66 +	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, server, this, &StubPlugin::Hook_LevelShutdown, false);
    1.67 +	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientActive, gameclients, this, &StubPlugin::Hook_ClientActive, true);
    1.68 +	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, gameclients, this, &StubPlugin::Hook_ClientDisconnect, true);
    1.69 +	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientPutInServer, gameclients, this, &StubPlugin::Hook_ClientPutInServer, true);
    1.70 +	SH_ADD_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, gameclients, this, &StubPlugin::Hook_SetCommandClient, true);
    1.71 +	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientSettingsChanged, gameclients, this, &StubPlugin::Hook_ClientSettingsChanged, false);
    1.72 +	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientConnect, gameclients, this, &StubPlugin::Hook_ClientConnect, false);
    1.73 +	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientCommand, gameclients, this, &StubPlugin::Hook_ClientCommand, false);
    1.74  
    1.75 -	SH_CALL(engine, &IVEngineServer::LogPrint)("All hooks started!\n");
    1.76 +#if defined ENGINE_ORIGINAL
    1.77 +	m_EngineCC = SH_GET_CALLCLASS(engine);
    1.78 +#endif
    1.79 +
    1.80 +	ENGINE_CALL(&IVEngineServer::LogPrint)("All hooks started!\n");
    1.81 +
    1.82 +#if defined ENGINE_ORANGEBOX
    1.83 +	g_pCVar = ICvar;
    1.84 +	ConVar_Register(0, &s_BaseAccessor);
    1.85 +#elif defined ENGINE_ORIGINAL
    1.86 +	ConCommandBaseMgr::OneTimeInit(&s_BaseAccessor);
    1.87 +#endif
    1.88  
    1.89  	return true;
    1.90  }
    1.91  
    1.92  bool StubPlugin::Unload(char *error, size_t maxlen)
    1.93  {
    1.94 -	for (size_t i = 0; i < m_hooks.size(); i++)
    1.95 -	{
    1.96 -		if (m_hooks[i] != 0)
    1.97 -		{
    1.98 -			SH_REMOVE_HOOK_ID(m_hooks[i]);
    1.99 -		}
   1.100 -	}
   1.101 -	m_hooks.clear();
   1.102 +	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, server, this, &StubPlugin::Hook_LevelInit, true);
   1.103 +	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, ServerActivate, server, this, &StubPlugin::Hook_ServerActivate, true);
   1.104 +	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, GameFrame, server, this, &StubPlugin::Hook_GameFrame, true);
   1.105 +	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, server, this, &StubPlugin::Hook_LevelShutdown, false);
   1.106 +	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientActive, gameclients, this, &StubPlugin::Hook_ClientActive, true);
   1.107 +	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, gameclients, this, &StubPlugin::Hook_ClientDisconnect, true);
   1.108 +	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientPutInServer, gameclients, this, &StubPlugin::Hook_ClientPutInServer, true);
   1.109 +	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, gameclients, this, &StubPlugin::Hook_SetCommandClient, true);
   1.110 +	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientSettingsChanged, gameclients, this, &StubPlugin::Hook_ClientSettingsChanged, false);
   1.111 +	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientConnect, gameclients, this, &StubPlugin::Hook_ClientConnect, false);
   1.112 +	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientCommand, gameclients, this, &StubPlugin::Hook_ClientCommand, false);
   1.113 +
   1.114 +#if defined ENGINE_ORIGINAL
   1.115 +	SH_RELEASE_CALLCLASS(m_EngineCC);
   1.116 +#endif
   1.117  
   1.118  	return true;
   1.119  }
   1.120 @@ -119,13 +154,13 @@
   1.121  	META_LOG(g_PLAPI, "Hook_ClientActive(%d, %d)", engine->IndexOfEdict(pEntity), bLoadGame);
   1.122  }
   1.123  
   1.124 -#if defined ORANGEBOX_BUILD
   1.125 +#if defined ENGINE_ORANGEBOX
   1.126  void StubPlugin::Hook_ClientCommand(edict_t *pEntity, const CCommand &args)
   1.127 -#else
   1.128 +#elif defined ENGINE_ORIGINAL
   1.129  void StubPlugin::Hook_ClientCommand(edict_t *pEntity)
   1.130  #endif
   1.131  {
   1.132 -#if !defined ORANGEBOX_BUILD
   1.133 +#if defined ENGINE_ORIGINAL
   1.134  	CCommand args;
   1.135  #endif
   1.136  
   1.137 @@ -147,9 +182,9 @@
   1.138  		for (int i = 1; i < 9; i++)
   1.139  		{
   1.140  			char num[10], msg[10], cmd[10];
   1.141 -			g_SMAPI->Format( num, sizeof(num), "%i", i );
   1.142 -			g_SMAPI->Format( msg, sizeof(msg), "Option %i", i );
   1.143 -			g_SMAPI->Format( cmd, sizeof(cmd), "option %i", i );
   1.144 +			MM_Format( num, sizeof(num), "%i", i );
   1.145 +			MM_Format( msg, sizeof(msg), "Option %i", i );
   1.146 +			MM_Format( cmd, sizeof(cmd), "option %i", i );
   1.147  
   1.148  			KeyValues *item1 = kv->FindKey(num, true);
   1.149  			item1->SetString("msg", msg);
   1.150 @@ -208,11 +243,12 @@
   1.151  
   1.152  		if (playerinfo != NULL
   1.153  			&& name != NULL
   1.154 +			&& strcmp(engine->GetPlayerNetworkIDString(pEdict), "BOT") != 0
   1.155  			&& playerinfo->GetName() != NULL
   1.156  			&& strcmp(name, playerinfo->GetName()) == 0)
   1.157  		{
   1.158  			char msg[128];
   1.159 -			g_SMAPI->Format(msg, sizeof(msg), "Your name changed to \"%s\" (from \"%s\")\n", name, playerinfo->GetName());
   1.160 +			MM_Format(msg, sizeof(msg), "Your name changed to \"%s\" (from \"%s\")\n", name, playerinfo->GetName());
   1.161  			engine->ClientPrintf(pEdict, msg);
   1.162  		}
   1.163  	}