Bnet.Patcher.Program.Main C# (CSharp) Method

Main() static private method

static private Main ( string args ) : void
args string
return void
        static void Main(string[] args)
        {
            var running = false;
            var hWnd = IntPtr.Zero;
            Console.WriteLine("Looking for Diablo III Process");
            while (!running)
            {
                foreach (var p in Process.GetProcesses())
                {
                    if (p.ProcessName == "Diablo III")
                    {
                        Console.WriteLine("Process Found!");
                        Console.WriteLine("Waiting for Diablo 3 Interface to load...");
                        while (!p.Responding)
                        {
                            //Waiting for interface to respond.
                        }
                        Console.WriteLine("Diablo3 Loaded, please wait...");
                        Thread.Sleep(3000);
                        Console.WriteLine("Applying Patch!...");

                        try
                        {
                            hWnd = OpenProcess(0x001F0FFF, false, p.Id);
                            if (hWnd == IntPtr.Zero)
                                throw new Exception("Failed to open process.");

                            var modules = p.Modules;
                            IntPtr baseAddr = IntPtr.Zero;

                            foreach (ProcessModule module in modules)
                            {
                                if (module.ModuleName == "battle.net.dll")
                                {
                                    if (module.FileVersionInfo.FileDescription == version)
                                    {
                                        baseAddr = module.BaseAddress;
                                        break;
                                    }
                                    else
                                        throw new Exception("battle.net.dll version different than expected.");
                                }
                            }

                            if (baseAddr == IntPtr.Zero)
                                throw new Exception("Failed to locate battle.net.dll");

                            var JMPAddr = baseAddr.ToInt32() + offset;
                            var BytesWritten = IntPtr.Zero;
                            byte[] JMP = new byte[] { 0xEB };
                            Console.WriteLine("battle.net.dll address: 0x{0:X8}", baseAddr.ToInt32());
                            var prevByte = ReadByte(hWnd, JMPAddr);
                            if (prevByte != 0x75)
                            {
                                running = true;
                                throw new Exception(string.Format("File already patched or unknown battle.net.dll version. 0x{0:X2} != 0x75", prevByte));
                            }

                            WriteProcessMemory(hWnd, new IntPtr(JMPAddr), JMP, 1, out BytesWritten);
                            Console.WriteLine("After write: 0x{0:X2}", ReadByte(hWnd, JMPAddr));

                            if (BytesWritten.ToInt32() < 1)
                                throw new Exception("Failed to write to process.");
                            else
                            {
                                Console.ForegroundColor = ConsoleColor.Green;
                                Console.WriteLine("Diablo III succesfully patched!");
                            }
                            running = true;
                            break;
                        }
                        catch (Exception e)
                        {
                            Console.Write(e.Message);
                        }
                        finally
                        {
                            if (hWnd != IntPtr.Zero)
                                CloseHandle(hWnd);
                        }
                    }
                }
            }
            Thread.Sleep(2000);
        }