Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Holds the game process for us to manipulate.
- /// Allows you to read/write memory, perform pattern scans, etc.
- /// See libReloaded/GameProcess (folder)
- /// </summary>
- public static ReloadedProcess GameProcess;
- /// <summary>
- /// Specifies the full directory location that the game
- /// is contained in.
- /// </summary>
- public static string GameDirectory;
- /// <summary>
- /// Specifies the full directory location that the current mod
- /// is contained in.
- /// </summary>
- public static string ModDirectory;
- public const string RESOURCE_PATH_ROOT = @"resource\gd_pc\";
- /// <summary>
- /// Function delegate for CreateFileA, the function the game uses to obtain handles to files it will load.
- /// </summary>
- /// <param name="filename"></param>
- /// <param name="access"></param>
- /// <param name="share"></param>
- /// <param name="securityAttributes"></param>
- /// <param name="creationDisposition"></param>
- /// <param name="flagsAndAttributes"></param>
- /// <param name="templateFile"></param>
- /// <returns></returns>
- [UnmanagedFunctionPointer( CallingConvention.Cdecl )]
- [ReloadedFunction( CallingConventions.Stdcall )]
- public delegate IntPtr CreateFileA
- (
- [MarshalAs( UnmanagedType.LPStr )] string filename,
- [MarshalAs( UnmanagedType.U4 )] FileAccess access,
- [MarshalAs( UnmanagedType.U4 )] FileShare share,
- IntPtr securityAttributes,
- [MarshalAs( UnmanagedType.U4 )] FileMode creationDisposition,
- [MarshalAs( UnmanagedType.U4 )] FileAttributes flagsAndAttributes,
- IntPtr templateFile
- );
- public static FunctionHook<CreateFileA> CreateFileAHook;
- /// <summary>
- /// Your own user code starts here.
- /// If this is your first time, do consider reading the notice above.
- /// It contains some very useful information.
- /// </summary>
- public static void Init()
- {
- #if DEBUG
- Print("SA2CustomFileLoader [DEBUG] initialized!");
- Debugger.Launch();
- #else
- Print( "SA2CustomFileLoader initialized!" );
- #endif
- // Initialize CreateFileA hook
- var kernel32Handle = Native.LoadLibrary( "kernel32" );
- var createFileA = Native.GetProcAddress( kernel32Handle, "CreateFileA" );
- CreateFileAHook = FunctionHook<CreateFileA>.CreateFunctionHook( ( long )createFileA, CreateFileAHookImpl );
- }
- public static IntPtr CreateFileAHookImpl( string filename, FileAccess fileAccess, FileShare fileShare, IntPtr securityAttributes,
- FileMode creationDisposition, FileAttributes flagsAndAttributes, IntPtr templateFile )
- {
- if ( TryGetRelativePath( filename, RESOURCE_PATH_ROOT, out var relativePath ) )
- {
- var customFilePath = Path.Combine( ModDirectory, relativePath );
- if ( File.Exists( customFilePath ) )
- {
- filename = customFilePath;
- }
- }
- return CreateFileAHook.OriginalFunction( filename, fileAccess, fileShare, securityAttributes, creationDisposition, flagsAndAttributes,
- templateFile );
- }
- public static bool TryGetRelativePath( string path, string pathRoot, out string relativePath )
- {
- var normalizedPath = path.ToLowerInvariant();
- var index = normalizedPath.IndexOf( pathRoot );
- if ( index != -1 )
- {
- relativePath = normalizedPath.Substring( index );
- return true;
- }
- relativePath = null;
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement