From ca562ef22a4212d96e685aa987bdef0851b02c7a Mon Sep 17 00:00:00 2001 From: Rutherther Date: Sun, 30 Jan 2022 13:40:02 +0100 Subject: [PATCH] feat(injector): verify common errors to ease the diagnosis --- .../Errors/InjectionFailedError.cs | 2 +- src/Inject/NosSmooth.Injector/NosInjector.cs | 29 ++++++++++++++++--- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/Inject/NosSmooth.Injector/Errors/InjectionFailedError.cs b/src/Inject/NosSmooth.Injector/Errors/InjectionFailedError.cs index fed27cd..3ea5fc5 100644 --- a/src/Inject/NosSmooth.Injector/Errors/InjectionFailedError.cs +++ b/src/Inject/NosSmooth.Injector/Errors/InjectionFailedError.cs @@ -12,4 +12,4 @@ namespace NosSmooth.Injector.Errors; /// The injection could not be finished successfully. /// /// The path to the dll. -public record InjectionFailedError(string DllPath) : ResultError($"Could not inject {DllPath} dll into the process."); \ No newline at end of file +public record InjectionFailedError(string DllPath, string Reason) : ResultError($"Could not inject {DllPath} dll into the process. {Reason}"); \ No newline at end of file diff --git a/src/Inject/NosSmooth.Injector/NosInjector.cs b/src/Inject/NosSmooth.Injector/NosInjector.cs index 2ee64eb..5d87c59 100644 --- a/src/Inject/NosSmooth.Injector/NosInjector.cs +++ b/src/Inject/NosSmooth.Injector/NosInjector.cs @@ -5,6 +5,7 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.Diagnostics; +using System.Runtime.InteropServices; using System.Security; using System.Text; using Microsoft.Extensions.Options; @@ -79,6 +80,11 @@ public class NosInjector { try { + if (!File.Exists(dllPath)) + { + return new NotFoundError($"Could not find the managed dll file at \"{dllPath}\"."); + } + using var injector = new Reloaded.Injector.Injector(process); var memory = new ExternalMemory(process); @@ -91,6 +97,11 @@ public class NosInjector var runtimePath = Path.Combine (directoryName, Path.GetFileNameWithoutExtension(dllPath)) + ".runtimeconfig.json"; + if (!File.Exists(runtimePath)) + { + return new NotFoundError($"Could not find the runtimeconfig.json file at \"{runtimePath}\"."); + } + using var dllPathMemory = AllocateString(memory, dllPath); using var classPathMemory = AllocateString(memory, classPath); using var methodNameMemory = AllocateString(memory, methodName); @@ -102,7 +113,7 @@ public class NosInjector return new GenericError("Could not allocate memory in the external process."); } - var loadParams = new LoadParams() + var loadParams = new LoadParams { LibraryPath = (int)dllPathMemory.Pointer, MethodName = (int)methodNameMemory.Pointer, @@ -111,16 +122,26 @@ public class NosInjector }; var nosSmoothInjectPath = Path.GetFullPath(_options.NosSmoothInjectPath); + if (!File.Exists(nosSmoothInjectPath)) + { + return new NotFoundError($"Could not find the dll to inject at \"{_options.NosSmoothInjectPath}\"."); + } + var injected = injector.Inject(nosSmoothInjectPath); if (injected == 0) { - return new InjectionFailedError(nosSmoothInjectPath); + return new InjectionFailedError + (nosSmoothInjectPath, "Did you forget to copy nethost.dll into the process directory?"); } var functionResult = injector.CallFunction(nosSmoothInjectPath, "LoadAndCallMethod", loadParams); - if (functionResult != 0) + if (functionResult != 1) { - return new InjectionFailedError(dllPath); + return new InjectionFailedError + ( + dllPath, + $"Couldn't initialize the nethost or call the main function, did you specify the class and method correctly? Result: {functionResult}" + ); } return Result.FromSuccess(); -- 2.48.1