-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgsc_custom_utils.cpp
280 lines (237 loc) · 5.12 KB
/
gsc_custom_utils.cpp
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h> // For isalnum
#include "gsc_custom_utils.hpp"
#define SQL_MAX_QUERY_SIZE (10 * 1024)
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
void Gsc_Utils_Void(int entnum)
{
(void)entnum; // Unused
}
void Gsc_Utils_constructMessage()
{
int nrParams = Scr_GetNumParam();
if (nrParams == 0)
{
Scr_Error("Usage: constructMessage(1 or more (localized) strings)\n");
return;
}
/*
int firstParmIndex
int lastParmIndex
const char *errorContext
char *string
unsigned int stringLimit
*/
char buf[1024] = {0};
Scr_ConstructMessageString(0, (nrParams - 1), "Combined string of constructMessage", buf, sizeof(buf));
stackPushString(buf);
}
void Gsc_Utils_setConfigStringByIndex()
{
int index = 0;
stackGetParamInt(0, &index);
char *value = NULL;
stackGetParamString(1, &value);
SV_SetConfigstring(index, value);
}
void Gsc_SV_GetConfigString()
{
char buf[1024] = {0};
int idx = 0;
if (stackGetParamInt(0, &idx))
{
SV_GetConfigstring(idx, buf, sizeof(buf));
}
stackPushString(buf);
}
void Gsc_Utils_ZeroInt(int entnum)
{
stackPushInt(0);
}
void Gsc_Utils_VoidFunc()
{
stackPushUndefined();
}
void Gsc_Utils_IsValidInt()
{
char *buf = NULL;
stackGetParamString(0, &buf);
if (!buf)
{
stackPushBool(false);
return;
}
// strtoX functions allow spaces so they can be called multiple times, but that's not safe for user input
if (strchr(buf, ' ') != NULL)
{
stackPushBool(false);
return;
}
char *ptr = NULL;
long number = strtol(buf, &ptr, 10);
(void)number;
if (buf == ptr)
{
stackPushBool(false);
return;
}
stackPushBool(true);
}
void Gsc_Utils_IsValidFloat()
{
char *buf = NULL;
stackGetParamString(0, &buf);
if (!buf)
{
stackPushBool(false);
return;
}
// strtoX functions allow spaces so they can be called multiple times, but that's not safe for user input
if (strchr(buf, ' ') != NULL)
{
stackPushBool(false);
return;
}
char *ptr = NULL;
float number = strtof(buf, &ptr);
(void)number;
if (buf == ptr)
{
stackPushBool(false);
return;
}
stackPushBool(true);
}
void Gsc_Utils_ContainsIllegalChars()
{
bool result = false;
char *buf = NULL;
stackGetParamString(0, &buf);
if (buf != NULL)
{
for (int i = 0; i < (int)strlen(buf); i++)
{
unsigned char uChar = (unsigned char)buf[i];
if (uChar < 0x20) /* space */
{
result = true;
break;
}
// Other things that CoD4 doesn't like as input:
if ((uChar == 0x25) || /* percent */
(uChar == 0x26) || /* ampersand */
(uChar == 0x22) || /* double quote */
(uChar == 0x5C) || /* backslash */
(uChar == 0x7E) || /* tilde -> weird client issues as it's persistently bound to console */
(uChar == 0x7F) || /* DEL */
(uChar == 0xFF)) /* NBSP */
{
result = true;
break;
}
}
}
stackPushBool(result);
}
void Gsc_Utils_Printf()
{
char *buf = NULL;
stackGetParamString(0, &buf);
printf("%s", buf);
}
void Gsc_Utils_VectorScale()
{
vec3_t vector;
float scale;
stackGetParamVector(0, vector);
stackGetParamFloat(1, &scale);
vector[0] *= scale;
vector[1] *= scale;
vector[2] *= scale;
stackPushVector(vector);
}
void Gsc_Utils_IsEntityThinking(int entnum)
{
#ifdef COD4
stackPushInt(1); // Not implemented
#else
if(*(int*)0x864F984 == entnum)
stackPushInt(1);
else
stackPushInt(0);
#endif // COD4
}
static bool isRandomSeeded = false;
void _seedRandom()
{
if(!isRandomSeeded)
{
srand(time(NULL) ^ getpid());
isRandomSeeded = true;
}
}
void Gsc_Utils_CreateRandomInt()
{
_seedRandom();
int res = ((rand() & 0xFFFF) << 16) | (rand() & 0xFFFF);
stackPushInt(res);
}
void Gsc_Utils_Rand()
{
_seedRandom();
stackPushInt(rand());
}
void Gsc_Utils_IsAlphaNumeric()
{
char *buf = NULL;
stackGetParamString(0, &buf);
if (buf == NULL)
{
stackPushInt(0);
return;
}
stackPushInt(isalnum(buf[0]) ? 1 : 0);
}
void Gsc_Utils_HexStringToInt()
{
char *buf = NULL;
stackGetParamString(0, &buf);
if(buf == NULL)
{
stackPushUndefined();
return;
}
int result = strtoul(buf, NULL, 16);
// Input wasn't 0, but function returned 0 (error)
if(result == 0 && buf[0] != 0)
{
stackPushUndefined();
}
else
{
stackPushInt(result);
}
}
void Gsc_Utils_IntToHexString()
{
int val = 0;
stackGetParamInt(0, &val);
char buf[9];
snprintf(buf, sizeof(buf), "%08x", val);
stackPushString(buf);
}
void Gsc_Utils_GetCodVersion()
{
#ifdef COD4
stackPushInt(4);
#else
stackPushInt(2);
#endif
}
#ifdef __cplusplus
}
#endif // __cplusplus