Skip to content

Commit de1163d

Browse files
committed
Handle compressed wilay files
1 parent fdf8df0 commit de1163d

File tree

1 file changed

+46
-27
lines changed

1 file changed

+46
-27
lines changed

XbTool/XbTool/Xb2/Textures/WilayRead.cs

+46-27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using Ionic.Zlib;
34

45
namespace XbTool.Xb2.Textures
56
{
@@ -9,42 +10,60 @@ public class WilayRead
910

1011
public WilayRead(byte[] file)
1112
{
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")
1419
{
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))
1728
{
18-
throw new NotSupportedException($"Can't read type {magic}");
29+
deflate.CopyTo(uncompressedData, uncompressedSize);
1930
}
2031

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;
2343

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];
2949

30-
for (int i = 0; i < length; i++)
50+
for (int i = 0; i < length; i++)
51+
{
52+
offsets[i] = new TextureOffset
3153
{
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+
}
3959

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;
4363

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);
4867
}
4968
}
5069
}

0 commit comments

Comments
 (0)