implemented Yu-Ris archives creation.

This commit is contained in:
morkt 2014-08-16 04:42:20 +04:00
parent 26526a97bb
commit dc30eb0083
12 changed files with 344 additions and 28 deletions

View File

@ -85,6 +85,9 @@
<Compile Include="CreateXP3Widget.xaml.cs">
<DependentUpon>CreateXP3Widget.xaml</DependentUpon>
</Compile>
<Compile Include="CreateYPFWidget.xaml.cs">
<DependentUpon>CreateYPFWidget.xaml</DependentUpon>
</Compile>
<Compile Include="ImageHG3.cs" />
<Compile Include="ImageRCT.cs" />
<Compile Include="ImageTLG.cs" />
@ -162,6 +165,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="CreateYPFWidget.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="WidgetINT.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>

View File

@ -33,12 +33,14 @@ using System.Windows.Media.Imaging;
using ZLibNet;
using GameRes.Formats.Strings;
using GameRes.Formats.Properties;
using GameRes.Utility;
namespace GameRes.Formats
namespace GameRes.Formats.YuRis
{
public class YpfOptions : ResourceOptions
{
public uint Key { get; set; }
public uint Key { get; set; }
public uint Version { get; set; }
}
[Export(typeof(ArchiveFormat))]
@ -48,6 +50,7 @@ namespace GameRes.Formats
public override string Description { get { return arcStrings.YPFDescription; } }
public override uint Signature { get { return 0x00465059; } }
public override bool IsHierarchic { get { return true; } }
public override bool CanCreate { get { return true; } }
private const uint DefaultKey = 0xffffffff;
@ -82,19 +85,11 @@ namespace GameRes.Formats
public override ResourceOptions GetDefaultOptions ()
{
return new YpfOptions { Key = Settings.Default.YPFKey };
}
public override ResourceOptions GetOptions (object w)
{
var widget = w as GUI.WidgetYPF;
if (null != widget)
return new YpfOptions
{
uint last_key = widget.GetKey() ?? DefaultKey;
Settings.Default.YPFKey = last_key;
return new YpfOptions { Key = last_key };
}
return this.GetDefaultOptions();
Key = Settings.Default.YPFKey,
Version = Settings.Default.YPFVersion,
};
}
public override object GetAccessWidget ()
@ -102,12 +97,159 @@ namespace GameRes.Formats
return new GUI.WidgetYPF();
}
public override object GetCreationWidget ()
{
return new GUI.CreateYPFWidget();
}
uint QueryEncryptionKey ()
{
var options = Query<YpfOptions> (arcStrings.YPFNotice);
return options.Key;
}
internal class YpfEntry : PackedEntry
{
public byte[] IndexName;
public uint NameHash;
public byte FileType;
public uint CheckSum;
}
delegate uint ChecksumFunc (byte[] data);
public override void Create (Stream output, IEnumerable<Entry> list, ResourceOptions options,
EntryCallback callback)
{
var ypf_options = GetOptions<YpfOptions> (options);
if (null == ypf_options)
throw new ArgumentException ("Invalid archive creation options", "options");
if (ypf_options.Key > 0xff)
throw new InvalidEncryptionScheme (arcStrings.MsgCreationKeyRequired);
if (0 == ypf_options.Version)
throw new InvalidFormatException (arcStrings.MsgInvalidVersion);
int callback_count = 0;
var encoding = Encodings.cp932.WithFatalFallback();
ChecksumFunc Checksum = data => Crc32.Compute (data, 0, data.Length);
uint data_offset = 0x20;
var file_table = new List<YpfEntry>();
foreach (var entry in list)
{
try
{
string file_name = entry.Name;
byte[] name_buf = encoding.GetBytes (file_name);
if (name_buf.Length > 0xff)
throw new InvalidFileName (entry.Name, arcStrings.MsgFileNameTooLong);
uint hash = Checksum (name_buf);
byte file_type = GetFileType (ypf_options.Version, file_name);
for (int i = 0; i < name_buf.Length; ++i)
name_buf[i] = (byte)(name_buf[i] ^ ypf_options.Key);
file_table.Add (new YpfEntry {
Name = file_name,
IndexName = name_buf,
NameHash = hash,
FileType = file_type,
IsPacked = 0 == file_type,
});
data_offset += (uint)(0x17 + name_buf.Length);
}
catch (EncoderFallbackException X)
{
throw new InvalidFileName (entry.Name, arcStrings.MsgIllegalCharacters, X);
}
}
file_table.Sort ((a, b) => a.NameHash.CompareTo (b.NameHash));
output.Position = data_offset;
uint current_offset = data_offset;
foreach (var entry in file_table)
{
if (null != callback)
callback (callback_count++, entry, arcStrings.MsgAddingFile);
entry.Offset = current_offset;
using (var input = File.OpenRead (entry.Name))
{
var file_size = input.Length;
if (file_size > uint.MaxValue || current_offset + file_size > uint.MaxValue)
throw new FileSizeException();
entry.UnpackedSize = (uint)file_size;
using (var checked_stream = new CheckedStream (output, new Adler32()))
{
if (entry.IsPacked)
{
using (var zstream = new ZLibStream (checked_stream, CompressionMode.Compress, true))
{
input.CopyTo (zstream);
zstream.Flush();
entry.Size = (uint)zstream.TotalOut;
}
}
else
{
input.CopyTo (checked_stream);
entry.Size = entry.UnpackedSize;
}
checked_stream.Flush();
entry.CheckSum = checked_stream.CheckSumValue;
current_offset += entry.Size;
}
}
}
if (null != callback)
callback (callback_count++, null, arcStrings.MsgWritingIndex);
output.Position = 0;
using (var writer = new BinaryWriter (output, encoding, true))
{
writer.Write (Signature);
writer.Write (ypf_options.Version);
writer.Write (file_table.Count);
writer.Write (data_offset);
writer.BaseStream.Seek (0x20, SeekOrigin.Begin);
foreach (var entry in file_table)
{
writer.Write (entry.NameHash);
byte name_len = (byte)~Parser.Decrypt (ypf_options.Version, (byte)entry.IndexName.Length);
writer.Write (name_len);
writer.Write (entry.IndexName);
writer.Write (entry.FileType);
writer.Write ((byte)(entry.IsPacked ? 1 : 0));
writer.Write (entry.UnpackedSize);
writer.Write (entry.Size);
writer.Write ((uint)entry.Offset);
writer.Write (entry.CheckSum);
}
}
}
static byte GetFileType (uint version, string name)
{
// 0x0F7: 0-ybn, 1-bmp, 2-png, 3-jpg, 4-gif, 5-avi, 6-wav, 7-ogg, 8-psd
// 0x122, 0x12C, 0x196: 0-ybn, 1-bmp, 2-png, 3-jpg, 4-gif, 5-wav, 6-ogg, 7-psd
string ext = Path.GetExtension (name).TrimStart ('.').ToLower();
if ("ybn" == ext) return 0;
if ("bmp" == ext) return 1;
if ("png" == ext) return 2;
if ("jpg" == ext || "jpeg" == ext) return 3;
if ("gif" == ext) return 4;
if ("avi" == ext && 0xf7 == version) return 5;
byte type = 0;
if ("wav" == ext) type = 5;
else if ("ogg" == ext) type = 6;
else if ("psd" == ext) type = 7;
if (0xf7 == version && 0 != type)
++type;
return type;
}
private class Parser
{
ArcView m_file;
@ -136,7 +278,7 @@ namespace GameRes.Formats
break;
dir_remaining -= 0x17;
uint name_size = Decrypt ((byte)(m_file.View.ReadByte (dir_offset+4) ^ 0xff));
uint name_size = Decrypt (m_version, (byte)(m_file.View.ReadByte (dir_offset+4) ^ 0xff));
if (name_size > dir_remaining)
break;
dir_remaining -= name_size;
@ -201,12 +343,12 @@ namespace GameRes.Formats
// 0x28 0x12C "Suzukaze no Melt" (no recovery - 00 00 00 00)
// 0xFF 0x196 "Mamono Musume-tachi to no Rakuen ~Slime & Scylla~"
public uint Decrypt (byte value)
static public byte Decrypt (uint version, byte value)
{
int pos = 4;
if (m_version >= 0x100)
if (version >= 0x100)
{
if (m_version >= 0x12c && m_version < 0x196)
if (version >= 0x12c && version < 0x196)
pos = 10;
else
pos = 0;

View File

@ -0,0 +1,27 @@
<Grid x:Class="GameRes.Formats.GUI.CreateYPFWidget"
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"
xmlns:s="clr-namespace:GameRes.Formats.Strings"
xmlns:gui="clr-namespace:GameRes.Formats.GUI">
<Grid.Resources>
<gui:KeyToStringConverter x:Key="guiKeyConverter" />
<gui:VersionToStringConverter x:Key="guiVersionConverter" />
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition MinWidth="50" Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label Content="{x:Static s:arcStrings.YPFLabelVersion}" ToolTip="{x:Static s:arcStrings.TooltipHex}"
Target="{Binding ElementName=Version}" Grid.Column="0" Grid.Row="0" Padding="0,1" VerticalAlignment="Center" HorizontalAlignment="Right" HorizontalContentAlignment="Right"/>
<TextBox Name="Version" Text="{Binding Source={x:Static p:Settings.Default}, Path=YPFVersion, Mode=TwoWay, Converter={StaticResource guiVersionConverter}}"
ToolTip="{x:Static s:arcStrings.TooltipHex}" Margin="5" Width="50" Grid.Column="1" Grid.Row="0"/>
<Label Content="{x:Static s:arcStrings.YPFLabelKey}" ToolTip="{x:Static s:arcStrings.TooltipHex}"
Target="{Binding ElementName=Passkey}" Grid.Column="0" Grid.Row="1" Padding="0,1" VerticalAlignment="Center" HorizontalAlignment="Right" HorizontalContentAlignment="Right"/>
<TextBox Name="Passkey" Text="{Binding Source={x:Static p:Settings.Default}, Path=YPFKey, Mode=TwoWay, Converter={StaticResource guiKeyConverter}}"
ToolTip="{x:Static s:arcStrings.TooltipHex}" Margin="5" Width="50" Grid.Column="1" Grid.Row="1"/>
</Grid>

View File

@ -0,0 +1,62 @@
using System;
using System.Globalization;
using System.Windows.Controls;
using System.Windows.Data;
namespace GameRes.Formats.GUI
{
/// <summary>
/// Interaction logic for CreateYPFWidget.xaml
/// </summary>
public partial class CreateYPFWidget : Grid
{
public CreateYPFWidget ()
{
InitializeComponent ();
}
}
[ValueConversion (typeof (uint), typeof (string))]
class VersionToStringConverter : IValueConverter
{
public object Convert (object value, Type targetType, object parameter, CultureInfo culture)
{
uint version = (uint)value;
return version.ToString ("X");
}
public object ConvertBack (object value, Type targetType, object parameter, CultureInfo culture)
{
string s = value as string;
if (string.IsNullOrEmpty (s))
return null;
uint version;
if (!uint.TryParse (s, NumberStyles.HexNumber, culture, out version))
return null;
return version;
}
}
[ValueConversion (typeof (uint), typeof (string))]
class KeyToStringConverter : IValueConverter
{
public object Convert (object value, Type targetType, object parameter, CultureInfo culture)
{
uint key = (uint)value;
if (key > 0xff)
return "";
return key.ToString ("X2");
}
public object ConvertBack (object value, Type targetType, object parameter, CultureInfo culture)
{
string s = value as string;
if (string.IsNullOrEmpty (s))
return uint.MaxValue;
uint key;
if (!uint.TryParse (s, NumberStyles.HexNumber, culture, out key) || key > 0xff)
return uint.MaxValue;
return key;
}
}
}

View File

@ -177,5 +177,17 @@ namespace GameRes.Formats.Properties {
this["PDScrambleContents"] = value;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("290")]
public uint YPFVersion {
get {
return ((uint)(this["YPFVersion"]));
}
set {
this["YPFVersion"] = value;
}
}
}
}

View File

@ -41,5 +41,8 @@
<Setting Name="PDScrambleContents" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
<Setting Name="YPFVersion" Type="System.UInt32" Scope="User">
<Value Profile="(Default)">290</Value>
</Setting>
</Settings>
</SettingsFile>

View File

@ -234,6 +234,15 @@ namespace GameRes.Formats.Strings {
}
}
/// <summary>
/// Looks up a localized string similar to Encryption key required for archive creation..
/// </summary>
public static string MsgCreationKeyRequired {
get {
return ResourceManager.GetString("MsgCreationKeyRequired", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Encryption method not implemented.
/// </summary>
@ -270,6 +279,15 @@ namespace GameRes.Formats.Strings {
}
}
/// <summary>
/// Looks up a localized string similar to Invalid archive version specified..
/// </summary>
public static string MsgInvalidVersion {
get {
return ResourceManager.GetString("MsgInvalidVersion", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Number of files exceedes archive limit..
/// </summary>
@ -387,6 +405,15 @@ namespace GameRes.Formats.Strings {
}
}
/// <summary>
/// Looks up a localized string similar to Hex number.
/// </summary>
public static string TooltipHex {
get {
return ResourceManager.GetString("TooltipHex", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Liar-soft game resource archive.
/// </summary>
@ -468,6 +495,15 @@ namespace GameRes.Formats.Strings {
}
}
/// <summary>
/// Looks up a localized string similar to Archive version.
/// </summary>
public static string YPFLabelVersion {
get {
return ResourceManager.GetString("YPFLabelVersion", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Archive directory is encrypted.
///Enter archive encryption key..

View File

@ -177,6 +177,9 @@ predefined encryption scheme.</value>
<data name="MsgCompressingIndex" xml:space="preserve">
<value>Compressing index...</value>
</data>
<data name="MsgCreationKeyRequired" xml:space="preserve">
<value>Encryption key required for archive creation.</value>
</data>
<data name="MsgEncNotImplemented" xml:space="preserve">
<value>Encryption method not implemented</value>
</data>
@ -189,6 +192,9 @@ predefined encryption scheme.</value>
<data name="MsgInvalidImageFormat" xml:space="preserve">
<value>{0}: image format not recognized.</value>
</data>
<data name="MsgInvalidVersion" xml:space="preserve">
<value>Invalid archive version specified.</value>
</data>
<data name="MsgTooManyFiles" xml:space="preserve">
<value>Number of files exceedes archive limit.</value>
</data>
@ -228,6 +234,9 @@ predefined encryption scheme.</value>
<data name="SGLabelEncoding" xml:space="preserve">
<value>Filename encoding</value>
</data>
<data name="TooltipHex" xml:space="preserve">
<value>Hex number</value>
</data>
<data name="XFLDescription" xml:space="preserve">
<value>Liar-soft game resource archive</value>
</data>
@ -255,6 +264,9 @@ predefined encryption scheme.</value>
<data name="YPFLabelKey" xml:space="preserve">
<value>8-bit encryption key</value>
</data>
<data name="YPFLabelVersion" xml:space="preserve">
<value>Archive version</value>
</data>
<data name="YPFNotice" xml:space="preserve">
<value>Archive directory is encrypted.
Enter archive encryption key.</value>

View File

@ -162,6 +162,9 @@
<data name="MsgCompressingIndex" xml:space="preserve">
<value>Сжимается оглавление...</value>
</data>
<data name="MsgCreationKeyRequired" xml:space="preserve">
<value>Для создания архива требуется ключ шифрования.</value>
</data>
<data name="MsgEncNotImplemented" xml:space="preserve">
<value>Метод шифрования не реализован</value>
</data>
@ -174,6 +177,9 @@
<data name="MsgInvalidImageFormat" xml:space="preserve">
<value>{0}: не удалось распознать формат изображения.</value>
</data>
<data name="MsgInvalidVersion" xml:space="preserve">
<value>Указана некорректная версия архива.</value>
</data>
<data name="MsgTooManyFiles" xml:space="preserve">
<value>Количество файлов превышает ограничения архива.</value>
</data>
@ -198,6 +204,9 @@
<data name="SGLabelEncoding" xml:space="preserve">
<value>Кодировка имён файлов</value>
</data>
<data name="TooltipHex" xml:space="preserve">
<value>Шестнадцатеричное число</value>
</data>
<data name="XP3CompressContents" xml:space="preserve">
<value>Сжать содержимое</value>
</data>
@ -216,6 +225,9 @@
<data name="YPFLabelKey" xml:space="preserve">
<value>8-битный ключ шифрования</value>
</data>
<data name="YPFLabelVersion" xml:space="preserve">
<value>Версия архива</value>
</data>
<data name="YPFNotice" xml:space="preserve">
<value>Заголовок архива зашифрован.
Введите ключ шифрования.</value>

View File

@ -2,11 +2,17 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:GameRes.Formats.Strings"
xmlns:p="clr-namespace:GameRes.Formats.Properties"
xmlns:gui="clr-namespace:GameRes.Formats.GUI"
MaxWidth="250">
<Grid.Resources>
<gui:KeyToStringConverter x:Key="guiKeyConverter" />
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition MinWidth="100" Width="*"/>
<ColumnDefinition MinWidth="50" Width="*"/>
</Grid.ColumnDefinitions>
<Label Content="{x:Static s:arcStrings.YPFLabelKey}" Target="{Binding ElementName=Passkey}" Grid.Column="0" Grid.Row="0" Padding="0,1" VerticalAlignment="Center" HorizontalAlignment="Right"/>
<TextBox Name="Passkey" Margin="5" Width="100" Grid.Column="1" Grid.Row="0"/>
</Grid>
<Label Content="{x:Static s:arcStrings.YPFLabelKey}" Target="{Binding ElementName=Passkey}" ToolTip="{x:Static s:arcStrings.TooltipHex}" Grid.Column="0" Grid.Row="0" Padding="0,1" VerticalAlignment="Center" HorizontalAlignment="Right"/>
<TextBox Name="Passkey" Text="{Binding Source={x:Static p:Settings.Default}, Path=YPFKey, Mode=TwoWay, Converter={StaticResource guiKeyConverter}}"
ToolTip="{x:Static s:arcStrings.TooltipHex}" Margin="5" Width="50" Grid.Column="1" Grid.Row="0"/>
</Grid>

View File

@ -12,12 +12,6 @@ namespace GameRes.Formats.GUI
public WidgetYPF ()
{
InitializeComponent();
uint key = Settings.Default.YPFKey;
if (key < 0x100)
this.Passkey.Text = key.ToString();
else
this.Passkey.Text = null;
}
public uint? GetKey ()

View File

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