-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
197 lines (190 loc) · 7.64 KB
/
Program.cs
File metadata and controls
197 lines (190 loc) · 7.64 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Dynamic;
using System.Data.Sql;
using System.Collections;
using System.Net;
using System.Threading;
using System.Data;
using System.CodeDom;
namespace BNKManager
{
class Program
{
static void Main(string[] args)
{
var i = 0;
WwiseBank myBank = null;
string TargetBankName = null;
foreach (string bnk in args)
{
if (Path.GetExtension(bnk) == ".bnk")
{
myBank = new WwiseBank(bnk);
TargetBankName = Path.GetFileName(bnk);
break;
}
}
if (myBank == null)
{
Console.WriteLine("Please, Drag .bnk file to me");
Console.ReadKey();
return;
}
args = args.Where((val => Path.GetExtension(val) == ".wem")).ToArray();
if (args.Count() == 0)
{
int b = 0;
if (Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "/wems"))
{
foreach (string file in Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + "wems"))
{
if(Path.GetExtension(file) == ".wem") {
args = args.Append(file).ToArray();
b++;
}
}
}
if (b == 0) {
args = null;
}
}
Console.WriteLine("Scanning: " + TargetBankName + "\n-----------------------------------");
double[] duration = { };
foreach (BankSection banksection in myBank.bankSections)
{
if (banksection is DATASection)
{
DATASection sect = banksection as DATASection;
foreach (DATASection.WEMFile file in sect.wemFiles)
{
duration = duration.Append(GetAudioFileSeconds(ref file.data)).ToArray();
}
}
}
foreach (BankSection banksection in myBank.bankSections)
{
if (banksection is HIRCSection)
{
HIRCSection sect = banksection as HIRCSection;
foreach (WwiseObject hh in sect.objects)
{
if (hh.objectType is WwiseObjectType.Sound_SFX__Sound_Voice)
{
SoundSFXVoiceWwiseObject obj = hh as SoundSFXVoiceWwiseObject;
Console.WriteLine($"DATA WEM: #{i+1}");
Console.WriteLine("ID: " + obj.ID.ToString());
Console.WriteLine("Audio File ID: " + obj.audioFileID.ToString());
Console.WriteLine("Sound Type: " + obj.soundType.ToString());
Console.WriteLine("Stream Type: " + obj.streamType.ToString());
Console.WriteLine("Duration: " + duration[i].ToString() + " sec\n");
i++;
}
}
}
}
Console.WriteLine("Total Files: " + i.ToString() + "\n-----------------------------------");
if (args == null)
{
Console.ReadKey();
return;
}
Console.WriteLine("Replacing: " + TargetBankName + "\n");
foreach (string ifile in args)
{
try
{
myBank = EditAudioFile(myBank, Convert.ToUInt32(Path.GetFileNameWithoutExtension(ifile)), File.ReadAllBytes(ifile));
}
catch (FormatException)
{
Console.WriteLine("Error: Wrong .wem Name!");
Console.ReadKey();
return;
}
catch (OverflowException)
{
Console.WriteLine("Error: Long .wem Name!");
Console.ReadKey();
return;
}
catch
{
Console.WriteLine("Error: can't convert .wem");
Console.ReadKey();
return;
}
}
if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "/Repacked Banks")) Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "/Repacked Banks");
myBank.Save(AppDomain.CurrentDomain.BaseDirectory + "/Repacked Banks/" + TargetBankName);
Console.ReadKey();
}
public static WwiseBank EditAudioFile(WwiseBank CurrentBank, uint fileID, byte[] newData)
{
var dataIndex = (DIDXSection)null;
var data = (DATASection)null;
var hirc = (HIRCSection)null;
foreach (BankSection Section in CurrentBank.bankSections) {
if (Section is DIDXSection) dataIndex = (DIDXSection)Section;
if (Section is DATASection) data = (DATASection)Section;
if (Section is HIRCSection) hirc = (HIRCSection)Section;
}
if (dataIndex != null && data != null)
{
uint lastOffset = 0;
for (int i = 0; i < dataIndex.embeddedWEMFiles.Count; i++)
{
dataIndex.embeddedWEMFiles[i].offset = lastOffset;
if (dataIndex.embeddedWEMFiles[i].ID == fileID)
{
dataIndex.embeddedWEMFiles[i].length = (uint)newData.Length;
}
lastOffset += dataIndex.embeddedWEMFiles[i].length + 10;
}
for (int i = 0; i < data.wemFiles.Count; i++)
{
if (data.wemFiles[i].info.ID == fileID)
{
Console.WriteLine($"Replaced: {fileID}.wem ({i+1})");
data.wemFiles[i].data = newData;
}
}
if (hirc != null)
{
foreach (WwiseObject obj in hirc.objects)
{
if (obj.objectType == WwiseObjectType.Sound_SFX__Sound_Voice)
{
SoundSFXVoiceWwiseObject soundObj = (SoundSFXVoiceWwiseObject)obj;
DIDXSection.EmbeddedWEM gotEmbedded = dataIndex.GetEmbeddedWEM(soundObj.audioFileID);
if (gotEmbedded != null)
{
soundObj.fileOffset = (uint)data.dataStartOffset + gotEmbedded.offset;
soundObj.fileLength = gotEmbedded.length;
}
}
}
}
}
return CurrentBank;
}
public static double GetAudioFileSeconds(ref byte[] data)
{
double result = 0;
using (BinaryReader br = new BinaryReader(new MemoryStream(data)))
{
br.BaseStream.Seek(4, SeekOrigin.Begin);
float fileSize = br.ReadUInt32();
br.BaseStream.Seek(28, SeekOrigin.Begin);
float bytesPerSecond = br.ReadUInt32();
float seconds = fileSize / bytesPerSecond;
result = Math.Round(seconds, 3);
}
return result;
}
}
}