-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuakePacket.cpp
511 lines (425 loc) · 11.5 KB
/
QuakePacket.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
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
// QuakePacket.cpp : implementation of the CQuakePacket class
//
// Pandora Bot v2.0
// By Darrin W. Cullop (dcullop@arizona.edu)
//
// Copyright (c) 1998-1999 by Darrin W. Cullop. All Rights Reserved.
//
// Permission to use, copy and distribute unedited copies of this whole
// document is hereby granted, provided that no fee is charged for the
// use or availability of this document (other than the normal connection
// costs for on-line services, if applicable). The above copyright notice
// and this permission notice must be left intact in all copies of this
// document. Short excerpts of this document may be quoted in discussion
// groups or mailing list articles, as long as a reference to the full
// document is given.
//
// Commercial distribution of this document, in whole or in part,
// requires prior agreement with the author. Commercial distribution
// includes any means by which the user has to pay either for the support
// (e.g. book, newsletter or CD-ROM) or for the document itself.
//
// Unauthorized commercial distribution is prohibited.
//
//////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "QuakePacket.h"
#include "PandoraDefs.h"
#include "NetUtils.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//////////////////////////////////////////////////////////////////////////////////////////
// CQuakePacket Local Definitions
#ifdef _DEBUG
// Include this for dumping extended information
// or comment it out for just the standard stuff
//#define DUMP_EXTENDED_INFO
#endif // _DEBUG
//////////////////////////////////////////////////////////////////////////////////////////
// CQuakePacket
IMPLEMENT_DYNCREATE(CQuakePacket, CObject)
//////////////////////////////////////////////////////////////////////////////////////////
// CQuakePacket Construction / Destruction
// Defined Inline
//////////////////////////////////////////////////////////////////////////////////////////
// CQuakePacket Operations
// Private Operations
void CQuakePacket::MergeBuffer(CQuakePacket& rOther, const WORD& nInsertPoint)
{
GetDataArray().InsertAt(nInsertPoint, &rOther.GetDataArray());
if (m_nPosition >= nInsertPoint)
{
m_nPosition += rOther.GetSize();
}
}
void CQuakePacket::SetReadMode()
{
m_bReadMode = TRUE;
m_nPosition = 0;
}
void CQuakePacket::SetWriteMode()
{
m_bReadMode = FALSE;
m_nPosition = GetSize();
}
//////////////////////////////////////////////////////////////////////////////////////////
// CQuakePacket Overrides
void CQuakePacket::Serialize(CArchive& ar)
{
m_arrData.Serialize(ar);
}
//////////////////////////////////////////////////////////////////////////////////////////
// CQuakePacket Diagnostics
#ifdef _DEBUG
void CQuakePacket::AssertValid() const
{
ASSERT( (GetPosition() < GetSize()) );
GetData().AssertValid();
CObject::AssertValid();
}
void CQuakePacket::Dump(CDumpContext& dc) const
{
// Standard Dumping Format
dc << "CQuakePacket" << "\n";
dc << "\tRead Mode: " << (GetReadMode() ? _T("True") : _T("False") ) << "\n";
dc << "\tPosition: " << GetPosition() << "\n";
// Special Format for displaying data in an easy (easier anyway) to read format
CString strTemp;
CString strHexData;
CString strRawData;
BYTE nCurrent;
register int nCounter;
#ifdef DUMP_EXTENDED_INFO
DWORD nCheckSum = 0;
for ( nCounter = 0; ( nCounter < GetSize() ); nCounter++ )
{
nCheckSum += GetData().GetAt( nCounter );
}
strTemp.Format("Length: %d (0x%04X), CheckSum: %d (0x%04X)",
GetSize(), GetSize(), nCheckSum, nCheckSum);
dc << "\tData [" << strTemp << "]" << "\n";
#else
dc << "\tData" << "\n";
#endif // DUMP_EXTENDED_INFO
for ( nCounter = 0; ( nCounter < GetSize() ); nCounter++ )
{
if ((nCounter % 16) == 0)
{
// Format the "lead" information at the beginning of each line
strHexData.Format("%05X: ", nCounter);
strRawData.Empty();
}
nCurrent = ( (BYTE) ( GetData().GetAt(nCounter) ) );
strTemp.Format(" %02X", nCurrent);
strHexData += strTemp;
// If the current character is non-printable,
// change it so it displays a decimal point instead
if ((nCurrent <= 32) || (nCurrent >= 128))
{
nCurrent = '.';
}
strRawData += ( (char)(nCurrent) );
// After processing 16 bytes of data, display the output to the screen
if ( (nCounter % 16) == 15 )
{
dc << "\t" << strHexData << "\t\t" << strRawData << "\n";
strHexData.Empty();
strRawData.Empty();
}
}
// If we stopped, and the last data set had not been printed out because
// it was less than 16 bytes long,
// then print out the last one.
if (!(strHexData.IsEmpty()))
{
// Make sure the string is as long as the other ones, so everything lines
// up properly. Here is where the number 56 comes from:
// 8 for the lead information (5 digits, plus a colon and two spaces)
// 3*16 for each byte of data (2 digits plus a space)
// 8 + 48 = 56
strTemp.Format("%-56s", strHexData);
dc << "\t" << strTemp << "\t\t" << strRawData << "\n";
}
// Call the base class
//CSystemObject::Dump(dc);
CObject::Dump(dc);
}
#endif
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
// CQuakePacket Inline Functions
///////////////////////////////////////////////////////////////////////////
// CQuakePacket Operations
void CQuakePacket::RemoveData(const WORD& nPosition, const WORD& nLength)
{
ASSERT(nPosition < GetSize());
if (m_nPosition > nPosition)
{
if (nLength > m_nPosition)
{
m_nPosition = 0;
}
else
{
m_nPosition -= nLength;
}
}
GetDataArray().RemoveAt(nPosition, nLength);
}
void CQuakePacket::PrependData(CQuakePacket& rOther)
{
MergeBuffer(rOther, 0);
}
void CQuakePacket::InsertData(CQuakePacket& rOther)
{
MergeBuffer(rOther, m_nPosition);
}
void CQuakePacket::Clear()
{
SetWriteMode();
m_nPosition = 0;
m_arrData.RemoveAll();
m_arrData.SetSize( MAX_PACKET_LENGTH, MAX_PACKET_LENGTH );
}
void CQuakePacket::Copy(const CQuakePacket& rCopyMe)
{
// TODO: Try and see if CopyMemory works or not
// CopyMemory( this, &rCopyMe, sizeof(rCopyMe) );
m_bReadMode = rCopyMe.GetReadMode();
m_nPosition = rCopyMe.GetSize();
m_arrData.RemoveAll();
m_arrData.SetSize(rCopyMe.GetData().GetSize());
m_arrData.Append(rCopyMe.GetData());
}
BOOL CQuakePacket::DataToRead() const
{
if ((m_bReadMode) && (m_nPosition < GetSize()))
{
return TRUE;
}
else
{
return FALSE;
}
}
//////////////////////////////////////////////////////////////////////////////
// CQuakePacket Overloaded Operators
CQuakePacket& CQuakePacket::operator=(const CQuakePacket& rCopyMe)
{
Copy(rCopyMe);
return (*this);
}
//////////////////////////////////////////////////////////////////////////////////
// Private Read/Write Functions
BYTE CQuakePacket::GetByte()
{
ASSERT(GetReadMode() == TRUE);
ASSERT(GetPosition() < GetDataArray().GetSize());
return GetDataArray().GetAt(m_nPosition++);
}
void CQuakePacket::PutByte(const BYTE& nData)
{
ASSERT(GetReadMode() == FALSE);
GetDataArray().SetAtGrow(m_nPosition++, nData);
}
//////////////////////////////////////////////////////////////////////////////////
// Read Functions
short CQuakePacket::ReadShort()
{
short nReturnMe;
ReadShort(nReturnMe);
return nReturnMe;
}
void CQuakePacket::ReadShort(short& nGet)
{
BYTE *pTemp;
register int nCount;
pTemp = (BYTE *)&nGet;
for(nCount = 0; nCount < sizeof(short); nCount++)
{
pTemp[nCount] = GetByte();
}
}
long CQuakePacket::ReadLong()
{
long nReturnMe;
ReadLong(nReturnMe);
return nReturnMe;
}
void CQuakePacket::ReadLong(long& nGet)
{
BYTE *pTemp;
register int nCount;
pTemp = (BYTE *)&nGet;
for(nCount = 0; nCount < sizeof(long); nCount++)
{
pTemp[nCount] = GetByte();
}
}
float CQuakePacket::ReadFloat()
{
float nReturnMe;
ReadFloat(nReturnMe);
return nReturnMe;
}
void CQuakePacket::ReadFloat(float& nGet)
{
BYTE* pTemp;
register int nCount;
pTemp = (BYTE *)&nGet;
for(nCount = 0; nCount < sizeof(float); nCount++)
{
pTemp[nCount] = GetByte();
}
}
float CQuakePacket::ReadCoord()
{
float nReturnMe;
ReadCoord( nReturnMe );
return nReturnMe;
}
void CQuakePacket::ReadCoord(float& nGet)
{
nGet = ((float)((float)ReadShort() / (float)8.0));
}
float CQuakePacket::ReadAngle()
{
float nReturnMe;
ReadAngle( nReturnMe );
return nReturnMe;
}
void CQuakePacket::ReadAngle(float& nGet)
// Angles are sent as a single byte (Scaled down from 360 to 256)
{
nGet = ((float)((float)ReadChar() * ((float)(360.0 / 256.0))));
}
float CQuakePacket::ReadAngle16()
{
float nReturnMe;
ReadAngle16( nReturnMe );
return nReturnMe;
}
void CQuakePacket::ReadAngle16(float& nGet)
// Some angles are sent as two bytes (Scaled from 360 to 65535)
{
nGet = ShortToAngle( ReadShort() );
}
CVector CQuakePacket::ReadAngles()
{
CVector vReturnMe;
ReadAngles(vReturnMe);
return vReturnMe;
}
void CQuakePacket::ReadAngles( CVector& rReadMe )
{
ReadAngle( rReadMe.GetSetX() );
ReadAngle( rReadMe.GetSetY() );
ReadAngle( rReadMe.GetSetZ() );
}
CVector CQuakePacket::ReadAngles16()
{
CVector vReturnMe;
ReadAngles16(vReturnMe);
return vReturnMe;
}
void CQuakePacket::ReadAngles16( CVector& rReadMe )
{
ReadAngle16( rReadMe.GetSetX() );
ReadAngle16( rReadMe.GetSetY() );
ReadAngle16( rReadMe.GetSetZ() );
}
CVector CQuakePacket::ReadVector()
{
CVector vReturnMe;
ReadVector(vReturnMe);
return vReturnMe;
}
void CQuakePacket::ReadVector(CVector& rReadMe)
{
ReadCoord( rReadMe.GetSetX() );
ReadCoord( rReadMe.GetSetY() );
ReadCoord( rReadMe.GetSetZ() );
}
CString CQuakePacket::ReadString()
{
CString strReturnMe;
ReadString(strReturnMe);
return strReturnMe;
}
void CQuakePacket::ReadString(CString& strReadMe)
{
register int nCounter = 0;
char chrTemp;
strReadMe.Empty();
while (DataToRead() && ((chrTemp = ReadChar()) != 0))
{
strReadMe += chrTemp;
}
}
//////////////////////////////////////////////////////////////////////////////////
// Write Operations
void CQuakePacket::WriteAngle(const float& nData)
// Angles are sent as a single byte (Scaled down from 360 to 256)
// NOTE: (256 / 360) = 0.7111111111111
{
WriteByte((BYTE)(nData * 0.7111));
}
void CQuakePacket::WriteAngle16(const float& nData)
{
WriteShort( AngleToShort(nData) );
}
void CQuakePacket::WriteShort(const short& nData)
{
BYTE* pTemp;
register int nCount;
pTemp = (BYTE *)&nData;
for (nCount = 0; nCount < sizeof(short); nCount++)
{
PutByte(pTemp[nCount]);
}
}
void CQuakePacket::WriteLong(const long& nData)
{
BYTE* pTemp;
register int nCount;
pTemp = (BYTE*)&nData;
for (nCount = 0; nCount < sizeof(long); nCount++)
{
PutByte(pTemp[nCount]);
}
}
void CQuakePacket::WriteFloat(const float& nData)
{
BYTE* pTemp;
register int nCount;
pTemp = (BYTE *)&nData;
for (nCount = 0; nCount < sizeof(float); nCount++)
{
PutByte(pTemp[nCount]);
}
}
void CQuakePacket::WriteCoord(const float& nData)
{
WriteShort( ((short)(nData * 8)) );
}
void CQuakePacket::WriteVector(const CVector& rData)
{
WriteCoord( rData.GetX() );
WriteCoord( rData.GetY() );
WriteCoord( rData.GetZ() );
}
void CQuakePacket::WriteString(const char* strData)
{
WORD nTemp;
VERIFY(strData != NULL);
nTemp = 0;
while (strData[nTemp] != '\0')
{
PutByte(strData[nTemp++]);
}
PutByte('\0');
}
//////////////////////////////////////////////////////////////////////////////////