[Attempt] introduce CC_Functions.Core
This commit is contained in:
parent
0fa5fd6e82
commit
85a67cd64a
|
@ -45,7 +45,12 @@ uptool:
|
|||
$tmp1 = $asmver.split('.')[2]
|
||||
$tmp2 = $asmver.split('.')[3]
|
||||
$suffix = "$tmp1.$tmp2"
|
||||
cd Misc
|
||||
cd Core
|
||||
dotnet pack --version-suffix "$suffix" -c Release -o .
|
||||
$tmp = $(Get-Item $(Resolve-Path *.nupkg).Path).Name
|
||||
echo $tmp
|
||||
dotnet nuget push $tmp -s https://api.nuget.org/v3/index.json -k $NUGET
|
||||
cd ..\Misc
|
||||
dotnet pack --version-suffix "$suffix" -c Release -o .
|
||||
$tmp = $(Get-Item $(Resolve-Path *.nupkg).Path).Name
|
||||
echo $tmp
|
||||
|
|
|
@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Commandline", "Commandline\
|
|||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CLITest", "CLITest\CLITest.csproj", "{3FAB0713-3021-4C6A-BF4A-ABBD542634A6}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Core", "Core\Core.csproj", "{780EC190-E223-46DE-B6FB-1CF56ABDF34E}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
@ -39,6 +41,10 @@ Global
|
|||
{3FAB0713-3021-4C6A-BF4A-ABBD542634A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{3FAB0713-3021-4C6A-BF4A-ABBD542634A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{3FAB0713-3021-4C6A-BF4A-ABBD542634A6}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{780EC190-E223-46DE-B6FB-1CF56ABDF34E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{780EC190-E223-46DE-B6FB-1CF56ABDF34E}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{780EC190-E223-46DE-B6FB-1CF56ABDF34E}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{780EC190-E223-46DE-B6FB-1CF56ABDF34E}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
using System;
|
||||
using System.Drawing;
|
||||
using System.Threading;
|
||||
using CC_Functions.Commandline;
|
||||
using CC_Functions.Commandline.TUI;
|
||||
|
||||
namespace CLITest
|
||||
|
|
|
@ -9,11 +9,11 @@
|
|||
<Deterministic>false</Deterministic>
|
||||
<PackageId>CC-Functions.Commandline</PackageId>
|
||||
<Title>CC-Functions.Commandline</Title>
|
||||
<Authors>CC24</Authors>
|
||||
<Authors>JFronny</Authors>
|
||||
<Description>Random pieces of code used across my projects. CLI/TUI extensions</Description>
|
||||
<Copyright>Copyright 2020</Copyright>
|
||||
<PackageProjectUrl>https://github.com/JFronny/CC-Functions</PackageProjectUrl>
|
||||
<RepositoryUrl>https://github.com/JFronny/CC-Functions.git</RepositoryUrl>
|
||||
<PackageProjectUrl>https://gitlab.com/JFronny/CC-Functions</PackageProjectUrl>
|
||||
<RepositoryUrl>https://gitlab.com/JFronny/CC-Functions.git</RepositoryUrl>
|
||||
<RepositoryType>git</RepositoryType>
|
||||
<AssemblyVersion>1.1.*</AssemblyVersion>
|
||||
<FileVersion>1.0.0.0</FileVersion>
|
||||
|
@ -31,7 +31,6 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Misc\Misc.csproj" />
|
||||
<ProjectReference Include="..\Core\Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
using System;
|
||||
using System.Drawing;
|
||||
using CC_Functions.Misc;
|
||||
using CC_Functions.Commandline.TUI;
|
||||
using CC_Functions.Core;
|
||||
|
||||
namespace CC_Functions.Commandline.TUI
|
||||
namespace CC_Functions.Commandline
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides differential drawing of a char[,] Do not use in combination with System.Console
|
|
@ -1,7 +1,7 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using CC_Functions.Misc;
|
||||
using CC_Functions.Core;
|
||||
|
||||
namespace CC_Functions.Commandline.TUI
|
||||
{
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
using System;
|
||||
using System.Drawing;
|
||||
using CC_Functions.Misc;
|
||||
using CC_Functions.Core;
|
||||
|
||||
namespace CC_Functions.Commandline.TUI
|
||||
{
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
using System;
|
||||
using System.Drawing;
|
||||
using CC_Functions.Misc;
|
||||
using CC_Functions.Core;
|
||||
|
||||
namespace CC_Functions.Commandline.TUI
|
||||
{
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
using System.Drawing;
|
||||
using CC_Functions.Misc;
|
||||
using CC_Functions.Core;
|
||||
|
||||
namespace CC_Functions.Commandline.TUI
|
||||
{
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using CC_Functions.Misc;
|
||||
using CC_Functions.Core;
|
||||
|
||||
namespace CC_Functions.Commandline.TUI
|
||||
{
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
using System;
|
||||
using CC_Functions.Misc;
|
||||
using CC_Functions.Core;
|
||||
|
||||
namespace CC_Functions.Commandline.TUI
|
||||
{
|
||||
|
|
|
@ -2,7 +2,7 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using CC_Functions.Misc;
|
||||
using CC_Functions.Core;
|
||||
|
||||
namespace CC_Functions.Commandline.TUI
|
||||
{
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
using System.Drawing;
|
||||
using System.Linq;
|
||||
|
||||
namespace CC_Functions.Misc
|
||||
namespace CC_Functions.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains extension functions to work with 1D and 2D arrays
|
|
@ -0,0 +1,29 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Library</OutputType>
|
||||
<RootNamespace>CC_Functions.Core</RootNamespace>
|
||||
<AssemblyName>CC_Functions.Core</AssemblyName>
|
||||
<LangVersion>8</LangVersion>
|
||||
<Deterministic>false</Deterministic>
|
||||
<PackageId>CC-Functions.Core</PackageId>
|
||||
<Title>CC-Functions.Core</Title>
|
||||
<Authors>JFronny</Authors>
|
||||
<Description>Random pieces of code without external dependencies. Used in other CC_Functions packages</Description>
|
||||
<Copyright>Copyright 2020</Copyright>
|
||||
<PackageProjectUrl>https://gitlab.com/JFronny/CC-Functions</PackageProjectUrl>
|
||||
<RepositoryUrl>https://gitlab.com/JFronny/CC-Functions.git</RepositoryUrl>
|
||||
<RepositoryType>git</RepositoryType>
|
||||
<AssemblyVersion>1.1.*</AssemblyVersion>
|
||||
<FileVersion>1.0.0.0</FileVersion>
|
||||
<VersionSuffix>0.0</VersionSuffix>
|
||||
<PackageVersion>1.1.$(VersionSuffix)</PackageVersion>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DocumentationFile>bin\Debug\Core.xml</DocumentationFile>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DocumentationFile>bin\Release\Core.xml</DocumentationFile>
|
||||
</PropertyGroup>
|
||||
</Project>
|
|
@ -0,0 +1,244 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
|
||||
namespace CC_Functions.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods for various types
|
||||
/// </summary>
|
||||
public static class GenericExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets an element from the dictionary or adds the default
|
||||
/// </summary>
|
||||
/// <param name="dict">The dictionary to get from</param>
|
||||
/// <param name="key">The key to check</param>
|
||||
/// <param name="def">The default value to place</param>
|
||||
/// <typeparam name="TKey">The key type</typeparam>
|
||||
/// <typeparam name="TValue">The value type</typeparam>
|
||||
/// <returns>The element at the key</returns>
|
||||
public static TValue Get<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, TValue def = default)
|
||||
{
|
||||
if (!dict.ContainsKey(key))
|
||||
dict[key] = def;
|
||||
return dict[key];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets an element and returns it
|
||||
/// </summary>
|
||||
/// <param name="dict">The dictionary to set in</param>
|
||||
/// <param name="key">The key to set at</param>
|
||||
/// <param name="val">The value to place</param>
|
||||
/// <typeparam name="TKey">The key type</typeparam>
|
||||
/// <typeparam name="TValue">The value type</typeparam>
|
||||
/// <returns>The value that was placed</returns>
|
||||
public static TValue Set<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, TValue val = default)
|
||||
{
|
||||
dict[key] = val;
|
||||
return dict[key];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to cast an object
|
||||
/// </summary>
|
||||
/// <param name="o">The object to try to parse</param>
|
||||
/// <param name="parsed">The parsed object (if successful) or the default (usually null)</param>
|
||||
/// <typeparam name="T">The type to cast to</typeparam>
|
||||
/// <returns>Whether the cast was successful</returns>
|
||||
public static bool TryCast<T>(this object o, out T parsed)
|
||||
{
|
||||
try
|
||||
{
|
||||
parsed = (T) o;
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
parsed = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs a function that transforms an object in-line
|
||||
/// </summary>
|
||||
/// <param name="self">The object to run on</param>
|
||||
/// <param name="func">The function to run</param>
|
||||
/// <typeparam name="TIn">The input type</typeparam>
|
||||
/// <typeparam name="TOut">The output type</typeparam>
|
||||
/// <returns></returns>
|
||||
public static TOut SelectO<TIn, TOut>(this TIn self, Func<TIn, TOut> func) => func.Invoke(self);
|
||||
|
||||
/// <summary>
|
||||
/// Runs a function under a condition in-line (equal to if)
|
||||
/// </summary>
|
||||
/// <param name="condition">The condition to check</param>
|
||||
/// <param name="func">The function to run</param>
|
||||
public static void RunIf(bool condition, Action func)
|
||||
{
|
||||
if (condition)
|
||||
func();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parses a string to a value of an enum
|
||||
/// </summary>
|
||||
/// <param name="value">The string to parse</param>
|
||||
/// <typeparam name="TEnum">The enum type (MUST be an enum)</typeparam>
|
||||
/// <returns>The element</returns>
|
||||
public static TEnum ParseToEnum<TEnum>(string value) => (TEnum) Enum.Parse(typeof(TEnum),
|
||||
Enum.GetNames(typeof(TEnum)).First(s => s.ToLower() == value.ToLower()));
|
||||
|
||||
/// <summary>
|
||||
/// Parses a string to a nullable bool (defaults to null if parse fails)
|
||||
/// </summary>
|
||||
/// <param name="value">The st string to parse</param>
|
||||
/// <returns>The output nullable bool</returns>
|
||||
public static bool? ParseBool(string value) =>
|
||||
bool.TryParse(value, out bool tmp) ? (bool?) tmp : null;
|
||||
|
||||
/// <summary>
|
||||
/// AND operation for nullable bools (uses <see cref="True">True</see>)
|
||||
/// </summary>
|
||||
/// <param name="left">First bool to check</param>
|
||||
/// <param name="right">Second bool to check</param>
|
||||
/// <returns>The operation result</returns>
|
||||
public static bool And(this bool? left, bool? right) => left.True() && right.True();
|
||||
|
||||
/// <summary>
|
||||
/// OR operation for nullable bools (uses <see cref="True">True</see>)
|
||||
/// </summary>
|
||||
/// <param name="left">First bool to check</param>
|
||||
/// <param name="right">Second bool to check</param>
|
||||
/// <returns>The operation result</returns>
|
||||
public static bool Or(this bool? left, bool? right) => left.True() || right.True();
|
||||
|
||||
/// <summary>
|
||||
/// XOR operation for nullable bools (uses <see cref="True">True</see>)
|
||||
/// </summary>
|
||||
/// <param name="left">First bool to check</param>
|
||||
/// <param name="right">Second bool to check</param>
|
||||
/// <returns>The operation result</returns>
|
||||
public static bool Xor(this bool? left, bool? right) => left.Or(right) && !left.And(right);
|
||||
|
||||
/// <summary>
|
||||
/// Whether the nullable bool is true (null->false)
|
||||
/// </summary>
|
||||
/// <param name="self">Value to check</param>
|
||||
/// <returns>Whether it is true</returns>
|
||||
public static bool True(this bool? self) => self == true;
|
||||
|
||||
/// <summary>
|
||||
/// Whether the nullable bool is false (null->false)
|
||||
/// </summary>
|
||||
/// <param name="self">Value to check</param>
|
||||
/// <returns>Whether it is false</returns>
|
||||
public static bool False(this bool? self) => self == false;
|
||||
|
||||
/// <summary>
|
||||
/// Whether the nullable bool is null
|
||||
/// </summary>
|
||||
/// <param name="self">Value to check</param>
|
||||
/// <returns>Whether it is null</returns>
|
||||
public static bool Null(this bool? self) => self == null;
|
||||
|
||||
/// <summary>
|
||||
/// Removes an element from a dictionary by its index (not key)
|
||||
/// </summary>
|
||||
/// <param name="dict">The dictionary to remove from</param>
|
||||
/// <param name="index">The index of the value</param>
|
||||
/// <typeparam name="TKey">The key type</typeparam>
|
||||
/// <typeparam name="TValue">The value type</typeparam>
|
||||
public static void RemoveAt<TKey, TValue>(this Dictionary<TKey, TValue> dict, int index) =>
|
||||
dict.Remove(dict.Keys.ToArray()[index]);
|
||||
|
||||
/// <summary>
|
||||
/// "Unshorten" (follow) an URL
|
||||
/// </summary>
|
||||
/// <param name="self">The URL to unshorten</param>
|
||||
/// <returns>The unshortened URL</returns>
|
||||
public static Uri Unshorten(this Uri self)
|
||||
{
|
||||
HttpWebRequest req = (HttpWebRequest) WebRequest.Create(self);
|
||||
req.AllowAutoRedirect = true;
|
||||
req.MaximumAutomaticRedirections = 100;
|
||||
WebResponse resp = req.GetResponse();
|
||||
return resp.ResponseUri;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rounds a RectangleF to a Rectangle instead of flooring
|
||||
/// </summary>
|
||||
/// <param name="self">The RectangleF to round</param>
|
||||
/// <returns>The rounded Rectangle</returns>
|
||||
public static Rectangle Round(this RectangleF self) => Rectangle.Round(self);
|
||||
|
||||
/// <summary>
|
||||
/// Ceilings a RectangleF to a Rectangle instead of flooring
|
||||
/// </summary>
|
||||
/// <param name="self">The RectangleF to ceil (?)</param>
|
||||
/// <returns>The ceiled (?) Rectangle</returns>
|
||||
public static Rectangle Ceiling(this RectangleF self) => Rectangle.Ceiling(self);
|
||||
|
||||
/// <summary>
|
||||
/// Pings an URL to check for availability
|
||||
/// </summary>
|
||||
/// <param name="self">The URL to check</param>
|
||||
/// <returns>Whether the service is online</returns>
|
||||
public static bool Ping(this Uri self)
|
||||
{
|
||||
try
|
||||
{
|
||||
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(self);
|
||||
request.Timeout = 3000;
|
||||
request.AllowAutoRedirect = true;
|
||||
using WebResponse response = request.GetResponse();
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the size of a dictionary
|
||||
/// </summary>
|
||||
/// <param name="directory">The dictionary to check</param>
|
||||
/// <returns>The size of the dictionary</returns>
|
||||
public static long GetSize(this DirectoryInfo directory) => IO.GetDirectorySize(directory.FullName);
|
||||
|
||||
/// <summary>
|
||||
/// Adds a directory to an archive recursively
|
||||
/// </summary>
|
||||
/// <param name="archive">The archive to add to</param>
|
||||
/// <param name="folderPath">The directory to add</param>
|
||||
/// <param name="entryName">The name of the directory in-archive</param>
|
||||
/// <param name="ignoredExtensions">Extensions for files to ignore</param>
|
||||
/// <param name="ignoredPaths">Paths to exclude from adding</param>
|
||||
/// <returns>The new entry</returns>
|
||||
public static ZipArchiveEntry AddDirectory(this ZipArchive archive, string folderPath, string entryName,
|
||||
string[] ignoredExtensions, string[] ignoredPaths)
|
||||
{
|
||||
entryName = entryName.TrimEnd('/');
|
||||
ZipArchiveEntry result = archive.CreateEntry($"{entryName}/");
|
||||
string[] files = Directory.GetFiles(folderPath);
|
||||
foreach (string t in files)
|
||||
if (!ignoredExtensions.Contains(Path.GetExtension(t)) &&
|
||||
!ignoredPaths.Any(s => IO.CheckPathEqual(s, t)))
|
||||
archive.CreateEntryFromFile(t, $"{entryName}/{Path.GetFileName(t)}");
|
||||
string[] dirs = Directory.GetDirectories(folderPath);
|
||||
foreach (string t in dirs)
|
||||
if (!ignoredPaths.Any(s => IO.CheckPathEqual(s, t)))
|
||||
archive.AddDirectory(t, $"{entryName}/{Path.GetFileName(t)}", ignoredExtensions,
|
||||
ignoredPaths);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace CC_Functions.Misc
|
||||
namespace CC_Functions.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// IO functions
|
|
@ -1,4 +1,4 @@
|
|||
namespace CC_Functions.Misc
|
||||
namespace CC_Functions.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Characters for use in CC-Functions.CommandLine
|
|
@ -1,10 +1,9 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using CC_Functions.Core;
|
||||
|
||||
namespace CC_Functions.Misc
|
||||
{
|
||||
|
@ -13,234 +12,6 @@ namespace CC_Functions.Misc
|
|||
/// </summary>
|
||||
public static class GenericExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets an element from the dictionary or adds the default
|
||||
/// </summary>
|
||||
/// <param name="dict">The dictionary to get from</param>
|
||||
/// <param name="key">The key to check</param>
|
||||
/// <param name="def">The default value to place</param>
|
||||
/// <typeparam name="TKey">The key type</typeparam>
|
||||
/// <typeparam name="TValue">The value type</typeparam>
|
||||
/// <returns>The element at the key</returns>
|
||||
public static TValue Get<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, TValue def = default)
|
||||
{
|
||||
if (!dict.ContainsKey(key))
|
||||
dict[key] = def;
|
||||
return dict[key];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets an element and returns it
|
||||
/// </summary>
|
||||
/// <param name="dict">The dictionary to set in</param>
|
||||
/// <param name="key">The key to set at</param>
|
||||
/// <param name="val">The value to place</param>
|
||||
/// <typeparam name="TKey">The key type</typeparam>
|
||||
/// <typeparam name="TValue">The value type</typeparam>
|
||||
/// <returns>The value that was placed</returns>
|
||||
public static TValue Set<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, TValue val = default)
|
||||
{
|
||||
dict[key] = val;
|
||||
return dict[key];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to cast an object
|
||||
/// </summary>
|
||||
/// <param name="o">The object to try to parse</param>
|
||||
/// <param name="parsed">The parsed object (if successful) or the default (usually null)</param>
|
||||
/// <typeparam name="T">The type to cast to</typeparam>
|
||||
/// <returns>Whether the cast was successful</returns>
|
||||
public static bool TryCast<T>(this object o, out T parsed)
|
||||
{
|
||||
try
|
||||
{
|
||||
parsed = (T) o;
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
parsed = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs a function that transforms an object in-line
|
||||
/// </summary>
|
||||
/// <param name="self">The object to run on</param>
|
||||
/// <param name="func">The function to run</param>
|
||||
/// <typeparam name="TIn">The input type</typeparam>
|
||||
/// <typeparam name="TOut">The output type</typeparam>
|
||||
/// <returns></returns>
|
||||
public static TOut SelectO<TIn, TOut>(this TIn self, Func<TIn, TOut> func) => func.Invoke(self);
|
||||
|
||||
/// <summary>
|
||||
/// Runs a function under a condition in-line (equal to if)
|
||||
/// </summary>
|
||||
/// <param name="condition">The condition to check</param>
|
||||
/// <param name="func">The function to run</param>
|
||||
public static void RunIf(bool condition, Action func)
|
||||
{
|
||||
if (condition)
|
||||
func();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parses a string to a value of an enum
|
||||
/// </summary>
|
||||
/// <param name="value">The string to parse</param>
|
||||
/// <typeparam name="TEnum">The enum type (MUST be an enum)</typeparam>
|
||||
/// <returns>The element</returns>
|
||||
public static TEnum ParseToEnum<TEnum>(string value) => (TEnum) Enum.Parse(typeof(TEnum),
|
||||
Enum.GetNames(typeof(TEnum)).First(s => s.ToLower() == value.ToLower()));
|
||||
|
||||
/// <summary>
|
||||
/// Parses a string to a nullable bool (defaults to null if parse fails)
|
||||
/// </summary>
|
||||
/// <param name="value">The st string to parse</param>
|
||||
/// <returns>The output nullable bool</returns>
|
||||
public static bool? ParseBool(string value) =>
|
||||
bool.TryParse(value, out bool tmp) ? (bool?) tmp : null;
|
||||
|
||||
/// <summary>
|
||||
/// AND operation for nullable bools (uses <see cref="True">True</see>)
|
||||
/// </summary>
|
||||
/// <param name="left">First bool to check</param>
|
||||
/// <param name="right">Second bool to check</param>
|
||||
/// <returns>The operation result</returns>
|
||||
public static bool And(this bool? left, bool? right) => left.True() && right.True();
|
||||
|
||||
/// <summary>
|
||||
/// OR operation for nullable bools (uses <see cref="True">True</see>)
|
||||
/// </summary>
|
||||
/// <param name="left">First bool to check</param>
|
||||
/// <param name="right">Second bool to check</param>
|
||||
/// <returns>The operation result</returns>
|
||||
public static bool Or(this bool? left, bool? right) => left.True() || right.True();
|
||||
|
||||
/// <summary>
|
||||
/// XOR operation for nullable bools (uses <see cref="True">True</see>)
|
||||
/// </summary>
|
||||
/// <param name="left">First bool to check</param>
|
||||
/// <param name="right">Second bool to check</param>
|
||||
/// <returns>The operation result</returns>
|
||||
public static bool Xor(this bool? left, bool? right) => left.Or(right) && !left.And(right);
|
||||
|
||||
/// <summary>
|
||||
/// Whether the nullable bool is true (null->false)
|
||||
/// </summary>
|
||||
/// <param name="self">Value to check</param>
|
||||
/// <returns>Whether it is true</returns>
|
||||
public static bool True(this bool? self) => self == true;
|
||||
|
||||
/// <summary>
|
||||
/// Whether the nullable bool is false (null->false)
|
||||
/// </summary>
|
||||
/// <param name="self">Value to check</param>
|
||||
/// <returns>Whether it is false</returns>
|
||||
public static bool False(this bool? self) => self == false;
|
||||
|
||||
/// <summary>
|
||||
/// Whether the nullable bool is null
|
||||
/// </summary>
|
||||
/// <param name="self">Value to check</param>
|
||||
/// <returns>Whether it is null</returns>
|
||||
public static bool Null(this bool? self) => self == null;
|
||||
|
||||
/// <summary>
|
||||
/// Removes an element from a dictionary by its index (not key)
|
||||
/// </summary>
|
||||
/// <param name="dict">The dictionary to remove from</param>
|
||||
/// <param name="index">The index of the value</param>
|
||||
/// <typeparam name="TKey">The key type</typeparam>
|
||||
/// <typeparam name="TValue">The value type</typeparam>
|
||||
public static void RemoveAt<TKey, TValue>(this Dictionary<TKey, TValue> dict, int index) =>
|
||||
dict.Remove(dict.Keys.ToArray()[index]);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the size of a dictionary
|
||||
/// </summary>
|
||||
/// <param name="directory">The dictionary to check</param>
|
||||
/// <returns>The size of the dictionary</returns>
|
||||
public static long GetSize(this DirectoryInfo directory) => IO.GetDirectorySize(directory.FullName);
|
||||
|
||||
/// <summary>
|
||||
/// Adds a directory to an archive recursively
|
||||
/// </summary>
|
||||
/// <param name="archive">The archive to add to</param>
|
||||
/// <param name="folderPath">The directory to add</param>
|
||||
/// <param name="entryName">The name of the directory in-archive</param>
|
||||
/// <param name="ignoredExtensions">Extensions for files to ignore</param>
|
||||
/// <param name="ignoredPaths">Paths to exclude from adding</param>
|
||||
/// <returns>The new entry</returns>
|
||||
public static ZipArchiveEntry AddDirectory(this ZipArchive archive, string folderPath, string entryName,
|
||||
string[] ignoredExtensions, string[] ignoredPaths)
|
||||
{
|
||||
entryName = entryName.TrimEnd('/');
|
||||
ZipArchiveEntry result = archive.CreateEntry($"{entryName}/");
|
||||
string[] files = Directory.GetFiles(folderPath);
|
||||
foreach (string t in files)
|
||||
if (!ignoredExtensions.Contains(Path.GetExtension(t)) &&
|
||||
!ignoredPaths.Any(s => IO.CheckPathEqual(s, t)))
|
||||
archive.CreateEntryFromFile(t, $"{entryName}/{Path.GetFileName(t)}");
|
||||
string[] dirs = Directory.GetDirectories(folderPath);
|
||||
foreach (string t in dirs)
|
||||
if (!ignoredPaths.Any(s => IO.CheckPathEqual(s, t)))
|
||||
archive.AddDirectory(t, $"{entryName}/{Path.GetFileName(t)}", ignoredExtensions,
|
||||
ignoredPaths);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// "Unshorten" (follow) an URL
|
||||
/// </summary>
|
||||
/// <param name="self">The URL to unshorten</param>
|
||||
/// <returns>The unshortened URL</returns>
|
||||
public static Uri Unshorten(this Uri self)
|
||||
{
|
||||
HttpWebRequest req = (HttpWebRequest) WebRequest.Create(self);
|
||||
req.AllowAutoRedirect = true;
|
||||
req.MaximumAutomaticRedirections = 100;
|
||||
WebResponse resp = req.GetResponse();
|
||||
return resp.ResponseUri;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pings an URL to check for availability
|
||||
/// </summary>
|
||||
/// <param name="self">The URL to check</param>
|
||||
/// <returns>Whether the service is online</returns>
|
||||
public static bool Ping(this Uri self)
|
||||
{
|
||||
try
|
||||
{
|
||||
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(self);
|
||||
request.Timeout = 3000;
|
||||
request.AllowAutoRedirect = true;
|
||||
using WebResponse response = request.GetResponse();
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rounds a RectangleF to a Rectangle instead of flooring
|
||||
/// </summary>
|
||||
/// <param name="self">The RectangleF to round</param>
|
||||
/// <returns>The rounded Rectangle</returns>
|
||||
public static Rectangle Round(this RectangleF self) => Rectangle.Round(self);
|
||||
|
||||
/// <summary>
|
||||
/// Ceilings a RectangleF to a Rectangle instead of flooring
|
||||
/// </summary>
|
||||
/// <param name="self">The RectangleF to ceil (?)</param>
|
||||
/// <returns>The ceiled (?) Rectangle</returns>
|
||||
public static Rectangle Ceiling(this RectangleF self) => Rectangle.Ceiling(self);
|
||||
|
||||
/// <summary>
|
||||
/// Extension method for <see cref="Crypto">Crypto's</see> Encrypt
|
||||
/// </summary>
|
||||
|
|
|
@ -7,11 +7,11 @@
|
|||
<Deterministic>false</Deterministic>
|
||||
<PackageId>CC-Functions.Misc</PackageId>
|
||||
<Title>CC-Functions.Misc</Title>
|
||||
<Authors>CC24</Authors>
|
||||
<Authors>JFronny</Authors>
|
||||
<Description>Random pieces of code used across my projects. I do NOT recommend using this in your own project!</Description>
|
||||
<Copyright>Copyright 2020</Copyright>
|
||||
<PackageProjectUrl>https://github.com/JFronny/CC-Functions</PackageProjectUrl>
|
||||
<RepositoryUrl>https://github.com/JFronny/CC-Functions.git</RepositoryUrl>
|
||||
<PackageProjectUrl>https://gitlab.com/JFronny/CC-Functions</PackageProjectUrl>
|
||||
<RepositoryUrl>https://gitlab.com/JFronny/CC-Functions.git</RepositoryUrl>
|
||||
<RepositoryType>git</RepositoryType>
|
||||
<AssemblyVersion>1.1.*</AssemblyVersion>
|
||||
<FileVersion>1.0.0.0</FileVersion>
|
||||
|
@ -36,4 +36,7 @@
|
|||
<Reference Include="System.IO.Compression" Condition="'$(TargetFramework)' == 'net461'" />
|
||||
<Reference Include="System.IO.Compression.FileSystem" Condition="'$(TargetFramework)' == 'net461'" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Core\Core.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
|
|
@ -7,11 +7,11 @@
|
|||
<UseWindowsForms>true</UseWindowsForms>
|
||||
<TargetFrameworks>net461;netcoreapp3.1</TargetFrameworks>
|
||||
<Deterministic>false</Deterministic>
|
||||
<Authors>CC24</Authors>
|
||||
<Authors>JFronny</Authors>
|
||||
<Description>W32 Additions for CC-Functions</Description>
|
||||
<Copyright>Copyright 2020</Copyright>
|
||||
<PackageProjectUrl>https://github.com/JFronny/CC-Functions</PackageProjectUrl>
|
||||
<RepositoryUrl>https://github.com/JFronny/CC-Functions.git</RepositoryUrl>
|
||||
<PackageProjectUrl>https://gitlab.com/JFronny/CC-Functions</PackageProjectUrl>
|
||||
<RepositoryUrl>https://gitlab.com/JFronny/CC-Functions.git</RepositoryUrl>
|
||||
<RepositoryType>git</RepositoryType>
|
||||
<AssemblyVersion>1.1.*</AssemblyVersion>
|
||||
<FileVersion>1.0.0.0</FileVersion>
|
||||
|
|
Reference in New Issue