| | | 1 | | // <copyright file="ObjMaterialFile.cs" company="Jérémy Ansel"> |
| | | 2 | | // Copyright (c) 2017, 2019 Jérémy Ansel |
| | | 3 | | // </copyright> |
| | | 4 | | // <license> |
| | | 5 | | // Licensed under the MIT license. See LICENSE.txt |
| | | 6 | | // </license> |
| | | 7 | | |
| | | 8 | | using System.Text; |
| | | 9 | | |
| | | 10 | | namespace JeremyAnsel.Media.WavefrontObj |
| | | 11 | | { |
| | | 12 | | public class ObjMaterialFile |
| | | 13 | | { |
| | 2044 | 14 | | public ObjMaterialFile() |
| | | 15 | | { |
| | 2044 | 16 | | this.Materials = new List<ObjMaterial>(); |
| | 2044 | 17 | | } |
| | | 18 | | |
| | 1476 | 19 | | public string? HeaderText { get; set; } |
| | | 20 | | |
| | | 21 | | [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.RootHidden)] |
| | 7904 | 22 | | public List<ObjMaterial> Materials { get; private set; } |
| | | 23 | | |
| | | 24 | | public static ObjMaterialFile FromFile(string? path) |
| | | 25 | | { |
| | 8 | 26 | | return FromFile(path, ObjMaterialFileReaderSettings.Default); |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | public static ObjMaterialFile FromFile(string? path, ObjMaterialFileReaderSettings settings) |
| | | 30 | | { |
| | 8 | 31 | | if (path == null) |
| | | 32 | | { |
| | 4 | 33 | | throw new ArgumentNullException(nameof(path)); |
| | | 34 | | } |
| | | 35 | | |
| | 4 | 36 | | using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)) |
| | | 37 | | { |
| | | 38 | | #if NET6_0_OR_GREATER |
| | 3 | 39 | | return ObjMaterialFileReader9.FromStream(stream, settings); |
| | | 40 | | #else |
| | 1 | 41 | | return ObjMaterialFileReader.FromStream(stream, settings); |
| | | 42 | | #endif |
| | | 43 | | } |
| | 4 | 44 | | } |
| | | 45 | | |
| | | 46 | | public static ObjMaterialFile FromStream(Stream? stream) |
| | | 47 | | { |
| | 8 | 48 | | return FromStream(stream, ObjMaterialFileReaderSettings.Default); |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | public static ObjMaterialFile FromStream(Stream? stream, ObjMaterialFileReaderSettings settings) |
| | | 52 | | { |
| | | 53 | | #if NET6_0_OR_GREATER |
| | 1296 | 54 | | return ObjMaterialFileReader9.FromStream(stream, settings); |
| | | 55 | | #else |
| | 432 | 56 | | return ObjMaterialFileReader.FromStream(stream, settings); |
| | | 57 | | #endif |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | public void WriteTo(string? path) |
| | | 61 | | { |
| | 8 | 62 | | if (path == null) |
| | | 63 | | { |
| | 4 | 64 | | throw new ArgumentNullException(nameof(path)); |
| | | 65 | | } |
| | | 66 | | |
| | 4 | 67 | | using (var writer = new StreamWriter(path)) |
| | | 68 | | { |
| | 4 | 69 | | ObjMaterialFileWriter.Write(this, writer); |
| | 4 | 70 | | } |
| | 4 | 71 | | } |
| | | 72 | | |
| | | 73 | | public void WriteTo(Stream? stream) |
| | | 74 | | { |
| | 308 | 75 | | if (stream == null) |
| | | 76 | | { |
| | 4 | 77 | | throw new ArgumentNullException(nameof(stream)); |
| | | 78 | | } |
| | | 79 | | |
| | 304 | 80 | | using (var writer = new StreamWriter(stream, new UTF8Encoding(false, true), 1024, true)) |
| | | 81 | | { |
| | 304 | 82 | | ObjMaterialFileWriter.Write(this, writer); |
| | 304 | 83 | | } |
| | 304 | 84 | | } |
| | | 85 | | } |
| | | 86 | | } |
| | | 87 | | |