/// <summary> /// Constructor for this class. /// </summary> /// <param name="key">The key to check for.</param> /// <param name="script">The script to execute when the key is pressed.</param> /// <param name="runAsync">Whether to run the script asynchronously.</param> public Action(Keys key, Objects.Script script, bool runAsync, bool keyDown = true) { this.Key = key; this.Script = script; this.RunAsynchronous = runAsync; this.KeyDown = keyDown; }
private void btnRemoveScript_Click(object sender, EventArgs e) { if (this.datagridScripts.SelectedRows.Count <= 0) { return; } Objects.Script script = (Objects.Script)datagridScripts.SelectedRows[0].Cells[0].Value; this.Client.Modules.ScriptManager.RemoveScript(script); }
void ScriptManager_ScriptFinished(Objects.Script script) { var row = this.GetRow(script); if (row != null) { row.Cells[1].Value = script.IsRunning; } }
void ScriptManager_ScriptRemoved(Objects.Script script) { var row = this.GetRow(script); if (row != null) { this.datagridScripts.Rows.Remove(row); } }
private DataGridViewRow GetRow(Objects.Script script) { foreach (DataGridViewRow row in this.datagridScripts.Rows) { Objects.Script s = (Objects.Script)row.Cells[0].Value; if (s == script) { return(row); } } return(null); }
private void txtboxAction_Click(object sender, EventArgs e) { OpenFileDialog openFile = new OpenFileDialog(); openFile.Filter = "C# code files (*.cs)|*.cs|All files (*.*)|*.*"; openFile.Title = "Choose a C# code file"; openFile.InitialDirectory = Application.StartupPath; if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK) { this.currentScript = new Objects.Script(this.Client, new FileInfo(openFile.FileName)); txtboxScriptPath.Text = this.currentScript.ScriptFile.Name; } }
private void btnStartStopScript_Click(object sender, EventArgs e) { if (this.datagridScripts.SelectedRows.Count <= 0) { return; } Objects.Script script = (Objects.Script)datagridScripts.SelectedRows[0].Cells[0].Value; if (!script.IsRunning) { script.Run(true); } else { script.Stop(); } }
private void btnNewScript_Click(object sender, EventArgs e) { OpenFileDialog openFile = new OpenFileDialog(); openFile.Filter = "C# code files (*.cs)|*.cs|All files (*.*)|*.*"; openFile.Title = "Choose a C# code file to open"; openFile.InitialDirectory = Application.StartupPath; openFile.Multiselect = true; if (openFile.ShowDialog() == DialogResult.OK) { foreach (string str in openFile.FileNames) { Objects.Script script = new Objects.Script(this.Client, new FileInfo(str)); this.Client.Modules.ScriptManager.AddScript(script); } } }
/// <summary> /// Executes the script. /// </summary> /// <param name="asynchronous">Whether to run the script on another thread or not.</param> public void Run(bool asynchronous) { if (this.Code.Length <= 0) { return; } this.FileInfo = this.Parent.Client.Modules.CacheManager.SetFile(new Random().Next() + "tempcavebotscript.cs", System.Text.Encoding.UTF8.GetBytes(this.Code)); this.ScriptPlaceholder = new Objects.Script(this.Parent.Client, this.FileInfo); this.ScriptPlaceholder.Finished += new Objects.Script.ScriptFinishedHandler(delegate(Objects.Script script) { this.FileInfo.Delete(); this.ScriptPlaceholder = null; }); if (!this.ScriptPlaceholder.Run(asynchronous)) { this.FileInfo.Delete(); } }
void ScriptManager_ScriptAdded(Objects.Script script) { this.datagridScripts.Rows.Add(script, script.IsRunning); }
/// <summary> /// Executes the script. /// </summary> /// <param name="asynchronous">Whether to run the script on another thread or not.</param> public void Run(bool asynchronous) { if (this.Code.Length <= 0) return; this.FileInfo = this.Parent.Client.Modules.CacheManager.SetFile(new Random().Next() + "tempcavebotscript.cs", System.Text.Encoding.UTF8.GetBytes(this.Code)); this.ScriptPlaceholder = new Objects.Script(this.Parent.Client, this.FileInfo); this.ScriptPlaceholder.Finished += new Objects.Script.ScriptFinishedHandler(delegate(Objects.Script script) { this.FileInfo.Delete(); this.ScriptPlaceholder = null; }); if (!this.ScriptPlaceholder.Run(asynchronous)) this.FileInfo.Delete(); }
/// <summary> /// Loads data (waypoints, loot, targets, settings) from a file. /// </summary> /// <param name="file">The file to load.</param> /// <returns>Returns true if file is found and is valid, false if not.</returns> public bool Load(FileInfo file) { if (!file.Exists) return false; if (file.Extension.ToLower() == ".cs") { Objects.Script script = new Objects.Script(this.Client, file); this.Clear(); script.Run(); return true; } else { Objects.Packet p = new Objects.Packet(); using (FileStream fstream = file.OpenRead()) { byte[] buffer = new byte[fstream.Length]; fstream.Read(buffer, 0, buffer.Length); p = new Objects.Packet(buffer); } // read metadata ushort version = p.GetUInt16(); if (this.Load(version, p)) return true; this.RemoveAllWaypoints(); this.RemoveAllLoot(); this.RemoveAllTargets(); this.CurrentSettings.LoadDefaults(); return false; } }
/// <summary> /// A method that continously updates the player's battlelist address and other things that requires automonity. /// </summary> private void AutomatedChecks() { try { int oldGameWindowMessageIndex = 0; bool hasSetInitScript = false; string oldName = string.Empty; int timeSinceLastFileCheck = 0, timeSinceLastDirectoryCheck = 0; while (true) { Thread.Sleep(1000); if (!this.Player.Connected) { continue; } this.Player.SetBattleListAddress(); foreach (Objects.GameWindow.Message msg in this.Window.GameWindow.GetMessages()) { if (msg.Type != GameWindow.Message.Types.GreenMessage || msg.Index <= oldGameWindowMessageIndex) { continue; } if (msg.Text.StartsWith("You see ")) { this.Window.StatusBar.SetText(this.Memory.ReadUInt16(this.Addresses.UI.LastRightClickedItem) + ":" + this.Memory.ReadUInt16(this.Addresses.UI.LastRightClickedItem + this.Addresses.Item.Distances.Count) + " [" + this.Memory.ReadUInt16(this.Addresses.UI.LastRightClickedItemX) + "," + this.Memory.ReadUInt16(this.Addresses.UI.LastRightClickedItemY) + "," + this.Memory.ReadByte(this.Addresses.UI.LastRightClickedItemZ) + "]"); oldGameWindowMessageIndex = (int)msg.Index; break; } } // create init scripts folder if it doesnt exist #region init script if (Environment.TickCount < timeSinceLastDirectoryCheck + 5000) { continue; } timeSinceLastDirectoryCheck = Environment.TickCount; string dirPath = Path.Combine(this.BotDirectory.FullName, "init"), playerName = this.Player.Name; if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); // generate example file StringBuilder builder = new StringBuilder(); int indent = 0, increment = 4; string indentation = indent.GenerateIndentation(); builder.AppendLine("// this file shows you how you can use the init scripts"); builder.AppendLine("// simply name a script after a player's name, and it'll run once when that player is online"); builder.AppendLine("// this script will attempt to load a script named FoodEater.cs located in <bot directory>/scripts and start it\n"); // generate using statements builder.AppendLine("using System;"); builder.AppendLine("using System.Collections.Generic;"); builder.AppendLine("using KarelazisBot;"); builder.AppendLine("using KarelazisBot.Modules;"); builder.AppendLine("using KarelazisBot.Objects;\n"); // generate class and Main method builder.AppendLine("public class Init\n{"); indent += increment; indentation = indent.GenerateIndentation(); builder.AppendLine(indentation + "public static void Main(Client client)"); builder.AppendLine(indentation + "{"); indent += increment; indentation = indent.GenerateIndentation(); // generate a script builder.AppendLine(indentation + "FileInfo fi = new FileInfo(Path.Combine(client.BotDirectory.FullName, \"scripts\", \"FoodEater.cs\"));"); builder.AppendLine(indentation + "if (fi.Exists)"); builder.AppendLine(indentation + "{"); indent += increment; indentation = indent.GenerateIndentation(); builder.AppendLine(indentation + "var script = client.Modules.ScriptManager.CreateScript(fi);"); builder.AppendLine(indentation + "client.Modules.ScriptManager.AddScript(script);"); builder.AppendLine(indentation + "script.Run(true);"); indent -= increment; indentation = indent.GenerateIndentation(); builder.AppendLine(indentation + "}"); // add closing brackets indent -= increment; indentation = indent.GenerateIndentation(); builder.AppendLine(indentation + "}"); indent -= increment; indentation = indent.GenerateIndentation(); builder.AppendLine(indentation + "}"); File.WriteAllText(Path.Combine(dirPath, "Example.cs"), builder.ToString()); } else if ((oldName != this.Player.Name || !hasSetInitScript) && Environment.TickCount > timeSinceLastFileCheck + 1000 * 5) { timeSinceLastFileCheck = Environment.TickCount; FileInfo fi = new FileInfo(Path.Combine(dirPath, playerName + ".cs")); if (fi.Exists) { oldName = playerName; hasSetInitScript = true; Objects.Script script = new Objects.Script(this, fi); try { script.Run(true); } catch { } } } #endregion } } catch (Exception ex) { File.AppendAllText("debug-automatedchecks.txt", ex.Message + "\n" + ex.StackTrace + "\n"); } }
internal void Add(System.Windows.Forms.Keys key, Objects.Script script) { this.KeyboardHook.HookedKeys.Add(key); this.KeysActions.Add(key, script); }