//
// InjectCommand.cs
//
// Copyright (c) František Boháček. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.ComponentModel;
using System.Diagnostics;
using Remora.Commands.Attributes;
using Remora.Commands.Groups;
using Remora.Results;
namespace NosSmooth.Injector.CLI.Commands
{
///
/// Injection command for injecting .NET 5+ libraries with UnmanagedCallersOnly method.
///
internal class InjectCommand : CommandGroup
{
private readonly NosInjector _injector;
///
/// Initializes a new instance of the class.
///
/// The nos smooth injector.
public InjectCommand(NosInjector injector)
{
_injector = injector;
}
///
/// The command to inject.
///
/// The id of the process or part of its name.
/// The path to the dll to inject.
/// The full type specifier. Default is LibraryName.DllMain, LibraryName.
/// The name of the UnmanagedCallersOnly method. Default is Main.
/// A result that may or may not have succeeded.
[Command("inject")]
public Task Inject
(
[Description("The id of the process to inject into.")]
string process,
[Description("The path to the dll to inject.")]
string dllPath,
[Option('t', "type"), Description("The full type specifier. Default is LibraryName.DllMain, LibraryName")]
string? typeName = null,
[Option('m', "method"), Description("The name of the UnmanagedCallersOnly method. Default is Main")]
string? methodName = null
)
{
if (!int.TryParse(process, out var processId))
{
var foundProcess = Process.GetProcesses().FirstOrDefault(x => x.ProcessName.Contains(process, StringComparison.OrdinalIgnoreCase));
if (foundProcess is null)
{
return Task.FromResult(Result.FromError(new NotFoundError("Could not find the given process.")));
}
processId = foundProcess.Id;
}
var dllName = Path.GetFileNameWithoutExtension(dllPath);
return Task.FromResult
(_injector.Inject(processId, dllPath, $"{dllName}.DllMain, {dllName}", methodName ?? "Main"));
}
}
}