mirror of
https://github.com/crskycode/GARbro.git
synced 2026-06-06 13:48:57 +08:00
Compare commits
12 Commits
d487074bbe
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2f752b84ac | ||
|
|
331af3a70c | ||
|
|
78cea04222 | ||
|
|
012b53df11 | ||
|
|
cdecd03d1a | ||
|
|
446b9dff3e | ||
|
|
02e9dc6c5c | ||
|
|
ca1c92477d | ||
|
|
d24f20c952 | ||
|
|
0a6d053817 | ||
|
|
f556ca9637 | ||
|
|
be3bf61c82 |
@@ -176,6 +176,10 @@
|
||||
<Compile Include="aNCHOR\ArcFPD.cs" />
|
||||
<Compile Include="aNCHOR\AudioFCD.cs" />
|
||||
<Compile Include="aNCHOR\Blowfish.cs" />
|
||||
<Compile Include="CatSystem\ArcBinV1.cs" />
|
||||
<Compile Include="CatSystem\ArcPidaV1.cs" />
|
||||
<Compile Include="Entergram\ArcPacV1.cs" />
|
||||
<Compile Include="Entergram\QuickLZ.cs" />
|
||||
<Compile Include="FrontierWorks\ArcPCARC.cs" />
|
||||
<Compile Include="Artemis\ImageNekoPNG.cs" />
|
||||
<Compile Include="CsWare\AudioWAV.cs" />
|
||||
@@ -252,6 +256,7 @@
|
||||
<Compile Include="ArcASAR.cs" />
|
||||
<Compile Include="Artemis\ArcMJA.cs" />
|
||||
<Compile Include="Leaf\LeafVideo.cs" />
|
||||
<Compile Include="LightVN\ArcMCDAT.cs" />
|
||||
<Compile Include="Macintosh\ImagePICT.cs" />
|
||||
<Compile Include="Macromedia\ArcDXR.cs" />
|
||||
<Compile Include="Macromedia\AudioSND.cs" />
|
||||
|
||||
196
ArcFormats/CatSystem/ArcBinV1.cs
Normal file
196
ArcFormats/CatSystem/ArcBinV1.cs
Normal file
@@ -0,0 +1,196 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.Composition;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace GameRes.Formats.CatSystem
|
||||
{
|
||||
internal class BinEntryV1 : Entry
|
||||
{
|
||||
public long Size64 { get; set; }
|
||||
}
|
||||
|
||||
internal class BinStreamV1 : Stream
|
||||
{
|
||||
private Stream mBaseStream;
|
||||
private readonly long mOffset;
|
||||
private readonly long mLength;
|
||||
private long mPosition = 0L;
|
||||
private bool mDisposed = false;
|
||||
|
||||
public BinStreamV1(Stream stream, long offset, long length)
|
||||
{
|
||||
this.mBaseStream = stream;
|
||||
this.mOffset = offset;
|
||||
this.mLength = length;
|
||||
}
|
||||
|
||||
public override bool CanRead => !this.mDisposed;
|
||||
public override bool CanSeek => !this.mDisposed;
|
||||
public override bool CanWrite => false;
|
||||
public override long Length => this.mLength;
|
||||
public override long Position
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.mPosition;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
if (value > this.mLength)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
this.mPosition = value;
|
||||
}
|
||||
}
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
}
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
Stream stream = this.mBaseStream;
|
||||
|
||||
stream.Position = this.mOffset + this.mPosition;
|
||||
int bytesRead = stream.Read(buffer, offset, (int)Math.Min(this.mLength - this.mPosition, count));
|
||||
|
||||
this.Decrypt(buffer, offset, bytesRead, this.mOffset, this.mPosition);
|
||||
this.mPosition += bytesRead;
|
||||
|
||||
return bytesRead;
|
||||
}
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
long pos = 0L;
|
||||
switch (origin)
|
||||
{
|
||||
case SeekOrigin.Begin:
|
||||
{
|
||||
pos = offset;
|
||||
break;
|
||||
}
|
||||
case SeekOrigin.Current:
|
||||
{
|
||||
pos = this.mPosition + offset;
|
||||
break;
|
||||
}
|
||||
case SeekOrigin.End:
|
||||
{
|
||||
pos = this.mLength + offset;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (pos < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
if (pos > this.mLength)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
this.mPosition = pos;
|
||||
return pos;
|
||||
}
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (!this.mDisposed)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
this.mBaseStream.Dispose();
|
||||
this.mBaseStream = Stream.Null;
|
||||
}
|
||||
this.mDisposed = true;
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void Decrypt(byte[] buffer, long offset, int count, long fileOffset, long arcOffset)
|
||||
{
|
||||
for(int i = 0; i < count; ++i)
|
||||
{
|
||||
byte key = (byte)((fileOffset + arcOffset + i) * 0x9D + (arcOffset + i) * 0x773);
|
||||
buffer[offset + i] -= key;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Export(typeof(ArchiveFormat))]
|
||||
public class BinOpenerV1 : ArchiveFormat
|
||||
{
|
||||
public override string Tag => "BinV1/CSPACK";
|
||||
public override string Description => "CatSystem2 resource archive";
|
||||
public override uint Signature => 0x40674461u;
|
||||
public override bool IsHierarchic => true;
|
||||
public override bool CanWrite => false;
|
||||
|
||||
public override ArcFile TryOpen(ArcView file)
|
||||
{
|
||||
using (ArcViewStream stream = file.CreateStream())
|
||||
{
|
||||
using (BinaryReader br = new BinaryReader(stream, Encoding.Unicode, true))
|
||||
{
|
||||
stream.Position = 8L;
|
||||
|
||||
List<BinEntryV1> entries = new List<BinEntryV1>();
|
||||
{
|
||||
string fn = br.ReadString();
|
||||
while (!string.IsNullOrEmpty(fn))
|
||||
{
|
||||
BinEntryV1 e = Create<BinEntryV1>(fn);
|
||||
e.Offset = br.ReadUInt32();
|
||||
e.Size64 = 0L;
|
||||
|
||||
entries.Add(e);
|
||||
|
||||
fn = br.ReadString();
|
||||
}
|
||||
}
|
||||
|
||||
if (entries.Any())
|
||||
{
|
||||
{
|
||||
BinEntryV1 last = entries.Last();
|
||||
last.Size64 = stream.Length - last.Offset;
|
||||
last.Size = (uint)last.Size64;
|
||||
}
|
||||
for (int i = 0; i < entries.Count - 1; ++i)
|
||||
{
|
||||
BinEntryV1 curr = entries[i + 0];
|
||||
BinEntryV1 next = entries[i + 1];
|
||||
curr.Size64 = next.Offset - curr.Offset;
|
||||
curr.Size = (uint)curr.Size64;
|
||||
}
|
||||
}
|
||||
|
||||
return new ArcFile(file, this, entries.Cast<Entry>().ToList());
|
||||
}
|
||||
}
|
||||
}
|
||||
public override Stream OpenEntry(ArcFile arc, Entry entry)
|
||||
{
|
||||
if(!(entry is BinEntryV1 e))
|
||||
{
|
||||
return base.OpenEntry(arc, entry);
|
||||
}
|
||||
return new BinStreamV1(arc.File.CreateStream(), e.Offset, e.Size64);
|
||||
}
|
||||
}
|
||||
}
|
||||
86
ArcFormats/CatSystem/ArcPidaV1.cs
Normal file
86
ArcFormats/CatSystem/ArcPidaV1.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.Composition;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace GameRes.Formats.CatSystem
|
||||
{
|
||||
internal class PidaEntryV1 : Entry
|
||||
{
|
||||
public ushort Width;
|
||||
public ushort Height;
|
||||
public short OffsetX;
|
||||
public short OffsetY;
|
||||
}
|
||||
|
||||
[Export(typeof(ArchiveFormat))]
|
||||
public class PidaOpenerV1 : ArchiveFormat
|
||||
{
|
||||
public override string Tag => "PidaV1";
|
||||
public override string Description => "CatSystem2 engine multi-image";
|
||||
public override uint Signature => 0x6DF22373u;
|
||||
public override bool IsHierarchic => true;
|
||||
public override bool CanWrite => false;
|
||||
|
||||
public PidaOpenerV1()
|
||||
{
|
||||
ContainedFormats = new[] { "PNG" };
|
||||
}
|
||||
|
||||
public override ArcFile TryOpen(ArcView file)
|
||||
{
|
||||
using (ArcViewStream stream = file.CreateStream())
|
||||
{
|
||||
using (BinaryReader br = new BinaryReader(stream, Encoding.Unicode, true))
|
||||
{
|
||||
stream.Position = 8L;
|
||||
|
||||
List<PidaEntryV1> entries = new List<PidaEntryV1>();
|
||||
{
|
||||
string fn = br.ReadString();
|
||||
while (!string.IsNullOrEmpty(fn))
|
||||
{
|
||||
PidaEntryV1 e = Create<PidaEntryV1>(fn);
|
||||
e.Offset = br.ReadUInt32();
|
||||
e.Size = 0u;
|
||||
|
||||
e.Width = br.ReadUInt16();
|
||||
e.Height = br.ReadUInt16();
|
||||
e.OffsetX = br.ReadInt16();
|
||||
e.OffsetY = br.ReadInt16();
|
||||
|
||||
e.Type = "image";
|
||||
entries.Add(e);
|
||||
|
||||
fn = br.ReadString();
|
||||
}
|
||||
}
|
||||
|
||||
if (entries.Any())
|
||||
{
|
||||
long imageDataOffset = stream.Position;
|
||||
|
||||
foreach (PidaEntryV1 e in entries)
|
||||
{
|
||||
e.Offset += imageDataOffset;
|
||||
}
|
||||
{
|
||||
PidaEntryV1 last = entries.Last();
|
||||
last.Size = (uint)(stream.Length - last.Offset);
|
||||
}
|
||||
for (int i = 0; i < entries.Count - 1; ++i)
|
||||
{
|
||||
PidaEntryV1 curr = entries[i + 0];
|
||||
PidaEntryV1 next = entries[i + 1];
|
||||
curr.Size = (uint)(next.Offset - curr.Offset);
|
||||
}
|
||||
}
|
||||
|
||||
return new ArcFile(file, this, entries.Cast<Entry>().ToList());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
153
ArcFormats/Entergram/ArcPacV1.cs
Normal file
153
ArcFormats/Entergram/ArcPacV1.cs
Normal file
@@ -0,0 +1,153 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.Composition;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace GameRes.Formats.Entergram
|
||||
{
|
||||
[Export(typeof(ArchiveFormat))]
|
||||
public class PacOpenerV1 : ArchiveFormat
|
||||
{
|
||||
public override string Tag => "PacV1/Entergram";
|
||||
public override string Description => "Entergram Unity resource archive";
|
||||
public override uint Signature => 0x20434150; // PAC/x20
|
||||
public override bool IsHierarchic => true;
|
||||
public override bool CanWrite => false;
|
||||
|
||||
private static readonly byte[] smHeader = new byte[]
|
||||
{
|
||||
0x50, 0x41, 0x43, 0x20, 0x56, 0x45, 0x52, 0x2D, 0x31, 0x2E, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
public override ArcFile TryOpen(ArcView file)
|
||||
{
|
||||
if (!file.View.BytesEqual(0L, smHeader))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
List<Entry> entries = this.ParseEntry(file);
|
||||
if(entries == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new ArcFile(file, this, entries);
|
||||
}
|
||||
|
||||
public override Stream OpenEntry(ArcFile arc, Entry entry)
|
||||
{
|
||||
if(!(entry is PackedEntry e))
|
||||
{
|
||||
return base.OpenEntry(arc, entry);
|
||||
}
|
||||
|
||||
if (e.IsPacked)
|
||||
{
|
||||
using(ArcViewStream s = arc.File.CreateStream(e.Offset, e.Size))
|
||||
{
|
||||
byte[] data = s.ReadBytes(int.MaxValue);
|
||||
data = QLZCompressor.Decompress(data);
|
||||
return new MemoryStream(data, false);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return base.OpenEntry(arc, entry);
|
||||
}
|
||||
}
|
||||
|
||||
private List<Entry> ParseEntry(ArcView file)
|
||||
{
|
||||
bool compressed = Path.GetFileNameWithoutExtension(file.Name).EndsWith("_c");
|
||||
using(ArcViewStream stream = file.CreateStream(smHeader.LongLength))
|
||||
{
|
||||
List<PackedEntry> entries = new List<PackedEntry>();
|
||||
|
||||
// 8 Bytes Entry Mode (Default)
|
||||
{
|
||||
stream.Position = 0L;
|
||||
while (stream.Position < stream.Length)
|
||||
{
|
||||
string name = ReadString(stream);
|
||||
|
||||
long offset = stream.ReadInt64() + 0x10L;
|
||||
long length = stream.ReadInt64();
|
||||
if (!CheckEntry(offset, length, file.MaxOffset))
|
||||
{
|
||||
entries.Clear();
|
||||
break;
|
||||
}
|
||||
|
||||
PackedEntry entry = Create<PackedEntry>(name);
|
||||
entry.Offset = offset;
|
||||
entry.Size = (uint)length;
|
||||
entry.IsPacked = compressed;
|
||||
entries.Add(entry);
|
||||
|
||||
stream.Seek(length, SeekOrigin.Current);
|
||||
}
|
||||
}
|
||||
|
||||
// 10 Bytes Entry Mode
|
||||
if (!entries.Any())
|
||||
{
|
||||
stream.Position = 0L;
|
||||
while (stream.Position < stream.Length)
|
||||
{
|
||||
string name = ReadString(stream);
|
||||
|
||||
long offset = stream.ReadInt64() + 0x14L;
|
||||
stream.Position += 2L;
|
||||
long length = stream.ReadInt64();
|
||||
stream.Position += 2L;
|
||||
if (!CheckEntry(offset, length, file.MaxOffset))
|
||||
{
|
||||
entries.Clear();
|
||||
break;
|
||||
}
|
||||
|
||||
PackedEntry entry = Create<PackedEntry>(name);
|
||||
entry.Offset = offset;
|
||||
entry.Size = (uint)length;
|
||||
entry.IsPacked = compressed;
|
||||
entries.Add(entry);
|
||||
|
||||
stream.Seek(length, SeekOrigin.Current);
|
||||
}
|
||||
}
|
||||
|
||||
return entries.Cast<Entry>().ToList();
|
||||
}
|
||||
}
|
||||
|
||||
private static string ReadString(ArcViewStream stream)
|
||||
{
|
||||
byte[] buf = new byte[0x20];
|
||||
if (stream.Read(buf, 0, buf.Length) != buf.Length)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
int len = Array.IndexOf<byte>(buf, 0);
|
||||
if (len <= 0)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
return Encoding.UTF8.GetString(buf, 0, len);
|
||||
}
|
||||
|
||||
private static bool CheckEntry(long offset, long length, long max)
|
||||
{
|
||||
return offset >= 0L &&
|
||||
length >= 0L &&
|
||||
offset < max &&
|
||||
length <= max &&
|
||||
length <= uint.MaxValue &&
|
||||
offset <= max - length;
|
||||
}
|
||||
}
|
||||
}
|
||||
170
ArcFormats/Entergram/QuickLZ.cs
Normal file
170
ArcFormats/Entergram/QuickLZ.cs
Normal file
@@ -0,0 +1,170 @@
|
||||
using System;
|
||||
|
||||
namespace GameRes.Formats.Entergram
|
||||
{
|
||||
internal static class QLZCompressor
|
||||
{
|
||||
public struct QLZHeader
|
||||
{
|
||||
public QLZHeader(byte[] src)
|
||||
{
|
||||
byte b = src[0];
|
||||
this.Compressible = (b & CONTAINER_Compressible) == CONTAINER_Compressible;
|
||||
if (this.Compressible)
|
||||
{
|
||||
b -= CONTAINER_Compressible;
|
||||
}
|
||||
if (b != STATIC_HEADER_FIRSTBYTE)
|
||||
{
|
||||
throw new Exception("Invalid QLZ Header: " + b.ToString());
|
||||
}
|
||||
this.CompressedSize = BitConverter.ToInt32(src, 1);
|
||||
this.RawSize = BitConverter.ToInt32(src, 5);
|
||||
}
|
||||
|
||||
public const int HEADER_LENGTH = 9;
|
||||
public const byte STATIC_HEADER_FIRSTBYTE = 0x5E;
|
||||
public const byte CONTAINER_Compressible = 1;
|
||||
public const int Level = 3;
|
||||
|
||||
public bool Compressible;
|
||||
public int CompressedSize;
|
||||
public int RawSize;
|
||||
}
|
||||
|
||||
public static byte[] Decompress(byte[] compressed)
|
||||
{
|
||||
QLZCompressor.QLZHeader qlzheader = new QLZCompressor.QLZHeader(compressed);
|
||||
if (qlzheader.RawSize == 0)
|
||||
{
|
||||
return new byte[0];
|
||||
}
|
||||
byte[] array = new byte[qlzheader.RawSize];
|
||||
if (!qlzheader.Compressible)
|
||||
{
|
||||
Array.Copy(compressed, QLZHeader.HEADER_LENGTH, array, 0, qlzheader.RawSize);
|
||||
}
|
||||
else
|
||||
{
|
||||
QLZCompressor.Decompress_Unsafe(compressed, array, qlzheader.RawSize);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
private unsafe static void Decompress_Unsafe(byte[] compressed, byte[] decompressed, int rawSize)
|
||||
{
|
||||
if (rawSize < decompressed.Length)
|
||||
{
|
||||
throw new Exception("Decompressed Array is not enough size");
|
||||
}
|
||||
|
||||
fixed (byte* ptr = compressed)
|
||||
{
|
||||
fixed (byte* ptr2 = decompressed)
|
||||
{
|
||||
int src = QLZHeader.HEADER_LENGTH;
|
||||
int dst = 0;
|
||||
uint cword_val = 1U;
|
||||
int last_matchstart = rawSize - UNCONDITIONAL_MATCHLEN - UNCOMPRESSED_END - 1;
|
||||
uint fetch = 0U;
|
||||
for (; ; )
|
||||
{
|
||||
if (cword_val == 1U)
|
||||
{
|
||||
cword_val = ReadUInt32(ptr + src);
|
||||
src += 4;
|
||||
if (dst <= last_matchstart)
|
||||
{
|
||||
fetch = ReadUInt32(ptr + src);
|
||||
}
|
||||
}
|
||||
if ((cword_val & 1U) == 1U)
|
||||
{
|
||||
cword_val >>= 1;
|
||||
uint offset;
|
||||
uint matchlen;
|
||||
if ((fetch & 3U) == 0U)
|
||||
{
|
||||
offset = (fetch & 0xFFU) >> 2;
|
||||
matchlen = 3U;
|
||||
src++;
|
||||
}
|
||||
else if ((fetch & 2U) == 0U)
|
||||
{
|
||||
offset = (fetch & 0xFFFFU) >> 2;
|
||||
matchlen = 3U;
|
||||
src += 2;
|
||||
}
|
||||
else if ((fetch & 1U) == 0U)
|
||||
{
|
||||
offset = (fetch & 0xFFFFU) >> 6;
|
||||
matchlen = ((fetch >> 2) & 0xFU) + 3U;
|
||||
src += 2;
|
||||
}
|
||||
else if ((fetch & 0x7FU) != 3U)
|
||||
{
|
||||
offset = (fetch >> 7) & 0x1FFFFU;
|
||||
matchlen = ((fetch >> 2) & 0x1FU) + 2U;
|
||||
src += 3;
|
||||
}
|
||||
else
|
||||
{
|
||||
offset = fetch >> 0xF;
|
||||
matchlen = ((fetch >> 7) & 0xFFU) + 3U;
|
||||
src += 4;
|
||||
}
|
||||
uint num7 = (uint)((long)dst - (long)((ulong)offset));
|
||||
ptr2[dst] = ptr2[num7];
|
||||
(ptr2 + dst)[1] = (ptr2 + num7)[1];
|
||||
(ptr2 + dst)[2] = (ptr2 + num7)[2];
|
||||
int num8 = 3;
|
||||
while ((long)num8 < (long)((ulong)matchlen))
|
||||
{
|
||||
(ptr2 + dst)[num8] = (ptr2 + num7)[num8];
|
||||
num8++;
|
||||
}
|
||||
dst += (int)matchlen;
|
||||
fetch = ReadUInt32(ptr + src);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (dst > last_matchstart)
|
||||
{
|
||||
break;
|
||||
}
|
||||
ptr2[dst] = ptr[src];
|
||||
dst++;
|
||||
src++;
|
||||
cword_val >>= 1;
|
||||
fetch = (uint)((((int)fetch >> 8) & 0xFFFF) | ((int)(ptr + src)[2] << 0x10) | ((int)(ptr + src)[3] << 0x18));
|
||||
}
|
||||
}
|
||||
while (dst <= rawSize - 1)
|
||||
{
|
||||
if (cword_val == 1U)
|
||||
{
|
||||
src += 4;
|
||||
cword_val = 0x80000000U;
|
||||
}
|
||||
ptr2[dst] = ptr[src];
|
||||
dst++;
|
||||
src++;
|
||||
cword_val >>= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private unsafe static uint ReadUInt32(byte* p)
|
||||
{
|
||||
return *(uint*)p;
|
||||
}
|
||||
|
||||
private const int HASH_VALUES = 0x1000;
|
||||
private const int MINOFFSET = 2;
|
||||
private const int UNCONDITIONAL_MATCHLEN = 6;
|
||||
private const int UNCOMPRESSED_END = 4;
|
||||
private const int CWORD_LEN = 4;
|
||||
private const int QLZ_POINTERS_3 = 0x10;
|
||||
}
|
||||
}
|
||||
193
ArcFormats/LightVN/ArcMCDAT.cs
Normal file
193
ArcFormats/LightVN/ArcMCDAT.cs
Normal file
@@ -0,0 +1,193 @@
|
||||
//! \file ArcMCDAT.cs
|
||||
//! \date 2026-5-27
|
||||
//! \brief LightVN Engine mcdat resource archive
|
||||
//
|
||||
// Copyright (C) 2026 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 System.Linq;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace GameRes.Formats.LightVN
|
||||
{
|
||||
internal class McdatArchive : ArcFile
|
||||
{
|
||||
private readonly Dictionary<string, string> mMap;
|
||||
private readonly string mRoot;
|
||||
private readonly byte[] mKey;
|
||||
|
||||
public McdatArchive(ArcView arc, ArchiveFormat impl, ICollection<Entry> dir,
|
||||
Dictionary<string, string> map, string root, byte[] key)
|
||||
: base(arc, impl, dir)
|
||||
{
|
||||
this.mMap = map;
|
||||
this.mRoot = root;
|
||||
this.mKey = key;
|
||||
}
|
||||
|
||||
public string GetFilePath(string name)
|
||||
{
|
||||
if (this.mMap.TryGetValue(name, out string relativePath))
|
||||
{
|
||||
return Path.Combine(this.mRoot, relativePath);
|
||||
}
|
||||
else
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
public void RestoreSize()
|
||||
{
|
||||
foreach(Entry e in this.Dir)
|
||||
{
|
||||
string path = this.GetFilePath(e.Name);
|
||||
if(!string.IsNullOrEmpty(path) && System.IO.File.Exists(path))
|
||||
{
|
||||
FileInfo fi = new FileInfo(path);
|
||||
e.Size = (uint)fi.Length;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Decrypt(byte[] data)
|
||||
{
|
||||
McdatArchive.Decrypt(data, this.mKey, 100);
|
||||
}
|
||||
|
||||
public static void Decrypt(byte[] data, byte[] key, int length)
|
||||
{
|
||||
int dataLen = data.Length;
|
||||
|
||||
int decLen;
|
||||
if (length < 0)
|
||||
{
|
||||
decLen = dataLen;
|
||||
}
|
||||
else
|
||||
{
|
||||
decLen = Math.Min(dataLen, length);
|
||||
}
|
||||
|
||||
for (int i = 1; i < decLen; ++i)
|
||||
{
|
||||
byte k = key[i % key.Length];
|
||||
data[dataLen - i] ^= k;
|
||||
}
|
||||
|
||||
for (int i = 0; i < decLen; ++i)
|
||||
{
|
||||
byte k = key[i % key.Length];
|
||||
data[i] ^= k;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Export(typeof(ArchiveFormat))]
|
||||
public class McdatOpener : ArchiveFormat
|
||||
{
|
||||
public override string Tag => "MCDAT/LightVN";
|
||||
public override string Description => "LightVN Engine resource archive";
|
||||
public override uint Signature => 0u;
|
||||
public override bool IsHierarchic => true;
|
||||
public override bool CanWrite => false;
|
||||
|
||||
public McdatOpener()
|
||||
{
|
||||
Extensions = new string[] { "mcdat" };
|
||||
}
|
||||
|
||||
private static readonly string smIndexRelativePath = "\\Data\\_\\0.mcdat";
|
||||
private static readonly byte[] smDefaultKey = new byte[]
|
||||
{
|
||||
0x64, 0x36, 0x63, 0x35, 0x66, 0x4B, 0x49, 0x33, 0x47, 0x67, 0x42, 0x57, 0x70, 0x5A, 0x46, 0x33,
|
||||
0x54, 0x7A, 0x36, 0x69, 0x61, 0x33, 0x6B, 0x46, 0x30,
|
||||
};
|
||||
|
||||
|
||||
public override ArcFile TryOpen(ArcView file)
|
||||
{
|
||||
if (!file.Name.EndsWith(smIndexRelativePath))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
string root = file.Name.Remove(file.Name.Length - smIndexRelativePath.Length);
|
||||
byte[] key = this.QueryKey();
|
||||
|
||||
byte[] index = file.View.ReadBytes(0L, (uint)file.MaxOffset);
|
||||
McdatArchive.Decrypt(index, key, -1);
|
||||
|
||||
Dictionary<string, string> map = null;
|
||||
try
|
||||
{
|
||||
string json = Encoding.UTF8.GetString(index);
|
||||
map = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (map == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
List<Entry> entries = new List<Entry>(map.Count);
|
||||
foreach (string name in map.Keys)
|
||||
{
|
||||
Entry entry = Create<Entry>(name);
|
||||
entry.Offset = 0L;
|
||||
entry.Size = 0u;
|
||||
entries.Add(entry);
|
||||
}
|
||||
|
||||
McdatArchive mcdatArc = new McdatArchive(file, this, entries, map, root, key);
|
||||
mcdatArc.RestoreSize();
|
||||
return mcdatArc;
|
||||
}
|
||||
|
||||
public override Stream OpenEntry(ArcFile arc, Entry entry)
|
||||
{
|
||||
McdatArchive mcdatArc = (McdatArchive)arc;
|
||||
|
||||
string path = mcdatArc.GetFilePath(entry.Name);
|
||||
if (!string.IsNullOrEmpty(path) && File.Exists(path))
|
||||
{
|
||||
byte[] data = File.ReadAllBytes(path);
|
||||
mcdatArc.Decrypt(data);
|
||||
return new MemoryStream(data, false);
|
||||
}
|
||||
return Stream.Null;
|
||||
}
|
||||
|
||||
private byte[] QueryKey()
|
||||
{
|
||||
return smDefaultKey;
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@@ -90,9 +90,9 @@ namespace GameRes
|
||||
Name = name ?? "";
|
||||
}
|
||||
|
||||
public ArcViewStream (ArcView file, long offset, uint size, string name = null)
|
||||
public ArcViewStream (ArcView file, long offset, long size, string name = null)
|
||||
{
|
||||
m_view = new ArcView.Frame (file, offset, Math.Min (size, MaxFrameSize));
|
||||
m_view = new ArcView.Frame (file, offset, (uint)Math.Min (size, MaxFrameSize));
|
||||
m_start = offset;
|
||||
m_size = size;
|
||||
m_position = 0;
|
||||
|
||||
@@ -214,9 +214,11 @@ namespace GameRes
|
||||
public ArcViewStream CreateStream (long offset)
|
||||
{
|
||||
var size = this.MaxOffset - offset;
|
||||
if (size > uint.MaxValue)
|
||||
throw new ArgumentOutOfRangeException ("Too large memory mapped stream");
|
||||
return new ArcViewStream (this, offset, (uint)size);
|
||||
if (size < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(offset), "Greater than archive size");
|
||||
}
|
||||
return new ArcViewStream (this, offset, size);
|
||||
}
|
||||
|
||||
public ArcViewStream CreateStream (long offset, uint size, string name = null)
|
||||
|
||||
Reference in New Issue
Block a user