/// <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)); }
/// <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)); }
/// <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)); } }
/// <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)); }