public static RvFile ReadInDatFile(RvDat datFile, ThreadWorker thWrk, out string extraDirName) { try { _thWrk = thWrk; extraDirName = null; string datRootFullName = datFile.GetData(RvDat.DatData.DatRootFullName); string fullPath = RvFile.GetDatPhysicalPath(datRootFullName); Debug.WriteLine("Reading Dat " + fullPath); DatRead.ReadDat(fullPath, ReadError, out DatHeader dh); if (dh == null) { return(null); } string dirNameRule = Path.GetDirectoryName(datRootFullName) + Path.DirSeparatorChar; if ( !datFile.MultiDatOverride && dh.Dir != "noautodir" && (datFile.MultiDatsInDirectory || !string.IsNullOrEmpty(dh.RootDir)) ) { // if we are auto adding extra directories then create a new directory. extraDirName = ""; if (string.IsNullOrEmpty(extraDirName) && datFile.UseDescriptionAsDirName && !string.IsNullOrWhiteSpace(dh.Description)) { extraDirName = dh.Description; } if (string.IsNullOrEmpty(extraDirName)) { extraDirName = dh.RootDir; } if (string.IsNullOrEmpty(extraDirName)) { extraDirName = dh.Name; } if (string.IsNullOrEmpty(extraDirName)) { extraDirName = Path.GetFileNameWithoutExtension(fullPath); } dirNameRule += VarFix.CleanFileName(extraDirName) + Path.DirSeparatorChar; } ReportError.LogOut($"DatRule {dirNameRule}"); DatRule datRule = FindDatRule(dirNameRule); DatClean.CleanFilenames(dh.BaseDir); switch (datRule.Filter) { case FilterType.CHDsOnly: DatClean.RemoveNonCHD(dh.BaseDir); break; case FilterType.RomsOnly: DatClean.RemoveCHD(dh.BaseDir); break; } DatClean.RemoveNoDumps(dh.BaseDir); SetMergeType(datRule, dh); if (datRule.SingleArchive) { DatClean.MakeDatSingleLevel(dh, datRule.UseDescriptionAsDirName, datRule.SubDirType, isFile(datRule, dh)); } DatClean.RemoveUnNeededDirectories(dh.BaseDir); SetCompressionMethod(datRule, dh); // This sorts the files into the required dir order for the set compression type. (And also sets '\' characters to '/' in zip files.) DatClean.DirectoryExpand(dh.BaseDir); // this works because we only expand files, so the order inside the zip / 7z does not matter. DatClean.RemoveEmptyDirectories(dh.BaseDir); DatClean.CleanFilenamesFixDupes(dh.BaseDir); // you may get repeat filenames inside Zip's / 7Z's and they may not be sorted to find them by now. RvFile newDir = ExternalDatConverter.ConvertFromExternalDat(dh, datFile); return(newDir); } catch (Exception e) { string datRootFullName = datFile?.GetData(RvDat.DatData.DatRootFullName); Console.WriteLine(e); throw new Exception("Error is DAT " + datRootFullName + " " + e.Message); } }
/// <summary> /// Add the new DAT's into the DAT list /// And merge in the new DAT data into the database /// </summary> /// <param name="dbDir">The Current database dir</param> /// <param name="tmpDir">A temp directory containing the DAT found in this directory in DatRoot</param> private static void AddNewDats(RvFile dbDir, RvFile tmpDir) { int dbIndex = 0; int scanIndex = 0; Debug.WriteLine(""); Debug.WriteLine("Scanning for Adding new DATS"); while (dbIndex < dbDir.DirDatCount || scanIndex < tmpDir.DirDatCount) { RvDat dbDat = null; RvDat fileDat = null; int res = 0; if (dbIndex < dbDir.DirDatCount && scanIndex < tmpDir.DirDatCount) { dbDat = dbDir.DirDat(dbIndex); fileDat = tmpDir.DirDat(scanIndex); res = DBHelper.DatCompare(dbDat, fileDat); Debug.WriteLine("Checking " + dbDat.GetData(RvDat.DatData.DatRootFullName) + " : and " + fileDat.GetData(RvDat.DatData.DatRootFullName) + " : " + res); } else if (scanIndex < tmpDir.DirDatCount) { fileDat = tmpDir.DirDat(scanIndex); res = 1; Debug.WriteLine("Checking : and " + fileDat.GetData(RvDat.DatData.DatRootFullName) + " : " + res); } else if (dbIndex < dbDir.DirDatCount) { dbDat = dbDir.DirDat(dbIndex); res = -1; Debug.WriteLine("Checking " + dbDat.GetData(RvDat.DatData.DatRootFullName) + " : and : " + res); } switch (res) { case 0: _datsProcessed++; _thWrk.Report(_datsProcessed); _thWrk.Report(new bgwText("Dat : " + Path.GetFileNameWithoutExtension(fileDat?.GetData(RvDat.DatData.DatRootFullName)))); Debug.WriteLine("Correct"); // Should already be set as correct above if (dbDat != null) { dbDat.Status = DatUpdateStatus.Correct; } dbIndex++; scanIndex++; break; case 1: _datsProcessed++; _thWrk.Report(_datsProcessed); _thWrk.Report(new bgwText("Scanning New Dat : " + Path.GetFileNameWithoutExtension(fileDat?.GetData(RvDat.DatData.DatRootFullName)))); Debug.WriteLine("Adding new DAT"); if (LoadNewDat(fileDat, dbDir)) { dbIndex++; } scanIndex++; break; case -1: // This should not happen as deleted dat have been removed above //dbIndex++; ReportError.SendAndShow("ERROR Deleting a DAT that should already be deleted."); break; } } }
public static RvFile ReadInDatFile(RvDat datFile, ThreadWorker thWrk) { try { _thWrk = thWrk; string datRootFullName = datFile.GetData(RvDat.DatData.DatRootFullName); DatRead dr = new DatRead { ErrorReport = ReadError }; string fullPath = RvFile.GetDatPhysicalPath(datRootFullName); Debug.WriteLine("Reading Dat " + fullPath); dr.ReadDat(fullPath, out DatHeader dh); if (dh == null) { return(null); } string extraPath = !string.IsNullOrEmpty(dh.RootDir) ? dh.RootDir : dh.Name; string dirName = Path.GetDirectoryName(datRootFullName) + System.IO.Path.DirectorySeparatorChar + extraPath + System.IO.Path.DirectorySeparatorChar; DatRule datRule = FindDatRule(dirName); DatClean.CleanFilenames(dh.BaseDir); switch (datRule.Filter) { case FilterType.CHDsOnly: DatClean.RemoveNonCHD(dh.BaseDir); break; case FilterType.RomsOnly: DatClean.RemoveCHD(dh.BaseDir); break; } DatClean.RemoveNoDumps(dh.BaseDir); SetMergeType(datRule, dh); if (datRule.SingleArchive) { DatClean.MakeDatSingleLevel(dh); } DatClean.RemoveUnNeededDirectories(dh.BaseDir); SetCompressionMethod(datRule, dh); // This sorts the files into the required dir order for the set compression type. DatClean.DirectoryExpand(dh.BaseDir); // this works because we only expand files, so the order inside the zip / 7z does not matter. DatClean.RemoveEmptyDirectories(dh.BaseDir); DatClean.CleanFilenamesFixDupes(dh.BaseDir); // you may get repeat filenames inside Zip's / 7Z's and they may not be sorted to find them by now. RvFile newDir = ExternalDatConverter.ConvertFromExternalDat(dh, datRootFullName, datFile.TimeStamp); return(newDir); } catch (Exception e) { string datRootFullName = datFile?.GetData(RvDat.DatData.DatRootFullName); Console.WriteLine(e); throw new Exception("Error is DAT " + datRootFullName + " " + e.Message); } }
private static void MakeFixFilesRecurse(RvBase b, bool selected) { if (selected) { if (b.Dat != null) { RvDir tDir = b as RvDir; if (tDir != null && tDir.Game != null && tDir.DirStatus.HasMissing()) { if (_tDat != b.Dat) { if (_tDat != null) { _ts.WriteLine("</datafile>"); _ts.WriteLine(); } if (_ts != null) { _ts.Close(); } _tDat = b.Dat; int test = 0; string datFilename = Path.Combine(_outdir, "fixDat_" + Path.GetFileNameWithoutExtension(_tDat.GetData(RvDat.DatData.DatFullName)) + ".dat"); while (File.Exists(datFilename)) { test++; datFilename = Path.Combine(_outdir, "fixDat_" + Path.GetFileNameWithoutExtension(_tDat.GetData(RvDat.DatData.DatFullName)) + "(" + test + ").dat"); } _ts = new StreamWriter(datFilename); _ts.WriteLine("<?xml version=\"1.0\"?>"); _ts.WriteLine( "<!DOCTYPE datafile PUBLIC \"-//Logiqx//DTD ROM Management Datafile//EN\" \"http://www.logiqx.com/Dats/datafile.dtd\">"); _ts.WriteLine(""); _ts.WriteLine("<datafile>"); _ts.WriteLine("\t<header>"); _ts.WriteLine("\t\t<name>fix_" + Etxt(_tDat.GetData(RvDat.DatData.DatName)) + "</name>"); if (_tDat.GetData(RvDat.DatData.SuperDat) == "superdat") { _ts.WriteLine("\t\t<type>SuperDAT</type>"); } _ts.WriteLine("\t\t<description>fix_" + Etxt(_tDat.GetData(RvDat.DatData.Description)) + "</description>"); _ts.WriteLine("\t\t<category>FIXDATFILE</category>"); _ts.WriteLine("\t\t<version>" + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + "</version>"); _ts.WriteLine("\t\t<date>" + DateTime.Now.ToString("MM/dd/yyyy") + "</date>"); _ts.WriteLine("\t\t<author>ROMVault</author>"); _ts.WriteLine("\t</header>"); } _ts.WriteLine("\t<game name=\"" + Etxt(tDir.SuperDatFileName()) + "\">"); if (!string.IsNullOrEmpty(tDir.Game.GetData(RvGame.GameData.Description))) { _ts.WriteLine("\t\t<description>" + Etxt(tDir.Game.GetData(RvGame.GameData.Description)) + "</description>"); } } RvFile tRom = b as RvFile; if (tRom != null) { if (tRom.DatStatus == DatStatus.InDatCollect && tRom.GotStatus != GotStatus.Got) { string strRom; if (tRom.FileStatusIs(FileStatus.SHA1CHDFromDAT | FileStatus.MD5CHDFromDAT)) { strRom = "\t\t<disk name=\"" + Etxt(tRom.Name) + "\""; } else { strRom = "\t\t<rom name=\"" + Etxt(tRom.Name) + "\""; } if (tRom.FileStatusIs(FileStatus.SizeFromDAT) && tRom.Size != null) { strRom += " size=\"" + tRom.Size + "\""; } string strCRC = ArrByte.ToString(tRom.CRC); if (tRom.FileStatusIs(FileStatus.CRCFromDAT) && !string.IsNullOrEmpty(strCRC)) { strRom += " crc=\"" + strCRC + "\""; } string strSHA1 = ArrByte.ToString(tRom.SHA1); if (tRom.FileStatusIs(FileStatus.SHA1FromDAT) && !string.IsNullOrEmpty(strSHA1)) { strRom += " sha1=\"" + strSHA1 + "\""; } string strMD5 = ArrByte.ToString(tRom.MD5); if (tRom.FileStatusIs(FileStatus.MD5FromDAT) && !string.IsNullOrEmpty(strMD5)) { strRom += " md5=\"" + strMD5 + "\""; } string strSHA1CHD = ArrByte.ToString(tRom.SHA1CHD); if (tRom.FileStatusIs(FileStatus.SHA1CHDFromDAT) && !string.IsNullOrEmpty(strSHA1CHD)) { strRom += " sha1=\"" + strSHA1CHD + "\""; } string strMD5CHD = ArrByte.ToString(tRom.MD5CHD); if (tRom.FileStatusIs(FileStatus.MD5CHDFromDAT) && !string.IsNullOrEmpty(strMD5CHD)) { strRom += " md5=\"" + strMD5CHD + "\""; } strRom += "/>"; _ts.WriteLine(strRom); } } } } RvDir d = b as RvDir; if (d != null) { for (int i = 0; i < d.ChildCount; i++) { bool nextSelected = selected; if (d.Tree != null) { nextSelected = d.Tree.Checked == RvTreeRow.TreeSelect.Selected; } MakeFixFilesRecurse(d.Child(i), nextSelected); } } if (selected) { if (b.Dat != null) { RvDir tDir = b as RvDir; if (tDir != null && tDir.Game != null && tDir.DirStatus.HasMissing()) { _ts.WriteLine("\t</game>"); } } } }
private static void FindAllDats(RvBase b, ReportType rt) { RvDir d = b as RvDir; if (d == null) { return; } if (d.DirDatCount > 0) { for (int i = 0; i < d.DirDatCount; i++) { RvDat dat = d.DirDat(i); int correct = 0; int missing = 0; int fixesNeeded = 0; if (d.Dat == dat) { correct += d.DirStatus.CountCorrect(); missing += d.DirStatus.CountMissing(); fixesNeeded += d.DirStatus.CountFixesNeeded(); } else { for (int j = 0; j < d.ChildCount; j++) { RvDir c = d.Child(j) as RvDir; if (c == null || c.Dat != dat) { continue; } correct += c.DirStatus.CountCorrect(); missing += c.DirStatus.CountMissing(); fixesNeeded += c.DirStatus.CountFixesNeeded(); } } switch (rt) { case ReportType.Complete: if (correct > 0 && missing == 0 && fixesNeeded == 0) { _ts.WriteLine(RemoveBase(dat.GetData(RvDat.DatData.DatFullName))); } break; case ReportType.CompletelyMissing: if (correct == 0 && missing > 0 && fixesNeeded == 0) { _ts.WriteLine(RemoveBase(dat.GetData(RvDat.DatData.DatFullName))); } break; case ReportType.PartialMissing: if ((correct > 0 && missing > 0) || fixesNeeded > 0) { _ts.WriteLine(RemoveBase(dat.GetData(RvDat.DatData.DatFullName))); _fileNameLength = 0; _fileSizeLength = 0; _repStatusLength = 0; ReportMissingFindSizes(d, dat, rt); ReportDrawBars(); ReportMissing(d, dat, rt); ReportDrawBars(); _ts.WriteLine(); } break; case ReportType.Fixing: if (fixesNeeded > 0) { _ts.WriteLine(RemoveBase(dat.GetData(RvDat.DatData.DatFullName))); _fileNameLength = 0; _fileSizeLength = 0; _repStatusLength = 0; ReportMissingFindSizes(d, dat, rt); ReportDrawBars(); ReportMissing(d, dat, rt); ReportDrawBars(); _ts.WriteLine(); } break; } } } if (b.Dat != null) { return; } for (int i = 0; i < d.ChildCount; i++) { FindAllDats(d.Child(i), rt); } }
/// <summary> /// Add the new DAT's into the DAT list /// And merge in the new DAT data into the database /// </summary> /// <param name="dbDir">The Current database dir</param> /// <param name="tmpDir">A temp directory containing the DAT found in this directory in DatRoot</param> private static void AddNewDats(RvDir dbDir, RvDir tmpDir) { bool autoAddDirectory = tmpDir.DirDatCount > 1; int dbIndex = 0; int scanIndex = 0; Debug.WriteLine(""); Debug.WriteLine("Scanning for Adding new DATS"); while ((dbIndex < dbDir.DirDatCount) || (scanIndex < tmpDir.DirDatCount)) { RvDat dbDat = null; RvDat fileDat = null; int res = 0; if ((dbIndex < dbDir.DirDatCount) && (scanIndex < tmpDir.DirDatCount)) { dbDat = dbDir.DirDat(dbIndex); fileDat = tmpDir.DirDat(scanIndex); res = DBHelper.DatCompare(dbDat, fileDat); Debug.WriteLine("Checking " + dbDat.GetData(RvDat.DatData.DatFullName) + " : and " + fileDat.GetData(RvDat.DatData.DatFullName) + " : " + res); } else if (scanIndex < tmpDir.DirDatCount) { fileDat = tmpDir.DirDat(scanIndex); res = 1; Debug.WriteLine("Checking : and " + fileDat.GetData(RvDat.DatData.DatFullName) + " : " + res); } else if (dbIndex < dbDir.DirDatCount) { dbDat = dbDir.DirDat(dbIndex); res = -1; Debug.WriteLine("Checking " + dbDat.GetData(RvDat.DatData.DatFullName) + " : and : " + res); } switch (res) { case 0: _datsProcessed++; _bgw.ReportProgress(_datsProcessed); _bgw.ReportProgress(0, new bgwText("Dat : " + Path.GetFileNameWithoutExtension(fileDat.GetData(RvDat.DatData.DatFullName)))); Debug.WriteLine("Correct"); // Should already be set as correct above dbDat.Status = DatUpdateStatus.Correct; dbIndex++; scanIndex++; break; case 1: _datsProcessed++; _bgw.ReportProgress(_datsProcessed); _bgw.ReportProgress(0, new bgwText("Scanning New Dat : " + Path.GetFileNameWithoutExtension(fileDat.GetData(RvDat.DatData.DatFullName)))); Debug.WriteLine("Adding new DAT"); if (UpdateDatFile(fileDat, autoAddDirectory, dbDir)) { dbIndex++; } scanIndex++; break; case -1: // This should not happen as deleted dat have been removed above //dbIndex++; ReportError.SendAndShow(Resources.DatUpdate_UpdateDatList_ERROR_Deleting_a_DAT_that_should_already_be_deleted); break; } } }
public static RvDir ReadInDatFile(RvDat datFile, BackgroundWorker bgw) { _bgw = bgw; RvDir newDir = new RvDir(FileType.Dir); string datFullName = datFile.GetData(RvDat.DatData.DatFullName); System.IO.Stream fs; int errorCode = IO.FileStream.OpenFileRead(datFullName, out fs); if (errorCode != 0) { _bgw.ReportProgress(0, new bgwShowError(datFullName, errorCode + ": " + new Win32Exception(errorCode).Message)); return(null); } System.IO.StreamReader myfile = new System.IO.StreamReader(fs, Program.Enc); string strLine = myfile.ReadLine(); myfile.Close(); fs.Close(); fs.Dispose(); if (strLine == null) { return(null); } if (strLine.ToLower().IndexOf("xml", StringComparison.Ordinal) >= 0) { if (!ReadXMLDat(ref newDir, datFullName)) { return(null); } } else if (strLine.ToLower().IndexOf("clrmamepro", StringComparison.Ordinal) >= 0 || strLine.ToLower().IndexOf("ROMVault", StringComparison.Ordinal) >= 0 || strLine.ToLower().IndexOf("game", StringComparison.Ordinal) >= 0) { if (!DatCmpReader.ReadDat(ref newDir, datFullName)) { return(null); } } else if (strLine.ToLower().IndexOf("doscenter", StringComparison.Ordinal) >= 0) { if (!DatDOSReader.ReadDat(ref newDir, datFullName)) { return(null); } } else { _bgw.ReportProgress(0, new bgwShowError(datFullName, "Invalid DAT File")); return(null); } if (newDir.Dat == null) { _bgw.ReportProgress(0, new bgwShowError(datFullName, "Invalid Header")); return(null); } newDir.Dat.AddData(RvDat.DatData.DatFullName, datFullName); newDir.Dat.TimeStamp = datFile.TimeStamp; newDir.Dat.Status = DatUpdateStatus.Correct; DatSetRemoveUnneededDirs(newDir); DatSetCheckParentSets(newDir); DatSetRenameAndRemoveDups(newDir); if (newDir.Dat.GetData(RvDat.DatData.MergeType).ToLower() == "full") { DatSetMergeSets(newDir); } DatSetCheckCollect(newDir); DatSetCreateSubDirs(newDir); if (newDir.Dat.GetData(RvDat.DatData.DirSetup).ToLower() == "nogame") { DatSetRemoveGameDir(newDir); } return(newDir); }