From 44ef5e0321b5365276743e5a4ff1d49ab2b539c0 Mon Sep 17 00:00:00 2001 From: "Chris Weermann (TGE)" Date: Fri, 3 Jul 2020 10:48:37 +0200 Subject: [PATCH] [General] fix crash with denuvo-less exe --- modloader/Hooking/ImportAddressTableHooker.cs | 76 +++++++++---------- modloader/ModConfig.json | 2 +- 2 files changed, 38 insertions(+), 40 deletions(-) diff --git a/modloader/Hooking/ImportAddressTableHooker.cs b/modloader/Hooking/ImportAddressTableHooker.cs index 45dacc0..6705f6d 100644 --- a/modloader/Hooking/ImportAddressTableHooker.cs +++ b/modloader/Hooking/ImportAddressTableHooker.cs @@ -1,22 +1,18 @@ using System; -using System.Collections.Generic; -using System.Diagnostics; +using System.IO; using System.Runtime.InteropServices; -using System.Text; -using Reloaded.Hooks; using Reloaded.Hooks.Definitions; -using Reloaded.Hooks.Internal; -using Reloaded.Hooks.Tools; using Reloaded.Memory.Sources; namespace modloader.Hooking { - public static unsafe class ImportAddressTableHooker - { - public static IHook Hook( IReloadedHooks hookFactory, - string libraryName, string functionName, TFunction function) + public static unsafe class ImportAddressTableHooker + { + public static IHook Hook( IReloadedHooks hookFactory, + string libraryName, string functionName, TFunction function ) { const int IMAGE_DIRECTORY_ENTRY_IMPORT = 1; + var cleanLibraryName = Path.GetFileNameWithoutExtension(libraryName); var imageBase = GetModuleHandle(null); var dosHeaders = (IMAGE_DOS_HEADERS*)imageBase; @@ -25,10 +21,12 @@ public static IHook Hook( IReloadedHooks hookFactory, var importsDirectory = ntHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]; var importDescriptor = (IMAGE_IMPORT_DESCRIPTOR*)(importsDirectory.VirtualAddress + (uint)imageBase); - while (importDescriptor->Name != 0) + while ( importDescriptor->Name != 0 ) { var importLibraryName = new string((sbyte*)((long)imageBase + importDescriptor->Name)); - if ( importLibraryName == libraryName ) + var cleanImportLibraryName = Path.GetFileNameWithoutExtension(importLibraryName); + + if ( cleanImportLibraryName.Equals( cleanLibraryName, StringComparison.OrdinalIgnoreCase ) ) { var importLibrary = LoadLibrary(importLibraryName); @@ -70,7 +68,7 @@ private class IndirectHook : IHook public IntPtr OriginalFunctionWrapperAddress { get; } public IReverseWrapper ReverseWrapper { get; } - public IndirectHook(IntPtr addressToFunctionPointer, TFunction function) + public IndirectHook( IntPtr addressToFunctionPointer, TFunction function ) { mAddressToFunctionPointer = addressToFunctionPointer; OriginalFunctionAddress = *( IntPtr* )mAddressToFunctionPointer; @@ -114,7 +112,7 @@ public string Name get { fixed ( byte* pNameBytes = NameBytes ) - return new string( (sbyte*)pNameBytes ); + return new string( ( sbyte* )pNameBytes ); } } } @@ -223,32 +221,32 @@ private struct IMAGE_NT_HEADERS32 [StructLayout( LayoutKind.Sequential, Pack = 1 )] private unsafe struct IMAGE_DOS_HEADERS - { - public fixed byte e_magic_byte[2]; // Magic number - public UInt16 e_cblp; // Bytes on last page of file - public UInt16 e_cp; // Pages in file - public UInt16 e_crlc; // Relocations - public UInt16 e_cparhdr; // Size of header in paragraphs - public UInt16 e_minalloc; // Minimum extra paragraphs needed - public UInt16 e_maxalloc; // Maximum extra paragraphs needed - public UInt16 e_ss; // Initial (relative) SS value - public UInt16 e_sp; // Initial SP value - public UInt16 e_csum; // Checksum - public UInt16 e_ip; // Initial IP value - public UInt16 e_cs; // Initial (relative) CS value - public UInt16 e_lfarlc; // File address of relocation table - public UInt16 e_ovno; // Overlay number - public fixed UInt16 e_res1[4]; // Reserved words - public UInt16 e_oemid; // OEM identifier (for e_oeminfo) - public UInt16 e_oeminfo; // OEM information; e_oemid specific - public fixed UInt16 e_res2[10]; // Reserved words - public Int32 e_lfanew; // File address of new exe header - } - - [DllImport( "kernel32.dll" )] + { + public fixed byte e_magic_byte[2]; // Magic number + public UInt16 e_cblp; // Bytes on last page of file + public UInt16 e_cp; // Pages in file + public UInt16 e_crlc; // Relocations + public UInt16 e_cparhdr; // Size of header in paragraphs + public UInt16 e_minalloc; // Minimum extra paragraphs needed + public UInt16 e_maxalloc; // Maximum extra paragraphs needed + public UInt16 e_ss; // Initial (relative) SS value + public UInt16 e_sp; // Initial SP value + public UInt16 e_csum; // Checksum + public UInt16 e_ip; // Initial IP value + public UInt16 e_cs; // Initial (relative) CS value + public UInt16 e_lfarlc; // File address of relocation table + public UInt16 e_ovno; // Overlay number + public fixed UInt16 e_res1[4]; // Reserved words + public UInt16 e_oemid; // OEM identifier (for e_oeminfo) + public UInt16 e_oeminfo; // OEM information; e_oemid specific + public fixed UInt16 e_res2[10]; // Reserved words + public Int32 e_lfanew; // File address of new exe header + } + + [DllImport( "kernel32.dll" )] private static extern IntPtr GetModuleHandle( string lpModuleName ); - [DllImport( "kernel32", SetLastError = true, CharSet = CharSet.Ansi )] - private static extern IntPtr LoadLibrary( [MarshalAs( UnmanagedType.LPStr )]string lpFileName ); + [DllImport( "kernel32", SetLastError = true, CharSet = CharSet.Ansi )] + private static extern IntPtr LoadLibrary( [MarshalAs( UnmanagedType.LPStr )] string lpFileName ); } } diff --git a/modloader/ModConfig.json b/modloader/ModConfig.json index fd8e1a3..94c3ae6 100644 --- a/modloader/ModConfig.json +++ b/modloader/ModConfig.json @@ -2,7 +2,7 @@ "ModId": "p4gpc.modloader", "ModName": "Persona 4 Golden PC Mod Loader", "ModAuthor": "TGE, Sewer56", - "ModVersion": "1.2.0a", + "ModVersion": "1.1.1", "ModDescription": "A mod loader for Persona 4 Golden (Steam)", "ModDll": "modloader.dll", "ModIcon": "Preview.png",