예제 #1
0
        public override void AdvanceFrame()
        {
            _sync.IncrementFrame();
            _current_input.Clear();

            Debug.Log($"End of frame({_sync.FrameCount})...");

            if (_rollingback)
            {
                return;
            }

            int frame = _sync.FrameCount;
            // Hold onto the current frame in our queue of saved states.  We'll need
            // the Checksum later to verify that our replay of the same frame got the
            // same results.
            var info = new SavedInfo {
                Frame  = frame,
                Input  = _last_input,
                Size   = _sync.GetLastSavedFrame().Size,
                Buffer = (byte *)UnsafeUtility.Malloc(_sync.GetLastSavedFrame().Size,
                                                      UnsafeUtility.AlignOf <byte>(),
                                                      Allocator.Temp),
                Checksum = _sync.GetLastSavedFrame().Checksum,
            };

            UnsafeUtility.MemCpy(info.Buffer, _sync.GetLastSavedFrame().Buffer, info.Size);
            _saved_frames.Push(info);

            if (frame - _last_verified == _check_distance)
            {
                // We've gone far enough ahead and should now start replaying frames.
                // Load the last verified frame and set the rollback flag to true.
                _sync.LoadFrame(_last_verified);

                _rollingback = true;
                while (!_saved_frames.IsEmpty)
                {
                    _callbacks.AdvanceFrame();

                    // Verify that the Checksumn of this frame is the same as the one in our
                    // list.
                    info = _saved_frames.Peek();
                    _saved_frames.Pop();

                    if (info.Frame != _sync.FrameCount)
                    {
                        Debug.LogWarning($"SyncTest: Frame number {info.Frame} does not match saved frame number {frame}");
                    }
                    int Checksum = _sync.GetLastSavedFrame().Checksum;
                    if (info.Checksum != Checksum)
                    {
                        _callbacks.OnLogState?.Invoke($"Original f{_sync.FrameCount}:", (IntPtr)info.Buffer, info.Size);
                        _callbacks.OnLogState?.Invoke($"Replay   f{_sync.FrameCount}:", (IntPtr)_sync.GetLastSavedFrame().Buffer,
                                                      _sync.GetLastSavedFrame().Size);
                        Debug.LogWarning($"SyncTest: Checksum for frame {frame} does not match saved ({info.Checksum} != {Checksum})");
                        Debug.Break();
                    }
                    else
                    {
                        Debug.Log($"Checksum {Checksum} for frame {info.Frame} matches.");
                    }
                    UnsafeUtility.Free(info.Buffer, Allocator.Temp);
                }
                _last_verified = frame;
                _rollingback   = false;
            }
        }
        public override void AdvanceFrame()
        {
            _sync.IncrementFrame();
            _current_input.Clear();

            if (_rollingback)
            {
                return;
            }
            Debug.Log($"End of frame({_sync.FrameCount})...");

            // Hold onto the current frame in our queue of saved states.  We'll need
            // the Checksum later to verify that our replay of the same frame got the
            // same results.
            var last_info = InfoFromLastSavedFrame();

            _saved_frames.Push(last_info);

            if (last_info.Frame - _last_verified == _check_distance)
            {
                // We've gone far enough ahead and should now start replaying frames.
                // Load the last verified frame and set the rollback flag to true.
                _sync.LoadFrame(_last_verified);

                _rollingback = true;
                while (!_saved_frames.IsEmpty)
                {
                    Debug.LogWarning("Rollback!");
                    _callbacks.AdvanceFrame();

                    // Verify that the Checksumn of this frame is the same as the one in our
                    // list.
                    var info = _saved_frames.Peek();
                    _saved_frames.Pop();

                    var curr_info = InfoFromLastSavedFrame();

                    if (last_info.Frame > 9)
                    {
                        var rolled = InfoFromLastSavedFrame(curr_info.Frame - _last_verified + 1);
                        //_callbacks.CompareGameState(ref rolled, ref curr_info);
                    }

                    if (info.Frame != curr_info.Frame)
                    {
                        Debug.LogWarning($"SyncTest: Frame number {info.Frame} does not match saved frame number {curr_info.Frame}");
                    }

                    if (info.Checksum != curr_info.Checksum)
                    {
                        _callbacks.OnLogState?.Invoke($"Original f{curr_info.Frame}:", (IntPtr)info.Buffer, info.Size);
                        _callbacks.OnLogState?.Invoke($"Replay   f{curr_info.Frame}:", (IntPtr)curr_info.Buffer, curr_info.Size);
                        Debug.LogWarning($"SyncTest: Checksum for frame {curr_info.Frame} does not match saved ({info.Checksum} != {curr_info.Checksum})");

                        //_callbacks.CompareGameState(ref info, ref curr_info);
                    }
                    else
                    {
                        Debug.Log($"Checksum {curr_info.Checksum} for frame {curr_info.Frame} matches.");
                    }
                    UnsafeUtility.Free(info.Buffer, Allocator.Temp);
                }
                _last_verified = last_info.Frame;
                _rollingback   = false;
            }
        }