-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBarsFile.cs
More file actions
191 lines (155 loc) · 5.54 KB
/
Copy pathBarsFile.cs
File metadata and controls
191 lines (155 loc) · 5.54 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
using System.Text;
namespace BarsTool;
public class BarsFile
{
public ushort Version { get; set; } = 0x0101;
public List<BarsAsset> Assets { get; set; } = [];
public static BarsFile Read(string path) => Read(File.ReadAllBytes(path));
public static BarsFile Read(byte[] data)
{
using var ms = new MemoryStream(data);
using var reader = new BinaryReader(ms, Encoding.UTF8);
uint magic = reader.ReadUInt32();
if (magic != 0x53524142) // "BARS"
throw new InvalidDataException("Not a valid BARS file.");
int fileSize = reader.ReadInt32();
ushort bom = reader.ReadUInt16();
if (bom != 0xFEFF)
throw new InvalidDataException($"Unexpected BOM: 0x{bom:X4}");
var bars = new BarsFile();
bars.Version = reader.ReadUInt16();
int assetCount = reader.ReadInt32();
var hashes = new uint[assetCount];
for (int i = 0; i < assetCount; i++)
hashes[i] = reader.ReadUInt32();
var amtaOffsets = new int[assetCount];
var audioOffsets = new int[assetCount];
for (int i = 0; i < assetCount; i++)
{
amtaOffsets[i] = reader.ReadInt32();
audioOffsets[i] = reader.ReadInt32();
}
for (int i = 0; i < assetCount; i++)
{
var asset = new BarsAsset { Hash = hashes[i] };
int amtaTotalSize = BitConverter.ToInt32(data, amtaOffsets[i] + 8);
try
{
asset.Amta = AmtaFile.Read(data, amtaOffsets[i], amtaTotalSize);
asset.Name = asset.Amta.Name;
}
catch
{
asset.Name = $"unknown_{hashes[i]:X8}";
}
if (audioOffsets[i] != -1 && audioOffsets[i] != 0)
{
int audioEnd = DetermineAudioEnd(audioOffsets[i], i, audioOffsets, data.Length);
int audioLen = audioEnd - audioOffsets[i];
asset.AudioData = new byte[audioLen];
Array.Copy(data, audioOffsets[i], asset.AudioData, 0, audioLen);
}
bars.Assets.Add(asset);
}
return bars;
}
public byte[] Write()
{
var sorted = Assets.OrderBy(a => a.Hash).ToList();
int assetCount = sorted.Count;
int headerSize = 0x10 + assetCount * 4 + assetCount * 8;
var amtaBytesList = new byte[assetCount][];
for (int i = 0; i < assetCount; i++)
amtaBytesList[i] = sorted[i].Amta!.BuildNew();
var amtaPositions = new int[assetCount];
int pos = headerSize;
for (int i = 0; i < assetCount; i++)
{
amtaPositions[i] = pos;
pos += amtaBytesList[i].Length;
}
var audioPositions = new int[assetCount];
for (int i = 0; i < assetCount; i++)
{
if (sorted[i].AudioData == null || sorted[i].AudioData!.Length == 0)
{
audioPositions[i] = -1;
}
else
{
pos = AlignUp(pos, 64);
audioPositions[i] = pos;
pos += sorted[i].AudioData!.Length;
}
}
int fileSize = pos;
using var ms = new MemoryStream(fileSize);
using var writer = new BinaryWriter(ms, Encoding.UTF8);
writer.Write(Encoding.ASCII.GetBytes("BARS"));
writer.Write(fileSize);
writer.Write((ushort)0xFEFF);
writer.Write(Version);
writer.Write(assetCount);
foreach (var asset in sorted)
writer.Write(asset.Hash);
for (int i = 0; i < assetCount; i++)
{
writer.Write(amtaPositions[i]);
writer.Write(audioPositions[i]);
}
for (int i = 0; i < assetCount; i++)
writer.Write(amtaBytesList[i]);
for (int i = 0; i < assetCount; i++)
{
if (audioPositions[i] == -1)
continue;
while (ms.Position < audioPositions[i])
writer.Write((byte)0);
writer.Write(sorted[i].AudioData!);
}
return ms.ToArray();
}
public void Save(string path) => File.WriteAllBytes(path, Write());
public void AddAudio(string name, AmtaFile amta, byte[]? audioData)
{
uint hash = Crc32.Compute(name);
Assets.RemoveAll(a => a.Hash == hash);
Assets.Add(new BarsAsset
{
Hash = hash,
Name = name,
Amta = amta,
AudioData = audioData
});
}
public bool RemoveAsset(string name)
{
uint hash = Crc32.Compute(name);
int removed = Assets.RemoveAll(a => a.Hash == hash);
return removed > 0;
}
public BarsAsset? FindAsset(string name)
{
uint hash = Crc32.Compute(name);
return Assets.FirstOrDefault(a => a.Hash == hash);
}
private static int DetermineAudioEnd(int audioStart, int index, int[] audioOffsets, int fileSize)
{
int minNext = fileSize;
for (int i = 0; i < audioOffsets.Length; i++)
{
if (audioOffsets[i] > 0 && audioOffsets[i] != -1 && audioOffsets[i] > audioStart && audioOffsets[i] < minNext)
minNext = audioOffsets[i];
}
return minNext;
}
private static int AlignUp(int value, int alignment) =>
(value + alignment - 1) / alignment * alignment;
}
public class BarsAsset
{
public uint Hash { get; set; }
public string Name { get; set; } = string.Empty;
public AmtaFile? Amta { get; set; }
public byte[]? AudioData { get; set; }
}