implemented FJSYS archives and MGD images.

This commit is contained in:
morkt 2016-11-25 07:42:32 +04:00
parent f44e13ae34
commit c42c088bf5
14 changed files with 554 additions and 0 deletions

View File

@ -91,6 +91,11 @@
<Compile Include="ArcARCX.cs" />
<Compile Include="ArcCG.cs" />
<Compile Include="BlackRainbow\ArcDX.cs" />
<Compile Include="NSystem\ArcFJSYS.cs" />
<Compile Include="NSystem\ImageMGD.cs" />
<Compile Include="NSystem\WidgetMSD.xaml.cs">
<DependentUpon>WidgetMSD.xaml</DependentUpon>
</Compile>
<Compile Include="rUGP\ArcRIO.cs" />
<Compile Include="Astronauts\ArcGXP.cs" />
<Compile Include="Banana\ImageGEC.cs" />
@ -734,6 +739,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="NSystem\WidgetMSD.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Qlie\WidgetQLIE.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>

View File

@ -0,0 +1,215 @@
//! \file ArcFJSYS.cs
//! \date Thu Nov 24 14:00:07 2016
//! \brief NSystem engine resource archive.
//
// Copyright (C) 2016 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.Security.Cryptography;
using System.Text;
using GameRes.Formats.Properties;
using GameRes.Formats.Strings;
using GameRes.Utility;
namespace GameRes.Formats.NSystem
{
internal class MsdArchive : ArcFile
{
public readonly string Key;
public MsdArchive (ArcView arc, ArchiveFormat impl, ICollection<Entry> dir, string key)
: base (arc, impl, dir)
{
Key = key;
}
}
[Export(typeof(ArchiveFormat))]
public class FjsysOpener : ArchiveFormat
{
public override string Tag { get { return "FJSYS"; } }
public override string Description { get { return "NSystem engine resource archive"; } }
public override uint Signature { get { return 0x59534A46; } } // 'FJSY'
public override bool IsHierarchic { get { return false; } }
public override bool CanWrite { get { return false; } }
public FjsysOpener ()
{
Extensions = new string[] { "" };
}
public override ArcFile TryOpen (ArcView file)
{
if (file.View.ReadByte (4) != 'S')
return null;
uint names_size = file.View.ReadUInt32 (0xC);
int count = file.View.ReadInt32 (0x10);
if (!IsSaneCount (count))
return null;
uint index_offset = 0x54;
uint index_size = (uint)count * 0x10;
var names = file.View.ReadBytes (index_offset + index_size, names_size);
var dir = new List<Entry> (count);
bool has_scripts = false;
for (int i = 0; i < count; ++i)
{
var name_offset = file.View.ReadInt32 (index_offset);
var name = Binary.GetCString (names, name_offset);
var entry = FormatCatalog.Instance.Create<Entry> (name);
entry.Size = file.View.ReadUInt32 (index_offset+4);
entry.Offset = file.View.ReadInt64 (index_offset+8);
if (!entry.CheckPlacement (file.MaxOffset))
return null;
has_scripts = has_scripts || name.EndsWith (".msd", StringComparison.InvariantCultureIgnoreCase);
dir.Add (entry);
index_offset += 0x10;
}
if (has_scripts)
{
var password = QueryPassword (file.Name);
if (!string.IsNullOrEmpty (password))
return new MsdArchive (file, this, dir, password);
}
return new ArcFile (file, this, dir);
}
public override Stream OpenEntry (ArcFile arc, Entry entry)
{
var msarc = arc as MsdArchive;
if (null == msarc || string.IsNullOrEmpty (msarc.Key)
|| !entry.Name.EndsWith (".msd", StringComparison.InvariantCultureIgnoreCase)
|| arc.File.View.AsciiEqual (entry.Offset, "MSCENARIO FILE "))
return base.OpenEntry (arc, entry);
var input = arc.File.CreateStream (entry.Offset, entry.Size);
return new CryptoStream (input, new MsdTransform (msarc.Key), CryptoStreamMode.Read);
}
string QueryPassword (string arc_name)
{
var title = FormatCatalog.Instance.LookupGame (arc_name);
if (!string.IsNullOrEmpty (title) && KnownPasswords.ContainsKey (title))
return KnownPasswords[title];
var options = Query<FjsysOptions> (arcStrings.FJSYSNotice);
return options.MsdPassword;
}
public override ResourceOptions GetDefaultOptions ()
{
return new FjsysOptions { MsdPassword = Settings.Default.FJSYSPassword };
}
public override object GetAccessWidget ()
{
return new GUI.WidgetMSD();
}
public static Dictionary<string, string> KnownPasswords = new Dictionary<string, string>();
public override ResourceScheme Scheme
{
get { return new FjsysScheme { MsdPasswords = KnownPasswords }; }
set { KnownPasswords = ((FjsysScheme)value).MsdPasswords; }
}
}
internal sealed class MsdTransform : ICryptoTransform
{
const int BlockSize = 0x20;
string m_key;
MD5 m_md5;
StringBuilder m_hash_str;
int m_block_num;
public bool CanReuseTransform { get { return false; } }
public bool CanTransformMultipleBlocks { get { return true; } }
public int InputBlockSize { get { return BlockSize; } }
public int OutputBlockSize { get { return BlockSize; } }
public MsdTransform (string key)
{
m_key = key;
m_md5 = MD5.Create();
m_hash_str = new StringBuilder (BlockSize);
m_block_num = 0;
}
public int TransformBlock (byte[] inputBuffer, int inputOffset, int inputCount,
byte[] outputBuffer, int outputOffset)
{
int block_count = inputCount / BlockSize;
for (int i = 0; i < block_count; ++i)
{
DoTransform (inputBuffer, inputOffset, BlockSize, outputBuffer, outputOffset);
inputOffset += BlockSize;
outputOffset += BlockSize;
}
return inputCount;
}
public byte[] TransformFinalBlock (byte[] inputBuffer, int inputOffset, int inputCount)
{
byte[] outputBuffer = new byte[inputCount];
DoTransform (inputBuffer, inputOffset, inputCount, outputBuffer, 0);
return outputBuffer;
}
void DoTransform (byte[] input, int src, int count, byte[] output, int dst)
{
string chunk_key_str = m_key + m_block_num.ToString();
++m_block_num;
var chunk_key = Encodings.cp932.GetBytes (chunk_key_str);
var hash = m_md5.ComputeHash (chunk_key);
m_hash_str.Clear();
for (int j = 0; j < hash.Length; ++j)
m_hash_str.AppendFormat ("{0:x2}", hash[j]);
count = Math.Min (m_hash_str.Length, count);
for (int k = 0; k < count; ++k)
output[dst++] = (byte)(input[src++] ^ m_hash_str[k]);
}
bool _disposed = false;
public void Dispose ()
{
if (!_disposed)
{
m_md5.Dispose();
_disposed = true;
}
GC.SuppressFinalize (this);
}
}
[Serializable]
public class FjsysScheme : ResourceScheme
{
public Dictionary<string, string> MsdPasswords;
}
public class FjsysOptions : ResourceOptions
{
public string MsdPassword;
}
}

View File

@ -0,0 +1,240 @@
//! \file ImageMGD.cs
//! \date Thu Nov 24 14:37:18 2016
//! \brief NSystem image format.
//
// Copyright (C) 2016 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 System.Windows.Media.Imaging;
namespace GameRes.Formats.NSystem
{
internal class MgdMetaData : ImageMetaData
{
public int DataOffset;
public int UnpackedSize;
public int Mode;
}
[Export(typeof(ImageFormat))]
public class MgdFormat : ImageFormat
{
public override string Tag { get { return "MGD/NSystem"; } }
public override string Description { get { return "NSystem image format"; } }
public override uint Signature { get { return 0x2044474D; } } // 'MGD '
public override ImageMetaData ReadMetaData (IBinaryStream file)
{
var header = file.ReadHeader (0x1A);
int header_size = header.ToUInt16 (4);
uint width = header.ToUInt16 (0xC);
uint height = header.ToUInt16 (0xE);
int unpacked_size = header.ToInt32 (0x10);
int mode = header.ToInt32 (0x18);
if (mode < 0 || mode > 2)
return null;
return new MgdMetaData
{
Width = width,
Height = height,
BPP = 32,
DataOffset = header_size,
UnpackedSize = unpacked_size,
Mode = mode,
};
}
public override ImageData Read (IBinaryStream file, ImageMetaData info)
{
var meta = (MgdMetaData)info;
file.Position = meta.DataOffset;
int data_size = file.ReadInt32();
switch (meta.Mode)
{
case 0:
var pixels = file.ReadBytes (data_size);
return ImageData.Create (info, PixelFormats.Bgra32, null, pixels);
case 1:
{
var decoder = new MgdDecoder (file, meta, data_size);
decoder.Unpack();
return ImageData.Create (info, PixelFormats.Bgra32, null, decoder.Data);
}
case 2:
using (var png = new StreamRegion (file.AsStream, file.Position, data_size, true))
{
var decoder = new PngBitmapDecoder (png,
BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
var frame = decoder.Frames[0];
frame.Freeze();
return new ImageData (frame, info);
}
default:
throw new InvalidFormatException();
}
}
public override void Write (Stream file, ImageData image)
{
throw new System.NotImplementedException ("MgdFormat.Write not implemented");
}
}
internal class MgdDecoder
{
IBinaryStream m_input;
MgdMetaData m_info;
byte[] m_output;
public byte[] Data { get { return m_output; } }
public MgdDecoder (IBinaryStream input, MgdMetaData info, int packed_size)
{
m_input = input;
m_info = info;
m_output = new byte[m_info.UnpackedSize];
}
public void Unpack ()
{
int alpha_size = m_input.ReadInt32();
UnpackAlpha (alpha_size);
int rgb_size = m_input.ReadInt32();
UnpackColor (rgb_size);
}
void UnpackAlpha (int length)
{
int dst = 3;
while (length > 0)
{
int count = m_input.ReadInt16();
length -= 2;
if (count < 0)
{
count = (count & 0x7FFF) + 1;
byte a = (byte)~m_input.ReadUInt8();
length--;
for (int i = 0; i < count; ++i)
{
m_output[dst] = a;
dst += 4;
}
}
else
{
for (int i = 0; i < count; ++i)
{
m_output[dst] = (byte)~m_input.ReadUInt8();
dst += 4;
}
length -= count;
}
}
}
void UnpackColor (int length)
{
int dst = 0;
while (length > 0)
{
int count = m_input.ReadUInt8();
length--;
switch (count & 0xC0)
{
case 0x80:
count &= 0x3F;
int b = m_output[dst-4];
int g = m_output[dst-3];
int r = m_output[dst-2];
for (int i = 0; i < count; ++i)
{
ushort delta = m_input.ReadUInt16();
length -= 2;
if (0 != (delta & 0x8000))
{
r += (delta >> 10) & 0x1F;
g += (delta >> 5) & 0x1F;
b += delta & 0x1F;
}
else
{
if (0 != (delta & 0x4000))
r -= (delta >> 10) & 0xF;
else
r += (delta >> 10) & 0xF;
if (0 != (delta & 0x0200))
g -= (delta >> 5) & 0xF;
else
g += (delta >> 5) & 0xF;
if (0 != (delta & 0x0010))
b -= delta & 0xF;
else
b += delta & 0xF;
}
m_output[dst ] = (byte)b;
m_output[dst+1] = (byte)g;
m_output[dst+2] = (byte)r;
dst += 4;
}
break;
case 0x40:
count &= 0x3F;
m_input.Read (m_output, dst, 3);
length -= 3;
int src = dst;
dst += 4;
for (int i = 0; i < count; ++i)
{
m_output[dst ] = m_output[src ];
m_output[dst+1] = m_output[src+1];
m_output[dst+2] = m_output[src+2];
dst += 4;
}
break;
case 0:
for (int i = 0; i < count; ++i)
{
m_input.Read (m_output, dst, 3);
length -= 3;
dst += 4;
}
break;
default:
throw new InvalidFormatException();
}
}
}
}
}

View File

@ -0,0 +1,10 @@
<StackPanel x:Class="GameRes.Formats.GUI.WidgetMSD"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:p="clr-namespace:GameRes.Formats.Properties">
<ComboBox Name="Title" ItemsSource="{Binding}"
SelectedValue="{Binding Source={x:Static p:Settings.Default}, Path=FJSYSPassword, Mode=TwoWay}"
SelectedValuePath="Value" DisplayMemberPath="Key" SelectionChanged="Title_SelectionChanged"
Width="250" HorizontalAlignment="Left"/>
<TextBox Name="Password" Width="250" HorizontalAlignment="Left" Margin="0,5,0,0"/>
</StackPanel>

View File

@ -0,0 +1,32 @@
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using GameRes.Formats.NSystem;
using GameRes.Formats.Properties;
using GameRes.Formats.Strings;
namespace GameRes.Formats.GUI
{
/// <summary>
/// Interaction logic for WidgetMSD.xaml
/// </summary>
public partial class WidgetMSD : StackPanel
{
public WidgetMSD ()
{
InitializeComponent ();
var first = new Dictionary<string, string> { { arcStrings.ArcNoEncryption, "" } };
Title.ItemsSource = first.Concat (FjsysOpener.KnownPasswords.OrderBy (x => x.Key));
Password.Text = Settings.Default.FJSYSPassword;
}
private void Title_SelectionChanged (object sender, SelectionChangedEventArgs e)
{
if (null != this.Title.SelectedItem && null != this.Password)
{
var selected = (KeyValuePair<string, string>)this.Title.SelectedItem;
this.Password.Text = selected.Value;
}
}
}
}

View File

@ -657,5 +657,17 @@ namespace GameRes.Formats.Properties {
this["EAGLSEncryption"] = value;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("")]
public string FJSYSPassword {
get {
return ((string)(this["FJSYSPassword"]));
}
set {
this["FJSYSPassword"] = value;
}
}
}
}

View File

@ -161,5 +161,8 @@
<Setting Name="EAGLSEncryption" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="FJSYSPassword" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
</Settings>
</SettingsFile>

View File

@ -188,6 +188,16 @@ namespace GameRes.Formats.Strings {
}
}
/// <summary>
/// Looks up a localized string similar to Archive contains encrypted scripts.
///Choose encryption scheme or enter a passphrase..
/// </summary>
public static string FJSYSNotice {
get {
return ResourceManager.GetString("FJSYSNotice", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Choose title or enter a key.
/// </summary>

View File

@ -379,4 +379,8 @@
<value>이미지가 암호화됨.
적절한 암호체계를 선택하세요.</value>
</data>
<data name="FJSYSNotice" xml:space="preserve">
<value>아카이브가 암호화된 스크립트를 포함함.
암호체계를 고르거나 암호를 입력하세요.</value>
</data>
</root>

View File

@ -382,4 +382,8 @@ Choose appropriate encryption scheme.</value>
<value>Image is encrypted.
Choose appropriate encryption scheme.</value>
</data>
<data name="FJSYSNotice" xml:space="preserve">
<value>Archive contains encrypted scripts.
Choose encryption scheme or enter a passphrase.</value>
</data>
</root>

View File

@ -158,6 +158,10 @@
<data name="DPKKeys" xml:space="preserve">
<value>Ключи шифрования</value>
</data>
<data name="FJSYSNotice" xml:space="preserve">
<value>Архив содержит зашифрованные скрипты.
Выберите способ шифрования или введите текстовый пароль.</value>
</data>
<data name="GALChoose" xml:space="preserve">
<value>Выберите наименование или введите ключ</value>
</data>

View File

@ -380,4 +380,8 @@
<value>图像已加密。
请选择正确的加密方式。</value>
</data>
<data name="FJSYSNotice" xml:space="preserve">
<value>压缩文件包含已加密的脚本。
请选择加密方式或输入密码。</value>
</data>
</root>

View File

@ -163,6 +163,9 @@
<setting name="EAGLSEncryption" serializeAs="String">
<value />
</setting>
<setting name="FJSYSPassword" serializeAs="String">
<value />
</setting>
</GameRes.Formats.Properties.Settings>
</userSettings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup></configuration>

View File

@ -1266,6 +1266,10 @@ Soukan Yuugi<br/>
Motto! Debutopia<br/>
Ura Seitokai yo, Koumon o Seiatsu Seyo!<br/>
</td></tr>
<tr class="odd"><td>*</td><td><tt>FJSYS</tt></td><td>No</td><td rowspan="2">NSystem</td><td rowspan="2">
Idol★Harem<br/>
</td></tr>
<tr class="odd last"><td>*.mgd</td><td><tt>MGD</tt></td><td>No</td></tr>
</table>
<p><a name="note-1" class="footnote">1</a> Non-encrypted only</p>
</body>