public SavedInfo InfoFromLastSavedFrame(int back_from_top = 1)
        {
            var state = _sync.GetLastSavedFrame(back_from_top);
            var info  = new SavedInfo
            {
                Frame  = _sync.FrameCount,
                Input  = _last_input,
                Size   = state.Size,
                Buffer = (byte *)UnsafeUtility.Malloc(state.Size,
                                                      UnsafeUtility.AlignOf <byte>(),
                                                      Allocator.Temp),
                Checksum = state.Checksum,
            };

            UnsafeUtility.MemCpy(info.Buffer, state.Buffer, info.Size);

            return(info);
        }
Exemplo n.º 2
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;
            }
        }