-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelFileReader.cs
More file actions
232 lines (183 loc) · 7.24 KB
/
Copy pathModelFileReader.cs
File metadata and controls
232 lines (183 loc) · 7.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using PG.StarWarsGame.Files.ALO.Data;
using PG.StarWarsGame.Files.ALO.Services;
using PG.StarWarsGame.Files.Binary;
namespace PG.StarWarsGame.Files.ALO.Binary.Reader.Models;
internal class ModelFileReader(AloLoadOptions loadOptions, Stream stream) : AloFileReader<AlamoModel>(loadOptions, stream)
{
public override AlamoModel Read()
{
var textures = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var shaders = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var proxies = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var bones = new List<string>();
var chunk = ChunkReader.TryReadChunk();
while (chunk.HasValue)
{
switch (chunk.Value.Type)
{
case (int)ModelChunkTypes.Skeleton:
ReadSkeleton(chunk.Value.BodySize, bones);
break;
case (int)ModelChunkTypes.Mesh:
ReadMesh(chunk.Value.BodySize, textures, shaders);
break;
case (int)ModelChunkTypes.Connections:
ReadConnections(chunk.Value.BodySize, proxies);
break;
default:
ChunkReader.Skip(chunk.Value.BodySize);
break;
}
chunk = ChunkReader.TryReadChunk();
}
return new AlamoModel
{
Bones = bones,
Textures = textures,
Shaders = shaders,
Proxies = proxies
};
}
private void ReadConnections(int size, HashSet<string> proxies)
{
if (SkipIfNotDesired(AloLoadOptions.Assets, size))
return;
var actualSize = 0;
do
{
var chunk = ChunkReader.ReadChunk(ref actualSize);
ThrowIfChunkSizeTooLargeException(chunk);
if (chunk.Type == (int)ModelChunkTypes.ProxyConnection)
{
ReadProxy(chunk.BodySize, out var proxy, ref actualSize);
proxies.Add(proxy);
}
else
ChunkReader.Skip(chunk.BodySize, ref actualSize);
} while (actualSize < size);
if (size != actualSize)
throw new BinaryCorruptedException("Unable to read alo model.");
}
private void ReadProxy(int size, out string proxy, ref int readSize)
{
var actualSize = 0;
proxy = null!;
do
{
var chunk = ChunkReader.ReadMiniChunk(ref actualSize);
if (chunk.Type == 5)
proxy = ChunkReader.ReadString(chunk.BodySize, Encoding.ASCII, true, ref actualSize);
else
ChunkReader.Skip(chunk.BodySize, ref actualSize);
} while (actualSize < size);
if (size != actualSize)
throw new BinaryCorruptedException("Unable to read alo model.");
if (proxy is null)
throw new BinaryCorruptedException("Alamo proxy does not have a name.");
readSize += actualSize;
}
private void ReadMesh(int size, ISet<string> textures, ISet<string> shaders)
{
if (SkipIfNotDesired(AloLoadOptions.Assets, size))
return;
var actualSize = 0;
do
{
var chunk = ChunkReader.ReadChunk(ref actualSize);
if (chunk.Type == (int)ModelChunkTypes.SubMeshMaterialInformation)
ReadSubMeshMaterialInformation(chunk.BodySize, textures, shaders, ref actualSize);
else
ChunkReader.Skip(chunk.BodySize, ref actualSize);
} while (actualSize < size);
if (size != actualSize)
throw new BinaryCorruptedException("Unable to read alo model.");
}
private void ReadSubMeshMaterialInformation(int size, ISet<string> textures, ISet<string> shaders, ref int readSize)
{
var actualSize = 0;
do
{
var chunk = ChunkReader.ReadChunk(ref actualSize);
switch (chunk.Type)
{
case (int)ModelChunkTypes.ShaderFileName:
{
var shader = ChunkReader.ReadString(chunk.BodySize, Encoding.ASCII, true, ref actualSize);
shaders.Add(shader);
break;
}
case (int)ModelChunkTypes.ShaderTexture:
ThrowIfChunkSizeTooLargeException(chunk);
ReadShaderTexture(chunk.BodySize, textures, ref actualSize);
break;
default:
ChunkReader.Skip(chunk.BodySize, ref actualSize);
break;
}
} while (actualSize < size);
if (size != actualSize)
throw new BinaryCorruptedException("Unable to read alo model.");
readSize += actualSize;
}
private void ReadShaderTexture(int size, ISet<string> textures, ref int readSize)
{
var actualTextureChunkSize = 0;
do
{
var mini = ChunkReader.ReadMiniChunk(ref actualTextureChunkSize);
if (mini.Type == 2)
{
var texture = ChunkReader.ReadString(mini.BodySize, Encoding.ASCII, true, ref actualTextureChunkSize);
textures.Add(texture);
}
else
ChunkReader.Skip(mini.BodySize, ref actualTextureChunkSize);
} while (actualTextureChunkSize != size);
readSize += actualTextureChunkSize;
}
private bool SkipIfNotDesired(AloLoadOptions onSwitches, int skipSize)
{
if (LoadOptions == AloLoadOptions.Full || LoadOptions.HasFlag(onSwitches))
return false;
ChunkReader.Skip(skipSize);
return true;
}
private void ReadSkeleton(int size, IList<string> bones)
{
if (SkipIfNotDesired(AloLoadOptions.Bones, size))
return;
var actualSize = 0;
var boneCountChunk = ChunkReader.ReadChunk(ref actualSize);
Debug.Assert(boneCountChunk is { BodySize: 128, Type: (int)ModelChunkTypes.BoneCount });
var boneCount = ChunkReader.ReadDword(ref actualSize);
ChunkReader.Skip(128 - sizeof(uint), ref actualSize);
for (var i = 0; i < boneCount; i++)
{
var bone = ChunkReader.ReadChunk(ref actualSize);
Debug.Assert(bone is { Type: (int)ModelChunkTypes.Bone, HasChildrenHint: true });
var boneReadSize = 0;
while (boneReadSize < bone.BodySize)
{
var innerBoneChunk = ChunkReader.ReadChunk(ref boneReadSize);
if (innerBoneChunk.Type == (int)ModelChunkTypes.BoneName)
{
var nameSize = innerBoneChunk.BodySize;
var name = ChunkReader.ReadString(nameSize, Encoding.ASCII, true, ref boneReadSize);
bones.Add(name);
}
else
{
ChunkReader.Skip(innerBoneChunk.BodySize, ref boneReadSize);
}
}
actualSize += boneReadSize;
}
if (size != actualSize)
throw new BinaryCorruptedException("Unable to read alo model.");
}
}