Novetus_src/NovetusLauncher/NovetusFuncs/CodeExtensions.cs

168 lines
4.3 KiB
C#

/*
* Created by SharpDevelop.
* User: Bitl
* Date: 10/10/2019
* Time: 7:00 AM
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO.Compression;
using System.IO;
public static class RichTextBoxExtensions
{
public static void AppendText(this RichTextBox box, string text, Color color)
{
box.SelectionStart = box.TextLength;
box.SelectionLength = 0;
box.SelectionColor = color;
box.AppendText(text);
box.SelectionColor = box.ForeColor;
}
}
public static class ProcessExtensions
{
public static bool IsRunning(this Process process)
{
try {
Process.GetProcessById(process.Id);
} catch (InvalidOperationException) when (!Env.Debugging) {
return false;
} catch (ArgumentException) when (!Env.Debugging) {
return false;
}
return true;
}
}
public static class StringExtensions
{
public static bool Contains(this string source, string toCheck, StringComparison comp)
{
if (source == null)
return false;
return source.IndexOf(toCheck, comp) >= 0;
}
}
public static class ZipArchiveExtensions
{
public static void ExtractToDirectory(this ZipArchive archive, string destinationDirectoryName, bool overwrite)
{
if (!overwrite)
{
archive.ExtractToDirectory(destinationDirectoryName);
return;
}
DirectoryInfo di = Directory.CreateDirectory(destinationDirectoryName);
string destinationDirectoryFullPath = di.FullName;
foreach (ZipArchiveEntry file in archive.Entries)
{
string completeFileName = Path.GetFullPath(Path.Combine(destinationDirectoryFullPath, file.FullName));
if (!completeFileName.StartsWith(destinationDirectoryFullPath, StringComparison.OrdinalIgnoreCase))
{
throw new IOException("Trying to extract file outside of destination directory. See this link for more info: https://snyk.io/research/zip-slip-vulnerability");
}
if (file.Name == "")
{// Assuming Empty for Directory
Directory.CreateDirectory(Path.GetDirectoryName(completeFileName));
continue;
}
file.ExtractToFile(completeFileName, true);
}
}
}
//credit to code4life
public static class ArrayHelper
{
public static object FindInDimensions(this object[,] target,
object searchTerm)
{
object result = null;
var rowLowerLimit = target.GetLowerBound(0);
var rowUpperLimit = target.GetUpperBound(0);
var colLowerLimit = target.GetLowerBound(1);
var colUpperLimit = target.GetUpperBound(1);
for (int row = rowLowerLimit; row < rowUpperLimit; row++)
{
for (int col = colLowerLimit; col < colUpperLimit; col++)
{
// you could do the search here...
}
}
return result;
}
}
//dotnetperls
static class SubstringExtensions
{
/// <summary>
/// Get string value between [first] a and [last] b.
/// </summary>
public static string Between(this string value, string a, string b)
{
int posA = value.IndexOf(a);
int posB = value.LastIndexOf(b);
if (posA == -1)
{
return "";
}
if (posB == -1)
{
return "";
}
int adjustedPosA = posA + a.Length;
if (adjustedPosA >= posB)
{
return "";
}
return value.Substring(adjustedPosA, posB - adjustedPosA);
}
/// <summary>
/// Get string value after [first] a.
/// </summary>
public static string Before(this string value, string a)
{
int posA = value.IndexOf(a);
if (posA == -1)
{
return "";
}
return value.Substring(0, posA);
}
/// <summary>
/// Get string value after [last] a.
/// </summary>
public static string After(this string value, string a)
{
int posA = value.LastIndexOf(a);
if (posA == -1)
{
return "";
}
int adjustedPosA = posA + a.Length;
if (adjustedPosA >= value.Length)
{
return "";
}
return value.Substring(adjustedPosA);
}
}