Skip to content

Commit

Permalink
Merge pull request #24511 from bdach/legacy-exporter-not-converting
Browse files Browse the repository at this point in the history
Fix legacy beatmap exporter not converting beatmap between decode and re-encode
  • Loading branch information
peppy authored Aug 12, 2023
2 parents 896cbb0 + 2533924 commit 7fa3508
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion osu.Desktop/LegacyIpc/LegacyTcpIpcProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private object onLegacyIpcMessageReceived(object message)
case LegacyIpcDifficultyCalculationRequest req:
try
{
WorkingBeatmap beatmap = new FlatFileWorkingBeatmap(req.BeatmapFile);
WorkingBeatmap beatmap = new FlatWorkingBeatmap(req.BeatmapFile);
var ruleset = beatmap.BeatmapInfo.Ruleset.CreateInstance();
Mod[] mods = ruleset.ConvertFromLegacyMods((LegacyMods)req.Mods).ToArray();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,26 @@
namespace osu.Game.Beatmaps
{
/// <summary>
/// A <see cref="WorkingBeatmap"/> which can be constructed directly from a .osu file, providing an implementation for
/// A <see cref="WorkingBeatmap"/> which can be constructed directly from an .osu file (via <see cref="FlatWorkingBeatmap(string, int?)"/>)
/// or an <see cref="IBeatmap"/> instance (via <see cref="FlatWorkingBeatmap(IBeatmap)"/>,
/// providing an implementation for
/// <see cref="WorkingBeatmap.GetPlayableBeatmap(osu.Game.Rulesets.IRulesetInfo,System.Collections.Generic.IReadOnlyList{osu.Game.Rulesets.Mods.Mod})"/>.
/// </summary>
public class FlatFileWorkingBeatmap : WorkingBeatmap
public class FlatWorkingBeatmap : WorkingBeatmap
{
private readonly Beatmap beatmap;
private readonly IBeatmap beatmap;

public FlatFileWorkingBeatmap(string file, int? beatmapId = null)
: this(readFromFile(file), beatmapId)
public FlatWorkingBeatmap(string file, int? beatmapId = null)
: this(readFromFile(file))
{
if (beatmapId.HasValue)
beatmap.BeatmapInfo.OnlineID = beatmapId.Value;
}

private FlatFileWorkingBeatmap(Beatmap beatmap, int? beatmapId = null)
public FlatWorkingBeatmap(IBeatmap beatmap)
: base(beatmap.BeatmapInfo, null)
{
this.beatmap = beatmap;

if (beatmapId.HasValue)
beatmap.BeatmapInfo.OnlineID = beatmapId.Value;
}

private static Beatmap readFromFile(string filename)
Expand Down
13 changes: 8 additions & 5 deletions osu.Game/Database/LegacyBeatmapExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public LegacyBeatmapExporter(Storage storage)

protected override Stream? GetFileContents(BeatmapSetInfo model, INamedFileUsage file)
{
bool isBeatmap = model.Beatmaps.Any(o => o.Hash == file.File.Hash);
var beatmapInfo = model.Beatmaps.SingleOrDefault(o => o.Hash == file.File.Hash);

if (!isBeatmap)
if (beatmapInfo == null)
return base.GetFileContents(model, file);

// Read the beatmap contents and skin
Expand All @@ -43,6 +43,9 @@ public LegacyBeatmapExporter(Storage storage)
using var contentStreamReader = new LineBufferedReader(contentStream);
var beatmapContent = new LegacyBeatmapDecoder().Decode(contentStreamReader);

var workingBeatmap = new FlatWorkingBeatmap(beatmapContent);
var playableBeatmap = workingBeatmap.GetPlayableBeatmap(beatmapInfo.Ruleset);

using var skinStream = base.GetFileContents(model, file);

if (skinStream == null)
Expand All @@ -56,10 +59,10 @@ public LegacyBeatmapExporter(Storage storage)

// Convert beatmap elements to be compatible with legacy format
// So we truncate time and position values to integers, and convert paths with multiple segments to bezier curves
foreach (var controlPoint in beatmapContent.ControlPointInfo.AllControlPoints)
foreach (var controlPoint in playableBeatmap.ControlPointInfo.AllControlPoints)
controlPoint.Time = Math.Floor(controlPoint.Time);

foreach (var hitObject in beatmapContent.HitObjects)
foreach (var hitObject in playableBeatmap.HitObjects)
{
// Truncate end time before truncating start time because end time is dependent on start time
if (hitObject is IHasDuration hasDuration && hitObject is not IHasPath)
Expand All @@ -86,7 +89,7 @@ public LegacyBeatmapExporter(Storage storage)
// Encode to legacy format
var stream = new MemoryStream();
using (var sw = new StreamWriter(stream, Encoding.UTF8, 1024, true))
new LegacyBeatmapEncoder(beatmapContent, beatmapSkin).Encode(sw);
new LegacyBeatmapEncoder(playableBeatmap, beatmapSkin).Encode(sw);

stream.Seek(0, SeekOrigin.Begin);

Expand Down

0 comments on commit 7fa3508

Please sign in to comment.