2015-11-09 04:14:37 +04:00
|
|
|
//! \file ArcDET.cs
|
|
|
|
//! \date Mon Nov 09 00:16:53 2015
|
|
|
|
//! \brief μ-GameOperationSystem resource archive.
|
|
|
|
//
|
2018-12-19 03:24:28 +04:00
|
|
|
// Copyright (C) 2015-2018 by morkt
|
2015-11-09 04:14:37 +04:00
|
|
|
//
|
|
|
|
// 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;
|
2018-12-19 03:24:28 +04:00
|
|
|
using GameRes.Compression;
|
2015-11-09 04:14:37 +04:00
|
|
|
using GameRes.Utility;
|
|
|
|
|
|
|
|
namespace GameRes.Formats.uGOS
|
|
|
|
{
|
|
|
|
[Export(typeof(ArchiveFormat))]
|
2018-12-19 03:24:28 +04:00
|
|
|
public class DetOpener : ArchiveFormat
|
2015-11-09 04:14:37 +04:00
|
|
|
{
|
|
|
|
public override string Tag { get { return "DET"; } }
|
|
|
|
public override string Description { get { return "μ-GameOperationSystem resource archive"; } }
|
|
|
|
public override uint Signature { get { return 0; } }
|
2015-12-15 00:48:21 +04:00
|
|
|
public override bool IsHierarchic { get { return true; } }
|
2016-10-11 00:05:22 +04:00
|
|
|
public override bool CanWrite { get { return false; } }
|
2015-11-09 04:14:37 +04:00
|
|
|
|
|
|
|
public override ArcFile TryOpen (ArcView file)
|
|
|
|
{
|
2017-04-10 23:15:13 +04:00
|
|
|
if (!file.Name.HasExtension (".det"))
|
2015-11-09 04:14:37 +04:00
|
|
|
return null;
|
|
|
|
var name_file = Path.ChangeExtension (file.Name, "nme");
|
|
|
|
var index_file = Path.ChangeExtension (file.Name, "atm");
|
|
|
|
if (!VFS.FileExists (name_file) || !VFS.FileExists (index_file))
|
|
|
|
return null;
|
|
|
|
using (var nme = VFS.OpenView (name_file))
|
|
|
|
using (var idx = VFS.OpenView (index_file))
|
|
|
|
{
|
2018-12-19 03:24:28 +04:00
|
|
|
var reader = new DetIndexReader (file, name_file: nme, index_file: idx);
|
|
|
|
var dir = reader.ReadIndex (0x10);
|
|
|
|
if (null == dir)
|
|
|
|
dir = reader.ReadIndex (0x14);
|
|
|
|
if (null == dir)
|
2015-11-09 04:14:37 +04:00
|
|
|
return null;
|
|
|
|
return new ArcFile (file, this, dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override Stream OpenEntry (ArcFile arc, Entry entry)
|
|
|
|
{
|
|
|
|
var pent = entry as PackedEntry;
|
|
|
|
if (null == pent || !pent.IsPacked)
|
|
|
|
return base.OpenEntry (arc, entry);
|
2018-12-19 03:24:28 +04:00
|
|
|
var input = arc.File.CreateStream (entry.Offset, entry.Size);
|
|
|
|
return new PackedStream<RleDecompressor> (input);
|
2015-11-09 04:14:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Unpack (Stream input, byte[] output)
|
|
|
|
{
|
|
|
|
int dst = 0;
|
|
|
|
while (dst < output.Length)
|
|
|
|
{
|
|
|
|
int ctl = input.ReadByte();
|
|
|
|
if (-1 == ctl)
|
|
|
|
return false;
|
|
|
|
if (0xFF != ctl)
|
|
|
|
{
|
|
|
|
output[dst++] = (byte)ctl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ctl = input.ReadByte();
|
|
|
|
if (-1 == ctl)
|
|
|
|
return false;
|
|
|
|
if (0xFF == ctl)
|
|
|
|
{
|
|
|
|
output[dst++] = 0xFF;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int offset = (ctl >> 2) + 1;
|
|
|
|
int count = (ctl & 3) + 3;
|
|
|
|
Binary.CopyOverlapped (output, dst-offset, dst, count);
|
|
|
|
dst += count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2018-12-19 03:24:28 +04:00
|
|
|
|
|
|
|
internal sealed class DetIndexReader
|
|
|
|
{
|
|
|
|
ArcView m_arc;
|
|
|
|
ArcView m_index;
|
|
|
|
byte[] m_names;
|
|
|
|
List<Entry> m_dir;
|
|
|
|
|
|
|
|
public DetIndexReader (ArcView arc_file, ArcView name_file, ArcView index_file)
|
|
|
|
{
|
|
|
|
m_arc = arc_file;
|
|
|
|
m_names = name_file.View.ReadBytes (0, (uint)name_file.MaxOffset);
|
|
|
|
m_index = index_file;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<Entry> ReadIndex (uint entry_size)
|
|
|
|
{
|
|
|
|
int count = (int)(m_index.MaxOffset / entry_size);
|
|
|
|
if (!ArchiveFormat.IsSaneCount (count))
|
|
|
|
return null;
|
|
|
|
if (null == m_dir)
|
|
|
|
m_dir = new List<Entry> (count);
|
|
|
|
else
|
|
|
|
m_dir.Clear();
|
|
|
|
uint idx_offset = 0;
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
if (idx_offset + entry_size > m_index.MaxOffset)
|
|
|
|
return null;
|
|
|
|
int name_offset = m_index.View.ReadInt32 (idx_offset);
|
|
|
|
if (name_offset < 0 || name_offset >= m_names.Length)
|
|
|
|
return null;
|
|
|
|
var name = Binary.GetCString (m_names, name_offset, m_names.Length - name_offset);
|
|
|
|
var entry = FormatCatalog.Instance.Create<PackedEntry> (name);
|
|
|
|
entry.Offset = m_index.View.ReadUInt32 (idx_offset + 4);
|
|
|
|
entry.Size = m_index.View.ReadUInt32 (idx_offset + 8);
|
|
|
|
if (!entry.CheckPlacement (m_arc.MaxOffset))
|
|
|
|
return null;
|
|
|
|
entry.IsPacked = true;
|
|
|
|
if (entry_size >= 0x14)
|
|
|
|
entry.UnpackedSize = m_index.View.ReadUInt32 (idx_offset + 0x10);
|
|
|
|
if (name.EndsWith (".bmp.txt", StringComparison.OrdinalIgnoreCase))
|
|
|
|
entry.Type = "image";
|
|
|
|
m_dir.Add (entry);
|
|
|
|
idx_offset += entry_size;
|
|
|
|
}
|
|
|
|
return m_dir;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal sealed class RleDecompressor : Decompressor
|
|
|
|
{
|
|
|
|
Stream m_input;
|
|
|
|
|
|
|
|
public override void Initialize (Stream input)
|
|
|
|
{
|
|
|
|
m_input = input;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override IEnumerator<int> Unpack ()
|
|
|
|
{
|
|
|
|
var frame = new byte[0x100];
|
|
|
|
int frame_pos = 0;
|
|
|
|
const int frame_mask = 0xFF;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
int ctl = m_input.ReadByte();
|
|
|
|
if (-1 == ctl)
|
|
|
|
yield break;
|
|
|
|
if (0xFF != ctl)
|
|
|
|
{
|
|
|
|
m_buffer[m_pos++] = frame[frame_pos++ & frame_mask] = (byte)ctl;
|
|
|
|
if (0 == --m_length)
|
|
|
|
yield return m_pos;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ctl = m_input.ReadByte();
|
|
|
|
if (-1 == ctl)
|
|
|
|
yield break;
|
|
|
|
if (0xFF == ctl)
|
|
|
|
{
|
|
|
|
m_buffer[m_pos++] = frame[frame_pos++ & frame_mask] = 0xFF;
|
|
|
|
if (0 == --m_length)
|
|
|
|
yield return m_pos;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int offset = frame_pos - ((ctl >> 2) + 1);
|
|
|
|
int count = (ctl & 3) + 3;
|
|
|
|
while (count --> 0)
|
|
|
|
{
|
|
|
|
byte v = frame[offset++ & frame_mask];
|
|
|
|
m_buffer[m_pos++] = frame[frame_pos++ & frame_mask] = v;
|
|
|
|
if (0 == --m_length)
|
|
|
|
yield return m_pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-09 04:14:37 +04:00
|
|
|
}
|