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

Limit upper number of editor beatmap states saved to 50

This commit is contained in:
Dean Herbert 2020-04-13 20:34:18 +09:00
parent 64422fbf6d
commit 2388799acf
2 changed files with 78 additions and 0 deletions

View File

@ -0,0 +1,71 @@
// 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 NUnit.Framework;
using osu.Game.Beatmaps;
using osu.Game.Screens.Edit;
namespace osu.Game.Tests.Editor
{
[TestFixture]
public class EditorChangeHandlerTest
{
[Test]
public void TestSaveRestoreState()
{
var handler = new EditorChangeHandler(new EditorBeatmap(new Beatmap()));
Assert.That(handler.HasUndoState, Is.False);
handler.SaveState();
Assert.That(handler.HasUndoState, Is.True);
handler.RestoreState(-1);
Assert.That(handler.HasUndoState, Is.False);
}
[Test]
public void TestMaxStatesSaved()
{
var handler = new EditorChangeHandler(new EditorBeatmap(new Beatmap()));
Assert.That(handler.HasUndoState, Is.False);
for (int i = 0; i < EditorChangeHandler.MAX_SAVED_STATES; i++)
handler.SaveState();
Assert.That(handler.HasUndoState, Is.True);
for (int i = 0; i < EditorChangeHandler.MAX_SAVED_STATES; i++)
{
Assert.That(handler.HasUndoState, Is.True);
handler.RestoreState(-1);
}
Assert.That(handler.HasUndoState, Is.False);
}
[Test]
public void TestMaxStatesExceeded()
{
var handler = new EditorChangeHandler(new EditorBeatmap(new Beatmap()));
Assert.That(handler.HasUndoState, Is.False);
for (int i = 0; i < EditorChangeHandler.MAX_SAVED_STATES * 2; i++)
handler.SaveState();
Assert.That(handler.HasUndoState, Is.True);
for (int i = 0; i < EditorChangeHandler.MAX_SAVED_STATES; i++)
{
Assert.That(handler.HasUndoState, Is.True);
handler.RestoreState(-1);
}
Assert.That(handler.HasUndoState, Is.False);
}
}
}

View File

@ -25,6 +25,8 @@ namespace osu.Game.Screens.Edit
private int bulkChangesStarted;
private bool isRestoring;
public const int MAX_SAVED_STATES = 50;
/// <summary>
/// Creates a new <see cref="EditorChangeHandler"/>.
/// </summary>
@ -43,6 +45,8 @@ namespace osu.Game.Screens.Edit
SaveState();
}
public bool HasUndoState => currentState > 0;
private void hitObjectAdded(HitObject obj) => SaveState();
private void hitObjectRemoved(HitObject obj) => SaveState();
@ -74,6 +78,9 @@ namespace osu.Game.Screens.Edit
if (currentState < savedStates.Count - 1)
savedStates.RemoveRange(currentState + 1, savedStates.Count - currentState - 1);
if (savedStates.Count > MAX_SAVED_STATES)
savedStates.RemoveAt(0);
using (var stream = new MemoryStream())
{
using (var sw = new StreamWriter(stream, Encoding.UTF8, 1024, true))