-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbars-to-bwav.cpp
221 lines (194 loc) · 9.34 KB
/
bars-to-bwav.cpp
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
/* Copyright (C) 2020 Jack Zhang (jackz314) - All Rights Reserved
* You may use, distribute and modify this code under the
* terms of the MIT license */
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm> // std::find
//for directory related stuff, need C++ 17
#include <filesystem>
using namespace std;
bool VERBOSE = false;
bool OVERWRITE = true;
bool createDirIfNotExist(const string& pathToCreate){
//create output directory if it doesn't exist
filesystem::path dir(pathToCreate);
if(!filesystem::exists(dir)){
if(!filesystem::create_directory(dir)){
cout << "Failed to create directory: " << pathToCreate << endl;
return false;
}
}
return true;
}
void displayHelp(){
cout << "Usage: bars-to-bwav <bars file or folder containing bars files> [bwav output folder] [--no-overwrite]" << endl;
cout << "or: `bars-to-bwav -h` to bring out this menu." << endl;
cout << "add --no-overwrite flag to prevent from overwriting files with the same names." << endl;
}
int main(int argc, char const *argv[])
{
if(argc < 2){
cout << "Missing file arguments!" << endl;
displayHelp();
return -1;
}
if(argc == 2 && string(argv[1]) == "-h"){
displayHelp();
return 0;
}
vector<string> barsFiles;
if(filesystem::is_directory(argv[1])){// process a directory of bars files
filesystem::directory_iterator end_itr;
filesystem::path p(argv[1]);
cout << "BARS file to be processed in directory " << argv[1] << ':' << endl;
for(filesystem::directory_iterator itr(p); itr != end_itr; itr++){
//only deal with bars files
if (is_regular_file(itr->path()) && itr->path().extension().generic_string() == ".bars") {
string fileName = itr->path().string();
barsFiles.push_back(fileName);
cout << fileName << endl;
}
}
}else{// process single file
barsFiles.push_back(argv[1]);
cout << "BARS file to be processed: " << argv[1] << endl;
}
string baseOutputDir;
if(argc > 2){// have output folder or no overwrite
if(argc == 4 && string(argv[3]) == "--no-overwrite"){ // have no overwrite and output folder
baseOutputDir = argv[2];
OVERWRITE = false;
}
if(argc == 3 && string(argv[2]) == "--no-overwrite"){ // only no overwrite
baseOutputDir = "BWAV-Output/";
OVERWRITE = false;
}else{ // only out dir
cout << '['<<argv[2]<<']' << endl;
baseOutputDir = argv[2];
}
if(baseOutputDir.back() != '/' || baseOutputDir.back() != '\\'){
baseOutputDir += '/';
}
}else{
baseOutputDir = "BWAV-Output/";
}
string spliter = "----------------------------------------";
int totalBWAVCount = 0;
int countB = 0;
for(string barsFile : barsFiles){
ifstream inFile(barsFile, ios::binary);
if(!inFile){
cout << "Failed to open BARS file " << barsFile << endl;
return -2;
}
inFile.seekg(0, ios::end);
streamsize barsSize = inFile.tellg();
inFile.seekg(0,ios::beg);
cout << spliter << '(' << dec << ++countB << '/' << barsFiles.size() << ')' << spliter << endl;
cout << "Processing BARS file " << barsFile << " size: " << barsSize << endl;
vector<char> fBuf(barsSize);
if(!inFile.read(fBuf.data(), barsSize)){
//read filed
cout << "Read file failed." << endl;
}
vector<string> audioNames;
vector<streamoff> startOffsets;
for (streamoff i = 0; i < barsSize - 4; i+=4){
//Hex representation of BWAV's header start (BWAV: 0x42 0x57 0x41 0x56)
if(fBuf[i] == 0x42 && fBuf[i+1] == 0x57 && fBuf[i+2] == 0x41 && fBuf[i+3] == 0x56){
//matched BWAV, send to start offsets
startOffsets.push_back(i);
if(VERBOSE) cout << "Found BWAV at offset 0x" << hex << i <<endl;
}
//Hex representation of BARS's AMTA header start (AMTA: 0x41 0x4D 0x54 0x41)
if(fBuf[i] == 0x41 && fBuf[i+1] == 0x4D && fBuf[i+2] == 0x54 && fBuf[i+3] == 0x41){
//matched AMTA, send to file names
streamsize nameLen = 0;
streamoff nameStart = i+0x48;//start of name string offset +0x48 after start of AMTA tag (possibly after a padding)
if(fBuf[nameStart] == 0x01){
//start is 0x01, skip padding (or something else) of 0x8 to the actual file name
nameStart += 0x8;
/* for(; nameStart < barsSize; nameStart++){
if(fBuf[nameStart] != '\0') break; // start of name, not null
} */
}
//go to file name end
for(streamoff j = nameStart; j < barsSize; j++, nameLen++){
// cout << "CHAR:[" << fBuf[j] << "]. Int: " << int(fBuf[j]) << endl;
if(fBuf[j] == -0x3e){ // some files needs to move start idx forward if -c2 is present, inconsistent
// cout << "FOUND -3e" << endl; // c2 in signed byte is -3e
nameStart = j + 1;
nameLen = -1; // so it's 0 when the next iteration begins
}
if(fBuf[j] == '\0') break; // end of name, null
}
string name = string(fBuf.begin() + nameStart, fBuf.begin() + nameStart + nameLen);
if(find(audioNames.begin(),audioNames.end(), name) != audioNames.end()){//already exists
//deal with duplicates with repeat counter
int repeatCounter = 1;
while(find(audioNames.begin(),audioNames.end(), name + '-' + to_string(repeatCounter)) != audioNames.end()){
repeatCounter++;
}
name += '-' + to_string(repeatCounter);
}
audioNames.push_back(name);
if(VERBOSE) cout << "Found AMTA tag at offset 0x" << hex << i << " Name: " << name << endl;
}
}
cout << endl << "Found all BWAV files. Total count: " << dec << startOffsets.size() << ". Writing bwav files to " << baseOutputDir << endl;
if(!createDirIfNotExist(baseOutputDir)) return -3;
if(audioNames.size() < startOffsets.size()){
cout << "BWAV names count is not the same as BWAV counts!" << endl;
for(size_t i = 1; i < startOffsets.size() - audioNames.size() + 1; i++){
audioNames.push_back("extra_" + to_string(i));
}
}
//write bwav files
string onlyBarsFileName = filesystem::path(barsFile).filename().string();
string outputDir = onlyBarsFileName.substr(0, onlyBarsFileName.size() - 5) + '/';//remove the extension and add directory slash
cout << "Output subdirectory: " << outputDir << endl;
if(!createDirIfNotExist(baseOutputDir + outputDir)) return -3;
int illegal_counter = 0;
for(size_t i = 0; i < startOffsets.size(); i++){
streamoff offset = startOffsets[i];
streamoff nextOffset;
if(i+1 == startOffsets.size()){
nextOffset = barsSize;
}else{
nextOffset = startOffsets[i+1];
}
streamsize writeSize = nextOffset - offset;
string fName = audioNames[i] + ".bwav";
if(VERBOSE) cout << dec << '(' << i+1 << '/' << startOffsets.size() << ") Writing " << fName << " from offset 0x" << hex << offset << endl;
string oFilePath = baseOutputDir + outputDir + fName;
//deal with duplicate file names, already dealt with before, here to deal with pre-existing ones
if(!OVERWRITE && filesystem::exists(oFilePath)){//don't overwrite
int repeatCounter = 1;
string newOFilePath = oFilePath.substr(0,oFilePath.size()-5) + '-';
while(filesystem::exists(newOFilePath + to_string(repeatCounter) + ".bwav")){//already exists, use another name
repeatCounter++;
}
oFilePath = newOFilePath + to_string(repeatCounter) + ".bwav";
}
fstream oFile(oFilePath, ios::out | ios::binary);
if(!oFile.write(fBuf.data()+offset, writeSize)){// if failed, probably due to illegal name
++illegal_counter;
oFilePath = baseOutputDir + outputDir + "illegal_name_" + to_string(illegal_counter) + ".bwav";
fstream oFileRetry(oFilePath, ios::out | ios::binary); // try again
if(!oFileRetry.write(fBuf.data()+offset, writeSize)){ // still failed
cout << "Write bwav failed. Path: [" << oFilePath << "] Count: " << dec << i << " Offset: 0x" << hex << offset << endl;
}else{
++totalBWAVCount;
}
oFileRetry.close();
}else{
++totalBWAVCount;
}
oFile.close();
}
}
cout << spliter << spliter << endl;
cout << "Done! Processed " << dec << barsFiles.size() << " BARS files and generated " << totalBWAVCount << " BWAV files in total." << endl;
return 0;
}