implemented *.pk archives and *.grx images.

This commit is contained in:
morkt 2015-07-16 21:44:20 +04:00
parent 2b59a488c4
commit 4f86894f9f
4 changed files with 391 additions and 2 deletions

View File

@ -119,6 +119,7 @@
<Compile Include="ArcPAC.cs" />
<Compile Include="ArcPCK.cs" />
<Compile Include="ArcPD.cs" />
<Compile Include="ArcPK.cs" />
<Compile Include="ArcQLIE.cs" />
<Compile Include="ArcRPA.cs" />
<Compile Include="ArcS25.cs" />
@ -199,6 +200,7 @@
<Compile Include="ImageGGP.cs" />
<Compile Include="ImageGR.cs" />
<Compile Include="ImageGRP.cs" />
<Compile Include="ImageGRX.cs" />
<Compile Include="ImageGS.cs" />
<Compile Include="ImageHG3.cs" />
<Compile Include="ImageIAF.cs" />

82
ArcFormats/ArcPK.cs Normal file
View File

@ -0,0 +1,82 @@
//! \file ArcPK.cs
//! \date Wed Jul 15 00:19:23 2015
//! \brief U-Me soft resource archives.
//
// Copyright (C) 2015 by morkt
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using GameRes.Utility;
namespace GameRes.Formats.UMeSoft
{
[Export(typeof(ArchiveFormat))]
public class PkOpener : ArchiveFormat
{
public override string Tag { get { return "PK"; } }
public override string Description { get { return "U-Me Soft resources archive"; } }
public override uint Signature { get { return 0; } }
public override bool IsHierarchic { get { return false; } }
public override bool CanCreate { get { return false; } }
public PkOpener ()
{
Extensions = new string[] { "pk", "gpk", "tpk", "wpk", "mpk" };
}
public override ArcFile TryOpen (ArcView file)
{
long index_end = file.MaxOffset - 4;
uint index_size = file.View.ReadUInt32 (index_end);
if (0 == index_size || index_size >= index_end)
return null;
long index_offset = index_end - index_size;
if (index_size > file.View.Reserve (index_offset, index_size))
return null;
var dir = new List<Entry>();
while (index_offset < index_end)
{
uint name_len = file.View.ReadByte (index_offset++);
if (0 == name_len)
break;
if (name_len+14 > index_end-index_offset)
return null;
string name = file.View.ReadString (index_offset, name_len);
index_offset += name_len+6;
var entry = FormatCatalog.Instance.CreateEntry (name);
entry.Size = file.View.ReadUInt32 (index_offset);
entry.Offset = file.View.ReadUInt32 (index_offset+4);
if (!entry.CheckPlacement (index_offset))
return null;
dir.Add (entry);
index_offset += 8;
}
if (0 == dir.Count)
return null;
return new ArcFile (file, this, dir);
}
}
}

298
ArcFormats/ImageGRX.cs Normal file
View File

@ -0,0 +1,298 @@
//! \file ImageGRX.cs
//! \date Wed Jul 15 00:59:44 2015
//! \brief U-Me Soft image format.
//
// Copyright (C) 2015 by morkt
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//
using System;
using System.ComponentModel.Composition;
using System.IO;
using System.Windows.Media;
using GameRes.Utility;
namespace GameRes.Formats.UMeSoft
{
internal class GrxMetaData : ImageMetaData
{
public bool IsPacked;
public bool HasAlpha;
public int AlphaOffset;
}
[Export(typeof(ImageFormat))]
public class GrxFormat : ImageFormat
{
public override string Tag { get { return "GRX"; } }
public override string Description { get { return "U-Me Soft image format"; } }
public override uint Signature { get { return 0x1A585247; } } // 'GRX'
public override ImageMetaData ReadMetaData (Stream stream)
{
byte[] header = new byte[0x10];
if (header.Length != stream.Read (header, 0, header.Length))
return null;
return new GrxMetaData
{
Width = LittleEndian.ToUInt16 (header, 8),
Height = LittleEndian.ToUInt16 (header, 10),
BPP = LittleEndian.ToUInt16 (header, 6),
IsPacked = 0 != header[4],
HasAlpha = 0 != header[5],
AlphaOffset = LittleEndian.ToInt32 (header, 12),
};
}
public override ImageData Read (Stream stream, ImageMetaData info)
{
var meta = info as GrxMetaData;
if (null == meta)
throw new ArgumentException ("GrxFormat.Read should be supplied with GrxMetaData", "info");
var reader = new Reader (stream, meta);
reader.Unpack();
return ImageData.Create (info, reader.Format, null, reader.Data, reader.Stride);
}
public override void Write (Stream file, ImageData image)
{
throw new System.NotImplementedException ("GrxFormat.Write not implemented");
}
internal class Reader
{
Stream m_input;
byte[] m_output;
GrxMetaData m_info;
int m_pixel_size;
uint m_aligned_width;
public byte[] Data { get { return m_output; } }
public PixelFormat Format { get; private set; }
public int Stride { get; private set; }
public Reader (Stream input, GrxMetaData info)
{
m_input = input;
m_info = info;
m_input.Seek (0x10, SeekOrigin.Current);
m_pixel_size = m_info.BPP / 8;
switch (m_info.BPP)
{
case 32:
Format = PixelFormats.Bgr32;
break;
case 24:
Format = PixelFormats.Bgr32;
m_pixel_size = 4;
break;
case 16:
Format = PixelFormats.Bgr565;
break;
case 8:
Format = PixelFormats.Gray8;
break;
default:
throw new InvalidFormatException();
}
m_aligned_width = (m_info.Width + 3u) & ~3u;
Stride = (int)(m_aligned_width * m_pixel_size);
m_output = new byte[Stride * (int)info.Height];
}
public void Unpack ()
{
if (!m_info.IsPacked)
m_input.Read (m_output, 0, m_output.Length);
else
UnpackColorData (m_output, m_info.BPP/8, m_pixel_size);
if (m_info.BPP >= 24 && m_info.HasAlpha && m_info.AlphaOffset > 0)
{
Format = PixelFormats.Bgra32;
m_input.Position = m_info.AlphaOffset;
var alpha = new byte[m_aligned_width * m_info.Height];
UnpackColorData (alpha, 1, 1);
int dst = 3;
int src = 0;
for (uint y = 0; y < m_info.Height; ++y)
{
for (uint x = 0; x < m_aligned_width; ++x)
{
m_output[dst] = alpha[src++];
dst += 4;
}
}
}
}
static readonly int[,] OffsetTable = new int[2,16] {
{ 0, -1, -1, -1, 0, -2, -2, -2, 0, -4, -4, -4, -2, -2, -4, -4 },
{ 0, 0, -1, 1, -2, 0, -2, 2, -4, 0, -4, 4, -4, 4, -2, 2 },
};
void UnpackColorData (byte[] output, int src_pixel_size, int dst_pixel_size)
{
int[] offset_step = new int[16];
int stride = ((int)m_info.Width * dst_pixel_size + 3) & ~3;
int delta = stride - (int)m_info.Width * dst_pixel_size;
for (int i = 0; i < 16; i++)
offset_step[i] = OffsetTable[0,i] * stride + OffsetTable[1,i] * dst_pixel_size;
int dst = 0;
for (uint y = 0; y < m_info.Height; ++y)
{
int w = (int)m_info.Width;
while (w > 0)
{
int flag = m_input.ReadByte();
if (-1 == flag)
throw new InvalidFormatException();
int count = flag & 3;
if (0 != (flag & 4))
{
count |= m_input.ReadByte() << 2;
}
w -= ++count;
if (0 == (flag & 0xF0))
{
if (0 != (flag & 8))
{
if (src_pixel_size == dst_pixel_size)
{
count *= dst_pixel_size;
if (count != m_input.Read (output, dst, count))
throw new InvalidFormatException();
dst += count;
}
else
{
for (int i = 0; i < count; ++i)
{
if (src_pixel_size != m_input.Read (output, dst, src_pixel_size))
throw new InvalidFormatException();
dst += dst_pixel_size;
}
}
}
else
{
if (src_pixel_size != m_input.Read (output, dst, src_pixel_size))
throw new InvalidFormatException();
--count;
dst += dst_pixel_size;
for (int i = count*dst_pixel_size; i > 0; i--)
{
output[dst] = output[dst-dst_pixel_size];
dst++;
}
}
}
else
{
int src = dst + offset_step[flag >> 4];
if (0 == (flag & 8))
{
for (int i = 0; i < count; i++)
{
for (int j = 0; j < src_pixel_size; ++j)
output[dst+j] = output[src+j];
dst += dst_pixel_size;
}
}
else
{
count *= dst_pixel_size;
Binary.CopyOverlapped (output, src, dst, count);
dst += count;
}
}
}
dst += delta;
}
}
}
}
// SGX header format
// signature 32bit
// GRX offset 32bit
// number of frames 32bit
// ... frames info (* number of frames)
// x coordinate 16bit
// y coordinate 16bit
// frame width 16bit
// frame height 16bit
// transparency color 32bit
internal class SgxMetaData : ImageMetaData
{
public int GrxOffset;
public ImageMetaData GrxInfo;
}
[Export(typeof(ImageFormat))]
public class SgxFormat : GrxFormat
{
public override string Tag { get { return "SGX"; } }
public override string Description { get { return "U-Me Soft multi-frame image format"; } }
public override uint Signature { get { return 0x1A584753; } } // 'SGX'
public SgxFormat ()
{
Extensions = new string[] { "grx" };
}
public override ImageMetaData ReadMetaData (Stream stream)
{
using (var reader = new ArcView.Reader (stream))
{
stream.Seek (4, SeekOrigin.Current);
int offset = reader.ReadInt32();
if (offset <= 8)
return null;
stream.Position = offset;
uint signature = reader.ReadUInt32();
if (signature != base.Signature)
return null;
stream.Seek (-4, SeekOrigin.Current);
var info = base.ReadMetaData (stream) as GrxMetaData;
if (null == info)
return null;
if (info.AlphaOffset > 0)
info.AlphaOffset += offset;
return new SgxMetaData { GrxOffset = offset, GrxInfo = info };
}
}
public override ImageData Read (Stream stream, ImageMetaData info)
{
var meta = info as SgxMetaData;
if (null == meta)
throw new ArgumentException ("SgxFormat.Read should be supplied with SgxMetaData", "info");
stream.Position = meta.GrxOffset;
return base.Read (stream, meta.GrxInfo);
}
}
}

View File

@ -126,11 +126,14 @@ Swan Song<br/>
<tr class="odd"><td>*.dat</td><td>-</td><td>No</td><td rowspan="2">Black Rainbow</td><td rowspan="2">Saiminjutsu</td></tr>
<tr class="odd"><td>*.bmd</td><td><tt>_BMD</tt></td><td>Yes</td></tr>
<tr><td>*.dat</td><td>-</td><td>Yes</td><td>Studio e.go!</td><td>Men at Work 2</td></tr>
<tr class="odd"><td>*.mbl</td><td>-</td><td>No</td><td rowspan="2">Marble</td><td rowspan="2">
Ikusa Otome Valkyrie<br/>
<tr class="odd"><td>*.mbl</td><td>-</td><td>No</td><td rowspan="3">Marble</td><td rowspan="3">
Chikatetsu Fuusa Jiken<br/>
Eien no Owari ni<br/>
Ikusa Otome Valkyrie<br/>
Sakura Machizaka Stories vol.1<br/>
</td></tr>
<tr class="odd"><td>*.prs</td><td><tt>YB</tt></td><td>No</td></tr>
<tr class="odd"><td>*.way</td><td><tt>WADY</tt></td><td>No</td></tr>
<tr><td>*.dat</td><td>-</td><td>No</td><td rowspan="2">M no Violet</td><td rowspan="2">Nanase Ren</td></tr>
<tr><td>*</td><td><tt>gra</tt><br/><tt>mas</tt></td><td>No</td></tr>
<tr class="odd"><td>*.ald</td><td>-</td><td>No</td><td rowspan="2">Alice Soft</td><td rowspan="2">Tsuma Shibori</td></tr>
@ -250,6 +253,10 @@ Jokei Kazoku ~Inbou~<br/>
<tr><td>*.mrg</td><td><tt>MRG</tt></td><td>No</td><td rowspan="3">F&amp;C</td><td rowspan="3">Konata yori Kanata made</td></tr>
<tr><td>*.mcg</td><td><tt>MCG 2.00</tt></td><td>No</td></tr>
<tr><td>*.acd</td><td><tt>ACD 1.00</tt></td><td>No</td></tr>
<tr class="odd"><td>*.pk<br/>*.gpk<br/>*.tpk<br/>*.wpk<br/></td><td>-</td><td>No</td><td rowspan="2">U-Me Soft</td><td rowspan="2">
Fetish 2: Omote no Kioku/Ura no Kioku<br/>
</td></tr>
<tr class="odd"><td>*.grx</td><td><tt>GRX\x1a</tt><br/><tt>SGX\x1a</tt></td><td>No</td></tr>
</table>
<p><a name="note-1">[1]</a> Non-encrypted only</p>
</body>