Skip to content

Commit

Permalink
Fix target-gated #ifs to cover every applicable TFM
Browse files Browse the repository at this point in the history
  • Loading branch information
YoshiRulz committed Jun 6, 2024
1 parent 7691f2f commit e468d6e
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 28 deletions.
21 changes: 14 additions & 7 deletions src/BizHawk.Client.Common/Api/Classes/UserDataApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@
using System.Collections.Generic;
using System.Linq;

#if NET5_0_OR_GREATER
using KeyCollectionType = System.Collections.Generic.IReadOnlySet<string>;
#else
using KeyCollectionType = System.Collections.Generic.IReadOnlyCollection<string>;
#endif

namespace BizHawk.Client.Common
{
public sealed class UserDataApi : IUserDataApi
{
private readonly IMovieSession _movieSession;

#if NET6_0
public IReadOnlySet<string> Keys
=> throw new NotImplementedException();
#else
public IReadOnlyCollection<string> Keys
=> _movieSession.UserBag.Keys.ToList();
#endif
public KeyCollectionType Keys
{
get
{
ICollection<string> keys = _movieSession.UserBag.Keys;
return (keys as KeyCollectionType) ?? keys.ToList();
}
}

public UserDataApi(IMovieSession movieSession) => _movieSession = movieSession;

Expand Down
2 changes: 1 addition & 1 deletion src/BizHawk.Client.Common/Api/Interfaces/IUserDataApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace BizHawk.Client.Common
{
public interface IUserDataApi : IExternalApi
{
#if NET6_0
#if NET5_0_OR_GREATER
IReadOnlySet<string> Keys { get; }
#else
IReadOnlyCollection<string> Keys { get; }
Expand Down
2 changes: 1 addition & 1 deletion src/BizHawk.Client.Common/movie/bk2/Bk2Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void SetFromMnemonic(string mnemonic)
else if (key.IsAxis)
{
var commaIndex = mnemonic.IndexOf(',', iterator);
#if NET6_0_OR_GREATER
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
var val = int.Parse(mnemonic.AsSpan(start: iterator, length: commaIndex - iterator));
#else
var axisValueString = mnemonic.Substring(startIndex: iterator, length: commaIndex - iterator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public void Insert(int index, Watch watch)
/// </param>
public void InsertRange(int index, IEnumerable<Watch> collection)
{
#if NET6_0
#if NET6_0_OR_GREATER
if (collection.TryGetNonEnumeratedCount(out var n) && n is 0) return;
#else
if (collection is ICollection<Watch> hasCount && hasCount.Count is 0) return;
Expand Down
2 changes: 1 addition & 1 deletion src/BizHawk.Common/Extensions/CollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public static bool CountIsExactly<T>(this IEnumerable<T> collection, int n)
? countable.Count == n
: collection.Take(n + 1).Count() == n;

#if !NET6_0
#if !(NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_0_OR_GREATER)
/// <summary>
/// Returns the value at <paramref name="key"/>.
/// If the key is not present, returns default(TValue).
Expand Down
4 changes: 2 additions & 2 deletions src/BizHawk.Common/MemoryBlock/MemoryViewStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public override void Flush()
private byte* CurrentPointer()
=> (byte*)Z.SS(_ptr + _pos);

#if NET6_0
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
public override int Read(Span<byte> buffer)
#else
public int Read(Span<byte> buffer)
Expand Down Expand Up @@ -110,7 +110,7 @@ public override long Seek(long offset, SeekOrigin origin)
public override void SetLength(long value)
=> throw new IOException();

#if NET6_0
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
public override void Write(ReadOnlySpan<byte> buffer)
#else
public void Write(ReadOnlySpan<byte> buffer)
Expand Down
6 changes: 3 additions & 3 deletions src/BizHawk.Common/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.IO.Compression;
using System.Linq;
using System.Reflection;
#if NET6_0
#if NETCOREAPP3_0_OR_GREATER
using System.Runtime.CompilerServices;
#endif
using System.Threading;
Expand Down Expand Up @@ -81,15 +81,15 @@ public static bool DictionaryEqual<TKey, TValue>(IDictionary<TKey, TValue> a, ID
return a.All(kvp => b.TryGetValue(kvp.Key, out var bVal) && comparer.Equals(kvp.Value, bVal));
}

#if NET6_0
#if NETCOREAPP3_0_OR_GREATER
public static string DescribeIsNull<T>(T? obj, [CallerArgumentExpression("obj")] string? expr = default)
#else
public static string DescribeIsNull<T>(T? obj, string expr)
#endif
where T : class
=> $"{expr} is {(obj is null ? "null" : "not null")}";

#if NET6_0
#if NETCOREAPP3_0_OR_GREATER
public static string DescribeIsNullValT<T>(T? boxed, [CallerArgumentExpression("boxed")] string? expr = default)
#else
public static string DescribeIsNullValT<T>(T? boxed, string expr)
Expand Down
2 changes: 1 addition & 1 deletion src/BizHawk.Common/checksums/MD5Checksum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static class MD5Checksum

public /*static readonly*/const string EmptyFile = "D41D8CD98F00B204E9800998ECF8427E";

#if NET6_0
#if NET5_0_OR_GREATER
public static byte[] Compute(ReadOnlySpan<byte> data)
=> MD5.HashData(data);
#else
Expand Down
20 changes: 11 additions & 9 deletions src/BizHawk.Common/checksums/SHA1Checksum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,9 @@ public static class SHA1Checksum

public /*static readonly*/const string Zero = "0000000000000000000000000000000000000000";

#if NET6_0
#if NET5_0_OR_GREATER
public static byte[] Compute(ReadOnlySpan<byte> data)
=> SHA1.HashData(data);

public static byte[] ComputeConcat(ReadOnlySpan<byte> dataA, ReadOnlySpan<byte> dataB)
{
using var impl = IncrementalHash.CreateHash(HashAlgorithmName.SHA1);
impl.AppendData(dataA);
impl.AppendData(dataB);
return impl.GetHashAndReset();
}
#else
private static unsafe byte[] UnmanagedImpl(byte[] buffer)
{
Expand Down Expand Up @@ -88,7 +80,17 @@ public static string ComputePrefixedHex(byte[] data)

public static byte[] Compute(ReadOnlySpan<byte> data)
=> Compute(data.ToArray());
#endif

#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
public static byte[] ComputeConcat(ReadOnlySpan<byte> dataA, ReadOnlySpan<byte> dataB)
{
using var impl = IncrementalHash.CreateHash(HashAlgorithmName.SHA1);
impl.AppendData(dataA);
impl.AppendData(dataB);
return impl.GetHashAndReset();
}
#else
public static byte[] ComputeConcat(ReadOnlySpan<byte> dataA, ReadOnlySpan<byte> dataB)
=> ComputeConcat(dataA.ToArray(), dataB.ToArray());
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/BizHawk.Common/checksums/SHA256Checksum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static class SHA256Checksum

public /*static readonly*/const string EmptyFile = "E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855";

#if NET6_0
#if NET5_0_OR_GREATER
public static byte[] Compute(ReadOnlySpan<byte> data)
=> SHA256.HashData(data);
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public sealed class SerializationStabilityTests

private const string ZWINDER_SER = @"{""CurrentUseCompression"":false,""CurrentBufferSize"":256,""CurrentTargetFrameLength"":500,""CurrentStoreType"":0,""RecentUseCompression"":false,""RecentBufferSize"":128,""RecentTargetFrameLength"":2000,""RecentStoreType"":0,""GapsUseCompression"":false,""GapsBufferSize"":64,""GapsTargetFrameLength"":125,""GapsStoreType"":0,""AncientStateInterval"":5000,""AncientStoreType"":0}";

#if NET6_0
#if NET5_0_OR_GREATER
private static readonly IReadOnlySet<Type> KnownGoodFromStdlib = new HashSet<Type>
#else
private static readonly ICollection<Type> KnownGoodFromStdlib = new HashSet<Type>
Expand Down

0 comments on commit e468d6e

Please sign in to comment.