(MGS): archive could contain non-WAV files too.

This commit is contained in:
morkt 2017-11-21 10:12:07 +04:00
parent 4967aae168
commit f5a847c966

View File

@ -95,6 +95,7 @@ namespace GameRes.Formats.Megu
public ushort Channels; public ushort Channels;
public uint SamplesPerSecond; public uint SamplesPerSecond;
public ushort BitsPerSample; public ushort BitsPerSample;
public byte Format;
} }
[Export(typeof(ArchiveFormat))] [Export(typeof(ArchiveFormat))]
@ -118,11 +119,9 @@ namespace GameRes.Formats.Megu
var dir = new List<Entry> (count); var dir = new List<Entry> (count);
int index_offset = 0x22; int index_offset = 0x22;
byte[] name_buf = new byte[16]; byte[] name_buf = new byte[16];
for (uint i = 0; i < count; ++i) for (int i = 0; i < count; ++i)
{ {
ushort channels = file.View.ReadUInt16 (index_offset+1); byte format = file.View.ReadByte (index_offset);
uint rate = file.View.ReadUInt32 (index_offset+3);
ushort bits = file.View.ReadUInt16 (index_offset+7);
int name_size = file.View.ReadByte (index_offset+9); int name_size = file.View.ReadByte (index_offset+9);
if (0 == name_size) if (0 == name_size)
return null; return null;
@ -131,18 +130,20 @@ namespace GameRes.Formats.Megu
file.View.Read (index_offset+10, name_buf, 0, (uint)name_size); file.View.Read (index_offset+10, name_buf, 0, (uint)name_size);
if (100 == flag) if (100 == flag)
MgdOpener.Decrypt (name_buf, 0, name_size); MgdOpener.Decrypt (name_buf, 0, name_size);
index_offset += 10 + name_size; var name = Encodings.cp932.GetString (name_buf, 0, name_size);
name = Path.ChangeExtension (name, GetExtFromFormatId (format));
var entry = new MgsEntry var entry = FormatCatalog.Instance.Create<MgsEntry> (name);
entry.Format = format;
if (0 == format)
{ {
Name = Encodings.cp932.GetString (name_buf, 0, name_size) + ".wav", entry.Channels = file.View.ReadUInt16 (index_offset+1);
Type = "audio", entry.SamplesPerSecond = file.View.ReadUInt32 (index_offset+3);
Size = file.View.ReadUInt32 (index_offset), entry.BitsPerSample = file.View.ReadUInt16 (index_offset+7);
Offset = file.View.ReadUInt32 (index_offset + 4), }
Channels = channels, index_offset += 10 + name_size;
SamplesPerSecond = rate, entry.Size = file.View.ReadUInt32 (index_offset);
BitsPerSample = bits, entry.Offset = file.View.ReadUInt32 (index_offset + 4);
};
if (!entry.CheckPlacement (file.MaxOffset)) if (!entry.CheckPlacement (file.MaxOffset))
return null; return null;
dir.Add (entry); dir.Add (entry);
@ -154,30 +155,32 @@ namespace GameRes.Formats.Megu
public override Stream OpenEntry (ArcFile arc, Entry entry) public override Stream OpenEntry (ArcFile arc, Entry entry)
{ {
var went = entry as MgsEntry; var went = entry as MgsEntry;
if (null == went) if (null == went || went.Format != 0)
return arc.File.CreateStream (entry.Offset, entry.Size); return arc.File.CreateStream (entry.Offset, entry.Size);
var riff = new byte[0x2c]; using (var riff = new MemoryStream (0x2C))
using (var buf = new MemoryStream (riff))
using (var wav = new BinaryWriter (buf))
{ {
wav.Write (0x46464952); // 'RIFF' ushort align = (ushort)(went.Channels * went.BitsPerSample / 8);
uint total_size = 0x2c - 8 + entry.Size; var format = new WaveFormat {
wav.Write (total_size); FormatTag = 1,
wav.Write (0x45564157); // 'WAVE' Channels = went.Channels,
wav.Write (0x20746d66); // 'fmt ' SamplesPerSecond = went.SamplesPerSecond,
wav.Write (0x10); AverageBytesPerSecond = went.SamplesPerSecond * align,
wav.Write ((ushort)1); BlockAlign = align,
wav.Write (went.Channels); BitsPerSample = went.BitsPerSample,
wav.Write (went.SamplesPerSecond); };
uint bps = went.SamplesPerSecond * went.Channels * went.BitsPerSample / 8; WaveAudio.WriteRiffHeader (riff, format, entry.Size);
wav.Write (bps);
wav.Write ((ushort)2);
wav.Write (went.BitsPerSample);
wav.Write (0x61746164); // 'data'
wav.Write (went.Size);
wav.Flush ();
var input = arc.File.CreateStream (entry.Offset, entry.Size); var input = arc.File.CreateStream (entry.Offset, entry.Size);
return new PrefixStream (riff, input); return new PrefixStream (riff.ToArray(), input);
}
}
internal static string GetExtFromFormatId (int id)
{
switch (id)
{
case 0: return "wav";
case 1: return "mid";
default: return null;
} }
} }
} }