#include <amxmodx>
#include <sockets>
#define QUERY_OFFSET 4
#define MAX_RESPONSE_LEN 128
#define TASK_ID_GET_CHALLENGE 5252
#define TASK_ID_GET_DATA 5255
new cpHltvIp, cpHltvPort, cpHltvPasswd, cpHltvDemoPath;
new cv_szHltvIp[MAX_IP_LENGTH], cv_iHltvPort, cv_szHltvPasswd[MAX_NAME_LENGTH], cv_szHltvDemoPath[MAX_NAME_LENGTH];
new g_hSocket, g_iSocketError, g_szRconChallenge[MAX_NAME_LENGTH];
new bool:g_bHltvIsReady;
enum eHltvCmd
{
HltvCmdGetChallenge = 1,
HltvCmdStartRecord,
HltvCmdStopRecord
}
public plugin_init()
{
cpHltvIp = register_cvar("hadr_hltv_ip", "127.0.0.1");
cpHltvPort = register_cvar("hadr_hltv_port", "27020");
cpHltvPasswd = register_cvar("hadr_hltv_rconpw", "123456", FCVAR_PROTECTED);
cpHltvDemoPath = register_cvar("hadr_hltv_demo_path", "hltv/");
register_concmd("amx_hltvstart", "CmdCommandStart", ADMIN_RCON);
register_concmd("amx_hltvstop", "CmdCommandStop", ADMIN_RCON);
}
public CmdCommandStart(id)
FnSendRconCommand(fmt("%d", HltvCmdStartRecord));
public CmdCommandStop(id)
FnSendRconCommand(fmt("%d", HltvCmdStopRecord));
public plugin_cfg()
{
get_pcvar_string(cpHltvIp, cv_szHltvIp, charsmax(cv_szHltvIp));
get_pcvar_string(cpHltvPasswd, cv_szHltvPasswd, charsmax(cv_szHltvPasswd));
get_pcvar_string(cpHltvDemoPath, cv_szHltvDemoPath, charsmax(cv_szHltvDemoPath));
cv_iHltvPort = get_pcvar_num(cpHltvPort);
}
new bool:g_bHltvIsRecording
public client_authorized(id, const auth[])
{
if(auth[0] == 'H')
{
log_amx( "HLTV connected");
set_task(60.0, "FnCheckActiveRecord");
}
}
public FnCheckActiveRecord()
FnSendRconCommand(fmt("%d", HltvCmdStartRecord));
FnCreateSocket()
{
if(g_hSocket)
socket_close(g_hSocket);
g_hSocket = socket_open(cv_szHltvIp, cv_iHltvPort, SOCKET_UDP, g_iSocketError);
if(g_hSocket <= 0 || g_iSocketError)
{
switch( g_iSocketError )
{
case 1: log_amx( "Error creating TCP socket");
case 2: log_amx( "Error resolving the hostname");
case 3: log_amx( "Error connecting a TCP socket");
}
socket_close(g_hSocket);
}
return g_hSocket;
}
FnSendRconCommand(HltvCmd[])
{
if(!(g_hSocket = FnCreateSocket()))
return;
new szData[MAX_RESPONSE_LEN];
setc(szData, QUERY_OFFSET, 0xff);
format(szData, charsmax(szData),"%schallenge rcon^n", szData);
socket_send(g_hSocket, szData, charsmax(szData));
set_task(3.0, "FnGetHltvChallengePost", !g_bHltvIsReady ? TASK_ID_GET_CHALLENGE : 0, HltvCmd, QUERY_OFFSET);
}
public FnGetHltvChallengePost(HltvCmd[], iTaskId)
{
new szData[MAX_RESPONSE_LEN];
if(iTaskId == TASK_ID_GET_CHALLENGE)
{
if(socket_is_readable(g_hSocket))
{
socket_recv(g_hSocket, szData, charsmax(szData));
parse(szData, g_szRconChallenge, charsmax(g_szRconChallenge), g_szRconChallenge, charsmax(g_szRconChallenge), g_szRconChallenge, charsmax(g_szRconChallenge));
trim(g_szRconChallenge);
g_bHltvIsReady = true;
FnGetHltvChallengePost(HltvCmd, 0);
return;
}
}
else if(g_bHltvIsReady)
{
g_bHltvIsReady = false;
setc(szData, QUERY_OFFSET, 0xff);
switch(str_to_num(HltvCmd))
{
case HltvCmdStartRecord:
{
if(g_bHltvIsRecording)
{
log_amx("Already recording");
return;
}
format(szData, charsmax(szData), "%srcon %s ^"%s^" record %s^n", szData, g_szRconChallenge, cv_szHltvPasswd, cv_szHltvDemoPath);
}
case HltvCmdStopRecord:
format(szData, charsmax(szData), "%srcon %s ^"%s^" stoprecording^n", szData, g_szRconChallenge, cv_szHltvPasswd);
}
socket_send(g_hSocket, szData, charsmax(szData));
set_task(5.0, "FnGetHltvChallengePost", TASK_ID_GET_DATA, HltvCmd, QUERY_OFFSET);
}
else
{
socket_close(g_hSocket);
}
}