1
1
using System ;
2
2
using System . IO ;
3
+ using Ionic . Zlib ;
3
4
4
5
namespace XbTool . Xb2 . Textures
5
6
{
@@ -9,42 +10,60 @@ public class WilayRead
9
10
10
11
public WilayRead ( byte [ ] file )
11
12
{
12
- using ( var stream = new MemoryStream ( file ) )
13
- using ( var reader = new BinaryReader ( stream ) )
13
+ var stream = new MemoryStream ( file ) ;
14
+ var reader = new BinaryReader ( stream ) ;
15
+
16
+ string magic = reader . ReadUTF8 ( 4 ) ;
17
+
18
+ if ( magic == "xbc1" )
14
19
{
15
- string magic = reader . ReadUTF8 ( 4 ) ;
16
- if ( magic != "LAHD" )
20
+ reader . BaseStream . Position += 4 ;
21
+ int uncompressedSize = reader . ReadInt32 ( ) ;
22
+ stream . Position = 0x30 ;
23
+
24
+ file = new byte [ uncompressedSize ] ;
25
+ var uncompressedData = new MemoryStream ( file ) ;
26
+
27
+ using ( var deflate = new ZlibStream ( stream , CompressionMode . Decompress , true ) )
17
28
{
18
- throw new NotSupportedException ( $ "Can't read type { magic } " ) ;
29
+ deflate . CopyTo ( uncompressedData , uncompressedSize ) ;
19
30
}
20
31
21
- int texturesOffset = BitConverter . ToInt32 ( file , 36 ) ;
22
- stream . Position = texturesOffset ;
32
+ uncompressedData . Position = 0 ;
33
+ stream = uncompressedData ;
34
+ reader = new BinaryReader ( stream ) ;
35
+ }
36
+ else if ( magic != "LAHD" )
37
+ {
38
+ throw new NotSupportedException ( $ "Can't read type { magic } ") ;
39
+ }
40
+
41
+ int texturesOffset = BitConverter . ToInt32 ( file , 36 ) ;
42
+ stream . Position = texturesOffset ;
23
43
24
- int offset = reader . ReadInt32 ( ) ;
25
- int length = reader . ReadInt32 ( ) ;
26
- stream . Position = texturesOffset + offset ;
27
- var offsets = new TextureOffset [ length ] ;
28
- Textures = new LahdTexture [ length ] ;
44
+ int offset = reader . ReadInt32 ( ) ;
45
+ int length = reader . ReadInt32 ( ) ;
46
+ stream . Position = texturesOffset + offset ;
47
+ var offsets = new TextureOffset [ length ] ;
48
+ Textures = new LahdTexture [ length ] ;
29
49
30
- for ( int i = 0 ; i < length ; i ++ )
50
+ for ( int i = 0 ; i < length ; i ++ )
51
+ {
52
+ offsets [ i ] = new TextureOffset
31
53
{
32
- offsets [ i ] = new TextureOffset
33
- {
34
- Field0 = reader . ReadInt32 ( ) ,
35
- Offset = reader . ReadInt32 ( ) ,
36
- Length = reader . ReadInt32 ( )
37
- } ;
38
- }
54
+ Field0 = reader . ReadInt32 ( ) ,
55
+ Offset = reader . ReadInt32 ( ) ,
56
+ Length = reader . ReadInt32 ( )
57
+ } ;
58
+ }
39
59
40
- for ( int i = 0 ; i < length ; i ++ )
41
- {
42
- stream . Position = texturesOffset + offsets [ i ] . Offset + offsets [ i ] . Length - 56 ;
60
+ for ( int i = 0 ; i < length ; i ++ )
61
+ {
62
+ stream . Position = texturesOffset + offsets [ i ] . Offset + offsets [ i ] . Length - 56 ;
43
63
44
- var texture = new byte [ offsets [ i ] . Length ] ;
45
- Array . Copy ( file , texturesOffset + offsets [ i ] . Offset , texture , 0 , offsets [ i ] . Length ) ;
46
- Textures [ i ] = new LahdTexture ( texture ) ;
47
- }
64
+ var texture = new byte [ offsets [ i ] . Length ] ;
65
+ Array . Copy ( file , texturesOffset + offsets [ i ] . Offset , texture , 0 , offsets [ i ] . Length ) ;
66
+ Textures [ i ] = new LahdTexture ( texture ) ;
48
67
}
49
68
}
50
69
}
0 commit comments