add acb&awb support
This commit is contained in:
@@ -0,0 +1,702 @@
|
||||
using CriWareFormats.Common;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CriWareFormats
|
||||
{
|
||||
struct CueName
|
||||
{
|
||||
public ushort CueIndex;
|
||||
public string Name;
|
||||
}
|
||||
|
||||
struct Cue
|
||||
{
|
||||
public byte ReferenceType;
|
||||
public ushort ReferenceIndex;
|
||||
}
|
||||
|
||||
struct BlockSequence
|
||||
{
|
||||
public ushort NumTracks;
|
||||
public uint TrackIndexOffset;
|
||||
public uint TrackIndexSize;
|
||||
public ushort NumBlocks;
|
||||
public uint BlockIndexOffset;
|
||||
public uint BlockIndexSize;
|
||||
}
|
||||
|
||||
struct Block
|
||||
{
|
||||
public ushort NumTracks;
|
||||
public uint TrackIndexOffset;
|
||||
public uint TrackIndexSize;
|
||||
}
|
||||
|
||||
struct Sequence
|
||||
{
|
||||
public ushort NumTracks;
|
||||
public uint TrackIndexOffset;
|
||||
public uint TrackIndexSize;
|
||||
public byte Type;
|
||||
}
|
||||
|
||||
struct Track
|
||||
{
|
||||
public ushort EventIndex;
|
||||
}
|
||||
|
||||
struct TrackCommand
|
||||
{
|
||||
public uint CommandOffset;
|
||||
public uint CommandSize;
|
||||
}
|
||||
|
||||
struct Synth
|
||||
{
|
||||
public byte Type;
|
||||
public uint ReferenceItemsOffset;
|
||||
public uint ReferenceItemsSize;
|
||||
}
|
||||
|
||||
struct Waveform
|
||||
{
|
||||
public ushort Id;
|
||||
public ushort PortNo;
|
||||
public byte Streaming;
|
||||
}
|
||||
|
||||
public class AcbNameLoader
|
||||
{
|
||||
private Stream acbStream;
|
||||
|
||||
private UtfTable header;
|
||||
private UtfTable cueNames;
|
||||
|
||||
private BinaryReaderEndian cueReader;
|
||||
private BinaryReaderEndian cueNameReader;
|
||||
private BinaryReaderEndian blockSequenceReader;
|
||||
private BinaryReaderEndian blockReader;
|
||||
private BinaryReaderEndian sequenceReader;
|
||||
private BinaryReaderEndian trackReader;
|
||||
private BinaryReaderEndian trackCommandReader;
|
||||
private BinaryReaderEndian synthReader;
|
||||
private BinaryReaderEndian waveformReader;
|
||||
|
||||
private Cue[] cue;
|
||||
private CueName[] cueName;
|
||||
private BlockSequence[] blockSequence;
|
||||
private Block[] block;
|
||||
private Sequence[] sequence;
|
||||
private Track[] track;
|
||||
private TrackCommand[] trackCommand;
|
||||
private Synth[] synth;
|
||||
private Waveform[] waveform;
|
||||
|
||||
private int cueRows;
|
||||
private int cueNameRows;
|
||||
private int blockSequenceRows;
|
||||
private int blockRows;
|
||||
private int sequenceRows;
|
||||
private int trackRows;
|
||||
private int trackCommandRows;
|
||||
private int synthRows;
|
||||
private int waveFormRows;
|
||||
|
||||
private bool isMemory;
|
||||
private int targetWaveId;
|
||||
private int targetPort;
|
||||
|
||||
private int synthDepth;
|
||||
private int sequenceDepth;
|
||||
|
||||
private short cueNameIndex;
|
||||
private string cueNameName;
|
||||
private int awbNameCount;
|
||||
private List<short> awbNameList;
|
||||
private string name;
|
||||
|
||||
public AcbNameLoader(Stream acb)
|
||||
{
|
||||
acbStream = acb;
|
||||
header = new UtfTable(acb, (uint)acbStream.Position);
|
||||
}
|
||||
|
||||
bool OpenUtfSubtable(out BinaryReaderEndian tableReader, out UtfTable table, string tableName, out int rows)
|
||||
{
|
||||
if (!header.Query(0, tableName, out VLData data))
|
||||
throw new ArgumentException("Error reading table.");
|
||||
|
||||
tableReader = new BinaryReaderEndian(acbStream);
|
||||
table = new UtfTable(tableReader.BaseStream, data.Offset, out rows, out string _);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void AddAcbName(byte streaming)
|
||||
{
|
||||
if (cueNameName.Length == 0)
|
||||
return;
|
||||
|
||||
for (int i = 0; i < awbNameCount; i++)
|
||||
{
|
||||
if (awbNameList[i] == cueNameIndex)
|
||||
return;
|
||||
}
|
||||
|
||||
if (awbNameCount > 0)
|
||||
{
|
||||
name += "; ";
|
||||
name += cueNameName;
|
||||
}
|
||||
else
|
||||
name = cueNameName;
|
||||
|
||||
if (streaming == 2 && isMemory)
|
||||
name += " [pre]";
|
||||
|
||||
awbNameList.Add(cueNameIndex);
|
||||
awbNameCount++;
|
||||
}
|
||||
|
||||
void PreloadAcbWaveForm()
|
||||
{
|
||||
ref int rows = ref waveFormRows;
|
||||
|
||||
if (rows != 0) return;
|
||||
if (!OpenUtfSubtable(out waveformReader, out UtfTable table, "WaveformTable", out rows))
|
||||
throw new Exception("Failure opening Waveform table.");
|
||||
if (rows == 0) return;
|
||||
|
||||
waveform = new Waveform[rows];
|
||||
|
||||
int cId = table.GetColumn("Id");
|
||||
int cMemoryAwbId = table.GetColumn("MemoryAwbId");
|
||||
int cStreamAwbId = table.GetColumn("StreamAwbId");
|
||||
int cStreamAwbPortNo = table.GetColumn("StreamAwbPortNo");
|
||||
int cStreaming = table.GetColumn("Streaming");
|
||||
|
||||
for (int i = 0; i < rows; i++)
|
||||
{
|
||||
ref Waveform r = ref waveform[i];
|
||||
|
||||
if (!table.Query(i, cId, out r.Id))
|
||||
{
|
||||
if (isMemory)
|
||||
{
|
||||
table.Query(i, cMemoryAwbId, out r.Id);
|
||||
waveform[i].PortNo = 0xFFFF;
|
||||
}
|
||||
else
|
||||
{
|
||||
table.Query(i, cStreamAwbId, out r.Id);
|
||||
table.Query(i, cStreamAwbPortNo, out r.PortNo);
|
||||
}
|
||||
}
|
||||
else
|
||||
waveform[i].PortNo = 0xFFFF;
|
||||
|
||||
table.Query(i, cStreaming, out r.Streaming);
|
||||
}
|
||||
}
|
||||
|
||||
void LoadAcbWaveForm(ushort index)
|
||||
{
|
||||
PreloadAcbWaveForm();
|
||||
|
||||
if (index > waveFormRows)
|
||||
throw new ArgumentOutOfRangeException(nameof(index));
|
||||
|
||||
ref Waveform r = ref waveform[index];
|
||||
|
||||
if (r.Id != targetWaveId)
|
||||
return;
|
||||
|
||||
if (targetPort >= 0 && r.PortNo != 0xFFFF && r.PortNo != targetPort)
|
||||
return;
|
||||
|
||||
if ((isMemory && r.Streaming == 1) || (!isMemory && r.Streaming == 0))
|
||||
return;
|
||||
|
||||
AddAcbName(r.Streaming);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void PreloadAcbSynth()
|
||||
{
|
||||
ref int rows = ref synthRows;
|
||||
|
||||
if (rows != 0) return;
|
||||
if (!OpenUtfSubtable(out synthReader, out UtfTable table, "SynthTable", out rows))
|
||||
throw new Exception("Failure opening Synth table.");
|
||||
if (rows == 0) return;
|
||||
|
||||
synth = new Synth[rows];
|
||||
|
||||
int cType = table.GetColumn("Type");
|
||||
int cReferenceItems = table.GetColumn("ReferenceItems");
|
||||
|
||||
for (int i = 0; i < rows; i++)
|
||||
{
|
||||
ref Synth r = ref synth[i];
|
||||
|
||||
table.Query(i, cType, out r.Type);
|
||||
table.Query(i, cReferenceItems, out r.ReferenceItemsOffset, out r.ReferenceItemsSize);
|
||||
}
|
||||
}
|
||||
|
||||
void LoadAcbSynth(ushort index)
|
||||
{
|
||||
PreloadAcbSynth();
|
||||
|
||||
if (index > synthRows)
|
||||
throw new ArgumentOutOfRangeException(nameof(index));
|
||||
|
||||
ref Synth r = ref synth[index];
|
||||
|
||||
synthDepth++;
|
||||
|
||||
if (synthDepth > 3)
|
||||
throw new Exception("Synth depth too high");
|
||||
|
||||
int count = (int)(r.ReferenceItemsSize / 4);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
synthReader.BaseStream.Position = r.ReferenceItemsOffset + i * 4;
|
||||
|
||||
ushort itemType = synthReader.ReadUInt16BE();
|
||||
ushort itemIndex = synthReader.ReadUInt16BE();
|
||||
|
||||
switch (itemType)
|
||||
{
|
||||
case 0:
|
||||
count = 0;
|
||||
break;
|
||||
|
||||
case 1:
|
||||
LoadAcbWaveForm(itemIndex);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
LoadAcbSynth(itemIndex);
|
||||
break;
|
||||
|
||||
case 3:
|
||||
LoadAcbSequence(itemIndex);
|
||||
break;
|
||||
|
||||
case 6:
|
||||
default:
|
||||
count = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
synthDepth--;
|
||||
}
|
||||
|
||||
void LoadAcbCommandTlvs(BinaryReaderEndian reader, uint commandOffset, uint commandSize)
|
||||
{
|
||||
uint pos = 0;
|
||||
uint maxPos = commandSize;
|
||||
|
||||
while (pos < maxPos)
|
||||
{
|
||||
reader.BaseStream.Position = commandOffset + pos;
|
||||
|
||||
ushort tlvCode = reader.ReadUInt16BE();
|
||||
ushort tlvSize = reader.ReadByte();
|
||||
|
||||
pos += 3;
|
||||
|
||||
switch (tlvCode)
|
||||
{
|
||||
case 2000:
|
||||
case 2003:
|
||||
if (tlvSize < 4)
|
||||
break;
|
||||
|
||||
reader.BaseStream.Position = commandOffset + pos;
|
||||
|
||||
ushort tlvType = reader.ReadUInt16BE();
|
||||
ushort tlvIndex = reader.ReadUInt16BE();
|
||||
|
||||
switch (tlvType)
|
||||
{
|
||||
case 2:
|
||||
LoadAcbSynth(tlvIndex);
|
||||
break;
|
||||
|
||||
case 3:
|
||||
LoadAcbSequence(tlvIndex);
|
||||
break;
|
||||
|
||||
default:
|
||||
maxPos = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
pos += tlvSize;
|
||||
}
|
||||
}
|
||||
|
||||
void PreloadAcbTrackCommand()
|
||||
{
|
||||
ref int rows = ref trackCommandRows;
|
||||
|
||||
if (rows != 0) return;
|
||||
if (!OpenUtfSubtable(out trackCommandReader, out UtfTable table, "TrackEventTable", out rows))
|
||||
{
|
||||
if (!OpenUtfSubtable(out trackCommandReader, out table, "CommandTable", out rows))
|
||||
throw new Exception("Failure opening Command table.");
|
||||
}
|
||||
if (rows == 0) return;
|
||||
|
||||
trackCommand = new TrackCommand[rows];
|
||||
|
||||
int cCommand = table.GetColumn("Command");
|
||||
|
||||
for (int i = 0; i < rows; i++)
|
||||
{
|
||||
ref TrackCommand r = ref trackCommand[i];
|
||||
|
||||
table.Query(i, cCommand, out r.CommandOffset, out r.CommandSize);
|
||||
}
|
||||
}
|
||||
|
||||
void LoadAcbTrackCommand(ushort index)
|
||||
{
|
||||
PreloadAcbTrackCommand();
|
||||
|
||||
if (index > trackCommandRows)
|
||||
throw new ArgumentOutOfRangeException(nameof(index));
|
||||
|
||||
LoadAcbCommandTlvs(
|
||||
trackCommandReader,
|
||||
trackCommand[index].CommandOffset,
|
||||
trackCommand[index].CommandSize);
|
||||
}
|
||||
|
||||
void PreloadAcbTrack()
|
||||
{
|
||||
ref int rows = ref trackRows;
|
||||
|
||||
if (rows != 0) return;
|
||||
if (!OpenUtfSubtable(out trackReader, out UtfTable table, "TrackTable", out rows))
|
||||
throw new Exception("Failure opening Track table.");
|
||||
if (rows == 0) return;
|
||||
|
||||
track = new Track[rows];
|
||||
|
||||
int cEventIndex = table.GetColumn("EventIndex");
|
||||
|
||||
for (int i = 0; i < rows; i++)
|
||||
{
|
||||
ref Track r = ref track[i];
|
||||
|
||||
table.Query(i, cEventIndex, out r.EventIndex);
|
||||
}
|
||||
}
|
||||
|
||||
void LoadAcbTrack(ushort index)
|
||||
{
|
||||
PreloadAcbTrack();
|
||||
|
||||
if (index > trackRows)
|
||||
throw new ArgumentOutOfRangeException(nameof(index));
|
||||
|
||||
ref Track r = ref track[index];
|
||||
|
||||
if (r.EventIndex == 65535)
|
||||
return;
|
||||
|
||||
LoadAcbTrackCommand(r.EventIndex);
|
||||
}
|
||||
|
||||
void PreloadAcbSequence()
|
||||
{
|
||||
ref int rows = ref sequenceRows;
|
||||
|
||||
if (rows != 0) return;
|
||||
if (!OpenUtfSubtable(out sequenceReader, out UtfTable table, "SequenceTable", out rows))
|
||||
throw new Exception("Failure opening Sequence table.");
|
||||
if (rows == 0) return;
|
||||
|
||||
sequence = new Sequence[rows];
|
||||
|
||||
int cNumTracks = table.GetColumn("NumTracks");
|
||||
int cTrackIndex = table.GetColumn("TrackIndex");
|
||||
int cType = table.GetColumn("Type");
|
||||
|
||||
for (int i = 0; i < rows; i++)
|
||||
{
|
||||
ref Sequence r = ref sequence[i];
|
||||
|
||||
table.Query(i, cNumTracks, out r.NumTracks);
|
||||
table.Query(i, cTrackIndex, out r.TrackIndexOffset, out r.TrackIndexSize);
|
||||
table.Query(i, cType, out r.Type);
|
||||
}
|
||||
}
|
||||
|
||||
void LoadAcbSequence(uint index)
|
||||
{
|
||||
PreloadAcbSequence();
|
||||
|
||||
if (index > sequenceRows)
|
||||
throw new ArgumentOutOfRangeException(nameof(index));
|
||||
|
||||
ref Sequence r = ref sequence[index];
|
||||
|
||||
sequenceDepth++;
|
||||
|
||||
if (sequenceDepth > 3)
|
||||
throw new Exception("Sequence depth too high.");
|
||||
|
||||
if (r.NumTracks * 2 > r.TrackIndexSize)
|
||||
throw new Exception("Wrong Sequence.TrackIndex size.");
|
||||
|
||||
switch (r.Type)
|
||||
{
|
||||
default:
|
||||
for (int i = 0; i < r.NumTracks; i++)
|
||||
{
|
||||
sequenceReader.BaseStream.Position = r.TrackIndexOffset + i * 2;
|
||||
|
||||
short trackIndexIndex = sequenceReader.ReadInt16BE();
|
||||
LoadAcbTrack((ushort)trackIndexIndex);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
sequenceDepth--;
|
||||
}
|
||||
|
||||
void PreloadAcbBlock()
|
||||
{
|
||||
ref int rows = ref blockRows;
|
||||
|
||||
if (rows != 0) return;
|
||||
if (!OpenUtfSubtable(out blockReader, out UtfTable table, "BlockTable", out rows))
|
||||
throw new Exception("Failure opening Block table.");
|
||||
if (rows == 0) return;
|
||||
|
||||
block = new Block[rows];
|
||||
|
||||
int cNumTracks = table.GetColumn("NumTracks");
|
||||
int cTrackIndex = table.GetColumn("TrackIndex");
|
||||
|
||||
for (int i = 0; i < rows; i++)
|
||||
{
|
||||
ref Block r = ref block[i];
|
||||
table.Query(i, cNumTracks, out r.NumTracks);
|
||||
table.Query(i, cTrackIndex, out VLData data);
|
||||
r.TrackIndexOffset = data.Offset;
|
||||
r.TrackIndexSize = data.Size;
|
||||
}
|
||||
}
|
||||
|
||||
void LoadAcbBlock(ushort index)
|
||||
{
|
||||
PreloadAcbBlock();
|
||||
|
||||
if (index > blockRows)
|
||||
throw new ArgumentOutOfRangeException(nameof(index));
|
||||
|
||||
ref Block r = ref block[index];
|
||||
|
||||
if (r.NumTracks * 2 > r.TrackIndexSize)
|
||||
throw new Exception("Wrong Block.TrackIndex size.");
|
||||
|
||||
for (int i = 0; i < r.NumTracks; i++)
|
||||
{
|
||||
blockReader.BaseStream.Position = r.TrackIndexOffset + i * 2;
|
||||
|
||||
short trackIndexIndex = blockReader.ReadInt16BE();
|
||||
LoadAcbTrack((ushort)trackIndexIndex);
|
||||
}
|
||||
}
|
||||
|
||||
void PreloadAcbBlockSequence()
|
||||
{
|
||||
ref int rows = ref blockSequenceRows;
|
||||
|
||||
if (rows != 0) return;
|
||||
if (!OpenUtfSubtable(out blockSequenceReader, out UtfTable table, "BlockSequenceTable", out rows))
|
||||
throw new Exception("Failure opening BlockSequence table.");
|
||||
if (rows == 0) return;
|
||||
|
||||
blockSequence = new BlockSequence[rows];
|
||||
|
||||
int cNumTracks = table.GetColumn("NumTracks");
|
||||
int cTrackIndex = table.GetColumn("TrackIndex");
|
||||
int cNumBlocks = table.GetColumn("NumBlocks");
|
||||
int cBlockIndex = table.GetColumn("BlockIndex");
|
||||
|
||||
for (int i = 0; i < rows; i++)
|
||||
{
|
||||
ref BlockSequence r = ref blockSequence[i];
|
||||
|
||||
table.Query(i, cNumTracks, out r.NumTracks);
|
||||
table.Query(i, cTrackIndex, out r.TrackIndexOffset, out r.TrackIndexSize);
|
||||
table.Query(i, cNumBlocks, out r.NumBlocks);
|
||||
table.Query(i, cBlockIndex, out r.BlockIndexOffset, out r.BlockIndexSize);
|
||||
}
|
||||
}
|
||||
|
||||
void LoadAcbBlockSequence(ushort index)
|
||||
{
|
||||
PreloadAcbBlockSequence();
|
||||
|
||||
if (index > blockSequenceRows)
|
||||
throw new ArgumentOutOfRangeException(nameof(index));
|
||||
|
||||
ref BlockSequence r = ref blockSequence[index];
|
||||
|
||||
if (r.NumTracks * 2 > r.TrackIndexSize)
|
||||
throw new Exception("Wrong BlockSequence.TrackIndex size.");
|
||||
|
||||
for (int i = 0; i < r.NumTracks; i++)
|
||||
{
|
||||
blockSequenceReader.BaseStream.Position = r.TrackIndexOffset + i * 2;
|
||||
|
||||
short trackIndexIndex = blockSequenceReader.ReadInt16();
|
||||
LoadAcbTrack((ushort)trackIndexIndex);
|
||||
}
|
||||
|
||||
if (r.NumBlocks * 2 > r.BlockIndexSize)
|
||||
throw new Exception("Wrong BlockSequence.BlockIndex size.");
|
||||
|
||||
for (int i = 0; i < r.NumBlocks; i++)
|
||||
{
|
||||
blockSequenceReader.BaseStream.Position = r.BlockIndexOffset + i * 2;
|
||||
|
||||
short blockIndexIndex = blockSequenceReader.ReadInt16();
|
||||
LoadAcbBlock((ushort)blockIndexIndex);
|
||||
}
|
||||
}
|
||||
|
||||
void PreloadAcbCue()
|
||||
{
|
||||
ref int rows = ref cueRows;
|
||||
|
||||
if (rows != 0) return;
|
||||
if (!OpenUtfSubtable(out cueReader, out UtfTable table, "CueTable", out rows))
|
||||
throw new Exception("Failure opening Cue table.");
|
||||
if (rows == 0) return;
|
||||
|
||||
cue = new Cue[rows];
|
||||
|
||||
int cReferenceType = table.GetColumn("ReferenceType");
|
||||
int cReferenceIndex = table.GetColumn("ReferenceIndex");
|
||||
|
||||
for (int i = 0; i < rows; i++)
|
||||
{
|
||||
ref Cue r = ref cue[i];
|
||||
|
||||
table.Query(i, cReferenceType, out r.ReferenceType);
|
||||
table.Query(i, cReferenceIndex, out r.ReferenceIndex);
|
||||
}
|
||||
}
|
||||
|
||||
void LoadAcbCue(ushort index)
|
||||
{
|
||||
PreloadAcbCue();
|
||||
|
||||
if (index > cueRows)
|
||||
throw new ArgumentOutOfRangeException(nameof(index));
|
||||
|
||||
ref Cue r = ref cue[index];
|
||||
|
||||
switch (r.ReferenceType)
|
||||
{
|
||||
case 1:
|
||||
LoadAcbWaveForm(r.ReferenceIndex);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
LoadAcbSynth(r.ReferenceIndex);
|
||||
break;
|
||||
|
||||
case 3:
|
||||
LoadAcbSequence(r.ReferenceIndex);
|
||||
break;
|
||||
|
||||
case 8:
|
||||
LoadAcbBlockSequence(r.ReferenceIndex);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void PreloadAcbCueName()
|
||||
{
|
||||
ref UtfTable table = ref cueNames;
|
||||
ref int rows = ref cueNameRows;
|
||||
|
||||
if (rows != 0) return;
|
||||
if (!OpenUtfSubtable(out cueNameReader, out table, "CueNameTable", out rows))
|
||||
throw new Exception("Failure opening CueName table.");
|
||||
if (rows == 0) return;
|
||||
|
||||
cueName = new CueName[rows];
|
||||
|
||||
int cCueIndex = table.GetColumn("CueIndex");
|
||||
int cCueName = table.GetColumn("CueName");
|
||||
|
||||
for (int i = 0; i < rows; i++)
|
||||
{
|
||||
ref CueName r = ref cueName[i];
|
||||
|
||||
table.Query(i, cCueIndex, out r.CueIndex);
|
||||
table.Query(i, cCueName, out r.Name);
|
||||
}
|
||||
}
|
||||
|
||||
void LoadAcbCueName(ushort index)
|
||||
{
|
||||
PreloadAcbCueName();
|
||||
|
||||
if (index > cueNameRows)
|
||||
throw new ArgumentOutOfRangeException(nameof(index));
|
||||
|
||||
ref CueName r = ref cueName[index];
|
||||
|
||||
cueNameIndex = (short)index;
|
||||
cueNameName = r.Name;
|
||||
|
||||
LoadAcbCue(r.CueIndex);
|
||||
}
|
||||
|
||||
public string LoadWaveName(int waveId, int port, bool memory)
|
||||
{
|
||||
targetWaveId = waveId;
|
||||
targetPort = port;
|
||||
isMemory = memory;
|
||||
|
||||
name = "";
|
||||
awbNameCount = 0;
|
||||
awbNameList = new List<short>();
|
||||
|
||||
PreloadAcbCueName();
|
||||
for (ushort i = 0; i < cueNameRows; i++)
|
||||
{
|
||||
LoadAcbCueName(i);
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 612a4d57772b3ad429b40eafbe7d0e3a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,57 @@
|
||||
using CriWareFormats.Common;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace CriWareFormats
|
||||
{
|
||||
public sealed class AcbReader : IDisposable
|
||||
{
|
||||
private readonly Stream outerStream;
|
||||
private readonly long positionOffset;
|
||||
private readonly uint awbOffset;
|
||||
private readonly uint awbLength;
|
||||
|
||||
private readonly AcbNameLoader acbNameLoader;
|
||||
|
||||
public AcbReader(Stream acbStream) : this(acbStream, 0) { }
|
||||
|
||||
public AcbReader(Stream acbStream, long offset)
|
||||
{
|
||||
outerStream = acbStream;
|
||||
positionOffset = offset;
|
||||
|
||||
acbStream.Position = positionOffset;
|
||||
|
||||
UtfTable utfTable = new UtfTable(acbStream, (uint)offset, out int rows, out string name);
|
||||
|
||||
if (rows != 1 || !name.Equals("Header"))
|
||||
throw new InvalidDataException("No Header table.");
|
||||
|
||||
if (!utfTable.Query(0, "AwbFile", out VLData awbValueData))
|
||||
throw new InvalidDataException("No embedded AWB file.");
|
||||
|
||||
awbOffset = awbValueData.Offset;
|
||||
awbLength = awbValueData.Size;
|
||||
|
||||
outerStream.Position = positionOffset;
|
||||
acbNameLoader = new AcbNameLoader(outerStream);
|
||||
}
|
||||
|
||||
public AwbReader GetAwb()
|
||||
{
|
||||
return new AwbReader(new SpliceStream(outerStream, awbOffset, awbLength), true);
|
||||
}
|
||||
|
||||
public string GetWaveName(int waveId, int port, bool memory)
|
||||
{
|
||||
outerStream.Position = positionOffset;
|
||||
return acbNameLoader.LoadWaveName(waveId, port, memory);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
outerStream.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 407dbae8a66935f499cad063a996b044
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,139 @@
|
||||
using CriWareFormats.Common;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace CriWareFormats
|
||||
{
|
||||
internal class Row
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public ushort Id { get; set; }
|
||||
}
|
||||
|
||||
public struct Wave
|
||||
{
|
||||
public int WaveId { get; set; }
|
||||
public long Offset { get; set; }
|
||||
public long Length { get; set; }
|
||||
}
|
||||
|
||||
public sealed class AwbReader : IDisposable
|
||||
{
|
||||
private readonly BinaryReader binaryReader;
|
||||
|
||||
private readonly byte offsetSize;
|
||||
private readonly int totalSubsongs;
|
||||
private readonly ushort alignment;
|
||||
private readonly ushort subkey;
|
||||
|
||||
private readonly List<Row> cueNames;
|
||||
private readonly List<Wave> waves;
|
||||
|
||||
public AwbReader(Stream awbStream, bool isEmbedded) : this(awbStream, 0)
|
||||
{
|
||||
IsEmbedded = isEmbedded;
|
||||
}
|
||||
|
||||
public AwbReader(Stream awbStream) : this(awbStream, 0) { }
|
||||
|
||||
public AwbReader(Stream awbStream, long positionOffset)
|
||||
{
|
||||
binaryReader = new BinaryReader(awbStream);
|
||||
|
||||
binaryReader.BaseStream.Position = positionOffset;
|
||||
|
||||
if (!binaryReader.ReadChars(4).SequenceEqual("AFS2".ToCharArray()))
|
||||
throw new InvalidDataException("Incorrect magic.");
|
||||
|
||||
binaryReader.BaseStream.Position += 0x1;
|
||||
offsetSize = binaryReader.ReadByte();
|
||||
binaryReader.BaseStream.Position += 0x2;
|
||||
totalSubsongs = binaryReader.ReadInt32();
|
||||
alignment = binaryReader.ReadUInt16();
|
||||
subkey = binaryReader.ReadUInt16();
|
||||
|
||||
waves = new List<Wave>(totalSubsongs);
|
||||
|
||||
for (int subsong = 1; subsong <= totalSubsongs; subsong++)
|
||||
{
|
||||
long offset = 0x10;
|
||||
|
||||
long waveIdOffset = offset + (subsong - 1) * 0x2;
|
||||
|
||||
binaryReader.BaseStream.Position = waveIdOffset;
|
||||
|
||||
int waveId = binaryReader.ReadUInt16();
|
||||
|
||||
offset += totalSubsongs * 0x2;
|
||||
|
||||
long subfileOffset = 0;
|
||||
long subfileNext = 0;
|
||||
long fileSize = binaryReader.BaseStream.Length;
|
||||
|
||||
offset += (subsong - 1) * offsetSize;
|
||||
|
||||
binaryReader.BaseStream.Position = offset;
|
||||
|
||||
switch (offsetSize)
|
||||
{
|
||||
case 0x4:
|
||||
subfileOffset = binaryReader.ReadUInt32();
|
||||
subfileNext = binaryReader.ReadUInt32();
|
||||
break;
|
||||
|
||||
case 0x2:
|
||||
subfileOffset = binaryReader.ReadUInt16();
|
||||
subfileNext = binaryReader.ReadUInt16();
|
||||
break;
|
||||
|
||||
default:
|
||||
Fail();
|
||||
break;
|
||||
}
|
||||
|
||||
subfileOffset += subfileOffset % alignment > 0 ?
|
||||
alignment - subfileOffset % alignment : 0;
|
||||
subfileNext += subfileNext % alignment > 0 && subfileNext < fileSize ?
|
||||
alignment - subfileNext % alignment : 0;
|
||||
long subfileSize = subfileNext - subfileOffset;
|
||||
|
||||
waves.Add(new Wave()
|
||||
{
|
||||
WaveId = waveId,
|
||||
Offset = subfileOffset,
|
||||
Length = subfileSize
|
||||
});
|
||||
|
||||
//if (CueNames.Count > 0) Console.Write("{0,-36}", CueNames.Where(row => row.Index == waveId).First().Name);
|
||||
//else Console.Write(waveId);
|
||||
|
||||
//Console.WriteLine(subfileOffset);
|
||||
}
|
||||
|
||||
cueNames = new List<Row>();
|
||||
}
|
||||
|
||||
public ushort Subkey => subkey;
|
||||
|
||||
public bool IsEmbedded { get; }
|
||||
|
||||
public List<Wave> Waves => waves;
|
||||
|
||||
public Stream GetWaveSubfileStream(Wave wave)
|
||||
{
|
||||
return new SpliceStream(binaryReader.BaseStream, wave.Offset, wave.Length);
|
||||
}
|
||||
|
||||
private static void Fail()
|
||||
{
|
||||
throw new Exception("Failure reading AWB file.");
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
binaryReader.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0e1fffc1a1dfe27459a35520448ccaf0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 13c5ca40b071c634bb7e480a0479ad81
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Assets.Scripts.Audio.CriWareFormats.Common
|
||||
{
|
||||
public class BinaryPrimitives
|
||||
{
|
||||
internal static ushort ReverseEndianness(ushort le)
|
||||
{
|
||||
byte[] tmp = BitConverter.GetBytes(le);
|
||||
Array.Reverse(tmp);
|
||||
return BitConverter.ToUInt16(tmp,0);
|
||||
}
|
||||
|
||||
internal static int ReverseEndianness(int le)
|
||||
{
|
||||
byte[] tmp = BitConverter.GetBytes(le);
|
||||
Array.Reverse(tmp);
|
||||
return BitConverter.ToInt32(tmp, 0);
|
||||
}
|
||||
|
||||
internal static uint ReverseEndianness(uint le)
|
||||
{
|
||||
byte[] tmp = BitConverter.GetBytes(le);
|
||||
Array.Reverse(tmp);
|
||||
return BitConverter.ToUInt32(tmp, 0);
|
||||
}
|
||||
|
||||
internal static long ReverseEndianness(long le)
|
||||
{
|
||||
byte[] tmp = BitConverter.GetBytes(le);
|
||||
Array.Reverse(tmp);
|
||||
return BitConverter.ToInt64(tmp, 0);
|
||||
}
|
||||
|
||||
internal static ulong ReverseEndianness(ulong le)
|
||||
{
|
||||
byte[] tmp = BitConverter.GetBytes(le);
|
||||
Array.Reverse(tmp);
|
||||
return BitConverter.ToUInt64(tmp, 0);
|
||||
}
|
||||
|
||||
public static float Int32BitsToSingle(int m)
|
||||
{
|
||||
return BitConverter.ToSingle(BitConverter.GetBytes(m), 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 35c4bac6c1dd0664b975a22cf7ea445a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,69 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Runtime;
|
||||
using Assets.Scripts.Audio.CriWareFormats.Common;
|
||||
|
||||
namespace CriWareFormats.Common
|
||||
{
|
||||
public class BinaryReaderEndian : BinaryReader
|
||||
|
||||
{
|
||||
public BinaryReaderEndian(Stream input) : base(input)
|
||||
{
|
||||
}
|
||||
|
||||
public short ReadInt16BE()
|
||||
{
|
||||
ushort le = base.ReadUInt16();
|
||||
return (short)BinaryPrimitives.ReverseEndianness(le);
|
||||
}
|
||||
|
||||
public ushort ReadUInt16BE()
|
||||
{
|
||||
ushort le = base.ReadUInt16();
|
||||
return BinaryPrimitives.ReverseEndianness(le);
|
||||
}
|
||||
|
||||
public int ReadInt32BE()
|
||||
{
|
||||
uint le = base.ReadUInt32();
|
||||
return (int)BinaryPrimitives.ReverseEndianness(le);
|
||||
}
|
||||
|
||||
public uint ReadUInt32BE()
|
||||
{
|
||||
uint le = base.ReadUInt32();
|
||||
return BinaryPrimitives.ReverseEndianness(le);
|
||||
}
|
||||
|
||||
public long ReadInt64BE()
|
||||
{
|
||||
ulong le = base.ReadUInt64();
|
||||
return (long)BinaryPrimitives.ReverseEndianness(le);
|
||||
}
|
||||
|
||||
public ulong ReadUInt64BE()
|
||||
{
|
||||
ulong le = base.ReadUInt64();
|
||||
return BinaryPrimitives.ReverseEndianness(le);
|
||||
}
|
||||
|
||||
public float ReadSingleBE()
|
||||
{
|
||||
float le = base.ReadSingle();
|
||||
byte[] floatBytes = BitConverter.GetBytes(le);
|
||||
byte[] reversed = new byte[4];
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
reversed[i] = floatBytes[3 - i];
|
||||
}
|
||||
return BitConverter.ToSingle(reversed, 0);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f17047d6b7698144588e1d5181473cf9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace CriWareFormats.Common
|
||||
{
|
||||
public static class Endian
|
||||
{
|
||||
public static ushort Reverse(ushort value)
|
||||
{
|
||||
return (ushort)(value << 8 | value >> 8);
|
||||
}
|
||||
|
||||
public static uint Reverse(uint value)
|
||||
{
|
||||
value = (value & 0x00FF00FFu) << 8 | (value & 0xFF00FF00u) >> 8;
|
||||
return (value & 0x0000FFFFu) << 16 | (value & 0xFFFF0000u) >> 16;
|
||||
}
|
||||
|
||||
public static ulong Reverse(ulong value)
|
||||
{
|
||||
value = (value & 0x00FF00FF00FF00FFu) << 8 | (value & 0xFF00FF00FF00FF00u) >> 8;
|
||||
value = (value & 0x0000FFFF0000FFFFu) << 16 | (value & 0xFFFF0000FFFF0000u) >> 16;
|
||||
return value << 32 | value >> 32;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dfc4d0df7cc8c294ead855ed1ccc762d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,116 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace CriWareFormats
|
||||
{
|
||||
public class SpliceStream : Stream
|
||||
{
|
||||
private readonly Stream innerStream;
|
||||
private readonly long realPosition;
|
||||
|
||||
private long internalPosition;
|
||||
|
||||
private readonly object positionLock = new object();
|
||||
|
||||
public SpliceStream(Stream sourceStream, long offset, long length)
|
||||
{
|
||||
innerStream = sourceStream;
|
||||
realPosition = offset;
|
||||
internalPosition = 0;
|
||||
|
||||
Length = length;
|
||||
}
|
||||
|
||||
public override bool CanRead => true;
|
||||
|
||||
public override bool CanSeek => true;
|
||||
|
||||
public override bool CanWrite => false;
|
||||
|
||||
public override long Length { get; }
|
||||
|
||||
public override long Position
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (positionLock)
|
||||
{
|
||||
return internalPosition;
|
||||
}
|
||||
}
|
||||
set
|
||||
{
|
||||
lock (positionLock)
|
||||
{
|
||||
long checkValue = value + realPosition;
|
||||
if (value < 0 || value >= Length) throw new ArgumentOutOfRangeException(nameof(value));
|
||||
internalPosition = value;
|
||||
|
||||
if (innerStream.CanSeek) innerStream.Position = checkValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
lock (positionLock)
|
||||
{
|
||||
if (innerStream.CanRead)
|
||||
{
|
||||
long restore = innerStream.Position;
|
||||
innerStream.Position = realPosition + internalPosition;
|
||||
int read = innerStream.Read(buffer, offset, count);
|
||||
internalPosition += read;
|
||||
innerStream.Position = restore;
|
||||
return read;
|
||||
}
|
||||
else return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
lock (positionLock)
|
||||
{
|
||||
switch (origin)
|
||||
{
|
||||
case SeekOrigin.Begin:
|
||||
if (offset >= Length) throw new ArgumentOutOfRangeException(nameof(offset));
|
||||
internalPosition = offset;
|
||||
break;
|
||||
|
||||
case SeekOrigin.Current:
|
||||
if (internalPosition + offset >= Length) throw new ArgumentOutOfRangeException(nameof(offset));
|
||||
internalPosition += offset;
|
||||
break;
|
||||
|
||||
case SeekOrigin.End:
|
||||
if (internalPosition - offset < 0) throw new ArgumentOutOfRangeException(nameof(offset));
|
||||
internalPosition = Length;
|
||||
internalPosition -= offset;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return internalPosition;
|
||||
}
|
||||
}
|
||||
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5e2b71da36bdac14fae41451686dac14
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,435 @@
|
||||
using CriWareFormats.Common;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace CriWareFormats
|
||||
{
|
||||
[Flags]
|
||||
public enum ColumnFlag : byte
|
||||
{
|
||||
Name = 0x10,
|
||||
Default = 0x20,
|
||||
Row = 0x40,
|
||||
Undefined = 0x80
|
||||
}
|
||||
|
||||
public enum ColumnType : byte
|
||||
{
|
||||
Byte = 0x00,
|
||||
SByte = 0x01,
|
||||
UInt16 = 0x02,
|
||||
Int16 = 0x03,
|
||||
UInt32 = 0x04,
|
||||
Int32 = 0x05,
|
||||
UInt64 = 0x06,
|
||||
Int64 = 0x07,
|
||||
Float = 0x08,
|
||||
Double = 0x09,
|
||||
String = 0x0A,
|
||||
VLData = 0x0B,
|
||||
UInt128 = 0x0C,
|
||||
Undefined = 0xFF
|
||||
}
|
||||
|
||||
public struct Column
|
||||
{
|
||||
public ColumnFlag Flag;
|
||||
public ColumnType Type;
|
||||
public string Name;
|
||||
public uint Offset;
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
}
|
||||
|
||||
public struct VLData
|
||||
{
|
||||
public uint Offset;
|
||||
public uint Size;
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"Offset: {Offset}, Length: {Size}";
|
||||
}
|
||||
}
|
||||
|
||||
public struct Result
|
||||
{
|
||||
public ColumnType Type;
|
||||
public object Value;
|
||||
}
|
||||
|
||||
public sealed class UtfTable
|
||||
{
|
||||
private readonly BinaryReaderEndian binaryReader;
|
||||
private readonly uint tableOffset;
|
||||
|
||||
private readonly uint tableSize;
|
||||
private readonly ushort version;
|
||||
private readonly ushort rowsOffset;
|
||||
private readonly uint stringsOffset;
|
||||
private readonly uint dataOffset;
|
||||
private readonly uint nameOffset;
|
||||
|
||||
private readonly ushort columns;
|
||||
private readonly ushort rowWidth;
|
||||
private readonly uint rows;
|
||||
|
||||
private readonly byte[] schemaBuffer;
|
||||
private readonly Column[] schema;
|
||||
|
||||
private readonly uint schemaOffset;
|
||||
private readonly uint schemaSize;
|
||||
private readonly uint rowsSize;
|
||||
private readonly uint dataSize;
|
||||
private readonly uint stringsSize;
|
||||
|
||||
private readonly byte[] stringTable;
|
||||
private readonly string tableName;
|
||||
|
||||
public UtfTable(Stream utfTableStream, out int utfTableRows, out string utfTableRowName) :
|
||||
this(utfTableStream, 0, out utfTableRows, out utfTableRowName)
|
||||
{ }
|
||||
|
||||
public UtfTable(Stream utfTableStream, uint offset) :
|
||||
this(utfTableStream, offset, out int _, out string _)
|
||||
{ }
|
||||
|
||||
public UtfTable(Stream utfTableStream, uint offset, out int utfTableRows, out string rowName)
|
||||
{
|
||||
binaryReader = new BinaryReaderEndian(utfTableStream);
|
||||
tableOffset = offset;
|
||||
|
||||
binaryReader.BaseStream.Position = offset;
|
||||
|
||||
if (!binaryReader.ReadChars(4).SequenceEqual("@UTF".ToCharArray()))
|
||||
throw new InvalidDataException("Incorrect magic.");
|
||||
tableSize = binaryReader.ReadUInt32BE() + 0x08;
|
||||
version = binaryReader.ReadUInt16BE();
|
||||
rowsOffset = (ushort)(binaryReader.ReadUInt16BE() + 0x08);
|
||||
stringsOffset = binaryReader.ReadUInt32BE() + 0x08;
|
||||
dataOffset = binaryReader.ReadUInt32BE() + 0x08;
|
||||
nameOffset = binaryReader.ReadUInt32BE();
|
||||
columns = binaryReader.ReadUInt16BE();
|
||||
rowWidth = binaryReader.ReadUInt16BE();
|
||||
rows = binaryReader.ReadUInt32BE();
|
||||
|
||||
schemaOffset = 0x20;
|
||||
schemaSize = rowsOffset - schemaOffset;
|
||||
rowsSize = stringsOffset - rowsOffset;
|
||||
stringsSize = dataOffset - stringsOffset;
|
||||
dataSize = tableSize - dataOffset;
|
||||
|
||||
if (version != 0x00 && version != 0x01)
|
||||
throw new InvalidDataException("Unknown @UTF version.");
|
||||
if (tableOffset + tableSize > utfTableStream.Length)
|
||||
throw new InvalidDataException("Table size exceeds bounds of file.");
|
||||
if (rowsOffset > tableSize || stringsOffset > tableSize || dataOffset > tableSize)
|
||||
throw new InvalidDataException("Offsets out of bounds.");
|
||||
if (stringsSize <= 0 || nameOffset > stringsSize)
|
||||
throw new InvalidDataException("Invalid string table size.");
|
||||
if (Columns <= 0)
|
||||
throw new InvalidDataException("Table has no columns.");
|
||||
|
||||
schemaBuffer = new byte[schemaSize];
|
||||
binaryReader.BaseStream.Position = tableOffset + schemaOffset;
|
||||
if (binaryReader.Read(schemaBuffer, 0, (int)schemaSize) != schemaSize)
|
||||
throw new InvalidDataException("Failed to read schema.");
|
||||
|
||||
stringTable = new byte[stringsSize];
|
||||
binaryReader.BaseStream.Position = tableOffset + stringsOffset;
|
||||
if (binaryReader.Read(stringTable, 0, (int)stringsSize) != stringsSize)
|
||||
throw new InvalidDataException("Failed to read string table.");
|
||||
|
||||
uint columnOffset = 0;
|
||||
uint schemaPos = 0;
|
||||
|
||||
tableName = GetStringFromTable(nameOffset);
|
||||
|
||||
schema = new Column[columns];
|
||||
|
||||
BinaryReaderEndian bytesReader = new BinaryReaderEndian(new MemoryStream(schemaBuffer) { Position = 0 });
|
||||
for (int i = 0; i < columns; i++)
|
||||
{
|
||||
bytesReader.BaseStream.Position = schemaPos;
|
||||
|
||||
byte info = bytesReader.ReadByte();
|
||||
uint nameOffset = bytesReader.ReadUInt32BE();
|
||||
|
||||
if (nameOffset > stringsSize)
|
||||
throw new InvalidDataException("String offset out of bounds.");
|
||||
schemaPos += 0x1 + 0x4;
|
||||
|
||||
bytesReader.BaseStream.Position = schemaPos;
|
||||
|
||||
schema[i] = new Column()
|
||||
{
|
||||
Flag = (ColumnFlag)(info & 0xF0),
|
||||
Type = (ColumnType)(info & 0x0F),
|
||||
Name = "",
|
||||
Offset = 0
|
||||
};
|
||||
|
||||
if (schema[i].Flag == 0 ||
|
||||
!schema[i].Flag.HasFlag(ColumnFlag.Name) ||
|
||||
schema[i].Flag.HasFlag(ColumnFlag.Default) && schema[i].Flag.HasFlag(ColumnFlag.Row) ||
|
||||
schema[i].Flag.HasFlag(ColumnFlag.Undefined))
|
||||
throw new InvalidDataException("Unknown column flag combo found.");
|
||||
|
||||
uint valueSize;
|
||||
switch (schema[i].Type)
|
||||
{
|
||||
case ColumnType.Byte:
|
||||
case ColumnType.SByte:
|
||||
valueSize = 0x1;
|
||||
break;
|
||||
case ColumnType.UInt16:
|
||||
case ColumnType.Int16:
|
||||
valueSize = 0x2;
|
||||
break;
|
||||
case ColumnType.UInt32:
|
||||
case ColumnType.Int32:
|
||||
case ColumnType.Float:
|
||||
case ColumnType.String:
|
||||
valueSize = 0x4;
|
||||
break;
|
||||
case ColumnType.UInt64:
|
||||
case ColumnType.Int64:
|
||||
case ColumnType.VLData:
|
||||
valueSize = 0x8;
|
||||
break;
|
||||
default:
|
||||
throw new InvalidDataException("Invalid column type.");
|
||||
};
|
||||
|
||||
if (schema[i].Flag.HasFlag(ColumnFlag.Name))
|
||||
schema[i].Name = GetStringFromTable(nameOffset);
|
||||
|
||||
if (schema[i].Flag.HasFlag(ColumnFlag.Default))
|
||||
{
|
||||
schema[i].Offset = schemaPos;
|
||||
schemaPos += valueSize;
|
||||
|
||||
bytesReader.BaseStream.Position = schemaPos;
|
||||
|
||||
}
|
||||
|
||||
if (schema[i].Flag.HasFlag(ColumnFlag.Row))
|
||||
{
|
||||
schema[i].Offset = columnOffset;
|
||||
columnOffset += valueSize;
|
||||
}
|
||||
}
|
||||
|
||||
utfTableRows = (int)rows;
|
||||
rowName = GetStringFromTable(nameOffset);
|
||||
|
||||
bytesReader.Dispose();
|
||||
}
|
||||
|
||||
public ushort Columns => columns;
|
||||
|
||||
public uint Rows => rows;
|
||||
|
||||
public Column[] Schema => schema;
|
||||
|
||||
public string TableName => tableName;
|
||||
|
||||
public int GetColumn(string columnName)
|
||||
{
|
||||
for (int i = 0; i < columns; i++)
|
||||
{
|
||||
Column column = schema[i];
|
||||
|
||||
if (column.Name is null || !column.Name.Equals(columnName))
|
||||
continue;
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
private bool Query(int row, int column, out Result result)
|
||||
{
|
||||
result = new Result();
|
||||
|
||||
if (row >= rows || row < 0)
|
||||
//throw new ArgumentOutOfRangeException(nameof(row));
|
||||
return false;
|
||||
if (column >= columns || column < 0)
|
||||
//throw new ArgumentOutOfRangeException(nameof(column));
|
||||
return false;
|
||||
|
||||
Column col = schema[column];
|
||||
uint dataOffset = 0;
|
||||
BinaryReaderEndian bytesReader = null;
|
||||
|
||||
result.Type = col.Type;
|
||||
|
||||
if (col.Flag.HasFlag(ColumnFlag.Default))
|
||||
{
|
||||
if (schemaBuffer != null)
|
||||
{
|
||||
bytesReader = new BinaryReaderEndian(new MemoryStream(schemaBuffer));
|
||||
bytesReader.BaseStream.Position = col.Offset;
|
||||
}
|
||||
else
|
||||
{
|
||||
dataOffset = tableOffset + schemaOffset + col.Offset;
|
||||
}
|
||||
}
|
||||
else if (col.Flag.HasFlag(ColumnFlag.Row))
|
||||
{
|
||||
dataOffset = (uint)(tableOffset + rowsOffset + row * rowWidth + col.Offset);
|
||||
}
|
||||
else
|
||||
throw new InvalidDataException("Invalid flag.");
|
||||
|
||||
binaryReader.BaseStream.Position = dataOffset;
|
||||
|
||||
switch (col.Type)
|
||||
{
|
||||
case ColumnType.Byte:
|
||||
result.Value = (bytesReader != null) ? bytesReader.ReadByte() : binaryReader.ReadByte();
|
||||
break;
|
||||
case ColumnType.SByte:
|
||||
result.Value = (bytesReader != null) ? bytesReader.ReadSByte() : binaryReader.ReadSByte();
|
||||
break;
|
||||
case ColumnType.UInt16:
|
||||
result.Value = (bytesReader != null) ? bytesReader.ReadUInt16BE() : binaryReader.ReadUInt16BE();
|
||||
break;
|
||||
case ColumnType.Int16:
|
||||
result.Value = (bytesReader != null) ? bytesReader.ReadInt16BE() : binaryReader.ReadInt16BE();
|
||||
break;
|
||||
case ColumnType.UInt32:
|
||||
result.Value = (bytesReader != null) ? bytesReader.ReadUInt32BE() : binaryReader.ReadUInt32BE();
|
||||
break;
|
||||
case ColumnType.Int32:
|
||||
result.Value = (bytesReader != null) ? bytesReader.ReadInt32BE() : binaryReader.ReadInt32BE();
|
||||
break;
|
||||
case ColumnType.UInt64:
|
||||
result.Value = (bytesReader != null) ? bytesReader.ReadUInt64BE() : binaryReader.ReadUInt64BE();
|
||||
break;
|
||||
case ColumnType.Int64:
|
||||
result.Value = (bytesReader != null) ? bytesReader.ReadInt64BE() : binaryReader.ReadInt64BE();
|
||||
break;
|
||||
case ColumnType.Float:
|
||||
result.Value = (bytesReader != null) ? bytesReader.ReadSingleBE() : binaryReader.ReadSingleBE();
|
||||
break;
|
||||
//case ColumnType.Double:
|
||||
// break;
|
||||
case ColumnType.String:
|
||||
uint nameOffset = (bytesReader != null) ? bytesReader.ReadUInt32BE() : binaryReader.ReadUInt32BE();
|
||||
if (nameOffset > stringsSize)
|
||||
throw new InvalidDataException("Name offset out of bounds.");
|
||||
result.Value = GetStringFromTable(nameOffset);
|
||||
break;
|
||||
case ColumnType.VLData:
|
||||
if ((bytesReader != null))
|
||||
{
|
||||
result.Value = new VLData()
|
||||
{
|
||||
Offset = bytesReader.ReadUInt32BE(),
|
||||
Size = bytesReader.ReadUInt32BE()
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
result.Value = new VLData()
|
||||
{
|
||||
Offset = binaryReader.ReadUInt32BE(),
|
||||
Size = binaryReader.ReadUInt32BE()
|
||||
};
|
||||
}
|
||||
break;
|
||||
//case ColumnType.UInt128:
|
||||
// break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Query<T>(int row, int column, out T value)
|
||||
{
|
||||
bool valid = Query(row, column, out Result result);
|
||||
|
||||
bool enumParseResult = Enum.TryParse<ColumnType>(typeof(T).Name, out ColumnType type);
|
||||
|
||||
if (!valid || !enumParseResult || result.Type != type)
|
||||
{
|
||||
value = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
value = (T)result.Value;
|
||||
|
||||
if (value is VLData vlData)
|
||||
{
|
||||
vlData.Offset += tableOffset + dataOffset;
|
||||
value = (T)(object)vlData;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Query<T>(int row, string columnName, out T value) =>
|
||||
Query(row, GetColumn(columnName), out value);
|
||||
|
||||
public bool Query(int row, int column, out uint offset, out uint size)
|
||||
{
|
||||
if (!Query(row, column, out VLData data))
|
||||
{
|
||||
offset = 0;
|
||||
size = 0;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
offset = data.Offset;
|
||||
size = data.Size;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Query(int row, string columnName, out uint offset, out uint size)
|
||||
=> Query(row, GetColumn(columnName), out offset, out size);
|
||||
|
||||
private string GetStringFromTable(uint offset)
|
||||
{
|
||||
if (offset > stringsSize)
|
||||
throw new InvalidDataException("Invalid string offset.");
|
||||
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
|
||||
int i = 0;
|
||||
while (i < stringsSize)
|
||||
{
|
||||
stringBuilder.Append((char)stringTable[offset + i++]);
|
||||
if (stringTable[offset + i] == '\0') break;
|
||||
}
|
||||
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
|
||||
public UtfTable OpenSubtable(string tableName)
|
||||
{
|
||||
if (!Query(0, tableName, out VLData tableValueData))
|
||||
throw new ArgumentException("Subtable does not exist.");
|
||||
|
||||
binaryReader.BaseStream.Position = tableValueData.Offset;
|
||||
|
||||
return new UtfTable(
|
||||
binaryReader.BaseStream,
|
||||
tableValueData.Offset,
|
||||
out int _,
|
||||
out string _);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d662e0678754d5945b5223b3ab889993
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user