示例#1
0
        public int GetRomFromFile(string foundFilePath)
        {
            int total = 0;

            var sha1       = Audit.GetHash(foundFilePath, HashOption.SHA1, (int)HeaderLength);
            Rom matchedRom = Roms.FirstOrDefault(x => sha1.Equals(x.SHA1, StringComparison.OrdinalIgnoreCase));

            if (matchedRom == null && HeaderLength > 0)
            {
                sha1       = Audit.GetHash(foundFilePath, HashOption.SHA1, 0);
                matchedRom = Roms.FirstOrDefault(x => sha1.Equals(x.SHA1, StringComparison.OrdinalIgnoreCase));
            }

            // Have found a match so do this stuff with it
            if (matchedRom != null)
            {
                // Check whether the release has a filename stored and that file exists
                if (string.IsNullOrEmpty(matchedRom.FileName) || !File.Exists(matchedRom.FilePath))
                {
                    string extension = Path.GetExtension(foundFilePath);
                    matchedRom.StoreFileName(extension);

                    if (foundFilePath != matchedRom.FilePath)
                    {
                        if (File.Exists(matchedRom.FilePath))
                        {
                            File.Move(matchedRom.FilePath, FileLocation.RomsBackup + matchedRom.FileName);
                        }

                        if (matchedRom.Platform_ID == CONSTANTS.LYNX_PLATFORM_ID)
                        {
                            string tempFile  = "lnxtmp.lyx";
                            string tempFile2 = "lnxtmp.lnx";
                            string tempPath  = Path.GetDirectoryName(FileLocation.HandyConverter) + @"\" + tempFile;
                            string tempPath2 = Path.GetDirectoryName(FileLocation.HandyConverter) + @"\" + tempFile2;

                            File.Delete(tempPath);
                            Thread.Sleep(100);
                            File.Copy(foundFilePath, tempPath);
                            Thread.Sleep(100);
                            if (Handy.ConvertLynx(tempFile))
                            {
                                File.Move(tempPath2, matchedRom.FilePath);
                            }
                        }

                        else
                        {
                            File.Copy(foundFilePath, matchedRom.FilePath);
                        }
                    }
                    total = 1;
                }
            }
            return(total);
        }
示例#2
0
        public int GetRomsFromZipFile(string filename)
        {
            int    total = 0;
            string sha1;
            Rom    matchedRom;

            try
            {
                using (ZipArchive archive = ZipFile.Open(filename, ZipArchiveMode.Read))
                {
                    foreach (ZipArchiveEntry entry in archive.Entries)
                    {
                        using (Stream stream = entry.Open())
                            using (MemoryStream memoryStream = new MemoryStream())
                            {
                                int count;
                                do
                                {
                                    byte[] buffer = new byte[1024];
                                    count = stream.Read(buffer, 0, 1024);
                                    memoryStream.Write(buffer, 0, count);
                                } while (stream.CanRead && count > 0);

                                // TODO Some roms in DB have no SHA1 this is a substantial bug

                                sha1       = Audit.GetHash(memoryStream, HashOption.SHA1, (int)HeaderLength);
                                matchedRom = Roms.FirstOrDefault(x => sha1.Equals(x.SHA1, StringComparison.OrdinalIgnoreCase));

                                if (matchedRom == null && HeaderLength > 0)
                                {
                                    sha1 = Audit.GetHash(memoryStream, HashOption.SHA1, 0);

                                    matchedRom = Roms.FirstOrDefault(x => sha1.Equals(x.SHA1, StringComparison.OrdinalIgnoreCase));
                                }

                                // Have found a match so do this stuff with it
                                if (matchedRom != null)
                                {
                                    // Check that the release has no filename, or the file doesn't yet exist
                                    if (string.IsNullOrEmpty(matchedRom.FileName) || !File.Exists(matchedRom.FilePath))
                                    {
                                        string extension = Path.GetExtension(entry.Name);
                                        matchedRom.StoreFileName(extension);

                                        if (File.Exists(matchedRom.FilePath))
                                        {
                                            File.Move(matchedRom.FilePath, FileLocation.RomsBackup + matchedRom.FileName);
                                        }

                                        if (matchedRom.Platform_ID == CONSTANTS.LYNX_PLATFORM_ID)
                                        {
                                            //TODO: This looks pretty shady
                                            string tempFile  = "lnxtmp.lyx";
                                            string tempFile2 = "lnxtmp.lnx";
                                            string tempPath  = Path.GetDirectoryName(FileLocation.HandyConverter) + @"\" + tempFile;
                                            string tempPath2 = Path.GetDirectoryName(FileLocation.HandyConverter) + @"\" + tempFile2;
                                            File.Delete(tempPath);
                                            File.Delete(tempPath2);

                                            entry.ExtractToFile(tempPath);
                                            Handy.ConvertLynx(tempFile);
                                            File.Move(tempPath, matchedRom.FilePath);
                                        }
                                        else
                                        {
                                            entry.ExtractToFile(matchedRom.FilePath);
                                        }
                                        total += 1;
                                    }
                                }
                            }
                    }
                }
            }

            catch (Exception)
            {
            }

            return(total);
        }