Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.35 KB | None | 0 0
  1. /// <summary>
  2. /// Holds the game process for us to manipulate.
  3. /// Allows you to read/write memory, perform pattern scans, etc.
  4. /// See libReloaded/GameProcess (folder)
  5. /// </summary>
  6. public static ReloadedProcess GameProcess;
  7.  
  8. /// <summary>
  9. /// Specifies the full directory location that the game
  10. /// is contained in.
  11. /// </summary>
  12. public static string GameDirectory;
  13.  
  14. /// <summary>
  15. /// Specifies the full directory location that the current mod
  16. /// is contained in.
  17. /// </summary>
  18. public static string ModDirectory;
  19.  
  20. public const string RESOURCE_PATH_ROOT = @"resource\gd_pc\";
  21.  
  22. /// <summary>
  23. /// Function delegate for CreateFileA, the function the game uses to obtain handles to files it will load.
  24. /// </summary>
  25. /// <param name="filename"></param>
  26. /// <param name="access"></param>
  27. /// <param name="share"></param>
  28. /// <param name="securityAttributes"></param>
  29. /// <param name="creationDisposition"></param>
  30. /// <param name="flagsAndAttributes"></param>
  31. /// <param name="templateFile"></param>
  32. /// <returns></returns>
  33. [UnmanagedFunctionPointer( CallingConvention.Cdecl )]
  34. [ReloadedFunction( CallingConventions.Stdcall )]
  35. public delegate IntPtr CreateFileA
  36. (
  37.     [MarshalAs( UnmanagedType.LPStr )] string filename,
  38.     [MarshalAs( UnmanagedType.U4 )] FileAccess access,
  39.     [MarshalAs( UnmanagedType.U4 )] FileShare share,
  40.     IntPtr securityAttributes,
  41.     [MarshalAs( UnmanagedType.U4 )] FileMode creationDisposition,
  42.     [MarshalAs( UnmanagedType.U4 )] FileAttributes flagsAndAttributes,
  43.     IntPtr templateFile
  44. );
  45.  
  46. public static FunctionHook<CreateFileA> CreateFileAHook;
  47.  
  48. /// <summary>
  49. /// Your own user code starts here.
  50. /// If this is your first time, do consider reading the notice above.
  51. /// It contains some very useful information.
  52. /// </summary>
  53. public static void Init()
  54. {
  55. #if DEBUG
  56.     Print("SA2CustomFileLoader [DEBUG] initialized!");
  57.     Debugger.Launch();
  58. #else
  59.     Print( "SA2CustomFileLoader initialized!" );
  60. #endif
  61.  
  62.     // Initialize CreateFileA hook
  63.     var kernel32Handle = Native.LoadLibrary( "kernel32" );
  64.     var createFileA = Native.GetProcAddress( kernel32Handle, "CreateFileA" );
  65.     CreateFileAHook = FunctionHook<CreateFileA>.CreateFunctionHook( ( long )createFileA, CreateFileAHookImpl );
  66. }
  67.  
  68. public static IntPtr CreateFileAHookImpl( string filename, FileAccess fileAccess, FileShare fileShare, IntPtr securityAttributes,
  69.                                             FileMode creationDisposition, FileAttributes flagsAndAttributes, IntPtr templateFile )
  70. {
  71.     if ( TryGetRelativePath( filename, RESOURCE_PATH_ROOT, out var relativePath ) )
  72.     {
  73.         var customFilePath = Path.Combine( ModDirectory, relativePath );
  74.         if ( File.Exists( customFilePath ) )
  75.         {
  76.             filename = customFilePath;
  77.         }
  78.     }
  79.  
  80.     return CreateFileAHook.OriginalFunction( filename, fileAccess, fileShare, securityAttributes, creationDisposition, flagsAndAttributes,
  81.                                                 templateFile );
  82. }
  83.  
  84. public static bool TryGetRelativePath( string path, string pathRoot, out string relativePath )
  85. {
  86.     var normalizedPath = path.ToLowerInvariant();
  87.     var index = normalizedPath.IndexOf( pathRoot );
  88.     if ( index != -1 )
  89.     {
  90.         relativePath = normalizedPath.Substring( index );
  91.         return true;
  92.     }
  93.  
  94.     relativePath = null;
  95.     return false;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement