Ejemplo n.º 1
0
        /// <summary>
        /// Deserializes all active <see cref="ReplayVariable"/> from the state.
        /// </summary>
        /// <param name="state">The <see cref="ReplayState"/> to read the variable data from</param>
        protected virtual void ReplayDeserializeVariables(ReplayState state)
        {
            // Get the size
            short size = state.Read16();

            // Read all variables
            for (int i = 0; i < size; i++)
            {
                bool matchedVariable = false;

                // Read the variable name hash
                int variableHash = state.Read32();

                // Check for matching variable
                foreach (ReplayVariable variable in replayVariables)
                {
                    // We have found a matching variable
                    if (variable.Name.GetHashCode() == variableHash)
                    {
                        // Deserialize the variable
                        variable.OnReplayDeserialize(state);
                        matchedVariable = true;

                        break;
                    }
                }

                // We failed to resolve the state - we cannot continue reading because the state contains invalid data
                if (matchedVariable == false)
                {
                    break;
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Deserializes all active <see cref="ReplayEvent"/> from the state and dispatches any events to the <see cref="OnReplayEvent(ReplayEvent)"/> handler.
        /// </summary>
        /// <param name="state">The <see cref="ReplayState"/> to read the variable data from</param>
        protected virtual void ReplayDeserializeEvents(ReplayState state)
        {
            // Read the event size
            short size = state.Read16();

            // Read all events
            for (int i = 0; i < size; i++)
            {
                // Create the event
                ReplayEvent evt = new ReplayEvent();

                // Read the event id
                evt.eventID = state.ReadByte();

                // Read the data size
                byte dataSize = state.ReadByte();

                // Read the event data
                evt.eventData = state.ReadState(dataSize);

                try
                {
                    // Trigger the event callback
                    OnReplayEvent(evt);
                }
                catch (Exception e)
                {
                    Debug.LogException(e);
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Called by the replay system when this <see cref="ReplayObject"/> should deserialize its replay data.
        /// </summary>
        /// <param name="state">The <see cref="ReplayState"/> to deserialize the data from</param>
        public void OnReplayDeserialize(ReplayState state)
        {
            // Always read the prefab name
            state.ReadString();

            // Read all data
            while (state.EndRead == false)
            {
                // Read the id
                ReplayIdentity identity = state.ReadIdentity();

                // Read the size
                short size = state.Read16();

                // Mathc flag
                bool matchedData = false;

                // Check for matched id
                foreach (ReplayBehaviour behaviour in observedComponents)
                {
                    // Check for destroyed components
                    if (behaviour == null)
                    {
                        continue;
                    }

                    // Check for matching sub id
                    if (behaviour.Identity == identity)
                    {
                        // We have matched the data so we can now deserialize
                        behaviourState = state.ReadState(size);

                        // Deserialize the component
                        behaviour.OnReplayDeserialize(behaviourState);

                        // Set matched flags
                        matchedData = true;
                        break;
                    }
                }

                // Check for found data
                if (matchedData == false)
                {
                    // We need to consume the data to maintain the state
                    for (int i = 0; i < size; i++)
                    {
                        // Call read byte multiple times to save allocating an array
                        state.ReadByte();
                    }
                    Debug.LogWarning("Possible replay state corruption for replay object " + gameObject);
                }
            }
        }
        /// <summary>
        /// Called by the relay system when the object should return to a previous state.
        /// </summary>
        /// <param name="state">The state object to deserialize the transform from</param>
        public override void OnReplayDeserialize(ReplayState state)
        {
            // Update the last transform
            OnReplayReset();

            // Read the transform flags
            targetFlags = (ReplayTransformFlags)state.Read16();

            // Check for low precision mode
            if ((targetFlags & ReplayTransformFlags.LowPrecision) == 0)
            {
                // Read the position
                if ((targetFlags & ReplayTransformFlags.Position) != 0)
                {
                    targetPosition = state.ReadVec3();
                }

                // Read the rotation
                if ((targetFlags & ReplayTransformFlags.Rotation) != 0)
                {
                    targetRotation = state.ReadQuat();
                }

                // Read the scale
                if ((targetFlags & ReplayTransformFlags.Scale) != 0)
                {
                    targetScale = state.ReadVec3();
                }
            }
            else
            {
                // Read the position
                if ((targetFlags & ReplayTransformFlags.Position) != 0)
                {
                    targetPosition = state.ReadVec3LowPrecision();
                }

                // Read the rotation
                if ((targetFlags & ReplayTransformFlags.Rotation) != 0)
                {
                    targetRotation = state.ReadQuatLowPrecision();
                }

                // Read the scale
                if ((targetFlags & ReplayTransformFlags.Scale) != 0)
                {
                    targetScale = state.ReadVec3LowPrecision();
                }
            }
        }