SonicAudioTools/Source/SonicAudioLib/CriMw/CriTableReader.cs

666 lines
19 KiB
C#
Raw Normal View History

2016-12-31 13:56:10 +01:00
using SonicAudioLib.IO;
2016-11-12 17:02:48 +01:00
using System;
2016-12-31 13:56:10 +01:00
using System.Collections.Generic;
using System.IO;
2016-11-12 17:02:48 +01:00
using System.Text;
using System.Linq;
using System.Collections;
2016-11-12 17:02:48 +01:00
namespace SonicAudioLib.CriMw
{
public class CriTableReader : IDisposable
{
2016-12-31 13:56:10 +01:00
private List<CriTableField> fields;
2016-11-12 17:02:48 +01:00
private Stream source;
private CriTableHeader header;
2017-01-30 11:47:50 +01:00
private Encoding encoding;
private long rowIndex = -1;
private long headerPosition;
2016-11-12 17:02:48 +01:00
private bool leaveOpen;
public object this[int fieldIndex]
{
get
{
return GetValue(fieldIndex);
}
}
public object this[string fieldName]
{
get
{
return GetValue(fieldName);
}
}
public ushort NumberOfFields
{
get
{
return header.FieldCount;
2016-11-12 17:02:48 +01:00
}
}
public uint NumberOfRows
{
get
{
return header.RowCount;
2016-11-12 17:02:48 +01:00
}
}
public string TableName
{
get
{
return header.TableName;
}
}
2017-01-30 11:47:50 +01:00
public long CurrentRow
2016-11-12 17:02:48 +01:00
{
get
{
return rowIndex;
}
}
public Stream SourceStream
{
get
{
return source;
}
}
2017-01-30 11:47:50 +01:00
public Encoding EncodingType
{
get
{
return encoding;
}
}
2016-11-12 17:02:48 +01:00
private void ReadTable()
{
headerPosition = source.Position;
2016-11-12 17:02:48 +01:00
if (!(header.Signature = DataStream.ReadBytes(source, 4)).SequenceEqual(CriTableHeader.SignatureBytes))
2016-11-12 17:02:48 +01:00
{
2017-01-26 22:44:33 +01:00
MemoryStream unmaskedSource = new MemoryStream();
CriTableMasker.FindKeys(header.Signature, out uint x, out uint m);
2017-01-26 22:44:33 +01:00
source.Seek(headerPosition, SeekOrigin.Begin);
CriTableMasker.Mask(source, unmaskedSource, source.Length, x, m);
2017-01-26 22:44:33 +01:00
// Close the old stream
if (!leaveOpen)
{
source.Close();
}
source = unmaskedSource;
source.Seek(4, SeekOrigin.Begin);
2016-11-12 17:02:48 +01:00
}
header.Length = DataStream.ReadUInt32BE(source) + 0x8;
header.UnknownByte = DataStream.ReadByte(source);
header.EncodingType = DataStream.ReadByte(source);
2017-01-30 11:47:50 +01:00
if (header.UnknownByte != 0)
{
throw new InvalidDataException($"Invalid byte ({header.UnknownByte}. Please report this error with the file(s).");
2017-01-30 11:47:50 +01:00
}
2017-01-30 11:47:50 +01:00
switch (header.EncodingType)
{
case CriTableHeader.EncodingTypeShiftJis:
encoding = Encoding.GetEncoding("shift-jis");
break;
case CriTableHeader.EncodingTypeUtf8:
encoding = Encoding.UTF8;
break;
default:
throw new InvalidDataException($"Unknown encoding type ({header.EncodingType}). Please report this error with the file(s).");
2017-01-30 11:47:50 +01:00
}
header.RowsPosition = (ushort)(DataStream.ReadUInt16BE(source) + 0x8);
header.StringPoolPosition = DataStream.ReadUInt32BE(source) + 0x8;
header.DataPoolPosition = DataStream.ReadUInt32BE(source) + 0x8;
2016-11-12 17:02:48 +01:00
header.TableName = ReadString();
header.FieldCount = DataStream.ReadUInt16BE(source);
header.RowLength = DataStream.ReadUInt16BE(source);
header.RowCount = DataStream.ReadUInt32BE(source);
2016-11-12 17:02:48 +01:00
uint offset = 0;
for (ushort i = 0; i < header.FieldCount; i++)
2016-11-12 17:02:48 +01:00
{
CriTableField field = new CriTableField();
field.Flag = (CriFieldFlag)DataStream.ReadByte(source);
2016-11-12 17:02:48 +01:00
if (field.Flag.HasFlag(CriFieldFlag.Name))
{
field.Name = ReadString();
}
if (field.Flag.HasFlag(CriFieldFlag.DefaultValue))
{
if (field.Flag.HasFlag(CriFieldFlag.Data))
{
field.Position = DataStream.ReadUInt32BE(source);
field.Length = DataStream.ReadUInt32BE(source);
2016-11-12 17:02:48 +01:00
}
else
{
field.Value = ReadValue(field.Flag);
}
}
// Not even per row, and not even constant value? Then there's no storage.
else if (!field.Flag.HasFlag(CriFieldFlag.RowStorage) && !field.Flag.HasFlag(CriFieldFlag.DefaultValue))
{
field.Value = CriField.NullValues[(byte)field.Flag & 0x0F];
2016-11-12 17:02:48 +01:00
}
// Row storage, calculate the offset
if (field.Flag.HasFlag(CriFieldFlag.RowStorage))
{
field.Offset = offset;
switch (field.Flag & CriFieldFlag.TypeMask)
{
case CriFieldFlag.Byte:
case CriFieldFlag.SByte:
offset += 1;
break;
case CriFieldFlag.Int16:
case CriFieldFlag.UInt16:
offset += 2;
break;
case CriFieldFlag.Int32:
case CriFieldFlag.UInt32:
case CriFieldFlag.Single:
case CriFieldFlag.String:
offset += 4;
break;
case CriFieldFlag.Int64:
case CriFieldFlag.UInt64:
case CriFieldFlag.Double:
case CriFieldFlag.Data:
offset += 8;
break;
}
}
2016-12-31 13:56:10 +01:00
fields.Add(field);
2016-11-12 17:02:48 +01:00
}
}
public string GetFieldName(int fieldIndex)
{
return fields[fieldIndex].Name;
}
public Type GetFieldType(int fieldIndex)
{
return CriField.FieldTypes[(byte)fields[fieldIndex].Flag & 0x0F];
}
public Type GetFieldType(string fieldName)
{
2016-12-31 13:56:10 +01:00
return CriField.FieldTypes[(byte)fields[GetFieldIndex(fieldName)].Flag & 0x0F];
2016-11-12 17:02:48 +01:00
}
public object GetFieldValue(int fieldIndex)
{
return fields[fieldIndex].Value;
}
2016-12-25 19:43:22 +01:00
internal CriFieldFlag GetFieldFlag(string fieldName)
2016-11-12 17:02:48 +01:00
{
2016-12-31 13:56:10 +01:00
return fields[GetFieldIndex(fieldName)].Flag;
2016-11-12 17:02:48 +01:00
}
2016-12-25 19:43:22 +01:00
internal CriFieldFlag GetFieldFlag(int fieldIndex)
2016-11-12 17:02:48 +01:00
{
2016-12-25 19:43:22 +01:00
return fields[fieldIndex].Flag;
2016-11-12 17:02:48 +01:00
}
public object GetFieldValue(string fieldName)
{
2016-12-31 13:56:10 +01:00
return fields[GetFieldIndex(fieldName)].Value;
2016-11-12 17:02:48 +01:00
}
public CriField GetField(int fieldIndex)
{
return new CriField(GetFieldName(fieldIndex), GetFieldType(fieldIndex), GetFieldValue(fieldIndex));
}
public CriField GetField(string fieldName)
{
return new CriField(fieldName, GetFieldType(fieldName), GetFieldValue(fieldName));
}
2016-12-31 13:56:10 +01:00
public int GetFieldIndex(string fieldName)
{
return fields.FindIndex(field => field.Name == fieldName);
}
2017-01-26 21:40:46 +01:00
public bool ContainsField(string fieldName)
{
return fields.Exists(field => field.Name == fieldName);
}
2016-11-12 17:02:48 +01:00
private void GoToValue(int fieldIndex)
{
source.Seek(headerPosition + header.RowsPosition + (header.RowLength * rowIndex) + fields[fieldIndex].Offset, SeekOrigin.Begin);
2016-11-12 17:02:48 +01:00
}
public bool Read()
{
if (rowIndex + 1 >= header.RowCount)
2016-11-12 17:02:48 +01:00
{
return false;
}
rowIndex++;
return true;
}
2017-01-30 11:47:50 +01:00
public bool MoveToRow(long rowIndex)
2016-11-12 17:02:48 +01:00
{
if (rowIndex >= header.RowCount)
2016-11-12 17:02:48 +01:00
{
return false;
}
this.rowIndex = rowIndex;
return true;
}
public object[] GetValueArray()
{
object[] values = new object[header.FieldCount];
2016-11-12 17:02:48 +01:00
for (int i = 0; i < header.FieldCount; i++)
2016-11-12 17:02:48 +01:00
{
if (fields[i].Flag.HasFlag(CriFieldFlag.Data))
{
values[i] = GetData(i);
}
else
{
values[i] = GetValue(i);
}
}
return values;
}
public IEnumerable GetValues()
{
for (int i = 0; i < header.FieldCount; i++)
{
yield return GetValue(i);
}
}
2016-11-12 17:02:48 +01:00
public object GetValue(int fieldIndex)
{
if (fieldIndex < 0 || fieldIndex >= fields.Count)
{
return null;
}
if (!fields[fieldIndex].Flag.HasFlag(CriFieldFlag.RowStorage))
{
if (fields[fieldIndex].Flag.HasFlag(CriFieldFlag.Data))
{
return new SubStream(source, 0, 0);
2016-11-12 17:02:48 +01:00
}
return fields[fieldIndex].Value;
}
GoToValue(fieldIndex);
return ReadValue(fields[fieldIndex].Flag);
}
public object GetValue(string fieldName)
{
2016-12-31 13:56:10 +01:00
return GetValue(GetFieldIndex(fieldName));
2016-11-12 17:02:48 +01:00
}
public T GetValue<T>(int fieldIndex)
{
return (T)GetValue(fieldIndex);
}
public T GetValue<T>(string fieldName)
{
return (T)GetValue(fieldName);
}
public byte GetByte(int fieldIndex)
{
return (byte)GetValue(fieldIndex);
}
public byte GetByte(string fieldName)
{
return (byte)GetValue(fieldName);
}
public sbyte GetSByte(int fieldIndex)
{
return (sbyte)GetValue(fieldIndex);
}
public sbyte GetSByte(string fieldName)
{
return (sbyte)GetValue(fieldName);
}
public ushort GetUInt16(int fieldIndex)
{
return (ushort)GetValue(fieldIndex);
}
public ushort GetUInt16(string fieldName)
{
return (ushort)GetValue(fieldName);
}
public short GetInt16(int fieldIndex)
{
return (short)GetValue(fieldIndex);
}
public short GetInt16(string fieldName)
{
return (short)GetValue(fieldName);
}
public uint GetUInt32(int fieldIndex)
{
return (uint)GetValue(fieldIndex);
}
public uint GetUInt32(string fieldName)
{
return (uint)GetValue(fieldName);
}
public int GetInt32(int fieldIndex)
{
return (int)GetValue(fieldIndex);
}
public int GetInt32(string fieldName)
{
return (int)GetValue(fieldName);
}
public ulong GetUInt64(int fieldIndex)
{
return (ulong)GetValue(fieldIndex);
}
public ulong GetUInt64(string fieldName)
{
return (ulong)GetValue(fieldName);
}
public long GetInt64(int fieldIndex)
{
return (long)GetValue(fieldIndex);
}
public long GetInt64(string fieldName)
{
return (long)GetValue(fieldName);
}
public float GetSingle(int fieldIndex)
2016-11-12 17:02:48 +01:00
{
return (float)GetValue(fieldIndex);
}
public float GetSingle(string fieldName)
2016-11-12 17:02:48 +01:00
{
return (float)GetValue(fieldName);
}
public double GetDouble(int fieldIndex)
{
return (double)GetValue(fieldIndex);
}
public double GetDouble(string fieldName)
{
return (double)GetValue(fieldName);
}
public string GetString(int fieldIndex)
{
return (string)GetValue(fieldIndex);
}
public string GetString(string fieldName)
{
return (string)GetValue(fieldName);
}
public SubStream GetSubStream(int fieldIndex)
2016-11-12 17:02:48 +01:00
{
return (SubStream)GetValue(fieldIndex);
2016-11-12 17:02:48 +01:00
}
public SubStream GetSubStream(string fieldName)
2016-11-12 17:02:48 +01:00
{
return (SubStream)GetValue(fieldName);
2016-11-12 17:02:48 +01:00
}
public byte[] GetData(int fieldIndex)
{
return GetSubStream(fieldIndex).ToArray();
2016-11-12 17:02:48 +01:00
}
public byte[] GetData(string fieldName)
{
2016-12-31 13:56:10 +01:00
return GetData(GetFieldIndex(fieldName));
2016-11-12 17:02:48 +01:00
}
public CriTableReader GetTableReader(string fieldName)
2016-12-25 19:43:22 +01:00
{
return new CriTableReader(GetSubStream(fieldName), false);
2016-12-25 19:43:22 +01:00
}
public CriTableReader GetTableReader(int fieldIndex)
2016-12-25 19:43:22 +01:00
{
return new CriTableReader(GetSubStream(fieldIndex), false);
2016-12-25 19:43:22 +01:00
}
2016-11-12 17:02:48 +01:00
public uint GetLength(int fieldIndex)
{
if (fieldIndex < 0 || fieldIndex >= fields.Count)
{
return 0;
}
if (!fields[fieldIndex].Flag.HasFlag(CriFieldFlag.RowStorage))
{
return fields[fieldIndex].Length;
}
GoToValue(fieldIndex);
source.Seek(4, SeekOrigin.Current);
return DataStream.ReadUInt32BE(source);
2016-11-12 17:02:48 +01:00
}
public uint GetLength(string fieldName)
{
2016-12-31 13:56:10 +01:00
return GetLength(GetFieldIndex(fieldName));
2016-11-12 17:02:48 +01:00
}
public uint GetPosition(int fieldIndex)
{
if (fieldIndex < 0 || fieldIndex >= fields.Count)
{
return 0;
}
if (!fields[fieldIndex].Flag.HasFlag(CriFieldFlag.RowStorage))
{
return fields[fieldIndex].Position;
}
GoToValue(fieldIndex);
return (uint)(headerPosition + header.DataPoolPosition + DataStream.ReadUInt32BE(source));
2016-11-12 17:02:48 +01:00
}
public uint GetPosition(string fieldName)
{
2016-12-31 13:56:10 +01:00
return GetPosition(GetFieldIndex(fieldName));
2016-11-12 17:02:48 +01:00
}
public bool GetBoolean(int fieldIndex)
{
return (byte)GetValue(fieldIndex) > 0;
}
public bool GetBoolean(string fieldName)
{
return (byte)GetValue(fieldName) > 0;
}
public Guid GetGuid(int fieldIndex)
{
return (Guid)GetValue(fieldIndex);
}
public Guid GetGuid(string fieldName)
{
return (Guid)GetValue(fieldName);
}
private string ReadString()
{
uint stringPosition = DataStream.ReadUInt32BE(source);
2016-11-12 17:02:48 +01:00
long previousPosition = source.Position;
source.Seek(headerPosition + header.StringPoolPosition + stringPosition, SeekOrigin.Begin);
string readString = DataStream.ReadCString(source, encoding);
source.Seek(previousPosition, SeekOrigin.Begin);
2016-11-12 17:02:48 +01:00
if (readString == StringPool.AdxBlankString || (readString == header.TableName && stringPosition == 0))
2016-11-12 17:02:48 +01:00
{
2016-12-25 19:43:22 +01:00
return string.Empty;
2016-11-12 17:02:48 +01:00
}
return readString;
2016-11-12 17:02:48 +01:00
}
private object ReadValue(CriFieldFlag fieldFlag)
{
switch (fieldFlag & CriFieldFlag.TypeMask)
{
case CriFieldFlag.Byte:
return DataStream.ReadByte(source);
2016-11-12 17:02:48 +01:00
case CriFieldFlag.SByte:
return DataStream.ReadSByte(source);
2016-11-12 17:02:48 +01:00
case CriFieldFlag.UInt16:
return DataStream.ReadUInt16BE(source);
2016-11-12 17:02:48 +01:00
case CriFieldFlag.Int16:
return DataStream.ReadInt16BE(source);
2016-11-12 17:02:48 +01:00
case CriFieldFlag.UInt32:
return DataStream.ReadUInt32BE(source);
2016-11-12 17:02:48 +01:00
case CriFieldFlag.Int32:
return DataStream.ReadInt32BE(source);
2016-11-12 17:02:48 +01:00
case CriFieldFlag.UInt64:
return DataStream.ReadUInt64BE(source);
2016-11-12 17:02:48 +01:00
case CriFieldFlag.Int64:
return DataStream.ReadInt64BE(source);
case CriFieldFlag.Single:
return DataStream.ReadSingleBE(source);
2016-11-12 17:02:48 +01:00
case CriFieldFlag.Double:
return DataStream.ReadDoubleBE(source);
2016-11-12 17:02:48 +01:00
case CriFieldFlag.String:
return ReadString();
case CriFieldFlag.Data:
{
uint position = DataStream.ReadUInt32BE(source);
uint length = DataStream.ReadUInt32BE(source);
2016-11-12 17:02:48 +01:00
2017-01-30 11:47:50 +01:00
// Some ACB files have the length info set to zero for UTF table fields, so find the correct length
if (position > 0 && length == 0)
2016-11-12 17:02:48 +01:00
{
source.Seek(headerPosition + header.DataPoolPosition + position, SeekOrigin.Begin);
2016-11-12 17:02:48 +01:00
if (DataStream.ReadBytes(source, 4).SequenceEqual(CriTableHeader.SignatureBytes))
2016-11-12 17:02:48 +01:00
{
length = DataStream.ReadUInt32BE(source) + 8;
2016-11-12 17:02:48 +01:00
}
}
return new SubStream(source, headerPosition + header.DataPoolPosition + position, length);
2016-11-12 17:02:48 +01:00
}
case CriFieldFlag.Guid:
return new Guid(DataStream.ReadBytes(source, 16));
2016-11-12 17:02:48 +01:00
}
return null;
}
public void Dispose()
{
fields.Clear();
if (!leaveOpen)
{
source.Close();
}
}
public static CriTableReader Create(byte[] sourceByteArray)
{
Stream source = new MemoryStream(sourceByteArray);
return Create(source);
}
public static CriTableReader Create(string sourceFileName)
{
Stream source = File.OpenRead(sourceFileName);
return Create(source);
}
public static CriTableReader Create(Stream source)
{
return Create(source, false);
}
public static CriTableReader Create(Stream source, bool leaveOpen)
{
return new CriTableReader(source, leaveOpen);
}
private CriTableReader(Stream source, bool leaveOpen)
{
this.source = source;
header = new CriTableHeader();
2016-12-31 13:56:10 +01:00
fields = new List<CriTableField>();
2016-11-12 17:02:48 +01:00
this.leaveOpen = leaveOpen;
ReadTable();
}
}
}