Пример #1
0
 /// <summary>
 ///     Write Log message for specific bot
 /// </summary>
 /// <param name="bot">BotClass</param>
 /// <param name="format"></param>
 /// <param name="args"></param>
 public void Write(Bot.Bot bot, string format, params object[] args)
 {
     if (bot == null)
     {
         _logger.Information(format, args);
         return;
     }
     _logger.ForContext("BotName", bot.Name).Information(format, args);
 }
Пример #2
0
        public bool KillSession(Bot.Bot bot)
        {
            var username = bot.Demonbuddy.BuddyAuthUsername;
            var password = bot.Demonbuddy.BuddyAuthPassword;

            // if username and password are empty stop here!
            if (username.Length <= 0 && password.Length <= 0)
            {
                return(false);
            }

            var client =
                _buddyAuthWebClients.FirstOrDefault(c => c.Username.Equals(username) && c.Password.Equals(password));

            if (client == null)
            {
                client = new BuddyAuthWebClient(username, password);
                _buddyAuthWebClients.Add(client);
            }
            client.KillSession(bot);
            return(false);
        }
 public static void Write(Bot.Bot bot, string message, params object[] args)
 {
     s_logger.ForContext("Bot", bot.Name).Information(message, args);
 }
 public static void Write(Bot.Bot bot, string message, string caller, params object[] args)
 {
     s_logger.ForContext("Bot", bot.Name).Information($"[{{caller}}] {message}", new object[] { caller }.Union(args).ToArray());
 }
Пример #5
0
        public void KillSession(Bot.Bot bot)
        {
            lock (_webClientLock)
            {
                if (bot.Demonbuddy.LoginTime.HasValue)
                {
                    var logintime = bot.Demonbuddy.LoginTime.Value;
                    _webClient.Headers["Referer"] = SessionsUrl;
                    var data  = new NameValueCollection();
                    var match = GetSessions()
                                .Where(
                        i =>
                        logintime.Subtract(i.Time).TotalSeconds <16 &&
                                                                 logintime.Subtract(i.Time).TotalSeconds> -16)
                                .OrderBy(i => logintime.Subtract(i.Time).TotalSeconds).FirstOrDefault();

                    if (match.Id == 0)
                    {
                        Logger.Instance.Write(bot, "BuddyAuth: No session found.");
                        return;
                    }

                    data.Set("selectedSessions[" + match.Number + "].Id", Convert.ToString(match.Id));
                    data.Set("selectedSessions[" + match.Number + "].IsChecked", "true");

                    try
                    {
                        var retry = 3;
                        do
                        {
                            try
                            {
                                _webClient.UploadValues(SessionsUrl, data);
                                Logger.Instance.Write(bot,
                                                      "BuddyAuth: Session with id: {0} killed! (Time difference: {1})",
                                                      match.Id, logintime.Subtract(match.Time).TotalSeconds);
                                return;
                            }
                            catch (WebException wex)
                            {
                                var code = ((HttpWebResponse)wex.Response).StatusCode;
                                if (code != HttpStatusCode.InternalServerError &&
                                    code != HttpStatusCode.BadGateway &&
                                    code != HttpStatusCode.ServiceUnavailable &&
                                    code != HttpStatusCode.GatewayTimeout &&
                                    code != HttpStatusCode.RequestTimeout)
                                {
                                    Logger.Instance.Write(bot, "Failed: {0}", wex.Message);
                                    return;
                                }

                                Logger.Instance.Write(bot, "Failed: {0} (next retry in 5 seconds) [{1}]", wex.Message,
                                                      3 - retry + 1);
                                Thread.Sleep(5000);
                            }
                        } while (retry-- > 0);
                    }
                    catch (Exception ex)
                    {
                        DebugHelper.Write(bot, "BuddyAuth session killer failed!");
                        DebugHelper.Exception(ex);
                        return;
                    }
                }
                Logger.Instance.Write(bot, "BuddyAuth: No session found.");
            }
        }
        public static void ManualPositionWindow(IntPtr handle, int x, int y, int w, int h, Bot.Bot bot = null)
        {
            // Set window position and size
            try
            {
                if (WinApi.GetWindowRect(new HandleRef(bot, handle), out var rct))
                {
                    if (w <= 0)
                    {
                        w = rct.Width;
                    }
                    if (h <= 0)
                    {
                        h = rct.Heigth;
                    }

                    DebugHelper.Write(bot, "ManualPosition window:{0}: X:{1} Y:{2} W:{3} H:{4}", handle, x, y, w, h);
                    if (
                        !WinApi.SetWindowPos(handle, IntPtr.Zero, x, y, w, h,
                                             WinApi.SetWindowPosFlags.SWP_NOACTIVATE | WinApi.SetWindowPosFlags.SWP_NOSENDCHANGING))
                    {
                        Logger.Instance.Write(bot, "ManualPosition window:{0}: Failed!", handle);
                    }
                }
                else
                {
                    Logger.Instance.Write(bot, "ManualPosition Failed to get window rectangle");
                }
            }
            catch (Exception ex)
            {
                DebugHelper.Write(bot, "Failed", "ManualPositionWindow(...)");
                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();
        }