Esempio n. 1
0
        /// <summary>
        /// Serializes the given <paramref name="table"/> in the specified <paramref name="endianness"/> and saves the output to the file specified by <paramref name="path"/>.
        /// </summary>
        /// <param name="table">The table to be serialized.</param>
        /// <param name="path">The path to the file to save the output to.</param>
        /// <param name="endianness">The endianness in which to write the output with.</param>
        public static void Serialize(object table, string path, Endianness endianness = Endianness.LittleEndian)
        {
            var tableType = table.GetType();

            using (var fileStream = File.OpenWrite(path))
                using (var serializer = new TableSerializer(tableType, fileStream))
                {
                    serializer.SerializeTable(tableType, table, endianness, false);
                }
        }
Esempio n. 2
0
        /// <summary>
        /// Serializes the given <paramref name="table"/> in the specified <paramref name="endianness"/> and saves the output the given <paramref name="stream"/>.
        /// </summary>
        /// <remarks>The stream is not disposed after calling this method.</remarks>
        /// <param name="table">The table to be serialized.</param>
        /// <param name="stream">The stream save the output to.</param>
        /// <param name="endianness">The endianness in which to write the output with.</param>
        public static void Serialize(object table, Stream stream, Endianness endianness = Endianness.LittleEndian)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            var tableType  = table.GetType();
            var serializer = new TableSerializer(tableType, stream);

            serializer.SerializeTable(tableType, table, endianness, true);
        }
Esempio n. 3
0
        /// <summary>
        /// Deserializes the given stream containing a serialized table of type <typeparamref name="TTable"/>.
        /// </summary>
        /// <remarks>The stream is not disposed after calling this method.</remarks>
        /// <typeparam name="TTable">The type of the table to deserialize.</typeparam>
        /// <param name="stream">A stream containing a serialized table.</param>
        /// <returns>A deserialized table instance of type <typeparamref name="TTable"/></returns>
        public static TTable Deserialize <TTable>(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            if (stream.Length == 0)
            {
                throw new ArgumentException("Stream cannot be empty.", nameof(stream));
            }

            var tableType = typeof(TTable);

            using (var serializer = new TableSerializer(tableType, stream))
                return((TTable)serializer.DeserializeTable(true));
        }
Esempio n. 4
0
        /// <summary>
        /// Deserializes the given file specified by <paramref name="path"/> containing a serialized table using the <paramref name="path"/>'s file name and <paramref name="game"/> parameter.
        /// </summary>
        /// <param name="path">A file path pointing to the table file to deserialize.</param>
        /// <param name="game">A <see cref="Game"/> enumeration used to determine the type of the table.</param>
        /// <returns>A deserialized table instance.</returns>
        public static object Deserialize(string path, Game game)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentException("Value cannot be null or empty.", nameof(path));
            }

            var name = Path.GetFileNameWithoutExtension(path);
            var type = sTableTypes.Single(x =>
            {
                var attrib = x.GetCustomAttribute <TableAttribute>();
                return(attrib.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase) && attrib.Games.Any(y => y == game));
            });

            using (var tableStream = File.OpenRead(path))
                using (var serializer = new TableSerializer(type, tableStream, name))
                    return(serializer.DeserializeTable(false));
        }
Esempio n. 5
0
        /// <summary>
        /// Deserializes the given file specified by <paramref name="path"/> containing a serialized table of type <typeparamref name="TTable"/>.
        /// </summary>
        /// <typeparam name="TTable">The type of the table to deserialize.</typeparam>
        /// <param name="path">A file path pointing to the table file to deserialize.</param>
        /// <returns>A deserialized table instance of type <typeparamref name="TTable"/></returns>
        public static TTable Deserialize <TTable>(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentException("Value cannot be null or empty.", nameof(path));
            }

            if (!File.Exists(path))
            {
                throw new ArgumentException("Specified file doesn't exist", nameof(path));
            }

            var tableType = typeof(TTable);
            var tableName = Path.GetFileNameWithoutExtension(path);

            using (var tableStream = File.OpenRead(path))
                using (var serializer = new TableSerializer(tableType, tableStream, tableName))
                {
                    return((TTable)serializer.DeserializeTable(false));
                }
        }
Esempio n. 6
0
        /// <summary>
        /// Deserializes the given stream containing a serialized table using the provided <paramref name="name"/> and <paramref name="game"/> parameters.
        /// </summary>
        /// <remarks>The stream is not disposed after calling this method.</remarks>
        /// <param name="stream">A stream containing a serialized table.</param>
        /// <param name="name">The name of the table file to deserialize.</param>
        /// <param name="game">A <see cref="Game"/> enumeration used to determine the type of the table.</param>
        /// <returns>A deserialized table instance.</returns>
        public static object Deserialize(Stream stream, string name, Game game)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("Value cannot be null or empty.", nameof(name));
            }

            var type = sTableTypes.Single(x =>
            {
                var attrib = x.GetCustomAttribute <TableAttribute>();
                return(attrib.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase) && attrib.Games.Any(y => y == game));
            });

            using (var serializer = new TableSerializer(type, stream, name))
                return(serializer.DeserializeTable(true));
        }