/// <summary> /// Create a HarmonyLibrary from the given stream, which should contain the bytes of /// an unmanaged EXE or an unmanaged DLL. /// </summary> /// <param name="rawDll">The stream that contains the EXE or DLL to load.</param> /// <param name="name">(optional) The name of the library we're loading (to be used when resolving other libraries).</param> /// <param name="loadFlags">(optional) Flags to control how this loading is performed.</param> /// <param name="otherLibraries">(optional) Any other HarmonyLibraries to use when resolving this library's imports.</param> /// <returns>A HarmonyLibrary instance.</returns> /// <throws cref="LoadFailedException">Thrown when the library cannot be loaded (if, for example, it is corrupt /// data, or if one of its imports cannot be resolved). The exception message will provide details to explain /// why the library-load failed.</throws> public static HarmonyLibrary CreateFromStream(Stream rawDll, string name = null, HarmonyLoadFlags loadFlags = 0, IEnumerable <HarmonyLibrary> otherLibraries = null) { StreamCollector collector = new StreamCollector(); collector.Read(rawDll); return(CreateFromBytes(collector.Bytes, 0, (uint)collector.Length, name, loadFlags, otherLibraries)); }
/// <summary> /// Create a HarmonyLibrary from a subset of the given byte array, which should contain the bytes of /// an unmanaged EXE or an unmanaged DLL. /// </summary> /// <param name="rawDll">An array of bytes that contains the EXE or DLL to load.</param> /// <param name="offset">Where within the raw byte array the actual EXE or DLL starts.</param> /// <param name="length">The length of the EXE or DLL bytes starting at the given offset in the array.</param> /// <param name="name">(optional) The name of the library we're loading (to be used when resolving other libraries).</param> /// <param name="loadFlags">(optional) Flags to control how this loading is performed.</param> /// <param name="otherLibraries">(optional) Any other HarmonyLibraries to use when resolving this library's imports.</param> /// <returns>A HarmonyLibrary instance.</returns> /// <throws cref="LoadFailedException">Thrown when the library cannot be loaded (if, for example, it is corrupt /// data, or if one of its imports cannot be resolved). The exception message will provide details to explain /// why the library-load failed.</throws> public static HarmonyLibrary CreateFromBytes(byte[] rawDll, uint offset, uint length, string name = null, HarmonyLoadFlags loadFlags = 0, IEnumerable <HarmonyLibrary> otherLibraries = null) { LibraryLoader libraryLoader = new LibraryLoader(loadFlags, otherLibraries); HarmonyLibrary library = libraryLoader.CreateLibrary(rawDll, offset, length, name); return(library); }
public LibraryLoader(HarmonyLoadFlags loadFlags, IEnumerable <HarmonyLibrary> otherLibraries) { otherLibraries = otherLibraries ?? Enumerable.Empty <HarmonyLibrary>(); _loadFlags = loadFlags; _otherLibraries = otherLibraries; _pageSize = GetPageSize(); }
/// <summary> /// The constructor is internal, because the real work is done inside the LibraryLoader class. This /// merely fills in the instance data after the LibraryLoader is done. /// </summary> internal HarmonyLibrary(string name, HarmonyLibraryKind kind, HarmonyLoadFlags loadFlags, UIntPtr baseAddress, uint imageSize, uint alignedImageSize, uint pageSize, IEnumerable <HarmonyLibrarySection> sections, IEnumerable <HarmonyImportLibrary> importLibraries, IDictionary <string, HarmonyExport> exportsByName, IDictionary <ushort, HarmonyExport> exportsByOrdinal) { Name = name; Kind = kind; LoadFlags = loadFlags; BaseAddress = baseAddress; ImageSize = imageSize; AlignedImageSize = alignedImageSize; PageSize = pageSize; Sections = Array.AsReadOnly(sections.ToArray()); ImportLibraries = Array.AsReadOnly(importLibraries.ToArray()); ExportsByName = new ReadOnlyDictionary <string, HarmonyExport>(exportsByName); ExportsByOrdinal = new ReadOnlyDictionary <ushort, HarmonyExport>(exportsByOrdinal); }
/// <summary> /// Create a HarmonyLibrary from the given byte array, which should contain the bytes of /// an unmanaged EXE or an unmanaged DLL. /// </summary> /// <param name="rawDll">The bytes of the EXE or DLL to load.</param> /// <param name="name">(optional) The name of the library we're loading (to be used when resolving other libraries).</param> /// <param name="loadFlags">(optional) Flags to control how this loading is performed.</param> /// <param name="otherLibraries">(optional) Any other HarmonyLibraries to use when resolving this library's imports.</param> /// <returns>A HarmonyLibrary instance.</returns> /// <throws cref="LoadFailedException">Thrown when the library cannot be loaded (if, for example, it is corrupt /// data, or if one of its imports cannot be resolved). The exception message will provide details to explain /// why the library-load failed.</throws> public static HarmonyLibrary CreateFromBytes(byte[] rawDll, string name = null, HarmonyLoadFlags loadFlags = 0, IEnumerable <HarmonyLibrary> otherLibraries = null) { return(CreateFromBytes(rawDll, 0, (uint)rawDll.Length, name, loadFlags, otherLibraries)); }
private static HarmonyImportLibrary LoadImport(byte *basePtr, uint size, IMAGE_IMPORT_DESCRIPTOR *importDescriptor, Dictionary <string, HarmonyLibrary> otherLibraryLookup, HarmonyLoadFlags loadFlags) { string moduleName = StringOperations.NulTerminatedBytesToString(basePtr + importDescriptor->Name, basePtr, size); HarmonyLibrary otherLibrary; if (otherLibraryLookup.TryGetValue(moduleName.ToLowerInvariant(), out otherLibrary)) { return(LoadHarmonyImport(basePtr, size, importDescriptor, otherLibrary)); } if ((loadFlags & HarmonyLoadFlags.PrivateImports) == 0) { return(LoadExternalDllImport(basePtr, size, importDescriptor, moduleName)); } return(null); }