Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define OUTPUT
- using Microsoft.Win32;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading;
- using System.Linq;
- using System.Text.RegularExpressions;
- using System.Globalization;
- namespace GTAVNoIntroOTF
- {
- public static class Program
- {
- //1.0.791.2 was old last version - 27.09.2016
- private static bool debug;
- public static void Main(string[] args)
- {
- bool patchintro = string.Join(" ", args).Contains("-dontpatchintro") ? false : true;
- bool patchlegalmessages = string.Join(" ", args).Contains("-dontpatchlegalmessages") ? false : true;
- bool setpriority = string.Join(" ", args).Contains("-dontsetpriority") ? false : true;
- debug = string.Join(" ", args).Contains("-debug") ? true : false;
- if (debug)
- {
- NonStandardConsole.OpenConsole();
- Thread currentthread = Thread.CurrentThread;
- currentthread.CurrentCulture = CultureInfo.InvariantCulture;
- CustomOutput.ConsoleWriteandAddLog("[" + DateTime.Now.ToLongTimeString() + " " + DateTime.Now.ToLongDateString() + "]");
- CustomOutput.ConsoleWriteandAddLog("Welcome to GTA V No Intro On The Fly V 1.1 - by Kryo4lex");
- CustomOutput.ConsoleWriteandAddLog("Passed commandline arguments:");
- CustomOutput.ConsoleWriteandAddLog(string.Join(" ", args));
- CustomOutput.ConsoleWriteandAddLog("Selected options:");
- if (patchintro)
- CustomOutput.ConsoleWriteandAddLog("Disable Intro: YES");
- else
- CustomOutput.ConsoleWriteandAddLog("Disable Intro: NO");
- if (patchlegalmessages)
- CustomOutput.ConsoleWriteandAddLog("Disable Legal Messages: YES");
- else
- CustomOutput.ConsoleWriteandAddLog("Disable Legal Messages: NO");
- if (setpriority)
- CustomOutput.ConsoleWriteandAddLog("Set process priority: YES");
- else
- CustomOutput.ConsoleWriteandAddLog("Set process priority: NO");
- CustomOutput.ConsoleWriteandAddLog("Trying to get game path, version and start game...");
- }
- if (!startrockstarversion())
- {
- if (!startsteamversion())
- {
- if (debug)
- {
- CustomOutput.ConsoleWriteandAddLog("Unable to locate and therefore start GTA V... If game is installed run it manually!");
- }
- }
- }
- GTAVNOIntroPatchOTF.Init(patchintro, patchlegalmessages,setpriority, debug);
- }
- private static bool startrockstarversion()
- {
- try
- {
- if (debug)
- {
- CustomOutput.ConsoleWriteandAddLog("Looking for GTA V Rockstar version...");
- }
- String installfolder = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Rockstar Games\Grand Theft Auto V", "InstallFolder", null).ToString();
- if (installfolder != null)
- {
- if (System.IO.File.Exists(installfolder + @"\" + "GTAVLauncher.exe"))
- {
- if (debug)
- {
- CustomOutput.ConsoleWriteandAddLog("Rockstar version found at: ");
- CustomOutput.ConsoleWriteandAddLog(installfolder);
- FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(installfolder + @"\GTAVLauncher.exe");
- Console.WriteLine("Detected game version:\n" + myFileVersionInfo.FileVersion);
- CustomOutput.ConsoleWriteandAddLog("Starting game...");
- }
- Process.Start(installfolder + @"\GTAVLauncher.exe");
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- catch
- {
- return false;
- }
- return true;
- }
- private static bool startsteamversion()
- {
- try
- {
- if (debug)
- {
- CustomOutput.ConsoleWriteandAddLog("Looking for GTA V Steam version...");
- }
- string steamgtavinstalllocation = GTA_V_Steam_Locator.GTA_V_Folder();
- if (steamgtavinstalllocation != string.Empty)
- {
- if (System.IO.File.Exists(steamgtavinstalllocation + @"\" + "GTAVLauncher.exe"))
- {
- if (debug)
- {
- CustomOutput.ConsoleWriteandAddLog("Steam version found at:");
- CustomOutput.ConsoleWriteandAddLog(steamgtavinstalllocation);
- FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(steamgtavinstalllocation + @"\" + "GTAVLauncher.exe");
- Console.WriteLine("Detected game version:\n" + myFileVersionInfo.FileVersion);
- CustomOutput.ConsoleWriteandAddLog("Starting game...");
- }
- Process.Start("steam://rungameid/271590");
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- catch
- {
- return false;
- }
- return true;
- }
- }
- public static class GTA_V_Steam_Locator
- {
- /// <summary>
- /// Retrieves the Steam main installation folder from the registry
- /// </summary>
- public static string SteamFolder()
- {
- RegistryKey steamKey = Registry.LocalMachine.OpenSubKey("Software\\Valve\\Steam")
- ?? Registry.LocalMachine.OpenSubKey("Software\\Wow6432Node\\Valve\\Steam");
- return steamKey.GetValue("InstallPath").ToString();
- }
- /// <summary>
- /// Retrieves a list of Steam library folders on this computer.
- /// </summary>
- public static List<string> SteamLibraryFolders()
- {
- List<string> folders = new List<string>();
- try
- {
- string steamFolder = SteamFolder();
- folders.Add(steamFolder);
- // the list of additional steam libraries can be found in the config.vdf file
- string configFile = Path.Combine(steamFolder, "config", "config.vdf");
- Regex regex = new Regex("BaseInstallFolder[^\"]*\"\\s*\"([^\"]*)\"");
- using (StreamReader reader = new StreamReader(configFile))
- {
- string line;
- while ((line = reader.ReadLine()) != null)
- {
- Match match = regex.Match(line);
- if (match.Success)
- {
- folders.Add(Regex.Unescape(match.Groups[1].Value));
- }
- }
- }
- }
- catch (Exception)
- {
- // if there's any error in getting the Steam directory, ignore it for now.
- }
- return folders;
- }
- public static string GTA_V_Folder()
- {
- var appFolders = SteamLibraryFolders().Select(x => x + "\\SteamApps\\common");
- foreach (var folder in appFolders)
- {
- try
- {
- var matches = Directory.GetDirectories(folder, "Grand Theft Auto V");
- if (matches.Length >= 1)
- {
- return matches[0];
- }
- }
- catch (DirectoryNotFoundException)
- {
- //continue;
- }
- }
- return String.Empty;
- }
- }
- public class GTAVNOIntroPatchOTF
- {
- #region Constants
- const int PROCESS_ALL_ACCESS = (int)(0x001F0FFF);
- const UInt64 BUFFER_SIZE = 8192 * 4;
- #endregion
- #region Windows DLL Imports
- [DllImport("kernel32.dll")]
- private static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);
- [DllImport("kernel32.dll")]
- private static extern bool ReadProcessMemory(int hProcess,
- UInt64 lpBaseAddress, byte[] lpBuffer, UInt64 dwSize, ref UInt64 lpNumberOfBytesRead);
- [DllImport("Kernel32.dll")]
- private static extern bool WriteProcessMemory(int handle, UInt64 lpBaseAddress, byte[] lpBuffer, int nSize, UInt64 lpNumberOfBytesWritten);
- /*
- [DllImport("kernel32.dll", SetLastError = true)]
- internal static extern bool VirtualProtect(UInt64 address, int size, UInt64 newProtect, out UInt64 oldProtect);
- /*
- [DllImport("USER32.DLL")]
- private static extern bool IsWindowVisible(IntPtr hWnd);
- */
- [DllImport("user32.dll")]
- [return: MarshalAs(UnmanagedType.Bool)]
- private static extern bool IsWindow(IntPtr hWnd);
- [DllImport("kernel32.dll")]
- private static extern IntPtr OpenThread(ThreadAccess dwDesiredAccess, bool bInheritHandle, uint dwThreadId);
- [DllImport("kernel32.dll")]
- private static extern uint SuspendThread(IntPtr hThread);
- [DllImport("kernel32.dll")]
- private static extern int ResumeThread(IntPtr hThread);
- [DllImport("kernel32.dll")]
- [return: MarshalAs(UnmanagedType.Bool)]
- private static extern bool CloseHandle(IntPtr hObject);
- #endregion
- private static IntPtr processHandle;
- private static bool Patchintro;
- private static bool Patchlegalmessages;
- private static bool Debug;
- private static bool Setpriotity;
- private static Stopwatch stopwatch;
- public static void Init(bool patchintro, bool patchlegalmessages,bool setpriority, bool debug)
- {
- Patchintro = patchintro;
- Patchlegalmessages = patchlegalmessages;
- Debug = debug;
- Setpriotity = setpriority;
- Thread thread1 = new Thread(main);
- thread1.Start();
- }
- private static void main()
- {
- if (Debug)
- {
- CustomOutput.ConsoleWriteandAddLog("Waiting for the GTA V process...");
- }
- //CpuAffinity.SetCurrentThreadToHaveCpuAffinityFor(3);
- //Waiting for the GTA V process
- while (!IsProcessOpen("GTA5"))
- {
- System.Threading.Thread.Sleep(5);
- }
- Process process = Process.GetProcessesByName("GTA5").First<Process>();;
- if (Debug)
- {
- CustomOutput.ConsoleWriteandAddLog((char)34 + "GTA5" + (char)34 + " process (exe) found!");
- CustomOutput.ConsoleWriteandAddLog("Process handle: " + Convert.ToString(process.Handle));
- CustomOutput.ConsoleWriteandAddLog("Process MainModule.BaseAddress: 0x" + process.MainModule.BaseAddress.ToString("X"));
- CustomOutput.ConsoleWriteandAddLog("Waiting for the GTA V process window...");
- }
- //The memory we are looking for only exists, if the game exe create a window
- //while (!IsWindowVisible(process.MainWindowHandle))
- /*
- while (!IsWindow(process.MainWindowHandle))
- {
- System.Threading.Thread.Sleep(5);
- }
- */
- process.WaitForInputIdle();
- if (Debug)
- {
- CustomOutput.ConsoleWriteandAddLog("Process window found!");
- // Create new stopwatch.
- stopwatch = new Stopwatch();
- // Begin timing.
- stopwatch.Start();
- CustomOutput.ConsoleWriteandAddLog("Suspending process!");
- }
- //game window exists --> memory we are looking for exists
- //stop exe process that it can't load anything
- SuspendProcess(process.Id);
- //if(process.Threads[0].ThreadState == ThreadState.)
- if (Debug)
- {
- CustomOutput.ConsoleWriteandAddLog("Opening process handle...");
- }
- //Open process to access it
- processHandle = OpenProcess(PROCESS_ALL_ACCESS, false, process.Id);
- //CustomOutput.ConsoleWriteandAddLog("Disable Intro:");
- if (Patchintro)
- {
- DisableIntro();
- }
- //CustomOutput.ConsoleWriteandAddLog("Disable Legal Messages:");
- if (Patchlegalmessages)
- {
- DisableLegalMessages();
- }
- /*
- int cpuaffinitybitmask = Convert.ToInt32("1111", 2);
- process.ProcessorAffinity = (IntPtr)cpuaffinitybitmask;
- if (Debug)
- {
- CustomOutput.ConsoleWriteandAddLog("Setting process CPU affinity to bitmask " + Convert.ToString(cpuaffinitybitmask) + " ...");
- }
- */
- if (Setpriotity)
- {
- if (Debug)
- {
- CustomOutput.ConsoleWriteandAddLog("Setting process priority to " + ProcessPriorityClass.RealTime.ToString());
- }
- process.PriorityClass = ProcessPriorityClass.RealTime;
- }
- if (Debug)
- {
- CustomOutput.ConsoleWriteandAddLog("Resuming process...");
- }
- //Continue game process, I guess you want to play right?
- ResumeProcess(process.Id);
- if (Debug)
- {
- CustomOutput.ConsoleWriteandAddLog("Closing process handle...");
- }
- //Close handle that we have no access problems...
- CloseHandle((IntPtr)processHandle);
- if (Debug)
- {
- CustomOutput.ConsoleWriteandAddLog("FINISHED!");
- //Stop timing.
- stopwatch.Stop();
- //Write result.
- CustomOutput.ConsoleWriteandAddLog("Total time for memory search and patching in ms: " + stopwatch.Elapsed.TotalMilliseconds.ToString());
- Console.ReadLine();
- }
- Environment.Exit(0);
- }
- #region Main functions
- private static void DisableIntro()
- {
- byte[] im_pattern = Encoding.ASCII.GetBytes("rockstar_logos");
- bool found = false;
- UInt64 bytesRead = 0;
- UInt64 n_Size = BUFFER_SIZE;
- UInt64 n_Size_pattern = (ulong)im_pattern.Length;
- byte[] buffer = new byte[n_Size];
- byte[] buffer_pattern = new byte[n_Size_pattern];
- Process process = Process.GetProcessesByName("GTA5")[0];
- UInt64 address = (UInt64)process.MainModule.BaseAddress;
- if (Debug)
- {
- CustomOutput.ConsoleWriteandAddLog("Looking for the Intro address...");
- }
- while (!found)
- {
- ReadProcessMemory((int)processHandle, address, buffer, (UInt64)buffer.Length, ref bytesRead);
- uint index = 0;
- //when we found what we are looking for, display the addresses and patch the memory
- if (IndexOf(buffer, im_pattern, out index))
- {
- ReadProcessMemory((int)processHandle, address + index, buffer_pattern, (UInt64)n_Size_pattern, ref bytesRead);
- if (MaskCheck(buffer_pattern, im_pattern, "xxxxxxxxxxxxxx"))
- {
- if (Debug)
- {
- CustomOutput.ConsoleWriteandAddLog("Found Intro Movies at: 0x" + (index + address).ToString("X"));
- CustomOutput.ConsoleWriteandAddLog("Applying memory patch for Intro Movies...");
- }
- //Console.ReadLine();
- //this is the most important function, the shit where magic happens!!!!
- //write the name of a file which for sure doesn't exist
- if (WriteString((int)processHandle, address + index, "lolololo_logos.bik"))
- {
- if (Debug)
- {
- CustomOutput.ConsoleWriteandAddLog("Memory patch for Intro Movies successful: YES");
- }
- ////Console.Beep(500, 500);
- }
- else
- {
- //should never happen lol...
- if (Debug)
- {
- CustomOutput.ConsoleWriteandAddLog("Memory patch for Intro Movies successful: NO");
- }
- }
- found = true;
- }
- }
- address += n_Size;
- }
- }
- private static void DisableLegalMessages()
- {
- // x x x ? ? ? x x x ? ? ? x x x x
- //the value under this line was for PC below 1.0.678.1
- //byte[] lm_pattern = { 0x72, 0x1F, 0xE8, 0x12, 0x8D, 0xFB, 0x00, 0x8B, 0x0D, 0x34, 0x7F, 0xC3, 0x01, 0xFF, 0xC1, 0x48 };
- byte[] lm_pattern = { 0x72, 0x1F, 0xE8, 0x12, 0x8D, 0xFB, 0x01, 0x8B, 0x0D, 0x34, 0x7F, 0xC3, 0x01, 0xFF, 0xC1, 0x48 };
- bool found = false;
- //the mask under this line was for PC below 1.0.678.1
- //string mask = "xxx???xxx???xxxx";
- string mask = "xxx????xx???xxxx";
- UInt64 bytesRead = 0;
- UInt64 n_Size = BUFFER_SIZE;
- UInt64 n_Size_pattern = (ulong)lm_pattern.Length;
- byte[] buffer = new byte[n_Size];
- byte[] buffer_pattern = new byte[n_Size_pattern];
- byte[] nop_data = { 0x90, 0x90 };
- //byte[] nop_data = { 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90 };
- Process process = Process.GetProcessesByName("GTA5")[0];
- UInt64 address = (UInt64)process.MainModule.BaseAddress;
- if (Debug)
- {
- CustomOutput.ConsoleWriteandAddLog("Looking for the legal messages address...");
- }
- while (!found)
- {
- ReadProcessMemory((int)processHandle, address, buffer, (UInt64)buffer.Length, ref bytesRead);
- uint index = 0;
- //when we found what we are looking for, display the addresses and patch the memory
- if (IndexOf(buffer, new byte[] { lm_pattern[0], lm_pattern[1], lm_pattern[2] }, out index))
- {
- ReadProcessMemory((int)processHandle, address + index, buffer_pattern, (UInt64)n_Size_pattern, ref bytesRead);
- if (MaskCheck(buffer_pattern, lm_pattern, mask))
- {
- if (Debug)
- {
- CustomOutput.ConsoleWriteandAddLog("Found Legal Messages at: 0x" + (index + address).ToString("X"));
- CustomOutput.ConsoleWriteandAddLog("Applying memory patch for Legal Messages...");
- }
- //Console.ReadLine();
- //this is the most important function, the shit where magic happens!!!!
- //Legal Messages have no movie files. They are "generated".
- //Here we overwrite the internal programm code which displays them with NOPs
- if (WriteProcessMemory((int)processHandle, address + index, nop_data, nop_data.Length, 0))
- {
- if (Debug)
- {
- CustomOutput.ConsoleWriteandAddLog("Memory patch for Legal Messages successful: YES");
- }
- ////Console.Beep(500, 500);
- }
- else
- {
- if (Debug)
- {
- CustomOutput.ConsoleWriteandAddLog("Memory patch for Legal Messages successful: NO");
- }
- }
- found = true;
- }
- }
- address += n_Size;
- }
- }
- #endregion
- #region Memory helper functions
- private static bool MaskCheck(byte[] btsrc, byte[] btPattern, string strMask)
- {
- // Loop the pattern and compare to the mask and dump.
- for (int x = 0; x < btPattern.Length; x++)
- {
- // If the mask char is a wildcard, just continue.
- if (strMask[x] == '?')
- continue;
- // If the mask char is not a wildcard, ensure a match is made in the pattern.
- if ((strMask[x] == 'x') && (btsrc[x] != btPattern[x]))
- return false;
- }
- // The loop was successful so we found the pattern.
- return true;
- }
- private static bool WriteString(int handle, UInt64 address, string value)
- {
- byte[] data = Encoding.Default.GetBytes(value + "\0");
- return WriteProcessMemory(handle, address, data, data.Length, 0);
- }
- private static bool WriteInt(int handle, UInt64 address, int value)
- {
- byte[] data = BitConverter.GetBytes(value);
- return WriteProcessMemory(handle, address, data, data.Length, 0);
- }
- private static bool IndexOf(byte[] arrayToSearchThrough, byte[] patternToFind, out uint index)
- {
- if (patternToFind.Length > arrayToSearchThrough.Length)
- {
- index = 0;
- return false;
- }
- for (uint i = 0; i < arrayToSearchThrough.Length - patternToFind.Length; i++)
- {
- bool found = true;
- for (uint j = 0; j < patternToFind.Length; j++)
- {
- if (arrayToSearchThrough[i + j] != patternToFind[j])
- {
- found = false;
- break;
- }
- }
- if (found)
- {
- index = i;
- return true;
- }
- }
- index = 0;
- return false;
- }
- #endregion
- #region Process/Thread functions
- private static bool IsProcessOpen(string name)
- {
- foreach (Process clsProcess in Process.GetProcesses())
- {
- if (clsProcess.ProcessName.Contains(name))
- {
- return true;
- }
- }
- return false;
- }
- [Flags]
- private enum ThreadAccess : int
- {
- TERMINATE = (0x0001),
- SUSPEND_RESUME = (0x0002),
- GET_CONTEXT = (0x0008),
- SET_CONTEXT = (0x0010),
- SET_INFORMATION = (0x0020),
- QUERY_INFORMATION = (0x0040),
- SET_THREAD_TOKEN = (0x0080),
- IMPERSONATE = (0x0100),
- DIRECT_IMPERSONATION = (0x0200)
- }
- private static void SuspendProcess(int pid)
- {
- var process = Process.GetProcessById(pid);
- if (process.ProcessName == string.Empty)
- return;
- foreach (ProcessThread pT in process.Threads)
- {
- IntPtr pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)pT.Id);
- if (pOpenThread == IntPtr.Zero)
- {
- continue;
- }
- SuspendThread(pOpenThread);
- CloseHandle(pOpenThread);
- }
- }
- private static void ResumeProcess(int pid)
- {
- var process = Process.GetProcessById(pid);
- if (process.ProcessName == string.Empty)
- return;
- foreach (ProcessThread pT in process.Threads)
- {
- IntPtr pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)pT.Id);
- if (pOpenThread == IntPtr.Zero)
- {
- continue;
- }
- var suspendCount = 0;
- do
- {
- suspendCount = ResumeThread(pOpenThread);
- } while (suspendCount > 0);
- CloseHandle(pOpenThread);
- }
- }
- #endregion
- }
- public static class NonStandardConsole
- {
- [DllImport("kernel32.dll", SetLastError = true)]
- static extern bool AllocConsole();
- [DllImport("kernel32.dll", SetLastError = true)]
- static extern bool FreeConsole();
- [DllImport("kernel32", SetLastError = true)]
- static extern bool AttachConsole(int dwProcessId);
- [DllImport("user32.dll")]
- static extern IntPtr GetForegroundWindow();
- [DllImport("user32.dll", SetLastError = true)]
- static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
- public static void CloseConsole()
- {
- FreeConsole();
- }
- public static void OpenConsole()
- {
- //Get a pointer to the forground window. The idea here is that
- //IF the user is starting our application from an existing console
- //shell, that shell will be the uppermost window. We'll get it
- //and attach to it
- IntPtr ptr = GetForegroundWindow();
- int u;
- GetWindowThreadProcessId(ptr, out u);
- Process process = Process.GetProcessById(u);
- if (process.ProcessName == "cmd") //Is the uppermost window a cmd process?
- {
- AttachConsole(process.Id);
- //we have a console to attach to ..
- //Console.WriteLine("hello. It looks like you started me from an existing console.");
- }
- else
- {
- //no console AND we're in console mode ... create a new console.
- AllocConsole();
- //Console.WriteLine(@"hello. It looks like you double clicked me to start AND you want console mode. Here's a new console.");
- }
- }
- }
- public static class CustomOutput
- {
- public static void ConsoleWriteandAddLog(string text)
- {
- Console.WriteLine(text);
- try
- {
- using (StreamWriter w = File.AppendText("log.txt"))
- {
- w.WriteLine(text);
- }
- }
- catch
- {
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement