-
Notifications
You must be signed in to change notification settings - Fork 15
/
BitStream_NoTemplate.cpp
1875 lines (1545 loc) · 46.8 KB
/
BitStream_NoTemplate.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
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
/// \depreciated
#if defined(_MSC_VER) && _MSC_VER < 1299 // VC6 doesn't support template specialization
#include "RakNetDefines.h"
#include "BitStream.h"
#include <stdlib.h>
#include <assert.h>
#include <memory.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <float.h>
#if defined ( __APPLE__ ) || defined ( __APPLE_CC__ )
#include <malloc/malloc.h>
#else
#include <malloc.h>
#endif
#ifdef __BITSTREAM_BIG_END
// Set up the read/write routines to produce Big-End network streams.
#define B16_1 0
#define B16_0 1
#define B32_3 0
#define B32_2 1
#define B32_1 2
#define B32_0 3
#define B64_7 0
#define B64_6 1
#define B64_5 2
#define B64_4 3
#define B64_3 4
#define B64_2 5
#define B64_1 6
#define B64_0 7
#else
// Default to producing Little-End network streams.
#define B16_1 1
#define B16_0 0
#define B32_3 3
#define B32_2 2
#define B32_1 1
#define B32_0 0
#define B64_7 7
#define B64_6 6
#define B64_5 5
#define B64_4 4
#define B64_3 3
#define B64_2 2
#define B64_1 1
#define B64_0 0
#endif
using namespace RakNet;
BitStream::BitStream()
{
numberOfBitsUsed = 0;
//numberOfBitsAllocated = 32 * 8;
numberOfBitsAllocated = BITSTREAM_STACK_ALLOCATION_SIZE * 8;
readOffset = 0;
//data = ( unsigned char* ) malloc( 32 );
data = ( unsigned char* ) stackData;
#ifdef _DEBUG
// assert( data );
#endif
//memset(data, 0, 32);
copyData = true;
}
BitStream::BitStream( int initialBytesToAllocate )
{
numberOfBitsUsed = 0;
readOffset = 0;
if (initialBytesToAllocate <= BITSTREAM_STACK_ALLOCATION_SIZE)
{
data = ( unsigned char* ) stackData;
numberOfBitsAllocated = BITSTREAM_STACK_ALLOCATION_SIZE * 8;
}
else
{
data = ( unsigned char* ) malloc( initialBytesToAllocate );
numberOfBitsAllocated = initialBytesToAllocate << 3;
}
#ifdef _DEBUG
assert( data );
#endif
// memset(data, 0, initialBytesToAllocate);
copyData = true;
}
BitStream::BitStream( char* _data, unsigned int lengthInBytes, bool _copyData )
{
numberOfBitsUsed = lengthInBytes << 3;
readOffset = 0;
copyData = _copyData;
numberOfBitsAllocated = lengthInBytes << 3;
if ( copyData )
{
if ( lengthInBytes > 0 )
{
if (lengthInBytes < BITSTREAM_STACK_ALLOCATION_SIZE)
{
data = ( unsigned char* ) stackData;
numberOfBitsAllocated = BITSTREAM_STACK_ALLOCATION_SIZE << 3;
}
else
{
data = ( unsigned char* ) malloc( lengthInBytes );
}
#ifdef _DEBUG
assert( data );
#endif
memcpy( data, _data, lengthInBytes );
}
else
data = 0;
}
else
data = ( unsigned char* ) _data;
}
// Use this if you pass a pointer copy to the constructor (_copyData==false) and want to overallocate to prevent reallocation
void BitStream::SetNumberOfBitsAllocated( const unsigned int lengthInBits )
{
#ifdef _DEBUG
assert( lengthInBits >= ( unsigned int ) numberOfBitsAllocated );
#endif
numberOfBitsAllocated = lengthInBits;
}
BitStream::~BitStream()
{
if ( copyData && numberOfBitsAllocated > BITSTREAM_STACK_ALLOCATION_SIZE << 3)
free( data ); // Use realloc and free so we are more efficient than delete and new for resizing
}
void BitStream::Reset( void )
{
// Note: Do NOT reallocate memory because BitStream is used
// in places to serialize/deserialize a buffer. Reallocation
// is a dangerous operation (may result in leaks).
if ( numberOfBitsUsed > 0 )
{
// memset(data, 0, BITS_TO_BYTES(numberOfBitsUsed));
}
// Don't free memory here for speed efficiency
//free(data); // Use realloc and free so we are more efficient than delete and new for resizing
numberOfBitsUsed = 0;
//numberOfBitsAllocated=8;
readOffset = 0;
//data=(unsigned char*)malloc(1);
// if (numberOfBitsAllocated>0)
// memset(data, 0, BITS_TO_BYTES(numberOfBitsAllocated));
}
// Write the native types to the end of the buffer
void BitStream::Write( const bool input )
{
#ifdef TYPE_CHECKING
unsigned char ID = 0;
WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8, true );
#endif
if ( input )
Write1();
else
Write0();
}
void BitStream::Write( const unsigned char input )
{
#ifdef TYPE_CHECKING
unsigned char ID = 1;
WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8, true );
#endif
WriteBits( ( unsigned char* ) & input, sizeof( input ) * 8, true );
}
void BitStream::Write( const char input )
{
#ifdef TYPE_CHECKING
unsigned char ID = 2;
WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8, true );
#endif
WriteBits( ( unsigned char* ) & input, sizeof( input ) * 8, true );
}
void BitStream::Write( const unsigned short input )
{
#ifdef TYPE_CHECKING
unsigned char ID = 3;
WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8, true );
#endif
#ifdef __BITSTREAM_NATIVE_END
WriteBits( ( unsigned char* ) & input, sizeof( input ) * 8, true );
#else
static unsigned char uint16w[2];
uint16w[B16_1] = (input >> 8)&(0xff);
uint16w[B16_0] = input&(0xff);
WriteBits( uint16w, sizeof( input ) * 8, true );
#endif
}
void BitStream::Write( const short input )
{
#ifdef TYPE_CHECKING
unsigned char ID = 4;
WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8, true );
#endif
#ifdef __BITSTREAM_NATIVE_END
WriteBits( ( unsigned char* ) & input, sizeof( input ) * 8, true );
#else
static unsigned char int16w[2];
int16w[B16_1] = (input >> 8)&(0xff);
int16w[B16_0] = input&(0xff);
WriteBits( int16w, sizeof( input ) * 8, true );
#endif
}
void BitStream::Write( const unsigned int input )
{
#ifdef TYPE_CHECKING
unsigned char ID = 5;
WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8, true );
#endif
#ifdef __BITSTREAM_NATIVE_END
WriteBits( ( unsigned char* ) & input, sizeof( input ) * 8, true );
#else
static unsigned char uint32w[4];
uint32w[B32_3] = (input >> 24)&(0x000000ff);
uint32w[B32_2] = (input >> 16)&(0x000000ff);
uint32w[B32_1] = (input >> 8)&(0x000000ff);
uint32w[B32_0] = (input)&(0x000000ff);
WriteBits( uint32w, sizeof( input ) * 8, true );
#endif
}
void BitStream::Write( const int input )
{
#ifdef TYPE_CHECKING
unsigned char ID = 6;
WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8, true );
#endif
#ifdef __BITSTREAM_NATIVE_END
WriteBits( ( unsigned char* ) & input, sizeof( input ) * 8, true );
#else
static unsigned char int32w[4];
int32w[B32_3] = (input >> 24)&(0x000000ff);
int32w[B32_2] = (input >> 16)&(0x000000ff);
int32w[B32_1] = (input >> 8)&(0x000000ff);
int32w[B32_0] = (input)&(0x000000ff);
WriteBits( int32w, sizeof( input ) * 8, true );
#endif
}
// This doesn't compile on windows. Find another way to output the warning
#if defined ( __APPLE__ ) || defined ( __APPLE_CC__ )|| defined ( _WIN32 )
//#warning Do NOT use 'long' for network data - it is not cross-compiler nor 32/64-bit safe
void BitStream::Write( const unsigned long input )
{
printf("*** WARNING: Do not use 'long' to declare network data.\n");
printf("*** It is not safe betwen compilers or between 32/64-bit systems.\n");
Write( (const unsigned int) input );
}
void BitStream::Write( const long input )
{
printf("*** WARNING: Do not use 'long' to declare network data.\n");
printf("*** It is not safe betwen compilers or between 32/64-bit systems.\n");
Write( (const int) input );
}
#endif
#ifdef HAS_INT64
void BitStream::Write( const uint64_t input )
{
#ifdef TYPE_CHECKING
unsigned char ID = 7;
WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8, true );
#endif
#ifdef __BITSTREAM_NATIVE_END
WriteBits( ( unsigned char* ) & input, sizeof( input ) * 8, true );
#else
static unsigned char uint64w[8];
uint64w[B64_7] = (input >> 56) & 0xff;
uint64w[B64_6] = (input >> 48) & 0xff;
uint64w[B64_5] = (input >> 40) & 0xff;
uint64w[B64_4] = (input >> 32) & 0xff;
uint64w[B64_3] = (input >> 24) & 0xff;
uint64w[B64_2] = (input >> 16) & 0xff;
uint64w[B64_1] = (input >> 8) & 0xff;
uint64w[B64_0] = input & 0xff;
WriteBits( uint64w, sizeof( input ) * 8, true );
#endif
}
void BitStream::Write( const int64_t input )
{
#ifdef TYPE_CHECKING
unsigned char ID = 8;
WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8, true );
#endif
#ifdef __BITSTREAM_NATIVE_END
WriteBits( ( unsigned char* ) & input, sizeof( input ) * 8, true );
#else
static unsigned char int64w[8];
int64w[B64_7] = (input >> 56) & 0xff;
int64w[B64_6] = (input >> 48) & 0xff;
int64w[B64_5] = (input >> 40) & 0xff;
int64w[B64_4] = (input >> 32) & 0xff;
int64w[B64_3] = (input >> 24) & 0xff;
int64w[B64_2] = (input >> 16) & 0xff;
int64w[B64_1] = (input >> 8) & 0xff;
int64w[B64_0] = input & 0xff;
WriteBits( int64w, sizeof( input ) * 8, true );
#endif
}
#endif
void BitStream::Write( const float input )
{
#ifdef TYPE_CHECKING
unsigned char ID = 9;
WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8, true );
#endif
#ifndef __BITSTREAM_NATIVE_END
unsigned int intval = *((unsigned int *)(&input));
Write(intval);
#else
WriteBits( ( unsigned char* ) & input, sizeof( input ) * 8, true );
#endif
}
void BitStream::Write( const double input )
{
#ifdef TYPE_CHECKING
unsigned char ID = 10;
WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8, true );
#endif
#if defined ( __BITSTREAM_NATIVE_END ) || ( ! defined (HAS_INT64) )
WriteBits( ( unsigned char* ) & input, sizeof( input ) * 8, true );
#else
uint64_t intval = *((uint64_t *)(&input));
Write(intval);
#endif
}
// Write an array or casted stream
void BitStream::Write( const char* input, const int numberOfBytes )
{
#ifdef TYPE_CHECKING
unsigned char ID = 11;
WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8, true );
WriteBits( ( unsigned char* ) & numberOfBytes, sizeof( int ) * 8, true );
#endif
WriteBits( ( unsigned char* ) input, numberOfBytes * 8, true );
}
void BitStream::Write( const BitStream *bitStream )
{
WriteBits(bitStream->GetData(), bitStream->GetNumberOfBitsUsed(), false);
}
void BitStream::Write( const NetworkID networkId )
{
Write(networkId.playerId.binaryAddress);
Write(networkId.playerId.port);
Write(networkId.localSystemId);
}
// Write the native types with simple compression.
// Best used with negatives and positives close to 0
void BitStream::WriteCompressed( const unsigned char input )
{
#ifdef TYPE_CHECKING
unsigned char ID = 12;
WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8, true );
#endif
WriteCompressed( ( unsigned char* ) & input, sizeof( input ) * 8, true );
}
void BitStream::WriteCompressed( const char input )
{
#ifdef TYPE_CHECKING
unsigned char ID = 13;
WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8, true );
#endif
WriteCompressed( ( unsigned char* ) & input, sizeof( input ) * 8, false );
}
void BitStream::WriteCompressed( const unsigned short input )
{
#ifdef TYPE_CHECKING
unsigned char ID = 14;
WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8, true );
#endif
#ifdef __BITSTREAM_NATIVE_END
WriteCompressed( ( unsigned char* ) & input, sizeof( input ) * 8, true );
#else
static unsigned char uint16wc[2];
uint16wc[B16_1] = (input >> 8)&(0xff);
uint16wc[B16_0] = input&(0xff);
WriteCompressed( uint16wc, sizeof( input ) * 8, true );
#endif
}
void BitStream::WriteCompressed( const short input )
{
#ifdef TYPE_CHECKING
unsigned char ID = 15;
WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8, true );
#endif
#ifdef __BITSTREAM_NATIVE_END
WriteCompressed( ( unsigned char* ) & input, sizeof( input ) * 8, true );
#else
static unsigned char int16wc[2];
int16wc[B16_1] = (input >> 8)&(0xff);
int16wc[B16_0] = input&(0xff);
WriteCompressed( int16wc, sizeof( input ) * 8, false );
#endif
}
void BitStream::WriteCompressed( const unsigned int input )
{
#ifdef TYPE_CHECKING
unsigned char ID = 16;
WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8, true );
#endif
#ifdef __BITSTREAM_NATIVE_END
WriteCompressed( ( unsigned char* ) & input, sizeof( input ) * 8, true );
#else
static unsigned char uint32wc[4];
uint32wc[B32_3] = (input >> 24)&(0x000000ff);
uint32wc[B32_2] = (input >> 16)&(0x000000ff);
uint32wc[B32_1] = (input >> 8)&(0x000000ff);
uint32wc[B32_0] = (input)&(0x000000ff);
WriteCompressed( uint32wc, sizeof( input ) * 8, true );
#endif
}
void BitStream::WriteCompressed( const int input )
{
#ifdef TYPE_CHECKING
unsigned char ID = 17;
WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8, true );
#endif
#ifdef __BITSTREAM_NATIVE_END
WriteCompressed( ( unsigned char* ) & input, sizeof( input ) * 8, true );
#else
static unsigned char int32wc[4];
int32wc[B32_3] = (input >> 24)&(0x000000ff);
int32wc[B32_2] = (input >> 16)&(0x000000ff);
int32wc[B32_1] = (input >> 8)&(0x000000ff);
int32wc[B32_0] = (input)&(0x000000ff);
WriteCompressed( int32wc, sizeof( input ) * 8, false );
#endif
}
// Doesn't work on windows. Find another way to output the warning
#if defined ( __APPLE__ ) || defined ( __APPLE_CC__ )|| defined ( _WIN32 )
void BitStream::WriteCompressed( const unsigned long input )
{
printf("*** WARNING: Do not use 'long' to declare network data.\n");
printf("*** It is not safe betwen compilers or between 32/64-bit systems.\n");
WriteCompressed( (const unsigned int) input );
}
void BitStream::WriteCompressed( const long input )
{
printf("*** WARNING: Do not use 'long' to declare network data.\n");
printf("*** It is not safe betwen compilers or between 32/64-bit systems.\n");
WriteCompressed( (const int) input );
}
#endif
#ifdef HAS_INT64
void BitStream::WriteCompressed( const uint64_t input )
{
#ifdef TYPE_CHECKING
unsigned char ID = 18;
WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8, true );
#endif
#ifdef __BITSTREAM_NATIVE_END
WriteCompressed( ( unsigned char* ) & input, sizeof( input ) * 8, true );
#else
static unsigned char uint64wc[8];
uint64wc[B64_7] = (input >> 56) & 0xff;
uint64wc[B64_6] = (input >> 48) & 0xff;
uint64wc[B64_5] = (input >> 40) & 0xff;
uint64wc[B64_4] = (input >> 32) & 0xff;
uint64wc[B64_3] = (input >> 24) & 0xff;
uint64wc[B64_2] = (input >> 16) & 0xff;
uint64wc[B64_1] = (input >> 8) & 0xff;
uint64wc[B64_0] = input & 0xff;
WriteCompressed( uint64wc, sizeof( input ) * 8, true );
#endif
}
void BitStream::WriteCompressed( const int64_t input )
{
#ifdef TYPE_CHECKING
unsigned char ID = 19;
WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8, true );
#endif
#ifdef __BITSTREAM_NATIVE_END
WriteCompressed( ( unsigned char* ) & input, sizeof( input ) * 8, true );
#else
static unsigned char int64wc[8];
int64wc[B64_7] = (input >> 56) & 0xff;
int64wc[B64_6] = (input >> 48) & 0xff;
int64wc[B64_5] = (input >> 40) & 0xff;
int64wc[B64_4] = (input >> 32) & 0xff;
int64wc[B64_3] = (input >> 24) & 0xff;
int64wc[B64_2] = (input >> 16) & 0xff;
int64wc[B64_1] = (input >> 8) & 0xff;
int64wc[B64_0] = input & 0xff;
WriteCompressed( int64wc, sizeof( input ) * 8, false );
#endif
}
#endif
void BitStream::WriteCompressed( const float input )
{
#ifdef TYPE_CHECKING
unsigned char ID = 20;
WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8, true );
#endif
// Not yet implemented (no compression)
#if defined ( __BITSTREAM_NATIVE_END )
WriteBits( ( unsigned char* ) &input, sizeof( input ) * 8, true );
#else
Write( input );
#endif
}
void BitStream::WriteNormVector( float x, float y, float z )
{
#ifdef _DEBUG
assert(x <= 1.01f && y <= 1.01f && z <= 1.01f && x >= -1.01f && y >= -1.01f && z >= -1.01f);
#endif
if (x>1.0f)
x=1.0f;
if (y>1.0f)
y=1.0f;
if (z>1.0f)
z=1.0f;
if (x<-1.0f)
x=-1.0f;
if (y<-1.0f)
y=-1.0f;
if (z<-1.0f)
z=-1.0f;
Write((bool) (x < 0.0f));
if (y==0.0f)
Write(true);
else
{
Write(false);
Write((unsigned short)((y+1.0f)*32767.5f));
}
if (z==0.0f)
Write(true);
else
{
Write(false);
Write((unsigned short)((z+1.0f)*32767.5f));
}
}
void BitStream::WriteVector( float x, float y, float z )
{
float magnitude = sqrtf(x * x + y * y + z * z);
Write(magnitude);
if (magnitude > 0.0f)
{
Write((unsigned short)((x/magnitude+1.0f)*32767.5f));
Write((unsigned short)((y/magnitude+1.0f)*32767.5f));
Write((unsigned short)((z/magnitude+1.0f)*32767.5f));
}
}
void BitStream::WriteNormQuat( float w, float x, float y, float z)
{
Write((bool)(w<0.0f));
Write((bool)(x<0.0f));
Write((bool)(y<0.0f));
Write((bool)(z<0.0f));
Write((unsigned short)(fabs(x)*65535.0));
Write((unsigned short)(fabs(y)*65535.0));
Write((unsigned short)(fabs(z)*65535.0));
// Leave out w and calcuate it on the target
}
void BitStream::WriteOrthMatrix(
float m00, float m01, float m02,
float m10, float m11, float m12,
float m20, float m21, float m22 )
{
double qw;
double qx;
double qy;
double qz;
// Convert matrix to quat
// http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/
qw = sqrt( 1 + m00 + m11 + m22 ) / 2;
qx = sqrt( 1 + m00 - m11 - m22 ) / 2;
qy = sqrt( 1 - m00 + m11 - m22 ) / 2;
qz = sqrt( 1 - m00 - m11 + m22 ) / 2;
if (qw < 0.0) qw=0.0;
if (qx < 0.0) qx=0.0;
if (qy < 0.0) qy=0.0;
if (qz < 0.0) qz=0.0;
qx = _copysign( qx, m21 - m12 );
qy = _copysign( qy, m02 - m20 );
qz = _copysign( qz, m20 - m02 );
WriteNormQuat((float)qw,(float)qx,(float)qy,(float)qz);
}
void BitStream::WriteCompressed( const double input )
{
#ifdef TYPE_CHECKING
unsigned char ID = 21;
WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8, true );
#endif
// Not yet implemented (no compression)
#if defined ( __BITSTREAM_NATIVE_END )
WriteBits( ( unsigned char* ) & input, sizeof( input ) * 8, true );
#else
Write( input );
#endif
}
// Read the native types from the front of the buffer
// Write the native types to the end of the buffer
bool BitStream::Read( bool& output )
{
#ifdef TYPE_CHECKING
unsigned char ID;
if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8 ) == false )
return false;
#ifdef _DEBUG
assert( ID == 0 );
#endif
#endif
//assert(readOffset+1 <=numberOfBitsUsed); // If this assert is hit the stream wasn't long enough to read from
if ( readOffset + 1 > numberOfBitsUsed )
return false;
//if (ReadBit()) // Check that bit
if ( data[ readOffset >> 3 ] & ( 0x80 >> ( readOffset++ % 8 ) ) ) // Is it faster to just write it out here?
output = true;
else
output = false;
return true;
}
bool BitStream::Read( unsigned char &output )
{
#ifdef TYPE_CHECKING
unsigned char ID;
if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8 ) == false )
return false;
assert( ID == 1 );
#endif
return ReadBits( ( unsigned char* ) & output, sizeof( output ) * 8 );
}
bool BitStream::Read( char &output )
{
#ifdef TYPE_CHECKING
unsigned char ID;
if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8 ) == false )
return false;
assert( ID == 2 );
#endif
return ReadBits( ( unsigned char* ) & output, sizeof( output ) * 8 );
}
bool BitStream::Read( unsigned short &output )
{
#ifdef TYPE_CHECKING
unsigned char ID;
if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8 ) == false )
return false;
assert( ID == 3 );
#endif
#ifdef __BITSTREAM_NATIVE_END
return ReadBits( ( unsigned char* ) & output, sizeof( output ) * 8 );
#else
static unsigned char uint16r[2];
if (ReadBits( uint16r, sizeof( output ) * 8 ) != true) return false;
output = (((unsigned short) uint16r[B16_1])<<8)|((unsigned short)uint16r[B16_0]);
return true;
#endif
}
bool BitStream::Read( short &output )
{
#ifdef TYPE_CHECKING
unsigned char ID;
if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8 ) == false )
return false;
assert( ID == 4 );
#endif
#ifdef __BITSTREAM_NATIVE_END
return ReadBits( ( unsigned char* ) & output, sizeof( output ) * 8 );
#else
static unsigned char int16r[2];
if (ReadBits( int16r, sizeof( output ) * 8 ) != true) return false;
output = (((unsigned short) int16r[B16_1])<<8)|((unsigned short)int16r[B16_0]);
return true;
#endif
}
bool BitStream::Read( unsigned int &output )
{
#ifdef TYPE_CHECKING
unsigned char ID;
if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8 ) == false )
return false;
assert( ID == 5 );
#endif
#ifdef __BITSTREAM_NATIVE_END
return ReadBits( ( unsigned char* ) & output, sizeof( output ) * 8 );
#else
static unsigned char uint32r[4];
if(ReadBits( uint32r, sizeof( output ) * 8 ) != true)
return false;
output = (((unsigned int) uint32r[B32_3])<<24)|
(((unsigned int) uint32r[B32_2])<<16)|
(((unsigned int) uint32r[B32_1])<<8)|
((unsigned int) uint32r[B32_0]);
return true;
#endif
}
bool BitStream::Read( int &output )
{
#ifdef TYPE_CHECKING
unsigned char ID;
if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8 ) == false )
return false;
assert( ID == 6 );
#endif
#ifdef __BITSTREAM_NATIVE_END
return ReadBits( ( unsigned char* ) & output, sizeof( output ) * 8 );
#else
static unsigned char int32r[4];
if(ReadBits( int32r, sizeof( output ) * 8 ) != true)
return false;
output = (((unsigned int) int32r[B32_3])<<24)|
(((unsigned int) int32r[B32_2])<<16)|
(((unsigned int) int32r[B32_1])<<8)|
((unsigned int) int32r[B32_0]);
return true;
#endif
}
// This doesn't compile on windows. Find another way to output the warning
#if defined ( __APPLE__ ) || defined ( __APPLE_CC__ )|| defined ( _WIN32 )
//#warning Do NOT use 'long' for network data - it is not cross-compiler nor 32/64-bit safe
bool BitStream::Read( unsigned long &output )
{
printf("*** WARNING: Do not use 'long' to declare network data.\n");
printf("*** It is not safe betwen compilers or between 32/64-bit systems.\n");
unsigned int tmp;
if ( !Read(tmp) ) return false;
output = tmp;
return true;
}
bool BitStream::Read( long &output )
{
printf("*** WARNING: Do not use 'long' to declare network data.\n");
printf("*** It is not safe betwen compilers or between 32/64-bit systems.\n");
int tmp;
if ( !Read(tmp) ) return false;
output = tmp;
return true;
}
#endif
#ifdef HAS_INT64
bool BitStream::Read( uint64_t &output )
{
#ifdef TYPE_CHECKING
unsigned char ID;
if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8 ) == false )
return false;
assert( ID == 7 );
#endif
#ifdef __BITSTREAM_NATIVE_END
return ReadBits( ( unsigned char* ) & output, sizeof( output ) * 8 );
#else
static unsigned char uint64r[8];
if(ReadBits( uint64r, sizeof( output ) * 8 ) != true)
return false;
output = (((uint64_t) uint64r[B64_7])<<56)|(((uint64_t) uint64r[B64_6])<<48)|
(((uint64_t) uint64r[B64_5])<<40)|(((uint64_t) uint64r[B64_4])<<32)|
(((uint64_t) uint64r[B64_3])<<24)|(((uint64_t) uint64r[B64_2])<<16)|
(((uint64_t) uint64r[B64_1])<<8)|((uint64_t) uint64r[B64_0]);
return true;
#endif
}
bool BitStream::Read( int64_t &output )
{
#ifdef TYPE_CHECKING
unsigned char ID;
if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8 ) == false )
return false;
assert( ID == 8 );
#endif
#ifdef __BITSTREAM_NATIVE_END
return ReadBits( ( unsigned char* ) & output, sizeof( output ) * 8 );
#else
static unsigned char int64r[8];
if(ReadBits( int64r, sizeof( output ) * 8 ) != true)
return false;
output = (((uint64_t) int64r[B64_7])<<56)|(((uint64_t) int64r[B64_6])<<48)|
(((uint64_t) int64r[B64_5])<<40)|(((uint64_t) int64r[B64_4])<<32)|
(((uint64_t) int64r[B64_3])<<24)|(((uint64_t) int64r[B64_2])<<16)|
(((uint64_t) int64r[B64_1])<<8)|((uint64_t) int64r[B64_0]);
return true;
#endif
}
#endif
bool BitStream::Read( float &output )
{
#ifdef TYPE_CHECKING
unsigned char ID;
if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8 ) == false )
return false;
assert( ID == 9 );
#endif
#ifdef __BITSTREAM_NATIVE_END
return ReadBits( ( unsigned char* ) & output, sizeof( output ) * 8 );
#else
unsigned int val;
if (Read(val) == false) return false;
output = *((float *)(&val));
return true;
#endif
}
bool BitStream::Read( double &output )
{
#ifdef TYPE_CHECKING
unsigned char ID;
if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8 ) == false )
return false;
assert( ID == 10 );
#endif
#if defined ( __BITSTREAM_NATIVE_END ) || ( ! defined ( HAS_INT64 ) )
return ReadBits( ( unsigned char* ) & output, sizeof( output ) * 8 );
#else
uint64_t val;
if (Read(val) == false) return false;
output = *((double *)(&val));
return true;
#endif
}
// Read an array or casted stream
bool BitStream::Read( char* output, const int numberOfBytes )
{
#ifdef TYPE_CHECKING
unsigned char ID;
if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8 ) == false )
return false;
assert( ID == 11 );
int NOB;
ReadBits( ( unsigned char* ) & NOB, sizeof( int ) * 8 );
assert( NOB == numberOfBytes );
#endif
return ReadBits( ( unsigned char* ) output, numberOfBytes * 8 );
}
bool BitStream::Read( NetworkID &output)
{
Read(output.playerId.binaryAddress);
Read(output.playerId.port);
return Read(output.localSystemId);
}
// Read the types you wrote with WriteCompressed
bool BitStream::ReadCompressed( unsigned char & output )
{
#ifdef TYPE_CHECKING
unsigned char ID;
if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * 8 ) == false )
return false;
assert( ID == 12 );
#endif
return ReadCompressed( ( unsigned char* ) & output, sizeof( output ) * 8, true );
}
bool BitStream::ReadCompressed( char &output )
{
#ifdef TYPE_CHECKING