sm-ext-RevEmuAPI/extension.cpp
2019-04-12 01:29:24 +02:00

217 lines
6.2 KiB
C++

/**
* vim: set ts=4 :
* =============================================================================
* SourceMod RevEmuAPI Extension
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
#include "extension.h"
#include "revemu.h"
/**
* @file extension.cpp
* @brief Implement extension code here.
*/
RevEmuAPI g_RevEmuAPI; /**< Global singleton for extension's main interface */
SMEXT_LINK(&g_RevEmuAPI);
pRev_BGetPlayerInfoByUserID Rev_BGetPlayerInfoByUserID;
pRev_BGetPlayerInfoBySteamID Rev_BGetPlayerInfoBySteamID;
/**
* @brief
*/
static cell_t IsPlayerSteam(IPluginContext *pContext, const cell_t *params)
{
IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(params[1]);
if (pPlayer == NULL)
{
return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
}
else if (!pPlayer->IsConnected())
{
return pContext->ThrowNativeError("Client %d is not connected", params[1]);
}
else if (pPlayer->IsFakeClient())
{
return pContext->ThrowNativeError("Client %d is a bot", params[1]);
}
PlayerInfo_t PlayerInfo;
if (!Rev_BGetPlayerInfoBySteamID(pPlayer->GetSteamId64(), &PlayerInfo))
{
if (!Rev_BGetPlayerInfoByUserID(pPlayer->GetUserId(), &PlayerInfo))
{
PlayerInfo.m_ePlayerType = k_eInvalid;
}
}
return PlayerInfo.m_ePlayerType == k_eSteamLegitUser;
}
/**
* @brief
*/
static cell_t GetPlayerType(IPluginContext *pContext, const cell_t *params)
{
IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(params[1]);
if (pPlayer == NULL)
{
return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
}
else if (!pPlayer->IsConnected())
{
return pContext->ThrowNativeError("Client %d is not connected", params[1]);
}
else if (pPlayer->IsFakeClient())
{
return pContext->ThrowNativeError("Client %d is a bot", params[1]);
}
PlayerInfo_t PlayerInfo;
if (!Rev_BGetPlayerInfoBySteamID(pPlayer->GetSteamId64(), &PlayerInfo))
{
if (!Rev_BGetPlayerInfoByUserID(pPlayer->GetUserId(), &PlayerInfo))
{
PlayerInfo.m_ePlayerType = k_eInvalid;
}
}
char sPlayerType[64];
switch(PlayerInfo.m_ePlayerType)
{
case(k_eSteamLegitUser):
snprintf(sPlayerType, sizeof(sPlayerType), "SteamLegit");
break;
case(k_eSteamCrackedUser):
snprintf(sPlayerType, sizeof(sPlayerType), "SteamCracked");
break;
case(k_eRevEmuUser):
snprintf(sPlayerType, sizeof(sPlayerType), "RevEmu");
break;
case(k_eRevEmuUserOld):
snprintf(sPlayerType, sizeof(sPlayerType), "RevEmuOld");
break;
case(k_eSettiSRCScanBot):
snprintf(sPlayerType, sizeof(sPlayerType), "SRCScanBot");
break;
case(k_eRevEmuUserV74):
snprintf(sPlayerType, sizeof(sPlayerType), "RevEmuV74");
break;
case(k_eRevEmuUserVeryOld):
snprintf(sPlayerType, sizeof(sPlayerType), "RevEmuVeryOld");
break;
case(k_eUnknownUser):
snprintf(sPlayerType, sizeof(sPlayerType), "Unknown");
break;
case(k_eSteam2Legit):
snprintf(sPlayerType, sizeof(sPlayerType), "Steam2Legit");
break;
case(k_eSteam2Cracked):
snprintf(sPlayerType, sizeof(sPlayerType), "Steam2Cracked");
break;
default:
snprintf(sPlayerType, sizeof(sPlayerType), "Invalid");
break;
}
pContext->StringToLocal(params[2], params[3], sPlayerType);
return true;
}
/**
* @brief
*/
const sp_nativeinfo_t g_ExtensionNatives[] =
{
{ "RevEmu_IsPlayerSteam", IsPlayerSteam },
{ "RevEmu_GetPlayerType", GetPlayerType },
{ NULL, NULL }
};
/**
* @brief This is called after the initial loading sequence has been processed.
*
* @param error Error message buffer.
* @param maxlength Size of error message buffer.
* @param late Whether or not the module was loaded after map load.
* @return True to succeed loading, false to fail.
*/
bool RevEmuAPI::SDK_OnLoad(char *error, size_t maxlength, bool late)
{
const char *pLibSteamClientPath = NULL;
#if defined POSIX
pLibSteamClientPath = "./bin/steamclient.so";
#elif defined WIN32_LEAN_AND_MEAN
pLibSteamClientPath = "./bin/steamclient.dll";
#endif
ILibrary *pLibSteamClient = libsys->OpenLibrary(pLibSteamClientPath, NULL, 0);
if (pLibSteamClient == NULL)
{
snprintf(error, maxlength, "Unable to open library. '%s'", pLibSteamClientPath);
return false;
}
Rev_BGetPlayerInfoByUserID = (pRev_BGetPlayerInfoByUserID)pLibSteamClient->GetSymbolAddress("Rev_BGetPlayerInfoByUserID");
if (Rev_BGetPlayerInfoByUserID == NULL)
{
snprintf(error, maxlength, "Unable to find emulator symbol. 'Rev_BGetPlayerInfoByUserID'");
pLibSteamClient->CloseLibrary();
return false;
}
Rev_BGetPlayerInfoBySteamID = (pRev_BGetPlayerInfoBySteamID)pLibSteamClient->GetSymbolAddress("Rev_BGetPlayerInfoBySteamID");
if (Rev_BGetPlayerInfoBySteamID == NULL)
{
snprintf(error, maxlength, "Unable to find emulator symbol. 'Rev_BGetPlayerInfoBySteamID'");
pLibSteamClient->CloseLibrary();
return false;
}
sharesys->AddNatives(myself, g_ExtensionNatives);
sharesys->RegisterLibrary(myself, "RevEmuAPI");
return true;
}