private void checkDatafile(DatafileCheckerStruct pDatafileCheckerStruct, DoWorkEventArgs e) { StreamWriter sw; datafile dataFile = new datafile(); XmlSerializer serializer = new XmlSerializer(dataFile.GetType()); TextReader textReader = new StreamReader(pDatafileCheckerStruct.datafilePath); dataFile = (datafile)serializer.Deserialize(textReader); textReader.Close(); AuditingUtil auditingUtil = new AuditingUtil(dataFile); // Check for duplicates string dupePath = pDatafileCheckerStruct.outputPath + Path.DirectorySeparatorChar + "_DUPE.TXT"; if (File.Exists(dupePath)) { File.Delete(dupePath); } sw = File.CreateText(dupePath); // Update max items maxItems = auditingUtil.ChecksumHash.Count; this.checkForDupes(auditingUtil, sw); sw.Close(); sw.Dispose(); }
private void checkForDupes(AuditingUtil pAuditingUtil, StreamWriter pStreamwriter) { ArrayList keys = new ArrayList(pAuditingUtil.ChecksumHash.Keys); int progress; this.progressStruct = new VGMToolbox.util.ProgressStruct(); foreach (string k in keys) { progress = (++totalItems * 100) / maxItems; this.progressStruct.FileName = k; ReportProgress(progress, this.progressStruct); ArrayList gameList = (ArrayList)pAuditingUtil.ChecksumHash[k]; if (gameList.Count > 1) { pStreamwriter.Write(String.Format("Checksum: {0}", k) + Environment.NewLine); foreach (AuditingUtil.ChecksumStruct cs in gameList) { pStreamwriter.Write(AuditingUtil.ROM_SPACER + cs.game + Path.DirectorySeparatorChar + cs.rom + Environment.NewLine); } pStreamwriter.Write(Environment.NewLine); } } }
public void rebuildSets(RebuildSetsStruct pRebuildSetsStruct, DoWorkEventArgs e) { // Set Max sets maxFiles += pRebuildSetsStruct.totalFiles; // Setup output directory pRebuildSetsStruct.pDestinationDir += Path.DirectorySeparatorChar; // Setup AuditingUtil AuditingUtil auditingUtil = new AuditingUtil(pRebuildSetsStruct.pDatFile); // Load/Create cache db try { string cacheDbPath = pRebuildSetsStruct.pDestinationDir + CACHE_DB_FILENAME; using (FileStream cacheFileStream = new FileStream(cacheDbPath, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { auditingUtil.ReadChecksumHashFromFile(cacheFileStream); } // Rebuild this.rebuildSets(pRebuildSetsStruct, auditingUtil, 0, e); // Finish rebuilding using (FileStream cacheFileStream = new FileStream(cacheDbPath, FileMode.Open, FileAccess.ReadWrite)) { auditingUtil.WriteChecksumHashToFile(cacheFileStream); auditingUtil.WriteHaveMissLists(pRebuildSetsStruct.pDestinationDir); } } catch (Exception ex) { this.progressStruct.Clear(); this.progressStruct.ErrorMessage = String.Format("Error while rebuilding sets: {0}{1}", ex.Message, Environment.NewLine); ReportProgress(Constants.IgnoreProgress, this.progressStruct); } }
private game[] buildGames(GetGameParamsStruct pGetGameParamsStruct, uint pDepth, DoWorkEventArgs e) { game[] gameArray = null; ArrayList gameList = new ArrayList(); pDepth++; game set; int progress; rom romfile; BuildRomStruct romParameters; string buildRomMessages; try { // Directories foreach (string d in Directory.GetDirectories(pGetGameParamsStruct.pDir)) { if (Directory.GetFiles(d, "*.*", SearchOption.AllDirectories).Length > 0) { if (pDepth == 1) { this.romList = new ArrayList(); this.dir = d; } set = new game(); foreach (string f in Directory.GetFiles(d)) { if (!CancellationPending) { progress = (++fileCount * 100) / pGetGameParamsStruct.totalFiles; this.progressStruct.Clear(); this.progressStruct.FileName = f; ReportProgress(progress, this.progressStruct); try { romParameters = new BuildRomStruct(); romParameters.AddMd5 = false; romParameters.AddSha1 = false; romParameters.FilePath = f; romParameters.TopLevelSetFolder = this.dir; romParameters.UseNormalChecksums = pGetGameParamsStruct.UseNormalChecksums; romfile = AuditingUtil.BuildRom(romParameters, out buildRomMessages); if (String.IsNullOrEmpty(buildRomMessages)) { if (romfile.name != null) { romList.Add(romfile); } } else { this.progressStruct.Clear(); this.progressStruct.FileName = f; this.progressStruct.ErrorMessage = buildRomMessages; ReportProgress(Constants.IgnoreProgress, this.progressStruct); } } catch (Exception _ex) { this.progressStruct.Clear(); this.progressStruct.FileName = f; this.progressStruct.ErrorMessage = "Error processing <" + f + "> (" + _ex.Message + ")" + "...Skipped" + Environment.NewLine; ReportProgress(Constants.IgnoreProgress, this.progressStruct); } } else { e.Cancel = true; break; } } if (!CancellationPending) { GetGameParamsStruct subdirGetGameParamsStruct = pGetGameParamsStruct; subdirGetGameParamsStruct.pDir = d; this.buildGames(subdirGetGameParamsStruct, pDepth, e); if (pDepth == 1 && romList.Count > 0) { set.rom = (rom[])this.romList.ToArray(typeof(rom)); set.name = d.Substring(d.LastIndexOf(Path.DirectorySeparatorChar) + 1); set.description = set.name; gameList.Add(set); } } else { break; } } // if ((Directory.GetFiles(d, "*.*", SearchOption.AllDirectories).Length - 1) > 0) } // foreach (string d in Directory.GetDirectories(pDir)) if (gameList.Count > 0) { gameArray = (game[])gameList.ToArray(typeof(game)); } } catch (Exception e1) { this.progressStruct.Clear(); this.progressStruct.FileName = null; this.progressStruct.ErrorMessage = e1.Message; ReportProgress(Constants.IgnoreProgress, this.progressStruct); } return(gameArray); }
private void doCancelCleanup(AuditingUtil pAuditingUtil, string pDestinationDir) { this.deleteQueuedTempDirs(); }
private void rebuildSets(RebuildSetsStruct pRebuildSetsStruct, AuditingUtil pAuditingUtil, uint pDepth, DoWorkEventArgs e) { int progress; RebuildSetsStruct subdirRebuildSetsStruct; try { if (pDepth++ == 0) { // Set Max sets //maxFiles += pRebuildSetsStruct.totalFiles; // process top level files libFilesForDeletion.Clear(); foreach (string f in Directory.GetFiles(pRebuildSetsStruct.pSourceDir)) { if (!CancellationPending) { this.rebuildFile(f, pRebuildSetsStruct, pAuditingUtil, e); progress = (++fileCount * 100) / maxFiles; this.progressStruct.Clear(); this.progressStruct.FileName = f; ReportProgress(progress, this.progressStruct); } else { e.Cancel = true; doCancelCleanup(pAuditingUtil, pRebuildSetsStruct.pDestinationDir); return; } } // foreach (string f in Directory.GetFiles(pRebuildSetsStruct.pSourceDir)) if (!CancellationPending) { this.deleteQueuedLibFiles(); } } // process subdirs if (!CancellationPending) { foreach (string d in Directory.GetDirectories(pRebuildSetsStruct.pSourceDir)) { libFilesForDeletion.Clear(); foreach (string f in Directory.GetFiles(d)) { if (!CancellationPending) { try { this.rebuildFile(f, pRebuildSetsStruct, pAuditingUtil, e); progress = (++fileCount * 100) / maxFiles; this.progressStruct.Clear(); this.progressStruct.FileName = f; ReportProgress(progress, this.progressStruct); } catch (Exception ex) { this.progressStruct.Clear(); this.progressStruct.FileName = f; this.progressStruct.ErrorMessage = "[" + f + "] " + ex.Message; ReportProgress(Constants.IgnoreProgress, this.progressStruct); } } else { e.Cancel = true; doCancelCleanup(pAuditingUtil, pRebuildSetsStruct.pDestinationDir); return; } } if (!CancellationPending) { // RebuildSetsStruct subdirRebuildSetsStruct = pRebuildSetsStruct; subdirRebuildSetsStruct = pRebuildSetsStruct; subdirRebuildSetsStruct.pSourceDir = d; this.rebuildSets(subdirRebuildSetsStruct, pAuditingUtil, pDepth, e); // Remove Queued Lib Files this.deleteQueuedLibFiles(); // remove empty directory if (pRebuildSetsStruct.pRemoveSource && Directory.GetFiles(d, "*.*", SearchOption.AllDirectories).Length == 0) { Directory.Delete(d); } } else { e.Cancel = true; doCancelCleanup(pAuditingUtil, pRebuildSetsStruct.pDestinationDir); return; } } // (string d in Directory.GetDirectories(pRebuildSetsStruct.pSourceDir)) } //if (!CancellationPending) else { e.Cancel = true; doCancelCleanup(pAuditingUtil, pRebuildSetsStruct.pDestinationDir); return; } } catch (Exception exception2) { this.progressStruct.Clear(); this.progressStruct.FileName = null; this.progressStruct.ErrorMessage = exception2.Message; ReportProgress(Constants.IgnoreProgress, this.progressStruct); } }
private void rebuildFile(string pFilePath, RebuildSetsStruct pRebuildSetsStruct, AuditingUtil pAuditingUtil, DoWorkEventArgs ea) { ArrayList pDestinationFiles = new ArrayList(); bool isFileLibrary = false; bool hasMultipleExtensions = false; bool fileMovedOrScannedOk = true; Type formatType = null; if (FormatUtil.IsZipFile(pFilePath)) { string tempDir = System.IO.Path.GetTempPath() + Path.GetFileNameWithoutExtension(pFilePath); CompressionUtil.ExtractAllFilesFromZipFile(pFilePath, tempDir); tempDirsForDeletion.Add(tempDir); // Add in case of cancel button RebuildSetsStruct zipRebuildSetsStruct = pRebuildSetsStruct; zipRebuildSetsStruct.pSourceDir = tempDir; zipRebuildSetsStruct.totalFiles = Directory.GetFiles(tempDir, "*.*", SearchOption.AllDirectories).Length; maxFiles += zipRebuildSetsStruct.totalFiles; this.rebuildSets(zipRebuildSetsStruct, pAuditingUtil, 0, ea); if (!CancellationPending) { Directory.Delete(tempDir, true); tempDirsForDeletion.Remove(tempDir); } } else { using (FileStream fs = File.OpenRead(pFilePath)) { formatType = FormatUtil.getObjectType(fs); fs.Seek(0, SeekOrigin.Begin); // CRC32 string crc32Value = String.Empty; Crc32 crc32Generator = new Crc32(); // @TODO - Change to file streams? Out of memory errors on repeat runs /* * // MD5 * string md5FileName = Path.GetTempFileName(); * MD5CryptoServiceProvider md5Hash = new MD5CryptoServiceProvider(); * //MemoryStream md5MemoryStream = new MemoryStream(); * FileStream md5MemoryStream = new FileStream(md5FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); * CryptoStream md5CryptoStream = new CryptoStream(md5MemoryStream, md5Hash, CryptoStreamMode.Write); * * // SHA1 * string sha1FileName = Path.GetTempFileName(); * SHA1CryptoServiceProvider sha1Hash = new SHA1CryptoServiceProvider(); * //MemoryStream sha1MemoryStream = new MemoryStream(); * FileStream sha1MemoryStream = new FileStream(sha1FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); * CryptoStream sha1CryptoStream = new CryptoStream(sha1MemoryStream, sha1Hash, CryptoStreamMode.Write); */ if ((pAuditingUtil.HasVgmtSizeFlag) && (formatType != null)) { try { IFormat vgmData = (IFormat)Activator.CreateInstance(formatType); vgmData.Initialize(fs, pFilePath); isFileLibrary = vgmData.IsFileLibrary(); hasMultipleExtensions = vgmData.HasMultipleFileExtensions(); // vgmData.getDatFileCrc32(pFilePath, ref libHash, ref crc32Generator, // ref md5CryptoStream, ref sha1CryptoStream, false, pStreamInput); vgmData.GetDatFileCrc32(ref crc32Generator); vgmData = null; } catch (EndOfStreamException e) { this.progressStruct.Clear(); this.progressStruct.FileName = pFilePath; this.progressStruct.ErrorMessage = String.Format("Error processing <{0}> as type [{1}], falling back to full file cheksum. Error received: {2}", pFilePath, formatType.Name, e.Message) + Environment.NewLine + Environment.NewLine; ReportProgress(Constants.IgnoreProgress, this.progressStruct); crc32Generator.Reset(); // ParseFile.AddChunkToChecksum(fs, 0, (int)fs.Length, ref crc32Generator, // ref md5CryptoStream, ref sha1CryptoStream); ChecksumUtil.AddChunkToChecksum(fs, 0, (int)fs.Length, ref crc32Generator); } catch (System.OutOfMemoryException e) { this.progressStruct.Clear(); this.progressStruct.FileName = pFilePath; this.progressStruct.ErrorMessage = String.Format("Error processing <{0}> as type [{1}], falling back to full file cheksum. Error received: {2}", pFilePath, formatType.Name, e.Message) + Environment.NewLine + Environment.NewLine; ReportProgress(Constants.IgnoreProgress, this.progressStruct); crc32Generator.Reset(); // ParseFile.AddChunkToChecksum(fs, 0, (int)fs.Length, ref crc32Generator, // ref md5CryptoStream, ref sha1CryptoStream); ChecksumUtil.AddChunkToChecksum(fs, 0, (int)fs.Length, ref crc32Generator); } catch (IOException e) { this.progressStruct.Clear(); this.progressStruct.FileName = pFilePath; this.progressStruct.ErrorMessage = String.Format("Error processing <{0}> as type [{1}], falling back to full file cheksum. Error received: {2}", pFilePath, formatType.Name, e.Message) + Environment.NewLine + Environment.NewLine; ReportProgress(Constants.IgnoreProgress, this.progressStruct); crc32Generator.Reset(); // ParseFile.AddChunkToChecksum(fs, 0, (int)fs.Length, ref crc32Generator, // ref md5CryptoStream, ref sha1CryptoStream); ChecksumUtil.AddChunkToChecksum(fs, 0, (int)fs.Length, ref crc32Generator); } } else { // ParseFile.AddChunkToChecksum(fs, 0, (int)fs.Length, ref crc32Generator, // ref md5CryptoStream, ref sha1CryptoStream); ChecksumUtil.AddChunkToChecksum(fs, 0, (int)fs.Length, ref crc32Generator); } // @TODO Add MD5/SHA1 to make checksum hash correct String(CRC32 + MD5 + SHA1) crc32Value = crc32Generator.Value.ToString("X8"); pDestinationFiles = (ArrayList)pAuditingUtil.ChecksumHash[crc32Value]; if (pDestinationFiles != null) { if (!pRebuildSetsStruct.ScanOnly) { fileMovedOrScannedOk = this.moveFile(pRebuildSetsStruct.pDestinationDir, pDestinationFiles, fs, pFilePath, pRebuildSetsStruct.pOverwriteExisting, pRebuildSetsStruct.pCompressOutput, hasMultipleExtensions); } pAuditingUtil.AddChecksumToCache(crc32Value); } else { pAuditingUtil.AddUnknownFile(pFilePath); } } /* * md5CryptoStream.Close(); * md5CryptoStream.Dispose(); * sha1CryptoStream.Close(); * sha1CryptoStream.Dispose(); * * md5MemoryStream.Close(); * md5MemoryStream.Dispose(); * sha1MemoryStream.Close(); * sha1MemoryStream.Dispose(); * * File.Delete(md5FileName); * File.Delete(sha1FileName); */ // Remove Source only if copied if (fileMovedOrScannedOk && pDestinationFiles != null && pRebuildSetsStruct.pRemoveSource && File.Exists(pFilePath)) { if (!isFileLibrary) { File.Delete(pFilePath); } else // Add to List for deletion later { libFilesForDeletion.Add(pFilePath); } } } // (FormatUtil.IsZipFile(pFilePath)) }