Merge pull request #15772 from peppy/remove-model-list-inits

Initialise `BeatmapSet.Beatmaps` list at construction time
This commit is contained in:
Dan Balasescu 2021-11-24 16:41:19 +09:00 committed by GitHub
commit 8ce5324c8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 137 additions and 106 deletions

View File

@ -584,7 +584,7 @@ public async Task TestImportWithDuplicateBeatmapIDs()
{
OnlineID = 1,
Metadata = metadata,
Beatmaps = new List<BeatmapInfo>
Beatmaps =
{
new BeatmapInfo
{

View File

@ -2,7 +2,7 @@
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
@ -143,7 +143,10 @@ private static BeatmapInfo getTestBeatmapInfo(string archiveFile)
var beatmap = decoder.Decode(reader);
info = beatmap.BeatmapInfo;
info.BeatmapSet.Beatmaps = new List<BeatmapInfo> { info };
Debug.Assert(info.BeatmapSet != null);
info.BeatmapSet.Beatmaps.Add(info);
info.BeatmapSet.Metadata = info.Metadata;
info.MD5Hash = stream.ComputeMD5Hash();
info.Hash = stream.ComputeSHA2Hash();

View File

@ -36,7 +36,7 @@ public void TestMusicNavigationActions()
// ensure we have at least two beatmaps available to identify the direction the music controller navigated to.
AddRepeatStep("import beatmap", () => Game.BeatmapManager.Import(new BeatmapSetInfo
{
Beatmaps = new List<BeatmapInfo>
Beatmaps =
{
new BeatmapInfo
{

View File

@ -47,6 +47,21 @@ private void load(GameHost host, AudioManager audio)
beatmaps = new List<BeatmapInfo>();
var metadata = new BeatmapMetadata
{
Artist = "Some Artist",
Title = "Some Beatmap",
AuthorString = "Some Author"
};
var beatmapSetInfo = new BeatmapSetInfo
{
OnlineID = 10,
Hash = Guid.NewGuid().ToString().ComputeMD5Hash(),
Metadata = metadata,
DateAdded = DateTimeOffset.UtcNow
};
for (int i = 0; i < 8; ++i)
{
int beatmapId = 10 * 10 + i;
@ -54,29 +69,21 @@ private void load(GameHost host, AudioManager audio)
int length = RNG.Next(30000, 200000);
double bpm = RNG.NextSingle(80, 200);
beatmaps.Add(new BeatmapInfo
var beatmap = new BeatmapInfo
{
Ruleset = rulesets.GetRuleset(i % 4),
OnlineID = beatmapId,
Length = length,
BPM = bpm,
Metadata = metadata,
BaseDifficulty = new BeatmapDifficulty()
});
};
beatmaps.Add(beatmap);
beatmapSetInfo.Beatmaps.Add(beatmap);
}
manager.Import(new BeatmapSetInfo
{
OnlineID = 10,
Hash = Guid.NewGuid().ToString().ComputeMD5Hash(),
Metadata = new BeatmapMetadata
{
Artist = "Some Artist",
Title = "Some Beatmap",
AuthorString = "Some Author"
},
Beatmaps = beatmaps,
DateAdded = DateTimeOffset.UtcNow
}).Wait();
manager.Import(beatmapSetInfo).Wait();
}
public override void SetUpSteps()

View File

@ -2,7 +2,6 @@
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
@ -42,7 +41,21 @@ private void load(GameHost host, AudioManager audio)
Dependencies.Cache(rulesets = new RulesetStore(ContextFactory));
Dependencies.Cache(manager = new BeatmapManager(LocalStorage, ContextFactory, rulesets, null, audio, Resources, host, Beatmap.Default));
var beatmaps = new List<BeatmapInfo>();
var metadata = new BeatmapMetadata
{
// Create random metadata, then we can check if sorting works based on these
Artist = "Some Artist " + RNG.Next(0, 9),
Title = "Some Song (set id 10)",
AuthorString = "Some Guy " + RNG.Next(0, 9),
};
var beatmapSet = new BeatmapSetInfo
{
OnlineID = 10,
Hash = new MemoryStream(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString())).ComputeMD5Hash(),
Metadata = metadata,
DateAdded = DateTimeOffset.UtcNow,
};
for (int i = 0; i < 6; i++)
{
@ -51,12 +64,13 @@ private void load(GameHost host, AudioManager audio)
int length = RNG.Next(30000, 200000);
double bpm = RNG.NextSingle(80, 200);
beatmaps.Add(new BeatmapInfo
beatmapSet.Beatmaps.Add(new BeatmapInfo
{
Ruleset = new OsuRuleset().RulesetInfo,
OnlineID = beatmapId,
DifficultyName = $"{beatmapId} (length {TimeSpan.FromMilliseconds(length):m\\:ss}, bpm {bpm:0.#})",
Length = length,
Metadata = metadata,
BPM = bpm,
BaseDifficulty = new BeatmapDifficulty
{
@ -65,20 +79,7 @@ private void load(GameHost host, AudioManager audio)
});
}
manager.Import(new BeatmapSetInfo
{
OnlineID = 10,
Hash = new MemoryStream(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString())).ComputeMD5Hash(),
Metadata = new BeatmapMetadata
{
// Create random metadata, then we can check if sorting works based on these
Artist = "Some Artist " + RNG.Next(0, 9),
Title = $"Some Song (set id 10), max bpm {beatmaps.Max(b => b.BPM):0.#})",
AuthorString = "Some Guy " + RNG.Next(0, 9),
},
Beatmaps = beatmaps,
DateAdded = DateTimeOffset.UtcNow,
}).Wait();
manager.Import(beatmapSet).Wait();
}
public override void SetUpSteps()

View File

@ -2,7 +2,6 @@
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using osu.Framework.Screens;
@ -110,7 +109,7 @@ private Func<BeatmapSetInfo> importBeatmap(int i, RulesetInfo ruleset = null)
Hash = Guid.NewGuid().ToString(),
OnlineID = i,
Metadata = metadata,
Beatmaps = new List<BeatmapInfo>
Beatmaps =
{
new BeatmapInfo
{

View File

@ -2,7 +2,6 @@
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using osu.Framework.Screens;
@ -41,7 +40,7 @@ public class TestScenePresentScore : OsuGameTestScene
Hash = Guid.NewGuid().ToString(),
OnlineID = 1,
Metadata = metadata,
Beatmaps = new List<BeatmapInfo>
Beatmaps =
{
new BeatmapInfo
{

View File

@ -836,23 +836,29 @@ private void checkInvisibleDifficultiesUnselectable()
private BeatmapSetInfo createTestBeatmapSet(int id, bool randomDifficultyCount = false)
{
return new BeatmapSetInfo
var metadata = new BeatmapMetadata
{
// Create random metadata, then we can check if sorting works based on these
Artist = $"peppy{id.ToString().PadLeft(6, '0')}",
Title = $"test set #{id}!",
AuthorString = string.Concat(Enumerable.Repeat((char)('z' - Math.Min(25, id - 1)), 5))
};
var beatmapSet = new BeatmapSetInfo
{
ID = id,
OnlineID = id,
Hash = new MemoryStream(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString())).ComputeMD5Hash(),
Metadata = new BeatmapMetadata
{
// Create random metadata, then we can check if sorting works based on these
Artist = $"peppy{id.ToString().PadLeft(6, '0')}",
Title = $"test set #{id}!",
AuthorString = string.Concat(Enumerable.Repeat((char)('z' - Math.Min(25, id - 1)), 5))
},
Beatmaps = getBeatmaps(randomDifficultyCount ? RNG.Next(1, 20) : 3).ToList()
Metadata = metadata,
};
foreach (var b in getBeatmaps(randomDifficultyCount ? RNG.Next(1, 20) : 3, metadata))
beatmapSet.Beatmaps.Add(b);
return beatmapSet;
}
private IEnumerable<BeatmapInfo> getBeatmaps(int count)
private IEnumerable<BeatmapInfo> getBeatmaps(int count, BeatmapMetadata metadata)
{
int id = 0;
@ -872,6 +878,7 @@ private IEnumerable<BeatmapInfo> getBeatmaps(int count)
DifficultyName = version,
StarRating = diff,
Ruleset = new OsuRuleset().RulesetInfo,
Metadata = metadata,
BaseDifficulty = new BeatmapDifficulty
{
OverallDifficulty = diff,
@ -882,19 +889,19 @@ private IEnumerable<BeatmapInfo> getBeatmaps(int count)
private BeatmapSetInfo createTestBeatmapSetWithManyDifficulties(int id)
{
var metadata = new BeatmapMetadata
{
// Create random metadata, then we can check if sorting works based on these
Artist = $"peppy{id.ToString().PadLeft(6, '0')}",
Title = $"test set #{id}!",
AuthorString = string.Concat(Enumerable.Repeat((char)('z' - Math.Min(25, id - 1)), 5))
};
var toReturn = new BeatmapSetInfo
{
ID = id,
OnlineID = id,
Hash = new MemoryStream(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString())).ComputeMD5Hash(),
Metadata = new BeatmapMetadata
{
// Create random metadata, then we can check if sorting works based on these
Artist = $"peppy{id.ToString().PadLeft(6, '0')}",
Title = $"test set #{id}!",
AuthorString = string.Concat(Enumerable.Repeat((char)('z' - Math.Min(25, id - 1)), 5))
},
Beatmaps = new List<BeatmapInfo>(),
Metadata = metadata,
};
for (int b = 1; b < 101; b++)
@ -902,10 +909,10 @@ private BeatmapSetInfo createTestBeatmapSetWithManyDifficulties(int id)
toReturn.Beatmaps.Add(new BeatmapInfo
{
OnlineID = b * 10,
Path = $"extra{b}.osu",
DifficultyName = $"Extra {b}",
Ruleset = rulesets.GetRuleset((b - 1) % 4),
StarRating = 2,
Metadata = metadata,
BaseDifficulty = new BeatmapDifficulty
{
OverallDifficulty = 3.5f,

View File

@ -183,17 +183,18 @@ private BeatmapSetInfo importBeatmapSet(int importID, IEnumerable<RulesetInfo> d
Hash = Guid.NewGuid().ToString(),
OnlineID = importID,
Metadata = metadata,
Beatmaps = difficultyRulesets.Select((ruleset, difficultyIndex) => new BeatmapInfo
{
OnlineID = importID * 1024 + difficultyIndex,
Metadata = metadata,
BaseDifficulty = new BeatmapDifficulty(),
Ruleset = ruleset,
StarRating = difficultyIndex + 1,
DifficultyName = $"SR{difficultyIndex + 1}"
}).ToList()
};
beatmapSet.Beatmaps.AddRange(difficultyRulesets.Select((ruleset, difficultyIndex) => new BeatmapInfo
{
OnlineID = importID * 1024 + difficultyIndex,
Metadata = metadata,
BaseDifficulty = new BeatmapDifficulty(),
Ruleset = ruleset,
StarRating = difficultyIndex + 1,
DifficultyName = $"SR{difficultyIndex + 1}"
}));
return Game.BeatmapManager.Import(beatmapSet).Result.Value;
}

View File

@ -907,7 +907,21 @@ private BeatmapSetInfo createTestBeatmapSet(RulesetInfo[] rulesets, int countPer
int setId = getImportId();
var beatmaps = new List<BeatmapInfo>();
var metadata = new BeatmapMetadata
{
// Create random metadata, then we can check if sorting works based on these
Artist = "Some Artist " + RNG.Next(0, 9),
Title = $"Some Song (set id {setId})",
AuthorString = "Some Guy " + RNG.Next(0, 9),
};
var beatmapSet = new BeatmapSetInfo
{
OnlineID = setId,
Hash = new MemoryStream(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString())).ComputeMD5Hash(),
DateAdded = DateTimeOffset.UtcNow,
Metadata = metadata
};
for (int i = 0; i < countPerRuleset; i++)
{
@ -916,12 +930,13 @@ private BeatmapSetInfo createTestBeatmapSet(RulesetInfo[] rulesets, int countPer
int length = RNG.Next(30000, 200000);
double bpm = RNG.NextSingle(80, 200);
beatmaps.Add(new BeatmapInfo
beatmapSet.Beatmaps.Add(new BeatmapInfo
{
Ruleset = getRuleset(),
OnlineID = beatmapId,
DifficultyName = $"{beatmapId} (length {TimeSpan.FromMilliseconds(length):m\\:ss}, bpm {bpm:0.#})",
Length = length,
Metadata = metadata,
BPM = bpm,
BaseDifficulty = new BeatmapDifficulty
{
@ -930,20 +945,7 @@ private BeatmapSetInfo createTestBeatmapSet(RulesetInfo[] rulesets, int countPer
});
}
return new BeatmapSetInfo
{
OnlineID = setId,
Hash = new MemoryStream(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString())).ComputeMD5Hash(),
Metadata = new BeatmapMetadata
{
// Create random metadata, then we can check if sorting works based on these
Artist = "Some Artist " + RNG.Next(0, 9),
Title = $"Some Song (set id {setId}, max bpm {beatmaps.Max(b => b.BPM):0.#})",
AuthorString = "Some Guy " + RNG.Next(0, 9),
},
Beatmaps = beatmaps,
DateAdded = DateTimeOffset.UtcNow,
};
return beatmapSet;
}
protected override void Dispose(bool isDisposing)

View File

@ -86,7 +86,7 @@ public WorkingBeatmap CreateNew(RulesetInfo ruleset, APIUser user)
var set = new BeatmapSetInfo
{
Metadata = metadata,
Beatmaps = new List<BeatmapInfo>
Beatmaps =
{
new BeatmapInfo
{

View File

@ -82,7 +82,7 @@ public BeatmapModelManager(Storage storage, IDatabaseContextFactory contextFacto
protected override async Task Populate(BeatmapSetInfo beatmapSet, ArchiveReader archive, CancellationToken cancellationToken = default)
{
if (archive != null)
beatmapSet.Beatmaps = createBeatmapDifficulties(beatmapSet.Files);
beatmapSet.Beatmaps.AddRange(createBeatmapDifficulties(beatmapSet.Files));
foreach (BeatmapInfo b in beatmapSet.Beatmaps)
{
@ -370,7 +370,6 @@ protected override BeatmapSetInfo CreateModel(ArchiveReader reader)
return new BeatmapSetInfo
{
OnlineID = beatmap.BeatmapInfo.BeatmapSet?.OnlineID,
Beatmaps = new List<BeatmapInfo>(),
Metadata = beatmap.Metadata,
DateAdded = DateTimeOffset.UtcNow
};

View File

@ -6,6 +6,7 @@
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using JetBrains.Annotations;
using Newtonsoft.Json;
using osu.Framework.Testing;
using osu.Game.Database;
using osu.Game.Extensions;
@ -30,7 +31,8 @@ public int? OnlineID
public BeatmapMetadata Metadata { get; set; }
public List<BeatmapInfo> Beatmaps { get; set; }
[NotNull]
public List<BeatmapInfo> Beatmaps { get; } = new List<BeatmapInfo>();
public BeatmapSetOnlineStatus Status { get; set; } = BeatmapSetOnlineStatus.None;
@ -40,17 +42,20 @@ public int? OnlineID
/// <summary>
/// The maximum star difficulty of all beatmaps in this set.
/// </summary>
public double MaxStarDifficulty => Beatmaps?.Max(b => b.StarRating) ?? 0;
[JsonIgnore]
public double MaxStarDifficulty => Beatmaps.Count == 0 ? 0 : Beatmaps.Max(b => b.StarRating);
/// <summary>
/// The maximum playable length in milliseconds of all beatmaps in this set.
/// </summary>
public double MaxLength => Beatmaps?.Max(b => b.Length) ?? 0;
[JsonIgnore]
public double MaxLength => Beatmaps.Count == 0 ? 0 : Beatmaps.Max(b => b.Length);
/// <summary>
/// The maximum BPM of all beatmaps in this set.
/// </summary>
public double MaxBPM => Beatmaps?.Max(b => b.BPM) ?? 0;
[JsonIgnore]
public double MaxBPM => Beatmaps.Count == 0 ? 0 : Beatmaps.Max(b => b.BPM);
[NotMapped]
public bool DeletePending { get; set; }

View File

@ -55,8 +55,6 @@ public WorkingBeatmapCache(ITrackStore trackStore, AudioManager audioManager, IR
public void Invalidate(BeatmapSetInfo info)
{
if (info.Beatmaps == null) return;
foreach (var b in info.Beatmaps)
Invalidate(b);
}

View File

@ -77,7 +77,7 @@ private void load(OsuColour colours, Bindable<WorkingBeatmap> beatmap)
filter.Search.OnCommit += (sender, newText) =>
{
BeatmapInfo toSelect = list.FirstVisibleSet?.Beatmaps?.FirstOrDefault();
BeatmapInfo toSelect = list.FirstVisibleSet?.Beatmaps.FirstOrDefault();
if (toSelect != null)
{

View File

@ -2,7 +2,7 @@
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
@ -33,12 +33,15 @@ public TestBeatmap(RulesetInfo ruleset, bool withHitObjects = true)
BeatmapInfo.Ruleset = ruleset;
BeatmapInfo.RulesetID = ruleset.ID ?? 0;
BeatmapInfo.BeatmapSet.Metadata = BeatmapInfo.Metadata;
BeatmapInfo.BeatmapSet.Beatmaps = new List<BeatmapInfo> { BeatmapInfo };
BeatmapInfo.BeatmapSet.OnlineID = Interlocked.Increment(ref onlineSetID);
BeatmapInfo.Length = 75000;
BeatmapInfo.OnlineInfo = new APIBeatmap();
BeatmapInfo.OnlineID = Interlocked.Increment(ref onlineBeatmapID);
Debug.Assert(BeatmapInfo.BeatmapSet != null);
BeatmapInfo.BeatmapSet.Metadata = BeatmapInfo.Metadata;
BeatmapInfo.BeatmapSet.Beatmaps.Add(BeatmapInfo);
BeatmapInfo.BeatmapSet.OnlineID = Interlocked.Increment(ref onlineSetID);
}
protected virtual Beatmap CreateBeatmap() => createTestBeatmap();

View File

@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
@ -220,6 +221,8 @@ protected APIBeatmapSet CreateAPIBeatmapSet(RulesetInfo ruleset = null)
{
var beatmap = CreateBeatmap(ruleset ?? Ruleset.Value).BeatmapInfo;
Debug.Assert(beatmap.BeatmapSet != null);
return new APIBeatmapSet
{
OnlineID = ((IBeatmapSetInfo)beatmap.BeatmapSet).OnlineID,
@ -230,13 +233,17 @@ protected APIBeatmapSet CreateAPIBeatmapSet(RulesetInfo ruleset = null)
Card = "https://assets.ppy.sh/beatmaps/163112/covers/card.jpg",
List = "https://assets.ppy.sh/beatmaps/163112/covers/list.jpg"
},
Title = beatmap.BeatmapSet.Metadata.Title,
TitleUnicode = beatmap.BeatmapSet.Metadata.TitleUnicode,
Artist = beatmap.BeatmapSet.Metadata.Artist,
ArtistUnicode = beatmap.BeatmapSet.Metadata.ArtistUnicode,
Author = beatmap.BeatmapSet.Metadata.Author,
Source = beatmap.BeatmapSet.Metadata.Source,
Tags = beatmap.BeatmapSet.Metadata.Tags,
Title = beatmap.Metadata.Title,
TitleUnicode = beatmap.Metadata.TitleUnicode,
Artist = beatmap.Metadata.Artist,
ArtistUnicode = beatmap.Metadata.ArtistUnicode,
Author = new APIUser
{
Username = beatmap.Metadata.Author.Username,
Id = beatmap.Metadata.Author.OnlineID
},
Source = beatmap.Metadata.Source,
Tags = beatmap.Metadata.Tags,
Beatmaps = new[]
{
new APIBeatmap