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 overlapping chat links crashing the game #25627

Merged
merged 7 commits into from
Dec 2, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions osu.Game.Tests/Visual/Online/TestSceneChatLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ private void load(OsuColour colours)
[TestCase("http://dev.ppy.sh!", LinkAction.External)]
[TestCase("forgothttps://dev.ppy.sh!", LinkAction.External)]
[TestCase("forgothttp://dev.ppy.sh!", LinkAction.External)]
[TestCase("00:12:345 - Test?", LinkAction.OpenEditorTimestamp)]
[TestCase("00:12:345 (1,2) - Test?", LinkAction.OpenEditorTimestamp)]
[TestCase($"{OsuGameBase.OSU_PROTOCOL}edit/00:12:345 - Test?", LinkAction.OpenEditorTimestamp)]
[TestCase($"{OsuGameBase.OSU_PROTOCOL}edit/00:12:345 (1,2) - Test?", LinkAction.OpenEditorTimestamp)]
[TestCase($"{OsuGameBase.OSU_PROTOCOL}00:12:345 - not an editor timestamp", LinkAction.External)]
[TestCase("Wiki link for tasty [[Performance Points]]", LinkAction.OpenWiki)]
[TestCase("(osu forums)[https://dev.ppy.sh/forum] (old link format)", LinkAction.External)]
[TestCase("[https://dev.ppy.sh/home New site] (new link format)", LinkAction.External)]
Expand Down
10 changes: 9 additions & 1 deletion osu.Game/Graphics/Containers/LinkFlowContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Localisation;
using osu.Framework.Logging;
using osu.Framework.Platform;
using osu.Game.Online;
using osu.Game.Users;
Expand Down Expand Up @@ -47,9 +48,16 @@ public void AddLinks(string text, List<Link> links)

foreach (var link in links)
{
string displayText = text.Substring(link.Index, link.Length);

if (previousLinkEnd > link.Index)
{
Logger.Log($@"Link ""{link.Url}"" with text ""{displayText}"" overlaps previous link, ignoring.");
continue;
}

AddText(text[previousLinkEnd..link.Index]);

string displayText = text.Substring(link.Index, link.Length);
object linkArgument = link.Argument;
string tooltip = displayText == link.Url ? null : link.Url;

Expand Down
8 changes: 5 additions & 3 deletions osu.Game/Online/Chat/MessageFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ private static void handleMatches(Regex regex, string display, string link, Mess
if (escapeChars != null)
displayText = escapeChars.Aggregate(displayText, (current, c) => current.Replace($"\\{c}", c.ToString()));

// Check for encapsulated links
if (result.Links.Find(l => (l.Index <= index && l.Index + l.Length >= index + m.Length) || (index <= l.Index && index + m.Length >= l.Index + l.Length)) == null)
// Check for overlapping links
if (!result.Links.Exists(l => l.Overlaps(index, m.Length)))
{
result.Text = result.Text.Remove(index, m.Length).Insert(index, displayText);

Expand Down Expand Up @@ -364,7 +364,9 @@ public Link(string url, int startIndex, int length, LinkAction action, object ar
Argument = argument;
}

public bool Overlaps(Link otherLink) => Index < otherLink.Index + otherLink.Length && otherLink.Index < Index + Length;
public bool Overlaps(Link otherLink) => Overlaps(otherLink.Index, otherLink.Length);

public bool Overlaps(int otherIndex, int otherLength) => Index < otherIndex + otherLength && otherIndex < Index + Length;

public int CompareTo(Link? otherLink) => Index > otherLink?.Index ? 1 : -1;
}
Expand Down