Example #1
0
        private void Proxy_PacketRecieved(Client client, Packet packet, string source)
        {
            if (new PacketType[]
            {
                //PacketType.PING,
                //PacketType.PONG,
                PacketType.NEWTICK,
                PacketType.MOVE,
                //PacketType.TEXT,
                //PacketType.UPDATE,
                //PacketType.UPDATEACK,
                //PacketType.SHOWEFFECT
            }.Contains(packet.Type))
            {
                return;
            }

            PluginUtils.Log(
                source + " Packet",
                packet.Type.ToString() + "  |  " + DateTime.Now.ToString("HH:mm:ss.fff") + Environment.NewLine +
                packet.GetFields() + Environment.NewLine);
        }
Example #2
0
        /// <summary>
        /// Starts a client listener on 127.0.0.1:2050.
        /// Fires the ProxyListenStarted event if successful.
        /// </summary>
        public void Start()
        {
            PluginUtils.Log("Listener", "Starting local listener...");

            bool success = PluginUtils.ProtectedInvoke(() =>
            {
                _localListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 2050);
                _localListener.Start();
                _localListener.BeginAcceptTcpClient(LocalConnect, null);
                PluginUtils.Log("Listener", "Local listener started.");
            }, "ClientListenerStart");

            if (!success)
            {
                return;
            }

            PluginUtils.ProtectedInvoke(() =>
            {
                ProxyListenStarted?.Invoke(this);
            }, "ProxyListenStarted");
        }
Example #3
0
        public void Initialize(Proxy proxy)
        {
            targets         = new List <Target> ();
            playerPositions = new Dictionary <int, Target> ();
            portals         = new List <Portal> ();
            enemies         = new List <Enemy> ();
            obstacles       = new List <Obstacle> ();

            obstacleIds = new List <ushort> ();
            GameData.Objects.Map.ForEach((kvp) => {
                if (kvp.Value.FullOccupy || kvp.Value.OccupySquare)
                {
                    obstacleIds.Add(kvp.Key);
                }
            });
            PluginUtils.Log("FameBot", "Found {0} obstacles.", obstacleIds.Count);

            gui = new FameBotGUI();
            PluginUtils.ShowGUI(gui);

            config = ConfigManager.GetConfiguration();

            Process[] processes = Process.GetProcessesByName(config.FlashPlayerName);
            if (processes.Length == 1)
            {
                Log("Automatically bound to client.");
                flashPtr = processes[0].MainWindowHandle;
                gui?.SetHandle(flashPtr);
                if (config.AutoConnect)
                {
                    Start();
                }
            }
            else if (processes.Length > 1)
            {
                Log("Multiple flash players running. Use the /bind command on the client you want to use.");
            }
            else
            {
                Log("Couldn't find flash player. Use the /bind command in game, then start the bot.");
            }

            #region Proxy Hooks
            proxy.HookCommand("bind", ReceiveCommand);
            proxy.HookCommand("start", ReceiveCommand);
            proxy.HookCommand("gui", ReceiveCommand);
            proxy.HookCommand("famebot", ReceiveCommand);
            proxy.HookCommand("stop1", ReceiveCommand);
            proxy.HookCommand("options", ReceiveCommand);
            proxy.HookPacket(PacketType.RECONNECT, OnReconnect);
            proxy.HookPacket(PacketType.UPDATE, OnUpdate);
            proxy.HookPacket(PacketType.NEWTICK, OnNewTick);
            proxy.HookPacket(PacketType.PLAYERHIT, OnHit);
            proxy.HookPacket(PacketType.MAPINFO, OnMapInfo);
            proxy.HookPacket(PacketType.TEXT, OnText);
            proxy.HookPacket(PacketType.GOTOACK, (client, packet) => {
                if (blockNextAck)
                {
                    packet.Send  = false;
                    blockNextAck = false;
                }
            });
            #endregion

            proxy.ClientConnected += (client) => {
                connectedClient = client;
                targets.Clear();
                playerPositions.Clear();
                enemies.Clear();
                obstacles.Clear();
                followTarget = false;
                isInNexus    = false;
                ResetAllKeys();
            };

            proxy.ClientDisconnected += (client) => {
                Log("Client disconnected. Waiting a few seconds before trying to press play...");
                PressPlay();
            };

            guiEvent += (evt) => {
                switch (evt)
                {
                case GuiEvent.StartBot:
                    Start();
                    break;

                case GuiEvent.StopBot:
                    Stop();
                    break;

                case GuiEvent.SettingsChanged:
                    Log("Updated config");
                    config = ConfigManager.GetConfiguration();
                    break;
                }
            };

            sendMessage += (message) => {
                if (!(connectedClient?.Connected ?? false))
                {
                    return;
                }
                PlayerTextPacket packet = (PlayerTextPacket)Packet.Create(PacketType.PLAYERTEXT);
                packet.Text = message;
                connectedClient.SendToServer(packet);
            };

            proxy.HookPacket(PacketType.FAILURE, (client, packet) => {
                FailurePacket p = packet as FailurePacket;

                if (p.ErrorId != 9)
                {
                    Console.WriteLine("<FAILURE> ID: " + p.ErrorId + " Message: " + p.ErrorMessage);
                }

                if (p.ErrorMessage == "{\"key\":\"server.realm_full\"}")
                {
                    Attempts++;
                    if (Attempts >= 2 && bestName != "")
                    {
                        _bestName = bestName;
                        ReconnectPacket reconnect = (ReconnectPacket)Packet.Create(PacketType.RECONNECT);
                        reconnect.Name            = "{\"text\":\"server.realm_of_the_mad_god\"}";
                        reconnect.Host            = "";
                        reconnect.Stats           = "";
                        reconnect.Port            = -1;
                        reconnect.GameId          = -3;
                        reconnect.KeyTime         = -1;
                        reconnect.IsFromArena     = false;
                        reconnect.Key             = new byte[0];

                        connectedClient.SendToClient(reconnect);
                        Attempts        = 0;
                        loopingforrealm = true;
                    }
                }
            });

            proxy.HookPacket(PacketType.CREATESUCCESS, (client, packet) => {
                target_ = null;
                if (enabled)
                {
                    PluginUtils.Delay(200, () => Stop());
                    PluginUtils.Delay(500, () => Stop());
                    PluginUtils.Delay(1300, () => Stop());
                    PluginUtils.Delay(2200, () => Start());
                }
            });
        }
Example #4
0
        public static void Load()
        {
            Parallel.Invoke(
                () =>
            {
                try
                {
                    Items = new GameDataMap <ushort, ItemStructure>(ItemStructure.Load(XDocument.Load("Objects.xml")));
                    PluginUtils.Log("GameData", "loaded items from file!");
                }
                catch
                {
                    Items = new GameDataMap <ushort, ItemStructure>(ItemStructure.Load(XDocument.Parse(RawObjectsXML)));
                }
                PluginUtils.Log("GameData", "Mapped {0} items.", Items.Map.Count);
            },
                () =>
            {
                try
                {
                    Tiles = new GameDataMap <ushort, TileStructure>(TileStructure.Load(XDocument.Load("Tiles.xml")));
                    PluginUtils.Log("GameData", "loaded tiles from file!");
                }
                catch
                {
                    Tiles = new GameDataMap <ushort, TileStructure>(TileStructure.Load(XDocument.Parse(RawTilesXML)));
                }
                PluginUtils.Log("GameData", "Mapped {0} tiles.", Tiles.Map.Count);
            },
                () =>
            {
                try
                {
                    Objects = new GameDataMap <ushort, ObjectStructure>(ObjectStructure.Load(XDocument.Load("Objects.xml")));
                    PluginUtils.Log("GameData", "loaded objects from file!");
                }
                catch
                {
                    Objects = new GameDataMap <ushort, ObjectStructure>(ObjectStructure.Load(XDocument.Parse(RawObjectsXML)));
                }
                PluginUtils.Log("GameData", "Mapped {0} objects.", Objects.Map.Count);
            },
                () =>
            {
                try
                {
                    Packets = new GameDataMap <byte, PacketStructure>(PacketStructure.Load(XDocument.Load("Packets.xml")));
                    PluginUtils.Log("GameData", "loaded packets from file!");
                }
                catch
                {
                    Packets = new GameDataMap <byte, PacketStructure>(PacketStructure.Load(XDocument.Parse(RawPacketsXML)));
                }
                PluginUtils.Log("GameData", "Mapped {0} packets.", Packets.Map.Count);
            },
                () =>
            {
                const string CHAR_LIST_FILE = "char_list.xml";

                XDocument charList = null;

                try
                {
                    charList = XDocument.Load("http://realmofthemadgodhrd.appspot.com/char/list");
                }
                catch (Exception)
                {
                }

                if (charList != null && charList.Element("Error") == null)
                {
                    charList.Save(CHAR_LIST_FILE);
                }
                else if (System.IO.File.Exists(CHAR_LIST_FILE))
                {
                    charList = XDocument.Load(CHAR_LIST_FILE);
                }
                else
                {
                    PluginUtils.Log("GameData", "Error! Unable to retrieve server list.");
                    return;
                }

                Servers = new GameDataMap <string, ServerStructure>(ServerStructure.Load(charList));
                PluginUtils.Log("GameData", "Mapped {0} servers.", Servers.Map.Count);
            });

            PluginUtils.Log("GameData", "Successfully loaded game data.");
        }
Example #5
0
        public static void Load()
        {
            bool hadError = false;

            Parallel.Invoke(
                () =>
            {
                try
                {
                    Items = new GameDataMap <ushort, ItemStructure>(ItemStructure.Load(XDocument.Load("Objects.xml")));
                    PluginUtils.Log("GameData", "Mapped {0} items.", Items.Map.Count);
                }
                catch (Exception e)
                {
                    hadError = true;
                    PluginUtils.Log("GameData", "Error while reading Objects.xml file: {0}", e.Message);
                }
            },
                () =>
            {
                try
                {
                    Tiles = new GameDataMap <ushort, TileStructure>(TileStructure.Load(XDocument.Load("Tiles.xml")));
                    PluginUtils.Log("GameData", "Mapped {0} tiles.", Tiles.Map.Count);
                }
                catch (Exception e)
                {
                    hadError = true;
                    PluginUtils.Log("GameData", "Error while reading Tiles.xml file: {0}", e.Message);
                }
            },
                () =>
            {
                try
                {
                    Objects = new GameDataMap <ushort, ObjectStructure>(ObjectStructure.Load(XDocument.Load("Objects.xml")));
                    PluginUtils.Log("GameData", "Mapped {0} objects.", Objects.Map.Count);
                }
                catch (Exception e)
                {
                    hadError = true;
                    PluginUtils.Log("GameData", "Error while reading Objects.xml file: {0}", e.Message);
                }
            },
                () =>
            {
                try
                {
                    Packets = new GameDataMap <byte, PacketStructure>(PacketStructure.Load(XDocument.Load("Packets.xml")));
                    PluginUtils.Log("GameData", "Mapped {0} packets.", Packets.Map.Count);
                }
                catch (Exception e)
                {
                    hadError = true;
                    PluginUtils.Log("GameData", "Error while reading Packets.xml file: {0}", e.Message);
                }
            },
                () =>
            {
                const string CHAR_LIST_FILE = "char_list.xml";

                XDocument charList = null;

                try
                {
                    charList = XDocument.Load("https://www.realmofthemadgod.com/char/list");
                }
                catch (Exception e)
                {
                    hadError = true;
                    PluginUtils.Log("GameData", "Error while fetching /char/list: {0}", e.Message);
                }

                // If the char list doesn't contain an error
                if (charList != null && charList.Element("Error") == null)
                {
                    // Make a backup of the char list
                    charList.Save(CHAR_LIST_FILE);
                }
                // If the backup char list file exists
                else if (System.IO.File.Exists(CHAR_LIST_FILE))
                {
                    charList = XDocument.Load(CHAR_LIST_FILE);
                }
                // The retrieved char list contains an error and a backup char list doesn't exist
                else
                {
                    PluginUtils.Log("GameData", "Cannot retrieve server list and no cache exists.");
                    hadError = true;
                    return;
                }

                Servers = new GameDataMap <string, ServerStructure>(ServerStructure.Load(charList));
                PluginUtils.Log("GameData", "Mapped {0} servers.", Servers.Map.Count);
            });

            if (hadError)
            {
                PluginUtils.Log("GameData", "One or more game data files could not be loaded.");
            }
            else
            {
                PluginUtils.Log("GameData", "Successfully loaded game data.");
            }
        }
Example #6
0
 // Send the log message to the K-Relay log with this plugin's name as a source
 private void Log(string text) => PluginUtils.Log(GetName(), text);
Example #7
0
        public void Initialize(Proxy proxy)
        {
            // Initialize lists so they are empty instead of null.
            targets         = new List <Target>();
            playerPositions = new Dictionary <int, Target>();
            portals         = new List <Portal>();
            enemies         = new List <Enemy>();
            obstacles       = new List <Obstacle>();

            // get obstacles
            obstacleIds = new List <ushort>();
            GameData.Objects.Map.ForEach((kvp) =>
            {
                if (kvp.Value.FullOccupy || kvp.Value.OccupySquare)
                {
                    obstacleIds.Add(kvp.Key);
                }
            });
            PluginUtils.Log("FameBot", "Found {0} obstacles.", obstacleIds.Count);

            // Initialize and display gui.
            gui = new FameBotGUI();
            PluginUtils.ShowGUI(gui);

            // Get the config.
            config = ConfigManager.GetConfiguration();

            // Look for all processes with the configured flash player name.
            Process[] processes = Process.GetProcessesByName(config.FlashPlayerName);
            if (processes.Length == 1)
            {
                // If there is one client open, bind to it.
                Log("Automatically bound to client.");
                flashPtr = processes[0].MainWindowHandle;
                gui?.SetHandle(flashPtr);
                if (config.AutoConnect)
                {
                    Start();
                }
            }
            // If there are multiple or no clients running, log a message.
            else if (processes.Length > 1)
            {
                Log("Multiple flash players running. Use the /bind command on the client you want to use.");
            }
            else
            {
                Log("Couldn't find flash player. Use the /bind command in game, then start the bot.");
            }

            #region Proxy Hooks
            proxy.HookCommand("bind", ReceiveCommand);
            proxy.HookCommand("start", ReceiveCommand);
            proxy.HookCommand("gui", ReceiveCommand);
            proxy.HookCommand("famebot", ReceiveCommand);

            proxy.HookPacket(PacketType.UPDATE, OnUpdate);
            proxy.HookPacket(PacketType.NEWTICK, OnNewTick);
            proxy.HookPacket(PacketType.PLAYERHIT, OnHit);
            proxy.HookPacket(PacketType.MAPINFO, OnMapInfo);
            proxy.HookPacket(PacketType.TEXT, OnText);
            proxy.HookPacket(PacketType.GOTOACK, (client, packet) =>
            {
                if (blockNextAck)
                {
                    packet.Send  = false;
                    blockNextAck = false;
                }
            });
            #endregion

            // Runs every time a client connects.
            proxy.ClientConnected += (client) =>
            {
                // Clear all lists and reset keys.
                connectedClient = client;
                targets.Clear();
                playerPositions.Clear();
                enemies.Clear();
                obstacles.Clear();
                followTarget = false;
                isInNexus    = false;
                ResetAllKeys();
            };

            // Runs every time a client disconnects.
            proxy.ClientDisconnected += (client) =>
            {
                Log("Client disconnected. Waiting a few seconds before trying to press play...");
                PressPlay();
            };

            guiEvent += (evt) =>
            {
                switch (evt)
                {
                case GuiEvent.StartBot:
                    Start();
                    break;

                case GuiEvent.StopBot:
                    Stop();
                    break;

                case GuiEvent.SettingsChanged:
                    Log("Updated config");
                    config = ConfigManager.GetConfiguration();
                    break;
                }
            };

            // Send an in game message when the gui fires the event.
            sendMessage += (message) =>
            {
                if (!(connectedClient?.Connected ?? false))
                {
                    return;
                }
                PlayerTextPacket packet = (PlayerTextPacket)Packet.Create(PacketType.PLAYERTEXT);
                packet.Text = message;
                connectedClient.SendToServer(packet);
            };
        }
Example #8
0
        public static void Load()
        {
            List <string> errors = new List <string>();

            Parallel.Invoke(
                () => {
                try
                {
                    Items = new GameDataMap <ushort, ItemStructure>(ItemStructure.Load(XDocument.Load(Path.Combine("Resources", "Objects.xml"))));
                } catch
                {
                    // If Objects.xml is missing the error is reported later on, so don't report it here.
                    Items = new GameDataMap <ushort, ItemStructure>(ItemStructure.Load(XDocument.Parse(RawObjectsXML)));
                }
                PluginUtils.Log("GameData", "Mapped {0} items.", Items.Map.Count);
            },
                () => {
                try
                {
                    Tiles = new GameDataMap <ushort, TileStructure>(TileStructure.Load(XDocument.Load(Path.Combine("Resources", "Tiles.xml"))));
                    PluginUtils.Log("GameData", "Loaded Tiles from Tiles.xml");
                }
                catch (Exception e)
                {
                    errors.Add(string.Format("(Tiles.xml) {0}", e.Message));
                    PluginUtils.Log("GameData", "Using Tiles.xml resource fallback.");
                    Tiles = new GameDataMap <ushort, TileStructure>(TileStructure.Load(XDocument.Parse(RawTilesXML)));
                }
                PluginUtils.Log("GameData", "Mapped {0} tiles.", Tiles.Map.Count);
            },
                () => {
                try
                {
                    Objects = new GameDataMap <ushort, ObjectStructure>(ObjectStructure.Load(XDocument.Load(Path.Combine("Resources", "Objects.xml"))));
                    PluginUtils.Log("GameData", "Loaded Objects from Objects.xml");
                }
                catch (Exception e)
                {
                    errors.Add(string.Format("(Objects.xml) {0}", e.Message));
                    PluginUtils.Log("GameData", "Using Objects.xml resource fallback.");
                    Objects = new GameDataMap <ushort, ObjectStructure>(ObjectStructure.Load(XDocument.Parse(RawObjectsXML)));
                }
                PluginUtils.Log("GameData", "Mapped {0} objects.", Objects.Map.Count);
            },
                () => {
                try
                {
                    Packets = new GameDataMap <byte, PacketStructure>(PacketStructure.Load(XDocument.Load(Path.Combine("Resources", "Packets.xml"))));
                    PluginUtils.Log("GameData", "Loaded Packets from Packets.xml");
                }
                catch (Exception e)
                {
                    errors.Add(string.Format("(Packets.xml) {0}", e.Message));
                    PluginUtils.Log("GameData", "Using Packets.xml resource fallback.");
                    Packets = new GameDataMap <byte, PacketStructure>(PacketStructure.Load(XDocument.Parse(RawPacketsXML)));
                }
                PluginUtils.Log("GameData", "Mapped {0} packets.", Packets.Map.Count);
            },
                () => {
                const string CHAR_LIST_FILE = "char_list.xml";

                XDocument charList = null;

                try
                {
                    charList = XDocument.Load("http://realmofthemadgodhrd.appspot.com/char/list");
                }
                catch (Exception) { }

                // If the char list doesn't contain an error
                if (charList != null && charList.Element("Error") == null)
                {
                    // Make a backup of the char list
                    charList.Save(CHAR_LIST_FILE);
                }
                // If the backup char list file exists
                else if (System.IO.File.Exists(CHAR_LIST_FILE))
                {
                    charList = XDocument.Load(CHAR_LIST_FILE);
                }
                // The retrieved char list contains an error and a backup char list doesn't exist
                else
                {
                    PluginUtils.Log("GameData", "Error! Unable to retrieve server list.");
                    return;
                }

                Servers = new GameDataMap <string, ServerStructure>(ServerStructure.Load(charList));
                PluginUtils.Log("GameData", "Mapped {0} servers.", Servers.Map.Count);
            });

            if (errors.Count == 0)
            {
                PluginUtils.Log("GameData", "Successfully loaded game data.");
            }
            else
            {
                Console.WriteLine();
                PluginUtils.Log("GameData", "{0} Error{1} encountered while loading game data.", errors.Count, errors.Count == 1 ? "" : "s");
                PluginUtils.Log("GameData", "It is recommended to fix these issues before using KRelay.");
                for (int i = 0; i < errors.Count; i++)
                {
                    PluginUtils.Log("GD Error", "\t{0}: {1}", i + 1, errors[i]);
                }
                Console.WriteLine();
            }
        }
Example #9
0
        private void OnMove(Client client, Packet packet)
        {
            if (!_states.ContainsKey(client))
            {
                return;
            }
            LootState state = _states[client];

            foreach (int bagId in state.LootBagItems.Keys)
            {
                float distance = state.LootBagLocations[bagId].DistanceTo(client.PlayerData.Pos);
                if (LootHelperConfig.Default.AutoLoot && Environment.TickCount - state.LastLoot > 900 && distance <= 1)
                {
                    for (int bi = 0; bi < state.LootBagItems[bagId].Length; bi++)
                    {
                        if (state.LootBagItems[bagId][bi] == -1)
                        {
                            continue;
                        }

                        if (!LootHelperConfig.Default.AutoLootList.Contains(ReverseLookup(state.LootBagItems[bagId][bi])))
                        {
                            continue;
                        }

                        state.LastLoot = Environment.TickCount;
                        for (int i = 4; i < client.PlayerData.Slot.Length; i++)
                        {
                            if (client.PlayerData.Slot[i] == -1)
                            {
                                InvSwapPacket invSwap = (InvSwapPacket)Packet.Create(PacketType.INVSWAP);
                                invSwap.Time     = client.Time + 10;
                                invSwap.Position = client.PlayerData.Pos;

                                invSwap.SlotObject1            = new SlotObject();
                                invSwap.SlotObject1.ObjectId   = bagId;
                                invSwap.SlotObject1.SlotId     = (byte)bi;
                                invSwap.SlotObject1.ObjectType = state.LootBagItems[bagId][bi];

                                invSwap.SlotObject2            = new SlotObject();
                                invSwap.SlotObject2.ObjectId   = client.ObjectId;
                                invSwap.SlotObject2.SlotId     = (byte)(i);
                                invSwap.SlotObject2.ObjectType = -1;

                                client.SendToServer(invSwap);
                                PluginUtils.Log("LootHelper", "looted :" + ReverseLookup(state.LootBagItems[bagId][bi]));
                                NotifiedYet = "";
                                break;
                            }
                        }
                    }
                }

                if (LootHelperConfig.Default.LootNotifications && Environment.TickCount - state.LastNotif > 2000 && distance < 15)
                {
                    state.LastNotif = Environment.TickCount;
                    string message = "";

                    foreach (int item in state.LootBagItems[bagId])
                    {
                        if (item != -1)
                        {
                            message += ReverseLookup(item) + "\\n";
                        }
                    }

                    if (message.Length > 3)
                    {
                        client.SendToClient(PluginUtils.CreateNotification(
                                                bagId, LootHelperConfig.Default.NotificationColor.ToArgb(), message));
                        if (NotifiedYet != message)
                        {
                            PluginUtils.Log("LootHelper", DateTime.Now.ToString() + " loot :" + message.Replace("\\n", " "));
                            NotifiedYet = message;
                        }
                    }
                }
            }
        }
Example #10
0
 private static void GameDataLog(string message, params object[] list) => PluginUtils.Log("GameData", message, list);