Esempio n. 1
0
        public void AdjustRealTimeToAnimation(MultiplayerFrameBufferObject animationFrame)
        {
            for (int i = 0; i < MultiplayerUtils.audioPlayerNames.Count; i++)
            {
                foreach (AudioOneShotEvent oneShot in audioOneShots[i])
                {
                    oneShot.time = animationFrame.realFrameTime + animationFrame.frameTime - oneShot.time;
                }
                foreach (AudioClipEvent clipEvent in audioClipEvents[i])
                {
                    clipEvent.time = animationFrame.realFrameTime + animationFrame.frameTime - clipEvent.time;
                }
                foreach (AudioVolumeEvent volumeEvent in audioVolumeEvents[i])
                {
                    volumeEvent.time = animationFrame.realFrameTime + animationFrame.frameTime - volumeEvent.time;
                }
                foreach (AudioPitchEvent pitchEvent in audioPitchEvents[i])
                {
                    pitchEvent.time = animationFrame.realFrameTime + animationFrame.frameTime - pitchEvent.time;
                }
                foreach (AudioCutoffEvent cutoffEvent in audioCutoffEvents[i])
                {
                    cutoffEvent.time = animationFrame.realFrameTime + animationFrame.frameTime - cutoffEvent.time;
                }
            }

            setRealTime = true;
        }
        public MultiplayerFrameBufferObject()
        {
            animFrame      = 0;
            deltaTime      = 0;
            timeSinceStart = 0;

            replayFrameBufferObject = this;
        }
Esempio n. 3
0
        TransformInfo[] BufferToInfo(MultiplayerFrameBufferObject frame)
        {
            TransformInfo[] info = new TransformInfo[replayController.playbackTransforms.Count];

            for (int i = 0; i < info.Length; i++)
            {
                info[i]          = new TransformInfo(this.skater.transform);
                info[i].position = frame.vectors[i];
                info[i].rotation = frame.quaternions[i];
            }

            return(info);
        }
        public void AdjustRealTimeToAnimation(MultiplayerFrameBufferObject animationFrame)
        {
            int audioPlayerCount = MultiplayerUtils.audioPlayerNames.Count;

            for (int i = 0; i < audioPlayerCount; i++)
            {
                int oneShotsCount = audioOneShots[i].Count;
                for (int j = 0; j < oneShotsCount; j++)
                {
                    audioOneShots[i][j].time = animationFrame.realFrameTime + animationFrame.frameTime - audioOneShots[i][j].time;
                }

                int clipEventsCount = audioClipEvents[i].Count;
                for (int j = 0; j < clipEventsCount; j++)
                {
                    audioClipEvents[i][j].time = animationFrame.realFrameTime + animationFrame.frameTime - audioClipEvents[i][j].time;
                }

                int volumeEventsCount = audioVolumeEvents[i].Count;
                for (int j = 0; j < volumeEventsCount; j++)
                {
                    audioVolumeEvents[i][j].time = animationFrame.realFrameTime + animationFrame.frameTime - audioVolumeEvents[i][j].time;
                }

                int pitchEventsCount = audioPitchEvents[i].Count;
                for (int j = 0; j < pitchEventsCount; j++)
                {
                    audioPitchEvents[i][j].time = animationFrame.realFrameTime + animationFrame.frameTime - audioPitchEvents[i][j].time;
                }

                int cutoffEventsCount = audioCutoffEvents[i].Count;
                for (int j = 0; j < cutoffEventsCount; j++)
                {
                    audioCutoffEvents[i][j].time = animationFrame.realFrameTime + animationFrame.frameTime - audioCutoffEvents[i][j].time;
                }
            }

            setRealTime = true;
        }
Esempio n. 5
0
        public void UnpackAnimator(byte[] recBuffer)
        {
            int receivedPacketSequence = BitConverter.ToInt32(recBuffer, 0);

            byte[] buffer = new byte[recBuffer.Length - 5];
            if (receivedPacketSequence < currentAnimationPacket - 5)
            {
                return;
            }
            else
            {
                Array.Copy(recBuffer, 5, buffer, 0, recBuffer.Length - 5);
                currentAnimationPacket = receivedPacketSequence > currentAnimationPacket ? receivedPacketSequence : currentAnimationPacket;
            }

            if (animationFrames.Find(f => f.animFrame == receivedPacketSequence) != null)
            {
                debugWriter.WriteLine("Duped frame of animation for player: " + this.username);
                return;
            }

            MultiplayerFrameBufferObject currentBufferObject = new MultiplayerFrameBufferObject();

            currentBufferObject.key       = recBuffer[4] == (byte)1 ? true : false;
            currentBufferObject.animFrame = receivedPacketSequence;

            currentBufferObject.frameTime = BitConverter.ToSingle(buffer, buffer.Length - 4);

            List <Vector3>    vectors     = new List <Vector3>();
            List <Quaternion> quaternions = new List <Quaternion>();

            for (int i = 0; i < 77; i++)
            {
                if (currentBufferObject.key)
                {
                    Vector3 readVector = new Vector3();
                    readVector.x = SystemHalf.HalfHelper.HalfToSingle(SystemHalf.Half.ToHalf(buffer, i * 12));
                    readVector.y = SystemHalf.HalfHelper.HalfToSingle(SystemHalf.Half.ToHalf(buffer, i * 12 + 2));
                    readVector.z = SystemHalf.HalfHelper.HalfToSingle(SystemHalf.Half.ToHalf(buffer, i * 12 + 4));

                    Quaternion readQuaternion = new Quaternion();
                    readQuaternion.eulerAngles = new Vector3(SystemHalf.HalfHelper.HalfToSingle(SystemHalf.Half.ToHalf(buffer, i * 12 + 6)),
                                                             SystemHalf.HalfHelper.HalfToSingle(SystemHalf.Half.ToHalf(buffer, i * 12 + 8)),
                                                             SystemHalf.HalfHelper.HalfToSingle(SystemHalf.Half.ToHalf(buffer, i * 12 + 10)));

                    vectors.Add(readVector);
                    quaternions.Add(readQuaternion);
                }
                else
                {
                    Vector3 readVector = new Vector3();
                    readVector.x = BitConverter.ToSingle(buffer, i * 24);
                    readVector.y = BitConverter.ToSingle(buffer, i * 24 + 4);
                    readVector.z = BitConverter.ToSingle(buffer, i * 24 + 8);

                    Quaternion readQuaternion = new Quaternion();
                    readQuaternion.eulerAngles = new Vector3(BitConverter.ToSingle(buffer, i * 24 + 12),
                                                             BitConverter.ToSingle(buffer, i * 24 + 16),
                                                             BitConverter.ToSingle(buffer, i * 24 + 20));

                    vectors.Add(readVector);
                    quaternions.Add(readQuaternion);
                }
            }

            currentBufferObject.vectors     = vectors.ToArray();
            currentBufferObject.quaternions = quaternions.ToArray();

            this.animationFrames.Add(currentBufferObject);
            this.animationFrames = this.animationFrames.OrderBy(f => f.animFrame).ToList();
        }
        public void PrepareReplay()
        {
            this.recordedFrames.Clear();

            this.replayAnimationFrames = this.replayAnimationFrames.OrderBy(f => f.animFrame).ToList().ToList();

            if (replayAnimationFrames == null || replayAnimationFrames.Count == 0)
            {
                return;
            }

            int firstKey = this.replayAnimationFrames.FindIndex(f => f.key);

            if (firstKey < 0)
            {
                return;
            }

            this.replayAnimationFrames.RemoveRange(0, firstKey);

            MultiplayerFrameBufferObject firstRealTime, lastRealTime;

            firstRealTime = this.replayAnimationFrames.Find(f => f.realFrameTime != -1f);
            lastRealTime  = this.replayAnimationFrames.Last(f => f.realFrameTime != -1f);

            if (firstRealTime == lastRealTime)
            {
                return;
            }

            float averageFrameTime = (lastRealTime.animFrame - firstRealTime.animFrame) / (lastRealTime.realFrameTime - firstRealTime.realFrameTime);

            ReplayRecordedFrame previousFrame = null;

            for (int f = 0; f < this.replayAnimationFrames.Count; f++)
            {
                MultiplayerFrameBufferObject frame = this.replayAnimationFrames[f];

                if (f == 0)
                {
                    frame.realFrameTime = firstRealTime.realFrameTime + ((frame.animFrame - firstRealTime.animFrame) * 1f / 30f);
                }

                //if(f > 0 && !frame.key && this.replayAnimationFrames[f - 1].animFrame != frame.animFrame - 1) {
                //	continue;
                //}

                TransformInfo[] transforms = null;

                if (previousFrame != null && !frame.key && this.replayAnimationFrames[f - 1].animFrame != frame.animFrame)
                {
                    if (this.replayAnimationFrames[f - 1].animFrame != frame.animFrame - 1)
                    {
                        MultiplayerFrameBufferObject nextKeyFrame = this.replayAnimationFrames.FirstOrDefault(nK => nK.animFrame > frame.animFrame);
                        int framesToInterpolate = frame.animFrame - this.replayAnimationFrames[f - 1].animFrame;
                        if (nextKeyFrame != null && nextKeyFrame.realFrameTime != -1f)
                        {
                            int frameDiff = frame.animFrame - nextKeyFrame.animFrame;
                            for (int i = 1; i < framesToInterpolate; i++)
                            {
                                Vector3[]    interpolatedVectors     = new Vector3[frame.vectors.Length];
                                Quaternion[] interpolatedQuaternions = new Quaternion[frame.vectors.Length];
                                for (int c = 0; c < frame.vectors.Length; c++)
                                {
                                    interpolatedVectors[c]     = Vector3.Lerp(this.replayAnimationFrames[f - 1].vectors[c], nextKeyFrame.vectors[c], c / frameDiff);
                                    interpolatedQuaternions[c] = Quaternion.Slerp(this.replayAnimationFrames[f - 1].quaternions[c], nextKeyFrame.quaternions[c], c / frameDiff);
                                }

                                float frameTime = this.replayAnimationFrames[f - 1].realFrameTime + (i / frameDiff) * (nextKeyFrame.frameTime - this.replayAnimationFrames[f - 1].realFrameTime);

                                this.recordedFrames.Add(new ReplayRecordedFrame(CreateInfoArray(interpolatedVectors, interpolatedQuaternions), frameTime));

                                previousFrame = this.recordedFrames.Last();
                            }
                        }
                    }

                    Vector3[]    vectors     = new Vector3[frame.vectors.Length];
                    Quaternion[] quaternions = new Quaternion[frame.vectors.Length];
                    for (int i = 0; i < frame.vectors.Length; i++)
                    {
                        vectors[i] = previousFrame.transformInfos[i].position + frame.vectors[i];
                        quaternions[i].eulerAngles = previousFrame.transformInfos[i].rotation.eulerAngles + frame.quaternions[i].eulerAngles;
                    }

                    transforms = CreateInfoArray(vectors, quaternions);
                }
                else if (frame.key && (f != 0 && frame.animFrame != this.replayAnimationFrames[f - 1].animFrame))
                {
                    transforms = BufferToInfo(frame);
                }

                if (frame.realFrameTime == -1f && f > 0)
                {
                    frame.realFrameTime = this.replayAnimationFrames[f - 1].realFrameTime + (frame.animFrame - this.replayAnimationFrames[f - 1].animFrame) * (1f / 30f);
                }

                if (transforms != null && frame.realFrameTime != -1f)
                {
                    this.recordedFrames.Add(new ReplayRecordedFrame(transforms, frame.realFrameTime));

                    previousFrame = this.recordedFrames.Last();
                }
            }

            Traverse.Create(replayController).Property("gameplayEvents").SetValue(new List <GPEvent>());

            FinalizeReplay(true);
        }
 TransformInfo[] BufferToInfo(MultiplayerFrameBufferObject frame)
 {
     return(CreateInfoArray(frame.vectors, frame.quaternions));
 }
        // TODO: refactor all this shit, I'm sure there's a better way
        public void LerpNextFrame(bool inReplay, bool recursive = false, float offset = 0, int recursionLevel = 0)
        {
            if (this.animationFrames.Count == 0 || this.animationFrames[0] == null)
            {
                return;
            }
            int animationFramesCount = this.animationFrames.Count;

            currentAnimationFrame = this.animationFrames[0];
            if (!startedAnimating && animationFrames.Count > 5)
            {
                if (currentAnimationFrame.vectors == null || !currentAnimationFrame.key)
                {
                    this.animationFrames.RemoveAt(0);
                    LerpNextFrame(inReplay, true, Time.unscaledDeltaTime, recursionLevel + 1);
                    return;
                }

                startedAnimating = true;

                for (int i = 0; i < 77; i++)
                {
                    bones[i].localPosition = currentAnimationFrame.vectors[i];
                    bones[i].localRotation = currentAnimationFrame.quaternions[i];
                }

                this.previousFrameTime     = currentAnimationFrame.frameTime;
                this.previousFinishedFrame = currentAnimationFrame;
                //this.firstFrameTime = currentAnimationFrame.frameTime;
                //this.startAnimTime = Time.time;

                this.animationFrames.RemoveAt(0);
            }

            if (!startedAnimating)
            {
                return;
            }

            int frameDelay = 0;

            if (this.animationFrames.Count < 2)
            {
                return;
            }

            if (this.animationFrames.Count < 4 || this.waitingForDelay)
            {
                this.waitingForDelay = !(this.animationFrames.Count > 5);
            }

            if (this.animationFrames.Count >= 10 || this.speedDelay)
            {
                if (this.animationFrames.Count > 50)
                {
                    this.animationFrames.RemoveRange(0, this.animationFrames.Count - 20);
                }

                this.speedDelay = !(this.animationFrames.Count < 7);
            }

            if (this.waitingForDelay || this.speedDelay)
            {
                frameDelay = this.animationFrames.Count - 6;
            }

            if (currentAnimationFrame.vectors == null)
            {
                this.animationFrames.RemoveAt(0);

                LerpNextFrame(inReplay, true, Time.unscaledDeltaTime, recursionLevel + 1);
                return;
            }

            if (currentAnimationFrame.deltaTime == 0)
            {
                currentAnimationFrame.deltaTime = currentAnimationFrame.frameTime - this.previousFrameTime;

                if (currentAnimationFrame.deltaTime == 0.0f)
                {
                    currentAnimationFrame.deltaTime = 1f / 30f;
                }

                if (frameDelay != 0)
                {
                    //debugWriter.Write("Adjusting current animation frame time from: " + currentAnimationFrame.deltaTime);
                    currentAnimationFrame.deltaTime = frameDelay < 0 ? currentAnimationFrame.deltaTime * Mathf.Max(Mathf.Abs(frameDelay), 2) : currentAnimationFrame.deltaTime / Mathf.Min(Mathf.Max(frameDelay, 2), 6);
                    //debugWriter.WriteLine("  To: " + currentAnimationFrame.deltaTime);

                    if (currentAnimationFrame.deltaTime > 0.14f)
                    {
                        debugWriter.WriteLine("Capping current frame to 140ms");
                        currentAnimationFrame.deltaTime = 0.14f;
                    }
                }
            }

            if (!recursive)
            {
                currentAnimationFrame.timeSinceStart += Time.unscaledDeltaTime;
            }
            else
            {
                currentAnimationFrame.timeSinceStart = offset;
            }

            // TODO: figure out why this occassionally spikes frames
            if (!inReplay)
            {
                if (currentAnimationFrame.timeSinceStart < currentAnimationFrame.deltaTime)
                {
                    for (int i = 0; i < 77; i++)
                    {
                        bones[i].localPosition = Vector3.Lerp(previousFinishedFrame.vectors[i], currentAnimationFrame.vectors[i], currentAnimationFrame.timeSinceStart / currentAnimationFrame.deltaTime);
                        bones[i].localRotation = Quaternion.Slerp(previousFinishedFrame.quaternions[i], currentAnimationFrame.quaternions[i], currentAnimationFrame.timeSinceStart / currentAnimationFrame.deltaTime);
                    }
                }

                replayController.ClipEndTime = PlayTime.time + 0.5f;

                if (!recursive)
                {
                    foreach (ReplayAudioEventPlayer replayAudioEventPlayer in replayController.AudioEventPlayers)
                    {
                        if (replayAudioEventPlayer != null && !replayAudioEventPlayer.enabled)
                        {
                            replayAudioEventPlayer.enabled = true;
                        }
                        if (replayAudioEventPlayer != null)
                        {
                            replayAudioEventPlayer.SetPlaybackTime(PlayTime.time, 1.0f);
                        }
                    }
                }
            }

            if (this.currentlySetUsername != this.username)
            {
                this.usernameText.text    = this.username;
                this.currentlySetUsername = this.username;
            }
            this.usernameObject.transform.position = this.skater.transform.position + this.skater.transform.up;
            this.usernameObject.transform.LookAt(mainCameraTransform);

            if (currentAnimationFrame.timeSinceStart >= currentAnimationFrame.deltaTime)
            {
                // 30FPS 120Seconds
                //if (!currentAnimationFrame.key) {
                //	if (this.recordedFrames.Count > 30 * 120) {
                //		this.recordedFrames.RemoveAt(0);
                //		this.recordedFrames.Add(new ReplayRecordedFrame(BufferToInfo(currentAnimationFrame), this.startAnimTime + currentAnimationFrame.frameTime - this.firstFrameTime));
                //	} else {
                //		this.recordedFrames.Add(new ReplayRecordedFrame(BufferToInfo(currentAnimationFrame), this.startAnimTime + currentAnimationFrame.frameTime - this.firstFrameTime));
                //	}
                //}
                previousFinishedFrame = currentAnimationFrame;
                if (currentAnimationFrame.replayFrameBufferObject == currentAnimationFrame)
                {
                    previousFinishedFrame.replayFrameBufferObject = this.replayAnimationFrames.Find(f => f.animFrame == currentAnimationFrame.animFrame);
                }
                previousFinishedFrame.replayFrameBufferObject.realFrameTime = PlayTime.time;

                if (!recursive)
                {
                    int soundsToRemove  = 0;
                    int soundQueueCount = soundQueue.Count;
                    for (soundsToRemove = 0; soundsToRemove < soundQueueCount; soundsToRemove++)
                    {
                        if (previousFinishedFrame.replayFrameBufferObject.frameTime + 1 / 15f < soundQueue[0].playTime)
                        {
                            break;
                        }

                        if (soundsToRemove == soundQueueCount - 1)
                        {
                            soundsToRemove++;
                            break;
                        }
                    }

                    if (soundsToRemove > 0)
                    {
                        for (int i = 0; i < soundsToRemove; i++)
                        {
                            soundQueue[i].AdjustRealTimeToAnimation(previousFinishedFrame.replayFrameBufferObject);
                            soundQueue[i].AddSoundsToPlayers(this.replayController, this.replayEventPlayerForName);
                        }

                        soundQueue.RemoveRange(0, soundsToRemove);
                    }
                }

                for (int i = 0; i < 77; i++)
                {
                    if (!inReplay)
                    {
                        bones[i].localPosition = currentAnimationFrame.vectors[i];
                        bones[i].localRotation = currentAnimationFrame.quaternions[i];
                    }

                    if (!this.animationFrames[1].key)
                    {
                        this.animationFrames[1].vectors[i] = currentAnimationFrame.vectors[i] + this.animationFrames[1].vectors[i];
                        this.animationFrames[1].quaternions[i].eulerAngles += currentAnimationFrame.quaternions[i].eulerAngles;
                    }
                }
                replayController.ClipEndTime = PlayTime.time + 0.5f;

                float oldTime  = currentAnimationFrame.timeSinceStart;
                float oldDelta = currentAnimationFrame.deltaTime;

                this.previousFrameTime = currentAnimationFrame.frameTime;
                this.animationFrames.RemoveAt(0);

                if (oldTime - oldDelta > 0f && recursionLevel < 2)
                {
                    this.LerpNextFrame(inReplay, true, oldTime - oldDelta, recursionLevel + 1);
                    return;
                }
            }
        }
        public void UnpackSounds(byte[] soundBytes)
        {
            int readBytes            = 0;
            int numberOfAudioPlayers = MultiplayerUtils.audioPlayerNames.Count;

            List <AudioOneShotEvent>[] newOneShots     = new List <AudioOneShotEvent> [numberOfAudioPlayers];
            List <AudioClipEvent>[]    newClipEvents   = new List <AudioClipEvent> [numberOfAudioPlayers];
            List <AudioVolumeEvent>[]  newVolumeEvents = new List <AudioVolumeEvent> [numberOfAudioPlayers];
            List <AudioPitchEvent>[]   newPitchEvents  = new List <AudioPitchEvent> [numberOfAudioPlayers];
            List <AudioCutoffEvent>[]  newCutoffEvents = new List <AudioCutoffEvent> [numberOfAudioPlayers];

            float earliestSoundTime = float.MaxValue;

            for (int i = 0; i < numberOfAudioPlayers; i++)
            {
                newOneShots[i]     = new List <AudioOneShotEvent>();
                newClipEvents[i]   = new List <AudioClipEvent>();
                newVolumeEvents[i] = new List <AudioVolumeEvent>();
                newPitchEvents[i]  = new List <AudioPitchEvent>();
                newCutoffEvents[i] = new List <AudioCutoffEvent>();

                int oneShots = BitConverter.ToInt32(soundBytes, readBytes);
                readBytes += 4;
                for (int j = 0; j < oneShots; j++)
                {
                    newOneShots[i].Add(new AudioOneShotEvent());
                    newOneShots[i][j].clipName    = MultiplayerUtils.ClipNameFromArrayByte(soundBytes[readBytes]);
                    newOneShots[i][j].time        = BitConverter.ToSingle(soundBytes, readBytes + 1);
                    newOneShots[i][j].volumeScale = BitConverter.ToSingle(soundBytes, readBytes + 5) * Main.settings.volumeMultiplier;

                    earliestSoundTime = Mathf.Min(newOneShots[i][j].time, earliestSoundTime);

                    readBytes += 9;
                }

                int clipEvents = BitConverter.ToInt32(soundBytes, readBytes);
                readBytes += 4;
                for (int j = 0; j < clipEvents; j++)
                {
                    newClipEvents[i].Add(new AudioClipEvent());
                    newClipEvents[i][j].clipName  = MultiplayerUtils.ClipNameFromArrayByte(soundBytes[readBytes]);
                    newClipEvents[i][j].time      = BitConverter.ToSingle(soundBytes, readBytes + 1);
                    newClipEvents[i][j].isPlaying = soundBytes[readBytes + 5] == 1 ? true : false;

                    earliestSoundTime = Mathf.Min(newClipEvents[i][j].time, earliestSoundTime);

                    readBytes += 6;
                }

                int volumeEvents = BitConverter.ToInt32(soundBytes, readBytes);
                readBytes += 4;
                for (int j = 0; j < volumeEvents; j++)
                {
                    newVolumeEvents[i].Add(new AudioVolumeEvent());
                    newVolumeEvents[i][j].time   = BitConverter.ToSingle(soundBytes, readBytes);
                    newVolumeEvents[i][j].volume = BitConverter.ToSingle(soundBytes, readBytes + 4) * Main.settings.volumeMultiplier;

                    earliestSoundTime = Mathf.Min(newVolumeEvents[i][j].time, earliestSoundTime);

                    readBytes += 8;
                }

                int pitchEvents = BitConverter.ToInt32(soundBytes, readBytes);
                readBytes += 4;
                for (int j = 0; j < pitchEvents; j++)
                {
                    newPitchEvents[i].Add(new AudioPitchEvent());
                    newPitchEvents[i][j].time  = BitConverter.ToSingle(soundBytes, readBytes);
                    newPitchEvents[i][j].pitch = BitConverter.ToSingle(soundBytes, readBytes + 4);

                    earliestSoundTime = Mathf.Min(newPitchEvents[i][j].time, earliestSoundTime);

                    readBytes += 8;
                }

                int cutoffEvents = BitConverter.ToInt32(soundBytes, readBytes);
                readBytes += 4;
                for (int j = 0; j < cutoffEvents; j++)
                {
                    newCutoffEvents[i].Add(new AudioCutoffEvent());
                    newCutoffEvents[i][j].time   = BitConverter.ToSingle(soundBytes, readBytes);
                    newCutoffEvents[i][j].cutoff = BitConverter.ToSingle(soundBytes, readBytes + 4);

                    earliestSoundTime = Mathf.Min(newCutoffEvents[i][j].time, earliestSoundTime);

                    readBytes += 8;
                }
            }

            MultiplayerSoundBufferObject newSoundBufferObject = new MultiplayerSoundBufferObject(numberOfAudioPlayers);

            soundQueue.Add(newSoundBufferObject);

            for (int i = 0; i < MultiplayerUtils.audioPlayerNames.Count; i++)
            {
                newSoundBufferObject.audioClipEvents[i]   = newClipEvents[i];
                newSoundBufferObject.audioOneShots[i]     = newOneShots[i];
                newSoundBufferObject.audioCutoffEvents[i] = newCutoffEvents[i];
                newSoundBufferObject.audioPitchEvents[i]  = newPitchEvents[i];
                newSoundBufferObject.audioVolumeEvents[i] = newVolumeEvents[i];
            }

            try {
                MultiplayerFrameBufferObject firstRealTime = this.replayAnimationFrames.FirstOrDefault(f => f.realFrameTime != -1f);

                if (firstRealTime != null)
                {
                    foreach (ReplayAudioEventPlayer audioPlayer in replayController.AudioEventPlayers)
                    {
                        if (!ReferenceEquals(audioPlayer, null))
                        {
                            if (audioPlayer.clipEvents != null)
                            {
                                MultiplayerUtils.RemoveAudioEventsOlderThanExcept(audioPlayer.clipEvents, firstRealTime.realFrameTime, 0);
                            }
                            if (audioPlayer.cutoffEvents != null)
                            {
                                MultiplayerUtils.RemoveAudioEventsOlderThanExcept(audioPlayer.cutoffEvents, firstRealTime.realFrameTime, 0);
                            }
                            if (audioPlayer.oneShotEvents != null)
                            {
                                MultiplayerUtils.RemoveAudioEventsOlderThanExcept(audioPlayer.oneShotEvents, firstRealTime.realFrameTime, 0);
                            }
                            if (audioPlayer.pitchEvents != null)
                            {
                                MultiplayerUtils.RemoveAudioEventsOlderThanExcept(audioPlayer.pitchEvents, firstRealTime.realFrameTime, 0);
                            }
                            if (audioPlayer.volumeEvents != null)
                            {
                                MultiplayerUtils.RemoveAudioEventsOlderThanExcept(audioPlayer.volumeEvents, firstRealTime.realFrameTime, 0);
                            }
                        }
                    }
                }
            } catch (Exception) { }
        }
        public void UnpackAnimations(byte[] recBuffer)
        {
            int receivedPacketSequence = BitConverter.ToInt32(recBuffer, 0);

            bool ordered = true;

            if (receivedPacketSequence < currentAnimationPacket - 5)
            {
                ordered = false;
            }
            else
            {
                currentAnimationPacket = Math.Max(currentAnimationPacket, receivedPacketSequence);
            }

            MultiplayerFrameBufferObject currentBufferObject = new MultiplayerFrameBufferObject();

            currentBufferObject.key       = recBuffer[4] == (byte)1 ? true : false;
            currentBufferObject.animFrame = receivedPacketSequence;

            currentBufferObject.frameTime = BitConverter.ToSingle(recBuffer, recBuffer.Length - 4);

            if (this.firstFrameTime != -1f && currentBufferObject.frameTime < this.firstFrameTime)
            {
                return;
            }

            Vector3[]    vectors     = new Vector3[77];
            Quaternion[] quaternions = new Quaternion[77];

            float[]           floatValues = null;
            ushort[]          halfValues  = null;
            SystemHalf.Half[] halfArray   = null;

            if (currentBufferObject.key)
            {
                halfValues = new ushort[77 * 6];
                halfArray  = new SystemHalf.Half[77 * 6];

                Buffer.BlockCopy(recBuffer, 5, halfValues, 0, 77 * 6 * sizeof(ushort));

                for (int i = 0; i < halfValues.Length; i++)
                {
                    halfArray[i]       = new SystemHalf.Half();
                    halfArray[i].Value = halfValues[i];
                }
            }
            else
            {
                floatValues = new float[77 * 6];

                Buffer.BlockCopy(recBuffer, 5, floatValues, 0, 77 * 6 * sizeof(float));
            }

            for (int i = 0; i < 77; i++)
            {
                if (currentBufferObject.key)
                {
                    vectors[i].x = SystemHalf.HalfHelper.HalfToSingle(halfArray[i * 6]);
                    vectors[i].y = SystemHalf.HalfHelper.HalfToSingle(halfArray[i * 6 + 1]);
                    vectors[i].z = SystemHalf.HalfHelper.HalfToSingle(halfArray[i * 6 + 2]);

                    quaternions[i].eulerAngles = new Vector3(SystemHalf.HalfHelper.HalfToSingle(halfArray[i * 6 + 3]),
                                                             SystemHalf.HalfHelper.HalfToSingle(halfArray[i * 6 + 4]),
                                                             SystemHalf.HalfHelper.HalfToSingle(halfArray[i * 6 + 5]));
                }
                else
                {
                    vectors[i].x = floatValues[i * 6];
                    vectors[i].y = floatValues[i * 6 + 1];
                    vectors[i].z = floatValues[i * 6 + 2];

                    quaternions[i].eulerAngles = new Vector3(floatValues[i * 6 + 3], floatValues[i * 6 + 4], floatValues[i * 6 + 5]);
                }
            }

            currentBufferObject.vectors     = vectors;
            currentBufferObject.quaternions = quaternions;

            if (ordered)
            {
                this.animationFrames.Add(currentBufferObject);

                this.animationFrames.Sort((f, f2) => f.animFrame.CompareTo(f2.animFrame));
                //this.animationFrames = this.animationFrames.OrderBy(f => f.animFrame).ToList();
            }

            if ((this.replayAnimationFrames.Count > 0 && this.replayAnimationFrames[0] != null && this.replayAnimationFrames[0].animFrame > currentBufferObject.animFrame) ||
                (!currentBufferObject.key && this.replayAnimationFrames.Count < 1))
            {
                return;
            }

            if (this.startAnimTime == -1f && this.firstFrameTime == -1f && currentBufferObject.key)
            {
                this.firstFrameTime = currentBufferObject.frameTime;
                this.startAnimTime  = PlayTime.time;
            }

            while (this.replayAnimationFrames.Count > 30 * 120)
            {
                this.replayAnimationFrames.RemoveAt(0);
            }

            Vector3[]    replayVectors     = new Vector3[vectors.Length];
            Quaternion[] replayQuaternions = new Quaternion[quaternions.Length];
            vectors.CopyTo(replayVectors, 0);
            quaternions.CopyTo(replayQuaternions, 0);

            MultiplayerFrameBufferObject replayFrameObject = new MultiplayerFrameBufferObject {
                key         = currentBufferObject.key,
                frameTime   = currentBufferObject.frameTime,
                animFrame   = currentBufferObject.animFrame,
                vectors     = replayVectors,
                quaternions = replayQuaternions
            };

            currentBufferObject.replayFrameBufferObject = replayFrameObject;

            this.replayAnimationFrames.Add(replayFrameObject);
        }
        public void PrepareReplay()
        {
            this.recordedFrames.Clear();

            int firstKey = this.replayAnimationFrames.FindIndex(f => f.key);

            this.replayAnimationFrames.RemoveRange(0, firstKey);

            MultiplayerFrameBufferObject firstRealTime, lastRealTime;

            firstRealTime = this.replayAnimationFrames.Find(f => f.realFrameTime != -1f);
            lastRealTime  = this.replayAnimationFrames.Last(f => f.realFrameTime != -1f);

            if (firstRealTime == lastRealTime)
            {
                return;
            }

            float averageFrameTime = (lastRealTime.animFrame - firstRealTime.animFrame) / (lastRealTime.realFrameTime - firstRealTime.realFrameTime);

            ReplayRecordedFrame previousFrame = null;

            for (int f = 0; f < this.replayAnimationFrames.Count; f++)
            {
                MultiplayerFrameBufferObject frame = this.replayAnimationFrames[f];

                if (f == 0)
                {
                    MultiplayerFrameBufferObject firstFrameWithTime = this.replayAnimationFrames.First(obj => obj.realFrameTime != -1f);
                    frame.realFrameTime = firstFrameWithTime.realFrameTime + (frame.animFrame - firstFrameWithTime.animFrame) * averageFrameTime;
                }

                //if(f > 0 && !frame.key && this.replayAnimationFrames[f - 1].animFrame != frame.animFrame - 1) {
                //	continue;
                //}

                TransformInfo[] transforms = null;

                if (previousFrame != null && !frame.key)
                {
                    Vector3[]    vectors     = new Vector3[frame.vectors.Length];
                    Quaternion[] quaternions = new Quaternion[frame.vectors.Length];
                    for (int i = 0; i < frame.vectors.Length; i++)
                    {
                        vectors[i] = previousFrame.transformInfos[i].position + frame.vectors[i];
                        quaternions[i].eulerAngles = previousFrame.transformInfos[i].rotation.eulerAngles + frame.quaternions[i].eulerAngles;
                    }

                    transforms = CreateInfoArray(vectors, quaternions);
                }
                if (frame.key)
                {
                    transforms = BufferToInfo(frame);
                }

                if (frame.realFrameTime == -1f && f > 0)
                {
                    frame.realFrameTime = this.replayAnimationFrames[f - 1].realFrameTime + (frame.animFrame - this.replayAnimationFrames[f - 1].animFrame) * (1f / 30f);
                }

                if (transforms != null)
                {
                    this.recordedFrames.Add(new ReplayRecordedFrame(transforms, frame.realFrameTime));

                    previousFrame = this.recordedFrames.Last();
                }
            }

            this.replayController.enabled = true;
            this.recordedFrames           = this.recordedFrames.OrderBy(f => f.time).ToList();
            this.EnsureQuaternionListContinuity();
            Traverse.Create(this.replayController).Property("ClipFrames").SetValue((from f in this.recordedFrames select f.Copy()).ToList());
            Traverse.Create(this.replayController).Field("m_audioEventPlayers").SetValue(new List <ReplayAudioEventPlayer>());
            {             // Subtract Start Time
                float firstFrameGameTime = ReplayRecorder.Instance.RecordedFrames[0].time;
                this.replayController.ClipFrames.ForEach(delegate(ReplayRecordedFrame f) {
                    f.time -= firstFrameGameTime;
                });
                UnityModManagerNet.UnityModManager.Logger.Log(this.replayController.ClipFrames[0].time + " " + ReplayEditorController.Instance.playbackController.ClipFrames[0].time + " " + firstFrameGameTime);
                this.replayController.ClipEndTime = this.replayController.ClipFrames[this.replayController.ClipFrames.Count - 1].time;
            }
            this.replayController.StartCoroutine("UpdateAnimationClip");
            this.usernameObject.GetComponent <Renderer>().enabled = false;
        }
        public void UnpackAnimations(byte[] recBuffer)
        {
            int receivedPacketSequence = BitConverter.ToInt32(recBuffer, 0);

            byte[] buffer  = new byte[recBuffer.Length - 5];
            bool   ordered = true;

            if (receivedPacketSequence < currentAnimationPacket - 5)
            {
                ordered = false;
            }
            else
            {
                Array.Copy(recBuffer, 5, buffer, 0, recBuffer.Length - 5);
                currentAnimationPacket = Math.Max(currentAnimationPacket, receivedPacketSequence);
            }

            MultiplayerFrameBufferObject currentBufferObject = new MultiplayerFrameBufferObject();

            currentBufferObject.key       = recBuffer[4] == (byte)1 ? true : false;
            currentBufferObject.animFrame = receivedPacketSequence;

            currentBufferObject.frameTime = BitConverter.ToSingle(buffer, buffer.Length - 4);

            if (this.firstFrameTime != -1f && currentBufferObject.frameTime < this.firstFrameTime)
            {
                return;
            }

            List <Vector3>    vectors     = new List <Vector3>();
            List <Quaternion> quaternions = new List <Quaternion>();

            for (int i = 0; i < 77; i++)
            {
                if (currentBufferObject.key)
                {
                    Vector3 readVector = new Vector3();
                    readVector.x = SystemHalf.HalfHelper.HalfToSingle(SystemHalf.Half.ToHalf(buffer, i * 12));
                    readVector.y = SystemHalf.HalfHelper.HalfToSingle(SystemHalf.Half.ToHalf(buffer, i * 12 + 2));
                    readVector.z = SystemHalf.HalfHelper.HalfToSingle(SystemHalf.Half.ToHalf(buffer, i * 12 + 4));

                    Quaternion readQuaternion = new Quaternion();
                    readQuaternion.eulerAngles = new Vector3(SystemHalf.HalfHelper.HalfToSingle(SystemHalf.Half.ToHalf(buffer, i * 12 + 6)),
                                                             SystemHalf.HalfHelper.HalfToSingle(SystemHalf.Half.ToHalf(buffer, i * 12 + 8)),
                                                             SystemHalf.HalfHelper.HalfToSingle(SystemHalf.Half.ToHalf(buffer, i * 12 + 10)));

                    vectors.Add(readVector);
                    quaternions.Add(readQuaternion);
                }
                else
                {
                    Vector3 readVector = new Vector3();
                    readVector.x = BitConverter.ToSingle(buffer, i * 24);
                    readVector.y = BitConverter.ToSingle(buffer, i * 24 + 4);
                    readVector.z = BitConverter.ToSingle(buffer, i * 24 + 8);

                    Quaternion readQuaternion = new Quaternion();
                    readQuaternion.eulerAngles = new Vector3(BitConverter.ToSingle(buffer, i * 24 + 12),
                                                             BitConverter.ToSingle(buffer, i * 24 + 16),
                                                             BitConverter.ToSingle(buffer, i * 24 + 20));

                    vectors.Add(readVector);
                    quaternions.Add(readQuaternion);
                }
            }

            currentBufferObject.vectors     = vectors.ToArray();
            currentBufferObject.quaternions = quaternions.ToArray();

            if (ordered && this.animationFrames.Find(f => f.animFrame == currentBufferObject.animFrame) == null)
            {
                this.animationFrames.Add(currentBufferObject);
                this.animationFrames = this.animationFrames.OrderBy(f => f.animFrame).ToList();
            }

            if ((this.replayAnimationFrames.Count > 0 && this.replayAnimationFrames[0] != null && this.replayAnimationFrames[0].animFrame > currentBufferObject.animFrame) ||
                (this.replayAnimationFrames.Count > 0 && this.replayAnimationFrames.Find(f => f.animFrame == currentBufferObject.animFrame) != null) ||
                (!currentBufferObject.key && this.replayAnimationFrames.Count < 1))
            {
                return;
            }

            if (this.startAnimTime == -1f && this.firstFrameTime == -1f && currentBufferObject.key)
            {
                this.firstFrameTime = currentBufferObject.frameTime;
                this.startAnimTime  = PlayTime.time;
            }

            if (this.replayAnimationFrames.Count > 30 * 120)
            {
                this.replayAnimationFrames.RemoveAt(0);
            }

            MultiplayerFrameBufferObject replayFrameObject = new MultiplayerFrameBufferObject {
                key         = currentBufferObject.key,
                frameTime   = currentBufferObject.frameTime,
                animFrame   = currentBufferObject.animFrame,
                vectors     = new Vector3[currentBufferObject.vectors.Length],
                quaternions = new Quaternion[currentBufferObject.quaternions.Length]
            };

            currentBufferObject.vectors.CopyTo(replayFrameObject.vectors, 0);
            currentBufferObject.quaternions.CopyTo(replayFrameObject.quaternions, 0);

            this.replayAnimationFrames.Add(replayFrameObject);
            this.replayAnimationFrames = this.replayAnimationFrames.OrderBy(f => f.animFrame).ToList();
        }
Esempio n. 13
0
        public void PrepareReplay()
        {
            this.recordedFrames.Clear();

            this.replayAnimationFrames = this.replayAnimationFrames.OrderBy(f => f.animFrame).ToList();

            if (replayAnimationFrames == null || replayAnimationFrames.Count == 0)
            {
                return;
            }

            int firstKey = this.replayAnimationFrames.FindIndex(f => f.key);

            if (firstKey < 0)
            {
                return;
            }

            this.replayAnimationFrames.RemoveRange(0, firstKey);

            MultiplayerFrameBufferObject firstRealTime, lastRealTime;

            firstRealTime = this.replayAnimationFrames.Find(f => f.realFrameTime != -1f);
            lastRealTime  = this.replayAnimationFrames.Last(f => f.realFrameTime != -1f);

            if (firstRealTime == lastRealTime)
            {
                return;
            }

            float averageFrameTime = (lastRealTime.animFrame - firstRealTime.animFrame) / (lastRealTime.realFrameTime - firstRealTime.realFrameTime);

            ReplayRecordedFrame previousFrame = null;

            for (int f = 0; f < this.replayAnimationFrames.Count; f++)
            {
                MultiplayerFrameBufferObject frame = this.replayAnimationFrames[f];

                if (f == 0)
                {
                    frame.realFrameTime = firstRealTime.realFrameTime + ((frame.animFrame - firstRealTime.animFrame) * 1f / 30f);
                }

                //if(f > 0 && !frame.key && this.replayAnimationFrames[f - 1].animFrame != frame.animFrame - 1) {
                //	continue;
                //}

                TransformInfo[] transforms = null;

                if (previousFrame != null && !frame.key)
                {
                    Vector3[]    vectors     = new Vector3[frame.vectors.Length];
                    Quaternion[] quaternions = new Quaternion[frame.vectors.Length];
                    for (int i = 0; i < frame.vectors.Length; i++)
                    {
                        vectors[i] = previousFrame.transformInfos[i].position + frame.vectors[i];
                        quaternions[i].eulerAngles = previousFrame.transformInfos[i].rotation.eulerAngles + frame.quaternions[i].eulerAngles;
                    }

                    transforms = CreateInfoArray(vectors, quaternions);
                }
                else if (frame.key)
                {
                    transforms = BufferToInfo(frame);
                }

                if (frame.realFrameTime == -1f && f > 0)
                {
                    frame.realFrameTime = this.replayAnimationFrames[f - 1].realFrameTime + (frame.animFrame - this.replayAnimationFrames[f - 1].animFrame) * (1f / 30f);
                }

                if (transforms != null)
                {
                    this.recordedFrames.Add(new ReplayRecordedFrame(transforms, frame.realFrameTime));

                    previousFrame = this.recordedFrames.Last();
                }
            }

            FinalizeReplay();
        }