Ejemplo n.º 1
0
        public static byte[] ToBytes(string packet, ITranslator translator)
        {
            for (int i = 0; i <= 13; i++)
            {
                packet = packet.Replace(
                    "[" + i + "]", ((char)i).ToString());
            }

            if (translator != null)
            {
                MatchCollection constantMatches = _fieldGrabber.Matches(packet);
                foreach (Match match in constantMatches)
                {
                    string field = match.Groups["field"].Value.ToLower();
                    packet = packet.Replace($"%{field}%", translator.Print(field));
                }
            }

            MatchCollection paramMatches = _paramGrabber.Matches(packet);

            foreach (Match match in paramMatches)
            {
                string type  = match.Groups["type"].Value.ToLower();
                string value = match.Groups["value"].Value;

                byte[] data = null;
                if (translator?.IsTranslatingPrimitives ?? false)
                {
                    data = translator.Translate((type.StartsWith("$") ?
                                                 type.Substring(1) : type), value);
                }
                else
                {
                    switch (type)
                    {
                    default:
                    {
                        // Jump over the '$' character.
                        data = translator?.Translate(type.Substring(1), value);
                        break;
                    }

                    case "s":
                    {
                        data = BigEndian.GetBytes(value);
                        break;
                    }

                    case "u":
                    {
                        ushort.TryParse(value, out ushort uValue);
                        data = BigEndian.GetBytes(uValue);
                        break;
                    }

                    case "i":
                    {
                        int.TryParse(value, out int iValue);
                        data = BigEndian.GetBytes(iValue);
                        break;
                    }

                    case "d":
                    {
                        double.TryParse(value, out double dValue);
                        data = BigEndian.GetBytes(dValue);
                        break;
                    }

                    case "b":
                    {
                        if (!byte.TryParse(value, out byte bValue))
                        {
                            data = BigEndian.GetBytes(
                                (value.Trim().ToLower() == "true"));
                        }
                        else
                        {
                            data = new[] { bValue }
                        };
                        break;
                    }
                    }
                }
                if (data != null)
                {
                    packet = packet.Replace(match.Value,
                                            Encoding.GetEncoding(0).GetString(data));
                }
            }
            if (packet.StartsWith("{l}") && packet.Length >= 5)
            {
                byte[] lengthData = BigEndian.GetBytes(packet.Length - 3);
                packet = Encoding.GetEncoding(0).GetString(lengthData) + packet.Substring(3);
            }
            return(Encoding.GetEncoding(0).GetBytes(packet));
        }
Ejemplo n.º 2
0
 public void WriteBoolean(bool value, int position)
 {
     byte[] encoded = BigEndian.GetBytes(value);
     WriteObject(encoded, value, position);
 }
Ejemplo n.º 3
0
 public void WriteDouble(double value, int position)
 {
     byte[] encoded = BigEndian.GetBytes(value);
     WriteObject(encoded, value, position);
 }
Ejemplo n.º 4
0
 public void WriteShort(ushort value, int position)
 {
     byte[] encoded = BigEndian.GetBytes(value);
     WriteObject(encoded, value, position);
 }
Ejemplo n.º 5
0
 public void WriteInteger(int value, int position)
 {
     byte[] encoded = BigEndian.GetBytes(value);
     WriteObject(encoded, value, position);
 }