Exemplo n.º 1
0
            private void HandleMsg(string msg)
            {
                // Message Example:
                // PID:CMD DATA
                // 1234:GameLeft 25-09-1985 18:27:00
                Debug.WriteLine("Recieved: " + msg);
                try
                {
                    string pid = msg.Split(':')[0];
                    string cmd = msg.Substring(pid.Length + 1).Split(' ')[0];
                    int    x;
                    msg = msg.Substring(((x = pid.Length + cmd.Length + 2) >= msg.Length ? 0 : x));

                    BotClass b =
                        BotSettings.Instance.Bots.FirstOrDefault(
                            f =>
                            (f.Demonbuddy != null && f.Demonbuddy.Proc != null) &&
                            f.Demonbuddy.Proc.Id == Convert.ToInt32(pid));
                    if (b == null)
                    {
                        Send("Error: Unknown process");
                        StatFailed++;
                        return;
                    }

                    long nowTicks = DateTime.UtcNow.Ticks;

                    switch (cmd)
                    {
                    case "Initialized":
                        b.AntiIdle.Stats = new BotStats
                        {
                            LastGame    = nowTicks,
                            LastPulse   = nowTicks,
                            PluginPulse = nowTicks,
                            LastRun     = nowTicks
                        };
                        b.AntiIdle.LastStats     = DateTime.UtcNow;
                        b.AntiIdle.State         = IdleState.CheckIdle;
                        b.AntiIdle.IsInitialized = true;
                        b.AntiIdle.InitAttempts  = 0;
                        Send("Roger!");
                        break;

                    case "GameLeft":
                        b.ProfileSchedule.Count++;
                        if (b.ProfileSchedule.Current.Runs > 0)
                        {
                            Logger.Instance.Write(b, "Runs completed ({0}/{1})", b.ProfileSchedule.Count,
                                                  b.ProfileSchedule.MaxRuns);
                        }
                        else
                        {
                            Logger.Instance.Write(b, "Runs completed {0}", b.ProfileSchedule.Count);
                        }

                        if (b.ProfileSchedule.IsDone)
                        {
                            string newprofile = b.ProfileSchedule.GetProfile;
                            Logger.Instance.Write(b, "Next profile: {0}", newprofile);
                            Send("LoadProfile " + newprofile);
                        }
                        else
                        {
                            Send("Roger!");
                        }
                        break;

                    case "NewDifficultyLevel":
                        Logger.Instance.Write(b, "Sending DifficultyLevel: {0}",
                                              b.ProfileSchedule.Current.DifficultyLevel);
                        Send("DifficultyLevel " + (int)b.ProfileSchedule.Current.DifficultyLevel);
                        break;

                    case "UserStop":
                        b.Status         = string.Format("User Stop: {0:d-m H:M:s}", DateTime.UtcNow);
                        b.AntiIdle.State = IdleState.UserStop;
                        Logger.Instance.Write(b, "Demonbuddy stopped by user");
                        Send("Roger!");
                        break;

                    case "StartDelay":
                        var delay = new DateTime(long.Parse(msg));
                        b.AntiIdle.StartDelay = delay.AddSeconds(60);
                        b.AntiIdle.State      = IdleState.StartDelay;
                        Send("Roger!");
                        break;

                    // Giles Compatibility
                    case "ThirdpartyStop":
                        b.Status         = string.Format("Thirdparty Stop: {0:d-m H:M:s}", DateTime.UtcNow);
                        b.AntiIdle.State = IdleState.UserStop;
                        Logger.Instance.Write(b, "Demonbuddy stopped by Thirdparty");
                        Send("Roger!");
                        break;

                    case "TrinityPause":
                        b.AntiIdle.State = IdleState.UserPause;
                        Logger.Instance.Write(b, "Trinity Pause Detected");
                        Send("Roger!");
                        break;

                    case "AllCompiled":
                    {
                        Logger.Instance.Write(b, "Check Force Enable Plugins? {0}", b.Demonbuddy.ForceEnableAllPlugins);
                        Send(b.Demonbuddy.ForceEnableAllPlugins ? "ForceEnableAll" : "ForceEnableYar");
                        break;
                    }

                    case "CrashTender":
                        if (Settings.Default.UseKickstart && File.Exists(msg))
                        {
                            b.Demonbuddy.CrashTender(msg);
                        }
                        else
                        {
                            b.Demonbuddy.CrashTender();
                        }
                        Send("Roger!");
                        break;

                    case "CheckConnection":
                        ConnectionCheck.CheckValidConnection(true);
                        Send("Roger!");
                        break;

                    case "D3Exit":
                        Send("Shutdown");
                        b.Diablo.Proc.CloseMainWindow();
                        break;

                    // Unknown command reply
                    default:
                        Send("Unknown command!");
                        Logger.Instance.WriteGlobal("Unknown command recieved: " + msg);
                        break;
                    }
                }
                catch (Exception ex)
                {
                    StatFailed++;
                    Send("Internal server error: " + ex.Message);
                    DebugHelper.Exception(ex);
                }
            }
Exemplo n.º 2
0
        public static void PositionWindows()
        {
            try
            {
                List <ScreensClass> workingScreens = Settings.Default.AutoPosScreens.Where(x => x.Enabled).ToList();
                if (workingScreens.Count == 0)
                {
                    return;
                }
                workingScreens.Sort((s1, s2) => s1.Order.CompareTo(s2.Order));

                IEnumerable <BotClass> bots = BotSettings.Instance.Bots.Where(x => x.IsEnabled);

                int sc = 0; // Screen counter
                int dy = 0; // Diablo Y-Axis counter
                int dx = 0; // Diablo X-Axis counter

                // Calculated window height
                decimal addy = (Settings.Default.AutoPosDiabloCascade ? 30 : Settings.Default.AutoPosDiabloH);

                foreach (BotClass bot in bots)
                {
                    DateTime     time   = DateTime.UtcNow;
                    ScreensClass screen = workingScreens[sc]; // set current screen

                    // Calculate demonbuddy position
                    if (!(bot.Demonbuddy.ManualPosSize && !Settings.Default.ForceAutoPos))
                    {
                        // todo
                    }

                    // Calculate diablo position
                    if (!(bot.Diablo.ManualPosSize && !Settings.Default.ForceAutoPos))
                    {
                        // Dont mess with IsBoxer
                        if (bot.Diablo.UseIsBoxer)
                        {
                            continue;
                        }

                        var y = (int)(addy * dy); // get next position on Y-Axis of the screen
                        // check if window pos+height does exceed screen working area
                        if ((y + addy) > screen.WorkingArea.Height)
                        {
                            dy = y = 0;                                      // reset counters + Y-Axis position
                            dx++;                                            // move to next X-Axis "line"
                        }
                        var x = (int)(Settings.Default.AutoPosDiabloW * dx); // get next position on X-Axis of the screen
                        // check if window pos+width does exceed screen working area
                        if ((x + Settings.Default.AutoPosDiabloW) > screen.WorkingArea.Width)
                        {
                            if (!Settings.Default.AutoPosForceWorkingArea)
                            {
                                sc++;
                                // Check if screen count is bigger than actual screens available
                                if (sc > workingScreens.Count - 1)
                                {
                                    sc = 0; // reset to first screen
                                }
                            }
                            dx = x = 0; // reset counters + X-Axis position
                            dy = y = 0; // reset counters + Y-Axis position
                        }

                        if (bot.Diablo.MainWindowHandle != IntPtr.Zero)
                        {
                            RemoveWindowFrame(bot.Diablo.MainWindowHandle);
                            RepositionWindow(bot.Diablo.MainWindowHandle, x + screen.WorkingArea.X,
                                             y + screen.WorkingArea.Y, (int)Settings.Default.AutoPosDiabloW,
                                             (int)Settings.Default.AutoPosDiabloH);
                        }
                        dy++; // move to next Y-Axis "line"
                    }

                    // calculate sleeptime
                    var sleep = (int)(Program.Sleeptime - DateTime.UtcNow.Subtract(time).TotalMilliseconds);
                    if (sleep > 0)
                    {
                        Thread.Sleep(sleep);
                    }
                }
            }
            catch (Exception ex)
            {
                DebugHelper.Write("Failed", "PositionWindows()");
                DebugHelper.Exception(ex);
            }
        }
        // Dont link this list

        public static void Create(Bot.Bot bot)
        {
            var imp = new Impersonator();

            try
            {
                if (bot.UseWindowsUser)
                {
                    imp.Impersonate(bot.WindowsUserName, "localhost", bot.WindowsUserPassword);
                }

                bot.Status = "Create Diablo Clone";
                var basepath  = Path.GetDirectoryName(bot.Diablo.Location);
                var clonepath = Path.Combine(bot.DiabloCloneLocation, "Diablo III");

                // if diablo base path does not exist stop here!
                if (basepath != null && !Directory.Exists(basepath))
                {
                    bot.Stop();
                    throw new Exception("Diablo base directory does not exist!");
                }

                // Check if given language is installed on basepath
                var testpath = Path.Combine(basepath, @"Data_D3\PC\MPQs", General.GetLocale(bot.Diablo.Language));
                if (!Directory.Exists(testpath))
                {
                    bot.Stop();
                    throw new Exception($"ERROR: {bot.Diablo.Language} language is not installed (path: {testpath})");
                }


                // if diablo clone does not exist create it
                if (!Directory.Exists(Path.Combine(clonepath, @"Data_D3\PC\MPQs\Cache")))
                {
                    Logger.Instance.Write(bot, "Creating new Diablo Clone");
                    Directory.CreateDirectory(Path.Combine(clonepath, @"Data_D3\PC\MPQs\Cache"));
                }

                // Create Search caches
                var baseFileCache  = new FileListCache(basepath);
                var cloneFileCache = new FileListCache(clonepath);

                // Check if all links are made for our clone
                foreach (var p in baseFileCache.FileList)
                {
                    try
                    {
                        if (p.Directory && !Directory.Exists(Path.Combine(clonepath.ToLower(), p.Path.ToLower())))
                        {
                            if (!s_noLinks.Any(n => General.WildcardMatch(n.Source, p.Path)))
                            {
                                Logger.Instance.Write(bot, "NewLink: {0} -> {1}", Path.Combine(clonepath, p.Path),
                                                      Path.Combine(basepath, p.Path));
                                //if (!CreateSymbolicLink( Path.Combine(clonepath,p.Path),  Path.Combine(basepath,p.Path), 1))
                                //  throw new Exception("Failed to create link!");
                                Directory.CreateDirectory(Path.Combine(clonepath, p.Path));
                            }
                            continue;
                        }
                        if (!p.Directory && !File.Exists(Path.Combine(clonepath.ToLower(), p.Path.ToLower())))
                        {
                            if (!s_noLinks.Any(n => General.WildcardMatch(n.Source, p.Path)))
                            {
                                Logger.Instance.Write(bot, "NewLink: {0} -> {1}", Path.Combine(clonepath, p.Path),
                                                      Path.Combine(basepath, p.Path));
                                if (Path.GetExtension(Path.Combine(clonepath, p.Path)).ToLower().Equals(".exe"))
                                {
                                    if (
                                        !CreateHardLink(Path.Combine(clonepath, p.Path), Path.Combine(basepath, p.Path),
                                                        IntPtr.Zero))
                                    {
                                        throw new Exception("Failed to create link!");
                                    }
                                }
                                else
                                {
                                    if (
                                        !CreateSymbolicLink(Path.Combine(clonepath, p.Path),
                                                            Path.Combine(basepath, p.Path), 0))
                                    {
                                        throw new Exception("Failed to create link!");
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                }

                // Remove links that have no target

                /*
                 * foreach (var p in cloneFileCache.FileList)
                 * {
                 *  try
                 *  {
                 *      if (p.directory && !Directory.Exists(Path.Combine(basepath, p.Path)))
                 *      {
                 *          if (!_noLinks.Any(n => General.WildcardMatch(n.Source.ToLower(), p.Path.ToLower())))
                 *              Console.WriteLine("Delete: {0}", p.Path);
                 *          continue;
                 *      }
                 *
                 *      if (!p.directory && !File.Exists(Path.Combine(basepath.ToLower(), p.Path.ToLower())))
                 *      {
                 *          if (!_noLinks.Any(n => General.WildcardMatch(n.Source, p.Path)))
                 *              Console.WriteLine("Delete: {0}", p.Path);
                 *      }
                 *  }
                 *  catch (Exception ex)
                 *  {
                 *      Logger.Instance.Write(bot, ex.ToString());
                 *  }
                 * }
                 */
            }
            catch (Exception ex)
            {
                bot.Stop();
                DebugHelper.Write(bot, "Failed to create clone!");
                DebugHelper.Exception(ex);
            }
            imp.Dispose();
        }