forked from nillerusr/source-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathccs.cpp
More file actions
1620 lines (1319 loc) · 40.5 KB
/
ccs.cpp
File metadata and controls
1620 lines (1319 loc) · 40.5 KB
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//========= Copyright Valve Corporation, All rights reserved. ============//
// ccs.cpp - "con command server"
// Not to be confused with the game's server - this is a completely different thing that allows multiple game clients to connect to a single client,
// so con commands can be sent simultaneously to multiple running game clients, screen captures can be sent in real-time to a single client for comparison purposes,
// and all convar's can be diff'd between all connected clients and the server.
// The server portion of this code needs socketlib, which hasn't been ported to Linux yet, and shouldn't be shipped because it could be a security risk
// (it's only intended for primarily graphics debugging and detailed comparisons of GL vs. D3D9).
#include "host_state.h"
//#define CON_COMMAND_SERVER_SUPPORT
#include "tier2/tier2.h"
#include "materialsystem/imaterialsystemhardwareconfig.h"
#ifdef CON_COMMAND_SERVER_SUPPORT
#include <algorithm>
#include "socketlib/socketlib.h"
#define MINIZ_NO_ARCHIVE_APIS
#include "../../thirdparty/miniz/miniz.c"
#include "../../thirdparty/miniz/simple_bitmap.h"
#define STBI_NO_STDIO
#include "../../thirdparty/stb_image/stb_image.c"
#include "ivideomode.h"
#endif
#include "cmd.h"
#include "filesystem.h"
#include "render.h"
#include "icliententitylist.h"
#include "client.h"
#include "icliententity.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//-----------------------------------------------------------------------------
#ifdef STAGING_ONLY
CON_COMMAND_F( ccs_write_convars, "Write all convars to file.", FCVAR_CHEAT )
{
FILE* pFile = fopen( "convars.txt", "w" );
if ( !pFile )
{
ConMsg( "Unable to open convars.txt\n" );
return;
}
ICvar::Iterator iter( g_pCVar );
for ( iter.SetFirst() ; iter.IsValid() ; iter.Next() )
{
ConCommandBase *var = iter.Get();
ConVar *pConVar = dynamic_cast< ConVar* >( var );
if ( !pConVar )
continue;
const char *pName = pConVar->GetName();
const char *pVal = pConVar->GetString();
if ( ( !pName ) || ( !pVal ) )
continue;
fprintf( pFile, "%s=%s\n", pName, pVal );
}
fclose( pFile );
ConMsg( "Wrote all convars to convars.txt\n" );
}
#endif
//-----------------------------------------------------------------------------
#ifdef CON_COMMAND_SERVER_SUPPORT
class frame_buf_window
{
friend LRESULT CALLBACK static_window_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK static_window_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
frame_buf_window* pApp = reinterpret_cast<frame_buf_window*>(GetWindowLong(hWnd, 0));
if (!pApp)
pApp = frame_buf_window::m_pCur_app;
Assert(pApp);
return pApp->window_proc(hWnd, message, wParam, lParam);
}
public:
frame_buf_window(const char* pTitle = "frame_buf_window", int width = 640, int height = 480, int scaleX = 1, int scaleY = 1);
virtual ~frame_buf_window();
// Return true if you handle the window message.
typedef bool (*user_window_proc_ptr_t)(LRESULT& hres, HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, void *pData);
void set_window_proc_callback(user_window_proc_ptr_t windowProcPtr, void *pData) { m_pWindow_proc = windowProcPtr; m_pWindow_proc_data = pData; }
int width(void) const { return m_width; }
int height(void) const { return m_height; }
void update(void);
void close(void);
const simple_bgr_bitmap& frameBuffer(void) const { return m_frame_buffer; }
simple_bgr_bitmap& frameBuffer(void) { return m_frame_buffer; }
private:
int m_width, m_height;
int m_orig_width, m_orig_height;
int m_orig_x, m_orig_y;
int m_scale_x, m_scale_y;
WNDCLASS m_window_class;
HWND m_window;
char m_bitmap_info[16 + sizeof(BITMAPINFO)];
BITMAPINFO* m_pBitmap_hdr;
HDC m_windowHDC;
simple_bgr_bitmap m_frame_buffer;
static frame_buf_window* m_pCur_app;
user_window_proc_ptr_t m_pWindow_proc;
void *m_pWindow_proc_data;
void create_window(const char* pTitle);
void create_bitmap(void);
LRESULT window_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
}; // class frame_buf_window
frame_buf_window* frame_buf_window::m_pCur_app;
frame_buf_window::frame_buf_window(const char* pTitle, int width, int height, int scaleX, int scaleY) :
m_width(width),
m_height(height),
m_orig_width(0),
m_orig_height(0),
m_orig_x(0),
m_orig_y(0),
m_window(0),
m_pBitmap_hdr(NULL),
m_windowHDC(0),
m_pWindow_proc(NULL),
m_pWindow_proc_data(NULL)
{
m_scale_x = scaleX;
m_scale_y = scaleY;
create_window(pTitle);
create_bitmap();
}
frame_buf_window::~frame_buf_window()
{
close();
}
void frame_buf_window::update(void)
{
if ( m_window )
{
InvalidateRect(m_window, NULL, FALSE);
SendMessage(m_window, WM_PAINT, 0, 0);
MSG msg;
while (PeekMessage(&msg, m_window, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
//Sleep(0);
}
}
void frame_buf_window::close(void)
{
m_frame_buffer.clear();
if (m_window)
{
ReleaseDC(m_window, m_windowHDC);
DestroyWindow(m_window);
m_window = 0;
m_windowHDC = 0;
}
}
void frame_buf_window::create_window(const char* pTitle)
{
memset( &m_window_class, 0, sizeof( m_window_class ) );
m_window_class.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
m_window_class.lpfnWndProc = static_window_proc;
m_window_class.cbWndExtra = sizeof(DWORD);
m_window_class.hCursor = LoadCursor(0, IDC_ARROW);
m_window_class.lpszClassName = pTitle;
m_window_class.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH); //GetStockObject(NULL_BRUSH);
RegisterClass(&m_window_class);
RECT rect;
rect.left = rect.top = 0;
rect.right = m_width * m_scale_x;
rect.bottom = m_height * m_scale_y;
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, 0);
rect.right -= rect.left;
rect.bottom -= rect.top;
m_orig_x = (GetSystemMetrics(SM_CXSCREEN) - rect.right) / 2;
m_orig_y = (GetSystemMetrics(SM_CYSCREEN) - rect.bottom) / 2;
m_orig_width = rect.right;
m_orig_height = rect.bottom;
m_pCur_app = this;
m_window = CreateWindowEx(
0, pTitle, pTitle,
WS_OVERLAPPEDWINDOW,
m_orig_x, m_orig_y, rect.right, rect.bottom, 0, 0, 0, 0);
SetWindowLong(m_window, 0, reinterpret_cast<LONG>(this));
m_pCur_app = NULL;
ShowWindow(m_window, SW_NORMAL);
}
void frame_buf_window::create_bitmap(void)
{
memset( &m_bitmap_info, 0, sizeof( m_bitmap_info ) );
m_pBitmap_hdr = reinterpret_cast<BITMAPINFO*>(&m_bitmap_info);
m_pBitmap_hdr->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
m_pBitmap_hdr->bmiHeader.biWidth = m_width;
m_pBitmap_hdr->bmiHeader.biHeight = -m_height;
m_pBitmap_hdr->bmiHeader.biBitCount = 24;
m_pBitmap_hdr->bmiHeader.biPlanes = 1;
m_pBitmap_hdr->bmiHeader.biCompression = BI_RGB;//BI_BITFIELDS;
// Only really needed for 32bpp BI_BITFIELDS
reinterpret_cast<uint32*>(m_pBitmap_hdr->bmiColors)[0] = 0x00FF0000;
reinterpret_cast<uint32*>(m_pBitmap_hdr->bmiColors)[1] = 0x0000FF00;
reinterpret_cast<uint32*>(m_pBitmap_hdr->bmiColors)[2] = 0x000000FF;
m_windowHDC = GetDC(m_window);
m_frame_buffer.init( m_width, m_height );
m_frame_buffer.cls( 30, 30, 30 );
//m_frame_buffer.draw_text( 50, 200, 2, 255, 127, 128, "This is a test!" );
}
LRESULT frame_buf_window::window_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (m_pWindow_proc)
{
LRESULT lres;
bool status = m_pWindow_proc(lres, hWnd, message, wParam, lParam, m_pWindow_proc_data);
if (status)
return lres;
}
switch (message)
{
case WM_PAINT:
{
if (m_frame_buffer.is_valid())
{
RECT window_size;
GetClientRect(hWnd, &window_size);
StretchDIBits(m_windowHDC,
0, 0, window_size.right, window_size.bottom,
0, 0, m_width, m_height,
m_frame_buffer.get_ptr(), m_pBitmap_hdr,
DIB_RGB_COLORS, SRCCOPY);
ValidateRect(hWnd, NULL);
}
break;
}
case WM_KEYDOWN:
{
const int cEscCode = 27;
if (cEscCode != (wParam & 0xFF))
break;
}
case WM_CLOSE:
{
close();
break;
}
}
return DefWindowProc( hWnd, message, wParam, lParam );
}
class CConCommandServerProtocolTypes
{
public:
enum
{
cProtocolPort = 3779,
cMaxClients = 4
};
struct PacketHeader_t
{
uint16 m_nID;
uint16 m_nType;
uint32 m_nTotalSize;
};
struct SetCameraPosPacket_t : PacketHeader_t
{
Vector m_Pos;
QAngle m_Angle;
};
struct ScreenshotPacket_t : PacketHeader_t
{
uint m_nScreenshotID;
char m_szFilename[256];
};
enum
{
cPacketHeaderID = 0x1234,
cPacketHeaderSize = sizeof( PacketHeader_t ),
};
enum PacketTypes_t
{
cPacketTypeMessage = 0,
cPacketTypeCommand = 1,
cPacketTypeScreenshotRequest = 2,
cPacketTypeScreenshotReply = 3,
cPacketTypeSetCameraPos = 4,
cPacketTypeConVarDumpRequest = 5,
cPacketTypeConVarDumpReply = 6,
cPackerTypeTotalMessages
};
};
struct DumpedConVar_t
{
CUtlString m_Name;
CUtlString m_Value;
bool operator< (const DumpedConVar_t & rhs ) const { return V_strcmp( m_Name.Get(), rhs.m_Name.Get() ) < 0; }
};
typedef CUtlVector<DumpedConVar_t> DumpedConVarVector_t;
static void DumpConVars( DumpedConVarVector_t &conVars )
{
ICvar::Iterator iter( g_pCVar );
for ( iter.SetFirst() ; iter.IsValid() ; iter.Next() )
{
ConCommandBase *var = iter.Get();
ConVar *pConVar = dynamic_cast< ConVar* >( var );
if ( !pConVar )
continue;
const char *pName = pConVar->GetName();
const char *pVal = pConVar->GetString();
if ( ( !pName ) || ( !pVal ) )
continue;
int nIndex = conVars.AddToTail();
conVars[nIndex].m_Name.Set( pName );
conVars[nIndex].m_Value.Set( pVal );
}
}
class CConCommandConnection : public CConCommandServerProtocolTypes
{
public:
CConCommandConnection() :
m_pSocket( NULL ),
m_bDeleteSocket( false ),
m_nEndpointIndex( -1 ),
m_bClientFlag( false ),
m_bReceivedNewCameraPos( false ),
m_nSendBufOfs( 0 ),
m_bHasNewScreenshot( false ),
m_nScreenshotID( 0 )
{
memset( &m_NewCameraPos, 0, sizeof( m_NewCameraPos ) );
}
~CConCommandConnection()
{
Deinit();
}
bool IsConnected() const { return m_nEndpointIndex >= 0; }
bool Init( const char *pAddress )
{
Deinit();
m_nEndpointIndex = 0;
m_bClientFlag = true;
m_RecvBuf.EnsureCapacity( 4096 );
m_SendBuf.EnsureCapacity( 4096 );
m_RecvBuf.SetCountNonDestructively( 0 );
m_SendBuf.SetCountNonDestructively( 0 );
m_nSendBufOfs = 0;
m_pSocket = new CSocketConnection;
m_bDeleteSocket = true;
m_pSocket->Init( CT_CLIENT, SP_TCP );
SocketErrorCode_t err = m_pSocket->ConnectToServer( pAddress, cProtocolPort );
if ( err != SOCKET_SUCCESS )
{
Warning( "CONCMDSRV: Failed connecting to con cmd server \"%s\"!\n", pAddress );
Deinit();
return false;
}
uint n = 0;
while ( m_pSocket->GetEndpointSocketState( 0 ) == SSTATE_CONNECTION_IN_PROGRESS )
{
bool bIsConnected = false;
err = m_pSocket->PollClientConnectionState( &bIsConnected );
if ( err != SOCKET_SUCCESS )
{
Warning( "CONCMDSRV: Failed connecting to con cmd server \"%s\"!\n", pAddress );
Deinit();
return false;
}
if ( bIsConnected )
break;
ThreadSleep( 10 );
if ( ++n >= 500 )
{
Warning( "CONCMDSRV: Failed connecting to con cmd server \"%s\"!\n", pAddress );
Deinit();
return false;
}
}
if ( m_pSocket->GetEndpointSocketState( 0 ) != SSTATE_CONNECTED )
{
Warning( "CONCMDSRV: Failed connecting to con cmd server \"%s\"!\n", pAddress );
Deinit();
return false;
}
int nFlag = 1;
m_pSocket->SetSocketOpt( 0, IPPROTO_TCP, TCP_NODELAY, &nFlag, sizeof( int ) );
return TickConnection();
}
bool Init( CSocketConnection *pSocket, int nEndpointIndex, bool bClientFlag )
{
Deinit();
int nFlag = 1;
pSocket->SetSocketOpt( nEndpointIndex, IPPROTO_TCP, TCP_NODELAY, &nFlag, sizeof( int ) );
m_pSocket = pSocket;
m_bDeleteSocket = false;
m_nEndpointIndex = nEndpointIndex;
m_bClientFlag = bClientFlag;
m_RecvBuf.EnsureCapacity( 4096 );
m_SendBuf.EnsureCapacity( 4096 );
m_RecvBuf.SetCountNonDestructively( 0 );
m_SendBuf.SetCountNonDestructively( 0 );
m_nSendBufOfs = 0;
return TickConnection();
}
void Deinit()
{
if ( m_nEndpointIndex < 0 )
return;
ConMsg( "CONCMDSRV: Disconnecting endpoint %i\n", m_nEndpointIndex );
if ( m_pSocket )
{
m_pSocket->ResetEndpoint( m_nEndpointIndex );
if ( m_bDeleteSocket )
{
m_pSocket->Cleanup();
delete m_pSocket;
}
m_pSocket = NULL;
}
m_nEndpointIndex = -1;
m_bDeleteSocket = false;
m_bClientFlag = false;
m_RecvBuf.SetCountNonDestructively( 0 );
m_SendBuf.SetCountNonDestructively( 0 );
m_nSendBufOfs = 0;
m_bHasNewScreenshot = false;
m_nScreenshotID = 0;
m_screenshot.clear();
m_bReceivedNewCameraPos = false;
memset( &m_NewCameraPos, 0, sizeof( m_NewCameraPos ) );
}
bool SendData( const void * pPacket, uint nSize )
{
if ( m_nEndpointIndex < 0 )
return false;
m_SendBuf.AddMultipleToTail( nSize, static_cast< const uint8 * >( pPacket ) );
return TickConnectionSend();
}
bool TickConnectionRecv( )
{
for ( ; ; )
{
uint8 buf[4096];
int nBytesRead = 0;
SocketErrorCode_t nReadErr = m_pSocket->ReadFromEndpoint( m_nEndpointIndex, buf, sizeof( buf ), &nBytesRead );
if ( nReadErr == SOCKET_ERR_READ_OPERATION_WOULD_BLOCK )
break;
else if ( nReadErr != SOCKET_SUCCESS )
{
Warning( "CONCMDSRV: Failed receiving data from client %i\n", m_nEndpointIndex );
Deinit();
return false;
}
m_RecvBuf.AddMultipleToTail( nBytesRead, buf );
if ( m_RecvBuf.Count() >= cPacketHeaderSize )
{
if ( !ProcessMessages() )
{
Warning( "CONCMDSRV: Failed processing messages from client %i\n", m_nEndpointIndex );
Deinit();
return false;
}
}
}
return true;
}
bool TickConnectionSend( )
{
while ( m_nSendBufOfs < m_SendBuf.Count() )
{
int nBytesWritten = 0;
SocketErrorCode_t nWriteErr = m_pSocket->WriteToEndpoint( m_nEndpointIndex, &m_SendBuf[ m_nSendBufOfs ], m_SendBuf.Count() - m_nSendBufOfs, &nBytesWritten );
if ( nWriteErr == SOCKET_ERR_WRITE_OPERATION_WOULD_BLOCK )
break;
else if ( nWriteErr != SOCKET_SUCCESS )
{
Warning( "CONCMDSRV: Failed sending data to client %i\n", m_nEndpointIndex );
Deinit();
return false;
}
m_nSendBufOfs += nBytesWritten;
if ( m_nSendBufOfs == m_SendBuf.Count() )
{
m_SendBuf.SetCountNonDestructively( 0 );
m_nSendBufOfs = 0;
break;
}
}
return true;
}
bool TickConnection()
{
if ( !IsConnected() )
return false;
if ( !TickConnectionSend() )
return false;
if ( !TickConnectionRecv() )
return false;
if ( m_bReceivedNewCameraPos )
{
m_bReceivedNewCameraPos = false;
char buf[256];
//V_snprintf( buf, sizeof( buf ), "setpos_exact %f %f %f;setang_exact %f %f %f\n", m_NewCameraPos.m_Pos.x, m_NewCameraPos.m_Pos.y, m_NewCameraPos.m_Pos.z, m_NewCameraPos.m_Angle.x, m_NewCameraPos.m_Angle.y, m_NewCameraPos.m_Angle.z );
V_snprintf( buf, sizeof( buf ), "setpos_exact 0x%X 0x%X 0x%X;setang_exact 0x%X 0x%X 0x%X\n", *(DWORD*)&m_NewCameraPos.m_Pos.x, *(DWORD*)&m_NewCameraPos.m_Pos.y, *(DWORD*)&m_NewCameraPos.m_Pos.z, *(DWORD*)&m_NewCameraPos.m_Angle.x, *(DWORD*)&m_NewCameraPos.m_Angle.y, *(DWORD*)&m_NewCameraPos.m_Angle.z );
Cbuf_AddText( buf );
Cbuf_Execute();
}
return true;
}
bool HasNewScreenshotFlag() const { return m_bHasNewScreenshot; }
void ClearNewScreenshotFlag() { m_bHasNewScreenshot = false; }
uint GetScreenshotID() const { return m_nScreenshotID; }
simple_bitmap &GetScreenshot() { return m_screenshot; }
bool HasNewConVarDumpFlag() const { return m_bHasNewConVarDump; }
void ClearHasNewConVarDumpFlag() { m_bHasNewConVarDump = false; }
DumpedConVarVector_t &GetConVarDumpVector() { return m_DumpedConVars; }
const DumpedConVarVector_t &GetConVarDumpVector() const { return m_DumpedConVars; }
private:
CSocketConnection *m_pSocket;
bool m_bDeleteSocket;
int m_nEndpointIndex;
bool m_bClientFlag;
CUtlVector<uint8> m_RecvBuf;
CUtlVector<uint8> m_SendBuf;
int m_nSendBufOfs;
bool m_bReceivedNewCameraPos;
SetCameraPosPacket_t m_NewCameraPos;
simple_bitmap m_screenshot;
bool m_bHasNewScreenshot;
uint m_nScreenshotID;
bool m_bHasNewConVarDump;
DumpedConVarVector_t m_DumpedConVars;
bool ProcessMessages()
{
while ( m_RecvBuf.Count() >= cPacketHeaderSize )
{
const int nCurRecvBufSize = m_RecvBuf.Count();
const PacketHeader_t header( *reinterpret_cast<const PacketHeader_t *>( &m_RecvBuf[0] ) );
if ( header.m_nID != cPacketHeaderID )
return false;
if ( header.m_nTotalSize < cPacketHeaderSize )
return false;
if ( header.m_nType >= cPackerTypeTotalMessages )
return false;
if ( (uint32)m_RecvBuf.Count() < header.m_nTotalSize )
break;
const uint nNumMessageBytes = header.m_nTotalSize - cPacketHeaderSize;
const uint8 *pMsg = nNumMessageBytes ? &m_RecvBuf[cPacketHeaderSize] : NULL;
switch ( header.m_nType )
{
case cPacketTypeMessage:
{
if ( nNumMessageBytes )
{
ConMsg( "CONCMDSRV: Message from client %i: ", m_nEndpointIndex );
CUtlVectorFixedGrowable<char, 4096> buf;
buf.SetCount( nNumMessageBytes + 1 );
memcpy( &buf[0], pMsg, nNumMessageBytes );
buf[nNumMessageBytes] = '\0';
ConMsg( "%s\n", &buf[0] );
}
break;
}
case cPacketTypeScreenshotRequest:
{
const ScreenshotPacket_t &requestPacket = *reinterpret_cast<const ScreenshotPacket_t *>( &m_RecvBuf[0] );
if ( requestPacket.m_nTotalSize != sizeof( ScreenshotPacket_t ) )
{
Warning( "CONCMDSRV: Invalid screenshot request packet!\n" );
return false;
}
if ( !videomode )
break;
uint nWidth = videomode->GetModeWidth();
uint nHeight = videomode->GetModeHeight();
if ( !nWidth || !nHeight )
break;
static void *s_pBuf;
if ( !s_pBuf )
{
s_pBuf = malloc( nWidth * nHeight * 3 );
}
videomode->ReadScreenPixels( 0, 0, nWidth, nHeight, s_pBuf, IMAGE_FORMAT_RGB888 );
size_t nPNGSize = 0;
void *pPNGData = tdefl_write_image_to_png_file_in_memory( s_pBuf, nWidth, nHeight, 3, &nPNGSize, true );
if ( pPNGData )
{
ScreenshotPacket_t replyPacket;
replyPacket.m_nID = cPacketHeaderID;
replyPacket.m_nTotalSize = sizeof( replyPacket ) + nPNGSize;
replyPacket.m_nType = cPacketTypeScreenshotReply;
V_strcpy( replyPacket.m_szFilename, requestPacket.m_szFilename );
replyPacket.m_nScreenshotID = requestPacket.m_nScreenshotID;
if ( !SendData( &replyPacket, sizeof( replyPacket ) ) )
{
free( pPNGData );
return false;
}
if ( !SendData( pPNGData, nPNGSize ) )
{
free( pPNGData );
return false;
}
free( pPNGData );
}
break;
}
case cPacketTypeScreenshotReply:
{
const ScreenshotPacket_t &replyPacket = *reinterpret_cast<const ScreenshotPacket_t *>( &m_RecvBuf[0] );
if ( replyPacket.m_nTotalSize <= sizeof( ScreenshotPacket_t ) )
{
Warning( "CONCMDSRV: Invalid screenshot reply packet!\n" );
return false;
}
const void *pPNGData = &m_RecvBuf[sizeof(ScreenshotPacket_t)];
uint nPNGDataSize = header.m_nTotalSize - sizeof(ScreenshotPacket_t);
if ( !replyPacket.m_nScreenshotID )
{
char szFilename[512];
V_snprintf( szFilename, sizeof( szFilename ), "%s_%u.png", replyPacket.m_szFilename, m_nEndpointIndex );
FileHandle_t fh = g_pFullFileSystem->Open( szFilename, "wb" );
if ( fh )
{
g_pFullFileSystem->Write( pPNGData, nPNGDataSize, fh );
g_pFullFileSystem->Close(fh);
}
}
else
{
int nWidth = 0, nHeight = 0, nComp = 3;
unsigned char *pImageData = stbi_load_from_memory( reinterpret_cast< stbi_uc const * >( pPNGData ), nPNGDataSize, &nWidth, &nHeight, &nComp, 3);
if ( !pImageData )
{
Warning( "CONCMDSRV: Failed unpacking PNG screenshot!\n" );
return false;
}
m_bHasNewScreenshot = true;
m_nScreenshotID = replyPacket.m_nScreenshotID;
if ( ( (int)m_screenshot.width() != nWidth ) || ( (int)m_screenshot.height() != nHeight ) )
{
m_screenshot.init( nWidth, nHeight );
}
memcpy( m_screenshot.get_ptr(), pImageData, nWidth * nHeight * 3 );
stbi_image_free( pImageData );
}
break;
}
case cPacketTypeCommand:
{
if ( nNumMessageBytes )
{
//ConMsg( "CONCMDSRV: Console command from client %i: ", m_nEndpointIndex );
CUtlVectorFixedGrowable<char, 4096> buf;
buf.SetCount( nNumMessageBytes + 1 );
memcpy( &buf[0], pMsg, nNumMessageBytes );
buf[nNumMessageBytes] = '\0';
//ConMsg( "\"%s\"\n", &buf[0] );
Cbuf_AddText( &buf[0] );
Cbuf_AddText( "\n" );
Cbuf_Execute();
}
break;
}
case cPacketTypeSetCameraPos:
{
const SetCameraPosPacket_t &setCameraPosPacket = reinterpret_cast<const SetCameraPosPacket_t &>( m_RecvBuf[0] );
if ( setCameraPosPacket.m_nTotalSize != sizeof( SetCameraPosPacket_t ) )
{
Warning( "CONCMDSRV: Invalid set camera pos packet!\n" );
return false;
}
m_bReceivedNewCameraPos = true;
m_NewCameraPos = setCameraPosPacket;
break;
}
case cPacketTypeConVarDumpRequest:
{
CUtlVector<uint8> conVarData;
DumpedConVarVector_t conVars;
DumpConVars( conVars );
for ( int i = 0; i < conVars.Count(); i++ )
{
const char *pName = conVars[i].m_Name.Get();
const char *pVal = conVars[i].m_Value.Get();
if ( ( !pName ) || ( !pVal ) )
continue;
conVarData.AddMultipleToTail( V_strlen( pName ) + 1, reinterpret_cast<const uint8 *>( pName ) );
conVarData.AddMultipleToTail( V_strlen( pVal ) + 1, reinterpret_cast<const uint8 *>( pVal ) );
}
PacketHeader_t replyPacket;
replyPacket.m_nID = cPacketHeaderID;
replyPacket.m_nTotalSize = sizeof( replyPacket ) + conVarData.Count();
replyPacket.m_nType = cPacketTypeConVarDumpReply;
if ( !SendData( &replyPacket, sizeof( replyPacket ) ) )
return false;
if ( conVarData.Count() )
{
if ( !SendData( &conVarData[0], conVarData.Count() ) )
return false;
}
break;
}
case cPacketTypeConVarDumpReply:
{
m_bHasNewConVarDump = true;
m_DumpedConVars.SetCountNonDestructively( 0 );
m_DumpedConVars.EnsureCapacity( 4096 );
const char *pCur = reinterpret_cast<const char *>( pMsg );
const char *pEnd = reinterpret_cast<const char *>( pMsg ) + nNumMessageBytes;
while ( pCur < pEnd )
{
const uint nBytesLeft = pEnd - pCur;
uint nNameLen = V_strlen( pCur );
if ( ( nNameLen + 1 ) >= nBytesLeft )
{
Warning( "CONCMDSRV: Failed parsing convar dump reply!\n" );
Assert( 0 );
return false;
}
uint nValLen = V_strlen( pCur + nNameLen + 1 );
uint nTotalLenInBytes = nNameLen + 1 + nValLen + 1;
if ( nTotalLenInBytes > nBytesLeft )
{
Warning( "CONCMDSRV: Failed parsing convar dump reply!\n" );
Assert( 0 );
return false;
}
int nIndex = m_DumpedConVars.AddToTail();
m_DumpedConVars[nIndex].m_Name.Set( pCur );
m_DumpedConVars[nIndex].m_Value.Set( pCur + nNameLen + 1 );
pCur += nTotalLenInBytes;
}
ConMsg( "Received convar dump reply from endpoint %i, %u total convars\n", m_nEndpointIndex, m_DumpedConVars.Count() );
break;
}
default:
{
return false;
}
}
// In case the connection gets lost during a send to reply
if ( ( m_nEndpointIndex < 0 ) || ( nCurRecvBufSize != m_RecvBuf.Count() ) )
return false;
int nNumBytesRemaining = m_RecvBuf.Count() - header.m_nTotalSize;
Assert( nNumBytesRemaining >= 0 );
if ( nNumBytesRemaining >= 0 )
{
memmove( &m_RecvBuf[0], &m_RecvBuf[0] + header.m_nTotalSize, nNumBytesRemaining );
m_RecvBuf.SetCountNonDestructively( nNumBytesRemaining );
}
}
return true;
}
};
ConVar ccs_broadcast_camera( "ccs_broadcast_camera", "0", FCVAR_CHEAT );
ConVar ccs_camera_sync( "ccs_camera_sync", "0", FCVAR_CHEAT );
ConVar ccs_remote_screenshots( "ccs_remote_screenshots", "0", FCVAR_CHEAT );
ConVar ccs_remote_screenshots_delta( "ccs_remote_screenshots_delta", "1", FCVAR_CHEAT );
class CConCommandServer : public CConCommandServerProtocolTypes
{
static bool UserWindowProc( LRESULT& hres, HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, void *pData )
{
CConCommandServer *pServer = static_cast< CConCommandServer * >( pData );
switch ( message )
{
case WM_KEYDOWN:
{
uint cKey = ( wParam & 0xFF );
if ( ( cKey >= '0' ) && ( cKey <= '9' ) )
{
pServer->m_nViewEndpointIndex = cKey - '0';
}
}
}
return false;
}
public:
CConCommandServer() :
m_bInitialized( false ),
m_pFrameBufWindow( NULL ),
m_nViewEndpointIndex( 0 )
{
}
~CConCommandServer()
{
Deinit();
}
bool Init()
{
Deinit();
m_Socket.Init( CT_SERVER, SP_TCP );
if ( m_Socket.Listen( cProtocolPort, cMaxClients ) != SOCKET_SUCCESS )
{
Warning( "CONCMDSRV: CConCommandServer:Init: Failed to create listen socket!\n" );
return false;
}
ConMsg( "CONCMDSVR: Listening for connections\n" );
uint nWidth = 1280, nHeight = 1024;
if ( videomode )
{
nWidth = videomode->GetModeWidth();
nHeight = videomode->GetModeHeight();
}
m_pFrameBufWindow = new frame_buf_window( "CCS", nWidth, nHeight );
m_pFrameBufWindow->set_window_proc_callback( UserWindowProc, this );
m_bInitialized = true;
return true;
}
void Deinit()
{
if ( !m_bInitialized )
return;
delete m_pFrameBufWindow;
m_pFrameBufWindow = NULL;