1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+ using System . Text ;
5+ using System . Threading . Tasks ;
6+
7+ using NUnit . Framework ;
8+
9+ using SharpGLTF . Memory ;
10+
11+ namespace SharpGLTF . Schema2 . LoadAndSave
12+ {
13+ [ AttachmentPathFormat ( "*/TestResults/ReadContextTests/?" , true ) ]
14+ internal class ReadContextTests
15+ {
16+ [ Test ]
17+ public void TestLoadWithReadContext ( )
18+ {
19+ var modelPath = ResourceInfo . From ( "SpecialCases/RelativePaths.gltf" ) ;
20+
21+ var baseDirectory = modelPath . File . Directory ;
22+
23+ ArraySegment < byte > _fileReader ( string assetName )
24+ {
25+ assetName = Uri . UnescapeDataString ( assetName ) ;
26+
27+ var filePath = System . IO . Path . Combine ( baseDirectory . FullName , assetName ) ;
28+ var finfo = new System . IO . FileInfo ( filePath ) ;
29+
30+ if ( finfo . Exists )
31+ {
32+ var data = System . IO . File . ReadAllBytes ( filePath ) ;
33+
34+ return new ArraySegment < byte > ( data ) ;
35+ }
36+
37+ throw new System . IO . FileNotFoundException ( filePath ) ;
38+ }
39+
40+ var context = ReadContext . Create ( _fileReader ) ;
41+
42+ var model = ModelRoot . Load ( "RelativePaths.gltf" , context ) ;
43+ }
44+
45+
46+ [ Test ]
47+ public void TestLoadWithReadContextAndTextureLoadSkip ( )
48+ {
49+ var modelPath = ResourceInfo . From ( "SpecialCases/RelativePaths.gltf" ) ;
50+
51+ var baseDirectory = modelPath . File . Directory ;
52+
53+ // we store the uris for the tests at the end.
54+ var imageUriCache = new HashSet < string > ( ) ;
55+
56+ // this is a very tiny, placeholder PNG image to be used as replacement
57+ // of the actual image. This ensures a valid image is passed to the model for
58+ // the tests to pass.
59+ var placeHolderImage = new ArraySegment < byte > ( MemoryImage . DefaultPngImage ) ;
60+
61+ ArraySegment < byte > _fileReader ( string assetName )
62+ {
63+ assetName = Uri . UnescapeDataString ( assetName ) ;
64+
65+ var filePath = System . IO . Path . Combine ( baseDirectory . FullName , assetName ) ;
66+ var finfo = new System . IO . FileInfo ( filePath ) ;
67+
68+ // skip image loading by replacing it with a tiny placeholder
69+ if ( finfo . Extension == ".jpg" || finfo . Extension == ".png" )
70+ {
71+ imageUriCache . Add ( assetName ) ;
72+ return placeHolderImage ;
73+ }
74+
75+ if ( finfo . Exists )
76+ {
77+ var data = System . IO . File . ReadAllBytes ( filePath ) ;
78+
79+ return new ArraySegment < byte > ( data ) ;
80+ }
81+
82+ throw new System . IO . FileNotFoundException ( filePath ) ;
83+ }
84+
85+ // load model and ensure the placeholder images contain proper paths
86+
87+ var context = ReadContext . Create ( _fileReader ) ;
88+
89+ var model = ModelRoot . Load ( "RelativePaths.gltf" , context ) ;
90+
91+ foreach ( var img in model . LogicalImages )
92+ {
93+ var bitmap = img . Content ; // Content is a MemoryImage object that countains a bitmap SourcePath.
94+
95+ Assert . That ( bitmap . SourcePath , Is . Not . Null ) ;
96+
97+ var srcPath = Uri . UnescapeDataString ( bitmap . SourcePath ) ;
98+
99+ Assert . That ( imageUriCache . Contains ( srcPath ) ) ;
100+ }
101+ }
102+ }
103+ }
0 commit comments