File: sciReport.c

package info (click to toggle)
openmohaa 0.81.1%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: trixie
  • size: 29,124 kB
  • sloc: ansic: 270,865; cpp: 250,173; sh: 234; asm: 141; xml: 64; makefile: 7
file content (874 lines) | stat: -rw-r--r-- 34,174 bytes parent folder | download | duplicates (2)
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
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#include "sciReport.h"
#include "sciSerialize.h"
#include "../md5.h"

#pragma warning(disable: 4267)

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL sciCreateReport(gsi_u8 theSessionGuid[SC_SESSION_GUID_SIZE], 
								 gsi_u32 theHeaderVersion,
								 gsi_u32 thePlayerCount,
								 gsi_u32 theTeamCount,
								 SCIReport ** theReportOut)
{
	SCIReport * theNewReport;
	SCIReportHeader * theReportHeader;
	gsi_u8 * theReportData;

	// roster is [CCID (16) + TeamIndex (4)] * numplayers
	const gsi_u32 theRosterSize = SC_GUID_BINARY_SIZE * thePlayerCount + 
		                          SC_REPORT_TEAMINDEX_LENGTH * thePlayerCount;

	GS_ASSERT(theReportOut != NULL);

	// allocate the report
	//GS_ASSERT(0); // todo: memalignment
	theNewReport = (SCIReport*)gsimalloc(sizeof(SCIReport));
	if (theNewReport == NULL)
		return SCResult_OUT_OF_MEMORY;
	memset(theNewReport, 0, sizeof(SCIReport));

	// allocate the report buffer (holds submission data)
	theReportData = (gsi_u8*)gsimalloc(SC_REPORT_BUFFER_BYTES);
	if (theReportData == NULL)
	{
		gsifree(theNewReport);
		return SCResult_OUT_OF_MEMORY;
	}
	memset(theReportData, 0, SC_REPORT_BUFFER_BYTES);
	theNewReport->mBuffer.mIsStatic = gsi_false;
	theNewReport->mBuffer.mCapacity = SC_REPORT_BUFFER_BYTES;

	// Fill in report header
	theReportHeader = (SCIReportHeader*)theReportData;
	memset(theReportHeader, 0, sizeof(SCIReportHeader));

	theReportHeader->mProtocolVersion  = htonl(SC_REPORT_PROTOCOL);
	theReportHeader->mDeveloperVersion = htonl(theHeaderVersion);

	theReportHeader->mRosterSectionLength = theRosterSize;
	theReportHeader->mAuthSectionLength = SC_REPORT_AUTHDATA_LENGTH * thePlayerCount;
	theReportHeader->mResultsSectionLength = SC_REPORT_ENTITYRESULT_LENGTH * (thePlayerCount + theTeamCount);
	theReportHeader->mPlayerCount      = (gsi_u16)thePlayerCount;
	theReportHeader->mTeamCount        = (gsi_u16)theTeamCount;
	//theReportHeader->mFlags			   = (gsi_u32)theOptionsFlags;
	
	// Finished, return new report
	theNewReport->mReportState = SCIReportState_ROSTER;
	theNewReport->mBuffer.mData = (char *)theReportData;
	theNewReport->mCurEntityStartPos = -1;
	theNewReport->mNumTeamsReported = 0;

	*theReportOut = theNewReport;
	GSI_UNUSED(theSessionGuid);
	return SCResult_NO_ERROR;
}


////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
SCResult sciDestroyReport(SCIReport *theReport)
{
	theReport->mReportState = SCIReportState_NONE;
	theReport->mCurEntityKeyCount = 0;
	theReport->mCurEntityStartPos = 0;
//	theReport->mNumPlayersReported = 0;
	theReport->mNumTeamsReported = 0;
	theReport->mNumResultsReported = 0;
	
	gsifree(theReport->mBuffer.mData);
	theReport->mBuffer.mData = NULL;
	theReport->mBuffer.mCapacity = 0;
	theReport->mBuffer.mLen = 0;
	theReport->mBuffer.mPos = 0;
	gsifree(theReport);
	return SCResult_NO_ERROR;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL sciReportSetPlayerConnectionId(SCIReport * theReport, gsi_u32 thePlayerIndex, const gsi_u8 theConnectionId[SC_CONNECTION_GUID_SIZE])
{
	gsi_u32 rosterDataOffset = 0;

	// Roster section is just after the header
	rosterDataOffset = sizeof(SCIReportHeader);
	rosterDataOffset += SC_REPORT_ROSTERDATA_LENGTH * thePlayerIndex;

	// copy the connection id
	GS_ASSERT((rosterDataOffset + SC_GUID_BINARY_SIZE) < theReport->mBuffer.mCapacity);

	sciSerializeGUID((gsi_u8 *)&theReport->mBuffer.mData[rosterDataOffset], theConnectionId);
	//memcpy(&theReport->mBuffer.mData[rosterDataOffset], theConnectionId, SC_CONNECTION_GUID_SIZE);

	return SCResult_NO_ERROR;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL sciReportSetPlayerTeamIndex(SCIReport * theReport, gsi_u32 thePlayerIndex, gsi_u32 theTeamIndex)
{
	//SCIReportHeader * aHeader = (SCIReportHeader*)theReport->mBuffer.mData;
	gsi_u32 rosterDataOffset = 0;

	// Roster section is just after the header
	rosterDataOffset = sizeof(SCIReportHeader);
	rosterDataOffset += SC_REPORT_ROSTERDATA_LENGTH * thePlayerIndex;

	// copy the team index, which must appear just after the connection id
	rosterDataOffset += SC_GUID_BINARY_SIZE;
	GS_ASSERT((rosterDataOffset + sizeof(gsi_i32)) < theReport->mBuffer.mCapacity);
	sciSerializeInt32((gsi_u8 *)&theReport->mBuffer.mData[rosterDataOffset], (gsi_i32)theTeamIndex);
	
	return SCResult_NO_ERROR;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL sciReportSetPlayerGameResult(SCIReport * theReport, gsi_u32 thePlayerIndex, SCGameResult theGameResult)
{
	SCIReportHeader * aHeader = (SCIReportHeader*)theReport->mBuffer.mData;
	gsi_u32 resultsDataOffset = 0;

	// Results section is just after the auth section
	resultsDataOffset = sizeof(SCIReportHeader) + aHeader->mRosterSectionLength + aHeader->mAuthSectionLength;
	resultsDataOffset += sizeof(gsi_u32) * thePlayerIndex; // 4 byte game result per player

	// copy the game result in network byte order
	GS_ASSERT((resultsDataOffset + sizeof(gsi_u32)) < theReport->mBuffer.mCapacity);
	sciSerializeInt32((gsi_u8 *)&theReport->mBuffer.mData[resultsDataOffset], theGameResult);
	
	return SCResult_NO_ERROR;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL sciReportSetPlayerAuthInfo(SCIReport * theReport, gsi_u32 thePlayerIndex, const GSLoginCertificate * theCertificate, const gsi_u8 theAuthHash[16])
{
	SCIReportHeader * aHeader = (SCIReportHeader*)theReport->mBuffer.mData;
	gsi_u32 authDataOffset = 0;

	// Auth section is just after the roster
	authDataOffset = sizeof(SCIReportHeader) + aHeader->mRosterSectionLength;
	authDataOffset += SC_REPORT_AUTHDATA_LENGTH * thePlayerIndex;

	// copy auth data into the buffer
	// &theReport->mBuffer.mData[authDataOffset]
	GSI_UNUSED(theAuthHash);
	GSI_UNUSED(theCertificate);
	return SCResult_NO_ERROR;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL sciReportSetTeamGameResult(SCIReport * theReport, gsi_u32 theTeamIndex, SCGameResult theGameResult)
{
	SCIReportHeader * aHeader = (SCIReportHeader*)theReport->mBuffer.mData;
	gsi_u32 resultsDataOffset = 0;

	// Results section is just after the auth section
	//    team results are just after player results
	resultsDataOffset = sizeof(SCIReportHeader) + aHeader->mRosterSectionLength + aHeader->mAuthSectionLength;
	resultsDataOffset += sizeof(gsi_u32) * aHeader->mPlayerCount; // 4 byte game result per player
	resultsDataOffset += sizeof(gsi_u32) * theTeamIndex; // 4 byte game result per player

	// copy the game result in network byte order
	GS_ASSERT((resultsDataOffset + sizeof(gsi_i32)) < theReport->mBuffer.mCapacity);
	sciSerializeInt32((gsi_u8 *)&theReport->mBuffer.mData[resultsDataOffset], theGameResult);
	
	return SCResult_NO_ERROR;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL sciReportSetAsMatchless(SCIReport * theReport)
{
	SCIReportHeader * aHeader;

	aHeader = (SCIReportHeader *)theReport->mBuffer.mData;

	aHeader->mFlags |= SC_REPORT_FLAG_MATCHLESS_SESSION;

	return SCResult_NO_ERROR;
}

/* -----------UNUSED------------
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL sciReportPlayerAuth(SCIReport * theReport,
									 gsi_u32     thePlayerIndex,
									 gsi_u8      thePlayerConnectionId[SC_CONNECTION_GUID_SIZE],
									 gsi_u32     thePlayerTeamIndex,
									 gsi_u32     theProfileId,
									 GSLoginCertificate  * theCertificate,
									 gsi_u8      theAuthHash[16])
{
	SCIReportHeader * aHeader = (SCIReportHeader*)theReport->mBuffer.mData;
	//unsigned int len = 0;

	gsi_u32 rosterDataOffset = 0;
	gsi_u32 authDataOffset = 0;

	//gsi_u8 * rosterWritePos = NULL;

	// Roster section is just after the header
	rosterDataOffset = sizeof(SCIReportHeader);

	// Auth section is just after the roster
	authDataOffset = sizeof(SCIReportHeader) + aHeader->mRosterSectionLength;

	// Check size and length
	GS_ASSERT((authDataOffset + aHeader->mAuthSectionLength) < SC_REPORT_BUFFER_BYTES);

	// Record player index and team in roster section
	//     The report contains an array of players, so we offset the write position
	//     based on the number of players previously reported
	rosterDataOffset += aHeader->mPlayerCount * (SC_GUID_BINARY_SIZE + SC_REPORT_ENTITYINDEX_LENGTH);

	// write the connection id GUID
	GS_ASSERT(rosterDataOffset < authDataOffset);
	sciSerializeGUID((gsi_u8 *)&theReport->mBuffer.mData[rosterDataOffset], thePlayerConnectionId);
	rosterDataOffset += SC_GUID_BINARY_SIZE;

	
//	GS_ASSERT(rosterDataOffset < authDataOffset);
//	memcpy(&theReport->mBuffer.mData[rosterDataOffset], thePlayerConnectionId, SC_GUID_BINARY_SIZE);
//	rosterDataOffset += SC_CONNECTION_GUID_SIZE;
	

	GS_ASSERT(rosterDataOffset < authDataOffset);
	sciSerializeInt16((gsi_u8 *)&theReport->mBuffer.mData[rosterDataOffset], (gsi_i16)thePlayerTeamIndex);
	rosterDataOffset += 2;  // we just wrote an Int16, so bump by two.
	
	GS_ASSERT(rosterDataOffset < authDataOffset); // check overrun
	
	// Write auth data for this player
	//     The report contains an array of auth data, so we offset the write position
	//     based on the number of players previously reported
	authDataOffset += aHeader->mPlayerCount * (SC_GUID_BINARY_SIZE + SC_REPORT_ENTITYINDEX_LENGTH);

	GS_ASSERT((authDataOffset + SC_REPORT_AUTHDATA_LENGTH) < theReport->mBuffer.mCapacity); // check buffer space

	// Write auth data
	//result = wsLoginCertWriteBinary(theCertificate, &theReport->mBuffer.mData[theReport->mBuffer.mPos], (SC_REPORT_BUFFER_BYTES - theReport->mBuffer.mPos), &len);
	//if (gsi_is_false(result))
	//	return SCResult_OUT_OF_MEMORY;
	//memcpy(&theReport->mBuffer.mData[theReport->mBuffer.mPos], theAuthHash, 16);
	//theReport->mBuffer.mPos += 16;

	//theReport->mNumPlayersReported++; // remember how many player's we've written data for
	GSI_UNUSED(theAuthHash);
	GSI_UNUSED(theCertificate);
	GSI_UNUSED(theProfileId);
	GSI_UNUSED(thePlayerIndex);
	return SCResult_NO_ERROR;
}

// -----------UNUSED------------
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL sciReportPlayerResult(SCIReport * theReport,
									   gsi_u32     thePlayerIndex,
									   SCGameResult theResult)
{
	SCIReportHeader * aHeader = (SCIReportHeader*)theReport->mBuffer.mData;
	//unsigned int len = 0;
	gsi_u32 resultsOffset = 0;

	resultsOffset = sizeof(SCIReportHeader) + aHeader->mRosterSectionLength + aHeader->mAuthSectionLength;
	GS_ASSERT(resultsOffset < SC_REPORT_BUFFER_BYTES);

	resultsOffset += SC_REPORT_ENTITYRESULT_LENGTH * theReport->mNumResultsReported;
	sciSerializeInt32((gsi_u8 *)&theReport->mBuffer.mData[resultsOffset], theResult);

	theReport->mNumResultsReported++;
	GSI_UNUSED(thePlayerIndex);
	return SCResult_NO_ERROR;
}

// -----------UNUSED------------
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL sciReportTeamResult(SCIReport * theReport,
									 gsi_u32     theTeamIndex,
									 SCGameResult theResult)
{
	SCIReportHeader * aHeader = (SCIReportHeader*)theReport->mBuffer.mData;
	//unsigned int len = 0;
	gsi_u32 resultsOffset = 0;

	resultsOffset = sizeof(SCIReportHeader) + aHeader->mRosterSectionLength + aHeader->mAuthSectionLength;
	resultsOffset += SC_REPORT_ENTITYRESULT_LENGTH * theReport->mNumResultsReported;
	GS_ASSERT(resultsOffset < SC_REPORT_BUFFER_BYTES);

	resultsOffset += SC_REPORT_ENTITYRESULT_LENGTH * theReport->mNumResultsReported;
	sciSerializeInt32((gsi_u8 *)&theReport->mBuffer.mData[resultsOffset], theResult);

	theReport->mNumResultsReported++;
	GSI_UNUSED(theTeamIndex);
	return SCResult_NO_ERROR;
}*/


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL sciReportBeginNewTeam(SCIReport * theReport)
{
	SCIReportHeader * aHeader = (SCIReportHeader*)theReport->mBuffer.mData;

	//sciReportEndEntity(theReport);

	theReport->mCurEntityKeyCount = 0;
	theReport->mCurEntityStartPos = (gsi_i32)theReport->mBuffer.mPos;
	theReport->mBuffer.mPos += sizeof(gsi_u16);
	sciSerializeInt16((gsi_u8 *)&theReport->mBuffer.mData[theReport->mCurEntityStartPos], (gsi_i16)theReport->mCurEntityKeyCount);
	
	aHeader->mTeamSectionLength += sizeof(gsi_u16);

	// Mark the start of the new entity
	//theReport->mStatus.mCurEntityStart = &theReport->mBuffer.mData[theReport->mBuffer.mPos];
	//theReport->mStatus.mCurEntityType = SC_REPORT_ENTITY_TEAM;
	//theReport->mBuffer.mPos += 4; // skip 4 byte for length, it will be filled in later
	//aHeader->mTeamCount++;
//	aHeader->mTeamDataLength += 4; // 4 bytes for the new team's length

	return SCResult_NO_ERROR;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL sciReportBeginNewPlayer(SCIReport * theReport)
{
	SCIReportHeader * aHeader = (SCIReportHeader*)theReport->mBuffer.mData;
	GS_ASSERT(theReport != NULL);

	//if (theReport->mReportState == SCIReportState_ROSTER)
	{
		// this is the first player

	}
	//GS_ASSERT(theReport->mReportState == SCIReportState_ROSTER);

	theReport->mCurEntityKeyCount = 0;
	theReport->mCurEntityStartPos = (gsi_i32)theReport->mBuffer.mPos;
	theReport->mBuffer.mPos += sizeof(gsi_u16);
	sciSerializeInt16((gsi_u8 *)&theReport->mBuffer.mData[theReport->mCurEntityStartPos], (gsi_i16)theReport->mCurEntityKeyCount);

	aHeader->mPlayerSectionLength += sizeof(gsi_u16);
	

	//sciReportEndEntity(theReport);

	// Mark the start of the new entity
	//theReport->mStatus.mCurEntityStart = &theReport->mBuffer.mData[theReport->mBuffer.mPos];
	//theReport->mStatus.mCurEntityType = SC_REPORT_ENTITY_PLAYER;
	//theReport->mBuffer.mPos += 4; // skip 4 byte for length, it will be filled in later
	//sciSerializeInt32(&theReport->mBuffer.mData[theReport->mBuffer.mPos], thePlayerIndex);
	//theReport->mBuffer.mPos += 4; // 4 byte index
	//aHeader->mPlayerCount++;
//	aHeader->mPlayerDataLength += 4; // 4 bytes for the new player's length
	
	return SCResult_NO_ERROR;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL sciReportBeginGlobalData(SCIReport * theReport)
{
	SCIReportHeader * aHeader = (SCIReportHeader*)theReport->mBuffer.mData;

	GS_ASSERT(theReport != NULL);
	GS_ASSERT(theReport->mReportState == SCIReportState_ROSTER);

	theReport->mReportState = SCIReportState_GLOBALDATA;
	//theReport->mCurEntityIndex = 0;

	// set buffer write position to start of global data
	theReport->mBuffer.mPos = sizeof(SCIReportHeader) + aHeader->mRosterSectionLength +
		aHeader->mAuthSectionLength + aHeader->mResultsSectionLength;
	
	return SCResult_NO_ERROR;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL sciReportBeginPlayerData(SCIReport * theReport)
{
	//SCIReportHeader * aHeader = (SCIReportHeader*)theReport->mBuffer.mData;

	GS_ASSERT(theReport != NULL);

	// Must not have passed into player data state yet.
	GS_ASSERT(theReport->mReportState == SCIReportState_ROSTER ||
		theReport->mReportState == SCIReportState_GLOBALDATA);
	
	// case where there is no global data
	if (theReport->mReportState == SCIReportState_ROSTER)
	{
	}

	theReport->mReportState    = SCIReportState_PLAYERDATA;
	//theReport->mCurEntityIndex = 0;

	return SCResult_NO_ERROR;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL sciReportBeginTeamData(SCIReport * theReport)
{
	GS_ASSERT(theReport != NULL);

	// Must not have passed into player data state yet.
	GS_ASSERT(
		theReport->mReportState == SCIReportState_ROSTER || 
		theReport->mReportState == SCIReportState_GLOBALDATA ||
		theReport->mReportState == SCIReportState_PLAYERDATA
		);
	
	theReport->mReportState    = SCIReportState_TEAMDATA;
	//theReport->mCurEntityIndex = 0;

	return SCResult_NO_ERROR;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL sciReportEndEntity(SCIReport * theReport)
{
	//gsi_u8 * anEntityEnd = NULL; 
	//gsi_u32  anEntityLen = 0;

	// Calculate and update the last entities length
	/*if (theReport->mStatus.mCurEntityStart != NULL)
	{
		anEntityEnd = &theReport->mBuffer.mData[theReport->mBuffer.mPos];
		anEntityLen = anEntityEnd - theReport->mStatus.mCurEntityStart;
		sciSerializeDataLength(theReport->mStatus.mCurEntityStart, anEntityLen);
		theReport->mStatus.mCurEntityStart = NULL;
	}*/
	GSI_UNUSED(theReport);
	return SCResult_NO_ERROR;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL sciReportEnd(SCIReport * theReport, gsi_bool isAuth, SCGameStatus theStatus)
{
	SCIReportHeader * aHeader = (SCIReportHeader*)theReport->mBuffer.mData;

	sciReportEndEntity(theReport);

	if (gsi_is_true(isAuth))
		aHeader->mFlags |= SC_REPORT_FLAG_AUTHORITATIVE;

	// Send header in network byte order
	aHeader->mGameStatus  = htonl(theStatus);

	aHeader->mFlags       = htonl(aHeader->mFlags);
	aHeader->mPlayerCount = htons(aHeader->mPlayerCount);
	aHeader->mTeamCount   = htons(aHeader->mTeamCount);
	aHeader->mReserved = 0;

	aHeader->mGameKeyCount = htons(aHeader->mGameKeyCount);
	aHeader->mPlayerKeyCount = htons(aHeader->mPlayerKeyCount);
	aHeader->mTeamKeyCount   = htons(aHeader->mTeamKeyCount);

	aHeader->mRosterSectionLength  = htonl(aHeader->mRosterSectionLength);
	aHeader->mAuthSectionLength    = htonl(aHeader->mAuthSectionLength);
	aHeader->mResultsSectionLength = htonl(aHeader->mResultsSectionLength);

	aHeader->mGameSectionLength   = htonl(aHeader->mGameSectionLength);
	aHeader->mPlayerSectionLength = htonl(aHeader->mPlayerSectionLength);
	aHeader->mTeamSectionLength   = htonl(aHeader->mTeamSectionLength);


//	aHeader->mSessionDataLength = htonl(aHeader->mSessionDataLength);
//	aHeader->mPlayerDataLength = htonl(aHeader->mPlayerDataLength);
//	aHeader->mTeamDataLength = htonl(aHeader->mTeamDataLength);

	return SCResult_NO_ERROR;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL sciReportAddIntValue(SCIReport * theReport,
									  gsi_u16     theKeyId,
									  gsi_i32     theValue)
{
	SCIReportHeader * aHeader = (SCIReportHeader*)theReport->mBuffer.mData;
	int writtenLen = 0;
	gsi_i16 theKeyType = SCIKeyType_INT32;
	
	// calculate length of data to be written
	writtenLen += sizeof(gsi_u16) + sizeof(gsi_u16); // 2 byte key ID, 2 byte key type;
	writtenLen += sizeof(gsi_u32); // 4 bytes of data

	// Check room in buffer
	GS_ASSERT((theReport->mBuffer.mPos + writtenLen) < theReport->mBuffer.mCapacity);

	// Update count and length markers
	if (theReport->mReportState == SCIReportState_GLOBALDATA)
	{
		aHeader->mGameKeyCount++;
		aHeader->mGameSectionLength += writtenLen;
	}
	else if (theReport->mReportState == SCIReportState_PLAYERDATA)
	{
		GS_ASSERT(theReport->mCurEntityStartPos != -1);
		aHeader->mPlayerKeyCount++;
		aHeader->mPlayerSectionLength += writtenLen;
		
		theReport->mCurEntityKeyCount++;
		sciSerializeInt16((gsi_u8 *)&theReport->mBuffer.mData[theReport->mCurEntityStartPos], (gsi_i16)theReport->mCurEntityKeyCount);
	}
	else if (theReport->mReportState == SCIReportState_TEAMDATA)
	{
		aHeader->mTeamKeyCount++;
		aHeader->mTeamSectionLength += writtenLen;

		theReport->mCurEntityKeyCount++;
		sciSerializeInt16((gsi_u8 *)&theReport->mBuffer.mData[theReport->mCurEntityStartPos], (gsi_i16)theReport->mCurEntityKeyCount);
	}
	// Needs real error handling code or an assertion on a non-constant expression
	//else 
	//	GS_ASSERT(0); // invalid state for writing key/value pairs!

	// Write the data
	sciSerializeInt16((gsi_u8 *)&theReport->mBuffer.mData[theReport->mBuffer.mPos], (gsi_i16)theKeyId);
	theReport->mBuffer.mPos += sizeof(gsi_u16);
	sciSerializeInt16((gsi_u8 *)&theReport->mBuffer.mData[theReport->mBuffer.mPos], theKeyType);
	theReport->mBuffer.mPos += sizeof(gsi_u16);
	sciSerializeInt32((gsi_u8 *)&theReport->mBuffer.mData[theReport->mBuffer.mPos], theValue);
	theReport->mBuffer.mPos += sizeof(gsi_u32);

	return SCResult_NO_ERROR;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL sciReportAddInt64Value(SCIReport * theReport, 
										gsi_u16 theKeyId, 
										gsi_i64 theValue)
{
	SCIReportHeader * aHeader = (SCIReportHeader*)theReport->mBuffer.mData;
	int writtenLen = 0;
	gsi_i16 theKeyType = SCIKeyType_INT64;

	// calculate length of data to be written
	writtenLen += sizeof(gsi_u16) + sizeof(gsi_u16); // 2 byte key ID, 2 byte key type;
	writtenLen += sizeof(gsi_i64); // 4 bytes of data

	// Check room in buffer
	GS_ASSERT((theReport->mBuffer.mPos + writtenLen) < theReport->mBuffer.mCapacity);

	// Update count and length markers
	if (theReport->mReportState == SCIReportState_GLOBALDATA)
	{
		aHeader->mGameKeyCount++;
		aHeader->mGameSectionLength += writtenLen;
	}
	else if (theReport->mReportState == SCIReportState_PLAYERDATA)
	{
		GS_ASSERT(theReport->mCurEntityStartPos != -1);
		aHeader->mPlayerKeyCount++;
		aHeader->mPlayerSectionLength += writtenLen;

		theReport->mCurEntityKeyCount++;
		sciSerializeInt16((gsi_u8 *)&theReport->mBuffer.mData[theReport->mCurEntityStartPos], (gsi_i16)theReport->mCurEntityKeyCount);
	}
	else if (theReport->mReportState == SCIReportState_TEAMDATA)
	{
		aHeader->mTeamKeyCount++;
		aHeader->mTeamSectionLength += writtenLen;

		theReport->mCurEntityKeyCount++;
		sciSerializeInt16((gsi_u8 *)&theReport->mBuffer.mData[theReport->mCurEntityStartPos], (gsi_i16)theReport->mCurEntityKeyCount);
	}

	// Write the data
	sciSerializeInt16((gsi_u8 *)&theReport->mBuffer.mData[theReport->mBuffer.mPos], (gsi_i16)theKeyId);
	theReport->mBuffer.mPos += sizeof(gsi_u16);
	sciSerializeInt16((gsi_u8 *)&theReport->mBuffer.mData[theReport->mBuffer.mPos], theKeyType);
	theReport->mBuffer.mPos += sizeof(gsi_u16);
	sciSerializeInt64((gsi_u8 *)&theReport->mBuffer.mData[theReport->mBuffer.mPos], theValue);
	theReport->mBuffer.mPos += sizeof(gsi_i64);

	return SCResult_NO_ERROR;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL sciReportAddShortValue(SCIReport * theReport,
									    gsi_u16     theKeyId,
									    gsi_i16     theValue)
{
	SCIReportHeader * aHeader = (SCIReportHeader*)theReport->mBuffer.mData;
	int writtenLen = 0;
	gsi_i16 theKeyType = SCIKeyType_INT16;
	
	// calculate length of data to be written
	writtenLen += sizeof(gsi_u16) + sizeof(gsi_u16); // 2 byte key ID, 2 byte key type;
	writtenLen += sizeof(gsi_u16); // 2 bytes of data

	// Check room in buffer
	GS_ASSERT((theReport->mBuffer.mPos + writtenLen) < theReport->mBuffer.mCapacity);

	// Update count and length markers
	if (theReport->mReportState == SCIReportState_GLOBALDATA)
	{
		aHeader->mGameKeyCount++;
		aHeader->mGameSectionLength += writtenLen;
	}
	else if (theReport->mReportState == SCIReportState_PLAYERDATA)
	{
		GS_ASSERT(theReport->mCurEntityStartPos != -1);
		aHeader->mPlayerKeyCount++;
		aHeader->mPlayerSectionLength += writtenLen;
		
		theReport->mCurEntityKeyCount++;
		sciSerializeInt16((gsi_u8 *)&theReport->mBuffer.mData[theReport->mCurEntityStartPos], (gsi_i16)theReport->mCurEntityKeyCount);
	}
	else if (theReport->mReportState == SCIReportState_TEAMDATA)
	{
		aHeader->mTeamKeyCount++;
		aHeader->mTeamSectionLength += writtenLen;

		theReport->mCurEntityKeyCount++;
		sciSerializeInt16((gsi_u8 *)&theReport->mBuffer.mData[theReport->mCurEntityStartPos], (gsi_i16)theReport->mCurEntityKeyCount);
	}
	// Needs real error handling code or an assertion on a non-constant expression
	//else 
	//	GS_ASSERT(0); // invalid state for writing key/value pairs!

	// Write the data
	sciSerializeInt16((gsi_u8 *)&theReport->mBuffer.mData[theReport->mBuffer.mPos], (gsi_i16)theKeyId);
	theReport->mBuffer.mPos += sizeof(gsi_u16);
	sciSerializeInt16((gsi_u8 *)&theReport->mBuffer.mData[theReport->mBuffer.mPos], theKeyType);
	theReport->mBuffer.mPos += sizeof(gsi_u16);
	sciSerializeInt16((gsi_u8 *)&theReport->mBuffer.mData[theReport->mBuffer.mPos], theValue);
	theReport->mBuffer.mPos += sizeof(gsi_u16);

	return SCResult_NO_ERROR;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL sciReportAddByteValue(SCIReport * theReport,
									   gsi_u16     theKeyId,
									   gsi_i8      theValue)
{
	SCIReportHeader * aHeader = (SCIReportHeader*)theReport->mBuffer.mData;
	int writtenLen = 0;
	gsi_i16 theKeyType = SCIKeyType_BYTE;
	
	// calculate length of data to be written
	writtenLen += sizeof(gsi_u16) + sizeof(gsi_u16); // 2 byte key ID, 2 byte key type;
	writtenLen += sizeof(gsi_u8); // 1 bytes of data

	// Check room in buffer
	GS_ASSERT((theReport->mBuffer.mPos + writtenLen) < theReport->mBuffer.mCapacity);

	// Update count and length markers
	if (theReport->mReportState == SCIReportState_GLOBALDATA)
	{
		aHeader->mGameKeyCount++;
		aHeader->mGameSectionLength += writtenLen;
	}
	else if (theReport->mReportState == SCIReportState_PLAYERDATA)
	{
		GS_ASSERT(theReport->mCurEntityStartPos != -1);
		aHeader->mPlayerKeyCount++;
		aHeader->mPlayerSectionLength += writtenLen;
		
		theReport->mCurEntityKeyCount++;
		sciSerializeInt16((gsi_u8 *)&theReport->mBuffer.mData[theReport->mCurEntityStartPos], (gsi_i16)theReport->mCurEntityKeyCount);
	}
	else if (theReport->mReportState == SCIReportState_TEAMDATA)
	{
		aHeader->mTeamKeyCount++;
		aHeader->mTeamSectionLength += writtenLen;

		theReport->mCurEntityKeyCount++;
		sciSerializeInt16((gsi_u8 *)&theReport->mBuffer.mData[theReport->mCurEntityStartPos], (gsi_i16)theReport->mCurEntityKeyCount);
	}
	// Needs real error handling code or an assertion on a non-constant expression
	//else 
	//	GS_ASSERT(0); // invalid state for writing key/value pairs!

	// Write the data
	sciSerializeInt16((gsi_u8 *)&theReport->mBuffer.mData[theReport->mBuffer.mPos], (gsi_i16)theKeyId);
	theReport->mBuffer.mPos += sizeof(gsi_u16);
	sciSerializeInt16((gsi_u8 *)&theReport->mBuffer.mData[theReport->mBuffer.mPos], theKeyType);
	theReport->mBuffer.mPos += sizeof(gsi_u16);
	sciSerializeInt8((gsi_u8 *)&theReport->mBuffer.mData[theReport->mBuffer.mPos], theValue);
	theReport->mBuffer.mPos += sizeof(gsi_u8);

	return SCResult_NO_ERROR;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL sciReportAddFloatValue(SCIReport * theReport,
									    gsi_u16     theKeyId,
									    float       theValue)
{
	SCIReportHeader * aHeader = (SCIReportHeader*)theReport->mBuffer.mData;
	int writtenLen = 0;
	gsi_i16 theKeyType = SCIKeyType_FLOAT;

	// calculate length of data to be written
	writtenLen += sizeof(gsi_u16) + sizeof(gsi_u16); // 2 byte key ID, 2 byte key type;
	writtenLen += sizeof(float);					 // stored in stream as a 4 byte char array

	// Check room in buffer
	GS_ASSERT((theReport->mBuffer.mPos + writtenLen) < theReport->mBuffer.mCapacity);

	// Update count and length markers
	if (theReport->mReportState == SCIReportState_GLOBALDATA)
	{
		aHeader->mGameKeyCount++;
		aHeader->mGameSectionLength += writtenLen;
	}
	else if (theReport->mReportState == SCIReportState_PLAYERDATA)
	{
		GS_ASSERT(theReport->mCurEntityStartPos != -1);
		aHeader->mPlayerKeyCount++;
		aHeader->mPlayerSectionLength += writtenLen;
		
		theReport->mCurEntityKeyCount++;
		sciSerializeInt16((gsi_u8 *)&theReport->mBuffer.mData[theReport->mCurEntityStartPos], (gsi_i16)theReport->mCurEntityKeyCount);
	}
	else if (theReport->mReportState == SCIReportState_TEAMDATA)
	{
		aHeader->mTeamKeyCount++;
		aHeader->mTeamSectionLength += writtenLen;

		theReport->mCurEntityKeyCount++;
		sciSerializeInt16((gsi_u8 *)&theReport->mBuffer.mData[theReport->mCurEntityStartPos], (gsi_i16)theReport->mCurEntityKeyCount);
	}
	// Needs real error handling code or an assertion on a non-constant expression
	//else 
	//	GS_ASSERT(0); // invalid state for writing key/value pairs!

	// Write the data
	sciSerializeInt16((gsi_u8 *)&theReport->mBuffer.mData[theReport->mBuffer.mPos], (gsi_i16)theKeyId);
	theReport->mBuffer.mPos += sizeof(gsi_u16);
	sciSerializeInt16((gsi_u8 *)&theReport->mBuffer.mData[theReport->mBuffer.mPos], theKeyType);
	theReport->mBuffer.mPos += sizeof(gsi_u16);
	sciSerializeFloat((gsi_u8 *)&theReport->mBuffer.mData[theReport->mBuffer.mPos], theValue);
	theReport->mBuffer.mPos += sizeof(float);

	return SCResult_NO_ERROR;
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SCResult SC_CALL sciReportAddStringValue(SCIReport *      theReport,
										 gsi_u16          theKeyId,
										 const gsi_char * theValue)
{
	SCIReportHeader * aHeader = (SCIReportHeader*)theReport->mBuffer.mData;
	int writtenLen = 0, numLenBytes = 0, asciiLen = 0;
	gsi_i16 theKeyType = SCIKeyType_STRING;	

	// for unicode we need to store the ascii-converted string first in the buffer so as
	// not to dynamically allocate space for it. Then once we have the length of it, we can
	// write over the buffer

#if defined(GSI_UNICODE)
	// First check room in buffer
	GS_ASSERT(((theReport->mBuffer.mPos + (_tcslen(theValue)) + (sizeof(gsi_u16)*2))
		< theReport->mBuffer.mCapacity));

	// as long as we have enough room - add to the buffer and store length of the Ascii string
	// need to remove 1 for the appended null char \0 which we want to remove
	asciiLen = UCS2ToAsciiString(theValue, &theReport->mBuffer.mData[theReport->mBuffer.mPos])-1;

	// now determine how many bytes are necessary to represent len
	numLenBytes = (asciiLen/127)+1; 

#else

	numLenBytes = (gsi_i32)(strlen(theValue)/127)+1; //finds out number of bytes necessary to represent len
	asciiLen = (int)strlen(theValue);
#endif


	// calculate length of data to be written
	writtenLen += sizeof(gsi_u16) + sizeof(gsi_u16);	// 2 byte key ID, 2 byte key type;
	writtenLen += asciiLen;								// 0 for empty string
	writtenLen += numLenBytes;							// the number of bytes necessary to represent len

	// Check room in buffer
	GS_ASSERT((theReport->mBuffer.mPos + writtenLen) < theReport->mBuffer.mCapacity);

	// Update count and length markers
	if (theReport->mReportState == SCIReportState_GLOBALDATA)
	{
		aHeader->mGameKeyCount++;
		aHeader->mGameSectionLength += writtenLen;
	}
	else if (theReport->mReportState == SCIReportState_PLAYERDATA)
	{
		aHeader->mPlayerKeyCount++;
		aHeader->mPlayerSectionLength += writtenLen;

		theReport->mCurEntityKeyCount++;
		sciSerializeInt16((gsi_u8 *)&theReport->mBuffer.mData[theReport->mCurEntityStartPos], (gsi_i16)theReport->mCurEntityKeyCount);
	}
	else if (theReport->mReportState == SCIReportState_TEAMDATA)
	{
		aHeader->mTeamKeyCount++;
		aHeader->mTeamSectionLength += writtenLen;

		theReport->mCurEntityKeyCount++;
		sciSerializeInt16((gsi_u8 *)&theReport->mBuffer.mData[theReport->mCurEntityStartPos], (gsi_i16)theReport->mCurEntityKeyCount);
	}
	// Needs real error handling code or an assertion on a non-constant expression
	//else 
	//	GS_ASSERT(0); // invalid state for writing key/value pairs!

	// Write the data
	sciSerializeInt16((gsi_u8 *)&theReport->mBuffer.mData[theReport->mBuffer.mPos], (gsi_i16)theKeyId);
	theReport->mBuffer.mPos += sizeof(gsi_u16);
	sciSerializeInt16((gsi_u8 *)&theReport->mBuffer.mData[theReport->mBuffer.mPos], theKeyType);
	theReport->mBuffer.mPos += sizeof(gsi_u16);

	// now prior to writing the string we need to prepend the length of the string to follow
	// the .NET format of (string length)(String)
	memset(&theReport->mBuffer.mData[theReport->mBuffer.mPos], asciiLen, (gsi_u32)numLenBytes);
	theReport->mBuffer.mPos += numLenBytes;

#if defined(GSI_UNICODE)
	// Now strip to Ascii and write the length - subtract 1 to get rid of appended null character
	theReport->mBuffer.mPos += UCS2ToAsciiString(theValue, &theReport->mBuffer.mData[theReport->mBuffer.mPos])-1;
#else
	strncpy(&theReport->mBuffer.mData[theReport->mBuffer.mPos], theValue, strlen(theValue));
	theReport->mBuffer.mPos += strlen(theValue);
#endif

	return SCResult_NO_ERROR;
}

#pragma warning(default: 4267)