public (object Instance, int BytesParsed) ObjectFromBytes(byte[] data, int length, Type type)
        {
            Type elementType = type.GetElementType();

            if (!ParentModule.HasConverterOfType(elementType))
            {
                NetBase.WriteDebug($"Invalid element type in array conversion! ({elementType})", true);
            }

            //The 'length' parameter here is used to determine how many bytes at the start of the array are for the length of the string data
            (ushort Instance, int BytesParsed)t = ParentModule.ObjectFromBytes <ushort>(data);

            ushort arraySize = t.Instance;

            byte[] arrayBytes = data.Skip(t.BytesParsed).ToArray();

            Array instances = Array.CreateInstance(elementType, arraySize);

            int parsed = t.BytesParsed;

            for (int i = 0; i < arraySize; ++i)
            {
                (object Instance, int BytesParsed)instanceTuple = ParentModule.ObjectFromBytes(elementType, arrayBytes, t.BytesParsed);
                instances.SetValue(instanceTuple.Instance, i);
                parsed    += instanceTuple.BytesParsed;
                arrayBytes = arrayBytes.Skip(instanceTuple.BytesParsed).ToArray();
            }

            return(instances, parsed);
        }
        public (object Instance, int BytesParsed) ObjectFromBytes(byte[] data, int length, Type type)
        {
            Type underlyingType = type.GetEnumUnderlyingType();

            if (!ParentModule.HasConverterOfType(underlyingType))
            {
                NetBase.WriteDebug($"Invalid element type for enum conversion! ({underlyingType})", true);
            }

            return(ParentModule.ObjectFromBytes(underlyingType, data, length));
        }
Example #3
0
        public (object Instance, int BytesParsed) ObjectFromBytes(byte[] data, int length, Type type)
        {
            if (length == -1)
            {
                return(Encoding.UTF8.GetString(data), data.Length);
            }

            //The 'length' parameter here is used to determine how many bytes at the start of the array are for the length of the string data
            byte[] lengthData = data.Take(length).ToArray();
            ushort dataLength = ParentModule.ObjectFromBytes <ushort>(lengthData).Instance;

            byte[] utf8Bytes = data.Skip(length).Take(dataLength).ToArray();

            return(Encoding.UTF8.GetString(utf8Bytes), lengthData.Length + utf8Bytes.Length);
        }