Initial commit
This commit is contained in:
51
Creator/Helper/Log.cs
Normal file
51
Creator/Helper/Log.cs
Normal file
@ -0,0 +1,51 @@
|
||||
using System;
|
||||
|
||||
namespace KattekerCreator.Helper
|
||||
{
|
||||
public static class Log
|
||||
{
|
||||
private const string Info = "INFO";
|
||||
private const string Error = "ERROR";
|
||||
|
||||
public static void WriteInfoLine(string text)
|
||||
{
|
||||
using (var c = new ConsoleWithOtherColor())
|
||||
{
|
||||
c.WriteLine($"{Info} [{DateTime.Now:G}] {text}");
|
||||
}
|
||||
}
|
||||
|
||||
public static void WriteErrorLine(string text)
|
||||
{
|
||||
using (var c = new ConsoleWithOtherColor(ConsoleColor.DarkRed))
|
||||
{
|
||||
c.WriteLine($"{Error} [{DateTime.Now:G}] {text}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class ConsoleWithOtherColor : IDisposable
|
||||
{
|
||||
public ConsoleWithOtherColor()
|
||||
{
|
||||
}
|
||||
|
||||
private readonly ConsoleColor? _defaultColor;
|
||||
|
||||
public ConsoleWithOtherColor(ConsoleColor color)
|
||||
{
|
||||
_defaultColor = Console.ForegroundColor;
|
||||
Console.ForegroundColor = color;
|
||||
}
|
||||
|
||||
public void WriteLine(string text)
|
||||
{
|
||||
Console.WriteLine(text);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_defaultColor != null) Console.ForegroundColor = _defaultColor.Value;
|
||||
}
|
||||
}
|
||||
}
|
166
Creator/Helper/SimpleCommandLineParser.cs
Normal file
166
Creator/Helper/SimpleCommandLineParser.cs
Normal file
@ -0,0 +1,166 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace KattekerCreator.Helper
|
||||
{
|
||||
public class SimpleCommandLineParser<TObject> where TObject : new()
|
||||
{
|
||||
private TObject _parsedObject;
|
||||
|
||||
public SimpleCommandLineParser(IEnumerable<string> args)
|
||||
{
|
||||
Parse(args);
|
||||
}
|
||||
|
||||
private IDictionary<string, string[]> Arguments { get; } = new Dictionary<string, string[]>();
|
||||
|
||||
public bool Contains(string name) => Arguments.ContainsKey(name);
|
||||
|
||||
public TObject GetObject()
|
||||
{
|
||||
if (!EqualityComparer<TObject>.Default.Equals(_parsedObject, default(TObject)))
|
||||
return _parsedObject;
|
||||
_parsedObject = new TObject();
|
||||
try
|
||||
{
|
||||
LoopProperties(pi =>
|
||||
{
|
||||
var displayName = GetDisplayName(pi);
|
||||
if (displayName == null) return;
|
||||
if (!Arguments.TryGetValue(displayName.ToLowerInvariant(), out var argument)) return;
|
||||
if (pi.PropertyType == typeof(bool))
|
||||
{
|
||||
pi.SetValue(_parsedObject, true);
|
||||
return;
|
||||
}
|
||||
if (pi.PropertyType.IsArray)
|
||||
pi.SetValue(_parsedObject, argument);
|
||||
else
|
||||
pi.SetValue(_parsedObject, argument.FirstOrDefault());
|
||||
});
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.WriteErrorLine("Error parsing commandline arguments: " + e.Message);
|
||||
ShowHelp();
|
||||
}
|
||||
return _parsedObject;
|
||||
}
|
||||
|
||||
public void ShowHelp(TextWriter textWriter = null)
|
||||
{
|
||||
if (textWriter == null) textWriter = Console.Out;
|
||||
var executable = Assembly.GetExecutingAssembly().GetName().Name + ".exe";
|
||||
var assemblyTitle = ((AssemblyTitleAttribute) Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(AssemblyTitleAttribute), false))?.Title ?? executable;
|
||||
var assemblyDescription = ((AssemblyDescriptionAttribute) Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(AssemblyDescriptionAttribute), false))?.Description;
|
||||
|
||||
textWriter.WriteLine();
|
||||
textWriter.WriteLine(assemblyTitle);
|
||||
textWriter.WriteLine();
|
||||
textWriter.WriteLine(assemblyDescription);
|
||||
textWriter.WriteLine();
|
||||
textWriter.WriteLine(executable + " <options>");
|
||||
textWriter.WriteLine();
|
||||
var dict = new Dictionary<string, string>();
|
||||
LoopProperties(pi =>
|
||||
{
|
||||
var displayName = GetDisplayName(pi);
|
||||
if (displayName == null) return;
|
||||
var description = GetDescription(pi);
|
||||
if (pi.PropertyType == typeof(bool)) description = "(Switch) " + description;
|
||||
dict.Add(displayName, description);
|
||||
});
|
||||
var longestWord = dict.Keys.Max(x => x.Length);
|
||||
foreach (var kv in dict)
|
||||
{
|
||||
textWriter.Write("-" + kv.Key.PadRight(longestWord + 3, ' '));
|
||||
textWriter.WriteLine(kv.Value);
|
||||
}
|
||||
textWriter.WriteLine();
|
||||
textWriter.WriteLine("A switch will be set to true, if it will called.");
|
||||
textWriter.WriteLine($"Example: {executable} <Switch>");
|
||||
textWriter.WriteLine();
|
||||
textWriter.WriteLine("A parameter will be set.");
|
||||
textWriter.WriteLine($"Example: {executable} <Key> \"Value\"");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shows the current set options.
|
||||
/// </summary>
|
||||
/// <param name="textWriter">If not set Console.Out will be set.</param>
|
||||
public void ShowProgramArguments(TextWriter textWriter = null)
|
||||
{
|
||||
if (textWriter == null) textWriter = Console.Out;
|
||||
LoopProperties(pi =>
|
||||
{
|
||||
var displayName = GetDisplayName(pi) ?? pi.Name;
|
||||
var value = pi.GetValue(_parsedObject);
|
||||
textWriter.Write(displayName.PadRight(20, ' '));
|
||||
if (value == null)
|
||||
{
|
||||
textWriter.WriteLine("<NOT SET>");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (value.GetType().IsArray)
|
||||
{
|
||||
var arrayAsString = ((IEnumerable) value)
|
||||
.Cast<object>()
|
||||
.Aggregate(string.Empty, (current, x) => current + x?.ToString() + ", ");
|
||||
textWriter.WriteLine(arrayAsString);
|
||||
}
|
||||
else
|
||||
{
|
||||
textWriter.WriteLine(value.ToString());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static TAttribute GetAttribute<TAttribute>(MemberInfo memberInfo) where TAttribute : Attribute =>
|
||||
(TAttribute) Attribute.GetCustomAttribute(memberInfo, typeof(TAttribute));
|
||||
|
||||
private static string GetDescription(MemberInfo memberInfo) => GetAttribute<DescriptionAttribute>(memberInfo)?.Description;
|
||||
|
||||
private static string GetDisplayName(MemberInfo memberInfo) => GetAttribute<DisplayNameAttribute>(memberInfo)?.DisplayName;
|
||||
|
||||
private static void LoopProperties(Action<PropertyInfo> action)
|
||||
{
|
||||
var properties = typeof(TObject).GetProperties(BindingFlags.Public | BindingFlags.Instance);
|
||||
foreach (var propertyInfo in properties)
|
||||
action.Invoke(propertyInfo);
|
||||
}
|
||||
|
||||
private void Parse(IEnumerable<string> args)
|
||||
{
|
||||
var currentName = "";
|
||||
var values = new List<string>();
|
||||
foreach (var arg in args)
|
||||
{
|
||||
if (arg.StartsWith("-", StringComparison.Ordinal))
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(currentName))
|
||||
Arguments[currentName.ToLowerInvariant()] = values.ToArray();
|
||||
values.Clear();
|
||||
currentName = arg.Substring(1);
|
||||
}
|
||||
else if (string.IsNullOrWhiteSpace(currentName))
|
||||
{
|
||||
Arguments[arg] = new string[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
values.Add(arg);
|
||||
}
|
||||
}
|
||||
|
||||
if (currentName != "")
|
||||
Arguments[currentName.ToLowerInvariant()] = values.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
102
Creator/Helper/Utils.cs
Normal file
102
Creator/Helper/Utils.cs
Normal file
@ -0,0 +1,102 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
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);
|
||||
var versionResource = (VersionResource) ri[Kernel32.ResourceTypes.RT_VERSION].First();
|
||||
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);
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user