Example #1
0
 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();
     }
 }
Example #2
0
        /// <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;
            }
        }
Example #3
0
        /// <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"); }
        }