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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//===========================================================================//
#include "tier0/platform.h"
#include "sys.h"
#include "testscriptmgr.h"
#include "tier0/dbg.h"
#include "filesystem_engine.h"
#include "tier1/strtools.h"
#include "cmd.h"
#include "convar.h"
#include "vstdlib/random.h"
#include <stdlib.h>
#if defined( _X360 )
#include "xbox/xbox_win32stubs.h"
#endif
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
CTestScriptMgr g_TestScriptMgr;
ConVar testscript_debug( "testscript_debug", "0", 0, "Debug test scripts." );
#ifdef _DEBUG
// --------------------------------------------------------------------------------------------------- //
// Global console commands the test script manager implements.
// --------------------------------------------------------------------------------------------------- //
CON_COMMAND_EXTERN( Test_Wait, Test_Wait, "" )
{
if ( args.ArgC() < 2 )
{
Error( "Test_Wait: requires seconds parameter." );
return;
}
float flSeconds = atof( args[ 1 ] );
GetTestScriptMgr()->SetWaitTime( flSeconds );
}
CON_COMMAND_EXTERN( Test_RunFrame, Test_RunFrame, "" )
{
GetTestScriptMgr()->SetWaitCheckPoint( "frame_end" );
}
CON_COMMAND_EXTERN( Test_WaitForCheckPoint, Test_WaitForCheckPoint, "" )
{
if ( args.ArgC() < 2 )
{
Error( "Test_WaitForCheckPoint <checkpoint name> [once]: requires checkpoint name." );
return;
}
bool bOnce = ( args.ArgC() >= 3 && Q_stricmp( args[2], "once" ) == 0 );
GetTestScriptMgr()->SetWaitCheckPoint( args[ 1 ], bOnce );
}
CON_COMMAND_EXTERN( Test_StartLoop, Test_StartLoop, "Test_StartLoop <loop name> - Denote the start of a loop. Really just defines a named point you can jump to." )
{
if ( args.ArgC() < 2 )
{
Error( "Test_StartLoop: requires a loop name." );
return;
}
GetTestScriptMgr()->StartLoop( args[ 1 ] );
}
CON_COMMAND_EXTERN( Test_LoopCount, Test_LoopCount, "Test_LoopCount <loop name> <count> - loop back to the specified loop start point the specified # of times." )
{
if ( args.ArgC() < 3 )
{
Error( "Test_LoopCount: requires a loop name and number of times to loop." );
return;
}
GetTestScriptMgr()->LoopCount( args[ 1 ], atoi( args[ 2 ] ) );
}
CON_COMMAND_EXTERN( Test_Loop, Test_Loop, "Test_Loop <loop name> - loop back to the specified loop start point unconditionally." )
{
if ( args.ArgC() < 2 )
{
Error( "Test_Loop: requires a loop name." );
return;
}
GetTestScriptMgr()->LoopCount( args[ 1 ], -1 );
}
CON_COMMAND_EXTERN( Test_LoopForNumSeconds, Test_LoopForNumSeconds, "Test_LoopForNumSeconds <loop name> <time> - loop back to the specified start point for the specified # of seconds." )
{
if ( args.ArgC() < 3 )
{
Error( "Test_LoopLoopForNumSeconds: requires a loop name and number of seconds to loop." );
return;
}
GetTestScriptMgr()->LoopForNumSeconds( args[ 1 ], atof( args[ 2 ] ) );
}
CON_COMMAND( Test_RandomChance, "Test_RandomChance <percent chance, 0-100> <token1> <token2...> - Roll the dice and maybe run the command following the percentage chance." )
{
if ( args.ArgC() < 3 )
{
Error( "Test_RandomChance: requires percentage chance parameter (0-100) followed by command to execute." );
}
float flPercent = atof( args[ 1 ] );
if ( RandomFloat( 0, 100 ) < flPercent )
{
char newString[1024];
newString[0] = 0;
for ( int i=2; i < args.ArgC(); i++ )
{
Q_strncat( newString, args[ i ], sizeof( newString ), COPY_ALL_CHARACTERS );
Q_strncat( newString, " ", sizeof( newString ), COPY_ALL_CHARACTERS );
}
Cbuf_InsertText( newString );
}
}
CON_COMMAND( Test_SendKey, "" )
{
if ( args.ArgC() < 2 )
{
Error( "Test_SendKey: requires key to send." );
}
Sys_TestSendKey( args[1] );
}
CON_COMMAND( Test_StartScript, "Start a test script running.." )
{
if ( args.ArgC() < 2 )
{
Warning( "Test_StartScript: requires filename of script to start (file must be under testscripts directory).\n" );
}
if ( !GetTestScriptMgr()->StartTestScript( args[1] ) )
Warning( "Error starting testscript '%s'\n", args[1] );
}
#endif
// --------------------------------------------------------------------------------------------------- //
// CTestScriptMgr implementation.
// --------------------------------------------------------------------------------------------------- //
CTestScriptMgr::CTestScriptMgr()
{
m_hFile = FILESYSTEM_INVALID_HANDLE;
m_NextCheckPoint[0] = 0;
m_WaitUntil = Sys_FloatTime();
}
CTestScriptMgr::~CTestScriptMgr()
{
Term();
}
bool CTestScriptMgr::StartTestScript( const char *pFilename )
{
Term();
char fullName[512];
Q_snprintf( fullName, sizeof( fullName ), "testscripts\\%s", pFilename );
m_hFile = g_pFileSystem->Open( fullName, "rt" );
if ( m_hFile == FILESYSTEM_INVALID_HANDLE )
return false;
RunCommands();
return true;
}
void CTestScriptMgr::Term()
{
if ( m_hFile != FILESYSTEM_INVALID_HANDLE )
{
g_pFileSystem->Close( m_hFile );
m_hFile = FILESYSTEM_INVALID_HANDLE;
}
}
bool CTestScriptMgr::IsInitted() const
{
return m_hFile != FILESYSTEM_INVALID_HANDLE;
}
void CTestScriptMgr::CheckPoint( const char *pName )
{
if ( !IsInitted() || IsTimerWaiting() )
return;
if ( testscript_debug.GetInt() )
{
if ( stricmp( pName, "frame_end" ) != 0 )
{
Msg( "TESTSCRIPT: CheckPoint -> '%s'.\n", pName );
}
}
m_CheckPointsHit.Insert( pName, 0 ); // Remember that we hit this checkpoint.
if ( m_NextCheckPoint[0] )
{
if ( Q_stricmp( m_NextCheckPoint, pName ) != 0 )
{
// This isn't the checkpoint you're looking for.
return;
}
}
// Either the timer expired, or we hit the checkpoint we were waiting for. Run some more commands.
m_NextCheckPoint[0] = 0;
RunCommands();
}
void CTestScriptMgr::RunCommands()
{
Assert( IsInitted() );
while ( !IsTimerWaiting() && !IsCheckPointWaiting() )
{
// Parse out the next command.
char curCommand[512];
int iCurPos = 0;
bool bInSlash = false;
while ( 1 )
{
g_pFileSystem->Read( &curCommand[iCurPos], 1, m_hFile );
if ( curCommand[iCurPos] == '/' )
{
if ( bInSlash )
{
// Ok, the rest of this line is a comment.
char tempVal = !'\n';
while ( tempVal != '\n' && !g_pFileSystem->EndOfFile( m_hFile ) )
{
g_pFileSystem->Read( &tempVal, 1, m_hFile );
}
--iCurPos;
break;
}
else
{
bInSlash = true;
}
}
else
{
bInSlash = false;
if ( curCommand[iCurPos] == ';' || curCommand[iCurPos] == '\n' || g_pFileSystem->EndOfFile( m_hFile ) )
{
// End of this command.
break;
}
}
++iCurPos;
}
curCommand[iCurPos] = 0;
// Did we hit the end of the file?
if ( curCommand[0] == 0 )
{
if ( g_pFileSystem->EndOfFile( m_hFile ) )
{
Term();
break;
}
else
{
continue;
}
}
Cbuf_AddText( curCommand );
Cbuf_Execute();
}
}
bool CTestScriptMgr::IsTimerWaiting() const
{
if ( Sys_FloatTime() < m_WaitUntil )
{
return true;
}
else
{
return false;
}
}
bool CTestScriptMgr::IsCheckPointWaiting() const
{
return m_NextCheckPoint[0] != 0;
}
void CTestScriptMgr::SetWaitTime( float flSeconds )
{
m_WaitUntil = Sys_FloatTime() + flSeconds;
}
CLoopInfo* CTestScriptMgr::FindLoop( const char *pLoopName )
{
FOR_EACH_LL( m_Loops, i )
{
if ( Q_stricmp( pLoopName, m_Loops[i]->m_Name ) == 0 )
return m_Loops[i];
}
return NULL;
}
void CTestScriptMgr::StartLoop( const char *pLoopName )
{
ErrorIfNotInitted();
if ( FindLoop( pLoopName ) )
{
Error( "CTestScriptMgr::StartLoop( %s ): loop already exists.", pLoopName );
}
CLoopInfo *pLoop = new CLoopInfo;
Q_strncpy( pLoop->m_Name, pLoopName, sizeof( pLoop->m_Name ) );
pLoop->m_nCount = 0;
pLoop->m_flStartTime = Sys_FloatTime();
pLoop->m_iNextCommandPos = g_pFileSystem->Tell( m_hFile );
pLoop->m_ListIndex = m_Loops.AddToTail( pLoop );
}
void CTestScriptMgr::LoopCount( const char *pLoopName, int nTimes )
{
ErrorIfNotInitted();
CLoopInfo *pLoop = FindLoop( pLoopName );
if ( !pLoop )
{
Error( "CTestScriptMgr::LoopCount( %s ): no loop with this name exists.", pLoopName );
}
++pLoop->m_nCount;
if ( pLoop->m_nCount < nTimes || nTimes == -1 )
{
g_pFileSystem->Seek( m_hFile, pLoop->m_iNextCommandPos, FILESYSTEM_SEEK_HEAD );
}
else
{
m_Loops.Remove( pLoop->m_ListIndex );
delete pLoop;
}
}
void CTestScriptMgr::LoopForNumSeconds( const char *pLoopName, double nSeconds )
{
ErrorIfNotInitted();
CLoopInfo *pLoop = FindLoop( pLoopName );
if ( !pLoop )
{
Error( "CTestScriptMgr::LoopForNumSeconds( %s ): no loop with this name exists.", pLoopName );
}
if ( Sys_FloatTime() - pLoop->m_flStartTime < nSeconds )
{
g_pFileSystem->Seek( m_hFile, pLoop->m_iNextCommandPos, FILESYSTEM_SEEK_HEAD );
}
else
{
m_Loops.Remove( pLoop->m_ListIndex );
delete pLoop;
}
}
void CTestScriptMgr::ErrorIfNotInitted()
{
if ( !IsInitted() )
{
Error( "CTestScriptMgr: not initialized." );
}
}
void CTestScriptMgr::SetWaitCheckPoint( const char *pCheckPointName, bool bOnce )
{
if ( testscript_debug.GetInt() )
{
if ( stricmp( pCheckPointName, "frame_end" ) != 0 )
{
Msg( "TESTSCRIPT: waiting for checkpoint '%s'%s\n", pCheckPointName, bOnce ? " (once)." : "." );
}
}
// Don't wait on this checkpoint if we alereayd
if ( bOnce && m_CheckPointsHit.Find( pCheckPointName ) != m_CheckPointsHit.InvalidIndex() )
return;
Q_strncpy( m_NextCheckPoint, pCheckPointName, sizeof( m_NextCheckPoint ) );
}
|