77using MLAPI . Data ;
88using MLAPI . NetworkingManagerComponents . Binary ;
99using MLAPI . NetworkingManagerComponents . Core ;
10+ using System . Collections ;
1011
1112namespace MLAPI . MonoBehaviours . Core
1213{
@@ -440,13 +441,14 @@ private void OnDestroy()
440441 internal List < SyncedVarField > syncedVarFields = new List < SyncedVarField > ( ) ;
441442 private HashSet < uint > OutOfSyncClients = new HashSet < uint > ( ) ;
442443 private bool syncVarInit = false ;
444+ internal bool [ ] syncMask ;
443445 internal void SyncVarInit ( )
444446 {
445447 if ( syncVarInit )
446448 return ;
447449 syncVarInit = true ;
448450 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 ++ )
450452 {
451453 if ( sortedFields [ i ] . IsDefined ( typeof ( SyncedVar ) , true ) )
452454 {
@@ -477,13 +479,10 @@ internal void SyncVarInit()
477479 }
478480 }
479481 }
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 ] ;
484483 }
485484
486- internal void OnSyncVarUpdate ( object value , byte fieldIndex )
485+ internal void OnSyncVarUpdate ( object value , int fieldIndex )
487486 {
488487 syncedVarFields [ fieldIndex ] . FieldInfo . SetValue ( this , value ) ;
489488 if ( syncedVarFields [ fieldIndex ] . HookMethod != null )
@@ -509,14 +508,17 @@ internal void FlushToClient(uint clientId)
509508 }
510509 if ( syncCount == 0 )
511510 return ;
512- writer . WriteByte ( ( byte ) syncCount ) ;
511+
513512 writer . WriteUInt ( networkId ) ; //NetId
514513 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 ++ )
516519 {
517520 if ( syncedVarFields [ i ] . Target && clientId != ownerClientId )
518521 continue ;
519- writer . WriteByte ( i ) ; //FieldIndex
520522 FieldTypeHelper . WriteFieldType ( writer , syncedVarFields [ i ] . FieldInfo , this , syncedVarFields [ i ] . FieldType ) ;
521523 }
522524 bool observed = InternalMessageHandler . Send ( clientId , "MLAPI_SYNC_VAR_UPDATE" , "MLAPI_INTERNAL" , writer , networkId ) ;
@@ -525,6 +527,16 @@ internal void FlushToClient(uint clientId)
525527 }
526528 }
527529
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+
528540 internal void SyncVarUpdate ( )
529541 {
530542 if ( ! syncVarInit )
@@ -533,10 +545,10 @@ internal void SyncVarUpdate()
533545 if ( ! SetDirtyness ( ) )
534546 return ;
535547
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 ++ )
540552 {
541553 if ( syncedVarFields [ i ] . Dirty )
542554 totalDirtyCount ++ ;
@@ -557,15 +569,17 @@ internal void SyncVarUpdate()
557569 using ( BitWriter writer = BitWriter . Get ( ) )
558570 {
559571 //Write all indexes
560- writer . WriteByte ( totalDirtyCount ) ;
561572 writer . WriteUInt ( networkId ) ; //NetId
562573 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 ++ )
564579 {
565580 //Writes all the indexes of the dirty syncvars.
566581 if ( syncedVarFields [ i ] . Dirty == true )
567582 {
568- writer . WriteByte ( i ) ; //FieldIndex
569583 FieldTypeHelper . WriteFieldType ( writer , syncedVarFields [ i ] . FieldInfo , this , syncedVarFields [ i ] . FieldType ) ;
570584 syncedVarFields [ i ] . FieldValue = syncedVarFields [ i ] . FieldInfo . GetValue ( this ) ;
571585 syncedVarFields [ i ] . Dirty = false ;
@@ -587,15 +601,17 @@ internal void SyncVarUpdate()
587601 using ( BitWriter writer = BitWriter . Get ( ) )
588602 {
589603 //Write all indexes
590- writer . WriteByte ( totalDirtyCount ) ;
591604 writer . WriteUInt ( networkId ) ; //NetId
592605 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 ++ )
594611 {
595612 //Writes all the indexes of the dirty syncvars.
596613 if ( syncedVarFields [ i ] . Dirty == true )
597614 {
598- writer . WriteByte ( i ) ; //FieldIndex
599615 FieldTypeHelper . WriteFieldType ( writer , syncedVarFields [ i ] . FieldInfo , this , syncedVarFields [ i ] . FieldType ) ;
600616 if ( nonTargetDirtyCount == 0 )
601617 {
@@ -618,15 +634,17 @@ internal void SyncVarUpdate()
618634 using ( BitWriter writer = BitWriter . Get ( ) )
619635 {
620636 //Write all indexes
621- writer . WriteByte ( nonTargetDirtyCount ) ;
622637 writer . WriteUInt ( networkId ) ; //NetId
623638 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 ++ )
625644 {
626645 //Writes all the indexes of the dirty syncvars.
627646 if ( syncedVarFields [ i ] . Dirty == true && ! syncedVarFields [ i ] . Target )
628647 {
629- writer . WriteByte ( i ) ; //FieldIndex
630648 FieldTypeHelper . WriteFieldType ( writer , syncedVarFields [ i ] . FieldInfo , this , syncedVarFields [ i ] . FieldType ) ;
631649 syncedVarFields [ i ] . FieldValue = syncedVarFields [ i ] . FieldInfo . GetValue ( this ) ;
632650 syncedVarFields [ i ] . Dirty = false ;
0 commit comments