2019-11-29 21:42:39 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-03-20 13:15:16 +01:00
|
|
|
|
using System.Drawing;
|
2020-01-10 16:58:48 +01:00
|
|
|
|
using System.IO;
|
2020-03-13 19:19:55 +01:00
|
|
|
|
using System.IO.Compression;
|
2019-11-29 21:42:39 +01:00
|
|
|
|
using System.Linq;
|
2020-03-13 19:19:55 +01:00
|
|
|
|
using System.Net;
|
2019-11-29 21:42:39 +01:00
|
|
|
|
|
|
|
|
|
namespace CC_Functions.Misc
|
|
|
|
|
{
|
|
|
|
|
public static class GenericExtensions
|
|
|
|
|
{
|
|
|
|
|
public static T get<G, T>(this Dictionary<G, T> dict, G key, T def)
|
|
|
|
|
{
|
|
|
|
|
if (!dict.ContainsKey(key))
|
|
|
|
|
dict[key] = def;
|
|
|
|
|
return dict[key];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T set<G, T>(this Dictionary<G, T> dict, G key, T val)
|
|
|
|
|
{
|
|
|
|
|
dict[key] = val;
|
|
|
|
|
return dict[key];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool tryCast<T>(this object o, out T parsed)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2019-12-22 16:58:16 +01:00
|
|
|
|
parsed = (T) o;
|
2019-11-29 21:42:39 +01:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2019-12-22 16:58:16 +01:00
|
|
|
|
parsed = default;
|
2019-11-29 21:42:39 +01:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static G selectO<T, G>(this T self, Func<T, G> func) => func.Invoke(self);
|
|
|
|
|
|
|
|
|
|
public static void runIf(bool condition, Action func)
|
|
|
|
|
{
|
|
|
|
|
if (condition)
|
|
|
|
|
func();
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-30 15:09:16 +01:00
|
|
|
|
public static T ParseToEnum<T>(string value) => (T) Enum.Parse(typeof(T),
|
|
|
|
|
Enum.GetNames(typeof(T)).First(s => s.ToLower() == value.ToLower()));
|
2019-11-29 21:42:39 +01:00
|
|
|
|
|
2019-12-22 16:58:16 +01:00
|
|
|
|
public static bool? ParseBool(string value) =>
|
|
|
|
|
string.IsNullOrWhiteSpace(value) || value.ToLower() == "Indeterminate"
|
|
|
|
|
? (bool?) null
|
|
|
|
|
: bool.Parse(value);
|
2019-11-29 21:42:39 +01:00
|
|
|
|
|
|
|
|
|
public static bool AND(this bool? left, bool? right) => left.TRUE() && right.TRUE();
|
|
|
|
|
|
|
|
|
|
public static bool OR(this bool? left, bool? right) => left.TRUE() || right.TRUE();
|
|
|
|
|
|
2019-12-22 16:58:16 +01:00
|
|
|
|
public static bool XOR(this bool? left, bool? right) => left.OR(right) && !left.AND(right);
|
2019-11-29 21:42:39 +01:00
|
|
|
|
|
|
|
|
|
public static bool TRUE(this bool? self) => self == true;
|
|
|
|
|
|
|
|
|
|
public static bool FALSE(this bool? self) => self == false;
|
|
|
|
|
|
|
|
|
|
public static bool NULL(this bool? self) => self == null;
|
2019-12-30 15:09:16 +01:00
|
|
|
|
|
|
|
|
|
public static void RemoveAt<T, G>(this Dictionary<T, G> dict, int index) =>
|
|
|
|
|
dict.Remove(dict.Keys.OfType<T>().ToArray()[index]);
|
2020-01-10 16:58:48 +01:00
|
|
|
|
|
|
|
|
|
public static long GetSize(this DirectoryInfo directory) => IO.GetDirectorySize(directory.FullName);
|
2020-03-13 19:19:55 +01:00
|
|
|
|
|
2020-03-13 19:38:59 +01:00
|
|
|
|
public static ZipArchiveEntry AddDirectory(this ZipArchive archive, string folderPath, string entryName,
|
2020-03-13 19:19:55 +01:00
|
|
|
|
string[] ignoredExtensions, string[] ignoredPaths)
|
|
|
|
|
{
|
|
|
|
|
entryName = entryName.TrimEnd('/');
|
|
|
|
|
ZipArchiveEntry result = archive.CreateEntry($"{entryName}/");
|
|
|
|
|
string[] files = Directory.GetFiles(folderPath);
|
|
|
|
|
for (int i = 0; i < files.Length; i++)
|
|
|
|
|
if (!ignoredExtensions.Contains(Path.GetExtension(files[i])) &&
|
|
|
|
|
!ignoredPaths.Any(s => IO.CheckPathEqual(s, files[i])))
|
|
|
|
|
archive.CreateEntryFromFile(files[i], $"{entryName}/{Path.GetFileName(files[i])}");
|
|
|
|
|
string[] dirs = Directory.GetDirectories(folderPath);
|
|
|
|
|
for (int i = 0; i < dirs.Length; i++)
|
|
|
|
|
if (!ignoredPaths.Any(s => IO.CheckPathEqual(s, dirs[i])))
|
|
|
|
|
archive.AddDirectory(dirs[i], $"{entryName}/{Path.GetFileName(dirs[i])}", ignoredExtensions,
|
|
|
|
|
ignoredPaths);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-13 19:38:59 +01:00
|
|
|
|
public static Uri Unshorten(this Uri self)
|
2020-03-13 19:19:55 +01:00
|
|
|
|
{
|
|
|
|
|
HttpWebRequest req = (HttpWebRequest) WebRequest.Create(self);
|
|
|
|
|
req.AllowAutoRedirect = true;
|
|
|
|
|
req.MaximumAutomaticRedirections = 100;
|
|
|
|
|
WebResponse resp = req.GetResponse();
|
|
|
|
|
return resp.ResponseUri;
|
|
|
|
|
}
|
2020-03-20 13:15:16 +01:00
|
|
|
|
public static Rectangle Round(this RectangleF self) => Rectangle.Round(self);
|
|
|
|
|
public static Rectangle Ceiling(this RectangleF self) => Rectangle.Ceiling(self);
|
2019-11-29 21:42:39 +01:00
|
|
|
|
}
|
|
|
|
|
}
|