Initial commit
This commit is contained in:
93
Creator/AssemblyFileInfo.cs
Normal file
93
Creator/AssemblyFileInfo.cs
Normal file
@ -0,0 +1,93 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Katteker;
|
||||
using TsudaKageyu;
|
||||
using Vestris.ResourceLib;
|
||||
|
||||
namespace KattekerCreator
|
||||
{
|
||||
public class AssemblyFileInfo
|
||||
{
|
||||
private readonly string _company;
|
||||
private readonly string _copyright;
|
||||
private readonly string _description;
|
||||
private readonly string _productName;
|
||||
|
||||
public AssemblyFileInfo(string fileName, string tempDir)
|
||||
{
|
||||
FileInfo = new FileInfo(fileName);
|
||||
using (var resourceInfo = new ResourceInfo())
|
||||
{
|
||||
resourceInfo.Load(fileName);
|
||||
var versionResource = (VersionResource) resourceInfo[Kernel32.ResourceTypes.RT_VERSION].First();
|
||||
var stringFileInfo = ((StringFileInfo) versionResource[nameof(StringFileInfo)]).Strings.FirstOrDefault()
|
||||
.Value;
|
||||
AssemblyIconPath = GetAssemblyIcon(fileName, tempDir);
|
||||
if (stringFileInfo.Strings.ContainsKey("CompanyName"))
|
||||
_company = stringFileInfo.Strings["CompanyName"].StringValue;
|
||||
if (stringFileInfo.Strings.ContainsKey("FileDescription"))
|
||||
_description = stringFileInfo.Strings["FileDescription"].StringValue;
|
||||
if (stringFileInfo.Strings.ContainsKey("LegalCopyright"))
|
||||
_copyright = stringFileInfo.Strings["LegalCopyright"].StringValue;
|
||||
if (stringFileInfo.Strings.ContainsKey("ProductName"))
|
||||
_productName = stringFileInfo.Strings["ProductName"].StringValue;
|
||||
if (!stringFileInfo.Strings.ContainsKey("ProductVersion")) return;
|
||||
AssemblyVersion = GetSemanticVersion(stringFileInfo.Strings["ProductVersion"].StringValue);
|
||||
}
|
||||
}
|
||||
|
||||
public string AssemblyIconPath { get; }
|
||||
|
||||
public SemanticVersion AssemblyVersion { get; private set; }
|
||||
|
||||
public string Company => _company ?? string.Empty;
|
||||
|
||||
public string Copyright => _copyright ?? string.Empty;
|
||||
|
||||
public string Description => _description ?? string.Empty;
|
||||
|
||||
public FileInfo FileInfo { get; }
|
||||
|
||||
public string ProductName => _productName ?? Description;
|
||||
|
||||
private static string GetAssemblyIcon(string programFile, string tempDir)
|
||||
{
|
||||
var applicationIcon = Path.Combine(tempDir, "application.ico");
|
||||
if (string.IsNullOrWhiteSpace(programFile)) throw new ApplicationException("Program not set.");
|
||||
if (!File.Exists(programFile)) throw new ApplicationException("Program not found.");
|
||||
if (File.Exists(applicationIcon)) File.Delete(applicationIcon);
|
||||
using (var fileStream = new FileStream(applicationIcon, FileMode.CreateNew))
|
||||
{
|
||||
try
|
||||
{
|
||||
var ie = new IconExtractor(programFile);
|
||||
using (var icon = ie.GetIcon(0))
|
||||
{
|
||||
icon?.Save(fileStream);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
using (var icon = Icon.ExtractAssociatedIcon(programFile))
|
||||
{
|
||||
icon?.Save(fileStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return applicationIcon;
|
||||
}
|
||||
|
||||
private static SemanticVersion GetSemanticVersion(string productVersion)
|
||||
{
|
||||
if (!SemanticVersion.TryParse(productVersion, out var semanticVersion))
|
||||
{
|
||||
Version.TryParse(productVersion, out var version);
|
||||
return SemanticVersion.Parse(version.ToString(3));
|
||||
}
|
||||
return semanticVersion?.Change(build: string.Empty);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user