diff --git a/Global.cs b/Global.cs new file mode 100644 index 0000000..09e6df8 --- /dev/null +++ b/Global.cs @@ -0,0 +1,11 @@ +global using System.Diagnostics; +global using System.Net; +global using System.Security.Cryptography; +global using System; +global using System.Collections.Generic; +global using System.Linq; +global using System.Text; +global using System.Threading.Tasks; + +global using static Xorog.UniversalExtensions.UniversalExtensionsEnums; +global using static Xorog.UniversalExtensions.Internal; \ No newline at end of file diff --git a/Internal.cs b/Internal.cs new file mode 100644 index 0000000..c36c085 --- /dev/null +++ b/Internal.cs @@ -0,0 +1,79 @@ +namespace Xorog.UniversalExtensions; + +internal static class Internal +{ + internal static string GetShortTimeFormat(this TimeSpan _timespan, TimeFormat timeFormat) + { + switch (timeFormat) + { + case TimeFormat.HOURS: + if (_timespan.TotalDays >= 1) + return $"{(Math.Floor(_timespan.TotalHours).ToString().Length == 1 ? $"0{Math.Floor(_timespan.TotalHours)}" : Math.Floor(_timespan.TotalHours))}:" + + $"{(_timespan.Minutes.ToString().Length == 1 ? $"0{_timespan.Minutes}" : _timespan.Minutes)}:" + + $"{(_timespan.Seconds.ToString().Length == 1 ? $"0{_timespan.Seconds}" : _timespan.Seconds)}"; + + if (_timespan.TotalHours >= 1) + return $"{(_timespan.Hours.ToString().Length == 1 ? $"0{_timespan.Hours}" : _timespan.Hours)}:" + + $"{(_timespan.Minutes.ToString().Length == 1 ? $"0{_timespan.Minutes}" : _timespan.Minutes)}:" + + $"{(_timespan.Seconds.ToString().Length == 1 ? $"0{_timespan.Seconds}" : _timespan.Seconds)}"; + + return $"{(_timespan.Minutes.ToString().Length == 1 ? $"0{_timespan.Minutes}" : _timespan.Minutes)}:" + + $"{(_timespan.Seconds.ToString().Length == 1 ? $"0{_timespan.Seconds}" : _timespan.Seconds)}"; + case TimeFormat.DAYS: + if (_timespan.TotalDays >= 1) + return $"{(Math.Floor(_timespan.TotalDays).ToString().Length == 1 ? $"0{Math.Floor(_timespan.TotalDays)}" : Math.Floor(_timespan.TotalDays))}" + + $"{(_timespan.Hours.ToString().Length == 1 ? $"0{_timespan.Hours}" : _timespan.Hours)}:" + + $"{(_timespan.Minutes.ToString().Length == 1 ? $"0{_timespan.Minutes}" : _timespan.Minutes)}:" + + $"{(_timespan.Seconds.ToString().Length == 1 ? $"0{_timespan.Seconds}" : _timespan.Seconds)}"; + + if (_timespan.TotalHours >= 1) + return $"{(Math.Floor(_timespan.TotalHours).ToString().Length == 1 ? $"0{Math.Floor(_timespan.TotalHours)}" : Math.Floor(_timespan.TotalHours))}:" + + $"{(_timespan.Minutes.ToString().Length == 1 ? $"0{_timespan.Minutes}" : _timespan.Minutes)}:" + + $"{(_timespan.Seconds.ToString().Length == 1 ? $"0{_timespan.Seconds}" : _timespan.Seconds)}"; + + return $"{(_timespan.Minutes.ToString().Length == 1 ? $"0{_timespan.Minutes}" : _timespan.Minutes)}:" + + $"{(_timespan.Seconds.ToString().Length == 1 ? $"0{_timespan.Seconds}" : _timespan.Seconds)}"; + + case TimeFormat.MINUTES: + if (_timespan.TotalHours >= 1) + return $"{(Math.Floor(_timespan.TotalMinutes).ToString().Length == 1 ? $"0{Math.Floor(_timespan.TotalMinutes)}" : Math.Floor(_timespan.TotalMinutes))}:" + + $"{(_timespan.Seconds.ToString().Length == 1 ? $"0{_timespan.Seconds}" : _timespan.Seconds)}"; + + return $"{(_timespan.Minutes.ToString().Length == 1 ? $"0{_timespan.Minutes}" : _timespan.Minutes)}:" + + $"{(_timespan.Seconds.ToString().Length == 1 ? $"0{_timespan.Seconds}" : _timespan.Seconds)}"; + + default: + return _timespan.ToString(); + } + } + internal static string GetTimeFormat(this TimeSpan _timespan, TimeFormat timeFormat) + { + switch (timeFormat) + { + case TimeFormat.HOURS: + if (_timespan.TotalDays >= 1) + return $"{Math.Floor(_timespan.TotalHours)} hours, {_timespan.Minutes} minutes"; + + if (_timespan.TotalHours >= 1) + return $"{_timespan.Hours} hours, {_timespan.Minutes} minutes"; + + return $"{_timespan.Minutes} minutes, {_timespan.Seconds} seconds"; + case TimeFormat.DAYS: + if (_timespan.TotalDays >= 1) + return $"{Math.Floor(_timespan.TotalDays)} days, {_timespan.Hours} hours"; + + if (_timespan.TotalHours >= 1) + return $"{_timespan.Hours} hours, {_timespan.Minutes} minutes"; + + return $"{_timespan.Minutes} minutes, {_timespan.Seconds} seconds"; + + case TimeFormat.MINUTES: + if (_timespan.TotalHours >= 1) + return $"{Math.Floor(_timespan.TotalMinutes)} minutes, {_timespan.Seconds} seconds"; + return $"{_timespan.Minutes} minutes, {_timespan.Seconds} seconds"; + + default: + return _timespan.ToString(); + } + } +} \ No newline at end of file diff --git a/UniversalExtensions.Enums.cs b/UniversalExtensions.Enums.cs new file mode 100644 index 0000000..ff9f968 --- /dev/null +++ b/UniversalExtensions.Enums.cs @@ -0,0 +1,11 @@ +namespace Xorog.UniversalExtensions; + +public class UniversalExtensionsEnums +{ + public enum TimeFormat + { + MINUTES, + HOURS, + DAYS + } +} \ No newline at end of file diff --git a/UniversalExtensions.cs b/UniversalExtensions.cs index c75206e..745fe88 100644 --- a/UniversalExtensions.cs +++ b/UniversalExtensions.cs @@ -1,24 +1,68 @@ -using System.Diagnostics; -using System.Net; -using System.Security.Cryptography; - -namespace Xorog.UniversalExtensions; +namespace Xorog.UniversalExtensions; public static class UniversalExtensions { + /// + /// Get the current CPU Usage on all plattforms + /// + /// + public static async Task GetCpuUsageForProcess() + { + var startTime = DateTime.UtcNow; + var startCpuUsage = Process.GetCurrentProcess().TotalProcessorTime; + await Task.Delay(500); + + var endTime = DateTime.UtcNow; + var endCpuUsage = Process.GetCurrentProcess().TotalProcessorTime; + var cpuUsedMs = (endCpuUsage - startCpuUsage).TotalMilliseconds; + var totalMsPassed = (endTime - startTime).TotalMilliseconds; + var cpuUsageTotal = cpuUsedMs / (Environment.ProcessorCount * totalMsPassed); + return cpuUsageTotal * 100; + } + + /// - /// Remove unsupported characters from string to generate a valid filename + /// Copy a directory recursively /// - /// The string with potentionally unwanted characters - /// The character the unwanted characters get replaced with (default: _) - /// A valid filename - public static string MakeValidFileName(this string name, char replace_char = '_') + /// + /// + /// + /// + public static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs) { - string invalidChars = System.Text.RegularExpressions.Regex.Escape(new string(System.IO.Path.GetInvalidFileNameChars())); - string invalidRegStr = string.Format(@"([{0}]*\.+$)|([{0}]+)", invalidChars); + // Get the subdirectories for the specified directory. + DirectoryInfo dir = new(sourceDirName); - return System.Text.RegularExpressions.Regex.Replace(name, invalidRegStr, replace_char.ToString()).Replace('&', replace_char); + if (!dir.Exists) + { + throw new DirectoryNotFoundException( + "Source directory does not exist or could not be found: " + + sourceDirName); + } + + DirectoryInfo[] dirs = dir.GetDirectories(); + + // If the destination directory doesn't exist, create it. + Directory.CreateDirectory(destDirName); + + // Get the files in the directory and copy them to the new location. + FileInfo[] files = dir.GetFiles(); + foreach (FileInfo file in files) + { + string tempPath = Path.Combine(destDirName, file.Name); + file.CopyTo(tempPath, false); + } + + // If copying subdirectories, copy them and their contents to new location. + if (copySubDirs) + { + foreach (DirectoryInfo subdir in dirs) + { + string tempPath = Path.Combine(destDirName, subdir.Name); + DirectoryCopy(subdir.FullName, tempPath, copySubDirs); + } + } } @@ -124,6 +168,22 @@ public static class UniversalExtensions + /// + /// Remove unsupported characters from string to generate a valid filename + /// + /// The string with potentionally unwanted characters + /// The character the unwanted characters get replaced with (default: _) + /// A valid filename + public static string MakeValidFileName(this string name, char replace_char = '_') + { + string invalidChars = System.Text.RegularExpressions.Regex.Escape(new string(System.IO.Path.GetInvalidFileNameChars())); + string invalidRegStr = string.Format(@"([{0}]*\.+$)|([{0}]+)", invalidChars); + + return System.Text.RegularExpressions.Regex.Replace(name, invalidRegStr, replace_char.ToString()).Replace('&', replace_char); + } + + + /// /// Compute the SHA256-Hash for a given file /// @@ -149,6 +209,7 @@ public static class UniversalExtensions (until.ToUniversalTime() - DateTime.UtcNow); + /// /// /// @@ -262,83 +323,6 @@ public static class UniversalExtensions - private static string GetShortTimeFormat(this TimeSpan _timespan, TimeFormat timeFormat) - { - switch (timeFormat) - { - case TimeFormat.HOURS: - if (_timespan.TotalDays >= 1) - return $"{(Math.Floor(_timespan.TotalHours).ToString().Length == 1 ? $"0{Math.Floor(_timespan.TotalHours)}" : Math.Floor(_timespan.TotalHours))}:" + - $"{(_timespan.Minutes.ToString().Length == 1 ? $"0{_timespan.Minutes}" : _timespan.Minutes)}:" + - $"{(_timespan.Seconds.ToString().Length == 1 ? $"0{_timespan.Seconds}" : _timespan.Seconds)}"; - - if (_timespan.TotalHours >= 1) - return $"{(_timespan.Hours.ToString().Length == 1 ? $"0{_timespan.Hours}" : _timespan.Hours)}:" + - $"{(_timespan.Minutes.ToString().Length == 1 ? $"0{_timespan.Minutes}" : _timespan.Minutes)}:" + - $"{(_timespan.Seconds.ToString().Length == 1 ? $"0{_timespan.Seconds}" : _timespan.Seconds)}"; - - return $"{(_timespan.Minutes.ToString().Length == 1 ? $"0{_timespan.Minutes}" : _timespan.Minutes)}:" + - $"{(_timespan.Seconds.ToString().Length == 1 ? $"0{_timespan.Seconds}" : _timespan.Seconds)}"; - case TimeFormat.DAYS: - if (_timespan.TotalDays >= 1) - return $"{(Math.Floor(_timespan.TotalDays).ToString().Length == 1 ? $"0{Math.Floor(_timespan.TotalDays)}" : Math.Floor(_timespan.TotalDays))}" + - $"{(_timespan.Hours.ToString().Length == 1 ? $"0{_timespan.Hours}" : _timespan.Hours)}:" + - $"{(_timespan.Minutes.ToString().Length == 1 ? $"0{_timespan.Minutes}" : _timespan.Minutes)}:" + - $"{(_timespan.Seconds.ToString().Length == 1 ? $"0{_timespan.Seconds}" : _timespan.Seconds)}"; - - if (_timespan.TotalHours >= 1) - return $"{(Math.Floor(_timespan.TotalHours).ToString().Length == 1 ? $"0{Math.Floor(_timespan.TotalHours)}" : Math.Floor(_timespan.TotalHours))}:" + - $"{(_timespan.Minutes.ToString().Length == 1 ? $"0{_timespan.Minutes}" : _timespan.Minutes)}:" + - $"{(_timespan.Seconds.ToString().Length == 1 ? $"0{_timespan.Seconds}" : _timespan.Seconds)}"; - - return $"{(_timespan.Minutes.ToString().Length == 1 ? $"0{_timespan.Minutes}" : _timespan.Minutes)}:" + - $"{(_timespan.Seconds.ToString().Length == 1 ? $"0{_timespan.Seconds}" : _timespan.Seconds)}"; - - case TimeFormat.MINUTES: - if (_timespan.TotalHours >= 1) - return $"{(Math.Floor(_timespan.TotalMinutes).ToString().Length == 1 ? $"0{Math.Floor(_timespan.TotalMinutes)}" : Math.Floor(_timespan.TotalMinutes))}:" + - $"{(_timespan.Seconds.ToString().Length == 1 ? $"0{_timespan.Seconds}" : _timespan.Seconds)}"; - - return $"{(_timespan.Minutes.ToString().Length == 1 ? $"0{_timespan.Minutes}" : _timespan.Minutes)}:" + - $"{(_timespan.Seconds.ToString().Length == 1 ? $"0{_timespan.Seconds}" : _timespan.Seconds)}"; - - default: - return _timespan.ToString(); - } - } - private static string GetTimeFormat(this TimeSpan _timespan, TimeFormat timeFormat) - { - switch (timeFormat) - { - case TimeFormat.HOURS: - if (_timespan.TotalDays >= 1) - return $"{Math.Floor(_timespan.TotalHours)} hours, {_timespan.Minutes} minutes"; - - if (_timespan.TotalHours >= 1) - return $"{_timespan.Hours} hours, {_timespan.Minutes} minutes"; - - return $"{_timespan.Minutes} minutes, {_timespan.Seconds} seconds"; - case TimeFormat.DAYS: - if (_timespan.TotalDays >= 1) - return $"{Math.Floor(_timespan.TotalDays)} days, {_timespan.Hours} hours"; - - if (_timespan.TotalHours >= 1) - return $"{_timespan.Hours} hours, {_timespan.Minutes} minutes"; - - return $"{_timespan.Minutes} minutes, {_timespan.Seconds} seconds"; - - case TimeFormat.MINUTES: - if (_timespan.TotalHours >= 1) - return $"{Math.Floor(_timespan.TotalMinutes)} minutes, {_timespan.Seconds} seconds"; - return $"{_timespan.Minutes} minutes, {_timespan.Seconds} seconds"; - - default: - return _timespan.ToString(); - } - } - - - /// /// Check if a string contains only digits /// @@ -364,78 +348,6 @@ public static class UniversalExtensions /// public static string GetAllDigits(this string str) => new String(str.Where(Char.IsDigit).ToArray()); - - public enum TimeFormat - { - MINUTES, - HOURS, - DAYS - } - - - - /// - /// Get the current CPU Usage on all plattforms - /// - /// - public static async Task GetCpuUsageForProcess() - { - var startTime = DateTime.UtcNow; - var startCpuUsage = Process.GetCurrentProcess().TotalProcessorTime; - await Task.Delay(500); - - var endTime = DateTime.UtcNow; - var endCpuUsage = Process.GetCurrentProcess().TotalProcessorTime; - var cpuUsedMs = (endCpuUsage - startCpuUsage).TotalMilliseconds; - var totalMsPassed = (endTime - startTime).TotalMilliseconds; - var cpuUsageTotal = cpuUsedMs / (Environment.ProcessorCount * totalMsPassed); - return cpuUsageTotal * 100; - } - - - - /// - /// Copy a directory recursively - /// - /// - /// - /// - /// - public static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs) - { - // Get the subdirectories for the specified directory. - DirectoryInfo dir = new(sourceDirName); - - if (!dir.Exists) - { - throw new DirectoryNotFoundException( - "Source directory does not exist or could not be found: " - + sourceDirName); - } - - DirectoryInfo[] dirs = dir.GetDirectories(); - - // If the destination directory doesn't exist, create it. - Directory.CreateDirectory(destDirName); - - // Get the files in the directory and copy them to the new location. - FileInfo[] files = dir.GetFiles(); - foreach (FileInfo file in files) - { - string tempPath = Path.Combine(destDirName, file.Name); - file.CopyTo(tempPath, false); - } - - // If copying subdirectories, copy them and their contents to new location. - if (copySubDirs) - { - foreach (DirectoryInfo subdir in dirs) - { - string tempPath = Path.Combine(destDirName, subdir.Name); - DirectoryCopy(subdir.FullName, tempPath, copySubDirs); - } - } - } } public static class StringExt diff --git a/Xorog.UniversalExtensions.csproj b/Xorog.UniversalExtensions.csproj index 132c02c..06ece7c 100644 --- a/Xorog.UniversalExtensions.csproj +++ b/Xorog.UniversalExtensions.csproj @@ -6,4 +6,8 @@ enable + + + +