This repository was archived by the owner on Dec 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathWZAudioProperty.cs
103 lines (92 loc) · 4.36 KB
/
WZAudioProperty.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
// reWZ is copyright angelsl, 2011 to 2015 inclusive.
//
// This file (WZAudioProperty.cs) is part of reWZ.
//
// reWZ is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// reWZ is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with reWZ. If not, see <http://www.gnu.org/licenses/>.
//
// Linking reWZ statically or dynamically with other modules
// is making a combined work based on reWZ. Thus, the terms and
// conditions of the GNU General Public License cover the whole combination.
//
// As a special exception, the copyright holders of reWZ give you
// permission to link reWZ with independent modules to produce an
// executable, regardless of the license terms of these independent modules,
// and to copy and distribute the resulting executable under terms of your
// choice, provided that you also meet, for each linked independent module,
// the terms and conditions of the license of that module. An independent
// module is a module which is not derived from or based on reWZ.
using System;
using System.Diagnostics;
namespace reWZ.WZProperties {
/// <summary>A sound property.</summary>
public sealed class WZAudioProperty : WZDelayedProperty<byte[]>, IDisposable {
private static readonly Guid WaveFormatExGuid = new Guid("05589f81-c356-11ce-bf01-00aa0055595a");
private static readonly Guid NoHeaderGuid = new Guid("00000000-0000-0000-0000-000000000000");
private byte[] _header;
internal WZAudioProperty(string name, WZObject parent, WZBinaryReader reader, WZImage container)
: base(name, parent, container, reader, false, WZObjectType.Audio) {}
/// <summary>The WAVEFORMATEX header, if present.</summary>
public byte[] Header {
get {
if (!_parsed) {
CheckParsed();
}
return _header;
}
}
public int Duration { get; private set; }
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
/// <filterpriority>2</filterpriority>
public void Dispose() {
_value = null;
_header = null;
_parsed = false;
}
internal override bool Parse(WZBinaryReader r, bool initial, out byte[] result) {
r.Skip(1);
int blockLen = r.ReadWZInt(); // sound data length
Duration = r.ReadWZInt(); // sound duration
r.Skip(1 + 16 + 16 + 2); // Byte, Major type GUID, Sub type GUID, byte, byte
Guid fmt = new Guid(r.ReadBytes(16));
if (fmt == WaveFormatExGuid) {
if (initial) {
r.Skip(r.ReadWZInt());
} else {
_header = r.ReadBytes(r.ReadWZInt());
if (_header.Length != 18 + GetCbSize(_header)) {
_header = null; // TODO FIXME figure out what those gibberish headers are
}
// But in any case they don't affect our uses
// File._aes.DecryptBytesAsciiKey(_header);
//if (_header.Length != 18 + GetCbSize(_header))
// Debug.WriteLine("Failed to parse WAVEFORMATEX header at node {0}", Path);
//throw new WZException($"Failed to parse WAVEFORMATEX header at node {Path}");
}
} else if (fmt != NoHeaderGuid) {
Debug.WriteLine("New format guid {0} @ {1}", fmt, Path);
}
if (!initial || (File._flag & WZReadSelection.EagerParseAudio) == WZReadSelection.EagerParseAudio) {
result = r.ReadBytes(blockLen);
return true;
} else {
r.Skip(blockLen);
result = null;
return false;
}
}
private static int GetCbSize(byte[] header) {
return header[16] | (header[17] << 8);
}
}
}