// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Data; namespace Spludlow.Mame { public class MameImport { // There is a machine rom that ends .7z (perofrm a checks?) private static List archiveExtentions = new List(new string[] { ".zip", ".rar" }); public static void Import() { MameConfiguration config = new MameConfiguration(); Import(config.ImportDirectory, false); } public static void ImportNoDiskVerify() { MameConfiguration config = new MameConfiguration(); Import(config.ImportDirectory, true); } public static void Import(string importDirectory) { Import(importDirectory, false); } public static void ImportNoDiskVerify(string importDirectory) { Import(importDirectory, true); } public static void Import(string importDirectory, bool dontDiskVerify) { // "" temp directory is system with long filename support MameConfiguration config = new MameConfiguration(); MameChdMan chdMan = new MameChdMan(config.BinariesDirectory); MameHashStores hashStores = new MameHashStores(); MameDatabaseHashes databaseHashes = new MameDatabaseHashes(); DataTable table = Spludlow.Data.TextTable.ReadText(new string[] { "Filename Length SHA1 FileType MachineAction SoftwareAction Error", "String Int64 String String String String String", }); Import(importDirectory, "", chdMan, hashStores, databaseHashes, table, dontDiskVerify); MameReport.Report("Import", table); } public static void Import( string importDirectory, string tempDirectory, MameChdMan chdMan, MameHashStores hashStores, MameDatabaseHashes databaseHashes, DataTable table, bool dontDiskVerify) { ClearAttributes(importDirectory); foreach (string filename in Directory.GetFiles(importDirectory, "*", SearchOption.AllDirectories)) { string reportFilename = filename.Substring(importDirectory.Length + 1); long length = Spludlow.Io.Files.FileLength(filename); DataRow row = table.Rows.Add(reportFilename, length, "", "", "", "", ""); string extention = Path.GetExtension(filename).ToLower(); try { // Archive if (archiveExtentions.Contains(extention) == true) { row["FileType"] = "ARCHIVE"; using (Spludlow.TempDirectory tempDir = new TempDirectory(tempDirectory)) { Spludlow.Archive.Extract(filename, tempDir.Path); Import(tempDir.Path, tempDirectory, chdMan, hashStores, databaseHashes, table, dontDiskVerify); } continue; } string sha1; // Disk if (extention == ".chd") { row["FileType"] = "DISK"; sha1 = chdMan.Hash(filename); row["SHA1"] = sha1; ProcessDisk(filename, sha1, row, "MachineAction", chdMan, databaseHashes.MachineDisk, hashStores.MachineDisk, dontDiskVerify); ProcessDisk(filename, sha1, row, "SoftwareAction", chdMan, databaseHashes.SoftwareDisk, hashStores.SoftwareDisk, dontDiskVerify); continue; } // ROM row["FileType"] = "ROM"; sha1 = Spludlow.Hashing.SHA1HexFile(filename); row["SHA1"] = sha1; ProcessRom(filename, sha1, row, "MachineAction", databaseHashes.MachineRom, hashStores.MachineRom); ProcessRom(filename, sha1, row, "SoftwareAction", databaseHashes.SoftwareRom, hashStores.SoftwareRom); } catch (Exception ee) { row["Error"] = ee.Message; Spludlow.Log.Error("MHS Import: " + filename, ee); } } } private static void ProcessRom(string filename, string sha1, DataRow row, string actionColumnName, HashSet databaseHashes, HashStore hashStore) { if (databaseHashes.Contains(sha1) == true) { if (hashStore.Exists(sha1) == false) { hashStore.Add(filename, false, sha1); row[actionColumnName] = "ADD"; } else { row[actionColumnName] = "HAVE"; } } else { row[actionColumnName] = "NO NEED"; } } private static void ProcessDisk(string filename, string sha1, DataRow row, string actionColumnName, MameChdMan chdMan, HashSet databaseHashes, HashStore hashStore, bool dontDiskVerify) { if (databaseHashes.Contains(sha1) == true) { if (hashStore.Exists(sha1) == false) { if (dontDiskVerify == true || chdMan.Verify(filename) == true) { hashStore.Add(filename, false, sha1); row[actionColumnName] = "ADD"; } else { row[actionColumnName] = "BAD"; } } else { row[actionColumnName] = "HAVE"; } } else { row[actionColumnName] = "NO NEED"; } } public static void ClearAttributes(string directory) { foreach (string filename in Directory.GetFiles(directory, "*", SearchOption.AllDirectories)) File.SetAttributes(filename, FileAttributes.Normal); } } }