2020-10-22 17:10:27 +08:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
2020-10-22 17:37:19 +08:00
|
|
|
using System.Collections.Specialized;
|
2020-10-26 14:24:12 +08:00
|
|
|
using System.Linq;
|
2020-10-22 17:10:27 +08:00
|
|
|
using NUnit.Framework;
|
|
|
|
using osu.Framework.Allocation;
|
2020-10-22 17:37:19 +08:00
|
|
|
using osu.Framework.Bindables;
|
2020-10-22 17:10:27 +08:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
|
|
|
using osu.Framework.Input.Bindings;
|
|
|
|
using osu.Framework.Input.Events;
|
|
|
|
using osu.Framework.Input.StateChanges;
|
2020-10-26 14:24:12 +08:00
|
|
|
using osu.Framework.Logging;
|
2020-10-23 13:47:08 +08:00
|
|
|
using osu.Framework.Testing;
|
2020-10-26 14:24:12 +08:00
|
|
|
using osu.Framework.Timing;
|
2020-10-22 17:10:27 +08:00
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
using osu.Game.Graphics.Sprites;
|
|
|
|
using osu.Game.Online.Spectator;
|
|
|
|
using osu.Game.Replays;
|
|
|
|
using osu.Game.Replays.Legacy;
|
|
|
|
using osu.Game.Rulesets;
|
|
|
|
using osu.Game.Rulesets.Replays;
|
|
|
|
using osu.Game.Rulesets.Replays.Types;
|
|
|
|
using osu.Game.Rulesets.UI;
|
|
|
|
using osu.Game.Tests.Visual.UserInterface;
|
|
|
|
using osuTK;
|
|
|
|
using osuTK.Graphics;
|
|
|
|
|
|
|
|
namespace osu.Game.Tests.Visual.Gameplay
|
|
|
|
{
|
2020-10-26 14:24:28 +08:00
|
|
|
public class TestSceneSpectatorPlayback : OsuManualInputManagerTestScene
|
2020-10-22 17:10:27 +08:00
|
|
|
{
|
|
|
|
protected override bool UseOnlineAPI => true;
|
|
|
|
|
|
|
|
private TestRulesetInputManager playbackManager;
|
|
|
|
private TestRulesetInputManager recordingManager;
|
|
|
|
|
|
|
|
private Replay replay;
|
|
|
|
|
2020-10-22 17:37:19 +08:00
|
|
|
private IBindableList<int> users;
|
2020-10-22 17:10:27 +08:00
|
|
|
|
2020-10-23 13:47:08 +08:00
|
|
|
private TestReplayRecorder recorder;
|
|
|
|
|
2020-10-26 14:24:12 +08:00
|
|
|
private readonly ManualClock manualClock = new ManualClock();
|
|
|
|
|
|
|
|
private OsuSpriteText latencyDisplay;
|
|
|
|
|
2020-10-22 17:10:27 +08:00
|
|
|
[Resolved]
|
|
|
|
private SpectatorStreamingClient streamingClient { get; set; }
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
public void SetUp() => Schedule(() =>
|
|
|
|
{
|
|
|
|
replay = new Replay();
|
|
|
|
|
2020-10-22 17:37:19 +08:00
|
|
|
users = streamingClient.PlayingUsers.GetBoundCopy();
|
|
|
|
users.BindCollectionChanged((obj, args) =>
|
|
|
|
{
|
|
|
|
switch (args.Action)
|
|
|
|
{
|
|
|
|
case NotifyCollectionChangedAction.Add:
|
|
|
|
foreach (int user in args.NewItems)
|
|
|
|
streamingClient.WatchUser(user);
|
|
|
|
break;
|
2020-10-22 18:16:34 +08:00
|
|
|
|
|
|
|
case NotifyCollectionChangedAction.Remove:
|
|
|
|
foreach (int user in args.OldItems)
|
|
|
|
streamingClient.StopWatchingUser(user);
|
|
|
|
break;
|
2020-10-22 17:37:19 +08:00
|
|
|
}
|
|
|
|
}, true);
|
|
|
|
|
2020-10-26 14:24:12 +08:00
|
|
|
streamingClient.OnNewFrames += onNewFrames;
|
2020-10-22 17:10:27 +08:00
|
|
|
|
|
|
|
Add(new GridContainer
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Content = new[]
|
|
|
|
{
|
|
|
|
new Drawable[]
|
|
|
|
{
|
|
|
|
recordingManager = new TestRulesetInputManager(new TestSceneModSettings.TestRulesetInfo(), 0, SimultaneousBindingMode.Unique)
|
|
|
|
{
|
2020-10-23 13:47:08 +08:00
|
|
|
Recorder = recorder = new TestReplayRecorder
|
2020-10-22 17:10:27 +08:00
|
|
|
{
|
|
|
|
ScreenSpaceToGamefield = pos => recordingManager.ToLocalSpace(pos),
|
|
|
|
},
|
|
|
|
Child = new Container
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
new Box
|
|
|
|
{
|
|
|
|
Colour = Color4.Brown,
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
},
|
|
|
|
new OsuSpriteText
|
|
|
|
{
|
|
|
|
Text = "Sending",
|
|
|
|
Scale = new Vector2(3),
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
},
|
|
|
|
new TestInputConsumer()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
new Drawable[]
|
|
|
|
{
|
|
|
|
playbackManager = new TestRulesetInputManager(new TestSceneModSettings.TestRulesetInfo(), 0, SimultaneousBindingMode.Unique)
|
|
|
|
{
|
2020-10-26 14:24:12 +08:00
|
|
|
Clock = new FramedClock(manualClock),
|
2020-10-22 17:10:27 +08:00
|
|
|
ReplayInputHandler = new TestFramedReplayInputHandler(replay)
|
|
|
|
{
|
|
|
|
GamefieldToScreenSpace = pos => playbackManager.ToScreenSpace(pos),
|
|
|
|
},
|
|
|
|
Child = new Container
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
new Box
|
|
|
|
{
|
|
|
|
Colour = Color4.DarkBlue,
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
},
|
|
|
|
new OsuSpriteText
|
|
|
|
{
|
|
|
|
Text = "Receiving",
|
|
|
|
Scale = new Vector2(3),
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
},
|
|
|
|
new TestInputConsumer()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-10-26 14:24:12 +08:00
|
|
|
|
|
|
|
Add(latencyDisplay = new OsuSpriteText());
|
2020-10-22 17:10:27 +08:00
|
|
|
});
|
|
|
|
|
2020-10-26 14:24:12 +08:00
|
|
|
private void onNewFrames(int userId, FrameDataBundle frames)
|
|
|
|
{
|
|
|
|
Logger.Log($"Received {frames.Frames.Count()} new frames ({string.Join(',', frames.Frames.Select(f => ((int)f.Time).ToString()))})");
|
|
|
|
|
|
|
|
foreach (var legacyFrame in frames.Frames)
|
|
|
|
{
|
|
|
|
var frame = new TestReplayFrame();
|
|
|
|
frame.FromLegacy(legacyFrame, null, null);
|
|
|
|
replay.Frames.Add(frame);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-22 17:10:27 +08:00
|
|
|
[Test]
|
|
|
|
public void TestBasic()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Update()
|
|
|
|
{
|
|
|
|
base.Update();
|
2020-10-26 14:24:12 +08:00
|
|
|
|
|
|
|
double elapsed = Time.Elapsed;
|
|
|
|
double? time = playbackManager?.ReplayInputHandler.SetFrameFromTime(manualClock.CurrentTime + elapsed);
|
|
|
|
|
|
|
|
if (time != null)
|
|
|
|
{
|
|
|
|
manualClock.CurrentTime = time.Value;
|
|
|
|
|
|
|
|
latencyDisplay.Text = $"latency: {Time.Current - time.Value:N1}ms";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
manualClock.CurrentTime = Time.Current;
|
|
|
|
}
|
2020-10-22 17:10:27 +08:00
|
|
|
}
|
|
|
|
|
2020-10-23 13:47:08 +08:00
|
|
|
[TearDownSteps]
|
|
|
|
public void TearDown()
|
|
|
|
{
|
2020-10-26 14:24:12 +08:00
|
|
|
AddStep("stop recorder", () =>
|
|
|
|
{
|
|
|
|
recorder.Expire();
|
|
|
|
streamingClient.OnNewFrames -= onNewFrames;
|
|
|
|
});
|
2020-10-23 13:47:08 +08:00
|
|
|
}
|
|
|
|
|
2020-10-22 17:10:27 +08:00
|
|
|
public class TestFramedReplayInputHandler : FramedReplayInputHandler<TestReplayFrame>
|
|
|
|
{
|
|
|
|
public TestFramedReplayInputHandler(Replay replay)
|
|
|
|
: base(replay)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void CollectPendingInputs(List<IInput> inputs)
|
|
|
|
{
|
|
|
|
inputs.Add(new MousePositionAbsoluteInput { Position = GamefieldToScreenSpace(CurrentFrame?.Position ?? Vector2.Zero) });
|
|
|
|
inputs.Add(new ReplayState<TestAction> { PressedActions = CurrentFrame?.Actions ?? new List<TestAction>() });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class TestInputConsumer : CompositeDrawable, IKeyBindingHandler<TestAction>
|
|
|
|
{
|
|
|
|
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => Parent.ReceivePositionalInputAt(screenSpacePos);
|
|
|
|
|
|
|
|
private readonly Box box;
|
|
|
|
|
|
|
|
public TestInputConsumer()
|
|
|
|
{
|
|
|
|
Size = new Vector2(30);
|
|
|
|
|
|
|
|
Origin = Anchor.Centre;
|
|
|
|
|
|
|
|
InternalChildren = new Drawable[]
|
|
|
|
{
|
|
|
|
box = new Box
|
|
|
|
{
|
|
|
|
Colour = Color4.Black,
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override bool OnMouseMove(MouseMoveEvent e)
|
|
|
|
{
|
|
|
|
Position = e.MousePosition;
|
|
|
|
return base.OnMouseMove(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool OnPressed(TestAction action)
|
|
|
|
{
|
|
|
|
box.Colour = Color4.White;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnReleased(TestAction action)
|
|
|
|
{
|
|
|
|
box.Colour = Color4.Black;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class TestRulesetInputManager : RulesetInputManager<TestAction>
|
|
|
|
{
|
|
|
|
public TestRulesetInputManager(RulesetInfo ruleset, int variant, SimultaneousBindingMode unique)
|
|
|
|
: base(ruleset, variant, unique)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override KeyBindingContainer<TestAction> CreateKeyBindingContainer(RulesetInfo ruleset, int variant, SimultaneousBindingMode unique)
|
|
|
|
=> new TestKeyBindingContainer();
|
|
|
|
|
|
|
|
internal class TestKeyBindingContainer : KeyBindingContainer<TestAction>
|
|
|
|
{
|
|
|
|
public override IEnumerable<KeyBinding> DefaultKeyBindings => new[]
|
|
|
|
{
|
|
|
|
new KeyBinding(InputKey.MouseLeft, TestAction.Down),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class TestReplayFrame : ReplayFrame, IConvertibleReplayFrame
|
|
|
|
{
|
|
|
|
public Vector2 Position;
|
|
|
|
|
|
|
|
public List<TestAction> Actions = new List<TestAction>();
|
|
|
|
|
|
|
|
public TestReplayFrame(double time, Vector2 position, params TestAction[] actions)
|
|
|
|
: base(time)
|
|
|
|
{
|
|
|
|
Position = position;
|
|
|
|
Actions.AddRange(actions);
|
|
|
|
}
|
|
|
|
|
|
|
|
public TestReplayFrame()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public void FromLegacy(LegacyReplayFrame currentFrame, IBeatmap beatmap, ReplayFrame lastFrame = null)
|
|
|
|
{
|
|
|
|
Position = currentFrame.Position;
|
|
|
|
Time = currentFrame.Time;
|
|
|
|
if (currentFrame.MouseLeft)
|
|
|
|
Actions.Add(TestAction.Down);
|
|
|
|
}
|
|
|
|
|
|
|
|
public LegacyReplayFrame ToLegacy(IBeatmap beatmap)
|
|
|
|
{
|
|
|
|
ReplayButtonState state = ReplayButtonState.None;
|
|
|
|
|
|
|
|
if (Actions.Contains(TestAction.Down))
|
|
|
|
state |= ReplayButtonState.Left1;
|
|
|
|
|
|
|
|
return new LegacyReplayFrame(Time, Position.X, Position.Y, state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public enum TestAction
|
|
|
|
{
|
|
|
|
Down,
|
|
|
|
}
|
|
|
|
|
|
|
|
internal class TestReplayRecorder : ReplayRecorder<TestAction>
|
|
|
|
{
|
|
|
|
public TestReplayRecorder()
|
|
|
|
: base(new Replay())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override ReplayFrame HandleFrame(Vector2 mousePosition, List<TestAction> actions, ReplayFrame previousFrame)
|
|
|
|
{
|
|
|
|
return new TestReplayFrame(Time.Current, mousePosition, actions.ToArray());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|