1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-28 04:02:57 +08:00

Make first non-default skin index a property

The previous code was very brittle - it was not always updating
properly, and seems to have worked either by a carefully crafted set of
circumstances, or just plain coincidence.

Having this be a get-only property avoids potential error in the future
caused by not updating the index properly, at the expense of an added
linear lookup.
This commit is contained in:
Bartłomiej Dach 2021-01-19 18:29:55 +01:00
parent 206a0b8bac
commit b00c6a1d60

View File

@ -38,7 +38,17 @@ namespace osu.Game.Overlays.Settings.Sections
private List<SkinInfo> skinItems; private List<SkinInfo> skinItems;
private int firstNonDefault; private int firstNonDefaultSkinIndex
{
get
{
var index = skinItems.FindIndex(s => s.ID > 0);
if (index < 0)
index = skinItems.Count;
return index;
}
}
[Resolved] [Resolved]
private SkinManager skins { get; set; } private SkinManager skins { get; set; }
@ -114,10 +124,7 @@ namespace osu.Game.Overlays.Settings.Sections
private void updateItems() private void updateItems()
{ {
skinItems = skins.GetAllUsableSkins(); skinItems = skins.GetAllUsableSkins();
firstNonDefault = skinItems.FindIndex(s => s.ID > 0); skinItems.Insert(firstNonDefaultSkinIndex, random_skin_info);
if (firstNonDefault < 0)
firstNonDefault = skinItems.Count;
skinItems.Insert(firstNonDefault, random_skin_info);
skinItems = sortList(skinItems); skinItems = sortList(skinItems);
skinDropdown.Items = skinItems; skinDropdown.Items = skinItems;
} }
@ -141,8 +148,8 @@ namespace osu.Game.Overlays.Settings.Sections
private List<SkinInfo> sortList(List<SkinInfo> skinsList) private List<SkinInfo> sortList(List<SkinInfo> skinsList)
{ {
// Sort user skins seperate from built-in skins // Sort user skins seperate from built-in skins
List<SkinInfo> userSkinsList = skinsList.GetRange(firstNonDefault + 1, skinsList.Count - (firstNonDefault + 1)); List<SkinInfo> userSkinsList = skinsList.GetRange(firstNonDefaultSkinIndex, skinsList.Count - firstNonDefaultSkinIndex);
skinsList.RemoveRange(firstNonDefault + 1, skinsList.Count - (firstNonDefault + 1)); skinsList.RemoveRange(firstNonDefaultSkinIndex, skinsList.Count - firstNonDefaultSkinIndex);
userSkinsList.Sort((a, b) => string.Compare(a.Name, b.Name, StringComparison.OrdinalIgnoreCase)); userSkinsList.Sort((a, b) => string.Compare(a.Name, b.Name, StringComparison.OrdinalIgnoreCase));
skinsList.AddRange(userSkinsList); skinsList.AddRange(userSkinsList);
return skinsList; return skinsList;