| | | 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 | | { |
| | 4 | 14 | | public ObjMaterialFile() |
| | | 15 | | { |
| | 4 | 16 | | Materials = new List<ObjMaterial>(); |
| | 4 | 17 | | } |
| | | 18 | | |
| | 4 | 19 | | public string? HeaderText { get; set; } |
| | | 20 | | |
| | | 21 | | [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.RootHidden)] |
| | 4 | 22 | | public List<ObjMaterial> Materials { get; private set; } |
| | | 23 | | |
| | | 24 | | public static ObjMaterialFile FromFile(string? path) |
| | | 25 | | { |
| | 4 | 26 | | return FromFile(path, ObjMaterialFileReaderSettings.Default); |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | public static ObjMaterialFile FromFile(string? path, ObjMaterialFileReaderSettings settings) |
| | | 30 | | { |
| | 4 | 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 | | { |
| | 4 | 38 | | return ObjMaterialFileReader.FromStream(stream, settings); |
| | | 39 | | } |
| | 4 | 40 | | } |
| | | 41 | | |
| | | 42 | | public static ObjMaterialFile FromStream(Stream? stream) |
| | | 43 | | { |
| | 4 | 44 | | return FromStream(stream, ObjMaterialFileReaderSettings.Default); |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | public static ObjMaterialFile FromStream(Stream? stream, ObjMaterialFileReaderSettings settings) |
| | | 48 | | { |
| | 4 | 49 | | return ObjMaterialFileReader.FromStream(stream, settings); |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | public void WriteTo(string? path) |
| | | 53 | | { |
| | 4 | 54 | | if (path == null) |
| | | 55 | | { |
| | 4 | 56 | | throw new ArgumentNullException(nameof(path)); |
| | | 57 | | } |
| | | 58 | | |
| | 4 | 59 | | using (var writer = new StreamWriter(path)) |
| | | 60 | | { |
| | 4 | 61 | | ObjMaterialFileWriter.Write(this, writer); |
| | 4 | 62 | | } |
| | 4 | 63 | | } |
| | | 64 | | |
| | | 65 | | public void WriteTo(Stream? stream) |
| | | 66 | | { |
| | 4 | 67 | | if (stream == null) |
| | | 68 | | { |
| | 4 | 69 | | throw new ArgumentNullException(nameof(stream)); |
| | | 70 | | } |
| | | 71 | | |
| | 4 | 72 | | using (var writer = new StreamWriter(stream, new UTF8Encoding(false, true), 1024, true)) |
| | | 73 | | { |
| | 4 | 74 | | ObjMaterialFileWriter.Write(this, writer); |
| | 4 | 75 | | } |
| | 4 | 76 | | } |
| | | 77 | | } |