implemented Will co. archives creation.

This commit is contained in:
morkt 2014-11-01 02:14:51 +04:00
parent 56d2a45d94
commit 0b8eb1f9d4
11 changed files with 242 additions and 8 deletions

View File

@ -93,6 +93,9 @@
<Compile Include="CreateSGWidget.xaml.cs"> <Compile Include="CreateSGWidget.xaml.cs">
<DependentUpon>CreateSGWidget.xaml</DependentUpon> <DependentUpon>CreateSGWidget.xaml</DependentUpon>
</Compile> </Compile>
<Compile Include="CreateWARCWidget.xaml.cs">
<DependentUpon>CreateWARCWidget.xaml</DependentUpon>
</Compile>
<Compile Include="CreateXP3Widget.xaml.cs"> <Compile Include="CreateXP3Widget.xaml.cs">
<DependentUpon>CreateXP3Widget.xaml</DependentUpon> <DependentUpon>CreateXP3Widget.xaml</DependentUpon>
</Compile> </Compile>
@ -184,6 +187,10 @@
<SubType>Designer</SubType> <SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
</Page> </Page>
<Page Include="CreateWARCWidget.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="CreateXP3Widget.xaml"> <Page Include="CreateXP3Widget.xaml">
<SubType>Designer</SubType> <SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>

View File

@ -23,8 +23,12 @@
// IN THE SOFTWARE. // IN THE SOFTWARE.
// //
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.Composition; using System.ComponentModel.Composition;
using System.IO;
using GameRes.Formats.Properties;
using GameRes.Formats.Strings;
namespace GameRes.Formats.Will namespace GameRes.Formats.Will
{ {
@ -35,14 +39,24 @@ namespace GameRes.Formats.Will
public uint DirOffset; public uint DirOffset;
} }
public class ArcOptions : ResourceOptions
{
public int NameLength { get; set; }
}
[Export(typeof(ArchiveFormat))] [Export(typeof(ArchiveFormat))]
public class ArcOpener : ArchiveFormat public class ArcOpener : ArchiveFormat
{ {
public override string Tag { get { return "ARC"; } } public override string Tag { get { return "WARC"; } }
public override string Description { get { return "Will Co. game engine resource archive"; } } public override string Description { get { return "Will Co. game engine resource archive"; } }
public override uint Signature { get { return 0; } } public override uint Signature { get { return 0; } }
public override bool IsHierarchic { get { return false; } } public override bool IsHierarchic { get { return false; } }
public override bool CanCreate { get { return false; } } public override bool CanCreate { get { return true; } }
ArcOpener ()
{
Extensions = new string[] { "arc" };
}
public override ArcFile TryOpen (ArcView file) public override ArcFile TryOpen (ArcView file)
{ {
@ -52,16 +66,14 @@ namespace GameRes.Formats.Will
uint dir_offset = 4; uint dir_offset = 4;
var ext_list = new List<ExtRecord> (ext_count); var ext_list = new List<ExtRecord> (ext_count);
int file_count = 0;
for (int i = 0; i < ext_count; ++i) for (int i = 0; i < ext_count; ++i)
{ {
string ext = file.View.ReadString (dir_offset, 4).ToLowerInvariant(); string ext = file.View.ReadString (dir_offset, 4).ToLowerInvariant();
int count = file.View.ReadInt32 (dir_offset+4); int count = file.View.ReadInt32 (dir_offset+4);
uint offset = file.View.ReadUInt32 (dir_offset+8); uint offset = file.View.ReadUInt32 (dir_offset+8);
if (count <= 0 || count > 0xffff || offset <= dir_offset) if (count <= 0 || count > 0xffff || offset <= dir_offset || offset > file.MaxOffset)
return null; return null;
ext_list.Add (new ExtRecord { Extension = ext, FileCount = count, DirOffset = offset }); ext_list.Add (new ExtRecord { Extension = ext, FileCount = count, DirOffset = offset });
file_count += count;
dir_offset += 12; dir_offset += 12;
} }
var dir = ReadFileList (file, ext_list, 9); var dir = ReadFileList (file, ext_list, 9);
@ -77,10 +89,14 @@ namespace GameRes.Formats.Will
var dir = new List<Entry>(); var dir = new List<Entry>();
foreach (var ext in ext_list) foreach (var ext in ext_list)
{ {
dir.Capacity = dir.Count + ext.FileCount;
uint dir_offset = ext.DirOffset; uint dir_offset = ext.DirOffset;
for (int i = 0; i < ext.FileCount; ++i) for (int i = 0; i < ext.FileCount; ++i)
{ {
string name = file.View.ReadString (dir_offset, name_size).ToLowerInvariant()+'.'+ext.Extension; string name = file.View.ReadString (dir_offset, name_size);
if (string.IsNullOrEmpty (name))
return null;
name = name.ToLowerInvariant()+'.'+ext.Extension;
var entry = FormatCatalog.Instance.CreateEntry (name); var entry = FormatCatalog.Instance.CreateEntry (name);
entry.Size = file.View.ReadUInt32 (dir_offset+name_size); entry.Size = file.View.ReadUInt32 (dir_offset+name_size);
entry.Offset = file.View.ReadUInt32 (dir_offset+name_size+4); entry.Offset = file.View.ReadUInt32 (dir_offset+name_size+4);
@ -92,5 +108,119 @@ namespace GameRes.Formats.Will
} }
return dir; return dir;
} }
public override ResourceOptions GetDefaultOptions ()
{
return new ArcOptions { NameLength = Settings.Default.WARCNameLength };
}
public override object GetCreationWidget ()
{
return new GUI.CreateWARCWidget();
}
internal class ArcEntry : Entry
{
public byte[] RawName;
}
internal class ArcDirectory
{
public byte[] Extension;
public uint DirOffset;
public List<ArcEntry> Files;
}
public override void Create (Stream output, IEnumerable<Entry> list, ResourceOptions options,
EntryCallback callback)
{
var arc_options = GetOptions<ArcOptions> (options);
var encoding = Encodings.cp932.WithFatalFallback();
int file_count = 0;
var file_table = new SortedDictionary<string, ArcDirectory>();
foreach (var entry in list)
{
string ext = Path.GetExtension (entry.Name).TrimStart ('.').ToUpperInvariant();
if (string.IsNullOrEmpty (ext))
throw new InvalidFileName (entry.Name, arcStrings.MsgNoExtension);
if (ext.Length > 3)
throw new InvalidFileName (entry.Name, arcStrings.MsgExtensionTooLong);
string name = Path.GetFileNameWithoutExtension (entry.Name).ToUpperInvariant();
byte[] raw_name = encoding.GetBytes (name);
if (raw_name.Length > arc_options.NameLength)
throw new InvalidFileName (entry.Name, arcStrings.MsgFileNameTooLong);
ArcDirectory dir;
if (!file_table.TryGetValue (ext, out dir))
{
byte[] raw_ext = encoding.GetBytes (ext);
if (raw_ext.Length > 3)
throw new InvalidFileName (entry.Name, arcStrings.MsgExtensionTooLong);
dir = new ArcDirectory { Extension = raw_ext, Files = new List<ArcEntry>() };
file_table[ext] = dir;
}
dir.Files.Add (new ArcEntry { Name = entry.Name, RawName = raw_name });
++file_count;
}
if (null != callback)
callback (file_count+1, null, null);
int callback_count = 0;
long dir_offset = 4 + file_table.Count * 12;
long data_offset = dir_offset + (arc_options.NameLength + 9) * file_count;
output.Position = data_offset;
foreach (var ext in file_table.Keys)
{
var dir = file_table[ext];
dir.DirOffset = (uint)dir_offset;
dir_offset += (arc_options.NameLength + 9) * dir.Files.Count;
foreach (var entry in dir.Files)
{
if (null != callback)
callback (callback_count++, entry, arcStrings.MsgAddingFile);
entry.Offset = data_offset;
using (var input = File.OpenRead (entry.Name))
{
var size = input.Length;
if (size > uint.MaxValue || data_offset + size > uint.MaxValue)
throw new FileSizeException();
data_offset += size;
entry.Size = (uint)size;
input.CopyTo (output);
}
}
}
if (null != callback)
callback (callback_count++, null, arcStrings.MsgWritingIndex);
output.Position = 0;
using (var header = new BinaryWriter (output, encoding, true))
{
byte[] buffer = new byte[arc_options.NameLength+1];
header.Write (file_table.Count);
foreach (var ext in file_table)
{
Array.Copy (ext.Value.Extension, buffer, ext.Value.Extension.Length);
for (int i = ext.Value.Extension.Length; i < 4; ++i)
buffer[i] = 0;
header.Write (buffer, 0, 4);
header.Write (ext.Value.Files.Count);
header.Write (ext.Value.DirOffset);
}
foreach (var ext in file_table)
{
foreach (var entry in ext.Value.Files)
{
Array.Copy (entry.RawName, buffer, entry.RawName.Length);
for (int i = entry.RawName.Length; i < buffer.Length; ++i)
buffer[i] = 0;
header.Write (buffer);
header.Write (entry.Size);
header.Write ((uint)entry.Offset);
}
}
}
}
} }
} }

View File

@ -0,0 +1,16 @@
<Grid x:Class="GameRes.Formats.GUI.CreateWARCWidget"
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">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Content="{x:Static s:arcStrings.WARCLabelLength}" Target="{Binding ElementName=NameLength}" Grid.Column="0" Margin="0"/>
<ComboBox Name="NameLength" Width="40" SelectedValuePath="Content" Grid.Column="1" Margin="8"
SelectedValue="{Binding Source={x:Static p:Settings.Default}, Path=WARCNameLength, Mode=TwoWay}">
<ComboBoxItem Content="8"/>
<ComboBoxItem Content="12"/>
</ComboBox>
</Grid>

View File

@ -0,0 +1,15 @@
using System.Windows.Controls;
namespace GameRes.Formats.GUI
{
/// <summary>
/// Interaction logic for CreateWARCWidget.xaml
/// </summary>
public partial class CreateWARCWidget : Grid
{
public CreateWARCWidget ()
{
InitializeComponent ();
}
}
}

View File

@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion ("1.0.2.20")] [assembly: AssemblyVersion ("1.0.2.21")]
[assembly: AssemblyFileVersion ("1.0.2.20")] [assembly: AssemblyFileVersion ("1.0.2.21")]

View File

@ -237,5 +237,17 @@ namespace GameRes.Formats.Properties {
this["NPAKey2"] = value; this["NPAKey2"] = value;
} }
} }
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("8")]
public int WARCNameLength {
get {
return ((int)(this["WARCNameLength"]));
}
set {
this["WARCNameLength"] = value;
}
}
} }
} }

View File

@ -56,5 +56,8 @@
<Setting Name="NPAKey2" Type="System.UInt32" Scope="User"> <Setting Name="NPAKey2" Type="System.UInt32" Scope="User">
<Value Profile="(Default)">555831124</Value> <Value Profile="(Default)">555831124</Value>
</Setting> </Setting>
<Setting Name="WARCNameLength" Type="System.Int32" Scope="User">
<Value Profile="(Default)">8</Value>
</Setting>
</Settings> </Settings>
</SettingsFile> </SettingsFile>

View File

@ -279,6 +279,15 @@ namespace GameRes.Formats.Strings {
} }
} }
/// <summary>
/// Looks up a localized string similar to File name extension too long..
/// </summary>
public static string MsgExtensionTooLong {
get {
return ResourceManager.GetString("MsgExtensionTooLong", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to File name is too long. /// Looks up a localized string similar to File name is too long.
/// </summary> /// </summary>
@ -315,6 +324,15 @@ namespace GameRes.Formats.Strings {
} }
} }
/// <summary>
/// Looks up a localized string similar to File name without extension..
/// </summary>
public static string MsgNoExtension {
get {
return ResourceManager.GetString("MsgNoExtension", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to Number of files exceedes archive limit.. /// Looks up a localized string similar to Number of files exceedes archive limit..
/// </summary> /// </summary>
@ -487,6 +505,16 @@ namespace GameRes.Formats.Strings {
} }
} }
/// <summary>
/// Looks up a localized string similar to Maximum file name length
///(not including extension).
/// </summary>
public static string WARCLabelLength {
get {
return ResourceManager.GetString("WARCLabelLength", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to Liar-soft game resource archive. /// Looks up a localized string similar to Liar-soft game resource archive.
/// </summary> /// </summary>

View File

@ -192,6 +192,9 @@ predefined encryption scheme.</value>
<data name="MsgEncNotImplemented" xml:space="preserve"> <data name="MsgEncNotImplemented" xml:space="preserve">
<value>Encryption method not implemented</value> <value>Encryption method not implemented</value>
</data> </data>
<data name="MsgExtensionTooLong" xml:space="preserve">
<value>File name extension too long.</value>
</data>
<data name="MsgFileNameTooLong" xml:space="preserve"> <data name="MsgFileNameTooLong" xml:space="preserve">
<value>File name is too long</value> <value>File name is too long</value>
</data> </data>
@ -204,6 +207,9 @@ predefined encryption scheme.</value>
<data name="MsgInvalidVersion" xml:space="preserve"> <data name="MsgInvalidVersion" xml:space="preserve">
<value>Invalid archive version specified.</value> <value>Invalid archive version specified.</value>
</data> </data>
<data name="MsgNoExtension" xml:space="preserve">
<value>File name without extension.</value>
</data>
<data name="MsgTooManyFiles" xml:space="preserve"> <data name="MsgTooManyFiles" xml:space="preserve">
<value>Number of files exceedes archive limit.</value> <value>Number of files exceedes archive limit.</value>
</data> </data>
@ -262,6 +268,10 @@ predefined encryption scheme.</value>
<data name="TooltipHex" xml:space="preserve"> <data name="TooltipHex" xml:space="preserve">
<value>Hex number</value> <value>Hex number</value>
</data> </data>
<data name="WARCLabelLength" xml:space="preserve">
<value>Maximum file name length
(not including extension)</value>
</data>
<data name="XFLDescription" xml:space="preserve"> <data name="XFLDescription" xml:space="preserve">
<value>Liar-soft game resource archive</value> <value>Liar-soft game resource archive</value>
</data> </data>

View File

@ -174,6 +174,9 @@
<data name="MsgEncNotImplemented" xml:space="preserve"> <data name="MsgEncNotImplemented" xml:space="preserve">
<value>Метод шифрования не реализован</value> <value>Метод шифрования не реализован</value>
</data> </data>
<data name="MsgExtensionTooLong" xml:space="preserve">
<value>Слишком длинное расширение имени файла.</value>
</data>
<data name="MsgFileNameTooLong" xml:space="preserve"> <data name="MsgFileNameTooLong" xml:space="preserve">
<value>Слишком длинное имя файла</value> <value>Слишком длинное имя файла</value>
</data> </data>
@ -186,6 +189,9 @@
<data name="MsgInvalidVersion" xml:space="preserve"> <data name="MsgInvalidVersion" xml:space="preserve">
<value>Указана некорректная версия архива.</value> <value>Указана некорректная версия архива.</value>
</data> </data>
<data name="MsgNoExtension" xml:space="preserve">
<value>Имя файла без расширения.</value>
</data>
<data name="MsgTooManyFiles" xml:space="preserve"> <data name="MsgTooManyFiles" xml:space="preserve">
<value>Количество файлов превышает ограничения архива.</value> <value>Количество файлов превышает ограничения архива.</value>
</data> </data>
@ -226,6 +232,10 @@
<data name="TooltipHex" xml:space="preserve"> <data name="TooltipHex" xml:space="preserve">
<value>Шестнадцатеричное число</value> <value>Шестнадцатеричное число</value>
</data> </data>
<data name="WARCLabelLength" xml:space="preserve">
<value>Максимальная длина имени файла
(не считая расширения)</value>
</data>
<data name="XP3CompressContents" xml:space="preserve"> <data name="XP3CompressContents" xml:space="preserve">
<value>Сжать содержимое</value> <value>Сжать содержимое</value>
</data> </data>

View File

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