1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-21 13:22:57 +08:00
osu-lazer/osu.Game/Tests/Beatmaps/BeatmapConversionTest.cs

243 lines
11 KiB
C#
Raw Normal View History

// 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.
2018-04-13 17:19:50 +08:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2018-04-13 17:19:50 +08:00
using System.Reflection;
using Newtonsoft.Json;
using NUnit.Framework;
using osu.Framework.Audio.Track;
using osu.Framework.Graphics.Textures;
2018-04-13 17:19:50 +08:00
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Formats;
using osu.Game.IO;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
2018-04-13 17:19:50 +08:00
using osu.Game.Rulesets.Objects;
namespace osu.Game.Tests.Beatmaps
{
[TestFixture]
2018-06-14 19:28:29 +08:00
public abstract class BeatmapConversionTest<TConvertMapping, TConvertValue>
where TConvertMapping : ConvertMapping<TConvertValue>, IEquatable<TConvertMapping>, new()
2018-04-13 17:19:50 +08:00
where TConvertValue : IEquatable<TConvertValue>
{
private const string resource_namespace = "Testing.Beatmaps";
private const string expected_conversion_suffix = "-expected-conversion";
protected abstract string ResourceAssembly { get; }
protected void Test(string name, params Type[] mods)
2018-04-13 17:19:50 +08:00
{
var ourResult = convert(name, mods.Select(m => (Mod)Activator.CreateInstance(m)).ToArray());
2018-04-13 17:19:50 +08:00
var expectedResult = read(name);
Assert.Multiple(() =>
{
int mappingCounter = 0;
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
while (true)
{
if (mappingCounter >= ourResult.Mappings.Count && mappingCounter >= expectedResult.Mappings.Count)
break;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
if (mappingCounter >= ourResult.Mappings.Count)
Assert.Fail($"A conversion did not generate any hitobjects, but should have, for hitobject at time: {expectedResult.Mappings[mappingCounter].StartTime}\n");
else if (mappingCounter >= expectedResult.Mappings.Count)
Assert.Fail($"A conversion generated hitobjects, but should not have, for hitobject at time: {ourResult.Mappings[mappingCounter].StartTime}\n");
else if (!expectedResult.Mappings[mappingCounter].Equals(ourResult.Mappings[mappingCounter]))
{
var expectedMapping = expectedResult.Mappings[mappingCounter];
var ourMapping = ourResult.Mappings[mappingCounter];
Assert.Fail($"The conversion mapping differed for object at time {expectedMapping.StartTime}:\n"
+ $"Expected {JsonConvert.SerializeObject(expectedMapping)}\n"
+ $"Received: {JsonConvert.SerializeObject(ourMapping)}\n");
}
2018-04-13 17:19:50 +08:00
else
{
var ourMapping = ourResult.Mappings[mappingCounter];
var expectedMapping = expectedResult.Mappings[mappingCounter];
2018-04-13 17:19:50 +08:00
Assert.Multiple(() =>
{
int objectCounter = 0;
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
while (true)
{
if (objectCounter >= ourMapping.Objects.Count && objectCounter >= expectedMapping.Objects.Count)
break;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
if (objectCounter >= ourMapping.Objects.Count)
2019-11-11 19:53:22 +08:00
{
2018-04-13 17:19:50 +08:00
Assert.Fail($"The conversion did not generate a hitobject, but should have, for hitobject at time: {expectedMapping.StartTime}:\n"
+ $"Expected: {JsonConvert.SerializeObject(expectedMapping.Objects[objectCounter])}\n");
2019-11-11 19:53:22 +08:00
}
2018-04-13 17:19:50 +08:00
else if (objectCounter >= expectedMapping.Objects.Count)
2019-11-11 19:53:22 +08:00
{
2018-04-13 17:19:50 +08:00
Assert.Fail($"The conversion generated a hitobject, but should not have, for hitobject at time: {ourMapping.StartTime}:\n"
+ $"Received: {JsonConvert.SerializeObject(ourMapping.Objects[objectCounter])}\n");
2019-11-11 19:53:22 +08:00
}
2018-06-14 19:28:29 +08:00
else if (!expectedMapping.Objects[objectCounter].Equals(ourMapping.Objects[objectCounter]))
2018-04-13 17:19:50 +08:00
{
2018-06-14 19:28:29 +08:00
Assert.Fail($"The conversion generated differing hitobjects for object at time: {expectedMapping.StartTime}:\n"
2018-04-13 17:19:50 +08:00
+ $"Expected: {JsonConvert.SerializeObject(expectedMapping.Objects[objectCounter])}\n"
+ $"Received: {JsonConvert.SerializeObject(ourMapping.Objects[objectCounter])}\n");
}
objectCounter++;
}
});
}
mappingCounter++;
}
});
}
private ConvertResult convert(string name, Mod[] mods)
2018-04-13 17:19:50 +08:00
{
var beatmap = getBeatmap(name);
var rulesetInstance = CreateRuleset();
beatmap.BeatmapInfo.Ruleset = beatmap.BeatmapInfo.RulesetID == rulesetInstance.RulesetInfo.ID ? rulesetInstance.RulesetInfo : new RulesetInfo();
var converterResult = new Dictionary<HitObject, IEnumerable<HitObject>>();
var working = new ConversionWorkingBeatmap(beatmap)
2018-04-13 17:19:50 +08:00
{
ConversionGenerated = (o, r, c) =>
{
converterResult[o] = r;
OnConversionGenerated(o, r, c);
}
};
working.GetPlayableBeatmap(rulesetInstance.RulesetInfo, mods);
2018-06-14 19:28:29 +08:00
return new ConvertResult
{
Mappings = converterResult.Select(r =>
{
var mapping = CreateConvertMapping(r.Key);
mapping.StartTime = r.Key.StartTime;
mapping.Objects.AddRange(r.Value.SelectMany(CreateConvertValue));
return mapping;
}).ToList()
};
}
protected virtual void OnConversionGenerated(HitObject original, IEnumerable<HitObject> result, IBeatmapConverter beatmapConverter)
{
2018-04-13 17:19:50 +08:00
}
private ConvertResult read(string name)
{
using (var resStream = openResource($"{resource_namespace}.{name}{expected_conversion_suffix}.json"))
using (var reader = new StreamReader(resStream))
{
var contents = reader.ReadToEnd();
return JsonConvert.DeserializeObject<ConvertResult>(contents);
}
}
2018-04-19 19:44:38 +08:00
private IBeatmap getBeatmap(string name)
2018-04-13 17:19:50 +08:00
{
using (var resStream = openResource($"{resource_namespace}.{name}.osu"))
using (var stream = new LineBufferedReader(resStream))
2018-04-13 17:19:50 +08:00
{
var decoder = Decoder.GetDecoder<Beatmap>(stream);
((LegacyBeatmapDecoder)decoder).ApplyOffsets = false;
return decoder.Decode(stream);
}
}
private Stream openResource(string name)
{
var localPath = Path.GetDirectoryName(Uri.UnescapeDataString(new UriBuilder(Assembly.GetExecutingAssembly().CodeBase).Path));
return Assembly.LoadFrom(Path.Combine(localPath, $"{ResourceAssembly}.dll")).GetManifestResourceStream($@"{ResourceAssembly}.Resources.{name}");
}
2018-06-14 19:28:29 +08:00
/// <summary>
/// Creates the conversion mapping for a <see cref="HitObject"/>. A conversion mapping stores important information about the conversion process.
/// This is generated _after_ the <see cref="HitObject"/> has been converted.
/// <para>
/// This should be used to validate the integrity of the conversion process after a conversion has occurred.
/// </para>
/// </summary>
protected virtual TConvertMapping CreateConvertMapping(HitObject source) => new TConvertMapping();
2018-06-14 19:28:29 +08:00
/// <summary>
/// Creates the conversion value for a <see cref="HitObject"/>. A conversion value stores information about the converted <see cref="HitObject"/>.
/// <para>
/// This should be used to validate the integrity of the converted <see cref="HitObject"/>.
/// </para>
/// </summary>
/// <param name="hitObject">The converted <see cref="HitObject"/>.</param>
2018-04-13 17:19:50 +08:00
protected abstract IEnumerable<TConvertValue> CreateConvertValue(HitObject hitObject);
2018-06-14 19:28:29 +08:00
/// <summary>
/// Creates the <see cref="Ruleset"/> applicable to this <see cref="BeatmapConversionTest{TConvertMapping,TConvertValue}"/>.
/// </summary>
/// <returns></returns>
protected abstract Ruleset CreateRuleset();
2018-04-13 17:19:50 +08:00
private class ConvertResult
{
[JsonProperty]
2018-06-14 19:28:29 +08:00
public List<TConvertMapping> Mappings = new List<TConvertMapping>();
2018-04-13 17:19:50 +08:00
}
private class ConversionWorkingBeatmap : WorkingBeatmap
{
public Action<HitObject, IEnumerable<HitObject>, IBeatmapConverter> ConversionGenerated;
private readonly IBeatmap beatmap;
public ConversionWorkingBeatmap(IBeatmap beatmap)
: base(beatmap.BeatmapInfo, null)
{
this.beatmap = beatmap;
}
protected override IBeatmap GetBeatmap() => beatmap;
protected override Texture GetBackground() => throw new NotImplementedException();
protected override Track GetTrack() => throw new NotImplementedException();
protected override IBeatmapConverter CreateBeatmapConverter(IBeatmap beatmap, Ruleset ruleset)
{
var converter = base.CreateBeatmapConverter(beatmap, ruleset);
converter.ObjectConverted += (orig, converted) => ConversionGenerated?.Invoke(orig, converted, converter);
return converter;
}
}
2018-04-13 17:19:50 +08:00
}
2018-06-14 19:28:29 +08:00
public abstract class BeatmapConversionTest<TConvertValue> : BeatmapConversionTest<ConvertMapping<TConvertValue>, TConvertValue>
where TConvertValue : IEquatable<TConvertValue>
{
}
public class ConvertMapping<TConvertValue> : IEquatable<ConvertMapping<TConvertValue>>
where TConvertValue : IEquatable<TConvertValue>
{
[JsonProperty]
public double StartTime;
[JsonIgnore]
public List<TConvertValue> Objects = new List<TConvertValue>();
[JsonProperty("Objects")]
2019-02-28 12:31:40 +08:00
private List<TConvertValue> setObjects
{
set => Objects = value;
}
2018-06-14 19:28:29 +08:00
2019-12-03 20:16:41 +08:00
public virtual bool Equals(ConvertMapping<TConvertValue> other) => StartTime == other?.StartTime;
2018-06-14 19:28:29 +08:00
}
2018-04-13 17:19:50 +08:00
}