public void OnReplayDeserialize(ReplayState state) { int size = state.Read32(); for (int i = 0; i < size; i++) { // Read the target identity ReplayIdentity identity = state.ReadIdentity(); int localSize = state.Read32(); // Read all states for (int j = 0; j < localSize; j++) { // Create empty data container ReplayInitialData data = new ReplayInitialData(); // Deserialize data data.OnReplayDeserialize(state); // Register state if (initialStates.ContainsKey(identity) == false) { initialStates.Add(identity, new List <ReplayInitialData>()); } initialStates[identity].Add(data); } } }
/// <summary> /// Registers the specified replay state with this snapshot. /// The specified identity is used during playback to ensure that the replay objects receives the correct state to deserialize. /// </summary> /// <param name="identity">The identity of the object that was serialized</param> /// <param name="state">The state data for the object</param> public void RecordSnapshot(ReplayIdentity identity, ReplayState state) { // Register the state if (states.ContainsKey(identity) == false) { states.Add(identity, state); } }
/// <summary> /// Called by Unity. /// Allows the <see cref="ReplayBehaviour"/> to validate its identity and register its self with the replay system. /// </summary> public virtual void Awake() { ReplayIdentity.RegisterIdentity(replayIdentity); // Generate the unique id if required - This is requried for objects instantiated during replay //replayIdentity.Generate(); // Find all replay variables and cache them ReplayFindVariables(); }
public ReplayInitialData RestoreInitialReplayObjectData(ReplayIdentity identity, float timestamp) { // Create an error return value ReplayInitialData data = new ReplayInitialData { objectIdentity = identity, timestamp = timestamp, position = Vector3.zero, rotation = Quaternion.identity, scale = Vector3.one, parentIdentity = null, observedComponentIdentities = null, }; // Check for existing object identity if (initialStates.ContainsKey(identity) == true) { // Get the state data List <ReplayInitialData> states = initialStates[identity]; // Check for any states if (states.Count > 0) { // Check for trivial case if (states.Count == 1) { return(states[0]); } int index = -1; float smallestDifference = float.MaxValue; // Find the best matching time stamp for (int i = 0; i < states.Count; i++) { // Get timestamp difference float timeDifference = Mathf.Abs(timestamp - states[i].timestamp); // Check for smaller difference if (timeDifference < smallestDifference) { // We have a new smallest time difference so make that the new target to beat index = i; smallestDifference = timeDifference; } } // Check for valid index if (index != -1) { // Use the state data at the best index match data = states[index]; } } } return(data); }
/// <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> /// Write the specified replay identity to this <see cref="ReplayState"/>. /// </summary> /// <param name="identity">The identity to write</param> public void Write(ReplayIdentity identity) { if (ReplayIdentity.byteSize == 4) { // Write as 4 byte id Write((int)identity); } else { // Write as 2 byte id Write((short)identity); } }
/// <summary> /// Attempts to recall the state information for the specified replay object identity. /// If the identity does not exist in the scene then the return value will be null. /// </summary> /// <param name="identity">The identity of the object to deserialize</param> /// <returns>The state information for the specified identity or null if the identity does not exist</returns> public ReplayState RestoreSnapshot(ReplayIdentity identity) { // Try to get the state if (states.ContainsKey(identity) == true) { // Get the state ReplayState state = states[identity]; // Reset the object state for reading state.PrepareForRead(); return(state); } // No state found return(null); }
public void OnReplayDeserialize(ReplayState state) { // Read the object identity objectIdentity = state.ReadIdentity(); timestamp = state.ReadFloat(); // Read the flags flags = (ReplayInitialDataFlags)state.Read16(); // Read position if ((flags & ReplayInitialDataFlags.Position) != 0) { position = state.ReadVec3(); } // Read rotation if ((flags & ReplayInitialDataFlags.Rotation) != 0) { rotation = state.ReadQuat(); } // Read scale if ((flags & ReplayInitialDataFlags.Scale) != 0) { scale = state.ReadVec3(); } // Read parent identity if ((flags & ReplayInitialDataFlags.Parent) != 0) { parentIdentity = state.ReadIdentity(); } // Read the number of observed components int size = state.Read16(); // Allocate the array observedComponentIdentities = new ReplayIdentity[size]; // Read all ids for (int i = 0; i < size; i++) { // Read the identity observedComponentIdentities[i] = state.ReadIdentity(); } }
/// <summary> /// Called by the replay system when this <see cref="ReplaySnapshot"/> should be deserialized from binary. /// </summary> /// <param name="reader">The binary stream to read the data from</param> public void OnReplayDataDeserialize(BinaryReader reader) { timeStamp = reader.ReadSingle(); // Read states size int size = reader.ReadInt32(); // Read all states for (int i = 0; i < size; i++) { // Read the identity ReplayIdentity identity = new ReplayIdentity(reader.ReadInt16()); // Read the size short stateSize = reader.ReadInt16(); // Read the state data ReplayState stateData = new ReplayState(reader.ReadBytes(stateSize)); // Add to states states.Add(identity, stateData); } }
/// <summary> /// Called by the replay system when this <see cref="ReplaySnapshot"/> should be deserialized. /// </summary> /// <param name="state">The <see cref="ReplayState"/> to read the data from</param> public void OnReplayDeserialize(ReplayState state) { timeStamp = state.ReadFloat(); // Read states size int size = state.Read32(); // Read all states for (int i = 0; i < size; i++) { // Read the identity ReplayIdentity identity = state.ReadIdentity(); // Read size short stateSize = state.Read16(); // Read the state data ReplayState stateData = state.ReadState(stateSize); // Add to states states.Add(identity, stateData); } }
public virtual void OnDestroy() { ReplayIdentity.UnregisterIdentity(replayIdentity); }
public Task <FingerPrintStatusCollection> CheckDuplicatesV2(ReplayIdentity replayIdentity) { throw new NotImplementedException(); }
public bool HasInitialReplayObjectData(ReplayIdentity identity) { // Check for any initial states return(initialStates.ContainsKey(identity)); }
// Methods /// <summary> /// Called by Unity. /// </summary> public void Awake() { ReplayIdentity.RegisterIdentity(replayIdentity); // Check if we need to generate an id //replayIdentity.Generate(); }