2014-07-22 03:26:28 +08:00
|
|
|
|
//! \file GameRes.cs
|
|
|
|
|
//! \date Mon Jun 30 20:12:13 2014
|
|
|
|
|
//! \brief game resources browser.
|
|
|
|
|
//
|
2015-05-14 19:58:16 +08:00
|
|
|
|
// Copyright (C) 2014-2015 by morkt
|
2014-07-28 04:50:18 +08:00
|
|
|
|
//
|
|
|
|
|
// 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.
|
|
|
|
|
//
|
2014-07-22 03:26:28 +08:00
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using GameRes.Strings;
|
|
|
|
|
|
|
|
|
|
namespace GameRes
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Basic filesystem entry.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class Entry
|
|
|
|
|
{
|
|
|
|
|
public virtual string Name { get; set; }
|
|
|
|
|
public virtual string Type { get; set; }
|
|
|
|
|
public long Offset { get; set; }
|
|
|
|
|
public uint Size { get; set; }
|
|
|
|
|
|
|
|
|
|
public Entry ()
|
|
|
|
|
{
|
|
|
|
|
Type = "";
|
|
|
|
|
Offset = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Check whether entry lies within specified file bound.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool CheckPlacement (long max_offset)
|
|
|
|
|
{
|
2015-11-26 06:23:24 +08:00
|
|
|
|
return Offset < max_offset && Size <= max_offset && Offset <= max_offset - Size;
|
2014-07-22 03:26:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class PackedEntry : Entry
|
|
|
|
|
{
|
|
|
|
|
public uint UnpackedSize { get; set; }
|
|
|
|
|
public bool IsPacked { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public abstract class IResource
|
|
|
|
|
{
|
|
|
|
|
/// <summary>Short tag sticked to resource (usually filename extension)</summary>
|
|
|
|
|
public abstract string Tag { get; }
|
|
|
|
|
|
|
|
|
|
/// <summary>Resource description (its source/inventor)</summary>
|
|
|
|
|
public abstract string Description { get; }
|
|
|
|
|
|
|
|
|
|
/// <summary>Resource type (image/archive/script)</summary>
|
|
|
|
|
public abstract string Type { get; }
|
|
|
|
|
|
|
|
|
|
/// <summary>First 4 bytes of the resource file as little-endian 32-bit integer,
|
|
|
|
|
/// or zero if it could vary.</summary>
|
|
|
|
|
public abstract uint Signature { get; }
|
|
|
|
|
|
2016-10-11 04:05:22 +08:00
|
|
|
|
/// <summary>Whether resource creation is supported by implementation.</summary>
|
|
|
|
|
public virtual bool CanWrite { get { return false; } }
|
|
|
|
|
|
2014-07-22 03:26:28 +08:00
|
|
|
|
/// <summary>Signatures peculiar to the resource (the one above is also included here).</summary>
|
2015-04-06 21:08:32 +08:00
|
|
|
|
public IEnumerable<uint> Signatures { get; protected set; }
|
2014-07-22 03:26:28 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>Filename extensions peculiar to the resource.</summary>
|
2015-04-06 21:08:32 +08:00
|
|
|
|
public IEnumerable<string> Extensions { get; protected set; }
|
2014-07-22 03:26:28 +08:00
|
|
|
|
|
2015-09-18 05:27:53 +08:00
|
|
|
|
/// <summary>Resource settings suitable for serialization.</summary>
|
|
|
|
|
public virtual ResourceScheme Scheme { get; set; }
|
|
|
|
|
|
2014-07-22 03:26:28 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create empty Entry that corresponds to implemented resource.
|
|
|
|
|
/// </summary>
|
2015-08-31 02:34:06 +08:00
|
|
|
|
public EntryType Create<EntryType> () where EntryType : Entry, new()
|
2014-07-22 03:26:28 +08:00
|
|
|
|
{
|
2015-08-31 02:34:06 +08:00
|
|
|
|
return new EntryType { Type = this.Type };
|
2014-07-22 03:26:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected IResource ()
|
|
|
|
|
{
|
2016-09-08 00:26:39 +08:00
|
|
|
|
Extensions = new string[] { GetDefaultExtension() };
|
2015-04-06 21:08:32 +08:00
|
|
|
|
Signatures = new uint[] { this.Signature };
|
2014-07-22 03:26:28 +08:00
|
|
|
|
}
|
2015-08-12 05:31:03 +08:00
|
|
|
|
|
2016-09-08 00:26:39 +08:00
|
|
|
|
protected string GetDefaultExtension ()
|
|
|
|
|
{
|
|
|
|
|
var ext = Tag.ToLowerInvariant();
|
|
|
|
|
int slash = ext.IndexOf ('/');
|
|
|
|
|
if (slash != -1)
|
|
|
|
|
ext = ext.Substring (0, slash);
|
|
|
|
|
return ext;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-12 05:31:03 +08:00
|
|
|
|
public virtual ResourceOptions GetDefaultOptions ()
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual ResourceOptions GetOptions (object widget)
|
|
|
|
|
{
|
|
|
|
|
return GetDefaultOptions();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual object GetCreationWidget ()
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual object GetAccessWidget ()
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected OptType GetOptions<OptType> (ResourceOptions res_options) where OptType : ResourceOptions
|
|
|
|
|
{
|
|
|
|
|
var options = res_options as OptType;
|
|
|
|
|
if (null == options)
|
|
|
|
|
options = this.GetDefaultOptions() as OptType;
|
|
|
|
|
return options;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected OptType Query<OptType> (string notice) where OptType : ResourceOptions
|
|
|
|
|
{
|
|
|
|
|
var args = new ParametersRequestEventArgs { Notice = notice };
|
|
|
|
|
FormatCatalog.Instance.InvokeParametersRequest (this, args);
|
|
|
|
|
if (!args.InputResult)
|
|
|
|
|
throw new OperationCanceledException();
|
|
|
|
|
|
|
|
|
|
return GetOptions<OptType> (args.Options);
|
|
|
|
|
}
|
2014-07-22 03:26:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ResourceOptions
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-18 05:27:53 +08:00
|
|
|
|
[Serializable]
|
|
|
|
|
public class ResourceScheme
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-27 07:37:51 +08:00
|
|
|
|
public enum ArchiveOperation
|
|
|
|
|
{
|
|
|
|
|
Abort,
|
|
|
|
|
Skip,
|
|
|
|
|
Continue,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public delegate ArchiveOperation EntryCallback (int num, Entry entry, string description);
|
|
|
|
|
|
2014-07-22 03:26:28 +08:00
|
|
|
|
public abstract class ArchiveFormat : IResource
|
|
|
|
|
{
|
|
|
|
|
public override string Type { get { return "archive"; } }
|
|
|
|
|
|
2016-10-11 04:05:22 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether archive file system could contain subdirectories.
|
|
|
|
|
/// </summary>
|
2014-07-24 09:43:20 +08:00
|
|
|
|
public abstract bool IsHierarchic { get; }
|
|
|
|
|
|
2014-07-22 03:26:28 +08:00
|
|
|
|
public abstract ArcFile TryOpen (ArcView view);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Extract file referenced by <paramref name="entry"/> into current directory.
|
|
|
|
|
/// </summary>
|
2014-07-29 11:01:36 +08:00
|
|
|
|
public void Extract (ArcFile file, Entry entry)
|
2014-07-22 03:26:28 +08:00
|
|
|
|
{
|
2016-08-02 09:17:25 +08:00
|
|
|
|
using (var input = OpenEntry (file, entry))
|
|
|
|
|
using (var output = CreateFile (entry.Name))
|
|
|
|
|
input.CopyTo (output);
|
2014-07-22 03:26:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Open file referenced by <paramref name="entry"/> as Stream.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public virtual Stream OpenEntry (ArcFile arc, Entry entry)
|
|
|
|
|
{
|
2016-10-16 13:22:53 +08:00
|
|
|
|
return arc.File.CreateStream (entry.Offset, entry.Size, entry.Name);
|
2014-07-22 03:26:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create file corresponding to <paramref name="entry"/> in current directory and open it
|
2014-07-29 11:01:36 +08:00
|
|
|
|
/// for writing. Overwrites existing file, if any.
|
2014-07-22 03:26:28 +08:00
|
|
|
|
/// </summary>
|
2015-05-14 19:58:16 +08:00
|
|
|
|
static public Stream CreateFile (string filename)
|
2014-09-12 20:03:30 +08:00
|
|
|
|
{
|
|
|
|
|
filename = CreatePath (filename);
|
|
|
|
|
if (File.Exists (filename))
|
|
|
|
|
{
|
|
|
|
|
// query somehow whether to overwrite existing file or not.
|
|
|
|
|
}
|
|
|
|
|
return File.Create (filename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static public string CreatePath (string filename)
|
2014-07-22 03:26:28 +08:00
|
|
|
|
{
|
2014-07-29 11:01:36 +08:00
|
|
|
|
string dir = Path.GetDirectoryName (filename);
|
|
|
|
|
if (!string.IsNullOrEmpty (dir)) // check for malformed filenames
|
2014-07-22 03:26:28 +08:00
|
|
|
|
{
|
2014-07-29 11:01:36 +08:00
|
|
|
|
string root = Path.GetPathRoot (dir);
|
|
|
|
|
if (!string.IsNullOrEmpty (root))
|
|
|
|
|
{
|
|
|
|
|
dir = dir.Substring (root.Length); // strip root
|
|
|
|
|
}
|
|
|
|
|
string cwd = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar;
|
|
|
|
|
dir = Path.GetFullPath (dir);
|
|
|
|
|
filename = Path.GetFileName (filename);
|
|
|
|
|
// check whether filename would reside within current directory
|
|
|
|
|
if (dir.StartsWith (cwd, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
// path looks legit, create it
|
|
|
|
|
Directory.CreateDirectory (dir);
|
|
|
|
|
filename = Path.Combine (dir, filename);
|
|
|
|
|
}
|
2014-07-22 03:26:28 +08:00
|
|
|
|
}
|
2014-09-12 20:03:30 +08:00
|
|
|
|
return filename;
|
2014-07-22 03:26:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-24 09:43:20 +08:00
|
|
|
|
/// <summary>
|
2014-07-29 11:01:36 +08:00
|
|
|
|
/// Create resource within stream <paramref name="file"/> containing entries from the
|
2014-07-22 03:26:28 +08:00
|
|
|
|
/// supplied <paramref name="list"/> and applying necessary <paramref name="options"/>.
|
|
|
|
|
/// </summary>
|
2014-07-27 07:37:51 +08:00
|
|
|
|
public virtual void Create (Stream file, IEnumerable<Entry> list, ResourceOptions options = null,
|
|
|
|
|
EntryCallback callback = null)
|
2014-07-22 03:26:28 +08:00
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException ("ArchiveFormat.Create is not implemented");
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-19 10:39:55 +08:00
|
|
|
|
public static bool IsSaneCount (int count)
|
2015-07-11 14:19:46 +08:00
|
|
|
|
{
|
|
|
|
|
return count > 0 && count < 0x20000;
|
|
|
|
|
}
|
2014-07-22 03:26:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public delegate void ParametersRequestEventHandler (object sender, ParametersRequestEventArgs e);
|
|
|
|
|
|
|
|
|
|
public class ParametersRequestEventArgs : EventArgs
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// String describing request nature (encryption key etc).
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string Notice { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Return value from ShowDialog()
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool InputResult { get; set; }
|
2014-07-27 07:37:51 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Archive-specific options set by InputWidget.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public ResourceOptions Options { get; set; }
|
2014-07-22 03:26:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-31 14:48:27 +08:00
|
|
|
|
public class OverwriteEventArgs : EventArgs
|
|
|
|
|
{
|
|
|
|
|
public string Filename { get; set; }
|
|
|
|
|
public bool Overwrite { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-25 11:05:55 +08:00
|
|
|
|
public class InvalidFormatException : FileFormatException
|
2014-07-22 03:26:28 +08:00
|
|
|
|
{
|
|
|
|
|
public InvalidFormatException() : base(garStrings.MsgInvalidFormat) { }
|
|
|
|
|
public InvalidFormatException (string msg) : base (msg) { }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class UnknownEncryptionScheme : Exception
|
|
|
|
|
{
|
|
|
|
|
public UnknownEncryptionScheme() : base(garStrings.MsgUnknownEncryption) { }
|
|
|
|
|
public UnknownEncryptionScheme (string msg) : base (msg) { }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class InvalidEncryptionScheme : Exception
|
|
|
|
|
{
|
|
|
|
|
public InvalidEncryptionScheme() : base(garStrings.MsgInvalidEncryption) { }
|
|
|
|
|
public InvalidEncryptionScheme (string msg) : base (msg) { }
|
|
|
|
|
}
|
2014-07-25 11:05:55 +08:00
|
|
|
|
|
|
|
|
|
public class FileSizeException : Exception
|
|
|
|
|
{
|
|
|
|
|
public FileSizeException () : base (garStrings.MsgFileTooLarge) { }
|
|
|
|
|
public FileSizeException (string msg) : base (msg) { }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class InvalidFileName : Exception
|
|
|
|
|
{
|
|
|
|
|
public string FileName { get; set; }
|
|
|
|
|
|
|
|
|
|
public InvalidFileName (string filename)
|
|
|
|
|
: this (filename, garStrings.MsgInvalidFileName)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public InvalidFileName (string filename, string message)
|
|
|
|
|
: base (message)
|
|
|
|
|
{
|
|
|
|
|
FileName = filename;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public InvalidFileName (string filename, Exception X)
|
|
|
|
|
: this (filename, garStrings.MsgInvalidFileName, X)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public InvalidFileName (string filename, string message, Exception X)
|
|
|
|
|
: base (message, X)
|
|
|
|
|
{
|
|
|
|
|
FileName = filename;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-22 03:26:28 +08:00
|
|
|
|
}
|