-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimationReaderBase.cs
More file actions
172 lines (143 loc) · 5.58 KB
/
Copy pathAnimationReaderBase.cs
File metadata and controls
172 lines (143 loc) · 5.58 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
using System.Collections.Generic;
using System.IO;
using System.Text;
using PG.StarWarsGame.Files.ALO.Data;
using PG.StarWarsGame.Files.ALO.Services;
using PG.StarWarsGame.Files.Binary;
using PG.StarWarsGame.Files.ChunkFiles.Binary.Model.Metadata;
namespace PG.StarWarsGame.Files.ALO.Binary.Reader.Animations;
internal abstract class AnimationReaderBase(AloLoadOptions loadOptions, Stream stream)
: AloFileReader<AlamoAnimation>(loadOptions, stream)
{
public sealed override AlamoAnimation Read()
{
var bones = new List<AnimationBoneData>();
AnimationInformationData info = default;
var rootChunk = ChunkReader.ReadChunk();
if (rootChunk is not { Type: (int)AnimationChunkTypes.AnimationFile })
throw new BinaryCorruptedException("Unable to read animation file.");
var actualSize = 0;
do
{
var chunk = ChunkReader.ReadChunk(ref actualSize);
ReadAnimation(chunk, ref info, bones);
actualSize += chunk.BodySize;
} while (actualSize < rootChunk.BodySize);
if (actualSize != rootChunk.BodySize)
throw new BinaryCorruptedException();
if (info.NumberBones != bones.Count)
throw new BinaryCorruptedException("The number of bones does not match the number of bone data.");
return new AlamoAnimation
{
FPS = info.FPS,
NumberBones = info.NumberBones,
NumberFrames = info.NumberFrames,
BoneData = bones,
};
}
protected virtual void ReadAnimation(
ChunkMetadata chunk,
ref AnimationInformationData animationInformation,
List<AnimationBoneData> bones)
{
switch (chunk.Type)
{
case (int)AnimationChunkTypes.AnimationInfo:
ThrowIfChunkSizeTooLargeException(chunk);
animationInformation = ReadAnimationInfo(chunk.BodySize);
break;
case (int)AnimationChunkTypes.BoneData:
ReadBonesData(chunk.BodySize, bones);
break;
default:
ChunkReader.Skip(chunk.BodySize);
break;
}
}
protected virtual void ReadBoneDataCore(ChunkMetadata chunk, List<AnimationBoneData> bones)
{
switch (chunk.Type)
{
case (int)AnimationChunkTypes.BoneDataInfo:
ThrowIfChunkSizeTooLargeException(chunk);
ReadBoneInfo(chunk.BodySize, bones);
break;
default:
ChunkReader.Skip(chunk.BodySize);
break;
}
}
private void ReadBonesData(int chunkSize, List<AnimationBoneData> bones)
{
var actualSize = 0;
do
{
var chunk = ChunkReader.ReadChunk(ref actualSize);
ReadBoneDataCore(chunk, bones);
actualSize += chunk.BodySize;
} while (actualSize < chunkSize);
if (actualSize != chunkSize)
throw new BinaryCorruptedException("Unable to read particle");
}
private void ReadBoneInfo(int chunkSize, List<AnimationBoneData> bones)
{
var actualSize = 0;
string name = null!;
uint index = 0;
do
{
var chunk = ChunkReader.ReadMiniChunk(ref actualSize);
switch (chunk.Type)
{
case (int)AnimationChunkTypes.BoneName:
name = ChunkReader.ReadString(chunk.BodySize, Encoding.ASCII, true, ref actualSize);
break;
case (int)AnimationChunkTypes.BoneIndex:
index = ChunkReader.ReadDword(ref actualSize);
break;
default:
ChunkReader.Skip(chunk.BodySize, ref actualSize);
break;
}
} while (actualSize < chunkSize);
if (actualSize != chunkSize)
throw new BinaryCorruptedException("Unable to read particle");
bones.Add(new AnimationBoneData(index, name));
}
private AnimationInformationData ReadAnimationInfo(int chunkSize)
{
var actualSize = 0;
var info = new AnimationInformationData();
do
{
var chunk = ChunkReader.ReadMiniChunk(ref actualSize);
switch (chunk.Type)
{
case (int)AnimationChunkTypes.NumFrames:
info.NumberFrames = ChunkReader.ReadDword(ref actualSize);
break;
case (int)AnimationChunkTypes.Fps:
info.FPS = ChunkReader.ReadFloat(ref actualSize);
break;
case (int)AnimationChunkTypes.NumBones:
info.NumberBones = ChunkReader.ReadDword(ref actualSize);
break;
case (int)AnimationChunkTypes.TranslationBlockSize:
info.TranslationBlockSize = ChunkReader.ReadDword(ref actualSize);
break;
case (int)AnimationChunkTypes.RotationBlockSize:
info.RotationBlockSize = ChunkReader.ReadDword(ref actualSize);
break;
case (int)AnimationChunkTypes.ScaleBlockSize:
info.ScaleBlockSize = ChunkReader.ReadDword(ref actualSize);
break;
default:
ChunkReader.Skip(chunk.BodySize, ref actualSize);
break;
}
} while (actualSize < chunkSize);
if (actualSize != chunkSize)
throw new BinaryCorruptedException("Unable to read animation");
return info;
}
}