diff --git a/RomRepoMgr.Core/Workers/DatImporter.cs b/RomRepoMgr.Core/Workers/DatImporter.cs index dc0af32..e7e74ab 100644 --- a/RomRepoMgr.Core/Workers/DatImporter.cs +++ b/RomRepoMgr.Core/Workers/DatImporter.cs @@ -224,6 +224,8 @@ namespace RomRepoMgr.Core.Workers Dictionary pendingFilesByMd5 = new Dictionary(); Dictionary pendingFilesByCrc = new Dictionary(); List pendingFiles = new List(); + List newFiles = new List(); + List newFilesByMachine = new List(); foreach(Rom rom in roms) { @@ -327,29 +329,12 @@ namespace RomRepoMgr.Core.Workers file = pendingFiles.FirstOrDefault(f => f.Crc32 == rom.CRC && f.Size == uSize); } - if(file == null && - rom.SHA512 != null) - file = Context.Singleton.Files.FirstOrDefault(f => f.Sha512 == rom.SHA512 && f.Size == uSize); - - if(file == null && - rom.SHA384 != null) - file = Context.Singleton.Files.FirstOrDefault(f => f.Sha384 == rom.SHA384 && f.Size == uSize); - - if(file == null && - rom.SHA256 != null) - file = Context.Singleton.Files.FirstOrDefault(f => f.Sha256 == rom.SHA256 && f.Size == uSize); - - if(file == null && - rom.SHA1 != null) - file = Context.Singleton.Files.FirstOrDefault(f => f.Sha1 == rom.SHA1 && f.Size == uSize); - - if(file == null && - rom.MD5 != null) - file = Context.Singleton.Files.FirstOrDefault(f => f.Md5 == rom.MD5 && f.Size == uSize); - - if(file == null && - rom.CRC != null) - file = Context.Singleton.Files.FirstOrDefault(f => f.Crc32 == rom.CRC && f.Size == uSize); + file ??= Context.Singleton.Files.FirstOrDefault(f => ((rom.SHA512 != null && f.Sha512 == rom.SHA512) || + (rom.SHA384 != null && f.Sha384 == rom.SHA384) || + (rom.SHA256 != null && f.Sha256 == rom.SHA256) || + (rom.SHA1 != null && f.Sha1 == rom.SHA1) || + (rom.MD5 != null && f.Md5 == rom.MD5) || + (rom.CRC != null && f.Crc32 == rom.CRC)) && f.Size == uSize); if(file == null) { @@ -366,7 +351,9 @@ namespace RomRepoMgr.Core.Workers UpdatedOn = DateTime.UtcNow }; - Context.Singleton.Files.Add(file); + newFiles.Add(file); + + //Context.Singleton.Files.Add(file); } if(string.IsNullOrEmpty(file.Crc32) && @@ -411,7 +398,7 @@ namespace RomRepoMgr.Core.Workers file.UpdatedOn = DateTime.UtcNow; } - Context.Singleton.FilesByMachines.Add(new FileByMachine + newFilesByMachine.Add(new FileByMachine { File = file, Machine = machine, @@ -443,6 +430,9 @@ namespace RomRepoMgr.Core.Workers SetIndeterminateProgress?.Invoke(this, System.EventArgs.Empty); + Context.Singleton.Files.AddRange(newFiles); + Context.Singleton.FilesByMachines.AddRange(newFilesByMachine); + Context.Singleton.SaveChanges(); SetProgressBounds?.Invoke(this, new ProgressBoundsEventArgs diff --git a/RomRepoMgr.Core/Workers/FileImporter.cs b/RomRepoMgr.Core/Workers/FileImporter.cs index 8c48cb5..263498b 100644 --- a/RomRepoMgr.Core/Workers/FileImporter.cs +++ b/RomRepoMgr.Core/Workers/FileImporter.cs @@ -17,9 +17,10 @@ namespace RomRepoMgr.Core.Workers { public class FileImporter { - const long BUFFER_SIZE = 131072; - readonly bool _deleteAfterImport; - readonly bool _onlyKnown; + const long BUFFER_SIZE = 131072; + readonly bool _deleteAfterImport; + readonly List _newFiles; + readonly bool _onlyKnown; readonly Dictionary _pendingFiles; @@ -30,6 +31,7 @@ namespace RomRepoMgr.Core.Workers public FileImporter(bool onlyKnown, bool deleteAfterImport) { _pendingFiles = new Dictionary(); + _newFiles = new List(); _onlyKnown = onlyKnown; _deleteAfterImport = deleteAfterImport; _position = 0; @@ -262,19 +264,13 @@ namespace RomRepoMgr.Core.Workers bool knownFile = _pendingFiles.TryGetValue(checksums[ChecksumType.Sha512], out DbFile dbFile); - dbFile ??= - ((((Context.Singleton.Files.FirstOrDefault(f => f.Size == uSize && - f.Sha512 == checksums[ChecksumType.Sha512]) ?? - Context.Singleton.Files.FirstOrDefault(f => f.Size == uSize && - f.Sha384 == checksums[ChecksumType.Sha384])) ?? - Context.Singleton.Files.FirstOrDefault(f => f.Size == uSize && - f.Sha256 == checksums[ChecksumType.Sha256])) ?? - Context.Singleton.Files.FirstOrDefault(f => f.Size == uSize && - f.Sha1 == checksums[ChecksumType.Sha1])) ?? - Context.Singleton.Files.FirstOrDefault(f => f.Size == uSize && - f.Md5 == checksums[ChecksumType.Md5])) ?? - Context.Singleton.Files.FirstOrDefault(f => f.Size == uSize && - f.Crc32 == checksums[ChecksumType.Crc32]); + dbFile ??= Context.Singleton.Files.FirstOrDefault(f => (f.Sha512 == checksums[ChecksumType.Sha512] || + f.Sha384 == checksums[ChecksumType.Sha384] || + f.Sha256 == checksums[ChecksumType.Sha256] || + f.Sha1 == checksums[ChecksumType.Sha1] || + f.Md5 == checksums[ChecksumType.Md5] || + f.Crc32 == checksums[ChecksumType.Crc32]) && + f.Size == uSize); if(dbFile == null) { @@ -384,7 +380,7 @@ namespace RomRepoMgr.Core.Workers dbFile.UpdatedOn = DateTime.UtcNow; if(!fileInDb) - Context.Singleton.Files.Add(dbFile); + _newFiles.Add(dbFile); inFs.Close(); @@ -449,7 +445,7 @@ namespace RomRepoMgr.Core.Workers dbFile.UpdatedOn = DateTime.UtcNow; if(!fileInDb) - Context.Singleton.Files.Add(dbFile); + _newFiles.Add(dbFile); if(_deleteAfterImport) File.Delete(path); @@ -473,7 +469,10 @@ namespace RomRepoMgr.Core.Workers Message = Localization.SavingChangesToDatabase }); + Context.Singleton.Files.AddRange(_newFiles); Context.Singleton.SaveChanges(); + + _newFiles.Clear(); } string GetArchiveFormat(string path, out long counter)