1
0
mirror of synced 2024-11-27 15:50:48 +01:00

add cpk support

This commit is contained in:
kohos 2020-03-12 10:35:30 +08:00
parent 735fe414ca
commit abb4cd24cb
28 changed files with 1787 additions and 1642 deletions

View File

@ -15,6 +15,7 @@ node index.js <Command> <Options> <Path>...
```shell ```shell
acb2hcas [-d] [-k <key>] [-t <type>] [-o <outputDir>] [-s] <acbPath>... acb2hcas [-d] [-k <key>] [-t <type>] [-o <outputDir>] [-s] <acbPath>...
acb2wavs [-k <key>] [-o <outputDir>] [-v <volume>] [-m <mode>] [-s] <acbPath>... acb2wavs [-k <key>] [-o <outputDir>] [-v <volume>] [-m <mode>] [-s] <acbPath>...
acb_mix [-k <key>] [-o <outputDir>] [-v <volume>] [-m <mode>] [-s] <acbPath>...
awb2hcas [-d] [-k <key>] [-w <awbKey>] [-t <type>] [-o <outputDir>] [-s] <awbPath>... awb2hcas [-d] [-k <key>] [-w <awbKey>] [-t <type>] [-o <outputDir>] [-s] <awbPath>...
awb2wavs [-k <key>] [-o <outputDir>] [-v <volume>] [-m <mode>] [-s] <awbPath>... awb2wavs [-k <key>] [-o <outputDir>] [-v <volume>] [-m <mode>] [-s] <awbPath>...
hca2wav [-k <key>] [-o <outputFile>] [-v <volume>] [-m <mode>] <hcaPath>... hca2wav [-k <key>] [-o <outputFile>] [-v <volume>] [-m <mode>] <hcaPath>...
@ -22,7 +23,7 @@ view_utf [-o <outputFile>] <acbPath/acfPath>...
decrypt_acb [-k <key>] [-t <type>] <acbPath>... decrypt_acb [-k <key>] [-t <type>] <acbPath>...
decrypt_awb [-k <key>] [-t <type>] <awbPath>... decrypt_awb [-k <key>] [-t <type>] <awbPath>...
decrypt_hca [-k <key>] [-w <awbKey>] [-t <type>] <hcaPath>... decrypt_hca [-k <key>] [-w <awbKey>] [-t <type>] <hcaPath>...
mix_acb [-k <key>] [-o <outputDir>] [-v <volume>] [-m <mode>] [-s] <acbPath>... extract_cpk [-o <outputDir>] <cpkPath>...
``` ```
## Options ## Options
@ -43,6 +44,8 @@ acb2hcas - Extract acb file to hca files (with/without decrypt)
acb2wavs - Extract acb file and convert hca files To wav files acb2wavs - Extract acb file and convert hca files To wav files
acb_mix - Experimental. Convert acb file (and awb files) to mixed wav files
awb2hcas - Extract awb file to hca files (with/without decrypt) awb2hcas - Extract awb file to hca files (with/without decrypt)
awb2wavs - Extract awb file and convert hca files To wav files awb2wavs - Extract awb file and convert hca files To wav files
@ -57,7 +60,7 @@ decrypt_awb - Decrypt awb file (Warning: will overwrite orignal file)
decrypt_hca - Decrypt hca file (Warning: will overwrite orignal file) decrypt_hca - Decrypt hca file (Warning: will overwrite orignal file)
mix_acb - Experimental. Convert acb file (and awb files) to mixed wav files extract_cpk - Extract cpk file
## Tips ## Tips

3
acb2hcas.bat Normal file
View File

@ -0,0 +1,3 @@
@echo off
node.exe "%~dp0src\index.js" acb2hcas %*
pause

View File

@ -1,5 +1,5 @@
@echo off @echo off
set /P key=Please input key and press Enter: set /P key=Please input key and press Enter:
if "%key%"=="" set key=0 if "%key%"=="" set key=0
node.exe "%~dp0index.js" acb2hcas -d -k %key% %* node.exe "%~dp0src\index.js" acb2hcas -d -k %key% %*
pause pause

View File

@ -1,5 +1,5 @@
@echo off @echo off
set /P key=Please input key and press Enter: set /P key=Please input key and press Enter:
if "%key%"=="" set key=0 if "%key%"=="" set key=0
node.exe "%~dp0index.js" mix_acb -k %key% %* node.exe "%~dp0src\index.js" acb2wavs -k %key% %*
pause pause

View File

@ -1,5 +1,5 @@
@echo off @echo off
set /P key=Please input key and press Enter: set /P key=Please input key and press Enter:
if "%key%"=="" set key=0 if "%key%"=="" set key=0
node.exe "%~dp0index.js" awb2wavs -k %key% %* node.exe "%~dp0src\index.js" acb_mix -k %key% %*
pause pause

3
awb2hcas.bat Normal file
View File

@ -0,0 +1,3 @@
@echo off
node.exe "%~dp0src\index.js" awb2hcas %*
pause

5
awb2hcas_decrypt.bat Normal file
View File

@ -0,0 +1,5 @@
@echo off
set /P key=Please input key and press Enter:
if "%key%"=="" set key=0
node.exe "%~dp0src\index.js" awb2hcas -d -k %key% %*
pause

View File

@ -1,5 +1,5 @@
@echo off @echo off
set /P key=Please input key and press Enter: set /P key=Please input key and press Enter:
if "%key%"=="" set key=0 if "%key%"=="" set key=0
node.exe "%~dp0index.js" acb2wavs -k %key% %* node.exe "%~dp0src\index.js" awb2wavs -k %key% %*
pause pause

5
decrypt_acb.bat Normal file
View File

@ -0,0 +1,5 @@
@echo off
set /P key=Please input key and press Enter:
if "%key%"=="" set key=0
node.exe "%~dp0src\index.js" decrypt_acb -k %key% %*
pause

5
decrypt_awb.bat Normal file
View File

@ -0,0 +1,5 @@
@echo off
set /P key=Please input key and press Enter:
if "%key%"=="" set key=0
node.exe "%~dp0src\index.js" decrypt_awb -k %key% %*
pause

View File

@ -1,7 +1,7 @@
@echo off @echo off
set /P key=Please input key and press Enter: set /P key=Please input key and press Enter:
if "%key%"=="" set key=0 if "%key%"=="" set key=0
set /P awbKey=Please input awbKey and press Enter: set /P awbKey=Please input awbKey and press Enter:
if "%awbKey%"=="" set awbKey=0 if "%awbKey%"=="" set awbKey=0
node.exe "%~dp0index.js" decrypt_hca -k %key% -w %awbKey% %* node.exe "%~dp0src\index.js" decrypt_hca -k %key% -w %awbKey% %*
pause pause

3
extract_cpk.bat Normal file
View File

@ -0,0 +1,3 @@
@echo off
node.exe "%~dp0src\index.js" extract_cpk %*
pause

View File

@ -1,7 +1,7 @@
@echo off @echo off
set /P key=Please input key and press Enter: set /P key=Please input key and press Enter:
if "%key%"=="" set key=0 if "%key%"=="" set key=0
set /P awbKey=Please input awbKey and press Enter: set /P awbKey=Please input awbKey and press Enter:
if "%awbKey%"=="" set awbKey=0 if "%awbKey%"=="" set awbKey=0
node.exe "%~dp0index.js" hca2wav -k %key% -w %awbKey% %* node.exe "%~dp0src\index.js" hca2wav -k %key% -w %awbKey% %*
pause pause

View File

@ -61,3 +61,6 @@ ONE PIECE DANCE BATTLE
アイドルマスター ミリオンライブ! シアターデイズ アイドルマスター ミリオンライブ! シアターデイズ
765765765765765 765765765765765
ハイスクール・フリート 艦隊バトルでピンチ!
43472919336422565

View File

@ -2,7 +2,7 @@
"name": "critools", "name": "critools",
"version": "1.0.0", "version": "1.0.0",
"description": "JavaScript tools for extract audio from game file", "description": "JavaScript tools for extract audio from game file",
"main": "index.js", "main": "src/index.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },

View File

@ -1,383 +1,287 @@
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const util = require('util'); const util = require('util');
const crypto = require('crypto'); const crypto = require('crypto');
const utf = require('./utf'); const afs2 = require('./afs2');
const afs2 = require('./afs2'); const hca = require('./hca');
const hca = require('./hca'); const utf = require('./utf');
const readFile = util.promisify(fs.readFile); const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile); const writeFile = util.promisify(fs.writeFile);
const mkdir = util.promisify(fs.mkdir); const mkdir = util.promisify(fs.mkdir);
function findZero(buffer, start) { async function parseAcb(acbPath) {
while (buffer[start] !== 0x0) start++; const pathInfo = path.parse(acbPath);
return start; const buffer = await readFile(acbPath);
} const utfs = utf.parseUtf(buffer);
if (!utfs) throw new Error(`NOT ACB FILE`);
function parseUtf(buffer, toString) { if (utfs.length !== 1) debugger;
if (!buffer || buffer.length < 4) return null; const acb = utfs[0];
let pos = 0; acb.buffer = buffer;
const config = {}; acb.memoryHcas = await afs2.parseAFS2(acb.AwbFile);
config.magic = buffer.slice(pos, 4).toString(); pos += 4; acb.streamHcas = [];
if (config.magic !== '@UTF') return null; for (let i = 0; i < acb.StreamAwbHash.length; i++) {
config.dataSize = buffer.readUInt32BE(pos); pos += 4; const StreamAwb = acb.StreamAwbHash[i];
buffer = buffer.slice(pos); const awbPath = path.join(pathInfo.dir, StreamAwb.Name + '.awb');
pos = 0; if (fs.existsSync(awbPath)) {
config.unknown = buffer.readUInt16BE(pos); pos += 2; const obj = await afs2.parseAFS2(awbPath);
if (config.unknown !== 1) debugger; acb.streamHcas.push(obj);
config.valueOffset = buffer.readUInt16BE(pos); pos += 2; }
config.stringOffset = buffer.readUInt32BE(pos); pos += 4; }
config.dataOffset = buffer.readUInt32BE(pos); pos += 4; for (let i = 0; i < acb.WaveformTable.length; i++) {
config.nameOffset = buffer.readUInt32BE(pos); pos += 4; const Waveform = acb.WaveformTable[i];
config.elementCount = buffer.readUInt16BE(pos); pos += 2; const isMemory = Waveform.Streaming === 0;
config.valueSize = buffer.readUInt16BE(pos); pos += 2; if (!isMemory) {
config.pageCount = buffer.readUInt32BE(pos); pos += 4; if (!acb.streamHcas[Waveform.StreamAwbPortNo]) {
let stringEnd = findZero(buffer, config.stringOffset); throw new Error(`MISSING ${acb.StreamAwbHash[i].Name}.awb`);
config.name = buffer.slice(config.stringOffset, stringEnd).toString(); }
let valuePos = config.valueOffset; }
const pages = []; }
config.types = []; return acb;
let firstPos = pos; }
for (let i = 0; i < config.pageCount; i++) {
let page = {}; async function parseCommand(acb, command, key) {
pos = firstPos; let samplingRate = 0, channelCount = 0;
for (let j = 0; j < config.elementCount; j++) { let k = 0;
const type = buffer.readUInt8(pos); pos = pos + 1; const commands = [];
if (i === 0) config.types[j] = type; while (k < command.length) {
let stringOffset = config.stringOffset + buffer.readUInt32BE(pos); pos += 4; const cmd = command.readUInt16BE(k); k += 2;
stringEnd = findZero(buffer, stringOffset); const len = command.readUInt8(k); k += 1;
const key = buffer.slice(stringOffset, stringEnd).toString(); if (len !== 4 && len !== 0) debugger;
let offset = 0; let file, u16;
switch (type >>> 5) { switch (cmd) {
case 0: debugger; break; case 0x0000:
case 1: offset = pos; break; k = command.length;
case 2: offset = valuePos; break; break;
} case 0x07d0: // Start Waveform
let value; u16 = command.readUInt16BE(k); k += 2;
switch (type & 0x1F) { if (u16 !== 0x0002) debugger;
case 0x10: value = buffer.readInt8(offset); offset += 1; break; const SynthIndex = command.readUInt16BE(k); k += 2;
case 0x11: value = buffer.readUInt8(offset); offset += 1; break; const Synth = acb.SynthTable[SynthIndex];
case 0x12: value = buffer.readInt16BE(offset); offset += 2; break; u16 = Synth.ReferenceItems.readUInt16BE(0);
case 0x13: value = buffer.readUInt16BE(offset); offset += 2; break; if (u16 !== 0x0001) debugger;
case 0x14: value = buffer.readInt32BE(offset); offset += 4; break; const WaveformIndex = Synth.ReferenceItems.readUInt16BE(2);
case 0x15: value = buffer.readUInt32BE(offset); offset += 4; break; const Waveform = acb.WaveformTable[WaveformIndex];
case 0x16: value = buffer.readBigInt64BE(offset); offset += 8; break; const isMemory = Waveform.Streaming === 0;
case 0x17: value = buffer.readBigUInt64BE(offset); offset += 8; break; if (Waveform.EncodeType === 2) {
case 0x18: value = buffer.readFloatBE(offset); offset += 4; break; file = isMemory ? acb.memoryHcas[Waveform.MemoryAwbId] : acb.streamHcas[Waveform.StreamAwbPortNo][Waveform.StreamAwbId];
case 0x19: debugger; value = buffer.readDoubleBE(offset); offset += 8; break; if (Buffer.isBuffer(file)) {
case 0x1A: const awbKey = isMemory ? acb.memoryHcas.config.key : acb.streamHcas[Waveform.StreamAwbPortNo].config.key;
stringOffset = config.stringOffset + buffer.readUInt32BE(offset); offset += 4; file = await hca.decodeHca(file, key, awbKey);
stringEnd = findZero(buffer, stringOffset); if (isMemory) acb.memoryHcas[Waveform.MemoryAwbId] = file; else acb.streamHcas[Waveform.StreamAwbPortNo][Waveform.StreamAwbId] = file;
value = buffer.slice(stringOffset, stringEnd).toString(); if (samplingRate === 0) samplingRate = file.samplingRate; else if (samplingRate !== file.samplingRate) throw new Error(`SamplingRate Different`);
break; if (channelCount === 0) channelCount = file.channelCount; else if (channelCount !== file.channelCount) throw new Error(`ChannelCount Different`);
case 0x1B: }
const bufferStart = config.dataOffset + buffer.readUInt32BE(offset); offset += 4; } else {
const bufferLen = buffer.readUInt32BE(offset); offset += 4; throw new Error(`Not HCA File`);
value = buffer.slice(bufferStart, bufferStart + bufferLen); }
let temp = parseUtf(value, toString); commands.push({ type: 0, pcmData: file.pcmData });
if (temp) value = temp; else if (toString) value = buffer.slice(bufferStart, bufferStart + bufferLen).toString('hex'); break;
break; case 0x07d1: // Set Position
} const StartOffset = command.readUInt32BE(k); k += 4;
switch (type >>> 5) { if (StartOffset > 3600000) debugger;
case 0: debugger; break; commands.push({ type: 1, offset: StartOffset });
case 1: pos = offset; break; break;
case 2: valuePos = offset; break; default:
} debugger;
page[key] = value; break;
} }
pages.push(page); }
} return { commands, samplingRate, channelCount };
pages.config = config; }
return pages;
} async function mixAcb(acbPath, key, wavDir, mode, skip) {
exports.parse = parseUtf; const pathInfo = path.parse(acbPath);
console.log(`Parsing ${pathInfo.base}...`);
async function parseAcb(acbPath) { const acb = await parseAcb(acbPath);
const pathInfo = path.parse(acbPath); if (wavDir === undefined) wavDir = path.join(pathInfo.dir, acb.Name);
const buffer = await readFile(acbPath); if (!fs.existsSync(wavDir)) {
const utfs = utf.parse(buffer); await mkdir(wavDir, { recursive: true });
if (!utfs) throw new Error(`NOT ACB FILE`); } else if (skip) {
if (utfs.length !== 1) debugger; console.log(`Skipped ${pathInfo.base}...`);
const acb = utfs[0]; return;
acb.buffer = buffer; }
acb.memoryHcas = await afs2.parse(acb.AwbFile); console.log(`Mixing ${pathInfo.base}...`);
acb.streamHcas = []; const cueNameMap = {};
for (let i = 0; i < acb.StreamAwbHash.length; i++) { for (let i = 0; i < acb.CueNameTable.length; i++) {
const StreamAwb = acb.StreamAwbHash[i]; const cueName = acb.CueNameTable[i];
const awbPath = path.join(pathInfo.dir, StreamAwb.Name + '.awb'); cueNameMap[cueName.CueIndex] = cueName.CueName;
if (fs.existsSync(awbPath)) { }
const obj = await afs2.parse(awbPath); for (let i = 0; i < acb.CueTable.length; i++) {
acb.streamHcas.push(obj); const Cue = acb.CueTable[i];
} let samplingRate = 0, channelCount = 0;
} if (Cue.ReferenceType !== 3) debugger;
for (let i = 0; i < acb.WaveformTable.length; i++) { const Sequence = acb.SequenceTable[Cue.ReferenceIndex];
const Waveform = acb.WaveformTable[i]; // Sequence.Type: 0 - Polyphonic, 1 - Sequential, Random, Random No Repeat, Switch, Shuffle Cue, Combo Sequential, Track Transition by Selector
const isMemory = Waveform.Streaming === 0; const timeline = [];
if (!isMemory) { let size = 0;
if (!acb.streamHcas[Waveform.StreamAwbPortNo]) { for (let j = 0; j < Sequence.NumTracks; j++) {
throw new Error(`MISSING ${acb.StreamAwbHash[i].Name}.awb`); const index = Sequence.TrackIndex.readUInt16BE(j * 2);
} const Track = acb.TrackTable[index];
} const TrackEvent = acb.TrackEventTable[Track.EventIndex];
} const track = await parseCommand(acb, TrackEvent.Command, key);
return acb; if (track.samplingRate) {
} if (samplingRate === 0) samplingRate = track.samplingRate; else if (track.samplingRate !== samplingRate) throw new Error(`SamplingRate Different`);
}
async function parseCommand(acb, command, key) { if (track.channelCount) {
let samplingRate = 0, channelCount = 0; if (channelCount === 0) channelCount = track.channelCount; else if (track.channelCount !== channelCount) throw new Error(`ChannelCount Different`);
let k = 0; }
const commands = []; let time = 0;
while (k < command.length) { for (let k = 0; k < track.commands.length; k++) {
const cmd = command.readUInt16BE(k); k += 2; const command = track.commands[k];
const len = command.readUInt8(k); k += 1; switch (command.type) {
if (len !== 4 && len !== 0) debugger; case 0:
let file, u16; let m = 0;
switch (cmd) { while (m < timeline.length && time > timeline[m].time) m++;
case 0x0000: let offset = Math.round(time * samplingRate * channelCount / 1000);
k = command.length; if (offset % channelCount !== 0) offset += channelCount - offset % channelCount;
break; if (m == timeline.length) timeline.push({ time, offset, pcmDatas: [] });
case 0x07d0: // Start Waveform const last = timeline[m].offset + command.pcmData.length;
u16 = command.readUInt16BE(k); k += 2; if (last > size) size = last;
if (u16 !== 0x0002) debugger; timeline[m].pcmDatas.push(command.pcmData);
const SynthIndex = command.readUInt16BE(k); k += 2; break;
const Synth = acb.SynthTable[SynthIndex]; case 1:
u16 = Synth.ReferenceItems.readUInt16BE(0); time += command.offset;
if (u16 !== 0x0001) debugger; break;
const WaveformIndex = Synth.ReferenceItems.readUInt16BE(2); }
const Waveform = acb.WaveformTable[WaveformIndex]; }
const isMemory = Waveform.Streaming === 0; }
if (Waveform.EncodeType === 2) { if (size === 0) continue;
file = isMemory ? acb.memoryHcas[Waveform.MemoryAwbId] : acb.streamHcas[Waveform.StreamAwbPortNo][Waveform.StreamAwbId]; const pcmData = new Float32Array(size);
if (Buffer.isBuffer(file)) { if (timeline.length === 0) continue;
const awbKey = isMemory ? acb.memoryHcas.config.key : acb.streamHcas[Waveform.StreamAwbPortNo].config.key; timeline.push({ offset: 0xFFFFFFFF, pcmDatas: [] });
file = await hca.decode(file, key, awbKey); const runnings = [];
if (isMemory) acb.memoryHcas[Waveform.MemoryAwbId] = file; else acb.streamHcas[Waveform.StreamAwbPortNo][Waveform.StreamAwbId] = file; let now = timeline[0].offset;
if (samplingRate === 0) samplingRate = file.samplingRate; else if (samplingRate !== file.samplingRate) throw new Error(`SamplingRate Different`); for (let i = 0; i < timeline.length; i++) {
if (channelCount === 0) channelCount = file.channelCount; else if (channelCount !== file.channelCount) throw new Error(`ChannelCount Different`); const wave = timeline[i];
} const len = wave.offset - now;
} else { const pcmDatas = [];
throw new Error(`Not HCA File`); let k = 0;
} while (k < runnings.length) {
commands.push({ type: 0, pcmData: file.pcmData }); const running = runnings[k];
break; let end = running.offset + len;
case 0x07d1: // Set Position if (end >= running.pcmData.length) {
const StartOffset = command.readUInt32BE(k); k += 4; pcmDatas.push(running.pcmData.slice(running.offset));
if (StartOffset > 3600000) debugger; runnings.splice(k, 1);
commands.push({ type: 1, offset: StartOffset }); } else {
break; pcmDatas.push(running.pcmData.slice(running.offset, end));
default: running.offset = end;
debugger; k++;
break; }
} }
} for (let j = 0; j < wave.pcmDatas.length; j++) {
return { commands, samplingRate, channelCount }; runnings.push({
} pcmData: wave.pcmDatas[j],
offset: 0
async function mixAcb(acbPath, key, wavDir, mode, skip) { });
const pathInfo = path.parse(acbPath); }
console.log(`Parsing ${pathInfo.base}...`); k = now;
const acb = await parseAcb(acbPath); if (pcmDatas.length > 0) {
if (wavDir === undefined) wavDir = path.join(pathInfo.dir, acb.Name); let max = 0;
if (!fs.existsSync(wavDir)) { for (let j = 1; j < pcmDatas.length; j++) if (pcmDatas[j].length > max) max = j;
await mkdir(wavDir, { recursive: true }); for (let j = 0; j < pcmDatas[max].length; j++) {
} else if (skip) { let f = 0;
console.log(`Skipped ${pathInfo.base}...`); for (let m = 0; m < pcmDatas.length; m++) {
return; if (j < pcmDatas[m].length) f += pcmDatas[m][j];
} }
console.log(`Mixing ${pathInfo.base}...`); if (f > 1.0) f = 1.0;
const cueNameMap = {}; if (f < -1.0) f = -1.0;
for (let i = 0; i < acb.CueNameTable.length; i++) { pcmData[k++] = f;
const cueName = acb.CueNameTable[i]; }
cueNameMap[cueName.CueIndex] = cueName.CueName; }
} now = wave.offset;
for (let i = 0; i < acb.CueTable.length; i++) { }
const Cue = acb.CueTable[i]; const wavPath = path.join(wavDir, cueNameMap[i] + '.wav');
let samplingRate = 0, channelCount = 0; console.log(`Writing ${cueNameMap[i] + '.wav'}...`);
if (Cue.ReferenceType !== 3) debugger; await hca.writeWavFile(wavPath, mode, channelCount, samplingRate, pcmData);
const Sequence = acb.SequenceTable[Cue.ReferenceIndex]; }
// Sequence.Type: 0 - Polyphonic, 1 - Sequential, Random, Random No Repeat, Switch, Shuffle Cue, Combo Sequential, Track Transition by Selector }
const timeline = []; exports.mixAcb = mixAcb;
let size = 0;
for (let j = 0; j < Sequence.NumTracks; j++) { async function acb2hcas(acbPath, key, hcaDir, type, skip) {
const index = Sequence.TrackIndex.readUInt16BE(j * 2); const pathInfo = path.parse(acbPath);
const Track = acb.TrackTable[index]; console.log(`Parsing ${pathInfo.base}...`);
const TrackEvent = acb.TrackEventTable[Track.EventIndex]; const acb = await parseAcb(acbPath);
const track = await parseCommand(acb, TrackEvent.Command, key); if (hcaDir === undefined) hcaDir = path.join(pathInfo.dir, acb.Name);
if (track.samplingRate) { if (!fs.existsSync(hcaDir)) {
if (samplingRate === 0) samplingRate = track.samplingRate; else if (track.samplingRate !== samplingRate) throw new Error(`SamplingRate Different`); await mkdir(hcaDir, { recursive: true });
} } else if (skip) {
if (track.channelCount) { console.log(`Skipped ${pathInfo.base}...`);
if (channelCount === 0) channelCount = track.channelCount; else if (track.channelCount !== channelCount) throw new Error(`ChannelCount Different`); return;
} }
let time = 0; console.log(`Extracting ${pathInfo.base}...`);
for (let k = 0; k < track.commands.length; k++) { let memory = 0, stream = 0;
const command = track.commands[k]; for (let i = 0; i < acb.WaveformTable.length; i++) {
switch (command.type) { const Waveform = acb.WaveformTable[i];
case 0: const isMemory = Waveform.Streaming === 0;
let m = 0; const hcaBuffer = isMemory ? acb.memoryHcas[Waveform.MemoryAwbId] : acb.streamHcas[Waveform.StreamAwbPortNo][Waveform.StreamAwbId];
while (m < timeline.length && time > timeline[m].time) m++; const awbKey = isMemory ? acb.memoryHcas.config.key : acb.streamHcas[Waveform.StreamAwbPortNo].config.key;
let offset = Math.round(time * samplingRate * channelCount / 1000); const name = isMemory ? `memory_${++memory}.hca` : `stream_${++stream}.hca`;
if (offset % channelCount !== 0) offset += channelCount - offset % channelCount; const hcaPath = path.join(hcaDir, name);
if (m == timeline.length) timeline.push({ time, offset, pcmDatas: [] }); if (key !== undefined) {
const last = timeline[m].offset + command.pcmData.length; console.log(`Decrypting ${name}...`);
if (last > size) size = last; await hca.decryptHca(hcaBuffer, key, awbKey, type);
timeline[m].pcmDatas.push(command.pcmData); }
break; console.log(`Writing ${name}...`);
case 1: await writeFile(hcaPath, hcaBuffer);
time += command.offset; }
break; }
} exports.acb2hcas = acb2hcas;
}
} async function acb2wavs(acbPath, key, wavDir, volume, mode, skip) {
if (size === 0) continue; const pathInfo = path.parse(acbPath);
const pcmData = new Float32Array(size); console.log(`Parsing ${pathInfo.base}...`);
if (timeline.length === 0) continue; const acb = await parseAcb(acbPath);
timeline.push({ offset: 0xFFFFFFFF, pcmDatas: [] }); if (wavDir === undefined) wavDir = path.join(pathInfo.dir, acb.Name);
const runnings = []; if (!fs.existsSync(wavDir)) {
let now = timeline[0].offset; await mkdir(wavDir, { recursive: true });
for (let i = 0; i < timeline.length; i++) { } else if (skip) {
const wave = timeline[i]; console.log(`Skipped ${pathInfo.base}...`);
const len = wave.offset - now; return;
const pcmDatas = []; }
let k = 0; console.log(`Extracting ${pathInfo.base}...`);
while (k < runnings.length) { let memory = 0, stream = 0;
const running = runnings[k]; for (let i = 0; i < acb.WaveformTable.length; i++) {
let end = running.offset + len; const Waveform = acb.WaveformTable[i];
if (end >= running.pcmData.length) { const isMemory = Waveform.Streaming === 0;
pcmDatas.push(running.pcmData.slice(running.offset)); const hcaBuffer = isMemory ? acb.memoryHcas[Waveform.MemoryAwbId] : acb.streamHcas[Waveform.StreamAwbPortNo][Waveform.StreamAwbId];
runnings.splice(k, 1); const awbKey = isMemory ? acb.memoryHcas.config.key : acb.streamHcas[Waveform.StreamAwbPortNo].config.key;
} else { const name = isMemory ? `memory_${++memory}.wav` : `stream_${++stream}.wav`;
pcmDatas.push(running.pcmData.slice(running.offset, end)); const wavPath = path.join(wavDir, name);
running.offset = end; await hca.decodeHcaToWav(hcaBuffer, key, awbKey, wavPath, volume, mode);
k++; }
} }
} exports.acb2wavs = acb2wavs;
for (let j = 0; j < wave.pcmDatas.length; j++) {
runnings.push({ async function decryptAcb(acbPath, key, type) {
pcmData: wave.pcmDatas[j], const pathInfo = path.parse(acbPath);
offset: 0 console.log(`Parsing ${pathInfo.base}...`);
}); const acb = await parseAcb(acbPath);
} console.log(`Decrypting ${pathInfo.base}...`);
k = now; if (acb.memoryHcas) {
if (pcmDatas.length > 0) { for (let i = 0; i < acb.memoryHcas.length; i++) {
let max = 0; await hca.decryptHca(acb.memoryHcas[i], key, acb.memoryHcas.config.key, type);
for (let j = 1; j < pcmDatas.length; j++) if (pcmDatas[j].length > max) max = j; }
for (let j = 0; j < pcmDatas[max].length; j++) { acb.memoryHcas.config.buffer.writeUInt16BE(0, 0xE);
let f = 0; }
for (let m = 0; m < pcmDatas.length; m++) { for (let i = 0; i < acb.StreamAwbHash.length; i++) {
if (j < pcmDatas[m].length) f += pcmDatas[m][j]; for (let j = 0; j < acb.streamHcas[i].length; j++) {
} await hca.decryptHca(acb.streamHcas[i][j], key, acb.streamHcas[i].config.key, type);
if (f > 1.0) f = 1.0; }
if (f < -1.0) f = -1.0; const buffer = acb.streamHcas[i].config.buffer;
pcmData[k++] = f; buffer.writeUInt16BE(0, 0xE);
} const md5 = crypto.createHash('md5');
} md5.update(buffer);
now = wave.offset; const hash = md5.digest();
} const awb = acb.StreamAwbHash[i];
const wavPath = path.join(wavDir, cueNameMap[i] + '.wav'); hash.copy(awb.Hash);
console.log(`Writing ${cueNameMap[i] + '.wav'}...`); await writeFile(path.join(pathInfo.dir, awb.Name + '.awb'), buffer);
await hca.writeWavFile(wavPath, mode, channelCount, samplingRate, pcmData); if (acb.StreamAwbAfs2Header) {
} const Header = acb.StreamAwbAfs2Header[i].Header;
} buffer.copy(Header, 0, 0, Header.length);
exports.mixAcb = mixAcb; }
}
async function acb2hcas(acbPath, key, hcaDir, type, skip) { await writeFile(acbPath, acb.buffer);
const pathInfo = path.parse(acbPath); }
console.log(`Parsing ${pathInfo.base}...`); exports.decryptAcb = decryptAcb;
const acb = await parseAcb(acbPath);
if (hcaDir === undefined) hcaDir = path.join(pathInfo.dir, acb.Name);
if (!fs.existsSync(hcaDir)) {
await mkdir(hcaDir, { recursive: true });
} else if (skip) {
console.log(`Skipped ${pathInfo.base}...`);
return;
}
console.log(`Extracting ${pathInfo.base}...`);
let memory = 0, stream = 0;
for (let i = 0; i < acb.WaveformTable.length; i++) {
const Waveform = acb.WaveformTable[i];
const isMemory = Waveform.Streaming === 0;
const hcaBuffer = isMemory ? acb.memoryHcas[Waveform.MemoryAwbId] : acb.streamHcas[Waveform.StreamAwbPortNo][Waveform.StreamAwbId];
const awbKey = isMemory ? acb.memoryHcas.config.key : acb.streamHcas[Waveform.StreamAwbPortNo].config.key;
const name = isMemory ? `memory_${++memory}.hca` : `stream_${++stream}.hca`;
const hcaPath = path.join(hcaDir, name);
if (key !== undefined) {
console.log(`Decrypting ${name}...`);
await hca.decrypt(hcaBuffer, key, awbKey, type);
}
console.log(`Writing ${name}...`);
await writeFile(hcaPath, hcaBuffer);
}
}
exports.acb2hcas = acb2hcas;
async function acb2wavs(acbPath, key, wavDir, volume, mode, skip) {
const pathInfo = path.parse(acbPath);
console.log(`Parsing ${pathInfo.base}...`);
const acb = await parseAcb(acbPath);
if (wavDir === undefined) wavDir = path.join(pathInfo.dir, acb.Name);
if (!fs.existsSync(wavDir)) {
await mkdir(wavDir, { recursive: true });
} else if (skip) {
console.log(`Skipped ${pathInfo.base}...`);
return;
}
console.log(`Extracting ${pathInfo.base}...`);
let memory = 0, stream = 0;
for (let i = 0; i < acb.WaveformTable.length; i++) {
const Waveform = acb.WaveformTable[i];
const isMemory = Waveform.Streaming === 0;
const hcaBuffer = isMemory ? acb.memoryHcas[Waveform.MemoryAwbId] : acb.streamHcas[Waveform.StreamAwbPortNo][Waveform.StreamAwbId];
const awbKey = isMemory ? acb.memoryHcas.config.key : acb.streamHcas[Waveform.StreamAwbPortNo].config.key;
const name = isMemory ? `memory_${++memory}.wav` : `stream_${++stream}.wav`;
const wavPath = path.join(wavDir, name);
console.log(`Writing ${name}...`);
await hca.decodeToWav(hcaBuffer, key, awbKey, wavPath, volume, mode);
}
}
exports.acb2wavs = acb2wavs;
async function decryptAcb(acbPath, key, type) {
const pathInfo = path.parse(acbPath);
console.log(`Parsing ${pathInfo.base}...`);
const acb = await parseAcb(acbPath);
console.log(`Decrypting ${pathInfo.base}...`);
if (acb.memoryHcas) {
for (let i = 0; i < acb.memoryHcas.length; i++) {
await hca.decrypt(acb.memoryHcas[i], key, acb.memoryHcas.config.key, type);
}
acb.memoryHcas.config.buffer.writeUInt16BE(0, 0xE);
}
for (let i = 0; i < acb.StreamAwbHash.length; i++) {
for (let j = 0; j < acb.streamHcas[i].length; j++) {
await hca.decrypt(acb.streamHcas[i][j], key, acb.streamHcas[i].config.key, type);
}
const buffer = acb.streamHcas[i].config.buffer;
buffer.writeUInt16BE(0, 0xE);
const md5 = crypto.createHash('md5');
md5.update(buffer);
const hash = md5.digest();
const awb = acb.StreamAwbHash[i];
hash.copy(awb.Hash);
await writeFile(path.join(pathInfo.dir, awb.Name + '.awb'), buffer);
if (acb.StreamAwbAfs2Header) {
const Header = acb.StreamAwbAfs2Header[i].Header;
buffer.copy(Header, 0, 0, Header.length);
}
}
await writeFile(acbPath, acb.buffer);
}
exports.decryptAcb = decryptAcb;
async function viewUtf(acbPath, outputPath) {
const pathInfo = path.parse(acbPath);
if (outputPath === undefined) outputPath = path.join(pathInfo.dir, pathInfo.name + '.json');
console.log(`Parsing ${pathInfo.base}...`);
const buffer = await readFile(acbPath);
const utf = parseUtf(buffer, true);
if (utf && utf.AwbFile && utf.AwbFile.length > 0x20) utf.AwbFile = utf.AwbFile.substring(0, 0x20);
console.log(`Writing ${path.parse(outputPath).base}...`);
await writeFile(outputPath, JSON.stringify(utf, null, 2));
}
exports.view = viewUtf;

View File

@ -1,121 +1,120 @@
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const util = require('util'); const util = require('util');
const hca = require('./hca'); const hca = require('./hca');
const readFile = util.promisify(fs.readFile); const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile); const writeFile = util.promisify(fs.writeFile);
const mkdir = util.promisify(fs.mkdir); const mkdir = util.promisify(fs.mkdir);
async function parseAFS2(buffer) { async function parseAFS2(buffer) {
if (typeof(buffer) === 'string') buffer = await readFile(buffer); if (typeof(buffer) === 'string') buffer = await readFile(buffer);
if (!buffer || buffer.length < 4) return null; if (!buffer || buffer.length < 4) return null;
let pos = 0; let pos = 0;
const config = {}; const config = {};
config.buffer = buffer; config.buffer = buffer;
config.magic = buffer.slice(pos, 4).toString(); pos += 4; config.magic = buffer.slice(pos, 4).toString(); pos += 4;
if (config.magic !== 'AFS2') return null; if (config.magic !== 'AFS2') return null;
config.unknown1 = buffer.readUInt8(pos); pos += 1; config.unknown1 = buffer.readUInt8(pos); pos += 1;
config.sizeLen = buffer.readUInt8(pos); pos += 1; config.sizeLen = buffer.readUInt8(pos); pos += 1;
config.unknown2 = buffer.readUInt8(pos); pos += 1; config.unknown2 = buffer.readUInt8(pos); pos += 1;
config.unknown3 = buffer.readUInt8(pos); pos += 1; config.unknown3 = buffer.readUInt8(pos); pos += 1;
config.fileCount = buffer.readUInt32LE(pos); pos += 4; config.fileCount = buffer.readUInt32LE(pos); pos += 4;
config.align = buffer.readUInt16LE(pos); pos += 2; config.align = buffer.readUInt16LE(pos); pos += 2;
config.key = buffer.readUInt16LE(pos); pos += 2; config.key = buffer.readUInt16LE(pos); pos += 2;
config.fileIds = []; config.fileIds = [];
for (let i = 0; i < config.fileCount; i++) { for (let i = 0; i < config.fileCount; i++) {
const fileId = buffer.readUInt16LE(pos); pos += 2; const fileId = buffer.readUInt16LE(pos); pos += 2;
config.fileIds.push(fileId); config.fileIds.push(fileId);
} }
const files = []; const files = [];
let start; let start;
if (config.sizeLen === 2) { if (config.sizeLen === 2) {
start = buffer.readUInt16LE(pos); pos += 2; start = buffer.readUInt16LE(pos); pos += 2;
} else if (config.sizeLen === 4) { } else if (config.sizeLen === 4) {
start = buffer.readUInt32LE(pos); pos += 4; start = buffer.readUInt32LE(pos); pos += 4;
} else debugger; } else debugger;
let mod = start % config.align; let mod = start % config.align;
if (mod != 0) start += config.align - mod; if (mod != 0) start += config.align - mod;
for (let i = 0; i < config.fileCount; i++) { for (let i = 0; i < config.fileCount; i++) {
let end; let end;
if (config.sizeLen === 2) { if (config.sizeLen === 2) {
end = buffer.readUInt16LE(pos); pos += 2; end = buffer.readUInt16LE(pos); pos += 2;
} else if (config.sizeLen === 4) { } else if (config.sizeLen === 4) {
end = buffer.readUInt32LE(pos); pos += 4; end = buffer.readUInt32LE(pos); pos += 4;
} else debugger; } else debugger;
files.push(buffer.slice(start, end)); files.push(buffer.slice(start, end));
start = end; start = end;
mod = start % config.align; mod = start % config.align;
if (mod != 0) start += config.align - mod; if (mod != 0) start += config.align - mod;
} }
files.config = config; files.config = config;
return files; return files;
} }
exports.parse = parseAFS2; exports.parseAFS2 = parseAFS2;
async function awb2hcas(awbPath, key, hcaDir, type, skip) { async function awb2hcas(awbPath, key, hcaDir, type, skip) {
const pathInfo = path.parse(awbPath); const pathInfo = path.parse(awbPath);
console.log(`Parsing ${pathInfo.base}...`); console.log(`Parsing ${pathInfo.base}...`);
const list = await parseAFS2(awbPath); const list = await parseAFS2(awbPath);
if (hcaDir === undefined) hcaDir = path.join(pathInfo.dir, pathInfo.name); if (hcaDir === undefined) hcaDir = path.join(pathInfo.dir, pathInfo.name);
if (!fs.existsSync(hcaDir)) { if (!fs.existsSync(hcaDir)) {
await mkdir(hcaDir, { recursive: true }); await mkdir(hcaDir, { recursive: true });
} else if (skip) { } else if (skip) {
console.log(`Skipped ${pathInfo.base}...`); console.log(`Skipped ${pathInfo.base}...`);
return; return;
} }
const len = ('' + list.length).length; const len = ('' + list.length).length;
console.log(`Extracting ${pathInfo.base}...`); console.log(`Extracting ${pathInfo.base}...`);
for (let i = 0; i < list.length; i++) { for (let i = 0; i < list.length; i++) {
const hcaBuff = list[i]; const hcaBuff = list[i];
let name = '' + (i + 1); let name = '' + (i + 1);
while (name.length < len) name = '0' + name; while (name.length < len) name = '0' + name;
if (key !== undefined) { if (key !== undefined) {
console.log(`Decrypting ${name}.hca...`); console.log(`Decrypting ${name}.hca...`);
await hca.decrypt(hcaBuff, key, list.config.key, type); await hca.decryptHca(hcaBuff, key, list.config.key, type);
} }
console.log(`Writing ${name}.hca...`); console.log(`Writing ${name}.hca...`);
await writeFile(path.join(hcaDir, name + '.hca'), hcaBuff); await writeFile(path.join(hcaDir, name + '.hca'), hcaBuff);
} }
} }
exports.awb2hcas = awb2hcas; exports.awb2hcas = awb2hcas;
async function awb2wavs(awbPath, key, wavDir, volume, mode, skip) { async function awb2wavs(awbPath, key, wavDir, volume, mode, skip) {
const pathInfo = path.parse(awbPath); const pathInfo = path.parse(awbPath);
console.log(`Parsing ${pathInfo.base}...`); console.log(`Parsing ${pathInfo.base}...`);
const list = await parseAFS2(awbPath); const list = await parseAFS2(awbPath);
if (wavDir === undefined) wavDir = path.join(pathInfo.dir, pathInfo.name); if (wavDir === undefined) wavDir = path.join(pathInfo.dir, pathInfo.name);
if (!fs.existsSync(wavDir)) { if (!fs.existsSync(wavDir)) {
await mkdir(wavDir, { recursive: true }); await mkdir(wavDir, { recursive: true });
} else if (skip) { } else if (skip) {
console.log(`Skipped ${pathInfo.base}...`); console.log(`Skipped ${pathInfo.base}...`);
return; return;
} }
const len = ('' + list.length).length; const len = ('' + list.length).length;
console.log(`Extracting ${pathInfo.base}...`); console.log(`Extracting ${pathInfo.base}...`);
for (let i = 0; i < list.length; i++) { for (let i = 0; i < list.length; i++) {
const hcaBuff = list[i]; const hcaBuff = list[i];
let name = '' + (i + 1); let name = '' + (i + 1);
while (name.length < len) name = '0' + name; while (name.length < len) name = '0' + name;
console.log(`Writing ${name}.wav...`); const wavPath = path.join(wavDir, name + '.wav');
const wavPath = path.join(wavDir, name + '.wav'); await hca.decodeHcaToWav(hcaBuff, key, list.config.key, wavPath, volume, mode);
await hca.decodeToWav(hcaBuff, key, list.config.key, wavPath, volume, mode); }
} }
} exports.awb2wavs = awb2wavs;
exports.awb2wavs = awb2wavs;
async function decryptAwb(awbPath, key, type) {
async function decryptAwb(awbPath, key, type) { const pathInfo = path.parse(awbPath);
const pathInfo = path.parse(awbPath); console.log(`Parsing ${pathInfo.base}...`);
console.log(`Parsing ${pathInfo.base}...`); const list = await parseAFS2(awbPath);
const list = await parseAFS2(awbPath); console.log(`Decrypting ${pathInfo.base}...`);
console.log(`Decrypting ${pathInfo.base}...`); for (let i = 0; i < list.length; i++) {
for (let i = 0; i < list.length; i++) { await hca.decryptHca(list[i], key, list.config.key, type);
await hca.decrypt(list[i], key, list.config.key, type); }
} const buffer = list.config.buffer;
const buffer = list.config.buffer; buffer.writeUInt16BE(0, 0xE);
buffer.writeUInt16BE(0, 0xE); console.log(`Writing ${pathInfo.base}...`);
console.log(`Writing ${pathInfo.base}...`); await writeFile(awbPath, buffer);
await writeFile(awbPath, buffer); }
} exports.decryptAwb = decryptAwb;
exports.decryptAwb = decryptAwb;

128
src/cpk.js Normal file
View File

@ -0,0 +1,128 @@
const fs = require('fs');
const path = require('path');
const util = require('util');
const utf = require('./utf');
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
const mkdir = util.promisify(fs.mkdir);
function parseTag(buffer, tag) {
if (tag !== buffer.slice(0, 4).toString()) return null;
const size = buffer.readUInt32LE(0x8);
if (!size) return null;
const offset = 0x10;
return utf.parseUtf(buffer.slice(offset, offset + size));
}
async function parseCpk(cpkPath) {
const buffer = await readFile(cpkPath);
let utfs = parseTag(buffer, 'CPK ');
if (!utfs || utfs.length !== 1) return null;
const cpk = { buffer };
cpk.info = utfs[0];
let offset, size;
// HTOC
offset = (Number)(cpk.info.HtocOffset);
size = (Number)(cpk.info.HtocSize);
if (offset && size) cpk.htoc = parseTag(buffer.slice(offset, offset + size), 'HTOC');
// TOC
offset = (Number)(cpk.info.TocOffset);
size = (Number)(cpk.info.TocSize);
if (offset && size) cpk.toc = parseTag(buffer.slice(offset, offset + size), 'TOC ');
// ETOC
offset = (Number)(cpk.info.EtocOffset);
size = (Number)(cpk.info.EtocSize);
if (offset && size) cpk.etoc = parseTag(buffer.slice(offset, offset + size), 'ETOC');
return cpk;
}
async function extractCpk(cpkPath, output) {
const cpk = await parseCpk(cpkPath);
if (!cpk) return;
if (output === undefined) output = path.parse(cpkPath).dir;
for (let i = 0; i < cpk.toc.length; i++) {
const item = cpk.toc[i];
let buffer = cpk.buffer;
const offset = (Number)(cpk.info.TocOffset + item.FileOffset);
let fileBuffer = buffer.slice(offset, offset + item.FileSize);
fileBuffer = extract(fileBuffer);
const dir = path.join(output, item.DirName);
if (!fs.existsSync(dir)) {
await mkdir(dir, { recursive: true });
}
await writeFile(path.join(dir, item.FileName), fileBuffer);
}
}
exports.extractCpk = extractCpk;
function extract(buffer) {
if ('CRILAYLA' !== buffer.slice(0, 0x8).toString()) return buffer;
const uncompressSize = buffer.readUInt32LE(0x8);
const headerOffset = buffer.readUInt32LE(0xC);
const result = Buffer.allocUnsafe(uncompressSize + 0x100);
for (let i = 0; i < 0x100; i++) result[i] = buffer[0x10 + headerOffset + i];
let output = 0;
const end = 0x100 + uncompressSize - 1;
const lens = [ 2, 3, 5, 8 ];
const reader = new BitReader(buffer.slice(0, buffer.length - 0x100));
while (output < uncompressSize) {
if (reader.getBits(1) > 0) {
let offset = end - output + reader.getBits(13) + 3;
let length = 3;
let level;
for (level = 0; level < lens.length; level++) {
const lv = reader.getBits(lens[level]);
length += lv;
if (lv != ((1 << lens[level]) - 1)) break;
}
if (level === lens.length) {
let lv;
do {
lv = reader.getBits(8);
length += lv;
} while (lv === 0xFF);
}
for (let i = 0; i < length; i++) {
result[end - output] = result[offset--];
output++;
}
} else {
result[end - output] = reader.getBits(8);
output++;
}
}
return result;
}
class BitReader {
constructor(buffer) {
this.buffer = buffer;
this.offset = buffer.length - 1;
this.pool = 0;
this.left = 0;
}
getBits(count) {
let result = 0;
let produced = 0;
let round;
while (produced < count) {
if (this.left == 0) {
this.pool = this.buffer[this.offset];
this.left = 8;
this.offset--;
}
if (this.left > (count - produced)) {
round = count - produced;
} else {
round = this.left;
}
result <<= round;
result |= ((this.pool >>> (this.left - round)) & ((1 << round) - 1));
this.left -= round;
produced += round;
}
return result;
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,151 +1,158 @@
const fs = require('fs'); const fs = require('fs');
const util = require('util'); const util = require('util');
const path = require('path'); const path = require('path');
const utf = require('./utf'); const utf = require('./utf');
const afs2 = require('./afs2'); const afs2 = require('./afs2');
const hca = require('./hca'); const acb = require('./acb');
const hca = require('./hca');
const lstat = util.promisify(fs.lstat); const cpk = require('./cpk');
const readdir = util.promisify(fs.readdir);
const lstat = util.promisify(fs.lstat);
function usage() { const readdir = util.promisify(fs.readdir);
console.log(`Usage: node index.js <Command> <Options> <Path>...`);
console.log(`Command:`); function usage() {
console.log(`\tacb2hcas [-d] [-k <key>] [-t <type>] [-o <outputDir>] [-s] <acbPath>...`); console.log(`Usage: node index.js <Command> <Options> <Path>...`);
console.log(`\tacb2wavs [-k <key>] [-o <outputDir>] [-v <volume>] [-m <mode>] [-s] <acbPath>...`); console.log(`Command:`);
console.log(`\tawb2hcas [-d] [-k <key>] [-t <type>] [-o <outputDir>] [-s] <awbPath>...`); console.log(`\tacb2hcas [-d] [-k <key>] [-t <type>] [-o <outputDir>] [-s] <acbPath>...`);
console.log(`\tawb2wavs [-k <key>] [-o <outputDir>] [-v <volume>] [-m <mode>] [-s] <awbPath>...`); console.log(`\tacb2wavs [-k <key>] [-o <outputDir>] [-v <volume>] [-m <mode>] [-s] <acbPath>...`);
console.log(`\thca2wav [-k <key>] [-w <awbKey>] [-o <outputFile>] [-v <volume>] [-m <mode>] <hcaPath>...`); console.log(`\tawb2hcas [-d] [-k <key>] [-t <type>] [-o <outputDir>] [-s] <awbPath>...`);
console.log(`\tview_utf [-o <outputFile>] <acbPath/acfPath>...`); console.log(`\tawb2wavs [-k <key>] [-o <outputDir>] [-v <volume>] [-m <mode>] [-s] <awbPath>...`);
console.log(`\tdecrypt_acb [-k <key>] [-t <type>] <acbPath>...`); console.log(`\thca2wav [-k <key>] [-w <awbKey>] [-o <outputFile>] [-v <volume>] [-m <mode>] <hcaPath>...`);
console.log(`\tdecrypt_awb [-k <key>] [-t <type>] <awbPath>...`); console.log(`\tview_utf [-o <outputFile>] <acbPath/acfPath>...`);
console.log(`\tdecrypt_hca [-k <key>] [-w <awbKey>] [-t <type>] <hcaPath>...`); console.log(`\tdecrypt_acb [-k <key>] [-t <type>] <acbPath>...`);
console.log(`\tmix_acb [-k <key>] [-o <outputDir>] [-v <volume>] [-m <mode>] [-s] <acbPath>...`); console.log(`\tdecrypt_awb [-k <key>] [-t <type>] <awbPath>...`);
console.log(`Options:`); console.log(`\tdecrypt_hca [-k <key>] [-w <awbKey>] [-t <type>] <hcaPath>...`);
console.log(`\t-d / --decrypt Decrypt hca files`); console.log(`\tacb_mix [-k <key>] [-o <outputDir>] [-v <volume>] [-m <mode>] [-s] <acbPath>...`);
console.log(`\t-k / --key <key> Decrypt key`); console.log(`\textract_cpk [-o <outputDir>] <cpkPath>...`);
console.log(`\t-w / --awbKey <awbKey> Decrypt key In Awb File`); console.log(`Options:`);
console.log(`\t-t / --type <type> Hca Encrypt Type (1 / 0) (Default: 1)`); console.log(`\t-d / --decrypt Decrypt hca files`);
console.log(`\t-o / --output <output> Output Directory / File`); console.log(`\t-k / --key <key> Decrypt key`);
console.log(`\t-v / --volume <volume> Wav Volume (Default: 1.0)`); console.log(`\t-w / --awbKey <awbKey> Decrypt key In Awb File`);
console.log(`\t-m / --mode <mode> Wav Bit Mode (Default: 16)`); console.log(`\t-t / --type <type> Hca Encrypt Type (1 / 0) (Default: 1)`);
console.log(`\t-s / --skip Skip exists files`); console.log(`\t-o / --output <output> Output Directory / File`);
} console.log(`\t-v / --volume <volume> Wav Volume (Default: 1.0)`);
console.log(`\t-m / --mode <mode> Wav Bit Mode (Default: 16)`);
async function handlePathes(pathes, ext) { console.log(`\t-s / --skip Skip exists files`);
let i = 0; }
while (i < pathes.length) {
const path1 = pathes[i]; async function handlePathes(pathes, ext) {
if (fs.existsSync(path1)) { let i = 0;
const stats1 = await lstat(path1); while (i < pathes.length) {
if (stats1.isDirectory()) { const path1 = pathes[i];
pathes.splice(i, 1); if (fs.existsSync(path1)) {
const files = await readdir(path1); const stats1 = await lstat(path1);
for (let j = 0; j < files.length; j++) { if (stats1.isDirectory()) {
const base = files[j]; pathes.splice(i, 1);
const path2 = path.join(path1, base); const files = await readdir(path1);
const stats2 = await lstat(path2); for (let j = 0; j < files.length; j++) {
if (path.parse(base).ext === ext || stats2.isDirectory()) { const base = files[j];
pathes.push(path2); const path2 = path.join(path1, base);
} const stats2 = await lstat(path2);
} if (path.parse(base).ext === ext || stats2.isDirectory()) {
} else if (ext && path.parse(path1).ext !== ext) { pathes.push(path2);
pathes.splice(i, 1); }
} else { }
i++; } else if (ext && path.parse(path1).ext !== ext) {
} pathes.splice(i, 1);
} else { } else {
pathes.splice(i, 1); i++;
} }
} } else {
} pathes.splice(i, 1);
}
(async () => { }
const argv = process.argv; }
if (argv.length < 3) {
usage(); (async () => {
return; const argv = process.argv;
} if (argv.length < 3) {
let decrypt = false, key = undefined, awbKey = undefined, output = undefined, volume = 1, mode = 16, type = 1, skip = false; usage();
let i = 3; return;
const pathes = []; }
while (i < argv.length) { let decrypt = false, key = undefined, awbKey = undefined, output = undefined, volume = 1, mode = 16, type = 1, skip = false;
const arg = argv.splice(i, 1)[0]; let i = 3;
if (arg === '-d' || arg === '--decrypt') { const pathes = [];
decrypt = true; while (i < argv.length) {
} else if (arg === '-k' || arg === '--key') { const arg = argv.splice(i, 1)[0];
key = argv.splice(i, 1)[0]; if (arg === '-d' || arg === '--decrypt') {
} else if (arg === '-w' || arg === '--awbKey') { decrypt = true;
awbKey = parseInt(argv.splice(i, 1)[0], 10); } else if (arg === '-k' || arg === '--key') {
} else if (arg === '-o' || arg === '--output') { key = argv.splice(i, 1)[0];
output = argv.splice(i, 1)[0]; } else if (arg === '-w' || arg === '--awbKey') {
} else if (arg === '-v' || arg === '--volume') { awbKey = parseInt(argv.splice(i, 1)[0], 10);
volume = parseFloat(argv.splice(i, 1)[0], 10); } else if (arg === '-o' || arg === '--output') {
} else if (arg === '-m' || arg === '--mode') { output = argv.splice(i, 1)[0];
mode = parseInt(argv.splice(i, 1)[0], 10); } else if (arg === '-v' || arg === '--volume') {
} else if (arg === '-t' || arg === '--type') { volume = parseFloat(argv.splice(i, 1)[0], 10);
type = parseInt(argv.splice(i, 1)[0], 10); } else if (arg === '-m' || arg === '--mode') {
} else if (arg === '-s' || arg === '--skip') { mode = parseInt(argv.splice(i, 1)[0], 10);
skip = true; } else if (arg === '-t' || arg === '--type') {
} else { type = parseInt(argv.splice(i, 1)[0], 10);
pathes.push(arg); } else if (arg === '-s' || arg === '--skip') {
} skip = true;
} } else {
if (pathes.length === 0) { pathes.push(arg);
usage(); }
return; }
} if (pathes.length === 0) {
try { usage();
switch (argv[2]) { return;
case 'acb2hcas': }
if (!decrypt) key = undefined; try {
await handlePathes(pathes, '.acb'); switch (argv[2]) {
for (let i = 0; i < pathes.length; i++) await utf.acb2hcas(pathes[i], key, output, type, skip); case 'acb2hcas':
break; if (!decrypt) key = undefined;
case 'acb2wavs': await handlePathes(pathes, '.acb');
await handlePathes(pathes, '.acb'); for (let i = 0; i < pathes.length; i++) await acb.acb2hcas(pathes[i], key, output, type, skip);
for (let i = 0; i < pathes.length; i++) await utf.acb2wavs(pathes[i], key, output, volume, mode, skip); break;
break; case 'acb2wavs':
case 'awb2hcas': await handlePathes(pathes, '.acb');
if (!decrypt) key = undefined; for (let i = 0; i < pathes.length; i++) await acb.acb2wavs(pathes[i], key, output, volume, mode, skip);
await handlePathes(pathes, '.awb'); break;
for (let i = 0; i < pathes.length; i++) await afs2.awb2hcas(pathes[i], key, output, type, skip); case 'awb2hcas':
break; if (!decrypt) key = undefined;
case 'awb2wavs': await handlePathes(pathes, '.awb');
await handlePathes(pathes, '.awb'); for (let i = 0; i < pathes.length; i++) await afs2.awb2hcas(pathes[i], key, output, type, skip);
for (let i = 0; i < pathes.length; i++) await afs2.awb2wavs(pathes[i], key, output, volume, mode, skip); break;
break; case 'awb2wavs':
case 'hca2wav': await handlePathes(pathes, '.awb');
await handlePathes(pathes, '.hca'); for (let i = 0; i < pathes.length; i++) await afs2.awb2wavs(pathes[i], key, output, volume, mode, skip);
for (let i = 0; i < pathes.length; i++) await hca.decodeToWav(pathes[i], key, awbKey, output, volume, mode); break;
break; case 'hca2wav':
case 'view_utf': await handlePathes(pathes, '.hca');
await handlePathes(pathes); for (let i = 0; i < pathes.length; i++) await hca.decodeHcaToWav(pathes[i], key, awbKey, output, volume, mode);
for (let i = 0; i < pathes.length; i++) await utf.view(pathes[i], output); break;
break; case 'view_utf':
case 'decrypt_acb': await handlePathes(pathes);
await handlePathes(pathes, '.acb'); for (let i = 0; i < pathes.length; i++) await utf.viewUtf(pathes[i], output);
for (let i = 0; i < pathes.length; i++) await utf.decryptAcb(pathes[i], key, type); break;
break; case 'decrypt_acb':
case 'decrypt_awb': await handlePathes(pathes, '.acb');
await handlePathes(pathes, '.awb'); for (let i = 0; i < pathes.length; i++) await acb.decryptAcb(pathes[i], key, type);
for (let i = 0; i < pathes.length; i++) await afs2.decryptAwb(pathes[i], key, type); break;
break; case 'decrypt_awb':
case 'decrypt_hca': await handlePathes(pathes, '.awb');
await handlePathes(pathes, '.hca'); for (let i = 0; i < pathes.length; i++) await afs2.decryptAwb(pathes[i], key, type);
for (let i = 0; i < pathes.length; i++) await hca.decrypt(pathes[i], key, awbKey, type, pathes[i]); break;
break; case 'decrypt_hca':
case 'mix_acb': await handlePathes(pathes, '.hca');
await handlePathes(pathes, '.acb'); for (let i = 0; i < pathes.length; i++) await hca.decryptHca(pathes[i], key, awbKey, type, pathes[i]);
for (let i = 0; i < pathes.length; i++) await utf.mixAcb(pathes[i], key, output, mode, skip); break;
break; case 'acb_mix':
default: await handlePathes(pathes, '.acb');
usage(); for (let i = 0; i < pathes.length; i++) await acb.mixAcb(pathes[i], key, output, mode, skip);
break; break;
} case 'extract_cpk':
console.log('FINISH!'); await handlePathes(pathes, '.cpk');
} catch (e) { for (let i = 0; i < pathes.length; i++) await cpk.extractCpk(pathes[i], output);
console.error(`ERROR: ${e.message}`); break;
debugger; default:
} usage();
})(); break;
}
console.log('FINISH!');
} catch (e) {
console.error(`ERROR: ${e.message}`);
debugger;
}
})();

98
src/utf.js Normal file
View File

@ -0,0 +1,98 @@
const fs = require('fs');
const path = require('path');
const util = require('util');
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
function findZero(buffer, start) {
while (buffer[start] !== 0x0) start++;
return start;
}
function parseUtf(buffer, toString) {
if (!buffer || buffer.length < 4) return null;
let pos = 0;
const config = {};
config.magic = buffer.slice(pos, 4).toString(); pos += 4;
if (config.magic !== '@UTF') return null;
config.dataSize = buffer.readUInt32BE(pos); pos += 4;
buffer = buffer.slice(pos);
pos = 0;
config.unknown = buffer.readUInt16BE(pos); pos += 2;
config.valueOffset = buffer.readUInt16BE(pos); pos += 2;
config.stringOffset = buffer.readUInt32BE(pos); pos += 4;
config.dataOffset = buffer.readUInt32BE(pos); pos += 4;
config.nameOffset = buffer.readUInt32BE(pos); pos += 4;
config.elementCount = buffer.readUInt16BE(pos); pos += 2;
config.valueSize = buffer.readUInt16BE(pos); pos += 2;
config.pageCount = buffer.readUInt32BE(pos); pos += 4;
let stringEnd = findZero(buffer, config.stringOffset);
config.name = buffer.slice(config.stringOffset, stringEnd).toString();
let valuePos = config.valueOffset;
const pages = [];
config.types = [];
let firstPos = pos;
for (let i = 0; i < config.pageCount; i++) {
let page = {};
pos = firstPos;
for (let j = 0; j < config.elementCount; j++) {
let type = buffer.readUInt8(pos); pos = pos + 1;
if (i === 0) config.types[j] = type;
let stringOffset = config.stringOffset + buffer.readUInt32BE(pos); pos += 4;
stringEnd = findZero(buffer, stringOffset);
const key = buffer.slice(stringOffset, stringEnd).toString();
const method = type >>> 5;
type = type & 0x1F;
let value = null;
if (method > 0) {
let offset = method === 1 ? pos : valuePos;
switch (type) {
case 0x10: value = buffer.readInt8(offset); offset += 1; break;
case 0x11: value = buffer.readUInt8(offset); offset += 1; break;
case 0x12: value = buffer.readInt16BE(offset); offset += 2; break;
case 0x13: value = buffer.readUInt16BE(offset); offset += 2; break;
case 0x14: value = buffer.readInt32BE(offset); offset += 4; break;
case 0x15: value = buffer.readUInt32BE(offset); offset += 4; break;
case 0x16: value = buffer.readBigInt64BE(offset); offset += 8; break;
case 0x17: value = buffer.readBigUInt64BE(offset); offset += 8; break;
case 0x18: value = buffer.readFloatBE(offset); offset += 4; break;
case 0x19: debugger; value = buffer.readDoubleBE(offset); offset += 8; break;
case 0x1A:
stringOffset = config.stringOffset + buffer.readUInt32BE(offset); offset += 4;
stringEnd = findZero(buffer, stringOffset);
value = buffer.slice(stringOffset, stringEnd).toString();
break;
case 0x1B:
const bufferStart = config.dataOffset + buffer.readUInt32BE(offset); offset += 4;
const bufferLen = buffer.readUInt32BE(offset); offset += 4;
value = buffer.slice(bufferStart, bufferStart + bufferLen);
let temp = parseUtf(value, toString);
if (temp) value = temp; else if (toString) value = buffer.slice(bufferStart, bufferStart + bufferLen).toString('hex');
break;
default:
console.log(`unknown type: ${type}`);
break;
}
if (method === 1) pos = offset; else valuePos = offset;
}
page[key] = value;
}
pages.push(page);
}
pages.config = config;
return pages;
}
exports.parseUtf = parseUtf;
async function viewUtf(acbPath, outputPath) {
const pathInfo = path.parse(acbPath);
if (outputPath === undefined) outputPath = path.join(pathInfo.dir, pathInfo.name + '.json');
console.log(`Parsing ${pathInfo.base}...`);
const buffer = await readFile(acbPath);
const obj = parseUtf(buffer, true);
if (obj && obj.AwbFile && obj.AwbFile.length > 0x20) obj.AwbFile = obj.AwbFile.substring(0, 0x20);
console.log(`Writing ${path.parse(outputPath).base}...`);
await writeFile(outputPath, JSON.stringify(obj, null, 2));
}
exports.viewUtf = viewUtf;

3
view_utf.bat Normal file
View File

@ -0,0 +1,3 @@
@echo off
node.exe "%~dp0src\index.js" view_utf %*
pause

View File

@ -1,3 +0,0 @@
@echo off
node.exe "%~dp0index.js" acb2hcas %*
pause

View File

@ -1,3 +0,0 @@
@echo off
node.exe "%~dp0index.js" awb2hcas %*
pause

View File

@ -1,5 +0,0 @@
@echo off
set /P key=Please input key and press Enter:
if "%key%"=="" set key=0
node.exe "%~dp0index.js" awb2hcas -d -k %key% %*
pause

View File

@ -1,5 +0,0 @@
@echo off
set /P key=Please input key and press Enter:
if "%key%"=="" set key=0
node.exe "%~dp0index.js" decrypt_acb -k %key% %*
pause

View File

@ -1,5 +0,0 @@
@echo off
set /P key=Please input key and press Enter:
if "%key%"=="" set key=0
node.exe "%~dp0index.js" decrypt_awb -k %key% %*
pause

View File

@ -1,3 +0,0 @@
@echo off
node.exe "%~dp0index.js" view_utf %*
pause