105 lines
3.6 KiB
C#
105 lines
3.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using Vestris.ResourceLib;
|
|
|
|
namespace KattekerCreator.Helper
|
|
{
|
|
public static class Utils
|
|
{
|
|
public static bool CheckFileExistens(string filePath)
|
|
{
|
|
if (File.Exists(filePath)) return true;
|
|
var file = filePath.Replace(Directory.GetParent(filePath) + "\\", "");
|
|
Console.WriteLine("Checking the prerequisites has failed.");
|
|
Console.WriteLine($"{file} is missing. Program exits now.");
|
|
Console.ReadKey();
|
|
return false;
|
|
}
|
|
|
|
public static string CreateTempDirectory()
|
|
{
|
|
string result;
|
|
do
|
|
{
|
|
result = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "temp", Environment.UserName,
|
|
Path.GetRandomFileName());
|
|
} while (Directory.Exists(result));
|
|
|
|
Directory.CreateDirectory(result);
|
|
return result;
|
|
}
|
|
|
|
public static bool IsFilesizeWrong(string filename)
|
|
{
|
|
var file = new FileInfo(filename);
|
|
return !file.Exists || file.Length < 524288;
|
|
}
|
|
|
|
public static void CopyResources(string inFile, string outFile)
|
|
{
|
|
using (var ri = new ResourceInfo())
|
|
{
|
|
ri.Load(inFile);
|
|
|
|
if (ri.ResourceTypes.Any(x => x.ResourceType == Kernel32.ResourceTypes.RT_GROUP_ICON))
|
|
{
|
|
var groupIcon = ri[Kernel32.ResourceTypes.RT_GROUP_ICON];
|
|
var iconDictionary = groupIcon.FirstOrDefault() as IconDirectoryResource;
|
|
iconDictionary?.SaveTo(outFile);
|
|
}
|
|
if (ri.ResourceTypes.Any(x => x.ResourceType == Kernel32.ResourceTypes.RT_VERSION))
|
|
{
|
|
var versionResource = (VersionResource)ri[Kernel32.ResourceTypes.RT_VERSION].First();
|
|
versionResource.Language = 0;
|
|
versionResource.SaveTo(outFile);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public static void DeleteTempDir(string tempDir)
|
|
{
|
|
#if !DEBUG
|
|
try
|
|
{
|
|
Directory.Delete(tempDir, true);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
//silently ignore
|
|
}
|
|
#endif
|
|
}
|
|
|
|
public static IEnumerable<FileInfo> EnumerateFiles(string programFile, IEnumerable<string> userdefinedFileExclusions = null)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(programFile)) return null;
|
|
var directoryName = Path.GetDirectoryName(programFile);
|
|
if (directoryName == null) return null;
|
|
var files = new DirectoryInfo(directoryName).EnumerateFiles("*.*", SearchOption.AllDirectories);
|
|
var filter = FileExclusions(userdefinedFileExclusions).ToArray();
|
|
return files.Where(x => !filter.Any(y => x.Name.EndsWith(y, StringComparison.Ordinal)));
|
|
}
|
|
|
|
private static IEnumerable<string> FileExclusions(IEnumerable<string> userdefinedfileExclusions = null)
|
|
{
|
|
yield return ".pdb";
|
|
yield return ".tmp";
|
|
yield return ".obj";
|
|
yield return ".pch";
|
|
yield return ".vshost.exe";
|
|
yield return ".vshost.exe.config";
|
|
yield return ".vshost.exe.manifest";
|
|
yield return "squirrelHelperInfo.json";
|
|
yield return "Katteker.config";
|
|
|
|
if (userdefinedfileExclusions == null) yield break;
|
|
foreach (var fileExclusion in userdefinedfileExclusions)
|
|
{
|
|
yield return fileExclusion;
|
|
}
|
|
}
|
|
}
|
|
} |