Files
ArchiSteamFarm/ArchiSteamFarm/RuntimeCompatibility.cs

167 lines
5.7 KiB
C#
Raw Permalink Normal View History

2019-02-16 17:34:17 +01:00
// _ _ _ ____ _ _____
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
2019-01-14 19:11:17 +01:00
// |
2020-02-01 23:33:35 +01:00
// Copyright 2015-2020 Łukasz "JustArchi" Domeradzki
2018-07-27 04:52:14 +02:00
// Contact: JustArchi@JustArchi.net
2019-01-14 19:11:17 +01:00
// |
2018-07-27 04:52:14 +02:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2019-01-14 19:11:17 +01:00
// |
2018-07-27 04:52:14 +02:00
// http://www.apache.org/licenses/LICENSE-2.0
2019-01-14 19:11:17 +01:00
// |
2018-07-27 04:52:14 +02:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Diagnostics;
using System.Threading.Tasks;
2019-01-10 23:44:32 +01:00
using JetBrains.Annotations;
2018-10-07 03:27:26 +02:00
#if NETFRAMEWORK
using Microsoft.AspNetCore.Hosting;
2018-08-08 11:27:00 +02:00
using System.Collections.Generic;
using System.IO;
using System.Net.WebSockets;
using System.Security.Cryptography;
using System.Threading;
#endif
namespace ArchiSteamFarm {
2019-01-12 01:24:07 +01:00
[PublicAPI]
public static class RuntimeCompatibility {
#if NETFRAMEWORK
private static readonly DateTime SavedProcessStartTime = DateTime.UtcNow;
#endif
#if NETFRAMEWORK
2019-01-12 01:24:07 +01:00
public static bool IsRunningOnMono => Type.GetType("Mono.Runtime") != null;
#else
public static bool IsRunningOnMono => false;
#endif
2019-01-12 01:24:07 +01:00
public static DateTime ProcessStartTime {
get {
#if NETFRAMEWORK
if (IsRunningOnMono) {
return SavedProcessStartTime;
}
#endif
using Process process = Process.GetCurrentProcess();
return process.StartTime;
}
}
2020-12-05 21:53:18 +01:00
#pragma warning disable CS1998
2019-01-12 01:24:07 +01:00
[PublicAPI]
public static class File {
public static async Task AppendAllTextAsync(string path, string contents) =>
2018-08-08 11:27:00 +02:00
#if NETFRAMEWORK
System.IO.File.AppendAllText(path, contents);
#else
await System.IO.File.AppendAllTextAsync(path, contents).ConfigureAwait(false);
#endif
public static void Move(string sourceFileName, string destFileName, bool overwrite) {
2020-06-13 15:06:07 +02:00
#if NETFRAMEWORK
2020-06-13 17:23:41 +02:00
if (overwrite && System.IO.File.Exists(destFileName)) {
2020-06-13 15:06:07 +02:00
System.IO.File.Delete(destFileName);
}
System.IO.File.Move(sourceFileName, destFileName);
#else
System.IO.File.Move(sourceFileName, destFileName, overwrite);
#endif
}
public static async Task<byte[]> ReadAllBytesAsync(string path) =>
2018-08-08 11:27:00 +02:00
#if NETFRAMEWORK
2018-06-04 16:54:22 +02:00
System.IO.File.ReadAllBytes(path);
#else
2018-06-04 16:54:22 +02:00
await System.IO.File.ReadAllBytesAsync(path).ConfigureAwait(false);
#endif
public static async Task<string> ReadAllTextAsync(string path) =>
2018-08-08 11:27:00 +02:00
#if NETFRAMEWORK
2018-06-04 16:54:22 +02:00
System.IO.File.ReadAllText(path);
#else
2018-06-04 16:54:22 +02:00
await System.IO.File.ReadAllTextAsync(path).ConfigureAwait(false);
#endif
public static async Task WriteAllTextAsync(string path, string contents) =>
2018-08-08 11:27:00 +02:00
#if NETFRAMEWORK
System.IO.File.WriteAllText(path, contents);
#else
await System.IO.File.WriteAllTextAsync(path, contents).ConfigureAwait(false);
#endif
}
2020-12-05 21:53:18 +01:00
#pragma warning restore CS1998
2019-01-12 01:24:07 +01:00
[PublicAPI]
public static class HashCode {
public static int Combine<T1, T2, T3>(T1 value1, T2 value2, T3 value3) =>
2018-12-14 22:27:50 +01:00
#if NETFRAMEWORK
(value1, value2, value3).GetHashCode();
#else
System.HashCode.Combine(value1, value2, value3);
#endif
}
2019-01-12 01:24:07 +01:00
[PublicAPI]
public static class Path {
public static string GetRelativePath(string relativeTo, string path) {
2018-08-08 11:27:00 +02:00
#if NETFRAMEWORK
2018-06-16 06:04:03 +02:00
if (!path.StartsWith(relativeTo, StringComparison.Ordinal)) {
throw new NotImplementedException();
}
string result = path[relativeTo.Length..];
return (result[0] == System.IO.Path.DirectorySeparatorChar) || (result[0] == System.IO.Path.AltDirectorySeparatorChar) ? result[1..] : result;
#else
return System.IO.Path.GetRelativePath(relativeTo, path);
#endif
}
}
2018-06-16 07:47:07 +02:00
2018-08-08 11:27:00 +02:00
#if NETFRAMEWORK
2020-11-14 22:37:00 +01:00
public static Task<byte[]> ComputeHashAsync(this HashAlgorithm hashAlgorithm, Stream inputStream) => Task.FromResult(hashAlgorithm.ComputeHash(inputStream));
2020-11-14 22:37:00 +01:00
public static IWebHostBuilder ConfigureWebHostDefaults(this IWebHostBuilder builder, Action<IWebHostBuilder> configure) {
configure(builder);
return builder;
}
2020-11-14 22:37:00 +01:00
public static bool Contains(this string input, string value, StringComparison comparisonType) => input.IndexOf(value, comparisonType) >= 0;
// ReSharper disable once UseDeconstructionOnParameter - we actually implement deconstruction here
2019-07-26 00:14:47 +02:00
public static void Deconstruct<TKey, TValue>(this KeyValuePair<TKey, TValue> kv, out TKey key, out TValue value) {
key = kv.Key;
value = kv.Value;
}
public static ValueTask DisposeAsync(this IDisposable disposable) {
2020-04-18 16:54:57 +02:00
disposable.Dispose();
2020-11-14 22:37:00 +01:00
return default(ValueTask);
2020-04-18 16:54:57 +02:00
}
public static async Task<WebSocketReceiveResult> ReceiveAsync(this WebSocket webSocket, byte[] buffer, CancellationToken cancellationToken) => await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), cancellationToken).ConfigureAwait(false);
public static async Task SendAsync(this WebSocket webSocket, byte[] buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken cancellationToken) => await webSocket.SendAsync(new ArraySegment<byte>(buffer), messageType, endOfMessage, cancellationToken).ConfigureAwait(false);
2019-01-10 23:44:32 +01:00
public static string[] Split(this string text, char separator, StringSplitOptions options = StringSplitOptions.None) => text.Split(new[] { separator }, options);
2019-07-26 00:14:47 +02:00
public static void TrimExcess<TKey, TValue>(this Dictionary<TKey, TValue> _) { } // no-op
2018-06-16 07:47:07 +02:00
#endif
}
2018-08-08 11:27:00 +02:00
}