@@ -6,7 +6,7 @@ use self::{
66} ;
77use crate :: prelude:: * ;
88use bones_matchmaker_proto:: { MATCH_ALPN , PLAY_ALPN } ;
9- use desync:: DesyncDebugHistoryBuffer ;
9+ use desync:: { DesyncDebugHistoryBuffer , DetectDesyncs } ;
1010use fxhash:: FxHasher ;
1111use ggrs:: { DesyncDetection , P2PSession } ;
1212use instant:: Duration ;
@@ -59,7 +59,9 @@ impl From<ggrs::InputStatus> for NetworkInputStatus {
5959
6060/// Module prelude.
6161pub mod prelude {
62- pub use super :: { input, lan, online, proto, DisconnectedPlayers , SyncingInfo , RUNTIME } ;
62+ pub use super :: {
63+ desync:: DetectDesyncs , input, lan, online, proto, DisconnectedPlayers , SyncingInfo , RUNTIME ,
64+ } ;
6365
6466 #[ cfg( feature = "net-debug" ) ]
6567 pub use super :: debug:: prelude:: * ;
@@ -523,20 +525,12 @@ pub struct GgrsSessionRunner<'a, InputTypes: NetworkInputConfig<'a>> {
523525 /// Local input delay ggrs session was initialized with
524526 local_input_delay : usize ,
525527
526- /// Interval in frames of how often to hash state and check for desync with other clients.
527- /// i.e if set to 10, will check every 10th frame. Desync detection disabled if None.
528- pub detect_desyncs : Option < u32 > ,
528+ /// When provided, desync detection is enabled. Contains settings for desync detection.
529+ detect_desyncs : Option < DetectDesyncs > ,
529530
530531 /// History buffer for desync debug data to fetch it upon detected desyncs.
531532 /// [`DefaultDesyncTree`] will be generated and saved here if feature `desync-debug` is enabled.
532533 pub desync_debug_history : Option < DesyncDebugHistoryBuffer < DefaultDesyncTree > > ,
533-
534- /// Override of hash function used to hash world for desync detection.
535- /// By default, [`World`]'s [`DesyncHash`] impl is used.
536- ///
537- /// This may be useful if you want to hash only a subset of components or resources
538- /// during testing.
539- pub world_hash_func : Option < fn ( & World ) -> u64 > ,
540534}
541535
542536/// The info required to create a [`GgrsSessionRunner`].
@@ -560,13 +554,8 @@ pub struct GgrsSessionRunnerInfo {
560554 /// `None` will use Bone's default.
561555 pub local_input_delay : Option < usize > ,
562556
563- /// Interval in frames of how often to hash state and check for desync with other clients.
564- /// i.e if set to 10, will check every 10th frame. Desync detection disabled if None.
565- pub detect_desyncs : Option < u32 > ,
566-
567- /// Override of hash function used to hash world for desync detection.
568- /// By default, [`World`]'s [`DesyncHash`] impl is used.
569- pub world_hash_func : Option < fn ( & World ) -> u64 > ,
557+ /// When provided, desync detection is enabled. Contains settings for desync detection.
558+ pub detect_desyncs : Option < DetectDesyncs > ,
570559}
571560
572561impl GgrsSessionRunnerInfo {
@@ -575,8 +564,7 @@ impl GgrsSessionRunnerInfo {
575564 socket : Socket ,
576565 max_prediction_window : Option < usize > ,
577566 local_input_delay : Option < usize > ,
578- world_hash_func : Option < fn ( & World ) -> u64 > ,
579- detect_desyncs : Option < u32 > ,
567+ detect_desyncs : Option < DetectDesyncs > ,
580568 ) -> Self {
581569 let player_idx = socket. player_idx ( ) ;
582570 let player_count = socket. player_count ( ) ;
@@ -587,7 +575,6 @@ impl GgrsSessionRunnerInfo {
587575 max_prediction_window,
588576 local_input_delay,
589577 detect_desyncs,
590- world_hash_func,
591578 }
592579 }
593580}
@@ -625,8 +612,10 @@ where
625612 . try_send ( NetworkDebugMessage :: SetMaxPrediction ( max_prediction) )
626613 . unwrap ( ) ;
627614
628- let desync_detection = match info. detect_desyncs {
629- Some ( interval) => DesyncDetection :: On { interval } ,
615+ let desync_detection = match info. detect_desyncs . as_ref ( ) {
616+ Some ( config) => DesyncDetection :: On {
617+ interval : config. detection_interval ,
618+ } ,
630619 None => DesyncDetection :: Off ,
631620 } ;
632621
@@ -655,9 +644,13 @@ where
655644 let session = builder. start_p2p_session ( info. socket . clone ( ) ) . unwrap ( ) ;
656645
657646 #[ cfg( feature = "desync-debug" ) ]
658- let desync_debug_history = info
659- . detect_desyncs
660- . map ( DesyncDebugHistoryBuffer :: < DefaultDesyncTree > :: new) ;
647+ let desync_debug_history = if let Some ( detect_desync) = info. detect_desyncs . as_ref ( ) {
648+ Some ( DesyncDebugHistoryBuffer :: < DefaultDesyncTree > :: new (
649+ detect_desync. detection_interval ,
650+ ) )
651+ } else {
652+ None
653+ } ;
661654
662655 #[ cfg( not( feature = "desync-debug" ) ) ]
663656 let desync_debug_history = None ;
@@ -678,7 +671,6 @@ where
678671 local_input_disabled : false ,
679672 detect_desyncs : info. detect_desyncs ,
680673 desync_debug_history,
681- world_hash_func : info. world_hash_func ,
682674 }
683675 }
684676}
@@ -866,7 +858,9 @@ where
866858 // GGRS should only use hashes from fixed interval.
867859
868860 // If desync detection enabled, hash world.
869- let checksum = if self . detect_desyncs . is_some ( ) {
861+ let checksum = if let Some ( detect_desyncs) =
862+ self . detect_desyncs . as_ref ( )
863+ {
870864 #[ cfg( feature = "desync-debug" ) ]
871865 {
872866 if let Some ( desync_debug_history) =
@@ -875,14 +869,17 @@ where
875869 if desync_debug_history
876870 . is_desync_detect_frame ( frame as u32 )
877871 {
878- desync_debug_history. record (
879- frame as u32 ,
880- world. desync_tree_node :: < FxHasher > ( ) . into ( ) ,
872+ let tree = DefaultDesyncTree :: from (
873+ world. desync_tree_node :: < FxHasher > (
874+ detect_desyncs. include_unhashable_nodes ,
875+ ) ,
881876 ) ;
877+ desync_debug_history. record ( frame as u32 , tree) ;
882878 }
883879 }
884880 }
885- if let Some ( hash_func) = self . world_hash_func {
881+
882+ if let Some ( hash_func) = detect_desyncs. world_hash_func {
886883 Some ( hash_func ( world) as u128 )
887884 } else {
888885 let mut hasher = FxHasher :: default ( ) ;
@@ -1043,8 +1040,7 @@ where
10431040 player_count : self . session . num_players ( ) . try_into ( ) . unwrap ( ) ,
10441041 max_prediction_window : Some ( self . session . max_prediction ( ) ) ,
10451042 local_input_delay : Some ( self . local_input_delay ) ,
1046- detect_desyncs : self . detect_desyncs ,
1047- world_hash_func : self . world_hash_func ,
1043+ detect_desyncs : self . detect_desyncs . clone ( ) ,
10481044 } ;
10491045 * self = GgrsSessionRunner :: new ( self . original_fps as f32 , runner_info) ;
10501046 }
0 commit comments