Switch-Toolbox/Toolbox/Program.cs

136 lines
4.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Reflection;
namespace Toolbox
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolve;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Toolbox.Library.Runtime.ExecutableDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
string[] args = Environment.GetCommandLineArgs();
List<string> Files = new List<string>();
foreach (var arg in args)
{
if (arg != Application.ExecutablePath)
Files.Add(arg);
}
var domain = AppDomain.CurrentDomain;
domain.AssemblyResolve += LoadAssembly;
Toolbox.Library.Runtime.UseDirectXTexDecoder = TryLoadDirectXTex();
MainForm form = new MainForm();
form.openedFiles = Files;
Application.Run(form);
}
private static bool TryLoadDirectXTex()
{
try
{
String folder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
String filePath = Path.Combine(folder, Environment.Is64BitProcess ? "x64" : "x86", "DirectXTexNetImpl.dll");
String filePathLib = Path.Combine(folder, "Lib", Environment.Is64BitProcess ? "x64" : "x86", "DirectXTexNetImpl.dll");
if (File.Exists(filePath))
{
Assembly assembly = Assembly.LoadFile(filePath);
return true;
}
if (File.Exists(filePathLib))
{
Assembly assembly = Assembly.LoadFile(filePathLib);
return true;
}
}
catch
{
}
return false;
}
///
/// Include externals dlls
///
private static Assembly LoadAssembly(object sender, ResolveEventArgs args)
{
Assembly result = null;
if (args != null && !string.IsNullOrEmpty(args.Name))
{
//Get current exe fullpath
FileInfo info = new FileInfo(Assembly.GetExecutingAssembly().Location);
//Get folder of the executing .exe
var folderPath = Path.Combine(info.Directory.FullName, "Lib");
//Build potential fullpath to the loading assembly
var assemblyName = args.Name.Split(new string[] { "," }, StringSplitOptions.None)[0];
var assemblyExtension = "dll";
var assemblyPath = Path.Combine(folderPath, string.Format("{0}.{1}", assemblyName, assemblyExtension));
//Check if the assembly exists in our "Libs" directory
if (File.Exists(assemblyPath))
{
Console.WriteLine("Loading .dll " + assemblyPath);
//Load the required assembly using our custom path
result = Assembly.LoadFrom(assemblyPath);
}
else
{
//Keep default loading
return args.RequestingAssembly;
}
}
return result;
}
// Custom assembly resolver to find the architecture-specific implementation assembly.
private static Assembly AssemblyResolve(object sender, ResolveEventArgs args)
{
if (args.Name.StartsWith("DirectXTexNetImpl", StringComparison.OrdinalIgnoreCase))
{
var assembly = Assembly.GetExecutingAssembly();
var path = GetAssemblyPath(Path.GetDirectoryName(assembly.Location));
if (!File.Exists(path))
{
// If we can't find the file, try using the CodeBase instead (which can
// be different if using shadow copy).
// CodeBase is a uri, so must parse out the filename.
path = GetAssemblyPath(Path.GetDirectoryName(new Uri(assembly.CodeBase).AbsolutePath));
}
return Assembly.LoadFile(path);
}
return null;
}
private static string GetAssemblyPath(string dir) => Path.Combine(dir,"Lib", ArchitectureMoniker, "DirectXTexNetImpl.dll");
// Note: currently no support for ARM.
// Don't use %PROCESSOR_ARCHITECTURE% as it calls x64 'AMD64'.
private static string ArchitectureMoniker => Environment.Is64BitProcess ? "x64" : "x86";
}
}