Skip to content

Commit

Permalink
Merge pull request #19211 from peppy/fix-multiplayer-participant-pane…
Browse files Browse the repository at this point in the history
…l-null-ruleset

Fix potential crash when attempting to create mod display using null ruleset
  • Loading branch information
peppy authored Jul 18, 2022
2 parents e14e2cc + f1133ca commit 3c35ef5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
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

0 comments on commit 3c35ef5

Please sign in to comment.