Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from xaios, a better SFV (Simple File Verification) read library than the one in ReScene
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Globalization;
- using System.IO;
- using System.Text;
- namespace CustomTasks.Library
- {
- /// <summary>
- /// A library to read Simple File Verification files
- /// </summary>
- public class SFVReader
- {
- /// <summary>
- /// Convenience method to construct an SFVReader from an SFV file.
- /// </summary>
- /// <param name="fileName">
- /// The complete file path to be read.
- /// </param>
- public SFVReader(string fileName)
- {
- if (fileName == null)
- {
- throw new ArgumentNullException("fileName");
- }
- using (StreamReader sfvStreamReader = new StreamReader(fileName, Encoding.ASCII))
- {
- this.SFVEntries = _ParseSFVFile(sfvStreamReader);
- }
- }
- /// <summary>
- /// Constructs an SFVReader from a stream
- /// </summary>
- /// <param name="sfvStream">
- /// The stream to be read.
- /// </param>
- public SFVReader(Stream sfvStream)
- {
- if(sfvStream == null)
- {
- throw new ArgumentNullException("sfvStream");
- }
- using (StreamReader sfvStreamReader = new StreamReader(sfvStream))
- {
- this.SFVEntries = _ParseSFVFile(sfvStreamReader);
- }
- }
- /// <summary>
- /// The SFV Entries stored in this SFV File
- /// </summary>
- public ReadOnlyCollection<SFVEntry> SFVEntries
- {
- get;
- private set;
- }
- /// <summary>
- /// Parses a Stream that represents an SFV File
- /// </summary>
- /// <param name="sfvStream">
- /// A Stream representing an SFV file
- /// </param>
- /// <returns>
- /// A read-only collection of SFV Entries contained in this SFV stream
- /// </returns>
- private ReadOnlyCollection<SFVEntry> _ParseSFVFile(StreamReader sfvStream)
- {
- List<SFVEntry> sfvEntries = new List<SFVEntry>();
- string currentLine;
- while (!sfvStream.EndOfStream)
- {
- // Grab a line from the stream
- currentLine = sfvStream.ReadLine();
- //Trim Trailing White Space, it is not important
- currentLine.TrimEnd();
- // Try to perform some validation first
- if (string.IsNullOrWhiteSpace(currentLine))
- {
- // If null, empty, or white space, skip the Line
- continue;
- }
- else if (currentLine.StartsWith(";"))
- {
- // If it starts with a ; skip the line
- continue;
- }
- else if (currentLine.Length < 10)
- {
- // If it is less than 9 (White Space+CRC32) then it is too
- // small to contain a file name and a check sum skip the line
- continue;
- }
- // Now try to read the entry, because we've trimmed all white
- // space and nothing is valid after the CRC32 we make the
- // assumption that the last 8 characters represent the CRC32
- uint crc32;
- if (uint.TryParse(currentLine.Substring(currentLine.Length - 8), NumberStyles.HexNumber, null, out crc32))
- {
- // At this point we should have the CRC32 of the file, now
- // we need the file name, this should be the remainder of
- // the line, sans any trailing white space.
- string fileName = currentLine.Substring(0,currentLine.Length - 8).TrimEnd();
- // Now add this SfvEntry to our List
- sfvEntries.Add(new SFVEntry(fileName, crc32));
- }
- else
- {
- // TODO: Globalization Concerns, this should be in a resource assembly
- string exception = string.Format("Invalid CRC32 found in line:{0}", currentLine);
- throw new FormatException(exception);
- }
- }
- return new ReadOnlyCollection<SFVEntry>(sfvEntries);
- }
- }
- /// <summary>
- /// Representation of an SFV Entry
- /// </summary>
- public class SFVEntry
- {
- /// <summary>
- ///
- /// </summary>
- /// <param name="fileName"></param>
- /// <param name="CRC32"></param>
- public SFVEntry(string fileName, uint CRC32)
- {
- if(string.IsNullOrWhiteSpace(fileName))
- {
- throw new ArgumentNullException("fileName");
- }
- this.FileName = fileName;
- this.CRC32 = CRC32;
- }
- /// <summary>
- /// The name of the file
- /// </summary>
- public string FileName { get; private set; }
- /// <summary>
- /// Associated CRC32
- /// </summary>
- public uint CRC32 { get; private set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement