[Enums, FileTypes/, Utilities] Use new enum, new class

Add a new "BaseFile" class for potential future use. This should be the "base" for all future files and folders that could exist. This is likely to change over time.
This commit is contained in:
Matt Nadareski
2018-02-15 22:06:20 -08:00
parent 336388c717
commit 3a45dcc1a5
18 changed files with 192 additions and 36 deletions

View File

@@ -9,20 +9,16 @@ using System.IO;
using MemoryStream = System.IO.MemoryStream;
using Stream = System.IO.Stream;
#endif
using SharpCompress.Common;
namespace SabreTools.Library.FileTypes
{
public abstract class BaseArchive
public abstract class BaseArchive : BaseFile
{
#region Protected instance variables
// Buffer size used by archives
protected const int _bufferSize = 4096 * 128;
protected ArchiveType _archiveType;
protected string _filename;
#endregion
#region Construtors
@@ -39,8 +35,8 @@ namespace SabreTools.Library.FileTypes
/// </summary>
/// <param name="filename">Name of the file to use as an archive</param>
public BaseArchive(string filename)
: base(filename)
{
_filename = filename;
}
#endregion

View File

@@ -0,0 +1,92 @@
using System.Collections.Generic;
using SabreTools.Library.Data;
namespace SabreTools.Library.FileTypes
{
public abstract class BaseFile
{
#region Protected instance variables
protected FileType _fileType;
protected string _filename;
protected List<BaseFile> _children;
// External hash values for the file
protected long? _size;
protected byte[] _crc;
protected byte[] _md5;
protected byte[] _sha1;
protected byte[] _sha256;
protected byte[] _sha384;
protected byte[] _sha512;
#endregion
#region Publicly facing variables
// TODO: Get all of these values automatically so there is no public "set"
public string Filename
{
get { return _filename; }
set { _filename = value; }
}
public long? Size
{
get { return _size; }
set { _size = value; }
}
public byte[] CRC
{
get { return _crc; }
set { _crc = value; }
}
public byte[] MD5
{
get { return _md5; }
set { _md5 = value; }
}
public byte[] SHA1
{
get { return _sha1; }
set { _sha1 = value; }
}
public byte[] SHA256
{
get { return _sha256; }
set { _sha256 = value; }
}
public byte[] SHA384
{
get { return _sha384; }
set { _sha384 = value; }
}
public byte[] SHA512
{
get { return _sha512; }
set { _sha512 = value; }
}
#endregion
#region Construtors
/// <summary>
/// Create a new Archive with no base file
/// </summary>
public BaseFile()
{
}
/// <summary>
/// Create a new Archive from the given file
/// </summary>
/// <param name="filename">Name of the file to use as an archive</param>
public BaseFile(string filename)
{
_filename = filename;
}
#endregion
}
}

View File

@@ -88,7 +88,7 @@ namespace SabreTools.Library.FileTypes
/// 0x68-0x7b - Parent SHA-1
/// ----------------------------------------------
/// </remarks>
public class CHDFile : IDisposable
public class CHDFile : BaseFile, IDisposable
{
#region Private instance variables
@@ -122,6 +122,7 @@ namespace SabreTools.Library.FileTypes
/// <param name="chdstream">Stream representing the CHD file</param>
public CHDFile(Stream chdstream)
{
_fileType = FileType.CHD;
m_br = new BinaryReader(chdstream);
}

View File

@@ -1,7 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using SabreTools.Library.Data;
using SabreTools.Library.DatItems;
/// <summary>
/// This code is based on the header format described at http://www.rarlab.com/technote.htm#srvheaders
@@ -116,7 +117,7 @@ using SabreTools.Library.Data;
/// </remarks>
namespace SabreTools.Library.FileTypes
{
public class CoreRarArchive
public class CoreRarArchive : BaseArchive
{
// SFX Module Information
public byte[] SFX;
@@ -148,6 +149,55 @@ namespace SabreTools.Library.FileTypes
// Entry Information
public List<CoreRarArchiveEntry> Entries = new List<CoreRarArchiveEntry>();
#region Unimplemented methods
public override bool ExtractAll(string outDir)
{
throw new NotImplementedException();
}
public override string ExtractEntry(string entryName, string outDir)
{
throw new NotImplementedException();
}
public override (MemoryStream, string) ExtractEntryStream(string entryName)
{
throw new NotImplementedException();
}
public override List<Rom> GetArchiveFileInfo(Hash omitFromScan = Hash.DeepHashes, bool date = false)
{
throw new NotImplementedException();
}
public override List<string> GetEmptyFolders()
{
throw new NotImplementedException();
}
public override bool IsTorrent()
{
throw new NotImplementedException();
}
public override bool Write(string inputFile, string outDir, Rom rom, bool date = false, bool romba = false)
{
throw new NotImplementedException();
}
public override bool Write(Stream inputStream, string outDir, Rom rom, bool date = false, bool romba = false)
{
throw new NotImplementedException();
}
public override bool Write(List<string> inputFiles, string outDir, List<Rom> roms, bool date = false, bool romba = false)
{
throw new NotImplementedException();
}
#endregion
}
public class CoreRarArchiveEntry

View File

@@ -37,6 +37,7 @@ namespace SabreTools.Library.FileTypes
public Folder()
: base()
{
_fileType = FileType.Folder;
}
/// <summary>
@@ -47,6 +48,7 @@ namespace SabreTools.Library.FileTypes
public Folder(string filename)
: base(filename)
{
_fileType = FileType.Folder;
}
#endregion

View File

@@ -21,7 +21,6 @@ using SeekOrigin = System.IO.SeekOrigin;
using Stream = System.IO.Stream;
#endif
using Ionic.Zlib;
using SharpCompress.Common;
namespace SabreTools.Library.FileTypes
{
@@ -38,6 +37,7 @@ namespace SabreTools.Library.FileTypes
public GZipArchive()
: base()
{
_fileType = FileType.GZipArchive;
}
/// <summary>
@@ -48,7 +48,7 @@ namespace SabreTools.Library.FileTypes
public GZipArchive(string filename)
: base(filename)
{
_archiveType = ArchiveType.GZip;
_fileType = FileType.GZipArchive;
}
#endregion

View File

@@ -27,6 +27,7 @@ namespace SabreTools.Library.FileTypes
public LRZipArchive()
: base()
{
_fileType = FileType.LRZipArchive;
}
/// <summary>
@@ -36,7 +37,7 @@ namespace SabreTools.Library.FileTypes
public LRZipArchive(string filename)
: base(filename)
{
//_archiveType = ArchiveType.LRZip;
_fileType = FileType.LRZipArchive;
}
#endregion

View File

@@ -27,6 +27,7 @@ namespace SabreTools.Library.FileTypes
public LZ4Archive()
: base()
{
_fileType = FileType.LZ4Archive;
}
/// <summary>
@@ -36,7 +37,7 @@ namespace SabreTools.Library.FileTypes
public LZ4Archive(string filename)
: base(filename)
{
//_archiveType = ArchiveType.LRZip;
_fileType = FileType.LZ4Archive;
}
#endregion

View File

@@ -20,7 +20,6 @@ using Stream = System.IO.Stream;
#endif
using SharpCompress.Archives;
using SharpCompress.Archives.Rar;
using SharpCompress.Common;
using SharpCompress.Readers;
namespace SabreTools.Library.FileTypes
@@ -38,6 +37,7 @@ namespace SabreTools.Library.FileTypes
public RarArchive()
: base()
{
_fileType = FileType.RarArchive;
}
/// <summary>
@@ -48,7 +48,7 @@ namespace SabreTools.Library.FileTypes
public RarArchive(string filename)
: base(filename)
{
_archiveType = ArchiveType.Rar;
_fileType = FileType.RarArchive;
}
#endregion
@@ -252,13 +252,13 @@ namespace SabreTools.Library.FileTypes
// Check for the signature first (Skipping the SFX Module)
byte[] signature = br.ReadBytes(8);
int startpos = 0;
while (startpos < Constants.MibiByte && BitConverter.ToString(signature, 0, 7) != Constants.RarSignature && BitConverter.ToString(signature) != Constants.RarFiveSig)
while (startpos < Constants.MibiByte && !signature.StartsWith(Constants.RarSignature, exact: true) && !signature.StartsWith(Constants.RarFiveSignature, exact: true))
{
startpos++;
br.BaseStream.Position = startpos;
signature = br.ReadBytes(8);
}
if (BitConverter.ToString(signature, 0, 7) != Constants.RarSignature && BitConverter.ToString(signature) != Constants.RarFiveSig)
if (!signature.StartsWith(Constants.RarSignature, exact: true) && !signature.StartsWith(Constants.RarFiveSignature, exact: true))
{
return;
}

View File

@@ -22,7 +22,6 @@ using ROMVault2.SupportedFiles.Zip;
using SevenZip; // TODO: Remove this when 7zip write is implemented in SharpCompress
using SharpCompress.Archives;
using SharpCompress.Archives.SevenZip;
using SharpCompress.Common;
using SharpCompress.Readers;
namespace SabreTools.Library.FileTypes
@@ -41,6 +40,7 @@ namespace SabreTools.Library.FileTypes
public SevenZipArchive()
: base()
{
_fileType = FileType.SevenZipArchive;
}
/// <summary>
@@ -51,7 +51,7 @@ namespace SabreTools.Library.FileTypes
public SevenZipArchive(string filename)
: base(filename)
{
_archiveType = ArchiveType.SevenZip;
_fileType = FileType.SevenZipArchive;
}
#endregion

View File

@@ -40,6 +40,7 @@ namespace SabreTools.Library.FileTypes
public TapeArchive()
: base()
{
_fileType = FileType.TapeArchive;
}
/// <summary>
@@ -50,7 +51,7 @@ namespace SabreTools.Library.FileTypes
public TapeArchive(string filename)
: base(filename)
{
_archiveType = ArchiveType.Tar;
_fileType = FileType.TapeArchive;
}
#endregion

View File

@@ -36,6 +36,7 @@ namespace SabreTools.Library.FileTypes
public TorrentZipArchive()
: base()
{
_fileType = FileType.ZipArchive;
}
/// <summary>
@@ -46,7 +47,7 @@ namespace SabreTools.Library.FileTypes
public TorrentZipArchive(string filename)
: base(filename)
{
_archiveType = ArchiveType.Zip;
_fileType = FileType.ZipArchive;
}
#endregion

View File

@@ -40,6 +40,7 @@ namespace SabreTools.Library.FileTypes
public XZArchive()
: base()
{
_fileType = FileType.XZArchive;
}
/// <summary>
@@ -50,7 +51,7 @@ namespace SabreTools.Library.FileTypes
public XZArchive(string filename)
: base(filename)
{
//_archiveType = ArchiveType.XZip;
_fileType = FileType.XZArchive;
}
#endregion

View File

@@ -27,6 +27,7 @@ namespace SabreTools.Library.FileTypes
public ZPAQArchive()
: base()
{
_fileType = FileType.ZPAQArchive;
}
/// <summary>
@@ -36,7 +37,7 @@ namespace SabreTools.Library.FileTypes
public ZPAQArchive(string filename)
: base(filename)
{
//_archiveType = ArchiveType.LRZip;
_fileType = FileType.ZPAQArchive;
}
#endregion

View File

@@ -27,6 +27,7 @@ namespace SabreTools.Library.FileTypes
public ZstdArchive()
: base()
{
_fileType = FileType.ZstdArchive;
}
/// <summary>
@@ -36,7 +37,7 @@ namespace SabreTools.Library.FileTypes
public ZstdArchive(string filename)
: base(filename)
{
//_archiveType = ArchiveType.LRZip;
_fileType = FileType.ZstdArchive;
}
#endregion