1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 10:18:22 +08:00

Ensure invariant of monotone time

This commit is contained in:
OliBomby 2023-08-11 23:39:28 +02:00
parent d2f5e696e7
commit 5d1ccc2601
2 changed files with 58 additions and 7 deletions

View File

@ -51,6 +51,11 @@ namespace osu.Game.Rulesets.UI
[Resolved(CanBeNull = true)]
private IPooledHitObjectProvider pooledObjectProvider { get; set; }
/// <summary>
/// Invoked when a <see cref="HitObject"/> is updated.
/// </summary>
public event Action<HitObject> HitObjectUpdated;
public HitObjectContainer()
{
RelativeSizeAxes = Axes.Both;
@ -108,6 +113,7 @@ namespace osu.Game.Rulesets.UI
drawable.OnNewResult += onNewResult;
bindStartTime(drawable);
bindUpdated(drawable);
AddInternal(drawable);
}
@ -116,7 +122,7 @@ namespace osu.Game.Rulesets.UI
drawable.OnNewResult -= onNewResult;
unbindStartTime(drawable);
unbindUpdated(drawable);
RemoveInternal(drawable, false);
}
@ -176,6 +182,27 @@ namespace osu.Game.Rulesets.UI
startTimeMap.Clear();
}
private void bindUpdated(DrawableHitObject hitObject)
{
hitObject.DefaultsApplied += onDefaultsApplied;
}
private void unbindUpdated(DrawableHitObject hitObject)
{
hitObject.DefaultsApplied += onDefaultsApplied;
}
private void unbindAllUpdated()
{
foreach (var h in AliveObjects)
unbindUpdated(h);
}
private void onDefaultsApplied(DrawableHitObject obj)
{
HitObjectUpdated?.Invoke(obj.HitObject);
}
protected override int Compare(Drawable x, Drawable y)
{
if (!(x is DrawableHitObject xObj) || !(y is DrawableHitObject yObj))
@ -192,6 +219,7 @@ namespace osu.Game.Rulesets.UI
{
base.Dispose(isDisposing);
unbindAllStartTimes();
unbindAllUpdated();
}
}
}

View File

@ -111,7 +111,7 @@ namespace osu.Game.Rulesets.UI
private readonly HitObjectEntryManager entryManager = new HitObjectEntryManager();
private readonly Stack<HitObjectLifetimeEntry> judgedEntries;
private readonly LinkedList<HitObjectLifetimeEntry> judgedEntries;
/// <summary>
/// Creates a new <see cref="Playfield"/>.
@ -125,12 +125,13 @@ namespace osu.Game.Rulesets.UI
h.NewResult += onNewResult;
h.HitObjectUsageBegan += o => HitObjectUsageBegan?.Invoke(o);
h.HitObjectUsageFinished += o => HitObjectUsageFinished?.Invoke(o);
h.HitObjectUpdated += onHitObjectUpdated;
}));
entryManager.OnEntryAdded += onEntryAdded;
entryManager.OnEntryRemoved += onEntryRemoved;
judgedEntries = new Stack<HitObjectLifetimeEntry>();
judgedEntries = new LinkedList<HitObjectLifetimeEntry>();
}
[BackgroundDependencyLoader]
@ -270,15 +271,16 @@ namespace osu.Game.Rulesets.UI
}
// When rewinding, revert future judgements in the reverse order.
while (judgedEntries.Count > 0)
while (judgedEntries.Last is not null)
{
var result = judgedEntries.Peek().Result;
var result = judgedEntries.Last.Value.Result;
Debug.Assert(result?.RawTime != null);
if (Time.Current >= result.RawTime.Value)
break;
revertResult(judgedEntries.Pop());
revertResult(judgedEntries.Last.Value);
judgedEntries.RemoveLast();
}
}
@ -471,10 +473,31 @@ namespace osu.Game.Rulesets.UI
#endregion
private void onHitObjectUpdated(HitObject _)
{
// The time of judged entries may have changed, so we need to re-sort the list to preserve the invariant of monotone time.
// Insertion sort on linked-list is O(n) for nearly-sorted lists, which is the case here.
var current = judgedEntries.First;
while (current?.Next is not null)
{
var next = current.Next;
if (current.Value.Result?.RawTime > next.Value.Result?.RawTime)
{
judgedEntries.Remove(next);
judgedEntries.AddBefore(current, next);
current = next.Previous;
}
else
current = next;
}
}
private void onNewResult(DrawableHitObject drawable, JudgementResult result)
{
Debug.Assert(result != null && drawable.Entry?.Result == result && result.RawTime != null);
judgedEntries.Push(drawable.Entry.AsNonNull());
judgedEntries.AddLast(drawable.Entry.AsNonNull());
NewResult?.Invoke(drawable, result);
}