added 'experimental' project.

This commit is contained in:
morkt 2016-12-05 04:00:25 +04:00
parent d2034f351d
commit a585f759a3
6 changed files with 317 additions and 0 deletions

View File

@ -0,0 +1,189 @@
//! \file ArcDB.cs
//! \date Sun Dec 04 23:39:13 2016
//! \brief ALL-TiME sqlite-backed resource archives.
//
// 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.Data.SQLite;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace GameRes.Formats.CellWorks
{
[Export(typeof(ArchiveFormat))]
public class IgsDatOpener : ArchiveFormat
{
public override string Tag { get { return "DAT/IGS"; } }
public override string Description { get { return "IGS engine resource archive"; } }
public override uint Signature { get { return 0; } }
public override bool IsHierarchic { get { return true; } }
public override bool CanWrite { get { return false; } }
internal static readonly string[] KnownPasswords = { "igs sample", "igs samp1e" };
public override ArcFile TryOpen (ArcView file)
{
if (!file.Name.EndsWith (".dat", StringComparison.InvariantCultureIgnoreCase))
return null;
var db_files = VFS.GetFiles (VFS.CombinePath (VFS.GetDirectoryName (file.Name), "*.db"));
if (!db_files.Any())
return null;
using (var igs = new IgsDbReader (file.Name))
{
foreach (var db_name in db_files.Select (e => e.Name))
{
int arc_id;
if (igs.GetArchiveId (db_name, out arc_id))
{
var dir = igs.ReadIndex (arc_id);
if (0 == dir.Count)
return null;
return new ArcFile (file, this, dir);
}
}
return null;
}
}
public override Stream OpenEntry (ArcFile arc, Entry entry)
{
using (var aes = Aes.Create())
{
var name_bytes = Encoding.UTF8.GetBytes (entry.Name);
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
aes.Key = CreateKey (32, name_bytes);
aes.IV = CreateKey (16, name_bytes);
using (var decryptor = aes.CreateDecryptor())
using (var enc = arc.File.CreateStream (entry.Offset, 0x110))
using (var input = new CryptoStream (enc, decryptor, CryptoStreamMode.Read))
{
var header = new byte[Math.Min (entry.Size, 0x100u)];
input.Read (header, 0, header.Length);
if (entry.Size <= 0x100)
return new BinMemoryStream (header);
var rest = arc.File.CreateStream (entry.Offset+0x110, entry.Size-0x100);
return new PrefixStream (header, rest);
}
}
}
internal static byte[] CreateKey (int length, byte[] src)
{
var key = new byte[length];
Buffer.BlockCopy (src, 0, key, 0, Math.Min (src.Length, length));
for (int i = length; i < src.Length; ++i)
key[i % length] ^= src[i];
return key;
}
}
internal sealed class IgsDbReader : IDisposable
{
SQLiteConnection m_conn;
SQLiteCommand m_arc_cmd;
public IgsDbReader (string arc_name)
{
m_conn = new SQLiteConnection();
m_arc_cmd = m_conn.CreateCommand();
m_arc_cmd.CommandText = @"SELECT id FROM archives WHERE name=?";
m_arc_cmd.Parameters.Add (m_arc_cmd.CreateParameter());
m_arc_cmd.Parameters[0].Value = Path.GetFileNameWithoutExtension (arc_name);
}
public bool GetArchiveId (string db_name, out int arc_id)
{
m_conn.ConnectionString = string.Format ("Data Source={0};Read Only=true;", db_name);
foreach (var password in IgsDatOpener.KnownPasswords)
{
m_conn.SetPassword (password);
m_conn.Open();
try
{
using (var reader = m_arc_cmd.ExecuteReader())
{
if (reader.Read())
{
arc_id = reader.GetInt32 (0);
return true;
}
}
// command executed successfully, but returned no rows
m_conn.Close();
break;
}
catch (SQLiteException)
{
// ignore open errors, try another password
}
m_conn.Close();
}
arc_id = -1;
return false;
}
public List<Entry> ReadIndex (int arc_id)
{
// tables: m_types file_infos images archives
// m_types: id type -> [normal, image, voice]
// archives: id name
// file_infos: id name filepath size offset typeID archiveID
using (var cmd = m_conn.CreateCommand())
{
cmd.CommandText = @"SELECT filepath,offset,size FROM file_infos WHERE archiveID=?";
cmd.Parameters.Add (cmd.CreateParameter());
cmd.Parameters[0].Value = arc_id;
using (var reader = cmd.ExecuteReader())
{
var dir = new List<Entry>();
while (reader.Read())
{
var name = reader.GetString (0);
var entry = FormatCatalog.Instance.Create<Entry> (name);
entry.Offset = reader.GetInt64 (1);
entry.Size = (uint)reader.GetInt32 (2);
dir.Add (entry);
}
return dir;
}
}
}
bool _disposed = false;
public void Dispose ()
{
if (!_disposed)
{
m_arc_cmd.Dispose();
m_conn.Dispose();
_disposed = true;
}
GC.SuppressFinalize (this);
}
}
}

View File

@ -0,0 +1,82 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{60054FD9-4472-4BB4-9E3D-2F80D3D22468}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>GameRes.Experimental</RootNamespace>
<AssemblyName>ArcExp</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<NuGetPackageImportStamp>5899a581</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<BaseAddress>16777216</BaseAddress>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\bin\Release\</OutputPath>
<DefineConstants>
</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<BaseAddress>16777216</BaseAddress>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Core" />
<Reference Include="System.Data.SQLite, Version=1.0.103.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL">
<HintPath>..\packages\System.Data.SQLite.Core.1.0.103\lib\net45\System.Data.SQLite.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="CellWorks\ArcDB.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ArcFormats\ArcFormats.csproj">
<Project>{a8865685-27cc-427b-ac38-e48d2ad05df4}</Project>
<Name>ArcFormats</Name>
</ProjectReference>
<ProjectReference Include="..\GameRes\GameRes.csproj">
<Project>{453c087f-e416-4ae9-8c03-d8760da0574b}</Project>
<Name>GameRes</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\System.Data.SQLite.Core.1.0.103\build\net45\System.Data.SQLite.Core.targets" Condition="Exists('..\packages\System.Data.SQLite.Core.1.0.103\build\net45\System.Data.SQLite.Core.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\System.Data.SQLite.Core.1.0.103\build\net45\System.Data.SQLite.Core.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\System.Data.SQLite.Core.1.0.103\build\net45\System.Data.SQLite.Core.targets'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle ("GameRes.Experimental")]
[assembly: AssemblyDescription ("Experimental implementations of game resources.")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany ("mørkt")]
[assembly: AssemblyProduct ("GameRes.Experimental")]
[assembly: AssemblyCopyright ("Copyright © 2016 mørkt")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("d9f6729a-adb8-4f4e-a929-9e2314d532c6")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="System.Data.SQLite.Core" version="1.0.103" targetFramework="net45" />
</packages>

View File

@ -25,6 +25,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Image.Convert", "Image.Conv
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SchemeBuilder", "SchemeBuilder\SchemeBuilder.csproj", "{B7E7EBFB-C06E-4FC8-9AF2-7CD132AB15FD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Experimental", "Experimental\Experimental.csproj", "{60054FD9-4472-4BB4-9E3D-2F80D3D22468}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -60,6 +62,9 @@ Global
{B7E7EBFB-C06E-4FC8-9AF2-7CD132AB15FD}.Prerelease|Any CPU.ActiveCfg = Prerelease|Any CPU
{B7E7EBFB-C06E-4FC8-9AF2-7CD132AB15FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B7E7EBFB-C06E-4FC8-9AF2-7CD132AB15FD}.Release|Any CPU.Build.0 = Release|Any CPU
{60054FD9-4472-4BB4-9E3D-2F80D3D22468}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{60054FD9-4472-4BB4-9E3D-2F80D3D22468}.Prerelease|Any CPU.ActiveCfg = Release|Any CPU
{60054FD9-4472-4BB4-9E3D-2F80D3D22468}.Release|Any CPU.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<repositories>
<repository path="..\ArcFormats\packages.config" />
<repository path="..\Experimental\packages.config" />
<repository path="..\GameRes\packages.config" />
<repository path="..\GUI\packages.config" />
</repositories>