114 lines
3.5 KiB
C#
114 lines
3.5 KiB
C#
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace Mitria_Minecraft_Launcher
|
|
{
|
|
public static class Extensions
|
|
{
|
|
public static bool EmptyDirectory(string location)
|
|
{
|
|
bool error = false;
|
|
|
|
DirectoryInfo directoryInfo = new DirectoryInfo(location);
|
|
|
|
foreach (var fi in directoryInfo.EnumerateFiles())
|
|
{
|
|
try
|
|
{
|
|
fi.IsReadOnly = false;
|
|
fi.Delete();
|
|
|
|
while (fi.Exists)
|
|
{
|
|
System.Threading.Thread.Sleep(10);
|
|
fi.Refresh();
|
|
}
|
|
}
|
|
catch (IOException e)
|
|
{
|
|
CommonLibrary.Log.ERROR(e.Message);
|
|
error = true;
|
|
}
|
|
}
|
|
foreach (var di in directoryInfo.EnumerateDirectories())
|
|
{
|
|
try
|
|
{
|
|
EmptyDirectory(di.FullName);
|
|
di.Delete();
|
|
while (di.Exists)
|
|
{
|
|
System.Threading.Thread.Sleep(10);
|
|
di.Refresh();
|
|
}
|
|
}
|
|
catch (IOException e)
|
|
{
|
|
CommonLibrary.Log.ERROR(e.Message);
|
|
error = true;
|
|
}
|
|
}
|
|
|
|
return error;
|
|
}
|
|
|
|
public static string ApplyExcuteCommand(ExcuteArgument excuteArgument)
|
|
{
|
|
string command = excuteArgument.Argument;
|
|
string regexPattern = @"\{[A-z]*\}";
|
|
Regex regex = new Regex(regexPattern);
|
|
MatchCollection resultCollection = regex.Matches(command);
|
|
List<string> commandList = new List<string>();
|
|
foreach (Match match in resultCollection)
|
|
{
|
|
string key = match.Value.Substring(1, match.Value.Length - 2);
|
|
if (!commandList.Contains(key))
|
|
{
|
|
commandList.Add(key);
|
|
}
|
|
}
|
|
|
|
foreach (string commandKey in commandList)
|
|
{
|
|
Parameter option = excuteArgument.Parameters.Find(x => x.Key == commandKey);
|
|
command = command.Replace("{" + commandKey + "}", option.Value);
|
|
}
|
|
|
|
return command;
|
|
}
|
|
|
|
|
|
public static bool IsDefault<T>(ref this T data) where T : struct
|
|
{
|
|
return default(T).Equals(data);
|
|
}
|
|
public static string UnitSeparator(this long value)
|
|
{
|
|
return string.Format("{0:#,0}", (object)value);
|
|
}
|
|
|
|
public static string UnitSeparator(this int value)
|
|
{
|
|
return string.Format("{0:#,0}", (object)value);
|
|
}
|
|
public static string GetFileHashCode(this FileInfo file)
|
|
{
|
|
using (MD5 md5 = MD5.Create())
|
|
{
|
|
using (Stream stream = file.OpenRead())
|
|
{
|
|
byte[] hashCode = md5.ComputeHash(stream);
|
|
StringBuilder hashStringBuilder = new StringBuilder();
|
|
foreach (byte b in hashCode)
|
|
{
|
|
hashStringBuilder.AppendFormat("{0:x2}", b);
|
|
}
|
|
return hashStringBuilder.ToString();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |