/// <summary>
        /// Serializes an array
        /// </summary>
        /// <typeparam name="T">The array value type</typeparam>
        /// <param name="array">The array</param>
        /// <param name="length">The array length</param>
        /// <param name="name">The array name, for logging</param>
        /// <returns>The serialized array</returns>
        public T[] SerializeArray <T>(T[] array, int length, string name = null)
        {
            // Use byte writing method if it's a byte array
            if (typeof(T) == typeof(byte))
            {
                if (Logger != null)
                {
                    string normalLog = $"{LogPrefix}({typeof(T)}[{length}]) {name ?? "<no name>"}: ";
                    Logger?.WriteLogLine($"{normalLog}{BinaryHelpers.ByteArrayToHexString((byte[])(object)array, 16, new string(' ', normalLog.Length))}");
                }

                Writer.Write((byte[])(object)array);
            }
            else
            {
                Logger?.WriteLogLine($"{LogPrefix}({typeof(T)}[{length}]) {name ?? "<no name>"}");

                // Write every value
                for (int i = 0; i < length; i++)
                {
                    Serialize <T>(array[i], name: name == null || Logger == null ? null : $"{name}[{i}]");
                }
            }

            // Return the array
            return(array);
        }
Пример #2
0
        /// <summary>
        /// Serializes an array
        /// </summary>
        /// <typeparam name="T">The array value type</typeparam>
        /// <param name="array">The array</param>
        /// <param name="length">The array length</param>
        /// <param name="name">The array name, for logging</param>
        /// <returns>The serialized array</returns>
        public T[] SerializeArray <T>(T[] array, int length, string name = null)
        {
            // Use byte reading method if it's a byte array
            if (typeof(T) == typeof(byte))
            {
                var logPrefix = LogPrefix;

                byte[] bytes = Reader.ReadBytes(length);

                if (Logger != null)
                {
                    string normalLog = $"{logPrefix}({typeof(T)}[{length}]) {name ?? "<no name>"}: ";
                    Logger?.WriteLogLine($"{normalLog}{BinaryHelpers.ByteArrayToHexString(bytes, 16, new string(' ', normalLog.Length))}");
                }

                return((T[])(object)bytes);
            }

            Logger?.WriteLogLine($"{LogPrefix}({typeof(T)}[{length}]) {name ?? "<no name>"}");

            // Create the buffer to read to
            var buffer = new T[length];

            // Read each value to the buffer
            for (int i = 0; i < length; i++)
            {
                // Read the value
                buffer[i] = Serialize <T>(default, name: name == null || Logger == null ? null : $"{name}[{i}]");