2017-11-18 17:27:06 +01:00
|
|
|
|
// _ _ _ ____ _ _____
|
|
|
|
|
|
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
|
|
|
|
|
|
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
|
|
|
|
|
|
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
|
|
|
|
|
|
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
|
|
|
|
|
|
//
|
|
|
|
|
|
// Copyright 2015-2017 Łukasz "JustArchi" Domeradzki
|
|
|
|
|
|
// Contact: JustArchi@JustArchi.net
|
|
|
|
|
|
//
|
|
|
|
|
|
// 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
|
|
|
|
|
|
//
|
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
//
|
|
|
|
|
|
// 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.
|
2015-10-28 19:21:27 +01:00
|
|
|
|
|
2016-04-01 20:18:21 +02:00
|
|
|
|
using System;
|
2017-01-28 15:29:38 +01:00
|
|
|
|
using System.Collections.Generic;
|
2016-05-13 19:20:24 +02:00
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2017-01-11 15:22:00 +01:00
|
|
|
|
using System.Globalization;
|
2016-04-01 20:18:21 +02:00
|
|
|
|
using System.Linq;
|
2016-04-12 07:40:02 +02:00
|
|
|
|
using System.Net;
|
2016-08-19 05:25:08 +02:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2017-08-05 22:35:03 +02:00
|
|
|
|
using System.Text;
|
2017-07-03 18:54:57 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2017-04-08 05:05:09 +02:00
|
|
|
|
using Humanizer;
|
2017-09-23 06:44:37 +02:00
|
|
|
|
using Humanizer.Localisation;
|
2015-10-25 06:16:50 +01:00
|
|
|
|
|
|
|
|
|
|
namespace ArchiSteamFarm {
|
|
|
|
|
|
internal static class Utilities {
|
2017-07-02 10:00:02 +02:00
|
|
|
|
private static readonly Random Random = new Random();
|
2016-12-23 03:37:29 +01:00
|
|
|
|
|
2016-05-13 19:20:24 +02:00
|
|
|
|
[SuppressMessage("ReSharper", "UnusedParameter.Global")]
|
2017-09-21 03:16:42 +02:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2016-10-07 00:04:08 +02:00
|
|
|
|
internal static void Forget(this object obj) { }
|
2016-03-06 22:14:02 +01:00
|
|
|
|
|
2017-09-26 01:04:05 +02:00
|
|
|
|
internal static string GetArgsString(string[] args, byte argsToSkip = 1, string delimiter = " ") {
|
|
|
|
|
|
if ((args == null) || (args.Length < argsToSkip) || string.IsNullOrEmpty(delimiter)) {
|
|
|
|
|
|
ASF.ArchiLogger.LogNullError(nameof(args) + " || " + nameof(delimiter));
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string result = string.Join(delimiter, GetArgs(args, argsToSkip));
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-13 06:32:42 +02:00
|
|
|
|
internal static string GetCookieValue(this CookieContainer cookieContainer, string url, string name) {
|
2017-07-03 18:54:57 +02:00
|
|
|
|
if ((cookieContainer == null) || string.IsNullOrEmpty(url) || string.IsNullOrEmpty(name)) {
|
|
|
|
|
|
ASF.ArchiLogger.LogNullError(nameof(cookieContainer) + " || " + nameof(url) + " || " + nameof(name));
|
2016-05-30 01:57:06 +02:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Uri uri;
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
uri = new Uri(url);
|
|
|
|
|
|
} catch (UriFormatException e) {
|
2017-01-31 01:10:01 +01:00
|
|
|
|
ASF.ArchiLogger.LogGenericException(e);
|
2016-04-12 07:40:02 +02:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-30 01:57:06 +02:00
|
|
|
|
CookieCollection cookies = cookieContainer.GetCookies(uri);
|
2016-12-23 03:47:12 +01:00
|
|
|
|
return cookies.Count != 0 ? (from Cookie cookie in cookies where cookie.Name.Equals(name) select cookie.Value).FirstOrDefault() : null;
|
2016-04-12 07:40:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-05 17:01:18 +02:00
|
|
|
|
internal static uint GetUnixTime() => (uint) DateTimeOffset.UtcNow.ToUnixTimeSeconds();
|
2016-12-23 03:37:29 +01:00
|
|
|
|
|
2017-01-11 15:22:00 +01:00
|
|
|
|
internal static bool IsValidHexadecimalString(string text) {
|
|
|
|
|
|
if (string.IsNullOrEmpty(text)) {
|
2017-01-31 01:10:01 +01:00
|
|
|
|
ASF.ArchiLogger.LogNullError(nameof(text));
|
2017-01-11 15:22:00 +01:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const byte split = 16;
|
|
|
|
|
|
for (byte i = 0; i < text.Length; i += split) {
|
|
|
|
|
|
string textPart = string.Join("", text.Skip(i).Take(split));
|
|
|
|
|
|
|
2017-04-05 17:23:18 +02:00
|
|
|
|
if (!ulong.TryParse(textPart, NumberStyles.HexNumber, null, out _)) {
|
2017-01-11 15:22:00 +01:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-07-02 10:00:02 +02:00
|
|
|
|
internal static int RandomNext() {
|
|
|
|
|
|
lock (Random) {
|
|
|
|
|
|
return Random.Next();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-05 22:35:03 +02:00
|
|
|
|
internal static string ReadLineMasked(char mask = '*') {
|
|
|
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
|
|
ConsoleKeyInfo keyInfo;
|
|
|
|
|
|
while ((keyInfo = Console.ReadKey(true)).Key != ConsoleKey.Enter) {
|
|
|
|
|
|
if (!char.IsControl(keyInfo.KeyChar)) {
|
|
|
|
|
|
result.Append(keyInfo.KeyChar);
|
|
|
|
|
|
Console.Write(mask);
|
|
|
|
|
|
} else if ((keyInfo.Key == ConsoleKey.Backspace) && (result.Length > 0)) {
|
|
|
|
|
|
result.Remove(result.Length - 1, 1);
|
|
|
|
|
|
|
|
|
|
|
|
if (Console.CursorLeft == 0) {
|
|
|
|
|
|
Console.SetCursorPosition(Console.BufferWidth - 1, Console.CursorTop - 1);
|
|
|
|
|
|
Console.Write(' ');
|
|
|
|
|
|
Console.SetCursorPosition(Console.BufferWidth - 1, Console.CursorTop - 1);
|
|
|
|
|
|
} else {
|
2017-11-28 21:31:45 +01:00
|
|
|
|
// There are two \b characters here
|
|
|
|
|
|
Console.Write(@" ");
|
2017-08-05 22:35:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
|
return result.ToString();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-07-10 17:07:48 +02:00
|
|
|
|
internal static void StartBackgroundAction(Action action, bool longRunning = true) {
|
2017-07-03 18:54:57 +02:00
|
|
|
|
if (action == null) {
|
|
|
|
|
|
ASF.ArchiLogger.LogNullError(nameof(action));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-07-10 17:07:48 +02:00
|
|
|
|
TaskCreationOptions options = TaskCreationOptions.DenyChildAttach;
|
|
|
|
|
|
|
|
|
|
|
|
if (longRunning) {
|
2017-12-07 08:44:01 +01:00
|
|
|
|
options |= TaskCreationOptions.LongRunning | TaskCreationOptions.PreferFairness;
|
2017-07-10 17:07:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Task.Factory.StartNew(action, options).Forget();
|
2017-07-03 18:54:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-07-10 17:07:48 +02:00
|
|
|
|
internal static void StartBackgroundFunction(Func<Task> function, bool longRunning = true) {
|
2017-07-03 18:54:57 +02:00
|
|
|
|
if (function == null) {
|
|
|
|
|
|
ASF.ArchiLogger.LogNullError(nameof(function));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-07-10 17:07:48 +02:00
|
|
|
|
TaskCreationOptions options = TaskCreationOptions.DenyChildAttach;
|
|
|
|
|
|
|
|
|
|
|
|
if (longRunning) {
|
2017-12-07 08:44:01 +01:00
|
|
|
|
options |= TaskCreationOptions.LongRunning | TaskCreationOptions.PreferFairness;
|
2017-07-10 17:07:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Task.Factory.StartNew(function, options).Forget();
|
2017-07-03 18:54:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-11-16 22:13:34 +01:00
|
|
|
|
internal static IEnumerable<T> ToEnumerable<T>(this T item) {
|
|
|
|
|
|
yield return item;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-09-23 06:44:37 +02:00
|
|
|
|
internal static string ToHumanReadable(this TimeSpan timeSpan) => timeSpan.Humanize(3, maxUnit: TimeUnit.Year);
|
2017-09-26 01:04:05 +02:00
|
|
|
|
|
|
|
|
|
|
private static string[] GetArgs(string[] args, byte argsToSkip = 1) {
|
|
|
|
|
|
if ((args == null) || (args.Length < argsToSkip)) {
|
|
|
|
|
|
ASF.ArchiLogger.LogNullError(nameof(args));
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
byte argsToCopy = (byte) (args.Length - argsToSkip);
|
|
|
|
|
|
string[] result = new string[argsToCopy];
|
|
|
|
|
|
|
|
|
|
|
|
if (argsToCopy > 0) {
|
|
|
|
|
|
Array.Copy(args, argsToSkip, result, 0, argsToCopy);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
2015-10-25 06:16:50 +01:00
|
|
|
|
}
|
2016-11-24 07:32:16 +01:00
|
|
|
|
}
|