Пример #1
0
        /// <summary>
        /// Loads log file and adds packets to list.
        /// </summary>
        /// <param name="path"></param>
        private void LoadFile(string path)
        {
            var newPackets = new List <PalePacket>();

            using (var sr = new StreamReader(path))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    line = line.Trim();
                    var recv = false;

                    if (string.IsNullOrWhiteSpace(line) || (!line.StartsWith("Send") && !(recv = line.StartsWith("Recv"))))
                    {
                        continue;
                    }

                    var spaceIdx = line.IndexOf(' ');

                    var date = DateTime.MinValue;
                    if (line[4] == '@')
                    {
                        date = DateTime.Parse(line.Substring(5, spaceIdx - 5));
                    }

                    var packetStr  = line.Substring(spaceIdx + 1, line.Length - spaceIdx - 1);
                    var packetArr  = HexTool.ToByteArray(packetStr);
                    var packet     = new Packet(packetArr, 0);
                    var palePacket = new PalePacket(packet, date, recv);

                    newPackets.Insert(0, palePacket);
                }
            }

            LstPackets.BeginUpdate();

            foreach (var palePacket in newPackets)
            {
                AddPacketToFormList(palePacket, false);
            }

            LstPackets.EndUpdate();

            UpdateCount();

            foreach (var palePacket in newPackets)
            {
                if (palePacket.Received)
                {
                    pluginManager.OnRecv(palePacket);
                }
                else
                {
                    pluginManager.OnSend(palePacket);
                }
            }
        }
Пример #2
0
        private void BtnParseHexPacket_Click(object sender, EventArgs e)
        {
            try
            {
                var packetHex = Clipboard.GetText();
                var bytes     = HexTool.ToByteArray(packetHex.Replace(" ", "").Replace("-", ""));
                var packet    = new Packet(bytes, 0);

                this.ParsePacketToInput(packet);
            }
            catch
            {
                MessageBox.Show("The text in your clipboard is not a valid packet.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Пример #3
0
        private void BtnConvert_Click(object sender, EventArgs e)
        {
            if (TxtHex.Text.Length < 4 + 8 + 3)
            {
                return;
            }

            try
            {
                var packetStr = TxtHex.Text.Replace(" ", "").Replace("-", "").Replace("\r", "").Replace("\n", "");
                var packetArr = HexTool.ToByteArray(packetStr);
                var packet    = new Packet(packetArr, 0);
                TxtPacket.Text = packet.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Пример #4
0
        /// <summary>
        /// Loads log file and adds packets to list.
        /// </summary>
        /// <param name="path"></param>
        private void LoadFile(string path)
        {
            var newPackets = new List <PalePacket>();

            using (var sr = new StreamReader(path))
            {
                string line;
                for (int ln = 1; (line = sr.ReadLine()) != null; ++ln)
                {
                    try
                    {
                        line = line.Trim();
                        var recv = false;

                        if (string.IsNullOrWhiteSpace(line) || (!line.StartsWith("Send ") && !(recv = line.StartsWith("Recv "))))
                        {
                            continue;
                        }

                        var tabIndex = line.IndexOf('\t');
                        var split    = line.Substring(0, tabIndex).Split(' ');

                        PacketType type;
                        DateTime   date;
                        string     name;
                        int        length = -1;

                        if (split.Length == 3)
                        {
                            type = (split[0] == "Send" ? PacketType.ClientServer : PacketType.ServerClient);
                            date = DateTime.Parse(split[1]);
                            name = split[2];
                        }
                        else if (split.Length == 4)
                        {
                            type   = (split[0] == "Send" ? PacketType.ClientServer : PacketType.ServerClient);
                            date   = DateTime.Parse(split[1]);
                            name   = split[2];
                            length = Convert.ToInt32(split[3]);
                        }
                        else
                        {
                            continue;
                        }

                        var packetStr  = line.Substring(tabIndex + 1, line.Length - tabIndex - 1);
                        var packetArr  = HexTool.ToByteArray(packetStr);
                        var packet     = new Packet(packetArr, type);
                        var palePacket = new PalePacket(name, length, packet, date, recv);

                        newPackets.Insert(0, palePacket);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error on line " + ln + ": " + ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }

            LstPackets.BeginUpdate();

            foreach (var palePacket in newPackets)
            {
                AddPacketToFormList(palePacket, false);
            }

            LstPackets.EndUpdate();

            UpdateCount();

            foreach (var palePacket in newPackets)
            {
                if (palePacket.Received)
                {
                    pluginManager.OnRecv(palePacket);
                }
                else
                {
                    pluginManager.OnSend(palePacket);
                }
            }
        }
Пример #5
0
        private Packet GetPacketFromInput()
        {
            if (!this.TryParseNumber(TxtOp.Text, out var op))
            {
                MessageBox.Show("Invalid OP code.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(null);
            }

            if (!this.TryParseNumber(TxtId.Text, out var id))
            {
                MessageBox.Show("Invalid id.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(null);
            }

            var packet = new Packet((int)op, id);

            var i = 0;

            foreach (var line in this.TxtData.Lines)
            {
                i++;

                if (string.IsNullOrWhiteSpace(line))
                {
                    continue;
                }

                var index = line.IndexOf(':');
                if (index == -1)
                {
                    MessageBox.Show($"Invalid data on line {i}: '{line}'.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(null);
                }

                var typeStr = line.Substring(0, index).Trim();
                var dataStr = line.Substring(index + 1).Trim();

                try
                {
                    switch (typeStr.ToLowerInvariant())
                    {
                    case "byte": packet.PutByte((byte)ParseNumber(dataStr)); break;

                    case "short": packet.PutShort((short)ParseNumber(dataStr)); break;

                    case "ushort": packet.PutUShort((ushort)ParseNumber(dataStr)); break;

                    case "int": packet.PutInt((int)ParseNumber(dataStr)); break;

                    case "uint": packet.PutUInt((uint)ParseNumber(dataStr)); break;

                    case "long": packet.PutLong((long)ParseNumber(dataStr)); break;

                    case "ulong": packet.PutULong((ulong)ParseNumber(dataStr)); break;

                    case "float": packet.PutFloat(float.Parse(dataStr)); break;

                    case "string": packet.PutString(dataStr.Replace("\\n", "\n").Replace("\\t", "\t")); break;

                    case "bin": packet.PutBin(HexTool.ToByteArray(dataStr.Replace(" ", "").Replace("-", ""))); break;

                    default: throw new Exception("Invalid type.");
                    }
                }
                catch
                {
                    MessageBox.Show($"Invalid data on line {i}: '{line}'.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(null);
                }
            }

            return(packet);
        }