diff --git a/DATFromDir/DATFromDir.cs b/DATFromDir/DATFromDir.cs index 77bca4ba..9b4fed3e 100644 --- a/DATFromDir/DATFromDir.cs +++ b/DATFromDir/DATFromDir.cs @@ -44,44 +44,106 @@ namespace SabreTools // This is where the main loop would go if (File.Exists(_basePath)) { + // Create the temporary output directory + Directory.CreateDirectory(_tempDir); + + psi.Arguments = _baseExtract + " " + _basePath; + Process zip = Process.Start(psi); + zip.WaitForExit(); + + bool encounteredErrors = zip.StandardError.ReadToEnd().Contains("ERROR"); + // Get a list of files including size and hashes Crc32 crc = new Crc32(); MD5 md5 = MD5.Create(); SHA1 sha1 = SHA1.Create(); - string fileCRC = String.Empty; - string fileMD5 = String.Empty; - string fileSHA1 = String.Empty; - - try + // If the file was an archive and was extracted successfully, check it + if (!encounteredErrors) { - using (FileStream fs = File.Open(_basePath, FileMode.Open)) + foreach (string entry in Directory.GetFiles(_tempDir, "*", SearchOption.AllDirectories)) { - foreach (byte b in crc.ComputeHash(fs)) + string fileCRC = String.Empty; + string fileMD5 = String.Empty; + string fileSHA1 = String.Empty; + + try { - fileCRC += b.ToString("x2").ToLower(); + using (FileStream fs = File.Open(entry, FileMode.Open)) + { + foreach (byte b in crc.ComputeHash(fs)) + { + fileCRC += b.ToString("x2").ToLower(); + } + } + using (FileStream fs = File.Open(entry, FileMode.Open)) + { + fileMD5 = BitConverter.ToString(md5.ComputeHash(fs)).Replace("-", ""); + } + using (FileStream fs = File.Open(entry, FileMode.Open)) + { + fileSHA1 = BitConverter.ToString(sha1.ComputeHash(fs)).Replace("-", ""); + } + } + catch (IOException) + { + continue; } - } - using (FileStream fs = File.Open(_basePath, FileMode.Open)) - { - fileMD5 = BitConverter.ToString(md5.ComputeHash(fs)).Replace("-", ""); - } - using (FileStream fs = File.Open(_basePath, FileMode.Open)) - { - fileSHA1 = BitConverter.ToString(sha1.ComputeHash(fs)).Replace("-", ""); - } - roms.Add(new Tuple( - "Default", - _basePath, - (new FileInfo(_basePath)).Length, - fileCRC, - fileMD5, - fileSHA1)); + roms.Add(new Tuple( + Path.GetFileNameWithoutExtension(_basePath), + entry.Remove(0, _tempDir.Length), + (new FileInfo(entry)).Length, + fileCRC, + fileMD5, + fileSHA1)); - Console.WriteLine("File parsed: " + _basePath.Remove(0, _basePath.Length)); + Console.WriteLine("File parsed: " + entry.Remove(0, _tempDir.Length)); + } + } + // Otherwise, just get the info on the file itself + else if (!Directory.Exists(_basePath) && File.Exists(_basePath)) + { + string fileCRC = String.Empty; + string fileMD5 = String.Empty; + string fileSHA1 = String.Empty; + + try + { + using (FileStream fs = File.Open(_basePath, FileMode.Open)) + { + foreach (byte b in crc.ComputeHash(fs)) + { + fileCRC += b.ToString("x2").ToLower(); + } + } + using (FileStream fs = File.Open(_basePath, FileMode.Open)) + { + fileMD5 = BitConverter.ToString(md5.ComputeHash(fs)).Replace("-", ""); + } + using (FileStream fs = File.Open(_basePath, FileMode.Open)) + { + fileSHA1 = BitConverter.ToString(sha1.ComputeHash(fs)).Replace("-", ""); + } + + roms.Add(new Tuple( + "Default", + _basePath, + (new FileInfo(_basePath)).Length, + fileCRC, + fileMD5, + fileSHA1)); + + Console.WriteLine("File parsed: " + _basePath.Remove(0, _basePath.Length)); + } + catch (IOException) { } + } + + // Delete the temp directory + if (Directory.Exists(_tempDir)) + { + Directory.Delete(_tempDir, true); } - catch (IOException) { } } else {