From 018bbde779db72c1d3e21d57d80ccd7b4f512f41 Mon Sep 17 00:00:00 2001 From: Sewer56 Date: Sat, 1 Jun 2019 05:44:57 +0100 Subject: [PATCH] Support loading FASM and FASM64 from directory of Reloaded.Injector DLL --- Source/Reloaded.Assembler/Assembler.cs | 57 +++++++++++++++---- .../Reloaded.Assembler.csproj | 2 +- 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/Source/Reloaded.Assembler/Assembler.cs b/Source/Reloaded.Assembler/Assembler.cs index 72fb137..c03bb48 100644 --- a/Source/Reloaded.Assembler/Assembler.cs +++ b/Source/Reloaded.Assembler/Assembler.cs @@ -22,6 +22,7 @@ You should have received a copy of the GNU General Public License using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.Reflection; using System.Runtime.InteropServices; using System.Text; using System.Threading; @@ -76,17 +77,13 @@ public Assembler(int textSize = 0x10000, int resultSize = 0x8000) IntPtr fasmDllHandle; - // Set functions - if (IntPtr.Size == 4) - fasmDllHandle = LoadLibraryW("FASM.dll"); - else if (IntPtr.Size == 8) - fasmDllHandle = LoadLibraryW("FASMX64.dll"); - else - { - // Does not actually check OS or architecture but it should be good enough for our purposes. - // Users should know that this is a Windows lib for x86/x64 - throw new FasmWrapperException("Only 32bit and 64bit desktop architectures are supported (X86 and X86_64)."); - } + // Get path of FASM dll + string fasmDllPath = GetFasmDLLPath(); + fasmDllHandle = LoadLibraryW(fasmDllPath); + + // Throw exception if dll not loaded. + if (fasmDllHandle == null) + throw new FasmWrapperException("Failed to load FASM dll. The FASM dll pointer from LoadLibraryW is null."); // Obtain delegates to FASM functions. IntPtr assembleAddress = GetProcAddress(fasmDllHandle, "fasm_Assemble"); @@ -192,6 +189,44 @@ public byte[] Assemble(string mnemonics, ushort passLimit = 100) } } + /// + /// Retrieves the path of the FASM dll to load. + /// + private string GetFasmDLLPath() + { + const string FASM86DLL = "FASM.dll"; + const string FASM64DLL = "FASMX64.dll"; + + // Check current directory. + if (IntPtr.Size == 4 && File.Exists(FASM86DLL)) + return FASM86DLL; + else if (IntPtr.Size == 8 && File.Exists(FASM64DLL)) + return FASM64DLL; + + // Check DLL Directory + string assemblyDirectory = GetExecutingDLLDirectory(); + string asmDirectoryFasm86 = Path.Combine(assemblyDirectory, FASM86DLL); + string asmDirectoryFasm64 = Path.Combine(assemblyDirectory, FASM64DLL); + + if (IntPtr.Size == 4 && File.Exists(asmDirectoryFasm86)) + return asmDirectoryFasm86; + else if (IntPtr.Size == 8 && File.Exists(asmDirectoryFasm64)) + return asmDirectoryFasm64; + + throw new FasmWrapperException("Appropriate FASM DLL for X86/64 has not been found in either current or library directory."); + } + + /// + /// Gets the directory of the currently executing assembly. + /// + private string GetExecutingDLLDirectory() + { + string codeBase = Assembly.GetExecutingAssembly().CodeBase; + UriBuilder uri = new UriBuilder(codeBase); + string path = Uri.UnescapeDataString(uri.Path); + return Path.GetDirectoryName(path); + } + /// /// Attempts to allocate the memory to store the text to be supplied to FASM assembler. /// diff --git a/Source/Reloaded.Assembler/Reloaded.Assembler.csproj b/Source/Reloaded.Assembler/Reloaded.Assembler.csproj index 40710d6..3df8eff 100644 --- a/Source/Reloaded.Assembler/Reloaded.Assembler.csproj +++ b/Source/Reloaded.Assembler/Reloaded.Assembler.csproj @@ -21,7 +21,7 @@ https://avatars1.githubusercontent.com/u/45473408?s=400&u=b591dd9f053703e87a08ccc56287a9119e9758cb&v=4 git https://github.com/Reloaded-Project/Reloaded.Assembler - 1.0.5 + 1.0.6 LICENSE