Store states as byte[] instead of Streams

This commit is contained in:
Dean Herbert 2020-04-13 17:18:50 +09:00
parent 0c60dbe796
commit bb53f96c71
3 changed files with 29 additions and 32 deletions

View File

@ -315,26 +315,25 @@ namespace osu.Game.Tests.Editor
differ.Patch(encode(current), encode(patch));
// Convert beatmaps to strings for assertion purposes.
string currentStr = Encoding.ASCII.GetString(encode(current).ToArray());
string patchStr = Encoding.ASCII.GetString(encode(patch).ToArray());
string currentStr = Encoding.ASCII.GetString(encode(current));
string patchStr = Encoding.ASCII.GetString(encode(patch));
Assert.That(currentStr, Is.EqualTo(patchStr));
}
private MemoryStream encode(IBeatmap beatmap)
private byte[] encode(IBeatmap beatmap)
{
var encoded = new MemoryStream();
using (var encoded = new MemoryStream())
using (var sw = new StreamWriter(encoded, leaveOpen: true))
{
new LegacyBeatmapEncoder(beatmap).Encode(sw);
return encoded;
return encoded.ToArray();
}
}
private IBeatmap decode(Stream stream)
private IBeatmap decode(byte[] state)
{
stream.Seek(0, SeekOrigin.Begin);
using (var stream = new MemoryStream(state))
using (var reader = new LineBufferedReader(stream, true))
return Decoder.GetDecoder<Beatmap>(reader).Decode(reader);
}

View File

@ -16,7 +16,9 @@ namespace osu.Game.Screens.Edit
public class EditorChangeHandler : IEditorChangeHandler
{
private readonly LegacyEditorBeatmapDiffer differ;
private readonly List<Stream> savedStates = new List<Stream>();
private readonly List<byte[]> savedStates = new List<byte[]>();
private int currentState = -1;
private readonly EditorBeatmap editorBeatmap;
@ -69,15 +71,17 @@ namespace osu.Game.Screens.Edit
if (isRestoring)
return;
var stream = new MemoryStream();
using (var sw = new StreamWriter(stream, Encoding.UTF8, 1024, true))
new LegacyBeatmapEncoder(editorBeatmap).Encode(sw);
if (currentState < savedStates.Count - 1)
savedStates.RemoveRange(currentState + 1, savedStates.Count - currentState - 1);
savedStates.Add(stream);
using (var stream = new MemoryStream())
{
using (var sw = new StreamWriter(stream, Encoding.UTF8, 1024, true))
new LegacyBeatmapEncoder(editorBeatmap).Encode(sw);
savedStates.Add(stream.ToArray());
}
currentState = savedStates.Count - 1;
}

View File

@ -4,12 +4,13 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using DiffPlex;
using osu.Framework.Audio.Track;
using osu.Framework.Graphics.Textures;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Formats;
using osu.Game.IO;
using Decoder = osu.Game.Beatmaps.Formats.Decoder;
namespace osu.Game.Screens.Edit
{
@ -22,7 +23,7 @@ namespace osu.Game.Screens.Edit
this.editorBeatmap = editorBeatmap;
}
public void Patch(Stream currentState, Stream newState)
public void Patch(byte[] currentState, byte[] newState)
{
// Diff the beatmaps
var result = new Differ().CreateLineDiffs(readString(currentState), readString(newState), true, false);
@ -36,7 +37,7 @@ namespace osu.Game.Screens.Edit
foreach (var block in result.DiffBlocks)
{
// Removed hitobject
// Removed hitobjects
for (int i = 0; i < block.DeleteCountA; i++)
{
int hoIndex = block.DeleteStartA + i - oldHitObjectsIndex - 1;
@ -47,7 +48,7 @@ namespace osu.Game.Screens.Edit
toRemove.Add(hoIndex);
}
// Added hitobject
// Added hitobjects
for (int i = 0; i < block.InsertCountB; i++)
{
int hoIndex = block.InsertStartB + i - newHitObjectsIndex - 1;
@ -74,18 +75,11 @@ namespace osu.Game.Screens.Edit
}
}
private string readString(Stream stream)
private string readString(byte[] state) => Encoding.UTF8.GetString(state);
private IBeatmap readBeatmap(byte[] state)
{
stream.Seek(0, SeekOrigin.Begin);
using (var sr = new StreamReader(stream, System.Text.Encoding.UTF8, true, 1024, true))
return sr.ReadToEnd();
}
private IBeatmap readBeatmap(Stream stream)
{
stream.Seek(0, SeekOrigin.Begin);
using (var stream = new MemoryStream(state))
using (var reader = new LineBufferedReader(stream, true))
return new PassThroughWorkingBeatmap(Decoder.GetDecoder<Beatmap>(reader).Decode(reader)).GetPlayableBeatmap(editorBeatmap.BeatmapInfo.Ruleset);
}