1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 12:17:26 +08:00

Fix frames potentially getting added to spectator replay in wrong format

The way spectator currently works, the `Spectator` screen is responsible
for adding new frames to the replay, even when it has a child
(`SpectatorPlayer`) present.

There was a possibility that a new play had already started, and on
returning to the Spectator screen (to initialise the new play) there
would be a brief period where the Player instance is still reading from
the replay, the `userBeganPlaying` call had not yet finished
initialising the new target replay, and `userSentFrames` is run
(asynchronously), writing frames to the previous replay using the
incorrect ruleset instance).

To make this work, it doesn't `Schedule` frame addition to the replay
(making things a bit unsafe). Changing this itself isn't such a simple
one to do, so I instead opted to fix this via locking.

Closes https://github.com/ppy/osu/issues/10777.
This commit is contained in:
Dean Herbert 2020-11-11 13:39:42 +09:00
parent 6593aac3f2
commit 11cf04eed1

View File

@ -3,6 +3,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using JetBrains.Annotations; using JetBrains.Annotations;
using osu.Framework.Allocation; using osu.Framework.Allocation;
@ -61,7 +62,9 @@ namespace osu.Game.Screens.Play
[Resolved] [Resolved]
private RulesetStore rulesets { get; set; } private RulesetStore rulesets { get; set; }
private Replay replay; private Score score;
private readonly object scoreLock = new object();
private Container beatmapPanelContainer; private Container beatmapPanelContainer;
@ -198,23 +201,32 @@ namespace osu.Game.Screens.Play
private void userSentFrames(int userId, FrameDataBundle data) private void userSentFrames(int userId, FrameDataBundle data)
{ {
// this is not scheduled as it handles propagation of frames even when in a child screen (at which point we are not alive).
// probably not the safest way to handle this.
if (userId != targetUser.Id) if (userId != targetUser.Id)
return; return;
// this should never happen as the server sends the user's state on watching, lock (scoreLock)
// but is here as a safety measure.
if (replay == null)
return;
foreach (var frame in data.Frames)
{ {
IConvertibleReplayFrame convertibleFrame = rulesetInstance.CreateConvertibleReplayFrame(); // this should never happen as the server sends the user's state on watching,
convertibleFrame.FromLegacy(frame, beatmap.Value.Beatmap); // but is here as a safety measure.
if (score == null)
return;
var convertedFrame = (ReplayFrame)convertibleFrame; // rulesetInstance should be guaranteed to be in sync with the score via scoreLock.
convertedFrame.Time = frame.Time; Debug.Assert(rulesetInstance != null && rulesetInstance.RulesetInfo.Equals(score.ScoreInfo.Ruleset));
replay.Frames.Add(convertedFrame); foreach (var frame in data.Frames)
{
IConvertibleReplayFrame convertibleFrame = rulesetInstance.CreateConvertibleReplayFrame();
convertibleFrame.FromLegacy(frame, beatmap.Value.Beatmap);
var convertedFrame = (ReplayFrame)convertibleFrame;
convertedFrame.Time = frame.Time;
score.Replay.Frames.Add(convertedFrame);
}
} }
} }
@ -247,10 +259,13 @@ namespace osu.Game.Screens.Play
if (userId != targetUser.Id) if (userId != targetUser.Id)
return; return;
if (replay != null) lock (scoreLock)
{ {
replay.HasReceivedAllFrames = true; if (score != null)
replay = null; {
score.Replay.HasReceivedAllFrames = true;
score = null;
}
} }
Schedule(clearDisplay); Schedule(clearDisplay);
@ -283,27 +298,28 @@ namespace osu.Game.Screens.Play
return; return;
} }
replay ??= new Replay { HasReceivedAllFrames = false }; lock (scoreLock)
var scoreInfo = new ScoreInfo
{ {
Beatmap = resolvedBeatmap, score = new Score
User = targetUser, {
Mods = state.Mods.Select(m => m.ToMod(resolvedRuleset)).ToArray(), ScoreInfo = new ScoreInfo
Ruleset = resolvedRuleset.RulesetInfo, {
}; Beatmap = resolvedBeatmap,
User = targetUser,
Mods = state.Mods.Select(m => m.ToMod(resolvedRuleset)).ToArray(),
Ruleset = resolvedRuleset.RulesetInfo,
},
Replay = new Replay { HasReceivedAllFrames = false },
};
ruleset.Value = resolvedRuleset.RulesetInfo; ruleset.Value = resolvedRuleset.RulesetInfo;
rulesetInstance = resolvedRuleset; rulesetInstance = resolvedRuleset;
beatmap.Value = beatmaps.GetWorkingBeatmap(resolvedBeatmap); beatmap.Value = beatmaps.GetWorkingBeatmap(resolvedBeatmap);
watchButton.Enabled.Value = true; watchButton.Enabled.Value = true;
this.Push(new SpectatorPlayerLoader(new Score this.Push(new SpectatorPlayerLoader(score));
{ }
ScoreInfo = scoreInfo,
Replay = replay,
}));
} }
private void showBeatmapPanel(SpectatorState state) private void showBeatmapPanel(SpectatorState state)