Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix potential crash when attempting to create mod display using null ruleset #19211

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion osu.Game/Online/API/APIMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Humanizer;
using MessagePack;
Expand Down Expand Up @@ -48,7 +49,7 @@ public APIMod(Mod mod)
}
}

public Mod ToMod(Ruleset ruleset)
public Mod ToMod([NotNull] Ruleset ruleset)
{
Mod resultMod = ruleset.CreateModFromAcronym(Acronym);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// 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.

#nullable disable

using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
Expand All @@ -22,6 +21,7 @@
using osu.Game.Online.Multiplayer;
using osu.Game.Online.Rooms;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
using osu.Game.Screens.Play.HUD;
using osu.Game.Users;
using osu.Game.Users.Drawables;
Expand All @@ -35,18 +35,18 @@ public class ParticipantPanel : MultiplayerRoomComposite, IHasContextMenu
public readonly MultiplayerRoomUser User;

[Resolved]
private IAPIProvider api { get; set; }
private IAPIProvider api { get; set; } = null!;

[Resolved]
private IRulesetStore rulesets { get; set; }
private IRulesetStore rulesets { get; set; } = null!;

private SpriteIcon crown;
private SpriteIcon crown = null!;

private OsuSpriteText userRankText;
private ModDisplay userModsDisplay;
private StateDisplay userStateDisplay;
private OsuSpriteText userRankText = null!;
private ModDisplay userModsDisplay = null!;
private StateDisplay userStateDisplay = null!;

private IconButton kickButton;
private IconButton kickButton = null!;

public ParticipantPanel(MultiplayerRoomUser user)
{
Expand Down Expand Up @@ -135,7 +135,7 @@ private void load()
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Font = OsuFont.GetFont(weight: FontWeight.Bold, size: 18),
Text = user?.Username
Text = user?.Username ?? string.Empty
},
userRankText = new OsuSpriteText
{
Expand Down Expand Up @@ -188,7 +188,7 @@ protected override void OnRoomUpdated()
const double fade_time = 50;

var currentItem = Playlist.GetCurrentItem();
var ruleset = currentItem != null ? rulesets.GetRuleset(currentItem.RulesetID)?.CreateInstance() : null;
Ruleset? ruleset = currentItem != null ? rulesets.GetRuleset(currentItem.RulesetID)?.CreateInstance() : null;

int? currentModeRank = ruleset != null ? User.User?.RulesetsStatistics?.GetValueOrDefault(ruleset.ShortName)?.GlobalRank : null;
userRankText.Text = currentModeRank != null ? $"#{currentModeRank.Value:N0}" : string.Empty;
Expand All @@ -205,10 +205,13 @@ protected override void OnRoomUpdated()

// If the mods are updated at the end of the frame, the flow container will skip a reflow cycle: https://github.com/ppy/osu-framework/issues/4187
// This looks particularly jarring here, so re-schedule the update to that start of our frame as a fix.
Schedule(() => userModsDisplay.Current.Value = User.Mods.Select(m => m.ToMod(ruleset)).ToList());
Schedule(() =>
{
userModsDisplay.Current.Value = ruleset != null ? User.Mods.Select(m => m.ToMod(ruleset)).ToList() : Array.Empty<Mod>();
});
}

public MenuItem[] ContextMenuItems
public MenuItem[]? ContextMenuItems
{
get
{
Expand Down