(RpgMaker): moved to Experimental project.

This commit is contained in:
morkt
2018-11-04 15:45:04 +04:00
parent 63e321ca62
commit bb8d3dadb8
5 changed files with 81 additions and 16 deletions

View File

@@ -67,6 +67,7 @@
<Reference Include="System.Data.SQLite">
<HintPath>..\packages\System.Data.SQLite.Core.1.0.106.0\lib\net46\System.Data.SQLite.dll</HintPath>
</Reference>
<Reference Include="System.Web.Extensions" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
@@ -79,6 +80,9 @@
<Compile Include="CellWorks\ArcDB.cs" />
<Compile Include="Opus\AudioOPUS.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RPGMaker\ArcRGSS.cs" />
<Compile Include="RPGMaker\AudioRPGMV.cs" />
<Compile Include="RPGMaker\ImageRPGMV.cs" />
<Compile Include="WebP\ImageWEBP.cs" />
</ItemGroup>
<ItemGroup>

View File

@@ -0,0 +1,176 @@
//! \file ArcRGSS3.cs
//! \date 2017 Nov 18
//! \brief RPG Maker resource archive implementation.
//
// Copyright (C) 2017 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.Text;
namespace GameRes.Formats.RPGMaker
{
[Export(typeof(ArchiveFormat))]
public class RgssOpener : ArchiveFormat
{
public override string Tag { get { return "RGSSAD"; } }
public override string Description { get { return "RPG Maker engine resource archive"; } }
public override uint Signature { get { return 0x53534752; } } // 'RGSS'
public override bool IsHierarchic { get { return true; } }
public override bool CanWrite { get { return false; } }
public RgssOpener ()
{
Extensions = new string[] { "rgss3a", "rgss2a", "rgssad" };
}
public override ArcFile TryOpen (ArcView file)
{
if (!file.View.AsciiEqual (4, "AD\0"))
return null;
int version = file.View.ReadByte (7);
using (var index = file.CreateStream())
{
List<Entry> dir = null;
if (3 == version)
dir = ReadIndexV3 (index);
else if (1 == version)
dir = ReadIndexV1 (index);
if (null == dir || 0 == dir.Count)
return null;
return new ArcFile (file, this, dir);
}
}
List<Entry> ReadIndexV1 (IBinaryStream file)
{
var max_offset = file.Length;
file.Position = 8;
var key_gen = new KeyGenerator (0xDEADCAFE);
var dir = new List<Entry>();
while (file.PeekByte() != -1)
{
uint name_length = file.ReadUInt32() ^ key_gen.GetNext();
var name_bytes = file.ReadBytes ((int)name_length);
var name = DecryptName (name_bytes, key_gen);
var entry = FormatCatalog.Instance.Create<RgssEntry> (name);
entry.Size = file.ReadUInt32() ^ key_gen.GetNext();
entry.Offset = file.Position;
entry.Key = key_gen.Current;
if (!entry.CheckPlacement (max_offset))
return null;
dir.Add (entry);
file.Seek (entry.Size, SeekOrigin.Current);
}
return dir;
}
List<Entry> ReadIndexV3 (IBinaryStream file)
{
var max_offset = file.Length;
file.Position = 8;
uint key = file.ReadUInt32() * 9 + 3;
var dir = new List<Entry>();
while (file.PeekByte() != -1)
{
uint offset = file.ReadUInt32() ^ key;
if (0 == offset)
break;
uint size = file.ReadUInt32() ^ key;
uint entry_key = file.ReadUInt32() ^ key;
uint name_length = file.ReadUInt32() ^ key;
var name_bytes = file.ReadBytes ((int)name_length);
var name = DecryptName (name_bytes, key);
var entry = FormatCatalog.Instance.Create<RgssEntry> (name);
entry.Offset = offset;
entry.Size = size;
entry.Key = entry_key;
if (!entry.CheckPlacement (max_offset))
return null;
dir.Add (entry);
}
return dir;
}
public override Stream OpenEntry (ArcFile arc, Entry entry)
{
var rent = (RgssEntry)entry;
var data = arc.File.View.ReadBytes (rent.Offset, rent.Size);
var key_gen = new KeyGenerator (rent.Key);
uint key = key_gen.GetNext();
for (int i = 0; i < data.Length; )
{
data[i] ^= (byte)(key >> (i << 3));
++i;
if (0 == (i & 3))
{
key = key_gen.GetNext();
}
}
return new BinMemoryStream (data);
}
string DecryptName (byte[] name, KeyGenerator key_gen)
{
for (int i = 0; i < name.Length; ++i)
{
name[i] ^= (byte)key_gen.GetNext();
}
return Encoding.UTF8.GetString (name);
}
string DecryptName (byte[] name, uint key)
{
for (int i = 0; i < name.Length; ++i)
{
name[i] ^= (byte)(key >> (i << 3));
}
return Encoding.UTF8.GetString (name);
}
}
internal class RgssEntry : Entry
{
public uint Key;
}
internal class KeyGenerator
{
uint m_seed;
public KeyGenerator (uint seed)
{
m_seed = seed;
}
public uint Current { get { return m_seed; } }
public uint GetNext ()
{
uint key = m_seed;
m_seed = m_seed * 7 + 3;
return key;
}
}
}

View File

@@ -0,0 +1,58 @@
//! \file AudioRPGMV.cs
//! \date 2018 Oct 14
//! \brief RPG Maker encrypted OGG audio.
//
// Copyright (C) 2018 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;
namespace GameRes.Formats.RPGMaker
{
[Export(typeof(AudioFormat))]
public sealed class RpgmvoAudio : AudioFormat
{
public override string Tag { get { return "RPGMVO"; } }
public override string Description { get { return "RPG Maker audio format (Ogg/Vorbis)"; } }
public override uint Signature { get { return 0x4D475052; } } // 'RPGMV'
public override bool CanWrite { get { return false; } }
public override SoundInput TryOpen (IBinaryStream file)
{
var header = file.ReadHeader (0x14);
if (header[4] != 'V')
return null;
var key = RpgmvDecryptor.LastKey ?? RpgmvDecryptor.FindKeyFor (file.Name);
if (null == key)
return null;
for (int i = 0; i < 4; ++i)
header[0x10+i] ^= key[i];
if (!header.AsciiEqual (0x10, "OggS"))
{
RpgmvDecryptor.LastKey = null;
return null;
}
RpgmvDecryptor.LastKey = key;
var ogg = RpgmvDecryptor.DecryptStream (file, key);
return OggAudio.Instance.TryOpen (ogg);
}
}
}

View File

@@ -0,0 +1,162 @@
//! \file ImageRPGMV.cs
//! \date 2018 Oct 14
//! \brief RPG Maker encrypted PNG image.
//
// Copyright (C) 2018 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.Collections;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Text;
using System.Web.Script.Serialization;
namespace GameRes.Formats.RPGMaker
{
internal class RpgmvpMetaData : ImageMetaData
{
public byte[] Key;
}
[Export(typeof(ImageFormat))]
public class RpgmvpFormat : ImageFormat
{
public override string Tag { get { return "RPGMVP"; } }
public override string Description { get { return "RPG Maker engine image format"; } }
public override uint Signature { get { return 0x4D475052; } } // 'RPGMV'
public override ImageMetaData ReadMetaData (IBinaryStream file)
{
var header = file.ReadHeader (0x14);
if (header[4] != 'V')
return null;
var key = RpgmvDecryptor.LastKey ?? RpgmvDecryptor.FindKeyFor (file.Name);
if (null == key)
return null;
for (int i = 0; i < 4; ++i)
header[0x10+i] ^= key[i];
if (!header.AsciiEqual (0x10, "\x89PNG"))
{
RpgmvDecryptor.LastKey = null;
return null;
}
RpgmvDecryptor.LastKey = key;
using (var png = RpgmvDecryptor.DecryptStream (file, key, true))
{
var info = Png.ReadMetaData (png);
if (null == info)
return null;
return new RpgmvpMetaData {
Width = info.Width,
Height = info.Height,
OffsetX = info.OffsetX,
OffsetY = info.OffsetY,
BPP = info.BPP,
Key = key,
};
}
}
public override ImageData Read (IBinaryStream file, ImageMetaData info)
{
var meta = (RpgmvpMetaData)info;
using (var png = RpgmvDecryptor.DecryptStream (file, meta.Key, true))
return Png.Read (png, info);
}
public override void Write (Stream file, ImageData image)
{
throw new System.NotImplementedException ("RpgmvpFormat.Write not implemented");
}
}
internal class RpgmvDecryptor
{
public static IBinaryStream DecryptStream (IBinaryStream input, byte[] key, bool leave_open = false)
{
input.Position = 0x10;
var header = input.ReadBytes (key.Length);
for (int i = 0; i < key.Length; ++i)
header[i] ^= key[i];
var result = new PrefixStream (header, new StreamRegion (input.AsStream, input.Position, leave_open));
return new BinaryStream (result, input.Name);
}
static byte[] GetKeyFromString (string hex)
{
if ((hex.Length & 1) != 0)
throw new System.ArgumentException ("invalid key string");
var key = new byte[hex.Length/2];
for (int i = 0; i < key.Length; ++i)
{
key[i] = (byte)(HexToInt (hex[i * 2]) << 4 | HexToInt (hex[i * 2 + 1]));
}
return key;
}
static int HexToInt (char x)
{
if (char.IsDigit (x))
return x - '0';
else
return char.ToUpper (x) - 'A' + 10;
}
static byte[] ParseSystemJson (string filename)
{
var json = File.ReadAllText (filename, Encoding.UTF8);
var serializer = new JavaScriptSerializer();
var sys = serializer.DeserializeObject (json) as IDictionary;
if (null == sys)
return null;
var key = sys["encryptionKey"] as string;
if (null == key)
return null;
return GetKeyFromString (key);
}
public static byte[] FindKeyFor (string filename)
{
foreach (var system_filename in FindSystemJson (filename))
{
if (File.Exists (system_filename))
return ParseSystemJson (system_filename);
}
return null;
}
static IEnumerable<string> FindSystemJson (string filename)
{
var dir_name = Path.GetDirectoryName (filename);
yield return Path.Combine (dir_name, @"..\..\data\System.json");
yield return Path.Combine (dir_name, @"..\..\..\www\data\System.json");
yield return Path.Combine (dir_name, @"..\data\System.json");
yield return Path.Combine (dir_name, @"data\System.json");
}
internal static readonly byte[] DefaultKey = {
0x77, 0x4E, 0x46, 0x45, 0xFC, 0x43, 0x2F, 0x71, 0x47, 0x95, 0xA2, 0x43, 0xE5, 0x10, 0x13, 0xD8
};
internal static byte[] LastKey = null;
}
}