Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.64 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel;
  3. using System.Diagnostics;
  4. using System.Runtime.CompilerServices;
  5. using System.Windows;
  6. using JustFOV.Annotations;
  7. using JustFOV.Util;
  8. using System.Windows.Input;
  9.  
  10. namespace JustFOV
  11. {
  12. public class Model : INotifyPropertyChanged
  13. {
  14. private const float DegToRad = (float) (Math.PI/180.0f);
  15. private const float RadToDeg = (float) (180.0f/Math.PI);
  16. private readonly IntPtr _handle;
  17.  
  18. private readonly byte[] _originalCallBytes;
  19.  
  20. private ICommand _restoreDefaultFOVCommand;
  21. private float _defaultFOV = 34.89f;
  22.  
  23. private bool _fovHackEnabled;
  24. private float _fovRecall;
  25.  
  26. public Model()
  27. {
  28. var processes = Process.GetProcessesByName("PlayJCMP"); // For MP Player ;)
  29. if (processes.Length == 0)
  30. {
  31. MessageBox.Show("Failed to find JustCause3.exe process", "Error", MessageBoxButton.OK,
  32. MessageBoxImage.Error);
  33. Application.Current.Shutdown(0);
  34. }
  35.  
  36. _handle = Natives.OpenProcess(
  37. (uint) Natives.ProcessAccessFlags.VMOperation |
  38. (uint) Natives.ProcessAccessFlags.VMRead |
  39. (uint) Natives.ProcessAccessFlags.VMWrite, false, processes[0].Id);
  40.  
  41. if (_handle == IntPtr.Zero)
  42. {
  43. MessageBox.Show("Failed to open a handle to JustCause3.exe process", "Error", MessageBoxButton.OK,
  44. MessageBoxImage.Error);
  45. Application.Current.Shutdown(0);
  46. }
  47.  
  48. _originalCallBytes = Natives.ReadBytes(_handle, _setFovCall, 5);
  49.  
  50. PatchSetFov(true);
  51. }
  52.  
  53. #region Properties
  54.  
  55. public float Fov
  56. {
  57. get { return GetFov()*RadToDeg; }
  58. set
  59. {
  60. PatchFov(value*DegToRad);
  61.  
  62. OnPropertyChanged();
  63. }
  64. }
  65.  
  66. public ICommand RestoreDefaultFOV
  67. {
  68. get
  69. {
  70. return _restoreDefaultFOVCommand ??
  71. (_restoreDefaultFOVCommand = new RelayCommand(p => Fov = _defaultFOV));
  72. }
  73. }
  74.  
  75. public bool FovHackEnabled
  76. {
  77. get
  78. {
  79. return _fovHackEnabled;
  80. }
  81. }
  82.  
  83. #endregion
  84.  
  85. ~Model()
  86. {
  87. PatchSetFov(false);
  88. }
  89.  
  90. public void PatchSetFov(bool overrideEnable)
  91. {
  92. _fovHackEnabled = overrideEnable;
  93.  
  94. Natives.WriteBytes(_handle, _setFovCall,
  95. overrideEnable ? new byte[] {0x90, 0x90, 0x90, 0x90, 0x90} : _originalCallBytes);
  96. }
  97.  
  98. public void RecallFov()
  99. {
  100. PatchFov(_fovRecall);
  101. }
  102.  
  103. private void PatchFov(float newFov)
  104. {
  105. _fovRecall = newFov;
  106.  
  107. var cameraManager = Natives.ReadIntPtr(_handle, _cameraManagerPtr);
  108. var currentCamera = Natives.ReadIntPtr(_handle, cameraManager + CurrentCameraOffset);
  109.  
  110. // Update the flags to indicate an FOV change has occurred
  111. var flags = Natives.ReadBytes(_handle, currentCamera + CameraFlagsOffset, 1);
  112. flags[0] |= 0x10;
  113. Natives.WriteBytes(_handle, currentCamera + CameraFlagsOffset, flags);
  114.  
  115. // Update the actual FOV values
  116. Natives.WriteFloat(_handle, currentCamera + FovOffset1, newFov);
  117. Natives.WriteFloat(_handle, currentCamera + FovOffset2, newFov);
  118. }
  119.  
  120. private float GetFov()
  121. {
  122. var cameraManager = Natives.ReadIntPtr(_handle, _cameraManagerPtr);
  123. var currentCamera = Natives.ReadIntPtr(_handle, cameraManager + CurrentCameraOffset);
  124.  
  125. return Natives.ReadFloat(_handle, currentCamera + FovOffset2);
  126. }
  127.  
  128. #region Offsets
  129.  
  130. // Patch 1.021
  131. //private readonly IntPtr _cameraManagerPtr = new IntPtr(0x142e72a58);
  132. //private readonly IntPtr _setFovCall = new IntPtr(0x143a546cf);
  133.  
  134. //private const int CurrentCameraOffset = 0x5c0;
  135. //private const int CameraFlagsOffset = 0x55e;
  136. //private const int FovOffset1 = 0x580;
  137. //private const int FovOffset2 = 0x584;
  138.  
  139. // Patch 20/01/2016
  140. //private readonly IntPtr _cameraManagerPtr = new IntPtr(0x142e72a58);
  141. //private readonly IntPtr _setFovCall = new IntPtr(0x143229d70);
  142.  
  143. //private const int CurrentCameraOffset = 0x5c0;
  144. //private const int CameraFlagsOffset = 0x55e;
  145. //private const int FovOffset1 = 0x580;
  146. //private const int FovOffset2 = 0x584;
  147.  
  148. // Patch 07/03/2016
  149. //private readonly IntPtr _cameraManagerPtr = new IntPtr(0x142F0CB58);
  150. //private readonly IntPtr _setFovCall = new IntPtr(0x143B3BC2D);
  151.  
  152. //private const int CurrentCameraOffset = 0x5c0;
  153. //private const int CameraFlagsOffset = 0x55e;
  154. //private const int FovOffset1 = 0x580;
  155. //private const int FovOffset2 = 0x584;
  156.  
  157. // Patch version 1.04 (03/06/2016)
  158. // NOTE(xforce): If I were to inject something in the process I could make this work independent of version
  159. // But I don't think that was the intention of this program
  160. // Or I write some memory search using ReadProcessMemory but that is slow
  161. // So on the next update use this to find the address to CameraManager easily
  162. // Or use a dump in IDA look for the ctor of CCameraManager
  163. // 48 8B 05 ? ? ? ? F3 0F 10 05 ? ? ? ? 4D 89 C1 48 8B 90 ? ? ? ? 4C 8D 41 0C 31 C0
  164. // This is a IDA compatible pattern
  165.  
  166. // This is the pattern for the SetFOV Call
  167. // E8 ? ? ? ? 0F 28 BC 24 ? ? ? ? 0F 28 B4 24 ? ? ? ? 48 8B 9C 24 ? ? ? ? 48 81 C4 ? ? ? ?
  168. // I however thing it's not required to patch it as this is only called
  169. // when the camera manager gets created
  170. // Or maybe I just have the wrong call
  171. // But I was not able to acquire the versions used previously, so ye
  172.  
  173. private readonly IntPtr _cameraManagerPtr = new IntPtr(0x142EBEBD0);
  174. private readonly IntPtr _setFovCall = new IntPtr(0x143ADAD71);
  175.  
  176. private const int CurrentCameraOffset = 0x5c0;
  177. private const int CameraFlagsOffset = 0x55e;
  178. private const int FovOffset1 = 0x580;
  179. private const int FovOffset2 = 0x584;
  180.  
  181. #endregion
  182.  
  183. #region INotifyPropertyChanged
  184.  
  185. public event PropertyChangedEventHandler PropertyChanged;
  186.  
  187. [NotifyPropertyChangedInvocator]
  188. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  189. {
  190. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  191. }
  192.  
  193. #endregion
  194. }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement