mirror of
https://github.com/crskycode/GARbro.git
synced 2026-07-08 01:30:18 +08:00
reorganized project directory structure.
This commit is contained in:
115
ArcFormats/BlackRainbow/ArcGSP.cs
Normal file
115
ArcFormats/BlackRainbow/ArcGSP.cs
Normal file
@@ -0,0 +1,115 @@
|
||||
//! \file ArcGSP.cs
|
||||
//! \date Wed Mar 04 11:30:32 2015
|
||||
//! \brief GSP archive implementation.
|
||||
//
|
||||
// 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.BlackRainbow
|
||||
{
|
||||
[Export(typeof(ArchiveFormat))]
|
||||
public class GspOpener : ArchiveFormat
|
||||
{
|
||||
public override string Tag { get { return "GSP"; } }
|
||||
public override string Description { get { return "GSP resource archive"; } }
|
||||
public override uint Signature { get { return 0; } }
|
||||
public override bool IsHierarchic { get { return false; } }
|
||||
public override bool CanCreate { get { return false; } }
|
||||
|
||||
public override ArcFile TryOpen (ArcView file)
|
||||
{
|
||||
int count = file.View.ReadInt32 (0);
|
||||
if (count <= 0 || count > 0x1ffff)
|
||||
return null;
|
||||
uint index_size = 0x40u * (uint)count;
|
||||
if (index_size > file.View.Reserve (4, index_size))
|
||||
return null;
|
||||
var dir = new List<Entry> (count);
|
||||
long index_offset = 4;
|
||||
long data_offset = index_offset + index_size;
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
string name = file.View.ReadString (index_offset+8, 0x38);
|
||||
var entry = FormatCatalog.Instance.CreateEntry (name);
|
||||
entry.Offset = file.View.ReadUInt32 (index_offset);
|
||||
entry.Size = file.View.ReadUInt32 (index_offset+4);
|
||||
if (entry.Offset < data_offset || !entry.CheckPlacement (file.MaxOffset))
|
||||
return null;
|
||||
dir.Add (entry);
|
||||
index_offset += 0x40;
|
||||
}
|
||||
return new ArcFile (file, this, dir);
|
||||
}
|
||||
}
|
||||
|
||||
[Export(typeof(ArchiveFormat))]
|
||||
public class DatOpener : ArchiveFormat
|
||||
{
|
||||
public override string Tag { get { return "DAT/BR"; } }
|
||||
public override string Description { get { return "BlackRainbow resource archive"; } }
|
||||
public override uint Signature { get { return 0; } }
|
||||
public override bool IsHierarchic { get { return false; } }
|
||||
public override bool CanCreate { get { return false; } }
|
||||
|
||||
public DatOpener ()
|
||||
{
|
||||
Extensions = new string[] { "dat", "pak" };
|
||||
Signatures = new uint[] { 2u, 4u, 5u };
|
||||
}
|
||||
|
||||
public override ArcFile TryOpen (ArcView file)
|
||||
{
|
||||
int count = file.View.ReadInt32 (8);
|
||||
if (count <= 0 || count > 0x1ffff)
|
||||
return null;
|
||||
uint base_offset = file.View.ReadUInt32 (0x0c);
|
||||
uint index_offset = 0x10;
|
||||
uint index_size = 4u * (uint)count;
|
||||
if (index_size > file.View.Reserve (index_offset, index_size))
|
||||
return null;
|
||||
var index = new List<uint> (count);
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
uint offset = file.View.ReadUInt32 (index_offset);
|
||||
if (offset != 0xffffffff)
|
||||
index.Add (base_offset + offset);
|
||||
index_offset += 4;
|
||||
}
|
||||
var dir = new List<Entry> (index.Count);
|
||||
for (int i = 0; i < index.Count; ++i)
|
||||
{
|
||||
long offset = index[i];
|
||||
string name = file.View.ReadString (offset, 0x20);
|
||||
var entry = FormatCatalog.Instance.CreateEntry (name);
|
||||
entry.Offset = offset + 0x24;
|
||||
entry.Size = file.View.ReadUInt32 (offset+0x20);
|
||||
dir.Add (entry);
|
||||
}
|
||||
return new ArcFile (file, this, dir);
|
||||
}
|
||||
}
|
||||
}
|
||||
279
ArcFormats/BlackRainbow/ImageBMD.cs
Normal file
279
ArcFormats/BlackRainbow/ImageBMD.cs
Normal file
@@ -0,0 +1,279 @@
|
||||
//! \file ImageBMD.cs
|
||||
//! \date Wed Mar 25 09:35:06 2015
|
||||
//! \brief Black Rainbow BMD image format implementation.
|
||||
//
|
||||
// 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.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using GameRes.Compression;
|
||||
using GameRes.Utility;
|
||||
|
||||
namespace GameRes.Formats.BlackRainbow
|
||||
{
|
||||
internal class BmdMetaData : ImageMetaData
|
||||
{
|
||||
public uint PackedSize;
|
||||
public int Flag;
|
||||
}
|
||||
|
||||
[Export(typeof(ImageFormat))]
|
||||
public class BmdFormat : ImageFormat
|
||||
{
|
||||
public override string Tag { get { return "BMD"; } }
|
||||
public override string Description { get { return "Black Rainbow bitmap format"; } }
|
||||
public override uint Signature { get { return 0x444d425fu; } } // '_BMD'
|
||||
|
||||
public override ImageMetaData ReadMetaData (Stream stream)
|
||||
{
|
||||
var header = new byte[0x14];
|
||||
if (header.Length != stream.Read (header, 0, header.Length))
|
||||
return null;
|
||||
|
||||
return new BmdMetaData
|
||||
{
|
||||
Width = LittleEndian.ToUInt32 (header, 8),
|
||||
Height = LittleEndian.ToUInt32 (header, 12),
|
||||
BPP = 32,
|
||||
PackedSize = LittleEndian.ToUInt32 (header, 4),
|
||||
Flag = LittleEndian.ToInt32 (header, 0x10),
|
||||
};
|
||||
}
|
||||
|
||||
public override ImageData Read (Stream stream, ImageMetaData info)
|
||||
{
|
||||
var meta = info as BmdMetaData;
|
||||
if (null == meta)
|
||||
throw new ArgumentException ("BmdFormat.Read should be supplied with BmdMetaData", "info");
|
||||
|
||||
stream.Position = 0x14;
|
||||
int image_size = (int)(meta.Width*meta.Height*4);
|
||||
using (var reader = new LzssReader (stream, (int)meta.PackedSize, image_size))
|
||||
{
|
||||
PixelFormat format = meta.Flag != 0 ? PixelFormats.Bgra32 : PixelFormats.Bgr32;
|
||||
reader.Unpack();
|
||||
byte[] pixels = reader.Data;
|
||||
var bitmap = BitmapSource.Create ((int)meta.Width, (int)meta.Height, 96, 96,
|
||||
format, null, pixels, (int)meta.Width*4);
|
||||
bitmap.Freeze();
|
||||
return new ImageData (bitmap, meta);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Write (Stream file, ImageData image)
|
||||
{
|
||||
using (var output = new BinaryWriter (file, Encoding.ASCII, true))
|
||||
using (var writer = new Writer (image.Bitmap))
|
||||
{
|
||||
writer.Pack();
|
||||
output.Write (Signature);
|
||||
output.Write (writer.Size);
|
||||
output.Write (image.Width);
|
||||
output.Write (image.Height);
|
||||
output.Write (writer.HasAlpha ? 1 : 0);
|
||||
output.Write (writer.Data, 0, (int)writer.Size);
|
||||
}
|
||||
}
|
||||
|
||||
internal class Writer : IDisposable
|
||||
{
|
||||
const int MinChunkSize = 3;
|
||||
const int MaxChunkSize = MinChunkSize+0xf;
|
||||
const int FrameSize = 0x1000;
|
||||
|
||||
byte[] m_input;
|
||||
MemoryStream m_output;
|
||||
bool m_has_alpha = false;
|
||||
byte[] m_frame = new byte[FrameSize];
|
||||
|
||||
public byte[] Data { get { return m_output.GetBuffer(); } }
|
||||
public uint Size { get { return (uint)m_output.Length; } }
|
||||
public bool HasAlpha { get { return m_has_alpha; } }
|
||||
|
||||
public Writer (BitmapSource bitmap)
|
||||
{
|
||||
if (bitmap.Format != PixelFormats.Bgra32)
|
||||
bitmap = new FormatConvertedBitmap (bitmap, PixelFormats.Bgra32, null, 0);
|
||||
|
||||
m_input = new byte[bitmap.PixelWidth*bitmap.PixelHeight*4];
|
||||
bitmap.CopyPixels (m_input, bitmap.PixelWidth*4, 0);
|
||||
for (int i = 3; i < m_input.Length; i += 4)
|
||||
{
|
||||
if (0xff != m_input[i])
|
||||
{
|
||||
m_has_alpha = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!m_has_alpha)
|
||||
for (int i = 3; i < m_input.Length; i += 4)
|
||||
m_input[i] = 0;
|
||||
m_output = new MemoryStream();
|
||||
}
|
||||
|
||||
public void Pack ()
|
||||
{
|
||||
int frame_pos = 0x1000 - 18;
|
||||
int src = 0;
|
||||
while (src < m_input.Length)
|
||||
{
|
||||
int chunk_size;
|
||||
int offset = FindChunk (src, out chunk_size);
|
||||
if (-1 == offset)
|
||||
{
|
||||
PutByte (m_input[src]);
|
||||
chunk_size = 1;
|
||||
}
|
||||
else
|
||||
PutChunk (offset, chunk_size);
|
||||
for (int i = 0; i < chunk_size; ++i)
|
||||
{
|
||||
m_frame[frame_pos++] = m_input[src++];
|
||||
frame_pos &= 0xfff;
|
||||
}
|
||||
}
|
||||
Flush();
|
||||
}
|
||||
|
||||
struct Chunk
|
||||
{
|
||||
public short Offset;
|
||||
public byte Data;
|
||||
|
||||
public Chunk (byte b)
|
||||
{
|
||||
Offset = -1;
|
||||
Data = b;
|
||||
}
|
||||
|
||||
public Chunk (int offset, int count)
|
||||
{
|
||||
Debug.Assert (offset < 0x1000 && count >= MinChunkSize && count <= MaxChunkSize);
|
||||
Offset = (short)offset;
|
||||
Data = (byte)((count - MinChunkSize) & 0x0f);
|
||||
}
|
||||
}
|
||||
|
||||
List<Chunk> m_queue = new List<Chunk> (8);
|
||||
|
||||
void PutByte (byte b)
|
||||
{
|
||||
m_queue.Add (new Chunk (b));
|
||||
if (8 == m_queue.Count)
|
||||
Flush();
|
||||
}
|
||||
|
||||
void PutChunk (int offset, int size)
|
||||
{
|
||||
m_queue.Add (new Chunk (offset, size));
|
||||
if (8 == m_queue.Count)
|
||||
Flush();
|
||||
}
|
||||
|
||||
void Flush ()
|
||||
{
|
||||
if (0 == m_queue.Count)
|
||||
return;
|
||||
int ctl = 0;
|
||||
int bit = 1;
|
||||
for (int i = 0; i < m_queue.Count; ++i)
|
||||
{
|
||||
if (m_queue[i].Offset < 0)
|
||||
ctl |= bit;
|
||||
bit <<= 1;
|
||||
}
|
||||
m_output.WriteByte ((byte)ctl);
|
||||
for (int i = 0; i < m_queue.Count; ++i)
|
||||
{
|
||||
var chunk = m_queue[i];
|
||||
if (chunk.Offset >= 0)
|
||||
{
|
||||
byte lo = (byte)(chunk.Offset & 0xff);
|
||||
byte hi = (byte)((chunk.Offset & 0xf00) >> 4);
|
||||
hi |= chunk.Data;
|
||||
m_output.WriteByte (lo);
|
||||
m_output.WriteByte (hi);
|
||||
}
|
||||
else
|
||||
m_output.WriteByte (chunk.Data);
|
||||
}
|
||||
m_queue.Clear();
|
||||
}
|
||||
|
||||
private int FindChunk (int pos, out int size)
|
||||
{
|
||||
size = 0;
|
||||
int chunk_limit = Math.Min (MaxChunkSize, m_input.Length-pos);
|
||||
if (chunk_limit < MinChunkSize)
|
||||
return -1;
|
||||
int offset = -1;
|
||||
for (int i = 0; i < m_frame.Length; )
|
||||
{
|
||||
int first = Array.IndexOf (m_frame, m_input[pos], i);
|
||||
if (-1 == first)
|
||||
break;
|
||||
int j = 1;
|
||||
while (j < chunk_limit && m_frame[(first+j)&0xfff] == m_input[pos+j])
|
||||
++j;
|
||||
if (j > size && j >= MinChunkSize)
|
||||
{
|
||||
offset = first;
|
||||
size = j;
|
||||
if (chunk_limit == j)
|
||||
break;
|
||||
}
|
||||
i = first + 1;
|
||||
}
|
||||
return offset;
|
||||
}
|
||||
|
||||
#region IDisposable Members
|
||||
bool disposed = false;
|
||||
|
||||
public void Dispose ()
|
||||
{
|
||||
Dispose (true);
|
||||
GC.SuppressFinalize (this);
|
||||
}
|
||||
|
||||
protected virtual void Dispose (bool disposing)
|
||||
{
|
||||
if (!disposed)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
m_output.Dispose();
|
||||
}
|
||||
disposed = true;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
73
ArcFormats/BlackRainbow/ImageBMZ.cs
Normal file
73
ArcFormats/BlackRainbow/ImageBMZ.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
//! \file ImageBMZ.cs
|
||||
//! \date Wed Mar 04 11:48:31 2015
|
||||
//! \brief Compressed bitmap 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.ComponentModel.Composition;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using GameRes.Utility;
|
||||
using GameRes.Compression;
|
||||
|
||||
namespace GameRes.Formats.BlackRainbow
|
||||
{
|
||||
[Export(typeof(ImageFormat))]
|
||||
public class BmzFormat : BmpFormat
|
||||
{
|
||||
public override string Tag { get { return "BMZ"; } }
|
||||
public override string Description { get { return "Compressed bitmap format"; } }
|
||||
public override uint Signature { get { return 0x33434c5au; } } // 'ZLC3'
|
||||
|
||||
public override void Write (Stream file, ImageData image)
|
||||
{
|
||||
using (var bmp = new MemoryStream())
|
||||
{
|
||||
base.Write (bmp, image);
|
||||
using (var output = new BinaryWriter (file, Encoding.ASCII, true))
|
||||
{
|
||||
output.Write (Signature);
|
||||
output.Write ((uint)bmp.Length);
|
||||
}
|
||||
bmp.Position = 0;
|
||||
using (var zstream = new ZLibStream (file, CompressionMode.Compress, CompressionLevel.Level9, true))
|
||||
bmp.CopyTo (zstream);
|
||||
}
|
||||
}
|
||||
|
||||
public override ImageMetaData ReadMetaData (Stream stream)
|
||||
{
|
||||
var header = new byte[8];
|
||||
if (header.Length != stream.Read (header, 0, header.Length))
|
||||
return null;
|
||||
using (var zstream = new ZLibStream (stream, CompressionMode.Decompress, true))
|
||||
return base.ReadMetaData (zstream);
|
||||
}
|
||||
|
||||
public override ImageData Read (Stream stream, ImageMetaData info)
|
||||
{
|
||||
stream.Seek (8, SeekOrigin.Current);
|
||||
using (var zstream = new ZLibStream (stream, CompressionMode.Decompress, true))
|
||||
return base.Read (zstream, info);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user