1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-28 06:42:54 +08:00

Add required parameters and other various changes

This commit is contained in:
Craftplacer 2020-08-15 22:03:24 +02:00
parent 69590113d6
commit c4a7fac760
8 changed files with 51 additions and 33 deletions

View File

@ -28,13 +28,25 @@ namespace osu.Game.Tests.Beatmaps.Formats
[TestFixture]
public class LegacyBeatmapEncoderTest
{
private static IEnumerable<string> allBeatmaps => TestResources.GetStore().GetAvailableResources().Where(res => res.EndsWith(".osu"));
private static readonly DllResourceStore resource_store = TestResources.GetStore();
private static IEnumerable<string> allBeatmaps = resource_store.GetAvailableResources().Where(res => res.EndsWith(".osu"));
private static Stream beatmapSkinStream = resource_store.GetStream("skin.ini");
private ISkin skin;
[SetUp]
public void Init()
{
skin = decodeSkinFromLegacy(beatmapSkinStream);
}
[TestCaseSource(nameof(allBeatmaps))]
public void TestEncodeDecodeStability(string name)
{
var decoded = decodeFromLegacy(TestResources.GetStore().GetStream(name));
var decodedAfterEncode = decodeFromLegacy(encodeToLegacy(decoded));
var decoded = decodeBeatmapFromLegacy(TestResources.GetStore().GetStream(name));
var decodedAfterEncode = decodeBeatmapFromLegacy(encodeToLegacy(decoded, skin));
sort(decoded);
sort(decodedAfterEncode);
@ -52,20 +64,24 @@ namespace osu.Game.Tests.Beatmaps.Formats
}
}
private IBeatmap decodeFromLegacy(Stream stream)
private IBeatmap decodeBeatmapFromLegacy(Stream stream)
{
using (var reader = new LineBufferedReader(stream))
return convert(new LegacyBeatmapDecoder { ApplyOffsets = false }.Decode(reader));
}
private Stream encodeToLegacy(IBeatmap beatmap)
private ISkin decodeSkinFromLegacy(Stream stream)
{
using (var reader = new LineBufferedReader(stream))
return new LegacySkin(SkinInfo.Default, resource_store, null);
}
private Stream encodeToLegacy(IBeatmap beatmap, ISkin skin)
{
var stream = new MemoryStream();
using (var writer = new StreamWriter(stream, Encoding.UTF8, 1024, true))
using (var rs = new ResourceStore<byte[]>())
{
var skin = new LegacyBeatmapSkin(beatmap.BeatmapInfo, rs, null);
new LegacyBeatmapEncoder(beatmap, skin).Encode(writer);
}

View File

@ -730,7 +730,8 @@ namespace osu.Game.Tests.Beatmaps.IO
BeatmapSetInfo setToUpdate = manager.GetAllUsableBeatmapSets()[0];
var beatmapInfo = setToUpdate.Beatmaps.First(b => b.RulesetID == 0);
Beatmap beatmapToUpdate = (Beatmap)manager.GetWorkingBeatmap(setToUpdate.Beatmaps.First(b => b.RulesetID == 0)).Beatmap;
var workingBeatmap = manager.GetWorkingBeatmap(setToUpdate.Beatmaps.First(b => b.RulesetID == 0));
Beatmap beatmapToUpdate = (Beatmap)workingBeatmap.Beatmap;
BeatmapSetFileInfo fileToUpdate = setToUpdate.Files.First(f => beatmapToUpdate.BeatmapInfo.Path.Contains(f.Filename));
string oldMd5Hash = beatmapToUpdate.BeatmapInfo.MD5Hash;
@ -738,7 +739,7 @@ namespace osu.Game.Tests.Beatmaps.IO
beatmapToUpdate.HitObjects.Clear();
beatmapToUpdate.HitObjects.Add(new HitCircle { StartTime = 5000 });
manager.Save(beatmapInfo, beatmapToUpdate);
manager.Save(beatmapInfo, beatmapToUpdate, workingBeatmap.Skin);
// Check that the old file reference has been removed
Assert.That(manager.QueryBeatmapSet(s => s.ID == setToUpdate.ID).Files.All(f => f.ID != fileToUpdate.ID));

View File

@ -13,7 +13,7 @@ namespace osu.Game.Tests.Editing
[Test]
public void TestSaveRestoreState()
{
var handler = new EditorChangeHandler(new EditorBeatmap(new Beatmap()));
var handler = new EditorChangeHandler(new EditorBeatmap(new Beatmap()), null);
Assert.That(handler.CanUndo.Value, Is.False);
Assert.That(handler.CanRedo.Value, Is.False);
@ -32,7 +32,7 @@ namespace osu.Game.Tests.Editing
[Test]
public void TestMaxStatesSaved()
{
var handler = new EditorChangeHandler(new EditorBeatmap(new Beatmap()));
var handler = new EditorChangeHandler(new EditorBeatmap(new Beatmap()), null);
Assert.That(handler.CanUndo.Value, Is.False);
@ -53,7 +53,7 @@ namespace osu.Game.Tests.Editing
[Test]
public void TestMaxStatesExceeded()
{
var handler = new EditorChangeHandler(new EditorBeatmap(new Beatmap()));
var handler = new EditorChangeHandler(new EditorBeatmap(new Beatmap()), null);
Assert.That(handler.CanUndo.Value, Is.False);

View File

@ -4,7 +4,6 @@
using System.IO;
using System.Text;
using NUnit.Framework;
using osu.Framework.IO.Stores;
using osu.Game.Audio;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Formats;
@ -15,7 +14,6 @@ using osu.Game.Rulesets.Osu;
using osu.Game.Rulesets.Osu.Beatmaps;
using osu.Game.Rulesets.Osu.Objects;
using osu.Game.Screens.Edit;
using osu.Game.Skinning;
using osuTK;
using Decoder = osu.Game.Beatmaps.Formats.Decoder;
@ -353,11 +351,7 @@ namespace osu.Game.Tests.Editing
using (var encoded = new MemoryStream())
{
using (var sw = new StreamWriter(encoded))
using (var rs = new ResourceStore<byte[]>())
{
var skin = new LegacyBeatmapSkin(beatmap.BeatmapInfo, rs, null);
new LegacyBeatmapEncoder(beatmap, skin).Encode(sw);
}
new LegacyBeatmapEncoder(beatmap, null).Encode(sw);
return encoded.ToArray();
}

View File

@ -195,7 +195,8 @@ namespace osu.Game.Beatmaps
/// </summary>
/// <param name="info">The <see cref="BeatmapInfo"/> to save the content against. The file referenced by <see cref="BeatmapInfo.Path"/> will be replaced.</param>
/// <param name="beatmapContent">The <see cref="IBeatmap"/> content to write.</param>
public void Save(BeatmapInfo info, IBeatmap beatmapContent)
/// <param name="skin">Optional beatmap skin for inline skin configuration in beatmap files.</param>
public void Save(BeatmapInfo info, IBeatmap beatmapContent, ISkin skin)
{
var setInfo = QueryBeatmapSet(s => s.Beatmaps.Any(b => b.ID == info.ID));
@ -203,7 +204,6 @@ namespace osu.Game.Beatmaps
{
using (var sw = new StreamWriter(stream, Encoding.UTF8, 1024, true))
{
var skin = new LegacyBeatmapSkin(info, Files.Store, audioManager);
new LegacyBeatmapEncoder(beatmapContent, skin).Encode(sw);
}

View File

@ -24,14 +24,14 @@ namespace osu.Game.Beatmaps.Formats
public const int LATEST_VERSION = 128;
private readonly IBeatmap beatmap;
private readonly LegacyBeatmapSkin beatmapSkin;
private readonly ISkin skin;
/// <param name="beatmap">The beatmap to encode</param>
/// <param name="beatmapSkin">An optional beatmap skin, for encoding the beatmap's combo colours.</param>
public LegacyBeatmapEncoder(IBeatmap beatmap, [CanBeNull] LegacyBeatmapSkin beatmapSkin)
/// <param name="skin">An optional skin, for encoding the beatmap's combo colours. This will only work if the parameter is a type of <see cref="LegacyBeatmapSkin"/>.</param>
public LegacyBeatmapEncoder(IBeatmap beatmap, [CanBeNull] ISkin skin)
{
this.beatmap = beatmap;
this.beatmapSkin = beatmapSkin;
this.skin = skin;
if (beatmap.BeatmapInfo.RulesetID < 0 || beatmap.BeatmapInfo.RulesetID > 3)
throw new ArgumentException("Only beatmaps in the osu, taiko, catch, or mania rulesets can be encoded to the legacy beatmap format.", nameof(beatmap));
@ -207,7 +207,10 @@ namespace osu.Game.Beatmaps.Formats
private void handleComboColours(TextWriter writer)
{
var colours = beatmapSkin?.Configuration.ComboColours;
if (!(skin is LegacyBeatmapSkin legacySkin))
return;
var colours = legacySkin?.Configuration.ComboColours;
if (colours == null || colours.Count == 0)
return;

View File

@ -33,6 +33,7 @@ using osu.Game.Screens.Edit.Compose;
using osu.Game.Screens.Edit.Setup;
using osu.Game.Screens.Edit.Timing;
using osu.Game.Screens.Play;
using osu.Game.Skinning;
using osu.Game.Users;
namespace osu.Game.Screens.Edit
@ -64,6 +65,7 @@ namespace osu.Game.Screens.Edit
private IBeatmap playableBeatmap;
private EditorBeatmap editorBeatmap;
private EditorChangeHandler changeHandler;
private ISkin beatmapSkin;
private DependencyContainer dependencies;
@ -92,6 +94,7 @@ namespace osu.Game.Screens.Edit
try
{
playableBeatmap = Beatmap.Value.GetPlayableBeatmap(Beatmap.Value.BeatmapInfo.Ruleset);
beatmapSkin = Beatmap.Value.Skin;
}
catch (Exception e)
{
@ -104,7 +107,7 @@ namespace osu.Game.Screens.Edit
AddInternal(editorBeatmap = new EditorBeatmap(playableBeatmap));
dependencies.CacheAs(editorBeatmap);
changeHandler = new EditorChangeHandler(editorBeatmap);
changeHandler = new EditorChangeHandler(editorBeatmap, beatmapSkin);
dependencies.CacheAs<IEditorChangeHandler>(changeHandler);
EditorMenuBar menuBar;
@ -399,7 +402,7 @@ namespace osu.Game.Screens.Edit
clock.SeekForward(!clock.IsRunning, amount);
}
private void saveBeatmap() => beatmapManager.Save(playableBeatmap.BeatmapInfo, editorBeatmap);
private void saveBeatmap() => beatmapManager.Save(playableBeatmap.BeatmapInfo, editorBeatmap, Beatmap.Value.Skin);
private void exportBeatmap()
{

View File

@ -6,7 +6,6 @@ using System.Collections.Generic;
using System.IO;
using System.Text;
using osu.Framework.Bindables;
using osu.Framework.IO.Stores;
using osu.Game.Beatmaps.Formats;
using osu.Game.Rulesets.Objects;
using osu.Game.Skinning;
@ -27,6 +26,7 @@ namespace osu.Game.Screens.Edit
private int currentState = -1;
private readonly EditorBeatmap editorBeatmap;
private readonly ISkin beatmapSkin;
private int bulkChangesStarted;
private bool isRestoring;
@ -36,7 +36,8 @@ namespace osu.Game.Screens.Edit
/// Creates a new <see cref="EditorChangeHandler"/>.
/// </summary>
/// <param name="editorBeatmap">The <see cref="EditorBeatmap"/> to track the <see cref="HitObject"/>s of.</param>
public EditorChangeHandler(EditorBeatmap editorBeatmap)
/// <param name="beatmapSkin">The skin to track the inline skin configuration of.</param>
public EditorChangeHandler(EditorBeatmap editorBeatmap, ISkin beatmapSkin)
{
this.editorBeatmap = editorBeatmap;
@ -46,6 +47,8 @@ namespace osu.Game.Screens.Edit
patcher = new LegacyEditorBeatmapPatcher(editorBeatmap);
this.beatmapSkin = beatmapSkin;
// Initial state.
SaveState();
}
@ -87,10 +90,8 @@ namespace osu.Game.Screens.Edit
using (var stream = new MemoryStream())
{
using (var sw = new StreamWriter(stream, Encoding.UTF8, 1024, true))
using (var rs = new ResourceStore<byte[]>())
{
var skin = new LegacyBeatmapSkin(editorBeatmap.BeatmapInfo, rs, null);
new LegacyBeatmapEncoder(editorBeatmap, skin).Encode(sw);
new LegacyBeatmapEncoder(editorBeatmap, beatmapSkin).Encode(sw);
}
savedStates.Add(stream.ToArray());