-
Notifications
You must be signed in to change notification settings - Fork 6
/
CommonPackage.cs
297 lines (249 loc) · 12.2 KB
/
CommonPackage.cs
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
namespace Marathon.Formats.Package
{
/// <summary>
/// File base for the Common.bin format.
/// <para>Used in SONIC THE HEDGEHOG for defining physics objects.</para>
/// </summary>
public class CommonPackage : FileBase
{
public CommonPackage() { }
public CommonPackage(string file, bool serialise = false)
{
switch (Path.GetExtension(file))
{
case ".json":
{
Objects = JsonDeserialise<List<CommonObject>>(file);
// Save extension-less JSON (exploiting .NET weirdness, because it doesn't omit all extensions).
if (serialise)
Save(Path.GetFileNameWithoutExtension(file));
break;
}
default:
{
Load(file);
if (serialise)
JsonSerialise(Objects);
break;
}
}
}
public override string Extension { get; } = ".bin";
public List<CommonObject> Objects { get; set; } = new();
public override void Load(Stream stream)
{
BINAReader reader = new(stream);
// Store first offset after the header.
long startPosition = reader.BaseStream.Position;
// Store the offset to the first string entry (repurposed as offset table length).
uint offsetTableLength = reader.ReadUInt32();
// Jump back to the first offset so we can iterate through the loop.
reader.JumpTo(startPosition);
// Read stream until we've reached the end of the offset table.
while (reader.BaseStream.Position < offsetTableLength)
{
CommonObject @object = new();
// Read all the offsets.
uint objectNameOffset = reader.ReadUInt32();
uint modelOffset = reader.ReadUInt32();
uint havokOffset = reader.ReadUInt32();
uint timeEventOffset = reader.ReadUInt32();
uint materialAnimationOffset = reader.ReadUInt32();
uint luaScriptOffset = reader.ReadUInt32();
@object.UnknownUInt32_1 = reader.ReadUInt32();
uint unknownString1Offset = reader.ReadUInt32();
@object.CollisionType = reader.ReadUInt32();
@object.CollisionFlags = reader.ReadUInt32();
@object.Rigidity = reader.ReadUInt32();
@object.EnemyDamage = reader.ReadUInt32();
@object.UnknownFloat_1 = reader.ReadSingle();
@object.Potency = reader.ReadUInt32();
@object.TargetFlags = reader.ReadUInt32();
@object.Health = reader.ReadUInt32();
@object.DebrisLifetimeBase = reader.ReadSingle();
@object.DebrisLifetimeModifier = reader.ReadSingle();
@object.UnknownUInt32_4 = reader.ReadUInt32();
uint breakObjectOffset = reader.ReadUInt32();
uint explosionOffset = reader.ReadUInt32();
uint particleFileOffset = reader.ReadUInt32();
uint particleNameOffset = reader.ReadUInt32();
uint sceneBankOffset = reader.ReadUInt32();
uint soundNameOffset = reader.ReadUInt32();
@object.PsiBehaviour = reader.ReadUInt32();
// Store current position to jump back to for the next entry.
long position = reader.BaseStream.Position;
// Read all the string values.
@object.PropName = reader.ReadNullTerminatedString(false, objectNameOffset, true);
@object.ModelName = reader.ReadNullTerminatedString(false, modelOffset, true);
@object.HavokName = reader.ReadNullTerminatedString(false, havokOffset, true);
@object.TimeEvent = reader.ReadNullTerminatedString(false, timeEventOffset, true);
@object.MaterialAnimation = reader.ReadNullTerminatedString(false, materialAnimationOffset, true);
@object.LuaScript = reader.ReadNullTerminatedString(false, luaScriptOffset, true);
@object.UnknownString_1 = reader.ReadNullTerminatedString(false, unknownString1Offset, true);
@object.BreakObject = reader.ReadNullTerminatedString(false, breakObjectOffset, true);
@object.Explosion = reader.ReadNullTerminatedString(false, explosionOffset, true);
@object.ParticleFile = reader.ReadNullTerminatedString(false, particleFileOffset, true);
@object.ParticleName = reader.ReadNullTerminatedString(false, particleNameOffset, true);
@object.SoundBank = reader.ReadNullTerminatedString(false, sceneBankOffset, true);
@object.SoundName = reader.ReadNullTerminatedString(false, soundNameOffset, true);
// Jump back to the saved position to read the next object.
reader.JumpTo(position);
// Save object entry into the Entries list.
Objects.Add(@object);
}
}
public override void Save(Stream stream)
{
BINAWriter writer = new(stream);
// Write the objects.
for (int i = 0; i < Objects.Count; i++)
{
writer.AddString($"object{i}Name", Objects[i].PropName);
writer.AddString($"object{i}ModelName", Objects[i].ModelName);
writer.AddString($"object{i}HKXName", Objects[i].HavokName);
writer.AddString($"object{i}TimeEvent", Objects[i].TimeEvent);
writer.AddString($"object{i}MaterialAnimation", Objects[i].MaterialAnimation);
writer.AddString($"object{i}LuaScript", Objects[i].LuaScript);
writer.Write(Objects[i].UnknownUInt32_1);
writer.AddString($"object{i}UnknownString1", Objects[i].UnknownString_1);
writer.Write(Objects[i].CollisionType);
writer.Write(Objects[i].CollisionFlags);
writer.Write(Objects[i].Rigidity);
writer.Write(Objects[i].EnemyDamage);
writer.Write(Objects[i].UnknownFloat_1);
writer.Write(Objects[i].Potency);
writer.Write(Objects[i].TargetFlags);
writer.Write(Objects[i].Health);
writer.Write(Objects[i].DebrisLifetimeBase);
writer.Write(Objects[i].DebrisLifetimeModifier);
writer.Write(Objects[i].UnknownUInt32_4);
writer.AddString($"object{i}BreakObject", Objects[i].BreakObject);
writer.AddString($"object{i}Explosion", Objects[i].Explosion);
writer.AddString($"object{i}ParticleFile", Objects[i].ParticleFile);
writer.AddString($"object{i}ParticleName", Objects[i].ParticleName);
writer.AddString($"object{i}SceneBank", Objects[i].SoundBank);
writer.AddString($"object{i}SoundName", Objects[i].SoundName);
writer.Write(Objects[i].PsiBehaviour);
}
// Write the footer.
writer.WriteNulls(4);
writer.FinishWrite();
}
}
public class CommonObject
{
/// <summary>
/// The name of the object.
/// </summary>
public string PropName { get; set; }
/// <summary>
/// The internal path to the model (*.xno) for the object.
/// </summary>
public string ModelName { get; set; }
/// <summary>
/// The internal path to the Havok data (*.hkx) for the object.
/// </summary>
public string HavokName { get; set; }
/// <summary>
/// The internal path to the <see cref="Event.TimeEvent"/> (*.tev) for the object.
/// </summary>
public string TimeEvent { get; set; }
/// <summary>
/// The internal path to the material animation (*.xnv) for the object.
/// </summary>
public string MaterialAnimation { get; set; }
/// <summary>
/// The internal path to the Lua script (*.lub) for the object.
/// </summary>
public string LuaScript { get; set; }
/// <summary>
/// TODO: Unknown - seems to screw with how the object breaks?
/// </summary>
public uint UnknownUInt32_1 { get; set; }
/// <summary>
/// TODO: Unknown.
/// </summary>
public string UnknownString_1 { get; set; }
/// <summary>
/// The type of collision this object uses globally for the sound effects and behaviour.
/// </summary>
public uint CollisionType { get; set; }
/// <summary>
/// The flags that determine how the player interacts with the object.
/// </summary>
public uint CollisionFlags { get; set; }
/// <summary>
/// The rigidity of the object.
/// <para>0 = Standard</para>
/// <para>1 = Colliable Debris</para>
/// <para>2 = Colliable but non intersectable debries</para>
/// <para>3 = Debris</para>
/// </summary>
public uint Rigidity { get; set; }
/// <summary>
/// The damage this object does when colliding with an enemy with applied force.
/// </summary>
public uint EnemyDamage { get; set; }
/// <summary>
/// TODO: Unknown - seems to be something regarding the object breaking from gravity?
/// </summary>
public float UnknownFloat_1 { get; set; }
/// <summary>
/// The required force an attack needs to be able to damage this object.
/// </summary>
public uint Potency { get; set; }
/// <summary>
/// The flags that determine how the player targets the object.
/// <para>15 = Homing Attack</para>
/// </summary>
public uint TargetFlags { get; set; }
/// <summary>
/// The amount of health this object has.
/// </summary>
public uint Health { get; set; }
/// <summary>
/// The amount of time this object lasts as debris.
/// </summary>
public float DebrisLifetimeBase { get; set; }
/// <summary>
/// A modifier to increase the time for debris to disappear to add variety.
/// </summary>
public float DebrisLifetimeModifier { get; set; }
/// <summary>
/// TODO: Unknown.
/// </summary>
public uint UnknownUInt32_4 { get; set; }
/// <summary>
/// The object spawned upon breaking this object.
/// </summary>
public string BreakObject { get; set; }
/// <summary>
/// The type of explosion stored in the <see cref="ExplosionPackage"/> format that'll be used when this object breaks.
/// </summary>
public string Explosion { get; set; }
/// <summary>
/// The internal path to the particle file used when this object breaks.
/// </summary>
public string ParticleFile { get; set; }
/// <summary>
/// The name of the particle in the particle file to use when this object breaks.
/// </summary>
public string ParticleName { get; set; }
/// <summary>
/// The internal path to the <see cref="Audio.SoundBank"/> (*.sbk) used when this object breaks.
/// </summary>
public string SoundBank { get; set; }
/// <summary>
/// The name of the sound in the <see cref="Audio.SoundBank"/> to use when this object breaks.
/// </summary>
public string SoundName { get; set; }
/// <summary>
/// <para>0 = Can't be picked up</para>
/// <para>1 = Normal Grab</para>
/// <para>2 = Kingdom Valley Pendulum</para>
/// <para>3 = Super Fast Projectile Launch</para>
/// </summary>
public uint PsiBehaviour { get; set; }
public override string ToString() => PropName;
}
}