1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-17 19:18:03 +08:00
osu-lazer/osu.Game/Modes/LegacyReplay.cs

269 lines
9.8 KiB
C#
Raw Normal View History

2017-03-04 18:02:36 +08:00
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Collections.Generic;
using System.IO;
2017-03-13 18:11:57 +08:00
using osu.Framework.Extensions.IEnumerableExtensions;
2017-03-04 18:02:36 +08:00
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<LegacyReplayFrame> Frames = new List<LegacyReplayFrame>();
protected LegacyReplay()
{
}
2017-03-04 18:02:36 +08:00
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(
2017-03-04 18:02:36 +08:00
lastTime,
float.Parse(split[1]),
384 - float.Parse(split[2]),
(LegacyButtonState)int.Parse(split[3])
));
}
}
2017-03-24 12:59:05 +08:00
public override ReplayInputHandler CreateInputHandler() => new LegacyReplayInputHandler(Frames);
2017-03-04 18:02:36 +08:00
/// <summary>
/// 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.
/// </summary>
protected class LegacyReplayInputHandler : ReplayInputHandler
2017-03-04 18:02:36 +08:00
{
private readonly List<LegacyReplayFrame> replayContent;
public LegacyReplayFrame CurrentFrame => !hasFrames ? null : replayContent[currentFrameIndex];
2017-03-06 13:18:44 +08:00
public LegacyReplayFrame NextFrame => !hasFrames ? null : replayContent[nextFrameIndex];
2017-03-09 12:55:29 +08:00
private int currentFrameIndex;
2017-03-06 13:18:44 +08:00
private int nextFrameIndex => MathHelper.Clamp(currentFrameIndex + (currentDirection > 0 ? 1 : -1), 0, replayContent.Count - 1);
2017-03-04 18:02:36 +08:00
public LegacyReplayInputHandler(List<LegacyReplayFrame> replayContent)
{
this.replayContent = replayContent;
}
2017-03-06 13:18:44 +08:00
private bool advanceFrame()
2017-03-04 18:02:36 +08:00
{
2017-03-06 13:18:44 +08:00
int newFrame = nextFrameIndex;
2017-03-04 18:02:36 +08:00
//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<InputState> GetPendingStates()
{
2017-03-13 18:11:57 +08:00
var buttons = new HashSet<MouseButton>();
if (CurrentFrame?.MouseLeft ?? false)
buttons.Add(MouseButton.Left);
if (CurrentFrame?.MouseRight ?? false)
buttons.Add(MouseButton.Right);
2017-03-04 18:02:36 +08:00
return new List<InputState>
{
new InputState
{
2017-03-13 18:11:57 +08:00
Mouse = new ReplayMouseState(ToScreenSpace(position ?? Vector2.Zero), buttons),
2017-03-04 18:02:36 +08:00
Keyboard = new ReplayKeyboardState(new List<Key>())
}
};
}
public bool AtLastFrame => currentFrameIndex == replayContent.Count - 1;
public bool AtFirstFrame => currentFrameIndex == 0;
public Vector2 Size => new Vector2(512, 384);
2017-03-07 10:29:55 +08:00
private const double sixty_frame_time = 1000.0 / 60;
2017-03-04 18:02:36 +08:00
2017-03-09 12:55:29 +08:00
private double currentTime;
private int currentDirection;
2017-03-04 18:02:36 +08:00
/// <summary>
/// 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).
/// </summary>
public bool FrameAccuratePlayback = true;
private bool hasFrames => replayContent.Count > 0;
2017-03-09 12:55:29 +08:00
private bool inImportantSection =>
2017-03-04 18:02:36 +08:00
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;
/// <summary>
/// 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.
/// </summary>
/// <param name="time">The time which we should use for finding the current frame.</param>
2017-03-06 13:20:44 +08:00
/// <returns>The usable time value. If null, we should not advance time as we do not have enough data.</returns>
2017-03-04 18:02:36 +08:00
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.
2017-03-06 13:18:44 +08:00
if (currentDirection == time.CompareTo(NextFrame.Time) && advanceFrame())
2017-03-04 18:02:36 +08:00
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
2017-03-04 18:02:36 +08:00
{
2017-03-13 18:11:57 +08:00
public ReplayMouseState(Vector2 position, IEnumerable<MouseButton> list)
2017-03-04 18:02:36 +08:00
{
Position = position;
2017-03-13 18:11:57 +08:00
list.ForEach(b => PressedButtons.Add(b));
2017-03-04 18:02:36 +08:00
}
}
protected class ReplayKeyboardState : KeyboardState
2017-03-04 18:02:36 +08:00
{
public ReplayKeyboardState(List<Key> keys)
{
Keys = keys;
}
}
}
[Flags]
protected enum LegacyButtonState
2017-03-04 18:02:36 +08:00
{
None = 0,
Left1 = 1,
Right1 = 2,
Left2 = 4,
Right2 = 8,
Smoke = 16
}
protected class LegacyReplayFrame
2017-03-04 18:02:36 +08:00
{
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)
{
2017-03-09 13:24:16 +08:00
throw new NotImplementedException();
2017-03-04 18:02:36 +08:00
}
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}";
}
}
}
}