Example #1
0
        /// <summary>
        /// Convert bytes to object and get a list of the field values
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"> Byte data </param>
        /// <returns></returns>
        public static List <object> ConvertByteToObject <T>(List <byte> data)
        {
            List <object> objectList = new List <object>();

            PropertyInfo[] fields       = typeof(T).GetProperties();
            int            currentIndex = 0;

            foreach (PropertyInfo field in fields)
            {
                SyncData meta = (SyncData)field.GetCustomAttribute(typeof(SyncData));
                if (meta != null)
                {
                    // Check the type and convert it
                    switch (Type.GetTypeCode(field.PropertyType))
                    {
                    case TypeCode.Boolean:
                        objectList.Add(ConvertByteToBool(ref data, ref currentIndex));
                        break;

                    case TypeCode.Int32:
                        objectList.Add(ConvertByteToInt(ref data, ref currentIndex));
                        break;

                    case TypeCode.UInt16:
                        objectList.Add(ConvertByteToUShort(ref data, ref currentIndex));
                        break;

                    case TypeCode.Single:
                        objectList.Add(ConvertByteToFloat(ref data, ref currentIndex));
                        break;

                    case TypeCode.String:
                        if (meta.GetDataType() == SyncData.TypeOfData.ip)
                        {
                            objectList.Add(ConvertByteToIp(ref data, ref currentIndex));
                        }
                        else
                        {
                            objectList.Add(ConvertByteToString(ref data, ref currentIndex));
                        }
                        break;

                    case TypeCode.Byte:
                        currentIndex++;
                        objectList.Add(data[currentIndex - 1]);
                        break;

                    default:
                        switch (field.PropertyType.Name)
                        {
                        case nameof(Vector2):
                            objectList.Add(ConvertByteToVector2(ref data, ref currentIndex));
                            break;
                        }
                        break;
                    }
                }
            }
            return(objectList);
        }
Example #2
0
        /// <summary>
        /// Convert object to byte data
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"> Instance of the object which should be converted </param>
        /// <returns></returns>
        public static List <byte> ConvertObjectToByte <T>(T obj)
        {
            List <byte> bytes = new List <byte>();

            PropertyInfo[] fields = typeof(T).GetProperties();
            foreach (PropertyInfo field in fields)
            {
                SyncData meta = (SyncData)field.GetCustomAttribute(typeof(SyncData), true);
                if (meta != null)
                {
                    object value = field.GetValue(obj);

                    // Check the type and convert it
                    switch (Type.GetTypeCode(field.PropertyType))
                    {
                    case TypeCode.Boolean:
                        bytes.Add(ConvertBoolToByte((bool)value));
                        break;

                    case TypeCode.Int32:
                        bytes.AddRange(ConvertIntToByte((int)value));
                        break;

                    case TypeCode.UInt16:
                        bytes.AddRange(ConvertUShortToByte((ushort)value));
                        break;

                    case TypeCode.Single:
                        bytes.AddRange(ConvertFloatToByte((float)value));;
                        break;

                    case TypeCode.String:
                        if (meta.GetDataType() == SyncData.TypeOfData.ip)
                        {
                            bytes.AddRange(ConvertIpToByte((string)value));
                        }
                        else
                        {
                            bytes.AddRange(ConvertStringToByte((string)value, ref bytes));
                        }
                        break;

                    case TypeCode.Byte:
                        bytes.Add((byte)value);
                        break;

                    default:
                        switch (field.PropertyType.Name)
                        {
                        case nameof(Vector2):
                            bytes.AddRange(ConvertVector2ToByte((Vector2)value));
                            break;

                        default:
                            Debug.LogError("Type was not found and was not send : " + field.PropertyType.Name);
                            break;
                        }
                        break;
                    }
                }
            }

            return(bytes);
        }