UIXC: Search for files in all included folders

This commit is contained in:
Yoshi Askharoun
2024-07-04 12:51:31 -05:00
parent 6be8a716ef
commit 94519126da
+39 -10
View File
@@ -5,6 +5,7 @@ using Microsoft.Iris.Session;
using Spectre.Console; using Spectre.Console;
using Spectre.Console.Cli; using Spectre.Console.Cli;
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.Loader; using System.Runtime.Loader;
namespace UIXC.Commands; namespace UIXC.Commands;
@@ -42,22 +43,19 @@ public class DecompileCommand : Command<DecompileCommand.Settings>
} }
}; };
var searchPaths = settings.IncludeDirectories.Prepend(Environment.CurrentDirectory).ToArray();
foreach (var givenPath in settings.Assemblies ?? []) foreach (var givenPath in settings.Assemblies ?? [])
{ {
try try
{ {
var assemblyPath = givenPath; var assemblyPath = ResolvePath(givenPath, searchPaths);
if (!Path.IsPathFullyQualified(givenPath))
{
assemblyPath = Path.GetFullPath(givenPath);
}
_ = AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyPath); _ = AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyPath);
} }
catch (Exception ex) catch (Exception ex)
{ {
AnsiConsole.WriteException(ex); AnsiConsole.WriteException(ex);
if (!AnsiConsole.Ask("Do you want to continue?", false)) if (!AnsiConsole.Confirm("Do you want to continue?", false))
return -1; return -1;
} }
} }
@@ -77,7 +75,9 @@ public class DecompileCommand : Command<DecompileCommand.Settings>
{ {
try try
{ {
var uibLoadResult = MarkupSystem.Load($"file://{input}", (uint)Random.Shared.Next()); var inputPath = ResolvePath(input, searchPaths);
var uibLoadResult = MarkupSystem.Load($"file://{inputPath}", (uint)Random.Shared.Next());
uibLoadResult.FullLoad(); uibLoadResult.FullLoad();
if (uibLoadResult.Status != LoadResultStatus.Success) if (uibLoadResult.Status != LoadResultStatus.Success)
@@ -113,14 +113,43 @@ public class DecompileCommand : Command<DecompileCommand.Settings>
else else
{ {
AnsiConsole.MarkupLine("[green]Decompilation finished.[/]"); AnsiConsole.MarkupLine("[green]Decompilation finished.[/]");
AnsiConsole.MarkupLine($"[green]Output saved to '{settings.OutputDir}'.[/]");
return 0; return 0;
} }
} }
private static string GetOutputPath(Settings settings, string inputFilePath) private static string GetOutputPath(Settings settings, string inputFilePath)
{ {
var outputDir = settings.OutputDir ?? Environment.CurrentDirectory; return Path.Combine(settings.OutputDir, Path.ChangeExtension(inputFilePath, settings.Language.GetExtension()));
return Path.Combine(outputDir, Path.ChangeExtension(inputFilePath, settings.Language.GetExtension())); }
private static string ResolvePath(string givenPath, ICollection<string> searchPaths)
{
if (!ResolvePath(givenPath, searchPaths, out var assemblyPath))
throw new FileNotFoundException(null, givenPath);
return assemblyPath;
}
private static bool ResolvePath(string givenPath, ICollection<string> searchPaths, [NotNullWhen(true)] out string? absolutePath)
{
if (Path.IsPathFullyQualified(givenPath))
{
absolutePath = givenPath;
return Path.Exists(absolutePath);
}
absolutePath = null;
if (searchPaths.Count == 0)
return false;
foreach (var searchPath in searchPaths)
{
absolutePath = Path.GetFullPath(givenPath, searchPath);
if (Path.Exists(absolutePath))
return true;
}
return false;
} }
public sealed class Settings : CompilerSettings public sealed class Settings : CompilerSettings