1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-12 12:22:56 +08:00

Merge pull request #25627 from Susko3/fix-overlapping-chat-links

Fix overlapping chat links crashing the game
This commit is contained in:
Bartłomiej Dach 2023-12-02 16:36:29 +01:00 committed by GitHub
commit ee2a06be2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 4 deletions

View File

@ -60,7 +60,11 @@ namespace osu.Game.Tests.Visual.Online
[TestCase("http://dev.ppy.sh!", LinkAction.External)] [TestCase("http://dev.ppy.sh!", LinkAction.External)]
[TestCase("forgothttps://dev.ppy.sh!", LinkAction.External)] [TestCase("forgothttps://dev.ppy.sh!", LinkAction.External)]
[TestCase("forgothttp://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("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("Wiki link for tasty [[Performance Points]]", LinkAction.OpenWiki)]
[TestCase("(osu forums)[https://dev.ppy.sh/forum] (old link format)", LinkAction.External)] [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)] [TestCase("[https://dev.ppy.sh/home New site] (new link format)", LinkAction.External)]

View File

@ -12,6 +12,7 @@ using System.Collections.Generic;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Localisation; using osu.Framework.Localisation;
using osu.Framework.Logging;
using osu.Framework.Platform; using osu.Framework.Platform;
using osu.Game.Online; using osu.Game.Online;
using osu.Game.Users; using osu.Game.Users;
@ -47,9 +48,16 @@ namespace osu.Game.Graphics.Containers
foreach (var link in 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]); AddText(text[previousLinkEnd..link.Index]);
string displayText = text.Substring(link.Index, link.Length);
object linkArgument = link.Argument; object linkArgument = link.Argument;
string tooltip = displayText == link.Url ? null : link.Url; string tooltip = displayText == link.Url ? null : link.Url;

View File

@ -85,8 +85,8 @@ namespace osu.Game.Online.Chat
if (escapeChars != null) if (escapeChars != null)
displayText = escapeChars.Aggregate(displayText, (current, c) => current.Replace($"\\{c}", c.ToString())); displayText = escapeChars.Aggregate(displayText, (current, c) => current.Replace($"\\{c}", c.ToString()));
// Check for encapsulated links // Check for overlapping 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) if (!result.Links.Exists(l => l.Overlaps(index, m.Length)))
{ {
result.Text = result.Text.Remove(index, m.Length).Insert(index, displayText); result.Text = result.Text.Remove(index, m.Length).Insert(index, displayText);
@ -364,7 +364,9 @@ namespace osu.Game.Online.Chat
Argument = argument; 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; public int CompareTo(Link? otherLink) => Index > otherLink?.Index ? 1 : -1;
} }