Example #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.Platform_ID.Lynx)
                    {
                        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);
    }
Example #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();
                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.Platform_ID.Lynx)
                        {
                            //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);
    }