forked from fdxx/l4d2_plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sm_cfgexec_once.sp
128 lines (103 loc) · 3.49 KB
/
sm_cfgexec_once.sp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
#include <sdktools>
#include <sourcescramble> // https://github.com/nosoop/SMExt-SourceScramble
#define VERSION "0.1"
int g_iCCommandSize;
Handle
g_hSDK_CCommand_Constructor,
g_hSDK_CCommand_Tokenize,
g_hSDK_CmdExecf;
public Plugin myinfo =
{
name = "sm_cfgexec_once",
author = "fdxx",
version = VERSION,
};
public void OnPluginStart()
{
Init();
CreateConVar("sm_cfgexec_once_version", VERSION, "version", FCVAR_NOTIFY | FCVAR_DONTRECORD);
RegAdminCmd("sm_cfgexec_once", Cmd_Execute, ADMFLAG_ROOT);
}
void Init()
{
char buffer[128];
strcopy(buffer, sizeof(buffer), "sm_cfgexec_once");
GameData hGameData = new GameData(buffer);
strcopy(buffer, sizeof(buffer), "CCommandSize");
g_iCCommandSize = hGameData.GetOffset(buffer);
if (g_iCCommandSize == -1)
SetFailState("Failed to GetOffset: %s", buffer);
// void CCommand::CCommand()
strcopy(buffer, sizeof(buffer), "CCommand::CCommand");
StartPrepSDKCall(SDKCall_Raw);
PrepSDKCall_SetFromConf(hGameData, SDKConf_Signature, buffer);
g_hSDK_CCommand_Constructor = EndPrepSDKCall();
if (g_hSDK_CCommand_Constructor == null)
SetFailState("Failed to create SDKCall: \"%s\"", buffer);
// bool CCommand::Tokenize( const char *pCommand, characterset_t *pBreakSet )
strcopy(buffer, sizeof(buffer), "CCommand::Tokenize");
StartPrepSDKCall(SDKCall_Raw);
PrepSDKCall_SetFromConf(hGameData, SDKConf_Signature, buffer);
PrepSDKCall_AddParameter(SDKType_String, SDKPass_Pointer);
PrepSDKCall_AddParameter(SDKType_PlainOldData, SDKPass_Plain);
PrepSDKCall_SetReturnInfo(SDKType_Bool, SDKPass_Plain);
g_hSDK_CCommand_Tokenize = EndPrepSDKCall();
if (g_hSDK_CCommand_Tokenize == null)
SetFailState("Failed to create SDKCall: \"%s\"", buffer);
//void Cmd_Exec_f( const CCommand &args )
strcopy(buffer, sizeof(buffer), "Cmd_Exec_f");
StartPrepSDKCall(SDKCall_Static);
PrepSDKCall_SetFromConf(hGameData, SDKConf_Signature, buffer);
PrepSDKCall_AddParameter(SDKType_PlainOldData, SDKPass_Plain);
g_hSDK_CmdExecf = EndPrepSDKCall();
if (g_hSDK_CmdExecf == null)
SetFailState("Failed to create SDKCall: \"%s\"", buffer);
delete hGameData;
}
Action Cmd_Execute(int client, int args)
{
if (args != 1)
{
char cmd[128];
GetCmdArg(0, cmd, sizeof(cmd));
ReplyToCommand(client, "Syntax: %s <filename>", cmd);
return Plugin_Handled;
}
static bool shit = false;
if (shit) return Plugin_Handled;
shit = true;
char file[PLATFORM_MAX_PATH], buffer[PLATFORM_MAX_PATH];
GetCmdArg(1, file, sizeof(file));
FormatEx(buffer, sizeof(buffer), "cfg/%s", file);
if (!FileExists(buffer))
{
LogError("%s does not exist", buffer);
return Plugin_Handled;
}
// https://github.com/lua9520/source-engine-2018-cstrike15_src/blob/master/engine/cmd.h#L193
// For some reason, it will not be executed immediately on Windows. So we manually call the Cmd_Exec_f function.
//ServerCommand("exec \"%s\"", file);
//ServerExecute();
FormatEx(buffer, sizeof(buffer), "exec \"%s\"", file);
if (ExecuteFile(buffer))
ReplyToCommand(client, "Executed file: %s", file);
else
LogError("Failed to execute file: %s", file);
return Plugin_Handled;
}
bool ExecuteFile(const char[] cmdStr)
{
bool result = false;
MemoryBlock hMemoryBlock = new MemoryBlock(g_iCCommandSize);
SDKCall(g_hSDK_CCommand_Constructor, hMemoryBlock.Address);
if (SDKCall(g_hSDK_CCommand_Tokenize, hMemoryBlock.Address, cmdStr, 0))
{
SDKCall(g_hSDK_CmdExecf, hMemoryBlock.Address);
result = true;
}
delete hMemoryBlock;
return result;
}