Guest User

Untitled

a guest
Jun 4th, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.08 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Threading;
  4. using System.Windows.Forms;
  5. using Styx;
  6. using Styx.Helpers;
  7. using Styx.Logic.BehaviorTree;
  8. using Styx.Plugins;
  9. using Styx.Plugins.PluginClass;
  10. using Styx.WoWInternals;
  11.  
  12. namespace Relogger
  13. {
  14.  
  15.     #region form
  16.  
  17.     public class SettingsForm : Form
  18.     {
  19.         private FlowLayoutPanel panel = new FlowLayoutPanel();
  20.         private Label LabelUsername = new Label();
  21.         private Label LabelPassword = new Label();
  22.         private Label LabelAccount = new Label();
  23.         private Label LabelRealm = new Label();
  24.         private Label LabelCharacter = new Label();
  25.         private Label LabelTries = new Label();
  26.         private TextBox TextUsername = new TextBox();
  27.         private TextBox TextPassword = new TextBox();
  28.         private TextBox TextAccount = new TextBox();
  29.         private TextBox TextRealm = new TextBox();
  30.         private TextBox TextCharacter = new TextBox();
  31.         private TextBox TextTries = new TextBox();
  32.  
  33.         public SettingsForm()
  34.         {
  35.             LabelUsername.Text = "Username:";
  36.             LabelPassword.Text = "Password:";
  37.             LabelAccount.Text = "Account:";
  38.             LabelRealm.Text = "Realm:";
  39.             LabelCharacter.Text = "Character:";
  40.             LabelTries.Text = "Tries:";
  41.             TextUsername.Text = ReLogger.settings.Username;
  42.             TextUsername.Width = 190;
  43.             TextPassword.Text = ReLogger.settings.Password;
  44.             TextPassword.Width = 190;
  45.             TextAccount.Text = ReLogger.settings.Account;
  46.             TextAccount.Width = 190;
  47.             TextRealm.Text = ReLogger.settings.Realm;
  48.             TextRealm.Width = 190;
  49.             TextCharacter.Text = ReLogger.settings.Character;
  50.             TextCharacter.Width = 190;
  51.             TextTries.Text = ReLogger.settings.Tries.ToString();
  52.             TextTries.Width = 190;
  53.             panel.Dock = DockStyle.Fill;
  54.             panel.Controls.Add(LabelUsername);
  55.             panel.Controls.Add(TextUsername);
  56.             panel.Controls.Add(LabelPassword);
  57.             panel.Controls.Add(TextPassword);
  58.             panel.Controls.Add(LabelAccount);
  59.             panel.Controls.Add(TextAccount);
  60.             panel.Controls.Add(LabelRealm);
  61.             panel.Controls.Add(TextRealm);
  62.             panel.Controls.Add(LabelCharacter);
  63.             panel.Controls.Add(TextCharacter);
  64.             panel.Controls.Add(LabelTries);
  65.             panel.Controls.Add(TextTries);
  66.             this.Text = "Relogger Settings";
  67.             this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
  68.             this.Height = 190;
  69.             this.Width = 320;
  70.             this.Controls.Add(panel);
  71.         }
  72.  
  73.         protected override void Dispose(bool disposing)
  74.         {
  75.             ReLogger.settings.Username = TextUsername.Text;
  76.             ReLogger.settings.Password = TextPassword.Text;
  77.             ReLogger.settings.Account = TextAccount.Text;
  78.             ReLogger.settings.Realm = TextRealm.Text;
  79.             ReLogger.settings.Character = TextCharacter.Text;
  80.             try
  81.             {
  82.                 ReLogger.settings.Tries = int.Parse(TextTries.Text);
  83.             } catch(Exception)
  84.             {
  85.                 ReLogger.settings.Tries = 10;
  86.             }
  87.             ReLogger.settings.Save();
  88.             base.Dispose(disposing);
  89.         }
  90.     }
  91.  
  92.     #endregion
  93.  
  94.     #region settings
  95.  
  96.     class ReloggerSettings : Settings
  97.     {
  98.         public ReloggerSettings() : base(Logging.ApplicationPath + "\\Settings\\Relogger_" + StyxWoW.Me.Name + ".xml")
  99.         {
  100.             Load();
  101.         }
  102.  
  103. [Setting, DefaultValue("")]
  104.         public string Username { get; set; }
  105.  
  106. [Setting, DefaultValue("")]
  107.         public string Password { get; set; }
  108.  
  109. [Setting, DefaultValue("")]
  110.         public string Account { get; set; }
  111.  
  112. [Setting, DefaultValue("")]
  113.         public string Realm { get; set; }
  114.  
  115. [Setting, DefaultValue("")]
  116.         public string Character { get; set; }
  117.  
  118. [Setting, DefaultValue(10)]
  119.         public int Tries { get; set; }
  120.     }
  121.  
  122.     #endregion
  123.  
  124.     #region relogger
  125.  
  126.     class ReLogger : HBPlugin
  127.     {
  128.  
  129.         #region variables
  130.  
  131.         public static ReloggerSettings settings = new ReloggerSettings();
  132.         private Thread _t;
  133.         private bool wasloggedout = false;
  134.  
  135.         #endregion
  136.  
  137.         #region lua
  138.  
  139.         #region is
  140.  
  141.         private bool IsLoginScreen() {
  142.             return Lua.GetReturnVal<bool>("if AccountLoginUI then return AccountLoginUI:IsVisible() else return false end ", 0);
  143.         }
  144.  
  145.         private bool IsAccountSelectScreen()
  146.         {
  147.             return Lua.GetReturnVal<bool>("if WoWAccountSelectDialog then return WoWAccountSelectDialog:IsShown() else return false end ", 0);
  148.         }
  149.  
  150.         private bool IsRealmSelectScreen()
  151.         {
  152.             return Lua.GetReturnVal<bool>("if RealmList then return RealmList:IsVisible() else return false end ", 0);
  153.         }
  154.  
  155.         private bool IsCharacterSelectScreen()
  156.         {
  157.             return Lua.GetReturnVal<bool>("if CharacterSelectUI then return CharacterSelectUI:IsVisible() else return false end ", 0);
  158.         }
  159.  
  160.         #endregion
  161.  
  162.         #region do
  163.  
  164.         private bool DoHideFrame(string frame)
  165.         {
  166.             bool b = Lua.GetReturnVal<bool>("local b = false " +
  167.                                             "if " + frame + " and " + frame + ":IsVisible() then " +
  168.                                                 " b = 1 " +
  169.                                                 frame + ":Hide() " +
  170.                                             "end " +
  171.                                             "return b ", 0);
  172.             return b;
  173.         }
  174.  
  175.         private void DoLogin()
  176.         {
  177.             Lua.DoString("DefaultServerLogin(\"" + settings.Username + "\", \"" + settings.Password + "\")");
  178.         }
  179.  
  180.         private bool DoAccountSelect()
  181.         {
  182.             bool b = Lua.GetReturnVal<bool>
  183.                         ("local b = false " +
  184.                         "for i = 1, GetNumGameAccounts() do " +
  185.                             "if string.lower(GetGameAccountInfo(i)) == string.lower(\"" + settings.Account + "\") or i == " + settings.Account + " then " +
  186.                                 "WoWAccountSelect_SelectAccount(i) " +
  187.                                 "b = 1 " +
  188.                                 "break " +
  189.                             "end " +
  190.                         "end " +
  191.                         "return b; ", 0);
  192.             return b;
  193.         }
  194.  
  195.         private bool DoRealmSelect()
  196.         {
  197.             string[] s = settings.Realm.Split(',');
  198.             bool b = Lua.GetReturnVal<bool>
  199.                         ("local b = false " +
  200.                          "for i = 1, select('#',GetRealmCategories()) do " +
  201.                             "for j = 1, GetNumRealms(i) do " +
  202.                             "if string.lower(GetRealmInfo(i, j)) == string.lower(\"" + settings.Realm + "\") or (j == " + (s.Length > 0 ? s[0] : "-1") + " and i == " + (s.Length > 1 ? s[1] : "-1") + ") then " +
  203.                                     "RealmList:Hide() " +
  204.                                     "ChangeRealm(i, j) " +
  205.                                     "b = 1 " +
  206.                                     "break " +
  207.                                 "end " +
  208.                              "end " +
  209.                          "end " +
  210.                          "return b; ", 0);
  211.             return b;
  212.         }
  213.  
  214.         private bool DoCharacterSelect()
  215.         {
  216.             bool b = Lua.GetReturnVal<bool>
  217.                        ("local b = false " +
  218.                         "if string.lower(GetServerName()) == string.lower(\"" + settings.Realm + "\") or tonumber(\"" + settings.Realm + "\") ~= nil then " +
  219.                             "if GetNumCharacters() > 0 then " +
  220.                                 "for i = 1,GetNumCharacters() do " +
  221.                                     "if string.lower(GetCharacterInfo(i)) == string.lower(\"" + settings.Character + "\") or i == " + settings.Character + " then " +
  222.                                         "CharacterSelect_SelectCharacter(i) " +
  223.                                         "EnterWorld() " +
  224.                                         "b = 1 " +
  225.                                         "break " +
  226.                                     "end " +
  227.                                 "end " +
  228.                             "end " +
  229.                         "else " +
  230.                             "RequestRealmList(1) " +
  231.                             "b = 1 " +
  232.                         "end " +
  233.                         "return b; ", 0);
  234.             return b;
  235.         }
  236.  
  237.         #endregion
  238.  
  239.         #endregion
  240.  
  241.         #region helper
  242.  
  243.         private void log(string s)
  244.         {
  245.             Logging.Write(Color.DarkCyan, "[" + Name + "] " + s);
  246.         }
  247.  
  248.         #endregion
  249.        
  250.         #region overrides
  251.  
  252.         public override void Dispose()
  253.         {
  254.             if (_t != null)
  255.             {
  256.                 log("Cleaning up");
  257.                 try
  258.                 {
  259.                     _t.Interrupt();
  260.                     _t.Abort();
  261.                 }
  262.                 catch (Exception) { }
  263.                 _t = null;
  264.             }
  265.         }
  266.  
  267.         public override string Author
  268.         {
  269.             get { return "eXemplar"; }
  270.         }
  271.  
  272.         public override string Name
  273.         {
  274.             get { return "ReLogger"; }
  275.         }
  276.  
  277.         public override System.Version Version
  278.         {
  279.             get { return new System.Version(1, 5); }
  280.         }
  281.  
  282.         public override void Pulse()
  283.         {
  284.             if (IsPluginEnabled())
  285.             {
  286.                 if (_t == null || !_t.IsAlive)
  287.                 {
  288.                     log("Creating thread");
  289.                     _t = new Thread(new ThreadStart(run));
  290.                     _t.IsBackground = true;
  291.                     _t.Start();
  292.                 }
  293.             }
  294.             else
  295.             {
  296.                 if (_t != null && _t.IsAlive)
  297.                 {
  298.                     log("Stoppping thread");
  299.                     _t.Interrupt();
  300.                     //_t.Abort();
  301.                 }
  302.             }
  303.         }
  304.  
  305.         public override bool WantButton
  306.         {
  307.             get
  308.             {
  309.                 Pulse();
  310.                 return IsPluginEnabled();
  311.             }
  312.         }
  313.  
  314.         public override string ButtonText
  315.         {
  316.             get
  317.             {
  318.                 return "Settings";
  319.             }
  320.         }
  321.  
  322.         public override void OnButtonPress()
  323.         {
  324.             new SettingsForm().Show();
  325.         }
  326.  
  327.         #endregion
  328.  
  329.         #region thread
  330.  
  331.         private void run()
  332.         {
  333.             int tries = 0;
  334.             while (IsPluginEnabled())
  335.             {
  336.                 try
  337.                 {
  338.                     Thread.Sleep(10000);
  339.                     if (DoHideFrame("ScriptErrorsFrame"))
  340.                     {
  341.                         log("Hiding ScriptErrorsFrame");
  342.                     }
  343.                     if (StyxWoW.IsInWorld)
  344.                     {
  345.                         if (wasloggedout && !TreeRoot.IsRunning)
  346.                         {
  347.                             log("Restarting Bot");
  348.                             wasloggedout = false;
  349.                             TreeRoot.Start();
  350.                             continue;
  351.                         }
  352.                         if (tries > 0)
  353.                         {
  354.                             log("Resetting tries");
  355.                             tries = 0;
  356.                         }
  357.                     }
  358.                     if (StyxWoW.IsInGame)
  359.                     {
  360.                         continue;
  361.                     }
  362.                     wasloggedout = true;
  363.                     if (tries >= settings.Tries)
  364.                     {
  365.                         log("Too many tries, aborting - " + tries);
  366.                         GetPlugin().Enabled = false;
  367.                         break;
  368.                     }
  369.                     if (TreeRoot.IsRunning)
  370.                     {
  371.                         log("Stopping Bot");
  372.                         TreeRoot.Stop();
  373.                         continue;
  374.                     }
  375.                     if (DoHideFrame("StaticPopup1"))
  376.                     {
  377.                         log("Hiding StaticPopup");
  378.                         continue;
  379.                     }
  380.                     if (IsAccountSelectScreen())
  381.                     {
  382.                         log("Account selection - " + settings.Account);
  383.                         if (DoAccountSelect())
  384.                         {
  385.                             log("Account selection - OK!");
  386.                         }
  387.                         else
  388.                         {
  389.                             log("Account selection - ERROR!");
  390.                             GetPlugin().Enabled = false;
  391.                             break;
  392.                         }
  393.                     }
  394.                     else if (IsLoginScreen())
  395.                     {
  396.                         tries++;
  397.                         log("Logging in - " + settings.Username + " (" + tries + ")");
  398.                         DoLogin();
  399.                     }
  400.                     else if (IsRealmSelectScreen())
  401.                     {
  402.                         log("Realm selection - " + settings.Realm);
  403.                         if (DoRealmSelect())
  404.                         {
  405.                             log("Realm selection - OK!");
  406.                         }
  407.                         else
  408.                         {
  409.                             log("Realm selection - ERROR!");
  410.                             GetPlugin().Enabled = false;
  411.                             break;
  412.                         }
  413.                     }
  414.                     else if (IsCharacterSelectScreen())
  415.                     {
  416.                         log("Character selection - " + settings.Character);
  417.                         if (DoCharacterSelect())
  418.                         {
  419.                             log("Character selection - OK!");
  420.                         }
  421.                         else
  422.                         {
  423.                             log("Character selection - ERROR!");
  424.                             GetPlugin().Enabled = false;
  425.                             break;
  426.                         }
  427.                     }
  428.                 }
  429.                 catch (Exception) { }
  430.             }
  431.             log("Thread finished");
  432.         }
  433.  
  434.         #endregion
  435.  
  436.         #region plugin
  437.  
  438.         private PluginContainer GetPlugin() {
  439.             foreach (PluginContainer p in PluginManager.Plugins)
  440.             {
  441.                 if (p != null && p.Name == Name)
  442.                 {
  443.                     return p;
  444.                 }
  445.             }
  446.             return null;
  447.         }
  448.  
  449.         private bool IsPluginEnabled()
  450.         {
  451.             PluginContainer p = GetPlugin();
  452.             if (p == null)
  453.             {
  454.                 return false;
  455.             }
  456.             return p.Enabled;
  457.         }
  458.  
  459.         #endregion
  460.  
  461.     }
  462.  
  463.     #endregion
  464.  
  465. }
Add Comment
Please, Sign In to add comment