diff --git a/osu.Game.Modes.Osu/OsuAutoReplay.cs b/osu.Game.Modes.Osu/OsuAutoReplay.cs index 86985c834a..ae85bd72d8 100644 --- a/osu.Game.Modes.Osu/OsuAutoReplay.cs +++ b/osu.Game.Modes.Osu/OsuAutoReplay.cs @@ -11,10 +11,11 @@ using System.Collections.Generic; using System.Diagnostics; using osu.Framework.Graphics; using osu.Game.Modes.Objects.Types; +using osu.Game.Modes.Replays; namespace osu.Game.Modes.Osu { - public class OsuAutoReplay : LegacyReplay + public class OsuAutoReplay : Replay { private static readonly Vector2 spinner_centre = new Vector2(256, 192); @@ -29,17 +30,20 @@ namespace osu.Game.Modes.Osu createAutoReplay(); } - private class LegacyReplayFrameComparer : IComparer + private class ReplayFrameComparer : IComparer { - public int Compare(LegacyReplayFrame f1, LegacyReplayFrame f2) + public int Compare(ReplayFrame f1, ReplayFrame f2) { + if (f1 == null) throw new NullReferenceException($@"{nameof(f1)} cannot be null"); + if (f2 == null) throw new NullReferenceException($@"{nameof(f2)} cannot be null"); + return f1.Time.CompareTo(f2.Time); } } - private static readonly IComparer replay_frame_comparer = new LegacyReplayFrameComparer(); + private static readonly IComparer replay_frame_comparer = new ReplayFrameComparer(); - private int findInsertionIndex(LegacyReplayFrame frame) + private int findInsertionIndex(ReplayFrame frame) { int index = Frames.BinarySearch(frame, replay_frame_comparer); @@ -59,7 +63,7 @@ namespace osu.Game.Modes.Osu return index; } - private void addFrameToReplay(LegacyReplayFrame frame) => Frames.Insert(findInsertionIndex(frame), frame); + private void addFrameToReplay(ReplayFrame frame) => Frames.Insert(findInsertionIndex(frame), frame); private static Vector2 circlePosition(double t, double radius) => new Vector2((float)(Math.Cos(t) * radius), (float)(Math.Sin(t) * radius)); @@ -74,9 +78,9 @@ namespace osu.Game.Modes.Osu EasingTypes preferredEasing = DelayedMovements ? EasingTypes.InOutCubic : EasingTypes.Out; - addFrameToReplay(new LegacyReplayFrame(-100000, 256, 500, LegacyButtonState.None)); - addFrameToReplay(new LegacyReplayFrame(beatmap.HitObjects[0].StartTime - 1500, 256, 500, LegacyButtonState.None)); - addFrameToReplay(new LegacyReplayFrame(beatmap.HitObjects[0].StartTime - 1000, 256, 192, LegacyButtonState.None)); + addFrameToReplay(new ReplayFrame(-100000, 256, 500, ReplayButtonState.None)); + addFrameToReplay(new ReplayFrame(beatmap.HitObjects[0].StartTime - 1500, 256, 500, ReplayButtonState.None)); + addFrameToReplay(new ReplayFrame(beatmap.HitObjects[0].StartTime - 1000, 256, 192, ReplayButtonState.None)); // We are using ApplyModsToRate and not ApplyModsToTime to counteract the speed up / slow down from HalfTime / DoubleTime so that we remain at a constant framerate of 60 fps. float frameDelay = (float)applyModsToRate(1000.0 / 60.0); @@ -106,18 +110,18 @@ namespace osu.Game.Modes.Osu //Make the cursor stay at a hitObject as long as possible (mainly for autopilot). if (h.StartTime - h.HitWindowFor(OsuScoreResult.Miss) > endTime + h.HitWindowFor(OsuScoreResult.Hit50) + 50) { - if (!(last is Spinner) && h.StartTime - endTime < 1000) addFrameToReplay(new LegacyReplayFrame(endTime + h.HitWindowFor(OsuScoreResult.Hit50), last.EndPosition.X, last.EndPosition.Y, LegacyButtonState.None)); - if (!(h is Spinner)) addFrameToReplay(new LegacyReplayFrame(h.StartTime - h.HitWindowFor(OsuScoreResult.Miss), h.Position.X, h.Position.Y, LegacyButtonState.None)); + if (!(last is Spinner) && h.StartTime - endTime < 1000) addFrameToReplay(new ReplayFrame(endTime + h.HitWindowFor(OsuScoreResult.Hit50), last.EndPosition.X, last.EndPosition.Y, ReplayButtonState.None)); + if (!(h is Spinner)) addFrameToReplay(new ReplayFrame(h.StartTime - h.HitWindowFor(OsuScoreResult.Miss), h.Position.X, h.Position.Y, ReplayButtonState.None)); } else if (h.StartTime - h.HitWindowFor(OsuScoreResult.Hit50) > endTime + h.HitWindowFor(OsuScoreResult.Hit50) + 50) { - if (!(last is Spinner) && h.StartTime - endTime < 1000) addFrameToReplay(new LegacyReplayFrame(endTime + h.HitWindowFor(OsuScoreResult.Hit50), last.EndPosition.X, last.EndPosition.Y, LegacyButtonState.None)); - if (!(h is Spinner)) addFrameToReplay(new LegacyReplayFrame(h.StartTime - h.HitWindowFor(OsuScoreResult.Hit50), h.Position.X, h.Position.Y, LegacyButtonState.None)); + if (!(last is Spinner) && h.StartTime - endTime < 1000) addFrameToReplay(new ReplayFrame(endTime + h.HitWindowFor(OsuScoreResult.Hit50), last.EndPosition.X, last.EndPosition.Y, ReplayButtonState.None)); + if (!(h is Spinner)) addFrameToReplay(new ReplayFrame(h.StartTime - h.HitWindowFor(OsuScoreResult.Hit50), h.Position.X, h.Position.Y, ReplayButtonState.None)); } else if (h.StartTime - h.HitWindowFor(OsuScoreResult.Hit100) > endTime + h.HitWindowFor(OsuScoreResult.Hit100) + 50) { - if (!(last is Spinner) && h.StartTime - endTime < 1000) addFrameToReplay(new LegacyReplayFrame(endTime + h.HitWindowFor(OsuScoreResult.Hit100), last.EndPosition.X, last.EndPosition.Y, LegacyButtonState.None)); - if (!(h is Spinner)) addFrameToReplay(new LegacyReplayFrame(h.StartTime - h.HitWindowFor(OsuScoreResult.Hit100), h.Position.X, h.Position.Y, LegacyButtonState.None)); + if (!(last is Spinner) && h.StartTime - endTime < 1000) addFrameToReplay(new ReplayFrame(endTime + h.HitWindowFor(OsuScoreResult.Hit100), last.EndPosition.X, last.EndPosition.Y, ReplayButtonState.None)); + if (!(h is Spinner)) addFrameToReplay(new ReplayFrame(h.StartTime - h.HitWindowFor(OsuScoreResult.Hit100), h.Position.X, h.Position.Y, ReplayButtonState.None)); } } @@ -173,13 +177,13 @@ namespace osu.Game.Modes.Osu // Do some nice easing for cursor movements if (Frames.Count > 0) { - LegacyReplayFrame lastFrame = Frames[Frames.Count - 1]; + ReplayFrame lastFrame = Frames[Frames.Count - 1]; // Wait until Auto could "see and react" to the next note. double waitTime = h.StartTime - Math.Max(0.0, DrawableOsuHitObject.TIME_PREEMPT - reactionTime); if (waitTime > lastFrame.Time) { - lastFrame = new LegacyReplayFrame(waitTime, lastFrame.MouseX, lastFrame.MouseY, lastFrame.ButtonState); + lastFrame = new ReplayFrame(waitTime, lastFrame.MouseX, lastFrame.MouseY, lastFrame.ButtonState); addFrameToReplay(lastFrame); } @@ -196,7 +200,7 @@ namespace osu.Game.Modes.Osu for (double time = lastFrame.Time + frameDelay; time < h.StartTime; time += frameDelay) { Vector2 currentPosition = Interpolation.ValueAt(time, lastPosition, targetPosition, lastFrame.Time, h.StartTime, easing); - addFrameToReplay(new LegacyReplayFrame((int)time, currentPosition.X, currentPosition.Y, lastFrame.ButtonState)); + addFrameToReplay(new ReplayFrame((int)time, currentPosition.X, currentPosition.Y, lastFrame.ButtonState)); } buttonIndex = 0; @@ -207,12 +211,12 @@ namespace osu.Game.Modes.Osu } } - LegacyButtonState button = buttonIndex % 2 == 0 ? LegacyButtonState.Left1 : LegacyButtonState.Right1; + ReplayButtonState button = buttonIndex % 2 == 0 ? ReplayButtonState.Left1 : ReplayButtonState.Right1; double hEndTime = (h as IHasEndTime)?.EndTime ?? h.StartTime; - LegacyReplayFrame newFrame = new LegacyReplayFrame(h.StartTime, targetPosition.X, targetPosition.Y, button); - LegacyReplayFrame endFrame = new LegacyReplayFrame(hEndTime + endDelay, h.EndPosition.X, h.EndPosition.Y, LegacyButtonState.None); + ReplayFrame newFrame = new ReplayFrame(h.StartTime, targetPosition.X, targetPosition.Y, button); + ReplayFrame endFrame = new ReplayFrame(hEndTime + endDelay, h.EndPosition.X, h.EndPosition.Y, ReplayButtonState.None); // Decrement because we want the previous frame, not the next one int index = findInsertionIndex(newFrame) - 1; @@ -220,19 +224,19 @@ namespace osu.Game.Modes.Osu // Do we have a previous frame? No need to check for < replay.Count since we decremented! if (index >= 0) { - LegacyReplayFrame previousFrame = Frames[index]; + ReplayFrame previousFrame = Frames[index]; var previousButton = previousFrame.ButtonState; // If a button is already held, then we simply alternate - if (previousButton != LegacyButtonState.None) + if (previousButton != ReplayButtonState.None) { - Debug.Assert(previousButton != (LegacyButtonState.Left1 | LegacyButtonState.Right1)); + Debug.Assert(previousButton != (ReplayButtonState.Left1 | ReplayButtonState.Right1)); // Force alternation if we have the same button. Otherwise we can just keep the naturally to us assigned button. if (previousButton == button) { - button = (LegacyButtonState.Left1 | LegacyButtonState.Right1) & ~button; - newFrame.SetButtonStates(button); + button = (ReplayButtonState.Left1 | ReplayButtonState.Right1) & ~button; + newFrame.ButtonState = button; } // We always follow the most recent slider / spinner, so remove any other frames that occur while it exists. @@ -246,7 +250,7 @@ namespace osu.Game.Modes.Osu { // Don't affect frames which stop pressing a button! if (j < Frames.Count - 1 || Frames[j].ButtonState == previousButton) - Frames[j].SetButtonStates(button); + Frames[j].ButtonState = button; } } } @@ -270,13 +274,13 @@ namespace osu.Game.Modes.Osu t = applyModsToTime(j - h.StartTime) * spinnerDirection; Vector2 pos = spinner_centre + circlePosition(t / 20 + angle, spin_radius); - addFrameToReplay(new LegacyReplayFrame((int)j, pos.X, pos.Y, button)); + addFrameToReplay(new ReplayFrame((int)j, pos.X, pos.Y, button)); } t = applyModsToTime(s.EndTime - h.StartTime) * spinnerDirection; Vector2 endPosition = spinner_centre + circlePosition(t / 20 + angle, spin_radius); - addFrameToReplay(new LegacyReplayFrame(s.EndTime, endPosition.X, endPosition.Y, button)); + addFrameToReplay(new ReplayFrame(s.EndTime, endPosition.X, endPosition.Y, button)); endFrame.MouseX = endPosition.X; endFrame.MouseY = endPosition.Y; @@ -288,10 +292,10 @@ namespace osu.Game.Modes.Osu for (double j = frameDelay; j < s.Duration; j += frameDelay) { Vector2 pos = s.PositionAt(j / s.Duration); - addFrameToReplay(new LegacyReplayFrame(h.StartTime + j, pos.X, pos.Y, button)); + addFrameToReplay(new ReplayFrame(h.StartTime + j, pos.X, pos.Y, button)); } - addFrameToReplay(new LegacyReplayFrame(s.EndTime, s.EndPosition.X, s.EndPosition.Y, button)); + addFrameToReplay(new ReplayFrame(s.EndTime, s.EndPosition.X, s.EndPosition.Y, button)); } // We only want to let go of our button if we are at the end of the current replay. Otherwise something is still going on after us so we need to keep the button pressed! diff --git a/osu.Game/Database/ScoreDatabase.cs b/osu.Game/Database/ScoreDatabase.cs index 096c0dcc29..5ce3ff273e 100644 --- a/osu.Game/Database/ScoreDatabase.cs +++ b/osu.Game/Database/ScoreDatabase.cs @@ -101,7 +101,7 @@ namespace osu.Game.Database using (var lzma = new LzmaStream(properties, replayInStream, compressedSize, outSize)) using (var reader = new StreamReader(lzma)) - score.Replay = score.CreateLegacyReplayFrom(reader); + score.Replay = score.CreateReplay(reader); } } diff --git a/osu.Game/Modes/LegacyReplay.cs b/osu.Game/Modes/LegacyReplay.cs deleted file mode 100644 index d57d4a15d2..0000000000 --- a/osu.Game/Modes/LegacyReplay.cs +++ /dev/null @@ -1,268 +0,0 @@ -// Copyright (c) 2007-2017 ppy Pty Ltd . -// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE - -using System; -using System.Collections.Generic; -using System.IO; -using osu.Framework.Extensions.IEnumerableExtensions; -using osu.Framework.Input; -using osu.Framework.MathUtils; -using osu.Game.Input.Handlers; -using osu.Game.IO.Legacy; -using OpenTK; -using OpenTK.Input; -using KeyboardState = osu.Framework.Input.KeyboardState; -using MouseState = osu.Framework.Input.MouseState; - -namespace osu.Game.Modes -{ - public class LegacyReplay : Replay - { - protected List Frames = new List(); - - protected LegacyReplay() - { - - } - - public LegacyReplay(StreamReader reader) - { - float lastTime = 0; - - foreach (var l in reader.ReadToEnd().Split(',')) - { - var split = l.Split('|'); - - if (split.Length < 4 || float.Parse(split[0]) < 0) continue; - - lastTime += float.Parse(split[0]); - - Frames.Add(new LegacyReplayFrame( - lastTime, - float.Parse(split[1]), - 384 - float.Parse(split[2]), - (LegacyButtonState)int.Parse(split[3]) - )); - } - } - - public override ReplayInputHandler CreateInputHandler() => new LegacyReplayInputHandler(Frames); - - /// - /// The ReplayHandler will take a replay and handle the propagation of updates to the input stack. - /// It handles logic of any frames which *must* be executed. - /// - protected class LegacyReplayInputHandler : ReplayInputHandler - { - private readonly List replayContent; - - public LegacyReplayFrame CurrentFrame => !hasFrames ? null : replayContent[currentFrameIndex]; - public LegacyReplayFrame NextFrame => !hasFrames ? null : replayContent[nextFrameIndex]; - - private int currentFrameIndex; - - private int nextFrameIndex => MathHelper.Clamp(currentFrameIndex + (currentDirection > 0 ? 1 : -1), 0, replayContent.Count - 1); - - public LegacyReplayInputHandler(List replayContent) - { - this.replayContent = replayContent; - } - - private bool advanceFrame() - { - int newFrame = nextFrameIndex; - - //ensure we aren't at an extent. - if (newFrame == currentFrameIndex) return false; - - currentFrameIndex = newFrame; - return true; - } - - public void SetPosition(Vector2 pos) - { - } - - private Vector2? position - { - get - { - if (!hasFrames) - return null; - - return Interpolation.ValueAt(currentTime, CurrentFrame.Position, NextFrame.Position, CurrentFrame.Time, NextFrame.Time); - } - } - - public override List GetPendingStates() - { - var buttons = new HashSet(); - if (CurrentFrame?.MouseLeft ?? false) - buttons.Add(MouseButton.Left); - if (CurrentFrame?.MouseRight ?? false) - buttons.Add(MouseButton.Right); - - return new List - { - new InputState - { - Mouse = new ReplayMouseState(ToScreenSpace(position ?? Vector2.Zero), buttons), - Keyboard = new ReplayKeyboardState(new List()) - } - }; - } - - public bool AtLastFrame => currentFrameIndex == replayContent.Count - 1; - public bool AtFirstFrame => currentFrameIndex == 0; - - public Vector2 Size => new Vector2(512, 384); - - private const double sixty_frame_time = 1000.0 / 60; - - private double currentTime; - private int currentDirection; - - /// - /// When set, we will ensure frames executed by nested drawables are frame-accurate to replay data. - /// Disabling this can make replay playback smoother (useful for autoplay, currently). - /// - public bool FrameAccuratePlayback = true; - - private bool hasFrames => replayContent.Count > 0; - - private bool inImportantSection => - FrameAccuratePlayback && - //a button is in a pressed state - (currentDirection > 0 ? CurrentFrame : NextFrame)?.ButtonState > LegacyButtonState.None && - //the next frame is within an allowable time span - Math.Abs(currentTime - NextFrame?.Time ?? 0) <= sixty_frame_time * 1.2; - - /// - /// Update the current frame based on an incoming time value. - /// There are cases where we return a "must-use" time value that is different from the input. - /// This is to ensure accurate playback of replay data. - /// - /// The time which we should use for finding the current frame. - /// The usable time value. If null, we should not advance time as we do not have enough data. - public override double? SetFrameFromTime(double time) - { - currentDirection = time.CompareTo(currentTime); - if (currentDirection == 0) currentDirection = 1; - - if (hasFrames) - { - //if we changed frames, we want to execute once *exactly* on the frame's time. - if (currentDirection == time.CompareTo(NextFrame.Time) && advanceFrame()) - return currentTime = CurrentFrame.Time; - - //if we didn't change frames, we need to ensure we are allowed to run frames in between, else return null. - if (inImportantSection) - return null; - } - - return currentTime = time; - } - - protected class ReplayMouseState : MouseState - { - public ReplayMouseState(Vector2 position, IEnumerable list) - { - Position = position; - list.ForEach(b => PressedButtons.Add(b)); - } - } - - protected class ReplayKeyboardState : KeyboardState - { - public ReplayKeyboardState(List keys) - { - Keys = keys; - } - } - } - - [Flags] - protected enum LegacyButtonState - { - None = 0, - Left1 = 1, - Right1 = 2, - Left2 = 4, - Right2 = 8, - Smoke = 16 - } - - protected class LegacyReplayFrame - { - public Vector2 Position => new Vector2(MouseX, MouseY); - - public float MouseX; - public float MouseY; - public bool MouseLeft; - public bool MouseRight; - public bool MouseLeft1; - public bool MouseRight1; - public bool MouseLeft2; - public bool MouseRight2; - public LegacyButtonState ButtonState; - public double Time; - - public LegacyReplayFrame(double time, float posX, float posY, LegacyButtonState buttonState) - { - MouseX = posX; - MouseY = posY; - ButtonState = buttonState; - SetButtonStates(buttonState); - Time = time; - } - - public void SetButtonStates(LegacyButtonState buttonState) - { - ButtonState = buttonState; - MouseLeft = (buttonState & (LegacyButtonState.Left1 | LegacyButtonState.Left2)) > 0; - MouseLeft1 = (buttonState & LegacyButtonState.Left1) > 0; - MouseLeft2 = (buttonState & LegacyButtonState.Left2) > 0; - MouseRight = (buttonState & (LegacyButtonState.Right1 | LegacyButtonState.Right2)) > 0; - MouseRight1 = (buttonState & LegacyButtonState.Right1) > 0; - MouseRight2 = (buttonState & LegacyButtonState.Right2) > 0; - } - - public LegacyReplayFrame(Stream s) : this(new SerializationReader(s)) - { - } - - public LegacyReplayFrame(SerializationReader sr) - { - ButtonState = (LegacyButtonState)sr.ReadByte(); - SetButtonStates(ButtonState); - - byte bt = sr.ReadByte(); - if (bt > 0)//Handle Pre-Taiko compatible replays. - SetButtonStates(LegacyButtonState.Right1); - - MouseX = sr.ReadSingle(); - MouseY = sr.ReadSingle(); - Time = sr.ReadInt32(); - } - - public void ReadFromStream(SerializationReader sr) - { - throw new NotImplementedException(); - } - - public void WriteToStream(SerializationWriter sw) - { - sw.Write((byte)ButtonState); - sw.Write((byte)0); - sw.Write(MouseX); - sw.Write(MouseY); - sw.Write(Time); - } - - public override string ToString() - { - return $"{Time}\t({MouseX},{MouseY})\t{MouseLeft}\t{MouseRight}\t{MouseLeft1}\t{MouseRight1}\t{MouseLeft2}\t{MouseRight2}\t{ButtonState}"; - } - } - } -} diff --git a/osu.Game/Modes/Mods/Mod.cs b/osu.Game/Modes/Mods/Mod.cs index c53c6faa21..b6f09b8506 100644 --- a/osu.Game/Modes/Mods/Mod.cs +++ b/osu.Game/Modes/Mods/Mod.cs @@ -157,7 +157,7 @@ namespace osu.Game.Modes.Mods public void Apply(HitRenderer hitRenderer) { - hitRenderer.InputManager.ReplayInputHandler = CreateReplayScore(hitRenderer.Beatmap)?.Replay?.CreateInputHandler(); + hitRenderer.SetReplay(CreateReplayScore(hitRenderer.Beatmap)?.Replay); } } diff --git a/osu.Game/Modes/Replay.cs b/osu.Game/Modes/Replay.cs deleted file mode 100644 index 6d93afdeb9..0000000000 --- a/osu.Game/Modes/Replay.cs +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright (c) 2007-2017 ppy Pty Ltd . -// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE - -using osu.Game.Input.Handlers; - -namespace osu.Game.Modes -{ - public abstract class Replay - { - public virtual ReplayInputHandler CreateInputHandler() => null; - } -} \ No newline at end of file diff --git a/osu.Game/Modes/Replays/FramedReplayInputHandler.cs b/osu.Game/Modes/Replays/FramedReplayInputHandler.cs new file mode 100644 index 0000000000..ae20ece515 --- /dev/null +++ b/osu.Game/Modes/Replays/FramedReplayInputHandler.cs @@ -0,0 +1,151 @@ +// Copyright (c) 2007-2017 ppy Pty Ltd . +// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using System; +using System.Collections.Generic; +using osu.Framework.Extensions.IEnumerableExtensions; +using osu.Framework.Input; +using osu.Framework.MathUtils; +using osu.Game.Input.Handlers; +using OpenTK; +using OpenTK.Input; +using KeyboardState = osu.Framework.Input.KeyboardState; +using MouseState = osu.Framework.Input.MouseState; + +namespace osu.Game.Modes.Replays +{ + /// + /// The ReplayHandler will take a replay and handle the propagation of updates to the input stack. + /// It handles logic of any frames which *must* be executed. + /// + public class FramedReplayInputHandler : ReplayInputHandler + { + private readonly Replay replay; + + protected List Frames => replay.Frames; + + public ReplayFrame CurrentFrame => !hasFrames ? null : Frames[currentFrameIndex]; + public ReplayFrame NextFrame => !hasFrames ? null : Frames[nextFrameIndex]; + + private int currentFrameIndex; + + private int nextFrameIndex => MathHelper.Clamp(currentFrameIndex + (currentDirection > 0 ? 1 : -1), 0, Frames.Count - 1); + + public FramedReplayInputHandler(Replay replay) + { + this.replay = replay; + } + + private bool advanceFrame() + { + int newFrame = nextFrameIndex; + + //ensure we aren't at an extent. + if (newFrame == currentFrameIndex) return false; + + currentFrameIndex = newFrame; + return true; + } + + public void SetPosition(Vector2 pos) + { + } + + private Vector2? position + { + get + { + if (!hasFrames) + return null; + + return Interpolation.ValueAt(currentTime, CurrentFrame.Position, NextFrame.Position, CurrentFrame.Time, NextFrame.Time); + } + } + + public override List GetPendingStates() + { + var buttons = new HashSet(); + if (CurrentFrame?.MouseLeft ?? false) + buttons.Add(MouseButton.Left); + if (CurrentFrame?.MouseRight ?? false) + buttons.Add(MouseButton.Right); + + return new List + { + new InputState + { + Mouse = new ReplayMouseState(ToScreenSpace(position ?? Vector2.Zero), buttons), + Keyboard = new ReplayKeyboardState(new List()) + } + }; + } + + public bool AtLastFrame => currentFrameIndex == Frames.Count - 1; + public bool AtFirstFrame => currentFrameIndex == 0; + + public Vector2 Size => new Vector2(512, 384); + + private const double sixty_frame_time = 1000.0 / 60; + + private double currentTime; + private int currentDirection; + + /// + /// When set, we will ensure frames executed by nested drawables are frame-accurate to replay data. + /// Disabling this can make replay playback smoother (useful for autoplay, currently). + /// + public bool FrameAccuratePlayback = true; + + private bool hasFrames => Frames.Count > 0; + + private bool inImportantSection => + FrameAccuratePlayback && + //a button is in a pressed state + ((currentDirection > 0 ? CurrentFrame : NextFrame)?.IsImportant ?? false) && + //the next frame is within an allowable time span + Math.Abs(currentTime - NextFrame?.Time ?? 0) <= sixty_frame_time * 1.2; + + /// + /// Update the current frame based on an incoming time value. + /// There are cases where we return a "must-use" time value that is different from the input. + /// This is to ensure accurate playback of replay data. + /// + /// The time which we should use for finding the current frame. + /// The usable time value. If null, we should not advance time as we do not have enough data. + public override double? SetFrameFromTime(double time) + { + currentDirection = time.CompareTo(currentTime); + if (currentDirection == 0) currentDirection = 1; + + if (hasFrames) + { + //if we changed frames, we want to execute once *exactly* on the frame's time. + if (currentDirection == time.CompareTo(NextFrame.Time) && advanceFrame()) + return currentTime = CurrentFrame.Time; + + //if we didn't change frames, we need to ensure we are allowed to run frames in between, else return null. + if (inImportantSection) + return null; + } + + return currentTime = time; + } + + protected class ReplayMouseState : MouseState + { + public ReplayMouseState(Vector2 position, IEnumerable list) + { + Position = position; + list.ForEach(b => PressedButtons.Add(b)); + } + } + + protected class ReplayKeyboardState : KeyboardState + { + public ReplayKeyboardState(List keys) + { + Keys = keys; + } + } + } +} \ No newline at end of file diff --git a/osu.Game/Modes/Replays/Replay.cs b/osu.Game/Modes/Replays/Replay.cs new file mode 100644 index 0000000000..62f60358e0 --- /dev/null +++ b/osu.Game/Modes/Replays/Replay.cs @@ -0,0 +1,12 @@ +// Copyright (c) 2007-2017 ppy Pty Ltd . +// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using System.Collections.Generic; + +namespace osu.Game.Modes.Replays +{ + public class Replay + { + public List Frames = new List(); + } +} \ No newline at end of file diff --git a/osu.Game/Modes/Replays/ReplayButtonState.cs b/osu.Game/Modes/Replays/ReplayButtonState.cs new file mode 100644 index 0000000000..d49139226c --- /dev/null +++ b/osu.Game/Modes/Replays/ReplayButtonState.cs @@ -0,0 +1,18 @@ +// Copyright (c) 2007-2017 ppy Pty Ltd . +// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using System; + +namespace osu.Game.Modes.Replays +{ + [Flags] + public enum ReplayButtonState + { + None = 0, + Left1 = 1, + Right1 = 2, + Left2 = 4, + Right2 = 8, + Smoke = 16 + } +} \ No newline at end of file diff --git a/osu.Game/Modes/Replays/ReplayFrame.cs b/osu.Game/Modes/Replays/ReplayFrame.cs new file mode 100644 index 0000000000..4cd681beaf --- /dev/null +++ b/osu.Game/Modes/Replays/ReplayFrame.cs @@ -0,0 +1,71 @@ +// Copyright (c) 2007-2017 ppy Pty Ltd . +// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using OpenTK; + +namespace osu.Game.Modes.Replays +{ + public class ReplayFrame + { + public Vector2 Position => new Vector2(MouseX, MouseY); + + public bool IsImportant => MouseLeft || MouseRight; + + public float MouseX; + public float MouseY; + + public bool MouseLeft => MouseLeft1 || MouseLeft2; + public bool MouseRight => MouseRight1 || MouseRight2; + + public bool MouseLeft1 + { + get { return (ButtonState & ReplayButtonState.Left1) > 0; } + set { setButtonState(ReplayButtonState.Left1, value); } + } + public bool MouseRight1 + { + get { return (ButtonState & ReplayButtonState.Right1) > 0; } + set { setButtonState(ReplayButtonState.Right1, value); } + } + public bool MouseLeft2 + { + get { return (ButtonState & ReplayButtonState.Left2) > 0; } + set { setButtonState(ReplayButtonState.Left2, value); } + } + public bool MouseRight2 + { + get { return (ButtonState & ReplayButtonState.Right2) > 0; } + set { setButtonState(ReplayButtonState.Right2, value); } + } + + private void setButtonState(ReplayButtonState singleButton, bool pressed) + { + if (pressed) + ButtonState |= singleButton; + else + ButtonState &= ~singleButton; + } + + public double Time; + + public ReplayButtonState ButtonState; + + protected ReplayFrame() + { + + } + + public ReplayFrame(double time, float posX, float posY, ReplayButtonState buttonState) + { + MouseX = posX; + MouseY = posY; + ButtonState = buttonState; + Time = time; + } + + public override string ToString() + { + return $"{Time}\t({MouseX},{MouseY})\t{MouseLeft}\t{MouseRight}\t{MouseLeft1}\t{MouseRight1}\t{MouseLeft2}\t{MouseRight2}\t{ButtonState}"; + } + } +} \ No newline at end of file diff --git a/osu.Game/Modes/Scoring/Score.cs b/osu.Game/Modes/Scoring/Score.cs index 75c243278d..c998b11f77 100644 --- a/osu.Game/Modes/Scoring/Score.cs +++ b/osu.Game/Modes/Scoring/Score.cs @@ -2,11 +2,13 @@ // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE using System; +using System.Collections.Generic; using Newtonsoft.Json; using osu.Game.Database; using osu.Game.Modes.Mods; using osu.Game.Users; using System.IO; +using osu.Game.Modes.Replays; namespace osu.Game.Modes.Scoring { @@ -45,11 +47,34 @@ namespace osu.Game.Modes.Scoring public DateTime Date; /// - /// Creates a legacy replay which is read from a stream. + /// Creates a replay which is read from a stream. /// /// The stream reader. /// The replay. - public virtual Replay CreateLegacyReplayFrom(StreamReader reader) => new LegacyReplay(reader); + public virtual Replay CreateReplay(StreamReader reader) + { + var frames = new List(); + + float lastTime = 0; + + foreach (var l in reader.ReadToEnd().Split(',')) + { + var split = l.Split('|'); + + if (split.Length < 4 || float.Parse(split[0]) < 0) continue; + + lastTime += float.Parse(split[0]); + + frames.Add(new ReplayFrame( + lastTime, + float.Parse(split[1]), + 384 - float.Parse(split[2]), + (ReplayButtonState)int.Parse(split[3]) + )); + } + + return new Replay { Frames = frames }; + } // [JsonProperty(@"count50")] 0, //[JsonProperty(@"count100")] 0, diff --git a/osu.Game/Modes/UI/HitRenderer.cs b/osu.Game/Modes/UI/HitRenderer.cs index 3d108b895d..8c1e495c7a 100644 --- a/osu.Game/Modes/UI/HitRenderer.cs +++ b/osu.Game/Modes/UI/HitRenderer.cs @@ -14,6 +14,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; +using osu.Game.Modes.Replays; using osu.Game.Modes.Scoring; namespace osu.Game.Modes.UI @@ -68,6 +69,14 @@ namespace osu.Game.Modes.UI /// /// The input manager. protected virtual KeyConversionInputManager CreateKeyConversionInputManager() => new KeyConversionInputManager(); + + protected virtual FramedReplayInputHandler CreateReplayInputHandler(Replay replay) => new FramedReplayInputHandler(replay); + + /// + /// Sets a replay to be used, overriding local input. + /// + /// The replay, null for local input. + public void SetReplay(Replay replay) => InputManager.ReplayInputHandler = replay != null ? CreateReplayInputHandler(replay) : null; } /// diff --git a/osu.Game/OsuGame.cs b/osu.Game/OsuGame.cs index 66196670b8..8ac86c5c67 100644 --- a/osu.Game/OsuGame.cs +++ b/osu.Game/OsuGame.cs @@ -125,7 +125,7 @@ namespace osu.Game Beatmap.Value = BeatmapDatabase.GetWorkingBeatmap(s.Beatmap); - menu.Push(new PlayerLoader(new Player { ReplayInputHandler = s.Replay.CreateInputHandler() })); + menu.Push(new PlayerLoader(new ReplayPlayer(s.Replay))); } protected override void LoadComplete() diff --git a/osu.Game/Screens/Play/Player.cs b/osu.Game/Screens/Play/Player.cs index 0554c0e77b..73d397b24b 100644 --- a/osu.Game/Screens/Play/Player.cs +++ b/osu.Game/Screens/Play/Player.cs @@ -15,7 +15,6 @@ using osu.Framework.Screens; using osu.Framework.Timing; using osu.Game.Configuration; using osu.Game.Database; -using osu.Game.Input.Handlers; using osu.Game.Modes; using osu.Game.Modes.UI; using osu.Game.Screens.Backgrounds; @@ -34,7 +33,7 @@ namespace osu.Game.Screens.Play internal override bool HasLocalCursorDisplayed => !hasReplayLoaded && !IsPaused; - private bool hasReplayLoaded => hitRenderer.InputManager.ReplayInputHandler != null; + private bool hasReplayLoaded => HitRenderer.InputManager.ReplayInputHandler != null; public BeatmapInfo BeatmapInfo; @@ -53,7 +52,7 @@ namespace osu.Game.Screens.Play private Ruleset ruleset; private ScoreProcessor scoreProcessor; - private HitRenderer hitRenderer; + protected HitRenderer HitRenderer; private Bindable dimLevel; private SkipButton skipButton; @@ -112,9 +111,9 @@ namespace osu.Game.Screens.Play }); ruleset = Ruleset.GetRuleset(Beatmap.PlayMode); - hitRenderer = ruleset.CreateHitRendererWith(Beatmap); + HitRenderer = ruleset.CreateHitRendererWith(Beatmap); - scoreProcessor = hitRenderer.CreateScoreProcessor(); + scoreProcessor = HitRenderer.CreateScoreProcessor(); hudOverlay = new StandardHudOverlay(); hudOverlay.KeyCounter.Add(ruleset.CreateGameplayKeys()); @@ -133,13 +132,10 @@ namespace osu.Game.Screens.Play }; - if (ReplayInputHandler != null) - hitRenderer.InputManager.ReplayInputHandler = ReplayInputHandler; - - hudOverlay.BindHitRenderer(hitRenderer); + hudOverlay.BindHitRenderer(HitRenderer); //bind HitRenderer to ScoreProcessor and ourselves (for a pass situation) - hitRenderer.OnAllJudged += onCompletion; + HitRenderer.OnAllJudged += onCompletion; //bind ScoreProcessor to ourselves (for a fail situation) scoreProcessor.Failed += onFail; @@ -152,7 +148,7 @@ namespace osu.Game.Screens.Play Clock = interpolatedSourceClock, Children = new Drawable[] { - hitRenderer, + HitRenderer, skipButton = new SkipButton { Alpha = 0 @@ -336,8 +332,6 @@ namespace osu.Game.Screens.Play private Bindable mouseWheelDisabled; - public ReplayInputHandler ReplayInputHandler; - protected override bool OnWheel(InputState state) => mouseWheelDisabled.Value && !IsPaused; } } \ No newline at end of file diff --git a/osu.Game/Screens/Play/ReplayPlayer.cs b/osu.Game/Screens/Play/ReplayPlayer.cs new file mode 100644 index 0000000000..4593656a2e --- /dev/null +++ b/osu.Game/Screens/Play/ReplayPlayer.cs @@ -0,0 +1,23 @@ +// Copyright (c) 2007-2017 ppy Pty Ltd . +// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using osu.Game.Modes.Replays; + +namespace osu.Game.Screens.Play +{ + public class ReplayPlayer : Player + { + public Replay Replay; + + public ReplayPlayer(Replay replay) + { + Replay = replay; + } + + protected override void LoadComplete() + { + base.LoadComplete(); + HitRenderer.SetReplay(Replay); + } + } +} diff --git a/osu.Game/osu.Game.csproj b/osu.Game/osu.Game.csproj index b2f76137fd..ca411a0233 100644 --- a/osu.Game/osu.Game.csproj +++ b/osu.Game/osu.Game.csproj @@ -98,8 +98,9 @@ + - + @@ -125,7 +126,8 @@ - + + @@ -200,6 +202,7 @@ +