private void menuAttach_Click(object sender, EventArgs e) { // displays the PickWindow form as a modal window and retrieves the picked window PickWindow pw = new PickWindow(); pw.ShowDialog(this); if (pw.PickedWindow != IntPtr.Zero) gamewindow = new GameWindow(pw.PickedWindow); else gamewindow = null; labelGameWindow.Text = "Game window: " + (gamewindow != null ? gamewindow.ToString("X8") : "0"); }
/// <summary> /// Handles a chat line and executes any valid command found. /// </summary> /// <param name="line">A raw chat line in formatted as username\tmessage</param> protected void HandleChatLine(string line) { string msgbody = ""; // will contain the message body string user = ""; // will contain the username GameKey key = GameKey.Invalid; // will contain the requested keystroke if the message is a command int times = 1; // will contain how many times the key will be pressed if the msg is a command int delay = 0; // will contain the duration of the keystroke if the msg is a command string[] splitted = line.Split('\t'); // split username from the message body try { user = splitted[0]; msgbody = splitted[1]; } catch (Exception) { // invalid format return; } try { // throttled commands (start9 & similar) string re1 = "([a-z]{1,9})"; // command (1 to 9 letters) string re2 = "(\\d)"; // any single digit // we're separating the command from the number by using a simple regex Regex r = new Regex(re1 + re2, RegexOptions.IgnoreCase | RegexOptions.Singleline); Match m = r.Match(msgbody); if (m.Success) { msgbody = m.Groups[1].ToString(); String d1 = m.Groups[2].ToString(); times = Convert.ToInt32(d1); if (times < 1) // ignore command0's { return; } if (times > 9) // will never happen { times = 1; } } else // invalid format { times = 1; } } catch (Exception) { // invalid format times = 1; } // check if the command actually exists by searching it sequentially in the listview // TODO: index commands in a dictionary for better performance? // (not really necessary for something this trivial) for (int i = 0; i < listKeyBindings.Items.Count; i++) { var item = listKeyBindings.Items[i]; if (msgbody == item.Text) { // command found! retrieve key and duration key = (GameKey)item.Tag; delay = Convert.ToInt32(item.SubItems[2].Text); break; } } if (key == GameKey.Invalid) // command not found { return; } if (delay <= 0) // invalid delay, should never happen { return; } // should never happen if ResetKeys is properly called when it should if (!st.keypresses.ContainsKey(msgbody)) { st.keypresses.Add(msgbody, 0); } // repeat the keystroke as many times as needed for (int i = 0; i < times; i++) { st.keypresses[msgbody]++; st.totalkeypresses++; if (menuUsePostMessage.Checked) { gamewindow.SendMinimizedKeystroke(key, delay); } else { if (menuUseSendKeys.Checked) { GameWindow.SendGlobalKeystroke(key, delay); } else { GameWindow.SendGlobalKeybdEvent(key, delay); } } } commandsdone++; // this is for the command/s counter // log the command listBoxCommands.Items.Add(user + " " + msgbody + (times > 1 ? times.ToString() : "")); // keep the command log under 100 lines to save RAM if (listBoxCommands.Items.Count > 100) { listBoxCommands.Items.RemoveAt(0); } // autscroll the command log to the bottom listBoxCommands.TopIndex = listBoxCommands.Items.Count - 1; // refresh keypresses counter in the GUI labelKeyPresses.Text = "Keypresses: " + st.totalkeypresses; }