public GameMasterMessagePacket(byte console, string sender, string message)
     : base()
 {
     Console = console;
     Sender = new ArtemisString(sender);
     Message = new ArtemisString(message);
 }
 public CommsIncomingPacket()
 {
     if (_log.IsDebugEnabled)
     {
         _log.DebugFormat("Starting {0}", MethodBase.GetCurrentMethod().ToString());
     }
     Sender  = new ArtemisString();
     Message = new ArtemisString();
     if (_log.IsDebugEnabled)
     {
         _log.DebugFormat("Ending {0}", MethodBase.GetCurrentMethod().ToString());
     }
 }
 public IncomingAudioPacket(byte[] byteArray)
 {
     if (_log.IsInfoEnabled)
     {
         _log.InfoFormat("{0}--bytes in: {1}", MethodBase.GetCurrentMethod().ToString(), Utility.BytesToDebugString(byteArray));
     }
     MessageID = BitConverter.ToInt32(byteArray, 0);
     AudioMode = (AudioModes)BitConverter.ToInt32(byteArray, 4);
     Title     = new ArtemisString(byteArray, 8);
     File      = new ArtemisString(byteArray, 8 + Title.Length);
     if (_log.IsInfoEnabled)
     {
         _log.InfoFormat("{0}--Result bytes: {1}", MethodBase.GetCurrentMethod().ToString(), Utility.BytesToDebugString(this.GetBytes()));
     }
 }
Пример #4
0
        public PlayerShip(byte[] byteArray, int startIndex)
        {
            if (byteArray != null)
            {
                if (_log.IsInfoEnabled)
                {
                    _log.InfoFormat("{0}--bytes in: {1}", MethodBase.GetCurrentMethod().ToString(), Utility.BytesToDebugString(byteArray));
                }


                Unknown0 = BitConverter.ToInt32(byteArray, startIndex);
                if (_log.IsInfoEnabled)
                {
                    _log.InfoFormat("Unknown0={0}", Unknown0.ToString());
                }
                Unknown1 = BitConverter.ToInt32(byteArray, startIndex + 4);
                if (_log.IsInfoEnabled)
                {
                    _log.InfoFormat("Unknown1={0}", Unknown1.ToString());
                }

                Unknown2 = BitConverter.ToInt32(byteArray, startIndex + 8);
                if (_log.IsInfoEnabled)
                {
                    _log.InfoFormat("Unknown2={0}", Unknown2.ToString());
                }
                Name = new ArtemisString(byteArray, startIndex + 12);



                if (_log.IsInfoEnabled)
                {
                    _log.InfoFormat("Name={0}", Name.ToString());
                }



                Length = 16 + Name.Length * 2;
                if (_log.IsInfoEnabled)
                {
                    _log.InfoFormat("Length={0}", Length.ToString());
                }
                if (_log.IsInfoEnabled)
                {
                    _log.InfoFormat("{0}--Result bytes: {1}", MethodBase.GetCurrentMethod().ToString(), Utility.BytesToDebugString(this.GetBytes()));
                }
            }
        }
        public CommsIncomingPacket(byte[] byteArray)
        {
            if (byteArray != null)
            {
                if (_log.IsInfoEnabled)
                {
                    _log.InfoFormat("{0}--bytes in: {1}", MethodBase.GetCurrentMethod().ToString(), Utility.BytesToDebugString(byteArray));
                }

                Priority = BitConverter.ToInt32(byteArray, 0);

                Sender = new ArtemisString(byteArray, 4);
                int newStart = 4 + (Sender.Length * 2) + 4;
                Message = new ArtemisString(byteArray, newStart);
                if (_log.IsInfoEnabled)
                {
                    _log.InfoFormat("{0}--Result bytes: {1}", MethodBase.GetCurrentMethod().ToString(), Utility.BytesToDebugString(this.GetBytes()));
                }
            }
        }
Пример #6
0
        public IntelPacket(byte[] byteArray)
        {
            if (byteArray != null)
            {
#if LOG4NET
                if (_log.IsInfoEnabled)
                {
                    _log.InfoFormat("{0}--bytes in: {1}", MethodBase.GetCurrentMethod().ToString(), Utility.BytesToDebugString(byteArray));
                }
#endif
                ID       = BitConverter.ToInt32(byteArray, 0);
                Unknown2 = byteArray[4];

                Message = new ArtemisString(byteArray, 5);
#if LOG4NET
                if (_log.IsInfoEnabled)
                {
                    _log.InfoFormat("{0}--Result bytes: {1}", MethodBase.GetCurrentMethod().ToString(), Utility.BytesToDebugString(this.GetBytes()));
                }
#endif
            }
        }
        public CommsIncomingPacket() : base()
        {
            Sender = new ArtemisString();
            Message = new ArtemisString();

        }
        private void OnBitConvert(object sender, RoutedEventArgs e)
        {
            BitConverterResult = string.Empty;
            if (IntConvert)
            {
                byte[] bytes = ConvertToByteArray(BitConverterValue);
                if (bytes.Length == 4)
                {
                    BitConverterResult = BitConverter.ToInt32(bytes, 0).ToString(CultureInfo.InvariantCulture);
                }
                else
                {
                    MessageBox.Show("Error: Integer type MUST be 4 bytes long!!", "BitConverter Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                //ConvertToByteArray
            }
            else if (ByteConvert)
            {
                StringBuilder sb = new StringBuilder();
                byte[] bytes = ConvertToByteArray(BitConverterValue);
                foreach (byte b in bytes)
                {
                    sb.Append(b.ToString(CultureInfo.InvariantCulture));
                    sb.Append(", ");
                }
                BitConverterResult = sb.ToString();
            }
            else if (ShortConvert)
            {
                byte[] bytes = ConvertToByteArray(BitConverterValue);
                if (bytes.Length == 2)
                {
                    BitConverterResult = BitConverter.ToInt16(bytes, 0).ToString(CultureInfo.InvariantCulture);
                }
                else
                {
                    MessageBox.Show("Error: Short type MUST be 2 bytes long!!", "BitConverter Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            else if (UIntConvert)
            {
                byte[] bytes = ConvertToByteArray(BitConverterValue);
                if (bytes.Length == 4)
                {
                    BitConverterResult = BitConverter.ToUInt32(bytes, 0).ToString(CultureInfo.InvariantCulture);
                }
                else
                {
                    MessageBox.Show("Error: UInt type MUST be 4 bytes long!!", "BitConverter Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            else if (FloatConvert)
            {
                byte[] bytes = ConvertToByteArray(BitConverterValue);
                if (bytes.Length == 4)
                {
                    BitConverterResult = BitConverter.ToSingle(bytes, 0).ToString(CultureInfo.InvariantCulture);
                }
                else
                {
                    MessageBox.Show("Error: Float type MUST be 4 bytes long!!", "BitConverter Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            else if (UShortConvert)
            {
                byte[] bytes = ConvertToByteArray(BitConverterValue);
                if (bytes.Length == 2)
                {
                    BitConverterResult = BitConverter.ToUInt16(bytes, 0).ToString(CultureInfo.InvariantCulture);
                }
                else
                {
                    MessageBox.Show("Error: UShort type MUST be 2 bytes long!!", "BitConverter Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            else if (StringConvert)
            {
                byte[] bytes = ConvertToByteArray(BitConverterValue);
                if (bytes.Length < 4)
                {
                    MessageBox.Show("Error: string types must include the length as an int type as the first 4 bytes!!",
                      "BitConverter Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else
                {
                    int by = BitConverter.ToInt32(bytes, 0);
                    if (bytes.Length != by * 2 + 4)
                    {
                        MessageBox.Show(
                            string.Format(CultureInfo.InvariantCulture, "Error: String length is defined as {0} characters, but length definition sets it as {1} characters",
                            (bytes.Length - 4) / 2, by), "BitConverter Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    else
                    {
                        using (MemoryStream ms = new MemoryStream(bytes))
                        {
                            using (ArtemisString s = new ArtemisString(ms.GetMemoryStream(0), 0))
                            {
                                BitConverterResult = s.Value;
                            }
                        }
                    }

                }
            }
        }
Пример #9
0
        public byte[] GetBytes()
        {
            List <byte> byteArray = new List <byte>();

            byteArray.AddRange(BitConverter.GetBytes(ID));



            List <byte>         values       = new List <byte>();
            List <PropertyInfo> propertyList = new List <PropertyInfo>();

            PropertyInfo[] properties = this.GetType().GetProperties();
            foreach (PropertyInfo prop in properties)
            {
                if (prop.Name != "ID" && prop.Name != "IncludedFields")
                {
                    propertyList.Add(prop);
                }
            }
            byte position = 1;
            byte workByte = 0;

            for (int i = 0; i < IncludedFields.Length; i++)
            {
                if (IncludedFields[i])
                {
                    if (_log.IsInfoEnabled)
                    {
                        _log.InfoFormat(" Property #{0} included", i.ToString());
                    }
                    workByte += position;
                    PropertyInfo prop = propertyList[i];
                    object       obj  = prop.GetValue(this, null);
                    if (obj != null)
                    {
                        if (prop.PropertyType == typeof(int?))
                        {
                            values.AddRange(BitConverter.GetBytes((int)obj));
                        }
                        if (prop.PropertyType == typeof(ArtemisString))
                        {
                            ArtemisString item = ((ArtemisString)obj);
                            if (item != null)
                            {
                                values.AddRange(item.GetBytes());
                            }
                            else
                            {
                                for (int j = 0; i < 4; i++)
                                {
                                    values.Add(0);
                                }
                            }
                        }
                        if (prop.PropertyType == typeof(float?))
                        {
                            values.AddRange(BitConverter.GetBytes((float)obj));
                        }
                        if (prop.PropertyType == typeof(byte?) || prop.PropertyType == typeof(OrdinanceTypes?))
                        {
                            values.Add((byte)obj);
                        }
                        if (prop.PropertyType == typeof(short?))
                        {
                            values.AddRange(BitConverter.GetBytes((short)obj));
                        }
                        if (prop.PropertyType == typeof(long?))
                        {
                            values.AddRange(BitConverter.GetBytes((long)obj));
                        }
                    }
                }
                position *= 2;
                if (position > 128 || position == 0)
                {
                    byteArray.Add(workByte);
                    if (_log.IsInfoEnabled)
                    {
                        _log.InfoFormat("**Adding Included byte: {0}", workByte.ToString());
                    }
                    position = 1;
                    workByte = 0;
                }
            }
            byteArray.AddRange(values);
            return(byteArray.ToArray());
        }
Пример #10
0
        public static void ProcessVariableData(PropertyInfo[] propertyList, object obj, byte[] sourceData, int index, bool[] IncludedFields)
        {
            if (propertyList != null && obj != null && sourceData != null && IncludedFields != null)
            {
                //@@@@
                int position = index;
                for (int i = 0; i < IncludedFields.Length; i++)
                {
                    if (position > sourceData.Length)
                    {
                        break;
                    }
                    if (IncludedFields[i])
                    {
                        try
                        {
                            PropertyInfo prop = propertyList[i];
                            if (prop.PropertyType == typeof(bool?))
                            {
                                //incoming must be byte--no way to translate other type at this point.
                                bool?boolVal = Convert.ToBoolean(sourceData[position]);
                                if (_log.IsInfoEnabled && boolVal != null)
                                {
                                    _log.InfoFormat("Property {0} set to {1}", prop.Name, boolVal.Value.ToString());
                                }
                                propertyList[i].SetValue(obj, boolVal, null);
                                position += 1;
                            }
                            if (prop.PropertyType == typeof(int?))
                            {
                                if (position + 4 < sourceData.Length)
                                {
                                    int?intVal = BitConverter.ToInt32(sourceData, position);
                                    if (_log.IsInfoEnabled && intVal != null)
                                    {
                                        _log.InfoFormat("Property {0} set to {1}", prop.Name, intVal.Value.ToString());
                                    }
                                    propertyList[i].SetValue(obj, intVal, null);
                                }
                                position += 4;
                            }
                            if (prop.PropertyType == typeof(ArtemisString))
                            {
                                ArtemisString val = new ArtemisString(sourceData, position);

                                if (_log.IsInfoEnabled && val != null)
                                {
                                    _log.InfoFormat("Property {0} set to {1}", prop.Name, val.Value);
                                }


                                propertyList[i].SetValue(obj, val, null);
                                position += val.Length * 2 + 4;
                            }
                            if (prop.PropertyType == typeof(float?))
                            {
                                if (position + 4 < sourceData.Length)
                                {
                                    float?floatVal = BitConverter.ToSingle(sourceData, position);
                                    propertyList[i].SetValue(obj, floatVal, null);
                                    if (_log.IsInfoEnabled && floatVal != null)
                                    {
                                        _log.InfoFormat("Property {0} set to {1}", prop.Name, floatVal.Value.ToString());
                                    }
                                }
                                position += 4;
                            }
                            if (prop.PropertyType == typeof(byte?))
                            {
                                if (_log.IsInfoEnabled)
                                {
                                    _log.InfoFormat("Property {0} set to {1}", prop.Name, sourceData[position].ToString());
                                }
                                propertyList[i].SetValue(obj, sourceData[position], null);
                                position++;
                            }
                            if (prop.PropertyType == typeof(short?))
                            {
                                if (position + 2 < sourceData.Length)
                                {
                                    short?shortVal = BitConverter.ToInt16(sourceData, position);
                                    if (_log.IsInfoEnabled && shortVal != null)
                                    {
                                        _log.InfoFormat("Property {0} set to {1}", prop.Name, shortVal.Value.ToString());
                                    }
                                    propertyList[i].SetValue(obj, shortVal, null);
                                }
                                position += 2;
                            }
                            if (prop.PropertyType == typeof(long?))
                            {
                                if (position + 8 < sourceData.Length)
                                {
                                    long?longVal = BitConverter.ToInt64(sourceData, position);
                                    if (_log.IsInfoEnabled && longVal != null)
                                    {
                                        _log.InfoFormat("Property {0} set to {1}", prop.Name, longVal.Value.ToString());
                                    }

                                    propertyList[i].SetValue(obj, longVal, null);
                                }
                                position += 8;
                            }

                            if (prop.PropertyType == typeof(OrdinanceTypes?))
                            {
                                OrdinanceTypes?OrdVal = (OrdinanceTypes?)sourceData[position++];
                                if (_log.IsInfoEnabled && OrdVal != null)
                                {
                                    _log.InfoFormat("Property {0} set to {1}", prop.Name, OrdVal.Value.ToString());
                                }

                                propertyList[i].SetValue(obj, OrdVal, null);
                                position++;
                            }
                        }
                        catch (Exception ex)
                        {
                            if (_log.IsWarnEnabled)
                            {
                                _log.Warn("Error getting bytes", ex);
                            }
                        }
                    }
                    else
                    {
                        if (_log.IsInfoEnabled)
                        {
                            _log.InfoFormat("Property {0} not included", propertyList[i].Name);
                        }
                    }
                }
            }
        }
Пример #11
0
        static KeyValuePair<int, object> GetIntermediateType(Type CastType, int index, Stream stream)
        {
            int retVal = index;
            object intermediateObject = null;
            Type cTp = CastType;
            if (cTp.IsEnum)
            {
             
                
                cTp = Enum.GetUnderlyingType(cTp);
            }
            if (cTp == typeof(byte) || cTp == typeof(byte?))
            {

                if (retVal < stream.Length)
                {
                    intermediateObject = Convert.ToByte(stream.ReadByte());
                    retVal = 1;
                }
            }
            if (cTp == typeof(short) || cTp == typeof(short?))
            {

                if (retVal < stream.Length - 1)
                {
                    intermediateObject = stream.ToInt16();
                    retVal = 2;
                }
            }
            if (cTp == typeof(int) || cTp == typeof(int?))
            {

                if (retVal < stream.Length - 3)
                {

                    intermediateObject = stream.ToInt32();
                    retVal = 4;
                }
            }
            if (cTp == typeof(float) || cTp == typeof(float?))
            {

                if (retVal < stream.Length - 3)
                {

                    intermediateObject = stream.ToSingle();
                    retVal = 4;
                }
            }
            if (cTp == typeof(long) || cTp == typeof(long?))
            {

                if (retVal < stream.Length - 7)
                {

                    intermediateObject = stream.ToInt64();
                    retVal = 8;
                }
            }
            if (CastType == typeof(ArtemisString))
            {
                if (retVal < stream.Length - 3)
                {
                    ArtemisString artyString = new ArtemisString(stream, index);
                    intermediateObject = artyString;
                    retVal = artyString.Length * 2 + 4;
                }
            }
            return new KeyValuePair<int,object>(retVal, intermediateObject);
        }
Пример #12
0
 public static void Write(this MemoryStream me, ArtemisString data)
 {
     if (me != null && data != null) me.Write(data.GetRawData().GetBuffer(), 0, data.Length);
 }