7
7
using MLAPI . Data ;
8
8
using MLAPI . NetworkingManagerComponents . Binary ;
9
9
using MLAPI . NetworkingManagerComponents . Core ;
10
+ using System . Collections ;
10
11
11
12
namespace MLAPI . MonoBehaviours . Core
12
13
{
@@ -440,13 +441,14 @@ private void OnDestroy()
440
441
internal List < SyncedVarField > syncedVarFields = new List < SyncedVarField > ( ) ;
441
442
private HashSet < uint > OutOfSyncClients = new HashSet < uint > ( ) ;
442
443
private bool syncVarInit = false ;
444
+ internal bool [ ] syncMask ;
443
445
internal void SyncVarInit ( )
444
446
{
445
447
if ( syncVarInit )
446
448
return ;
447
449
syncVarInit = true ;
448
450
FieldInfo [ ] sortedFields = GetType ( ) . GetFields ( BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . FlattenHierarchy | BindingFlags . Instance ) . OrderBy ( x => x . Name ) . ToArray ( ) ;
449
- for ( byte i = 0 ; i < sortedFields . Length ; i ++ )
451
+ for ( int i = 0 ; i < sortedFields . Length ; i ++ )
450
452
{
451
453
if ( sortedFields [ i ] . IsDefined ( typeof ( SyncedVar ) , true ) )
452
454
{
@@ -477,13 +479,10 @@ internal void SyncVarInit()
477
479
}
478
480
}
479
481
}
480
- if ( syncedVarFields . Count > 255 )
481
- {
482
- if ( LogHelper . CurrentLogLevel <= LogLevel . Normal ) LogHelper . LogError ( "MLAPI: You can not have more than 255 SyncVar's per NetworkedBehaviour!" ) ;
483
- }
482
+ syncMask = new bool [ syncedVarFields . Count ] ;
484
483
}
485
484
486
- internal void OnSyncVarUpdate ( object value , byte fieldIndex )
485
+ internal void OnSyncVarUpdate ( object value , int fieldIndex )
487
486
{
488
487
syncedVarFields [ fieldIndex ] . FieldInfo . SetValue ( this , value ) ;
489
488
if ( syncedVarFields [ fieldIndex ] . HookMethod != null )
@@ -509,14 +508,17 @@ internal void FlushToClient(uint clientId)
509
508
}
510
509
if ( syncCount == 0 )
511
510
return ;
512
- writer . WriteByte ( ( byte ) syncCount ) ;
511
+
513
512
writer . WriteUInt ( networkId ) ; //NetId
514
513
writer . WriteUShort ( networkedObject . GetOrderIndex ( this ) ) ; //Behaviour OrderIndex
515
- for ( byte i = 0 ; i < syncedVarFields . Count ; i ++ )
514
+
515
+ bool [ ] mask = GetDirtyMask ( false , clientId ) ;
516
+ for ( int i = 0 ; i < mask . Length ; i ++ ) writer . WriteBool ( mask [ i ] ) ;
517
+
518
+ for ( int i = 0 ; i < syncedVarFields . Count ; i ++ )
516
519
{
517
520
if ( syncedVarFields [ i ] . Target && clientId != ownerClientId )
518
521
continue ;
519
- writer . WriteByte ( i ) ; //FieldIndex
520
522
FieldTypeHelper . WriteFieldType ( writer , syncedVarFields [ i ] . FieldInfo , this , syncedVarFields [ i ] . FieldType ) ;
521
523
}
522
524
bool observed = InternalMessageHandler . Send ( clientId , "MLAPI_SYNC_VAR_UPDATE" , "MLAPI_INTERNAL" , writer , networkId ) ;
@@ -525,6 +527,16 @@ internal void FlushToClient(uint clientId)
525
527
}
526
528
}
527
529
530
+ private ref bool [ ] GetDirtyMask ( bool ignoreTarget , uint ? clientId = null )
531
+ {
532
+ for ( int i = 0 ; i < syncedVarFields . Count ; i ++ )
533
+ syncMask [ i ] = ( clientId == null && ignoreTarget && syncedVarFields [ i ] . Dirty && ! syncedVarFields [ i ] . Target ) ||
534
+ ( clientId == null && ! ignoreTarget && syncedVarFields [ i ] . Dirty ) ||
535
+ ( clientId != null && ! syncedVarFields [ i ] . Target ) ||
536
+ ( clientId != null && syncedVarFields [ i ] . Target && ownerClientId == clientId . Value ) ;
537
+ return ref syncMask ;
538
+ }
539
+
528
540
internal void SyncVarUpdate ( )
529
541
{
530
542
if ( ! syncVarInit )
@@ -533,10 +545,10 @@ internal void SyncVarUpdate()
533
545
if ( ! SetDirtyness ( ) )
534
546
return ;
535
547
536
- byte nonTargetDirtyCount = 0 ;
537
- byte totalDirtyCount = 0 ;
538
- byte dirtyTargets = 0 ;
539
- for ( byte i = 0 ; i < syncedVarFields . Count ; i ++ )
548
+ int nonTargetDirtyCount = 0 ;
549
+ int totalDirtyCount = 0 ;
550
+ int dirtyTargets = 0 ;
551
+ for ( int i = 0 ; i < syncedVarFields . Count ; i ++ )
540
552
{
541
553
if ( syncedVarFields [ i ] . Dirty )
542
554
totalDirtyCount ++ ;
@@ -557,15 +569,17 @@ internal void SyncVarUpdate()
557
569
using ( BitWriter writer = BitWriter . Get ( ) )
558
570
{
559
571
//Write all indexes
560
- writer . WriteByte ( totalDirtyCount ) ;
561
572
writer . WriteUInt ( networkId ) ; //NetId
562
573
writer . WriteUShort ( networkedObject . GetOrderIndex ( this ) ) ; //Behaviour OrderIndex
563
- for ( byte i = 0 ; i < syncedVarFields . Count ; i ++ )
574
+
575
+ bool [ ] mask = GetDirtyMask ( false ) ;
576
+ for ( int i = 0 ; i < mask . Length ; i ++ ) writer . WriteBool ( mask [ i ] ) ;
577
+
578
+ for ( int i = 0 ; i < syncedVarFields . Count ; i ++ )
564
579
{
565
580
//Writes all the indexes of the dirty syncvars.
566
581
if ( syncedVarFields [ i ] . Dirty == true )
567
582
{
568
- writer . WriteByte ( i ) ; //FieldIndex
569
583
FieldTypeHelper . WriteFieldType ( writer , syncedVarFields [ i ] . FieldInfo , this , syncedVarFields [ i ] . FieldType ) ;
570
584
syncedVarFields [ i ] . FieldValue = syncedVarFields [ i ] . FieldInfo . GetValue ( this ) ;
571
585
syncedVarFields [ i ] . Dirty = false ;
@@ -587,15 +601,17 @@ internal void SyncVarUpdate()
587
601
using ( BitWriter writer = BitWriter . Get ( ) )
588
602
{
589
603
//Write all indexes
590
- writer . WriteByte ( totalDirtyCount ) ;
591
604
writer . WriteUInt ( networkId ) ; //NetId
592
605
writer . WriteUShort ( networkedObject . GetOrderIndex ( this ) ) ; //Behaviour OrderIndex
593
- for ( byte i = 0 ; i < syncedVarFields . Count ; i ++ )
606
+
607
+ bool [ ] mask = GetDirtyMask ( false ) ;
608
+ for ( int i = 0 ; i < mask . Length ; i ++ ) writer . WriteBool ( mask [ i ] ) ;
609
+
610
+ for ( int i = 0 ; i < syncedVarFields . Count ; i ++ )
594
611
{
595
612
//Writes all the indexes of the dirty syncvars.
596
613
if ( syncedVarFields [ i ] . Dirty == true )
597
614
{
598
- writer . WriteByte ( i ) ; //FieldIndex
599
615
FieldTypeHelper . WriteFieldType ( writer , syncedVarFields [ i ] . FieldInfo , this , syncedVarFields [ i ] . FieldType ) ;
600
616
if ( nonTargetDirtyCount == 0 )
601
617
{
@@ -618,15 +634,17 @@ internal void SyncVarUpdate()
618
634
using ( BitWriter writer = BitWriter . Get ( ) )
619
635
{
620
636
//Write all indexes
621
- writer . WriteByte ( nonTargetDirtyCount ) ;
622
637
writer . WriteUInt ( networkId ) ; //NetId
623
638
writer . WriteUShort ( networkedObject . GetOrderIndex ( this ) ) ; //Behaviour OrderIndex
624
- for ( byte i = 0 ; i < syncedVarFields . Count ; i ++ )
639
+
640
+ bool [ ] mask = GetDirtyMask ( true ) ;
641
+ for ( int i = 0 ; i < mask . Length ; i ++ ) writer . WriteBool ( mask [ i ] ) ;
642
+
643
+ for ( int i = 0 ; i < syncedVarFields . Count ; i ++ )
625
644
{
626
645
//Writes all the indexes of the dirty syncvars.
627
646
if ( syncedVarFields [ i ] . Dirty == true && ! syncedVarFields [ i ] . Target )
628
647
{
629
- writer . WriteByte ( i ) ; //FieldIndex
630
648
FieldTypeHelper . WriteFieldType ( writer , syncedVarFields [ i ] . FieldInfo , this , syncedVarFields [ i ] . FieldType ) ;
631
649
syncedVarFields [ i ] . FieldValue = syncedVarFields [ i ] . FieldInfo . GetValue ( this ) ;
632
650
syncedVarFields [ i ] . Dirty = false ;
0 commit comments