2019-09-14 04:35:42 +00:00
|
|
|
|
using System;
|
2019-07-21 04:40:54 +00:00
|
|
|
|
using System.Collections.Generic;
|
2019-10-31 03:40:33 +00:00
|
|
|
|
using System.Data.HashFunction.xxHash;
|
2019-08-04 22:08:03 +00:00
|
|
|
|
using System.Diagnostics;
|
2019-07-21 04:40:54 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2019-07-23 04:27:26 +00:00
|
|
|
|
using System.Net.Http;
|
2019-11-05 04:14:31 +00:00
|
|
|
|
using System.Reactive.Subjects;
|
2019-10-03 04:35:29 +00:00
|
|
|
|
using System.Reflection;
|
2019-07-21 04:40:54 +00:00
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Text;
|
2019-08-02 23:04:04 +00:00
|
|
|
|
using System.Threading;
|
2019-07-21 04:40:54 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2019-11-21 00:04:01 +00:00
|
|
|
|
using Alphaleonis.Win32.Filesystem;
|
2019-10-26 19:45:37 +00:00
|
|
|
|
using Ceras;
|
2019-09-14 04:35:42 +00:00
|
|
|
|
using ICSharpCode.SharpZipLib.BZip2;
|
|
|
|
|
using IniParser;
|
|
|
|
|
using Newtonsoft.Json;
|
2019-12-04 04:12:08 +00:00
|
|
|
|
using ReactiveUI;
|
|
|
|
|
using Wabbajack.Common.StatusFeed;
|
2019-12-05 05:07:44 +00:00
|
|
|
|
using Wabbajack.Common.StatusFeed.Errors;
|
2019-10-16 21:36:14 +00:00
|
|
|
|
using YamlDotNet.Serialization;
|
|
|
|
|
using YamlDotNet.Serialization.NamingConventions;
|
2019-11-21 00:04:01 +00:00
|
|
|
|
using Directory = System.IO.Directory;
|
2019-08-29 22:49:48 +00:00
|
|
|
|
using File = Alphaleonis.Win32.Filesystem.File;
|
|
|
|
|
using FileInfo = Alphaleonis.Win32.Filesystem.FileInfo;
|
|
|
|
|
using Path = Alphaleonis.Win32.Filesystem.Path;
|
2019-07-21 04:40:54 +00:00
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Common
|
|
|
|
|
{
|
|
|
|
|
public static class Utils
|
|
|
|
|
{
|
2019-11-01 10:55:50 +00:00
|
|
|
|
public static bool IsMO2Running(string mo2Path)
|
|
|
|
|
{
|
|
|
|
|
Process[] processList = Process.GetProcesses();
|
2019-11-01 11:06:12 +00:00
|
|
|
|
return processList.Where(process => process.ProcessName == "ModOrganizer").Any(process => Path.GetDirectoryName(process.MainModule?.FileName) == mo2Path);
|
2019-11-01 10:55:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-03 04:35:29 +00:00
|
|
|
|
public static string LogFile { get; private set; }
|
|
|
|
|
static Utils()
|
|
|
|
|
{
|
2019-11-21 15:49:14 +00:00
|
|
|
|
var programName = Assembly.GetEntryAssembly()?.Location ?? "Wabbajack";
|
|
|
|
|
LogFile = programName + ".log";
|
2019-10-03 04:35:29 +00:00
|
|
|
|
_startTime = DateTime.Now;
|
|
|
|
|
|
|
|
|
|
if (LogFile.FileExists())
|
|
|
|
|
File.Delete(LogFile);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-04 04:12:08 +00:00
|
|
|
|
private static readonly Subject<IStatusMessage> LoggerSubj = new Subject<IStatusMessage>();
|
|
|
|
|
public static IObservable<IStatusMessage> LogMessages => LoggerSubj;
|
2019-07-21 04:40:54 +00:00
|
|
|
|
|
2019-09-24 15:26:44 +00:00
|
|
|
|
private static readonly string[] Suffix = {"B", "KB", "MB", "GB", "TB", "PB", "EB"}; // Longs run out around EB
|
2019-09-14 04:35:42 +00:00
|
|
|
|
|
2019-10-03 04:35:29 +00:00
|
|
|
|
private static object _lock = new object();
|
2019-11-05 04:14:31 +00:00
|
|
|
|
|
2019-10-03 04:35:29 +00:00
|
|
|
|
private static DateTime _startTime;
|
|
|
|
|
|
2019-12-04 04:12:08 +00:00
|
|
|
|
|
2019-08-19 22:08:34 +00:00
|
|
|
|
public static void Log(string msg)
|
2019-12-04 04:12:08 +00:00
|
|
|
|
{
|
|
|
|
|
Log(new GenericInfo(msg));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T Log<T>(T msg) where T : IStatusMessage
|
2019-08-19 22:08:34 +00:00
|
|
|
|
{
|
2019-12-05 04:57:05 +00:00
|
|
|
|
LogToFile(msg.ShortDescription);
|
2019-11-21 15:49:14 +00:00
|
|
|
|
LoggerSubj.OnNext(msg);
|
2019-12-04 04:12:08 +00:00
|
|
|
|
return msg;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 05:07:44 +00:00
|
|
|
|
public static void Error(Exception ex, string extraMessage = null)
|
|
|
|
|
{
|
|
|
|
|
Log(new GenericException(ex, extraMessage));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ErrorThrow(Exception ex, string extraMessage = null)
|
|
|
|
|
{
|
|
|
|
|
Error(ex, extraMessage);
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 04:57:05 +00:00
|
|
|
|
public static void Error(IException err)
|
2019-12-04 04:12:08 +00:00
|
|
|
|
{
|
2019-12-05 05:07:44 +00:00
|
|
|
|
LogToFile($"{err.ShortDescription}\n{err.Exception.StackTrace}");
|
2019-12-04 04:12:08 +00:00
|
|
|
|
LoggerSubj.OnNext(err);
|
2019-12-05 04:58:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ErrorThrow(IException err)
|
|
|
|
|
{
|
|
|
|
|
Error(err);
|
2019-12-05 04:57:05 +00:00
|
|
|
|
throw err.Exception;
|
2019-08-19 22:08:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 05:07:44 +00:00
|
|
|
|
private static void LogToFile(string msg)
|
2019-10-12 19:15:19 +00:00
|
|
|
|
{
|
|
|
|
|
lock (_lock)
|
|
|
|
|
{
|
2019-12-05 04:57:05 +00:00
|
|
|
|
File.AppendAllText(LogFile, $"{(DateTime.Now - _startTime).TotalSeconds:0.##} - {msg}\r\n");
|
2019-10-12 19:15:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-19 22:08:34 +00:00
|
|
|
|
public static void Status(string msg, int progress = 0)
|
|
|
|
|
{
|
2019-12-03 23:28:19 +00:00
|
|
|
|
WorkQueue.CurrentQueue?.Report(msg, progress);
|
2019-08-19 22:08:34 +00:00
|
|
|
|
}
|
2019-09-14 04:35:42 +00:00
|
|
|
|
|
2019-07-21 04:40:54 +00:00
|
|
|
|
/// <summary>
|
2019-09-14 04:35:42 +00:00
|
|
|
|
/// MurMur3 hashes the file pointed to by this string
|
2019-07-21 04:40:54 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="file"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string FileSHA256(this string file)
|
|
|
|
|
{
|
|
|
|
|
var sha = new SHA256Managed();
|
|
|
|
|
using (var o = new CryptoStream(Stream.Null, sha, CryptoStreamMode.Write))
|
|
|
|
|
{
|
|
|
|
|
using (var i = File.OpenRead(file))
|
2019-09-14 04:35:42 +00:00
|
|
|
|
{
|
2019-09-03 21:17:00 +00:00
|
|
|
|
i.CopyToWithStatus(new FileInfo(file).Length, o, $"Hashing {Path.GetFileName(file)}");
|
2019-09-14 04:35:42 +00:00
|
|
|
|
}
|
2019-07-21 04:40:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-14 04:35:42 +00:00
|
|
|
|
return sha.Hash.ToBase64();
|
2019-07-21 04:40:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-06 23:52:48 +00:00
|
|
|
|
public static string FileHash(this string file, bool nullOnIOError = false)
|
2019-10-31 03:40:33 +00:00
|
|
|
|
{
|
2019-11-06 23:52:48 +00:00
|
|
|
|
try
|
2019-10-31 03:40:33 +00:00
|
|
|
|
{
|
2019-11-06 23:52:48 +00:00
|
|
|
|
var hash = new xxHashConfig();
|
|
|
|
|
hash.HashSizeInBits = 64;
|
|
|
|
|
hash.Seed = 0x42;
|
|
|
|
|
using (var fs = File.OpenRead(file))
|
|
|
|
|
{
|
|
|
|
|
var config = new xxHashConfig();
|
|
|
|
|
config.HashSizeInBits = 64;
|
2019-11-20 00:15:46 +00:00
|
|
|
|
using (var f = new StatusFileStream(fs, $"Hashing {Path.GetFileName(file)}"))
|
|
|
|
|
{
|
|
|
|
|
var value = xxHashFactory.Instance.Create(config).ComputeHash(f);
|
|
|
|
|
return value.AsBase64String();
|
|
|
|
|
}
|
2019-11-06 23:52:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (IOException ex)
|
|
|
|
|
{
|
|
|
|
|
if (nullOnIOError) return null;
|
|
|
|
|
throw ex;
|
2019-10-31 03:40:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-20 00:15:46 +00:00
|
|
|
|
public static string FileHashCached(this string file)
|
|
|
|
|
{
|
|
|
|
|
var hashPath = file + Consts.HashFileExtension;
|
|
|
|
|
if (File.Exists(hashPath) && File.GetLastWriteTime(file) <= File.GetLastWriteTime(hashPath))
|
|
|
|
|
{
|
|
|
|
|
return File.ReadAllText(hashPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var hash = file.FileHash();
|
|
|
|
|
File.WriteAllText(hashPath, hash);
|
|
|
|
|
return hash;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-14 22:22:53 +00:00
|
|
|
|
public static async Task<string> FileHashAsync(this string file, bool nullOnIOError = false)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var hash = new xxHashConfig();
|
|
|
|
|
hash.HashSizeInBits = 64;
|
|
|
|
|
hash.Seed = 0x42;
|
|
|
|
|
using (var fs = File.OpenRead(file))
|
|
|
|
|
{
|
|
|
|
|
var config = new xxHashConfig();
|
|
|
|
|
config.HashSizeInBits = 64;
|
|
|
|
|
var value = await xxHashFactory.Instance.Create(config).ComputeHashAsync(fs);
|
|
|
|
|
return value.AsBase64String();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (IOException ex)
|
|
|
|
|
{
|
|
|
|
|
if (nullOnIOError) return null;
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-25 23:52:03 +00:00
|
|
|
|
public static void CopyToWithStatus(this Stream istream, long maxSize, Stream ostream, string status)
|
|
|
|
|
{
|
|
|
|
|
var buffer = new byte[1024 * 64];
|
|
|
|
|
if (maxSize == 0) maxSize = 1;
|
2019-11-21 15:49:14 +00:00
|
|
|
|
long totalRead = 0;
|
2019-08-25 23:52:03 +00:00
|
|
|
|
while (true)
|
|
|
|
|
{
|
2019-09-14 04:35:42 +00:00
|
|
|
|
var read = istream.Read(buffer, 0, buffer.Length);
|
2019-08-25 23:52:03 +00:00
|
|
|
|
if (read == 0) break;
|
2019-11-21 15:49:14 +00:00
|
|
|
|
totalRead += read;
|
2019-08-25 23:52:03 +00:00
|
|
|
|
ostream.Write(buffer, 0, read);
|
2019-11-21 15:49:14 +00:00
|
|
|
|
Status(status, (int) (totalRead * 100 / maxSize));
|
2019-08-25 23:52:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-22 05:19:42 +00:00
|
|
|
|
public static string xxHash(this byte[] data, bool nullOnIOError = false)
|
2019-07-26 20:59:14 +00:00
|
|
|
|
{
|
2019-11-22 05:19:42 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var hash = new xxHashConfig();
|
|
|
|
|
hash.HashSizeInBits = 64;
|
|
|
|
|
hash.Seed = 0x42;
|
|
|
|
|
using (var fs = new MemoryStream(data))
|
|
|
|
|
{
|
|
|
|
|
var config = new xxHashConfig();
|
|
|
|
|
config.HashSizeInBits = 64;
|
|
|
|
|
using (var f = new StatusFileStream(fs, $"Hashing memory stream"))
|
|
|
|
|
{
|
|
|
|
|
var value = xxHashFactory.Instance.Create(config).ComputeHash(f);
|
|
|
|
|
return value.AsBase64String();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (IOException ex)
|
|
|
|
|
{
|
|
|
|
|
if (nullOnIOError) return null;
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
2019-07-26 20:59:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-21 04:40:54 +00:00
|
|
|
|
/// <summary>
|
2019-09-14 04:35:42 +00:00
|
|
|
|
/// Returns a Base64 encoding of these bytes
|
2019-07-21 04:40:54 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="data"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string ToBase64(this byte[] data)
|
|
|
|
|
{
|
|
|
|
|
return Convert.ToBase64String(data);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-05 00:27:46 +00:00
|
|
|
|
public static string ToHex(this byte[] bytes)
|
2019-09-05 22:18:54 +00:00
|
|
|
|
{
|
2019-09-14 04:35:42 +00:00
|
|
|
|
var builder = new StringBuilder();
|
|
|
|
|
for (var i = 0; i < bytes.Length; i++) builder.Append(bytes[i].ToString("x2"));
|
2019-09-05 22:18:54 +00:00
|
|
|
|
return builder.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-05 00:27:46 +00:00
|
|
|
|
public static byte[] FromHex(this string hex)
|
|
|
|
|
{
|
|
|
|
|
return Enumerable.Range(0, hex.Length)
|
|
|
|
|
.Where(x => x % 2 == 0)
|
|
|
|
|
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
|
|
|
|
|
.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static DateTime AsUnixTime(this long timestamp)
|
|
|
|
|
{
|
2019-11-21 15:04:48 +00:00
|
|
|
|
DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
|
2019-11-22 00:41:01 +00:00
|
|
|
|
dtDateTime = dtDateTime.AddSeconds(timestamp);
|
2019-11-05 00:27:46 +00:00
|
|
|
|
return dtDateTime;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-23 04:27:26 +00:00
|
|
|
|
/// <summary>
|
2019-09-14 04:35:42 +00:00
|
|
|
|
/// Returns data from a base64 stream
|
2019-07-23 04:27:26 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="data"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static byte[] FromBase64(this string data)
|
|
|
|
|
{
|
|
|
|
|
return Convert.FromBase64String(data);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-21 04:40:54 +00:00
|
|
|
|
/// <summary>
|
2019-09-14 04:35:42 +00:00
|
|
|
|
/// Executes the action for every item in coll
|
2019-07-21 04:40:54 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <param name="coll"></param>
|
|
|
|
|
/// <param name="f"></param>
|
|
|
|
|
public static void Do<T>(this IEnumerable<T> coll, Action<T> f)
|
|
|
|
|
{
|
|
|
|
|
foreach (var i in coll) f(i);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-25 23:52:03 +00:00
|
|
|
|
public static void DoIndexed<T>(this IEnumerable<T> coll, Action<int, T> f)
|
|
|
|
|
{
|
2019-09-14 04:35:42 +00:00
|
|
|
|
var idx = 0;
|
2019-08-25 23:52:03 +00:00
|
|
|
|
foreach (var i in coll)
|
|
|
|
|
{
|
|
|
|
|
f(idx, i);
|
|
|
|
|
idx += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-07-21 04:40:54 +00:00
|
|
|
|
/// <summary>
|
2019-09-14 04:35:42 +00:00
|
|
|
|
/// Loads INI data from the given filename and returns a dynamic type that
|
|
|
|
|
/// can use . operators to navigate the INI.
|
2019-07-21 04:40:54 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="file"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static dynamic LoadIniFile(this string file)
|
|
|
|
|
{
|
|
|
|
|
return new DynamicIniData(new FileIniDataParser().ReadFile(file));
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-12 18:03:45 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Loads a INI from the given string
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="file"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static dynamic LoadIniString(this string file)
|
|
|
|
|
{
|
|
|
|
|
return new DynamicIniData(new FileIniDataParser().ReadData(new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(file)))));
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-27 12:11:10 +00:00
|
|
|
|
public static void ToCERAS<T>(this T obj, string filename, ref SerializerConfig config)
|
2019-10-26 19:45:37 +00:00
|
|
|
|
{
|
2019-10-27 12:11:10 +00:00
|
|
|
|
var ceras = new CerasSerializer(config);
|
2019-10-26 19:45:37 +00:00
|
|
|
|
byte[] buffer = null;
|
|
|
|
|
ceras.Serialize(obj, ref buffer);
|
2019-10-27 11:02:30 +00:00
|
|
|
|
using(var m1 = new MemoryStream(buffer))
|
|
|
|
|
using (var m2 = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
BZip2.Compress(m1, m2, false, 9);
|
|
|
|
|
m2.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
File.WriteAllBytes(filename, m2.ToArray());
|
|
|
|
|
}
|
2019-10-26 19:45:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-27 12:11:10 +00:00
|
|
|
|
public static T FromCERAS<T>(this Stream data, ref SerializerConfig config)
|
|
|
|
|
{
|
|
|
|
|
var ceras = new CerasSerializer(config);
|
|
|
|
|
byte[] bytes = data.ReadAll();
|
|
|
|
|
using (var m1 = new MemoryStream(bytes))
|
|
|
|
|
using (var m2 = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
BZip2.Decompress(m1, m2, false);
|
|
|
|
|
m2.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
return ceras.Deserialize<T>(m2.ToArray());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-21 04:40:54 +00:00
|
|
|
|
public static void ToJSON<T>(this T obj, string filename)
|
|
|
|
|
{
|
2019-09-14 04:35:42 +00:00
|
|
|
|
File.WriteAllText(filename,
|
|
|
|
|
JsonConvert.SerializeObject(obj, Formatting.Indented,
|
|
|
|
|
new JsonSerializerSettings {TypeNameHandling = TypeNameHandling.Auto}));
|
2019-07-21 04:40:54 +00:00
|
|
|
|
}
|
2019-10-26 19:45:37 +00:00
|
|
|
|
/*
|
2019-08-19 22:08:34 +00:00
|
|
|
|
public static void ToBSON<T>(this T obj, string filename)
|
|
|
|
|
{
|
2019-09-14 04:35:42 +00:00
|
|
|
|
using (var fo = File.OpenWrite(filename))
|
|
|
|
|
using (var br = new BsonDataWriter(fo))
|
2019-08-19 22:08:34 +00:00
|
|
|
|
{
|
|
|
|
|
fo.SetLength(0);
|
2019-09-14 04:35:42 +00:00
|
|
|
|
var serializer = JsonSerializer.Create(new JsonSerializerSettings
|
|
|
|
|
{TypeNameHandling = TypeNameHandling.Auto});
|
2019-08-19 22:08:34 +00:00
|
|
|
|
serializer.Serialize(br, obj);
|
|
|
|
|
}
|
2019-10-26 19:45:37 +00:00
|
|
|
|
}*/
|
2019-08-19 22:08:34 +00:00
|
|
|
|
|
|
|
|
|
public static ulong ToMilliseconds(this DateTime date)
|
|
|
|
|
{
|
2019-09-14 04:35:42 +00:00
|
|
|
|
return (ulong) (date - new DateTime(1970, 1, 1)).TotalMilliseconds;
|
2019-08-19 22:08:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-31 02:24:42 +00:00
|
|
|
|
public static string ToJSON<T>(this T obj,
|
|
|
|
|
TypeNameHandling handling = TypeNameHandling.All,
|
|
|
|
|
TypeNameAssemblyFormatHandling format = TypeNameAssemblyFormatHandling.Full)
|
2019-07-31 03:59:19 +00:00
|
|
|
|
{
|
2019-09-14 04:35:42 +00:00
|
|
|
|
return JsonConvert.SerializeObject(obj, Formatting.Indented,
|
2019-10-31 02:24:42 +00:00
|
|
|
|
new JsonSerializerSettings {TypeNameHandling = handling, TypeNameAssemblyFormatHandling = format});
|
2019-07-31 03:59:19 +00:00
|
|
|
|
}
|
2019-10-26 19:45:37 +00:00
|
|
|
|
|
2019-10-31 02:24:42 +00:00
|
|
|
|
public static T FromJSON<T>(this string filename,
|
|
|
|
|
TypeNameHandling handling = TypeNameHandling.All,
|
|
|
|
|
TypeNameAssemblyFormatHandling format = TypeNameAssemblyFormatHandling.Full)
|
2019-07-21 04:40:54 +00:00
|
|
|
|
{
|
2019-09-14 04:35:42 +00:00
|
|
|
|
return JsonConvert.DeserializeObject<T>(File.ReadAllText(filename),
|
2019-10-31 02:24:42 +00:00
|
|
|
|
new JsonSerializerSettings {TypeNameHandling = handling, TypeNameAssemblyFormatHandling = format});
|
2019-07-31 03:59:19 +00:00
|
|
|
|
}
|
2019-10-26 19:45:37 +00:00
|
|
|
|
/*
|
2019-08-19 22:08:34 +00:00
|
|
|
|
public static T FromBSON<T>(this string filename, bool root_is_array = false)
|
|
|
|
|
{
|
|
|
|
|
using (var fo = File.OpenRead(filename))
|
2019-09-14 04:35:42 +00:00
|
|
|
|
using (var br = new BsonDataReader(fo, root_is_array, DateTimeKind.Local))
|
2019-08-19 22:08:34 +00:00
|
|
|
|
{
|
2019-09-14 04:35:42 +00:00
|
|
|
|
var serializer = JsonSerializer.Create(new JsonSerializerSettings
|
|
|
|
|
{TypeNameHandling = TypeNameHandling.Auto});
|
2019-08-19 22:08:34 +00:00
|
|
|
|
return serializer.Deserialize<T>(br);
|
|
|
|
|
}
|
2019-10-26 19:45:37 +00:00
|
|
|
|
}*/
|
2019-08-19 22:08:34 +00:00
|
|
|
|
|
2019-10-31 02:24:42 +00:00
|
|
|
|
public static T FromJSONString<T>(this string data,
|
|
|
|
|
TypeNameHandling handling = TypeNameHandling.All,
|
|
|
|
|
TypeNameAssemblyFormatHandling format = TypeNameAssemblyFormatHandling.Full)
|
2019-07-31 03:59:19 +00:00
|
|
|
|
{
|
2019-09-14 04:35:42 +00:00
|
|
|
|
return JsonConvert.DeserializeObject<T>(data,
|
2019-10-31 02:24:42 +00:00
|
|
|
|
new JsonSerializerSettings {TypeNameHandling = handling, TypeNameAssemblyFormatHandling = format});
|
2019-07-21 04:40:54 +00:00
|
|
|
|
}
|
2019-07-23 04:27:26 +00:00
|
|
|
|
public static T FromJSON<T>(this Stream data)
|
|
|
|
|
{
|
|
|
|
|
var s = Encoding.UTF8.GetString(data.ReadAll());
|
2019-10-01 22:39:25 +00:00
|
|
|
|
return JsonConvert.DeserializeObject<T>(s,
|
|
|
|
|
new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto });
|
2019-07-23 04:27:26 +00:00
|
|
|
|
}
|
2019-07-21 04:40:54 +00:00
|
|
|
|
|
|
|
|
|
public static bool FileExists(this string filename)
|
|
|
|
|
{
|
|
|
|
|
return File.Exists(filename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string RelativeTo(this string file, string folder)
|
|
|
|
|
{
|
|
|
|
|
return file.Substring(folder.Length + 1);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-21 22:47:17 +00:00
|
|
|
|
/// <summary>
|
2019-09-14 04:35:42 +00:00
|
|
|
|
/// Returns the string compressed via BZip2
|
2019-07-21 22:47:17 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="data"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static byte[] BZip2String(this string data)
|
|
|
|
|
{
|
|
|
|
|
using (var os = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
using (var bz = new BZip2OutputStream(os))
|
|
|
|
|
{
|
|
|
|
|
using (var bw = new BinaryWriter(bz))
|
2019-09-14 04:35:42 +00:00
|
|
|
|
{
|
2019-07-21 22:47:17 +00:00
|
|
|
|
bw.Write(data);
|
2019-09-14 04:35:42 +00:00
|
|
|
|
}
|
2019-07-21 22:47:17 +00:00
|
|
|
|
}
|
2019-09-14 04:35:42 +00:00
|
|
|
|
|
2019-07-21 22:47:17 +00:00
|
|
|
|
return os.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-21 04:48:59 +00:00
|
|
|
|
public static void BZip2ExtractToFile(this Stream src, string dest)
|
|
|
|
|
{
|
|
|
|
|
using (var os = File.OpenWrite(dest))
|
|
|
|
|
{
|
|
|
|
|
os.SetLength(0);
|
|
|
|
|
using (var bz = new BZip2InputStream(src))
|
|
|
|
|
bz.CopyTo(os);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-21 22:47:17 +00:00
|
|
|
|
/// <summary>
|
2019-09-14 04:35:42 +00:00
|
|
|
|
/// Returns the string compressed via BZip2
|
2019-07-21 22:47:17 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="data"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string BZip2String(this byte[] data)
|
|
|
|
|
{
|
|
|
|
|
using (var s = new MemoryStream(data))
|
|
|
|
|
{
|
|
|
|
|
using (var bz = new BZip2InputStream(s))
|
|
|
|
|
{
|
|
|
|
|
using (var bw = new BinaryReader(bz))
|
2019-09-14 04:35:42 +00:00
|
|
|
|
{
|
2019-07-21 22:47:17 +00:00
|
|
|
|
return bw.ReadString();
|
2019-09-14 04:35:42 +00:00
|
|
|
|
}
|
2019-07-21 22:47:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-21 21:32:58 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// A combination of .Select(func).Where(v => v != default). So select and filter default values.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TIn"></typeparam>
|
|
|
|
|
/// <typeparam name="TOut"></typeparam>
|
|
|
|
|
/// <param name="coll"></param>
|
|
|
|
|
/// <param name="func"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static IEnumerable<TOut> Keep<TIn, TOut>(this IEnumerable<TIn> coll, Func<TIn, TOut> func) where TOut : IComparable<TOut>
|
|
|
|
|
{
|
|
|
|
|
return coll.Select(func).Where(v => v.CompareTo(default) != 0);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-22 03:36:25 +00:00
|
|
|
|
public static byte[] ReadAll(this Stream ins)
|
|
|
|
|
{
|
|
|
|
|
using (var ms = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
ins.CopyTo(ms);
|
|
|
|
|
return ms.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 21:13:29 +00:00
|
|
|
|
public static TR[] PMap<TI, TR>(this IEnumerable<TI> coll, WorkQueue queue, StatusUpdateTracker updateTracker,
|
2019-11-17 04:16:42 +00:00
|
|
|
|
Func<TI, TR> f)
|
|
|
|
|
{
|
|
|
|
|
var cnt = 0;
|
|
|
|
|
var collist = coll.ToList();
|
|
|
|
|
return collist.PMap(queue, itm =>
|
|
|
|
|
{
|
|
|
|
|
updateTracker.MakeUpdate(collist.Count, Interlocked.Increment(ref cnt));
|
|
|
|
|
return f(itm);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-24 23:03:36 +00:00
|
|
|
|
public static void PMap<TI>(this IEnumerable<TI> coll, WorkQueue queue, StatusUpdateTracker updateTracker,
|
|
|
|
|
Action<TI> f)
|
|
|
|
|
{
|
|
|
|
|
var cnt = 0;
|
|
|
|
|
var collist = coll.ToList();
|
|
|
|
|
collist.PMap(queue, itm =>
|
|
|
|
|
{
|
|
|
|
|
updateTracker.MakeUpdate(collist.Count, Interlocked.Increment(ref cnt));
|
|
|
|
|
f(itm);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-12-03 21:13:29 +00:00
|
|
|
|
public static TR[] PMap<TI, TR>(this IEnumerable<TI> coll, WorkQueue queue,
|
2019-11-17 04:16:42 +00:00
|
|
|
|
Func<TI, TR> f)
|
2019-07-22 22:17:46 +00:00
|
|
|
|
{
|
2019-08-02 23:04:04 +00:00
|
|
|
|
var colllst = coll.ToList();
|
|
|
|
|
|
2019-11-21 15:49:14 +00:00
|
|
|
|
var remainingTasks = colllst.Count;
|
2019-08-10 15:21:50 +00:00
|
|
|
|
|
2019-07-22 22:17:46 +00:00
|
|
|
|
var tasks = coll.Select(i =>
|
|
|
|
|
{
|
2019-09-14 04:35:42 +00:00
|
|
|
|
var tc = new TaskCompletionSource<TR>();
|
2019-11-17 04:16:42 +00:00
|
|
|
|
queue.QueueTask(() =>
|
2019-07-22 22:17:46 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
tc.SetResult(f(i));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
tc.SetException(ex);
|
|
|
|
|
}
|
2019-11-21 15:49:14 +00:00
|
|
|
|
Interlocked.Decrement(ref remainingTasks);
|
2019-07-22 22:17:46 +00:00
|
|
|
|
});
|
|
|
|
|
return tc.Task;
|
|
|
|
|
}).ToList();
|
|
|
|
|
|
2019-08-10 15:21:50 +00:00
|
|
|
|
// To avoid thread starvation, we'll start to help out in the work queue
|
|
|
|
|
if (WorkQueue.WorkerThread)
|
2019-11-21 15:49:14 +00:00
|
|
|
|
while (remainingTasks > 0)
|
2019-11-17 04:16:42 +00:00
|
|
|
|
if (queue.Queue.TryTake(out var a, 500))
|
2019-09-14 04:35:42 +00:00
|
|
|
|
a();
|
2019-08-10 15:21:50 +00:00
|
|
|
|
|
2019-07-22 22:17:46 +00:00
|
|
|
|
return tasks.Select(t =>
|
|
|
|
|
{
|
|
|
|
|
t.Wait();
|
2019-07-26 20:59:14 +00:00
|
|
|
|
if (t.IsFaulted)
|
|
|
|
|
throw t.Exception;
|
2019-07-22 22:17:46 +00:00
|
|
|
|
return t.Result;
|
2019-12-03 21:13:29 +00:00
|
|
|
|
}).ToArray();
|
2019-07-22 22:17:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-17 04:16:42 +00:00
|
|
|
|
public static void PMap<TI>(this IEnumerable<TI> coll, WorkQueue queue, Action<TI> f)
|
2019-07-22 22:17:46 +00:00
|
|
|
|
{
|
2019-11-17 04:16:42 +00:00
|
|
|
|
coll.PMap(queue, i =>
|
2019-07-22 22:17:46 +00:00
|
|
|
|
{
|
2019-08-10 15:21:50 +00:00
|
|
|
|
f(i);
|
|
|
|
|
return false;
|
|
|
|
|
});
|
2019-07-22 22:17:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-14 04:35:42 +00:00
|
|
|
|
public static void DoProgress<T>(this IEnumerable<T> coll, string msg, Action<T> f)
|
2019-08-29 22:49:48 +00:00
|
|
|
|
{
|
|
|
|
|
var lst = coll.ToList();
|
|
|
|
|
lst.DoIndexed((idx, i) =>
|
|
|
|
|
{
|
|
|
|
|
Status(msg, idx * 100 / lst.Count);
|
|
|
|
|
f(i);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void OnQueue(Action f)
|
|
|
|
|
{
|
|
|
|
|
new List<bool>().Do(_ => f());
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-23 04:27:26 +00:00
|
|
|
|
public static HttpResponseMessage GetSync(this HttpClient client, string url)
|
|
|
|
|
{
|
|
|
|
|
var result = client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
|
|
|
|
|
result.Wait();
|
|
|
|
|
return result.Result;
|
|
|
|
|
}
|
2019-09-14 04:35:42 +00:00
|
|
|
|
|
2019-07-23 04:27:26 +00:00
|
|
|
|
public static string GetStringSync(this HttpClient client, string url)
|
|
|
|
|
{
|
|
|
|
|
var result = client.GetStringAsync(url);
|
|
|
|
|
result.Wait();
|
|
|
|
|
return result.Result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Stream GetStreamSync(this HttpClient client, string url)
|
|
|
|
|
{
|
|
|
|
|
var result = client.GetStreamAsync(url);
|
|
|
|
|
result.Wait();
|
|
|
|
|
return result.Result;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-26 23:02:49 +00:00
|
|
|
|
public static Stream PostStreamSync(this HttpClient client, string url, HttpContent content)
|
|
|
|
|
{
|
|
|
|
|
var result = client.PostAsync(url, content);
|
|
|
|
|
result.Wait();
|
|
|
|
|
var stream = result.Result.Content.ReadAsStreamAsync();
|
|
|
|
|
stream.Wait();
|
|
|
|
|
return stream.Result;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-15 04:30:37 +00:00
|
|
|
|
public static IEnumerable<T> DistinctBy<T, V>(this IEnumerable<T> vs, Func<T, V> select)
|
|
|
|
|
{
|
2019-09-14 04:35:42 +00:00
|
|
|
|
var set = new HashSet<V>();
|
|
|
|
|
foreach (var v in vs)
|
|
|
|
|
{
|
2019-08-15 04:30:37 +00:00
|
|
|
|
var key = select(v);
|
|
|
|
|
if (set.Contains(key)) continue;
|
|
|
|
|
yield return v;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T Last<T>(this T[] a)
|
|
|
|
|
{
|
|
|
|
|
if (a == null || a.Length == 0)
|
|
|
|
|
throw new InvalidDataException("null or empty array");
|
|
|
|
|
return a[a.Length - 1];
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-07 23:06:38 +00:00
|
|
|
|
public static V GetOrDefault<K, V>(this IDictionary<K, V> dict, K key)
|
|
|
|
|
{
|
2019-09-14 04:35:42 +00:00
|
|
|
|
if (dict.TryGetValue(key, out var v)) return v;
|
|
|
|
|
return default;
|
2019-08-07 23:06:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-10 13:02:25 +00:00
|
|
|
|
public static string ToFileSizeString(this long byteCount)
|
|
|
|
|
{
|
|
|
|
|
if (byteCount == 0)
|
|
|
|
|
return "0" + Suffix[0];
|
2019-09-14 04:35:42 +00:00
|
|
|
|
var bytes = Math.Abs(byteCount);
|
|
|
|
|
var place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));
|
|
|
|
|
var num = Math.Round(bytes / Math.Pow(1024, place), 1);
|
|
|
|
|
return Math.Sign(byteCount) * num + Suffix[place];
|
2019-09-10 13:02:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-20 22:49:32 +00:00
|
|
|
|
public static string ToFileSizeString(this int byteCount)
|
|
|
|
|
{
|
|
|
|
|
return ToFileSizeString((long)byteCount);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-12 23:44:35 +00:00
|
|
|
|
public static void CreatePatch(byte[] a, byte[] b, Stream output)
|
|
|
|
|
{
|
2019-11-22 05:19:42 +00:00
|
|
|
|
var dataA = a.xxHash().FromBase64().ToHex();
|
|
|
|
|
var dataB = b.xxHash().FromBase64().ToHex();
|
2019-11-21 15:49:14 +00:00
|
|
|
|
var cacheFile = Path.Combine("patch_cache", $"{dataA}_{dataB}.patch");
|
2019-09-12 23:44:35 +00:00
|
|
|
|
if (!Directory.Exists("patch_cache"))
|
|
|
|
|
Directory.CreateDirectory("patch_cache");
|
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
2019-11-21 15:49:14 +00:00
|
|
|
|
if (File.Exists(cacheFile))
|
2019-09-12 23:44:35 +00:00
|
|
|
|
{
|
2019-11-21 15:49:14 +00:00
|
|
|
|
using (var f = File.OpenRead(cacheFile))
|
2019-09-12 23:44:35 +00:00
|
|
|
|
{
|
|
|
|
|
f.CopyTo(output);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-11-21 15:49:14 +00:00
|
|
|
|
var tmpName = Path.Combine("patch_cache", Guid.NewGuid() + ".tmp");
|
2019-09-12 23:44:35 +00:00
|
|
|
|
|
2019-11-21 15:49:14 +00:00
|
|
|
|
using (var f = File.OpenWrite(tmpName))
|
2019-09-12 23:44:35 +00:00
|
|
|
|
{
|
|
|
|
|
BSDiff.Create(a, b, f);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-22 05:19:42 +00:00
|
|
|
|
File.Move(tmpName, cacheFile, MoveOptions.ReplaceExisting);
|
2019-09-12 23:44:35 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-22 05:19:42 +00:00
|
|
|
|
public static bool TryGetPatch(string foundHash, string fileHash, out byte[] ePatch)
|
2019-09-12 23:44:35 +00:00
|
|
|
|
{
|
2019-11-21 15:49:14 +00:00
|
|
|
|
var patchName = Path.Combine("patch_cache",
|
2019-11-05 00:27:46 +00:00
|
|
|
|
$"{foundHash.FromBase64().ToHex()}_{fileHash.FromBase64().ToHex()}.patch");
|
2019-11-22 05:19:42 +00:00
|
|
|
|
if (File.Exists(patchName))
|
|
|
|
|
{
|
|
|
|
|
ePatch = File.ReadAllBytes(patchName);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ePatch = null;
|
|
|
|
|
return false;
|
2019-09-12 23:44:35 +00:00
|
|
|
|
}
|
2019-09-23 21:37:10 +00:00
|
|
|
|
|
2019-12-04 04:12:08 +00:00
|
|
|
|
/*
|
2019-09-23 21:37:10 +00:00
|
|
|
|
public static void Warning(string s)
|
|
|
|
|
{
|
|
|
|
|
Log($"WARNING: {s}");
|
2019-12-04 04:12:08 +00:00
|
|
|
|
}*/
|
2019-09-23 21:37:10 +00:00
|
|
|
|
|
2019-09-29 04:41:47 +00:00
|
|
|
|
public static TV GetOrDefault<TK, TV>(this Dictionary<TK, TV> dict, TK key)
|
|
|
|
|
{
|
|
|
|
|
return dict.TryGetValue(key, out var result) ? result : default;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-01 22:39:25 +00:00
|
|
|
|
public static IEnumerable<T> ButLast<T>(this IEnumerable<T> coll)
|
|
|
|
|
{
|
|
|
|
|
var lst = coll.ToList();
|
|
|
|
|
return lst.Take(lst.Count() - 1);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-23 21:37:10 +00:00
|
|
|
|
public static byte[] ConcatArrays(this IEnumerable<byte[]> arrays)
|
|
|
|
|
{
|
|
|
|
|
var outarr = new byte[arrays.Sum(a => a.Length)];
|
|
|
|
|
int offset = 0;
|
|
|
|
|
foreach (var arr in arrays)
|
|
|
|
|
{
|
|
|
|
|
Array.Copy(arr, 0, outarr, offset, arr.Length);
|
|
|
|
|
offset += arr.Length;
|
|
|
|
|
}
|
|
|
|
|
return outarr;
|
|
|
|
|
}
|
2019-10-12 18:03:45 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Roundtrips the value throught the JSON routines
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TV"></typeparam>
|
|
|
|
|
/// <typeparam name="TR"></typeparam>
|
|
|
|
|
/// <param name="tv"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static T ViaJSON<T>(this T tv)
|
|
|
|
|
{
|
|
|
|
|
return tv.ToJSON().FromJSONString<T>();
|
|
|
|
|
}
|
2019-10-12 22:15:20 +00:00
|
|
|
|
|
2019-12-04 04:12:08 +00:00
|
|
|
|
/*
|
2019-10-12 22:15:20 +00:00
|
|
|
|
public static void Error(string msg)
|
|
|
|
|
{
|
|
|
|
|
Log(msg);
|
|
|
|
|
throw new Exception(msg);
|
2019-12-04 04:12:08 +00:00
|
|
|
|
}*/
|
2019-10-16 03:17:27 +00:00
|
|
|
|
|
2019-11-29 23:56:56 +00:00
|
|
|
|
public static Stream GetEmbeddedResourceStream(string name)
|
2019-10-16 03:17:27 +00:00
|
|
|
|
{
|
|
|
|
|
return (from assembly in AppDomain.CurrentDomain.GetAssemblies()
|
|
|
|
|
where !assembly.IsDynamic
|
|
|
|
|
from rname in assembly.GetManifestResourceNames()
|
|
|
|
|
where rname == name
|
|
|
|
|
select assembly.GetManifestResourceStream(name)).First();
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-16 21:36:14 +00:00
|
|
|
|
public static T FromYaml<T>(this Stream s)
|
|
|
|
|
{
|
|
|
|
|
var d = new DeserializerBuilder()
|
|
|
|
|
.WithNamingConvention(PascalCaseNamingConvention.Instance)
|
|
|
|
|
.Build();
|
|
|
|
|
return d.Deserialize<T>(new StreamReader(s));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T FromYaml<T>(this string s)
|
|
|
|
|
{
|
|
|
|
|
var d = new DeserializerBuilder()
|
|
|
|
|
.WithNamingConvention(PascalCaseNamingConvention.Instance)
|
|
|
|
|
.Build();
|
2019-10-16 21:49:20 +00:00
|
|
|
|
return d.Deserialize<T>(new StringReader(s));
|
2019-10-16 21:36:14 +00:00
|
|
|
|
}
|
2019-11-02 15:38:03 +00:00
|
|
|
|
public static void LogStatus(string s)
|
|
|
|
|
{
|
|
|
|
|
Status(s);
|
|
|
|
|
Log(s);
|
|
|
|
|
}
|
2019-11-03 03:23:59 +00:00
|
|
|
|
|
2019-11-21 00:04:01 +00:00
|
|
|
|
private static long TestDiskSpeedInner(WorkQueue queue, string path)
|
2019-11-20 23:39:03 +00:00
|
|
|
|
{
|
2019-11-21 15:49:14 +00:00
|
|
|
|
var startTime = DateTime.Now;
|
2019-11-20 23:39:03 +00:00
|
|
|
|
var seconds = 2;
|
|
|
|
|
return Enumerable.Range(0, queue.ThreadCount)
|
|
|
|
|
.PMap(queue, idx =>
|
|
|
|
|
{
|
|
|
|
|
var random = new Random();
|
|
|
|
|
|
|
|
|
|
var file = Path.Combine(path, $"size_test{idx}.bin");
|
|
|
|
|
long size = 0;
|
|
|
|
|
byte[] buffer = new byte[1024 * 8];
|
|
|
|
|
random.NextBytes(buffer);
|
|
|
|
|
using (var fs = File.OpenWrite(file))
|
|
|
|
|
{
|
2019-11-21 15:49:14 +00:00
|
|
|
|
while (DateTime.Now < startTime + new TimeSpan(0, 0, seconds))
|
2019-11-20 23:39:03 +00:00
|
|
|
|
{
|
|
|
|
|
fs.Write(buffer, 0, buffer.Length);
|
|
|
|
|
// Flush to make sure large buffers don't cause the rate to be higher than it should
|
|
|
|
|
fs.Flush();
|
|
|
|
|
size += buffer.Length;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
File.Delete(file);
|
|
|
|
|
return size;
|
|
|
|
|
}).Sum() / seconds;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-21 00:04:01 +00:00
|
|
|
|
private static Dictionary<string, long> _cachedDiskSpeeds = new Dictionary<string, long>();
|
|
|
|
|
public static long TestDiskSpeed(WorkQueue queue, string path)
|
|
|
|
|
{
|
2019-11-21 15:49:14 +00:00
|
|
|
|
var driveName = Volume.GetUniqueVolumeNameForPath(path);
|
|
|
|
|
if (_cachedDiskSpeeds.TryGetValue(driveName, out long speed))
|
2019-11-21 00:04:01 +00:00
|
|
|
|
return speed;
|
|
|
|
|
speed = TestDiskSpeedInner(queue, path);
|
2019-11-21 15:49:14 +00:00
|
|
|
|
_cachedDiskSpeeds[driveName] = speed;
|
2019-11-21 00:04:01 +00:00
|
|
|
|
return speed;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-03 03:23:59 +00:00
|
|
|
|
/// https://stackoverflow.com/questions/422090/in-c-sharp-check-that-filename-is-possibly-valid-not-that-it-exists
|
|
|
|
|
public static IErrorResponse IsFilePathValid(string path)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(path))
|
|
|
|
|
{
|
2019-11-30 21:45:39 +00:00
|
|
|
|
return ErrorResponse.Fail("Path is empty.");
|
2019-11-03 03:23:59 +00:00
|
|
|
|
}
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var fi = new System.IO.FileInfo(path);
|
|
|
|
|
}
|
|
|
|
|
catch (ArgumentException ex)
|
|
|
|
|
{
|
|
|
|
|
return ErrorResponse.Fail(ex.Message);
|
|
|
|
|
}
|
2019-11-21 15:04:48 +00:00
|
|
|
|
catch (PathTooLongException ex)
|
2019-11-03 03:23:59 +00:00
|
|
|
|
{
|
|
|
|
|
return ErrorResponse.Fail(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
catch (NotSupportedException ex)
|
|
|
|
|
{
|
|
|
|
|
return ErrorResponse.Fail(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
return ErrorResponse.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IErrorResponse IsDirectoryPathValid(string path)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(path))
|
|
|
|
|
{
|
2019-11-30 21:45:39 +00:00
|
|
|
|
return ErrorResponse.Fail("Path is empty");
|
2019-11-03 03:23:59 +00:00
|
|
|
|
}
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var fi = new System.IO.DirectoryInfo(path);
|
|
|
|
|
}
|
|
|
|
|
catch (ArgumentException ex)
|
|
|
|
|
{
|
|
|
|
|
return ErrorResponse.Fail(ex.Message);
|
|
|
|
|
}
|
2019-11-21 15:04:48 +00:00
|
|
|
|
catch (PathTooLongException ex)
|
2019-11-03 03:23:59 +00:00
|
|
|
|
{
|
|
|
|
|
return ErrorResponse.Fail(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
catch (NotSupportedException ex)
|
|
|
|
|
{
|
|
|
|
|
return ErrorResponse.Fail(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
return ErrorResponse.Success;
|
|
|
|
|
}
|
2019-11-23 17:37:24 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Both AlphaFS and C#'s Directory.Delete sometimes fail when certain files are read-only
|
|
|
|
|
/// or have other weird attributes. This is the only 100% reliable way I've found to completely
|
|
|
|
|
/// delete a folder. If you don't like this code, it's unlikely to change without a ton of testing.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path"></param>
|
|
|
|
|
public static void DeleteDirectory(string path)
|
|
|
|
|
{
|
|
|
|
|
var info = new ProcessStartInfo
|
|
|
|
|
{
|
|
|
|
|
FileName = "cmd.exe",
|
|
|
|
|
Arguments = $"/c del /f /q /s \"{path}\" && rmdir /q /s \"{path}\" ",
|
|
|
|
|
RedirectStandardError = true,
|
|
|
|
|
RedirectStandardInput = true,
|
|
|
|
|
RedirectStandardOutput = true,
|
|
|
|
|
UseShellExecute = false,
|
|
|
|
|
CreateNoWindow = true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var p = new Process
|
|
|
|
|
{
|
|
|
|
|
StartInfo = info
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
p.Start();
|
|
|
|
|
ChildProcessTracker.AddProcess(p);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
p.PriorityClass = ProcessPriorityClass.BelowNormal;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (!p.HasExited)
|
|
|
|
|
{
|
|
|
|
|
var line = p.StandardOutput.ReadLine();
|
|
|
|
|
if (line == null) break;
|
|
|
|
|
Status(line);
|
|
|
|
|
}
|
|
|
|
|
p.WaitForExit();
|
|
|
|
|
}
|
2019-07-21 04:40:54 +00:00
|
|
|
|
}
|
2019-11-20 00:15:46 +00:00
|
|
|
|
}
|