public void Start(bool noprofile = false, string profilepath = null, bool crashtenderstart = false)
        {
            if (!Parent.IsStarted || !Parent.Diablo.IsRunning || (_crashTenderRestart && !crashtenderstart))
            {
                return;
            }
            if (!File.Exists(Location))
            {
                Logger.Instance.Write("File not found: {0}", Location);
                return;
            }

            while (Parent.IsStarted && Parent.Diablo.IsRunning)
            {
                // Get Last login time and kill old session
                if (GetLastLoginTime)
                {
                    BuddyAuth.Instance.KillSession(Parent);
                }

                _isStopped = false;

                // Reset AntiIdle;
                Parent.AntiIdle.Reset(true);

                var arguments = "-pid=" + Parent.Diablo.Proc.Id;
                arguments += " -key=" + Key;
                arguments += " -autostart";
                arguments += string.Format(" -routine=\"{0}\"", CombatRoutine);
                arguments += string.Format(" -bnetaccount=\"{0}\"", Parent.Diablo.Username);
                arguments += string.Format(" -bnetpassword=\"{0}\"", Parent.Diablo.Password);

                if (profilepath != null)
                {
                    // Check if current profile path is Kickstart
                    var file = Path.GetFileName(profilepath);
                    if (file == null || (file.Equals("YAR_Kickstart.xml") || file.Equals("YAR_TMP_Kickstart.xml")))
                    {
                        profilepath = Parent.ProfileSchedule.GetProfile;
                    }

                    var profile = new Profile()
                    {
                        Location = profilepath
                    };
                    var path = ProfileKickstart.GenerateKickstart(profile);
                    arguments += string.Format(" -profile=\"{0}\"", path);
                }
                else if (Parent.ProfileSchedule.Profiles.Count > 0 && !noprofile)
                {
                    var path = Parent.ProfileSchedule.GetProfile;
                    if (File.Exists(path))
                    {
                        arguments += string.Format(" -profile=\"{0}\"", path);
                    }
                }
                else if (!noprofile)
                {
                    Logger.Instance.Write(
                        "Warning: Launching Demonbuddy without a starting profile (Add a profile to the profilescheduler for this bot)");
                }

                if (NoFlash)
                {
                    arguments += " -noflash";
                }
                if (AutoUpdate)
                {
                    arguments += " -autoupdate";
                }
                if (NoUpdate)
                {
                    arguments += " -noupdate";
                }

                if (ForceEnableAllPlugins)
                {
                    arguments += " -YarEnableAll";
                }

                Debug.WriteLine("DB Arguments: {0}", arguments);

                var p = new ProcessStartInfo(Location, arguments)
                {
                    WorkingDirectory = Path.GetDirectoryName(Location)
                };
                p = UserAccount.ImpersonateStartInfo(p, Parent);

                // Check/Install latest Communicator plugin
                var plugin = string.Format("{0}\\Plugins\\YAR\\Plugin.cs", p.WorkingDirectory);
                if (!PluginVersionCheck.Check(plugin))
                {
                    PluginVersionCheck.Install(plugin);
                }


                DateTime timeout;
                try                                        // Try to start Demonbuddy
                {
                    Parent.Status = "Starting Demonbuddy"; // Update Status
                    Proc          = Process.Start(p);

                    if (Program.IsRunAsAdmin)
                    {
                        Proc.PriorityClass = General.GetPriorityClass(Priority);
                    }
                    else
                    {
                        Logger.Instance.Write(Parent, "Failed to change priority (No admin rights)");
                    }


                    // Set affinity
                    if (CpuCount != Environment.ProcessorCount)
                    {
                        ProcessorAffinity = AllProcessors; // set it to all ones
                        CpuCount          = Environment.ProcessorCount;
                    }
                    Proc.ProcessorAffinity = (IntPtr)ProcessorAffinity;


                    Logger.Instance.Write(Parent, "Demonbuddy:{0}: Waiting for process to become ready", Proc.Id);

                    timeout = DateTime.Now;
                    while (true)
                    {
                        if (General.DateSubtract(timeout) > 30)
                        {
                            Logger.Instance.Write(Parent, "Demonbuddy:{0}: Failed to start!", Proc.Id);
                            Parent.Restart();
                            return;
                        }
                        Thread.Sleep(500);
                        try
                        {
                            Proc.Refresh();
                            if (Proc.WaitForInputIdle(100) || CrashChecker.IsResponding(MainWindowHandle))
                            {
                                break;
                            }
                        }
                        catch
                        {
                        }
                    }

                    if (_isStopped)
                    {
                        return;
                    }
                }
                catch (Exception ex)
                {
                    Logger.Instance.Write(ex.ToString());
                    Parent.Stop();
                }

                timeout = DateTime.Now;
                while (!FindMainWindow())
                {
                    if (General.DateSubtract(timeout) > 30)
                    {
                        MainWindowHandle = Proc.MainWindowHandle;
                        break;
                    }
                    Thread.Sleep(500);
                }

                // Window postion & resizing
                if (ManualPosSize)
                {
                    AutoPosition.ManualPositionWindow(MainWindowHandle, X, Y, W, H, Parent);
                }
                Logger.Instance.Write(Parent, "Demonbuddy:{0}: Process is ready", Proc.Id);

                // Wait for demonbuddy to be Initialized (this means we are logged in)
                // If we don't wait here the Region changeing for diablo fails!
                Logger.Instance.Write(Parent, "Demonbuddy:{0}: Waiting for demonbuddy to log into Diablo", Proc.Id);
                while (!IsInitialized && !_isStopped)
                {
                    Thread.Sleep(1000);
                }
                // We made to many attempts break here
                if (Parent.AntiIdle.FailedInitCount > 3)
                {
                    break;
                }
                if (!Parent.AntiIdle.IsInitialized)
                {
                    continue;                                 // Retry
                }
                // We are ready to go
                Logger.Instance.Write(Parent, "Demonbuddy:{0}: Initialized! We are ready to go", Proc.Id);
                Parent.AntiIdle.FailedInitCount = 0; // only reset counter
                break;
            } // while (Parent.IsStarted && Parent.Diablo.IsRunning)
        }
Ejemplo n.º 2
0
        public void Start(bool noprofile = false)
        {
            if (!Parent.IsStarted || !Parent.Diablo.IsRunning)
            {
                return;
            }
            if (!File.Exists(Location))
            {
                Logger.Instance.Write("File not found: {0}", Location);
                return;
            }

            // Get Last login time and kill old session
            if (GetLastLoginTime)
            {
                BuddyAuth.Instance.KillSession(Parent);
            }

            _isStopped = false;

            // Reset AntiIdle;
            Parent.AntiIdle.Reset(true);

            var arguments = "-pid=" + Parent.Diablo.Proc.Id;

            arguments += " -key=" + Key;
            arguments += " -autostart";
            arguments += string.Format(" -routine=\"{0}\"", CombatRoutine);
            arguments += string.Format(" -bnetaccount=\"{0}\"", Parent.Diablo.Username);
            arguments += string.Format(" -bnetpassword=\"{0}\"", Parent.Diablo.Password);

            if (Parent.ProfileSchedule.Profiles.Count > 0 && !noprofile)
            {
                var profilepath = Parent.ProfileSchedule.GetProfile;
                if (File.Exists(profilepath))
                {
                    arguments += string.Format(" -profile=\"{0}\"", profilepath);
                }
            }
            else if (!noprofile)
            {
                Logger.Instance.Write("Warning: Launching Demonbuddy without a starting profile (Add a profile to the profilescheduler for this bot)");
            }

            if (NoFlash)
            {
                arguments += " -noflash";
            }
            if (AutoUpdate)
            {
                arguments += " -autoupdate";
            }
            if (NoUpdate)
            {
                arguments += " -noupdate";
            }

            if (ForceEnableAllPlugins)
            {
                arguments += " -YarEnableAll";
            }

            Debug.WriteLine("DB Arguments: {0}", arguments);

            var p = new ProcessStartInfo(Location, arguments)
            {
                WorkingDirectory = Path.GetDirectoryName(Location)
            };

            // Check/Install latest Communicator plugin
            var plugin = string.Format("{0}\\Plugins\\YAR\\Plugin.cs", p.WorkingDirectory);

            if (!PluginVersionCheck.Check(plugin))
            {
                PluginVersionCheck.Install(plugin);
            }


            try                                        // Try to start Demonbuddy
            {
                Parent.Status = "Starting Demonbuddy"; // Update Status
                Proc          = Process.Start(p);

                if (Program.IsRunAsAdmin)
                {
                    Proc.PriorityClass = General.GetPriorityClass(Priority);
                }
                else
                {
                    Logger.Instance.Write(Parent, "Failed to change priority (No admin rights)");
                }


                // Set affinity
                if (CpuCount != Environment.ProcessorCount)
                {
                    ProcessorAffinity = AllProcessors; // set it to all ones
                    CpuCount          = Environment.ProcessorCount;
                }
                Proc.ProcessorAffinity = (IntPtr)ProcessorAffinity;


                Logger.Instance.Write(Parent, "Demonbuddy:{0}: Waiting for process to become ready", Proc.Id);
                if (!Proc.WaitForInputIdle(30000))
                {
                    Logger.Instance.Write(Parent, "Demonbuddy:{0}: Failed to start!", Proc.Id);
                    Parent.Restart();
                    return;
                }

                if (_isStopped)
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                Logger.Instance.Write(ex.ToString());
                Parent.Stop();
            }

            var timeout = DateTime.Now;

            while (!FindMainWindow())
            {
                if (General.DateSubtract(timeout) > 30)
                {
                    MainWindowHandle = Proc.MainWindowHandle;
                    break;
                }
                Thread.Sleep(500);
            }

            // Window postion & resizing
            if (ManualPosSize)
            {
                AutoPosition.ManualPositionWindow(MainWindowHandle, X, Y, W, H, Parent);
            }

            Logger.Instance.Write(Parent, "Demonbuddy:{0}: Process is ready", Proc.Id);
        }