From a585f759a34228682f9f49de2f47c8aa42d07e9a Mon Sep 17 00:00:00 2001 From: morkt Date: Mon, 5 Dec 2016 04:00:25 +0400 Subject: [PATCH] added 'experimental' project. --- Experimental/CellWorks/ArcDB.cs | 189 ++++++++++++++++++++++++ Experimental/Experimental.csproj | 82 ++++++++++ Experimental/Properties/AssemblyInfo.cs | 36 +++++ Experimental/packages.config | 4 + GARbro.sln | 5 + packages/repositories.config | 1 + 6 files changed, 317 insertions(+) create mode 100644 Experimental/CellWorks/ArcDB.cs create mode 100644 Experimental/Experimental.csproj create mode 100644 Experimental/Properties/AssemblyInfo.cs create mode 100644 Experimental/packages.config diff --git a/Experimental/CellWorks/ArcDB.cs b/Experimental/CellWorks/ArcDB.cs new file mode 100644 index 00000000..2befdabc --- /dev/null +++ b/Experimental/CellWorks/ArcDB.cs @@ -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 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(); + while (reader.Read()) + { + var name = reader.GetString (0); + var entry = FormatCatalog.Instance.Create (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); + } + } +} diff --git a/Experimental/Experimental.csproj b/Experimental/Experimental.csproj new file mode 100644 index 00000000..92fbf8c1 --- /dev/null +++ b/Experimental/Experimental.csproj @@ -0,0 +1,82 @@ + + + + + Debug + AnyCPU + {60054FD9-4472-4BB4-9E3D-2F80D3D22468} + Library + Properties + GameRes.Experimental + ArcExp + v4.5 + 512 + 5899a581 + + + true + full + false + ..\bin\Debug\ + DEBUG;TRACE + prompt + 4 + 16777216 + + + none + true + ..\bin\Release\ + + + prompt + 4 + 16777216 + + + + + + + ..\packages\System.Data.SQLite.Core.1.0.103\lib\net45\System.Data.SQLite.dll + True + + + + + + + + + + + + + + {a8865685-27cc-427b-ac38-e48d2ad05df4} + ArcFormats + + + {453c087f-e416-4ae9-8c03-d8760da0574b} + GameRes + + + + + + + + + + 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}. + + + + + \ No newline at end of file diff --git a/Experimental/Properties/AssemblyInfo.cs b/Experimental/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..543150ba --- /dev/null +++ b/Experimental/Properties/AssemblyInfo.cs @@ -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")] diff --git a/Experimental/packages.config b/Experimental/packages.config new file mode 100644 index 00000000..d7d349ea --- /dev/null +++ b/Experimental/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/GARbro.sln b/GARbro.sln index 0b4c70c6..ddfa9b0e 100644 --- a/GARbro.sln +++ b/GARbro.sln @@ -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 diff --git a/packages/repositories.config b/packages/repositories.config index 5ec48574..beb2ebb0 100644 --- a/packages/repositories.config +++ b/packages/repositories.config @@ -1,6 +1,7 @@  + \ No newline at end of file