Use another workaround for mono/.NET runtime inconsistencies

This commit is contained in:
Bartłomiej Dach
2021-06-08 23:25:09 +02:00
parent 58837b5e4d
commit 1fdccce978
3 changed files with 30 additions and 4 deletions

View File

@@ -3,4 +3,4 @@ M:System.Object.Equals(System.Object)~System.Boolean;Don't use object.Equals. Us
M:System.ValueType.Equals(System.Object)~System.Boolean;Don't use object.Equals(Fallbacks to ValueType). Use IEquatable<T> or EqualityComparer<T>.Default instead.
T:System.IComparable;Don't use non-generic IComparable. Use generic version instead.
M:System.Enum.HasFlag(System.Enum);Use osu.Framework.Extensions.EnumExtensions.HasFlagFast<T>() instead.
F:System.UriKind.RelativeOrAbsolute;Incompatible results when run on mono, see https://www.mono-project.com/docs/faq/known-issues/urikind-relativeorabsolute/
F:System.UriKind.RelativeOrAbsolute;Incompatible results when run on mono (see https://www.mono-project.com/docs/faq/known-issues/urikind-relativeorabsolute/). Use Validation.TryParseUri(string, out Uri?) instead.

View File

@@ -12,6 +12,7 @@ using osu.Framework.Allocation;
using osu.Framework.Caching;
using osu.Framework.Extensions.EnumExtensions;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Utils;
using osuTK;
namespace osu.Framework.Graphics.Containers.Markdown
@@ -177,10 +178,10 @@ namespace osu.Framework.Graphics.Containers.Markdown
if (string.IsNullOrEmpty(url))
continue;
// Can't use Uri.TryCreate with RelativeOrAbsolute, see https://www.mono-project.com/docs/faq/known-issues/urikind-relativeorabsolute/.
bool isAbsolute = url.Contains("://");
if (!Validation.TryParseUri(url, out Uri linkUri))
continue;
if (isAbsolute)
if (linkUri.IsAbsoluteUri)
continue;
if (documentUri != null)

View File

@@ -1,9 +1,13 @@
// 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.
using System;
using System.Diagnostics.CodeAnalysis;
using osuTK;
using osu.Framework.Graphics;
#nullable enable
namespace osu.Framework.Utils
{
public static class Validation
@@ -23,5 +27,26 @@ namespace osu.Framework.Utils
/// <param name="toCheck">The <see cref="MarginPadding"/> to check.</param>
/// <returns>False if either component of <paramref name="toCheck"/> are Infinity or NaN, true otherwise. </returns>
public static bool IsFinite(MarginPadding toCheck) => float.IsFinite(toCheck.Top) && float.IsFinite(toCheck.Bottom) && float.IsFinite(toCheck.Left) && float.IsFinite(toCheck.Right);
/// <summary>
/// Attempts to parse <paramref name="uriString"/> as an absolute or relative <see cref="Uri"/> in a platform-agnostic manner.
/// </summary>
/// <remarks>
/// This method is a workaround for inconsistencies across .NET and mono runtimes;
/// on mono runtimes paths starting with <c>/</c> are considered absolute as per POSIX,
/// and on .NET such paths are considered to be relative.
/// This method uses the .NET behaviour.
/// For more info, see <a href="https://www.mono-project.com/docs/faq/known-issues/urikind-relativeorabsolute/">Mono documentation</a>.
/// </remarks>
/// <param name="uriString">The string representation of the URI to parse.</param>
/// <param name="result">The resultant parsed URI, if parsing succeeded.</param>
/// <returns><see langword="true"/> if parsing succeeded; <see langword="false"/> otherwise.</returns>
public static bool TryParseUri(string uriString, [NotNullWhen(true)] out Uri? result)
{
#pragma warning disable RS0030 // Bypassing banned API check, as it'll actually be used properly here
UriKind kind = uriString.StartsWith("/", StringComparison.Ordinal) ? UriKind.Relative : UriKind.RelativeOrAbsolute;
#pragma warning restore RS0030
return Uri.TryCreate(uriString, kind, out result);
}
}
}