-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c17a6e
commit 9eeef4d
Showing
7 changed files
with
141 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
using System; | ||
using System.IO; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace SRL | ||
{ | ||
static partial class StringReloader | ||
{ | ||
static void InstallCreateFileHooks() | ||
{ | ||
dGetFileAttrA = new GetFileAttributesADelegate(GetFileAttributes); | ||
dGetFileAttrW = new GetFileAttributesWDelegate(GetFileAttributes); | ||
dGetFileAttrExA = new GetFileAttributesExADelegate(GetFileAttributesEx); | ||
dGetFileAttrExW = new GetFileAttributesExWDelegate(GetFileAttributesEx); | ||
dCreateFileA = new CreateFileADelegate(CreateFile); | ||
dCreateFileW = new CreateFileWDelegate(CreateFile); | ||
|
||
hCreateFileA = new FxHook("kernel32.dll", "CreateFileA", dCreateFileA); | ||
hCreateFileW = new FxHook("kernel32.dll", "CreateFileW", dCreateFileW); | ||
hGetFileAttrA = new FxHook("kernel32.dll", "GetFileAttributesA", dGetFileAttrA); | ||
hGetFileAttrW = new FxHook("kernel32.dll", "GetFileAttributesW", dGetFileAttrW); | ||
hGetFileAttrExA = new FxHook("kernel32.dll", "GetFileAttributesExA", dGetFileAttrExA); | ||
hGetFileAttrExW = new FxHook("kernel32.dll", "GetFileAttributesExW", dGetFileAttrExW); | ||
|
||
hCreateFileA.Install(); | ||
hCreateFileW.Install(); | ||
hGetFileAttrA.Install(); | ||
hGetFileAttrW.Install(); | ||
hGetFileAttrExA.Install(); | ||
hGetFileAttrExW.Install(); | ||
|
||
Base = BaseDir.TrimEnd('\\', '/'); | ||
if (Base == AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\', '/')) | ||
Base += "\\Patch"; | ||
|
||
Base += '\\'; | ||
|
||
Log("CreateFile Hook Path: {0}", true, Base); | ||
} | ||
|
||
static bool GetFileAttributesEx(string FileName, IntPtr fInfoLevelId, IntPtr lpFileInformation) | ||
{ | ||
FileName = ParsePath(FileName); | ||
|
||
return GetFileAttributesExW(FileName, fInfoLevelId, lpFileInformation); | ||
} | ||
|
||
static uint GetFileAttributes(string FileName) | ||
{ | ||
FileName = ParsePath(FileName); | ||
|
||
return GetFileAttributesW(FileName); | ||
} | ||
static IntPtr CreateFile(string FileName, IntPtr Access, IntPtr Share, IntPtr Security, IntPtr Mode, IntPtr Flags, IntPtr TemplateFile) | ||
{ | ||
FileName = ParsePath(FileName); | ||
|
||
return CreateFileW(FileName, Access, Share, Security, Mode, Flags, TemplateFile); | ||
} | ||
|
||
static string ParsePath(string Path) | ||
{ | ||
|
||
string PatchPath = Base + System.IO.Path.GetFileName(Path); | ||
|
||
if (ValidPath(PatchPath)) | ||
{ | ||
#if DEBUG | ||
Log("File Path Converted from:\n{0}\nto:\n{1}", true, Path, PatchPath); | ||
#endif | ||
return PatchPath; | ||
} | ||
|
||
return Path; | ||
} | ||
|
||
static string Base; | ||
|
||
static bool ValidPath(string Path) => GetFileAttributesW(Path) != INVALID_FILE_ATTRIBUTES; | ||
|
||
const uint INVALID_FILE_ATTRIBUTES = 0xFFFFFFFF; | ||
|
||
static GetFileAttributesADelegate dGetFileAttrA; | ||
static GetFileAttributesWDelegate dGetFileAttrW; | ||
static GetFileAttributesExADelegate dGetFileAttrExA; | ||
static GetFileAttributesExWDelegate dGetFileAttrExW; | ||
static CreateFileADelegate dCreateFileA; | ||
static CreateFileWDelegate dCreateFileW; | ||
|
||
static FxHook hGetFileAttrA; | ||
static FxHook hGetFileAttrW; | ||
static FxHook hGetFileAttrExA; | ||
static FxHook hGetFileAttrExW; | ||
static FxHook hCreateFileA; | ||
static FxHook hCreateFileW; | ||
|
||
[UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet = CharSet.Ansi, SetLastError = true)] | ||
delegate uint GetFileAttributesADelegate([MarshalAs(UnmanagedType.LPStr)] string Filepath); | ||
|
||
[UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet = CharSet.Unicode, SetLastError = true)] | ||
delegate uint GetFileAttributesWDelegate([MarshalAs(UnmanagedType.LPWStr)] string Filepath); | ||
|
||
[UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet = CharSet.Ansi, SetLastError = true)] | ||
delegate bool GetFileAttributesExADelegate([MarshalAs(UnmanagedType.LPStr)] string Filepath, IntPtr fInfoLevelId, IntPtr lpFileInformation); | ||
|
||
[UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet = CharSet.Unicode, SetLastError = true)] | ||
delegate bool GetFileAttributesExWDelegate([MarshalAs(UnmanagedType.LPWStr)] string Filepath, IntPtr fInfoLevelId, IntPtr lpFileInformation); | ||
|
||
[UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Ansi, SetLastError = true)] | ||
delegate IntPtr CreateFileADelegate([MarshalAs(UnmanagedType.LPStr)] string filename, IntPtr Access, IntPtr Share, IntPtr Security, IntPtr Mode, IntPtr Flags, IntPtr templateFile); | ||
|
||
[UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode, SetLastError = true)] | ||
delegate IntPtr CreateFileWDelegate([MarshalAs(UnmanagedType.LPWStr)] string filename, IntPtr Access, IntPtr Share, IntPtr Security, IntPtr Mode, IntPtr Flags, IntPtr templateFile); | ||
|
||
|
||
[DllImport("kernelbase.dll", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Unicode, SetLastError = true)] | ||
static extern bool GetFileAttributesExW(string lpFileName, IntPtr fInfoLevelId, IntPtr lpFileInformation); | ||
|
||
[DllImport("kernelbase.dll", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Unicode, SetLastError = true)] | ||
static extern uint GetFileAttributesW(string lpFileName); | ||
|
||
[DllImport("kernelbase.dll", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Unicode, SetLastError = true)] | ||
static extern IntPtr CreateFileW(string FileName, IntPtr Access, IntPtr Share, IntPtr Security, IntPtr Mode, IntPtr Flags, IntPtr templateFile); | ||
|
||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters