Esempio n. 1
0
        /// <summary>
        /// Fixed a missing file inside a .ZIP file.
        /// </summary>
        /// <param name="fixZip">The RvFile of the actual .ZIP file that is being fixed.</param>
        /// <param name="fixZippedFile">A temp copy of the RvFile record of the actual compressed file inside the fixZip .zip that is about to be fixed.</param>
        /// <param name="tempFixZip">Is the new output archive file that is being created to fix this zip, that will become the new zip once done</param>
        /// <param name="filesUserForFix"></param>
        /// <param name="totalFixed"></param>
        /// <param name="errorMessage"></param>
        /// <returns></returns>
        public static ReturnCode CanBeFixed(RvFile fixZip, RvFile fixZippedFile, ref ICompress tempFixZip, Dictionary <string, RvFile> filesUserForFix, ref int totalFixed, out string errorMessage)
        {
            if (!(
                    fixZippedFile.DatStatus == DatStatus.InDatCollect && (fixZippedFile.GotStatus == GotStatus.NotGot || fixZippedFile.GotStatus == GotStatus.Corrupt)))
            {
                ReportError.SendAndShow("Error in Fix Rom Status " + fixZippedFile.RepStatus + " : " + fixZippedFile.DatStatus + " : " + fixZippedFile.GotStatus);
            }

            ReportError.LogOut("CanBeFixed:");
            ReportError.LogOut(fixZippedFile);

            if (tempFixZip == null)
            {
                ReturnCode ret1 = FixAZipFunctions.OpenTempFizZip(fixZip, out tempFixZip, out errorMessage);
                if (ret1 != ReturnCode.Good)
                {
                    return(ret1);
                }
            }

            List <RvFile> lstFixRomTable = FindSourceFile.GetFixFileList(fixZippedFile);

            ReportError.LogOut("CanBeFixed: picking from");
            ReportError.ReportList(lstFixRomTable);

            if (DBHelper.IsZeroLengthFile(fixZippedFile))
            {
                RvFile fileInZ = new RvFile(FileType.ZipFile)
                {
                    Size = 0
                };
                ReturnCode returnCode1 = FixFileUtils.CopyFile(fileInZ, tempFixZip, null, fixZippedFile, false, out errorMessage);

                switch (returnCode1)
                {
                case ReturnCode.Good:     // correct reply to continue;
                    break;

                case ReturnCode.Cancel:
                    return(returnCode1);

                default:
                    throw new FixAZip.ZipFileException(returnCode1, fixZippedFile.FullName + " " + fixZippedFile.RepStatus + " " + returnCode1 + " : " + errorMessage);
                }
            }
            else
            {
                if (lstFixRomTable.Count == 0)
                {
                    // thought we could fix it, turns out we cannot
                    fixZippedFile.GotStatus = GotStatus.NotGot;
                    errorMessage            = "";
                    return(ReturnCode.Good);
                }

                RvFile fileIn = FindSourceFile.FindSourceToUseForFix(fixZippedFile, lstFixRomTable);

                if (fileIn.FileType == FileType.SevenZipFile)
                {
                    ReturnCode returnCode1 = Decompress7ZipFile.DecompressSource7ZipFile(fileIn.Parent, false, out errorMessage);
                    if (returnCode1 != ReturnCode.Good)
                    {
                        ReportError.LogOut($"DecompressSource7Zip: OutputOutput {fixZip.FileName} return {returnCode1}");
                        return(returnCode1);
                    }
                    lstFixRomTable = FindSourceFile.GetFixFileList(fixZippedFile);
                    fileIn         = FindSourceFile.FindSourceToUseForFix(fixZippedFile, lstFixRomTable);
                }

                ReportError.LogOut("CanBeFixed: Copying from");
                ReportError.LogOut(fileIn);

                FixAZipFunctions.GetSourceDir(fileIn, out string sourceDir, out string sourceFile);

                string fixZipFullName = fixZip.TreeFullName;

                bool rawCopy = FixFileUtils.TestRawCopy(fileIn, fixZippedFile, false);
                Report.ReportProgress(new bgwShowFix(Path.GetDirectoryName(fixZipFullName), Path.GetFileName(fixZipFullName), fixZippedFile.Name, fixZippedFile.Size, rawCopy ? "<--Raw" : "<--Compress", sourceDir, sourceFile, fileIn.Name));

                fixZippedFile.FileTestFix(fileIn);

                ReturnCode returnCode = FixFileUtils.CopyFile(fileIn, tempFixZip, null, fixZippedFile, false, out errorMessage);
                switch (returnCode)
                {
                case ReturnCode.Good:     // correct reply so continue;
                    break;

                case ReturnCode.RescanNeeded:
                    ReportError.LogOut($"CanBeFixed: RescanNeeded");
                    return(returnCode);

                case ReturnCode.SourceDataStreamCorrupt:
                {
                    ReportError.LogOut($"CanBeFixed: Source Data Stream Corrupt /  CRC Error");
                    Report.ReportProgress(new bgwShowFixError("CRC Error"));
                    fileIn.GotStatus = GotStatus.Corrupt;
                    return(returnCode);
                }

                case ReturnCode.SourceCheckSumMismatch:
                {
                    ReportError.LogOut($"CanBeFixed: Source Checksum Mismatch / Fix file CRC was not as expected");
                    Report.ReportProgress(new bgwShowFixError("Fix file CRC was not as expected"));
                    return(returnCode);
                }

                case ReturnCode.DestinationCheckSumMismatch:
                {
                    ReportError.LogOut($"CanBeFixed: Destination Checksum Mismatch / Destination file CRC was not as expected");
                    Report.ReportProgress(new bgwShowFixError("Destination file CRC was not as expected"));
                    return(returnCode);
                }

                case ReturnCode.FileSystemError:
                {
                    ReportError.LogOut($"CanBeFixed: Source File Error {errorMessage}");
                    Report.ReportProgress(new bgwShowFixError($"CanBeFixed: Source File Error {errorMessage}"));
                    return(returnCode);
                }

                case ReturnCode.Cancel:
                    return(returnCode);

                default:
                    throw new FixAZip.ZipFileException(returnCode, fixZippedFile.FullName + " " + fixZippedFile.RepStatus + " " + returnCode + Environment.NewLine + errorMessage);
                }
            }

            //Check to see if the files used for fix, can now be set to delete
            foreach (RvFile f in lstFixRomTable)
            {
                string fn = f.TreeFullName;
                if (!filesUserForFix.ContainsKey(fn))
                {
                    filesUserForFix.Add(fn, f);
                }
            }
            totalFixed++;

            errorMessage = "";
            return(ReturnCode.Good);
        }
Esempio n. 2
0
        public static ReturnCode FixZip(RvFile fixZip, List <RvFile> fileProcessQueue, ref int totalFixed, out string errorMessage)
        {
            errorMessage = "";

            //Check for error status
            if (fixZip.DirStatus.HasUnknown())
            {
                return(ReturnCode.FindFixes); // Error
            }
            bool needsTrrntzipped = fixZip.ZipStatus != ZipStatus.TrrntZip && fixZip.GotStatus == GotStatus.Got && fixZip.DatStatus == DatStatus.InDatCollect && Settings.rvSettings.ConvertToTrrntzip;

            // zip files should not internally need a case fix, this code was added due to another error.
            // that while scanning matched no-case matches in a zip file.
            bool needsCaseFixed = false;

            for (int iRom = 0; iRom < fixZip.ChildCount; iRom++)
            {
                needsCaseFixed |= !string.IsNullOrEmpty(fixZip.Child(iRom).FileName);
            }

            // file corrupt and not in tosort
            //      if file cannot be fully fixed copy to corrupt
            //      process zipfile

            if (fixZip.GotStatus == GotStatus.Corrupt && fixZip.DatStatus != DatStatus.InToSort && !fixZip.DirStatus.HasFixable())
            {
                ReturnCode moveReturnCode = FixAZipFunctions.MoveZipToCorrupt(fixZip, out errorMessage);
                if (moveReturnCode != ReturnCode.Good)
                {
                    return(moveReturnCode);
                }
            }

            // has fixable
            //      process zipfile

            else if (fixZip.DirStatus.HasFixable())
            {
                // do nothing here but continue on to process zip.
            }

            // need trrntzipped
            //      process zipfile

            else if (needsTrrntzipped)
            {
                // rv7Zip format is not finalized yet so do not use
                if (!Settings.rvSettings.ConvertToRV7Z && (fixZip.FileType == FileType.SevenZip))
                //if (fixZip.FileType == FileType.SevenZip)
                {
                    needsTrrntzipped = false;
                }
                // do nothing here but continue on to process zip.
            }
            else if (needsCaseFixed)
            {
                // do nothing here but continue on to process zip.
            }


            // got empty zip that should be deleted
            //      process zipfile
            else if (fixZip.GotStatus == GotStatus.Got && fixZip.GotStatus != GotStatus.Corrupt && !fixZip.DirStatus.HasAnyFiles())
            {
                // do nothing here but continue on to process zip.
            }

            // else
            //      skip this zipfile
            else
            {
                // nothing can be done to return
                return(ReturnCode.Good);
            }

            if (!fixZip.DirStatus.HasFixable() && !needsTrrntzipped && !needsCaseFixed)
            {
                return(ReturnCode.Good);
            }

            string fixZipFullName = fixZip.TreeFullName;

            if (!fixZip.DirStatus.HasFixable() && needsTrrntzipped)
            {
                Report.ReportProgress(new bgwShowFix(Path.GetDirectoryName(fixZipFullName), Path.GetFileName(fixZipFullName), "", 0, "TrrntZipping", "", "", ""));
            }


            FixFileUtils.CheckCreateDirectories(fixZip.Parent);

            string filename = fixZip.FullName;

            if (fixZip.GotStatus == GotStatus.NotGot)
            {
                if (File.Exists(filename))
                {
                    errorMessage = "Unexpected file found in directory. Rescan needed.\n" + filename;
                    return(ReturnCode.RescanNeeded);
                }
            }

            ReportError.LogOut("");
            ReportError.LogOut(fixZipFullName + " : " + fixZip.RepStatus);
            ReportError.LogOut("------------------------------------------------------------");
            Debug.WriteLine(fixZipFullName + " : " + fixZip.RepStatus);
            ReportError.LogOut("Zip File Status Before Fix:");

            int deleteCount     = 0;
            int moveToSortCount = 0;
            int notThereCount   = 0;

            for (int intLoop = 0; intLoop < fixZip.ChildCount; intLoop++)
            {
                var fixZipFile = fixZip.Child(intLoop);
                ReportError.LogOut(fixZipFile);
                if (fixZipFile.RepStatus == RepStatus.MoveToSort || fixZipFile.RepStatus == RepStatus.MoveToCorrupt)
                {
                    moveToSortCount++;
                }
                if (fixZipFile.RepStatus == RepStatus.Delete)
                {
                    deleteCount++;
                }
                if (fixZipFile.RepStatus == RepStatus.Missing || fixZip.RepStatus == RepStatus.NotCollected)
                {
                    notThereCount++;
                }
            }
            ReportError.LogOut("");

            ReturnCode returnCode    = ReturnCode.Good;
            RepStatus  fileRepStatus = RepStatus.UnSet;

            ICompress tempFixZip       = null;
            ICompress toSortCorruptOut = null;
            ICompress toSortZipOut     = null;

            try
            {
                RvFile toSortGame        = null;
                RvFile toSortCorruptGame = null;

                Dictionary <string, RvFile> filesUsedForFix = new Dictionary <string, RvFile>();
                List <RvFile> fixZipTemp = new List <RvFile>();

                FileType fixFileType = fixZip.FileType;

                for (int iRom = 0; iRom < fixZip.ChildCount; iRom++)
                {
                    RvFile fixZippedFile = new RvFile(DBTypeGet.FileFromDir(fixFileType));
                    fixZip.Child(iRom).CopyTo(fixZippedFile);

                    fixZipTemp.Add(fixZippedFile);

                    ReportError.LogOut(fixZippedFile.RepStatus + " : " + fixZip.Child(iRom).FullName);

                    fileRepStatus = fixZippedFile.RepStatus;
                    switch (fixZippedFile.RepStatus)
                    {
                        #region Nothing to copy

                    // any file we do not have or do not want in the destination zip
                    case RepStatus.Missing:
                    case RepStatus.NotCollected:
                    case RepStatus.Rename:
                    case RepStatus.Delete:
                        if (!
                            (
                                // got the file in the original zip but will be deleting it
                                fixZippedFile.DatStatus == DatStatus.NotInDat && fixZippedFile.GotStatus == GotStatus.Got ||
                                fixZippedFile.DatStatus == DatStatus.NotInDat && fixZippedFile.GotStatus == GotStatus.Corrupt ||
                                fixZippedFile.DatStatus == DatStatus.InDatMerged && fixZippedFile.GotStatus == GotStatus.Got ||
                                fixZippedFile.DatStatus == DatStatus.InToSort && fixZippedFile.GotStatus == GotStatus.Got ||
                                fixZippedFile.DatStatus == DatStatus.InToSort && fixZippedFile.GotStatus == GotStatus.Corrupt ||

                                // do not have this file and cannot fix it here
                                fixZippedFile.DatStatus == DatStatus.InDatCollect && fixZippedFile.GotStatus == GotStatus.NotGot ||
                                fixZippedFile.DatStatus == DatStatus.InDatBad && fixZippedFile.GotStatus == GotStatus.NotGot ||
                                fixZippedFile.DatStatus == DatStatus.InDatMerged && fixZippedFile.GotStatus == GotStatus.NotGot
                            )
                            )
                        {
                            ReportError.SendAndShow("Error in Fix Rom Status " + fixZippedFile.RepStatus + " : " +
                                                    fixZippedFile.DatStatus + " : " + fixZippedFile.GotStatus);
                        }

                        if (fixZippedFile.RepStatus == RepStatus.Delete)
                        {
                            if (Settings.rvSettings.DetailedFixReporting)
                            {
                                Report.ReportProgress(new bgwShowFix(Path.GetDirectoryName(fixZipFullName), Path.GetFileName(fixZipFullName), fixZippedFile.Name, fixZippedFile.Size, "Delete", "", "", ""));
                            }

                            returnCode = FixFileUtils.DoubleCheckDelete(fixZip.Child(iRom), out errorMessage);
                            if (returnCode != ReturnCode.Good)
                            {
                                CloseZipFile(ref tempFixZip);
                                CloseToSortGame(toSortGame, ref toSortZipOut);
                                CloseToSortCorruptGame(toSortGame, ref toSortZipOut);
                                return(returnCode);
                            }
                        }

                        fixZippedFile.GotStatus = GotStatus.NotGot;
                        break;

                        #endregion


                    // any files we are just moving from the original zip to the destination zip
                    case RepStatus.Correct:
                    case RepStatus.InToSort:
                    case RepStatus.NeededForFix:
                    case RepStatus.Corrupt:
                    {
                        returnCode = FixAZipFunctions.CorrectZipFile(fixZip, fixZippedFile, ref tempFixZip, iRom, fileProcessQueue, out errorMessage);
                        if (returnCode != ReturnCode.Good)
                        {
                            CloseZipFile(ref tempFixZip);
                            CloseToSortGame(toSortGame, ref toSortZipOut);
                            CloseToSortCorruptGame(toSortGame, ref toSortZipOut);
                            return(returnCode);
                        }
                        break;
                    }

                    case RepStatus.CanBeFixed:
                    case RepStatus.CorruptCanBeFixed:
                    {
                        returnCode = FixAZipFunctions.CanBeFixed(fixZip, fixZippedFile, ref tempFixZip, filesUsedForFix, ref totalFixed, out errorMessage);
                        if (returnCode != ReturnCode.Good)
                        {
                            CloseZipFile(ref tempFixZip);
                            CloseToSortGame(toSortGame, ref toSortZipOut);
                            CloseToSortCorruptGame(toSortGame, ref toSortZipOut);
                            return(returnCode);
                        }
                        break;
                    }

                    case RepStatus.MoveToSort:
                    {
                        returnCode = FixAZipFunctions.MovetoSort(fixZip, fixZippedFile, ref toSortGame, ref toSortZipOut, iRom);
                        if (returnCode != ReturnCode.Good)
                        {
                            CloseZipFile(ref tempFixZip);
                            CloseToSortGame(toSortGame, ref toSortZipOut);
                            CloseToSortCorruptGame(toSortGame, ref toSortZipOut);
                            return(returnCode);
                        }
                        break;
                    }

                    case RepStatus.MoveToCorrupt:
                        returnCode = FixAZipFunctions.MoveToCorrupt(fixZip, fixZippedFile, ref toSortCorruptGame, ref toSortCorruptOut, iRom);
                        if (returnCode != ReturnCode.Good)
                        {
                            CloseZipFile(ref tempFixZip);
                            CloseToSortGame(toSortGame, ref toSortZipOut);
                            CloseToSortCorruptGame(toSortGame, ref toSortZipOut);
                            return(returnCode);
                        }
                        break;

                    default:
                        ReportError.UnhandledExceptionHandler(
                            "Unknown file status found " + fixZippedFile.RepStatus + " while fixing file " +
                            fixZip.Name + " Dat Status = " + fixZippedFile.DatStatus + " GotStatus " +
                            fixZippedFile.GotStatus);
                        break;
                    }

                    if (Report.CancellationPending())
                    {
                        tempFixZip?.ZipFileCloseFailed();
                        toSortZipOut?.ZipFileCloseFailed();
                        toSortCorruptOut?.ZipFileCloseFailed();
                        tempFixZip       = null;
                        toSortZipOut     = null;
                        toSortCorruptOut = null;

                        errorMessage = "Cancel";
                        return(ReturnCode.Cancel);
                    }
                }

                //if ToSort Zip Made then close the zip and add this new zip to the Database
                CloseToSortGame(toSortGame, ref toSortZipOut);

                //if Corrupt Zip Made then close the zip and add this new zip to the Database
                CloseToSortCorruptGame(toSortCorruptGame, ref toSortCorruptOut);

                #region Process original Zip

                if (File.Exists(filename))
                {
                    if (!File.SetAttributes(filename, FileAttributes.Normal))
                    {
                        int error = Error.GetLastError();
                        Report.ReportProgress(new bgwShowError(filename,
                                                               "Error Setting File Attributes to Normal. Deleting Original Fix File. Code " + error));
                    }

                    try
                    {
                        File.Delete(filename);
                    }
                    catch (Exception e)
                    {
                        errorMessage = "Error While trying to delete file " + filename + ". " + e.Message;

                        if (tempFixZip != null && tempFixZip.ZipOpen != ZipOpenType.Closed)
                        {
                            tempFixZip.ZipFileClose();
                            tempFixZip = null;
                        }

                        return(ReturnCode.RescanNeeded);
                    }
                }

                #endregion

                bool checkDelete = false;

                #region process the temp Zip rename it to the original Zip

                if (tempFixZip != null && tempFixZip.ZipOpen != ZipOpenType.Closed)
                {
                    string tempFilename = tempFixZip.ZipFilename;
                    tempFixZip.ZipFileClose();

                    if (tempFixZip.LocalFilesCount() > 0)
                    {
                        // now rename the temp fix file to the correct filename
                        File.Move(tempFilename, filename);
                        FileInfo nFile  = new FileInfo(filename);
                        RvFile   tmpZip = new RvFile(FileType.Zip)
                        {
                            Name             = Path.GetFileName(filename),
                            FileModTimeStamp = nFile.LastWriteTime
                        };
                        tmpZip.SetStatus(fixZip.DatStatus, GotStatus.Got);

                        fixZip.FileAdd(tmpZip, false);
                        fixZip.ZipStatus = tempFixZip.ZipStatus;
                    }
                    else
                    {
                        File.Delete(tempFilename);
                        checkDelete = true;
                    }

                    tempFixZip = null;
                }
                else
                {
                    checkDelete = true;
                }

                #endregion

                #region Now put the New Game Status information into the Database.

                int intLoopFix = 0;
                foreach (RvFile tmpZip in fixZipTemp)
                {
                    tmpZip.CopyTo(fixZip.Child(intLoopFix));

                    if (fixZip.Child(intLoopFix).RepStatus == RepStatus.Deleted)
                    {
                        if (fixZip.Child(intLoopFix).FileRemove() == EFile.Delete)
                        {
                            fixZip.ChildRemove(intLoopFix);
                            continue;
                        }
                    }

                    intLoopFix++;
                }

                #endregion

                List <RvFile> usedFiles = filesUsedForFix.Values.ToList();

                FixFileUtils.CheckFilesUsedForFix(usedFiles, fileProcessQueue, false);

                if (checkDelete)
                {
                    FixFileUtils.CheckDeleteFile(fixZip);
                }

                ReportError.LogOut("");
                ReportError.LogOut("Zip File Status After Fix:");
                for (int intLoop = 0; intLoop < fixZip.ChildCount; intLoop++)
                {
                    ReportError.LogOut(fixZip.Child(intLoop));
                }

                ReportError.LogOut("");

                return(ReturnCode.Good);
            }
            catch (ZipFileException ex)
            {
                tempFixZip?.ZipFileCloseFailed();
                toSortZipOut?.ZipFileCloseFailed();
                toSortCorruptOut?.ZipFileCloseFailed();
                tempFixZip       = null;
                toSortZipOut     = null;
                toSortCorruptOut = null;

                errorMessage = ex.Message;
                return(ex.returnCode);
            }
            catch (Exception ex)
            {
                tempFixZip?.ZipFileCloseFailed();
                toSortZipOut?.ZipFileCloseFailed();
                toSortCorruptOut?.ZipFileCloseFailed();
                tempFixZip       = null;
                toSortZipOut     = null;
                toSortCorruptOut = null;

                errorMessage = ex.Message;
                return(ReturnCode.LogicError);
            }
            finally
            {
                if (tempFixZip != null)
                {
                    ReportError.UnhandledExceptionHandler($"{tempFixZip.ZipFilename} tempZipOut was left open, ZipFile= {fixZipFullName} , fileRepStatus= {fileRepStatus} , returnCode= {returnCode}");
                }
                if (toSortZipOut != null)
                {
                    ReportError.UnhandledExceptionHandler($"{toSortZipOut.ZipFilename} toSortZipOut was left open");
                }
                if (toSortCorruptOut != null)
                {
                    ReportError.UnhandledExceptionHandler($"{toSortCorruptOut.ZipFilename} toSortCorruptOut was left open");
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Fixed a missing file inside a .ZIP file.
        /// </summary>
        /// <param name="fixZip">The RvFile of the actual .ZIP file that is being fixed.</param>
        /// <param name="fixZippedFile">A temp copy of the RvFile record of the actual compressed file inside the fixZip .zip that is about to be fixed.</param>
        /// <param name="tempFixZip">Is the new output archive file that is being created to fix this zip, that will become the new zip once done</param>
        /// <param name="iRom"></param>
        /// <param name="filesUserForFix"></param>
        /// <param name="errorMessage"></param>
        /// <returns></returns>
        public static ReturnCode CorrectZipFile(RvFile fixZip, RvFile fixZippedFile, ref ICompress tempFixZip, int iRom, Dictionary <string, RvFile> filesUserForFix, out string errorMessage)
        {
            if (!(
                    fixZippedFile.DatStatus == DatStatus.InDatCollect && fixZippedFile.GotStatus == GotStatus.Got ||
                    fixZippedFile.DatStatus == DatStatus.InDatMerged && fixZippedFile.GotStatus == GotStatus.Got ||
                    fixZippedFile.DatStatus == DatStatus.NotInDat && fixZippedFile.GotStatus == GotStatus.Got ||
                    fixZippedFile.DatStatus == DatStatus.InToSort && fixZippedFile.GotStatus == GotStatus.Got ||
                    fixZippedFile.DatStatus == DatStatus.InToSort && fixZippedFile.GotStatus == GotStatus.Corrupt))
            {
                ReportError.SendAndShow("Error in Fix Rom Status " + fixZippedFile.RepStatus + " : " + fixZippedFile.DatStatus + " : " + fixZippedFile.GotStatus);
            }

            ReportError.LogOut("CorrectZipFile:");
            ReportError.LogOut(fixZippedFile);

            if (fixZippedFile.GotStatus == GotStatus.Corrupt && fixZippedFile.FileType == FileType.SevenZipFile)
            {
                fixZippedFile.GotStatus = GotStatus.NotGot; // Changes RepStatus to Deleted
                errorMessage            = "";
                return(ReturnCode.Good);
            }

            if (tempFixZip == null)
            {
                ReturnCode ret1 = FixAZipFunctions.OpenTempFizZip(fixZip, out tempFixZip, out errorMessage);
                if (ret1 != ReturnCode.Good)
                {
                    return(ret1);
                }
            }

            RvFile        fileIn         = fixZip.Child(iRom);
            List <RvFile> lstFixRomTable = null;

            if (fileIn.FileType == FileType.SevenZipFile && fileIn.Size > 0)
            {
                lstFixRomTable = FindSourceFile.GetFixFileList(fixZippedFile);
                ReportError.LogOut("CorrectZipFile: picking from");
                ReportError.ReportList(lstFixRomTable);

                fileIn = FindSourceFile.FindSourceToUseForFix(fixZippedFile, lstFixRomTable);

                if (fileIn.FileType == FileType.SevenZipFile)
                {
                    ReturnCode returnCode1 = Decompress7ZipFile.DecompressSource7ZipFile(fixZip, true, out errorMessage);
                    if (returnCode1 != ReturnCode.Good)
                    {
                        ReportError.LogOut($"DecompressSource7Zip: OutputOutput {fixZip.FileName} return {returnCode1}");
                        return(returnCode1);
                    }

                    lstFixRomTable = FindSourceFile.GetFixFileList(fixZippedFile);
                    fileIn         = FindSourceFile.FindSourceToUseForFix(fixZippedFile, lstFixRomTable);
                }
            }

            ReportError.LogOut("Copying from");
            ReportError.LogOut(fileIn);

            FixAZipFunctions.GetSourceDir(fileIn, out string sourceDir, out string sourceFile);

            bool rawCopyForce = fixZippedFile.RepStatus == RepStatus.InToSort || fixZippedFile.RepStatus == RepStatus.Corrupt;

            if (Settings.rvSettings.DetailedFixReporting)
            {
                string fixZipFullName = fixZip.TreeFullName;

                bool rawCopy = FixFileUtils.TestRawCopy(fileIn, fixZippedFile, rawCopyForce);
                Report.ReportProgress(new bgwShowFix(Path.GetDirectoryName(fixZipFullName), Path.GetFileName(fixZipFullName), fixZippedFile.Name, fixZippedFile.Size, rawCopy ? "<<--Raw" : "<<--Compress", sourceDir, sourceFile, fileIn.Name));
            }

            RepStatus  originalStatus = fixZippedFile.RepStatus;
            ReturnCode returnCode     = FixFileUtils.CopyFile(fileIn, tempFixZip, null, fixZippedFile, rawCopyForce, out errorMessage);

            switch (returnCode)
            {
            case ReturnCode.Good:     // correct reply to continue;
                if (originalStatus == RepStatus.NeededForFix)
                {
                    fixZippedFile.RepStatus = RepStatus.NeededForFix;
                }
                break;

            case ReturnCode.SourceDataStreamCorrupt:
            {
                ReportError.LogOut($"CorrectZipFile: Source Data Stream Corrupt /  CRC Error");
                Report.ReportProgress(new bgwShowFixError("CRC Error"));
                RvFile tFile = fixZip.Child(iRom);
                tFile.GotStatus = GotStatus.Corrupt;
                break;
            }

            case ReturnCode.SourceCheckSumMismatch:
            {
                ReportError.LogOut($"CorrectZipFile: Source Checksum Mismatch / Fix file CRC was not as expected");
                Report.ReportProgress(new bgwShowFixError("Fix file CRC was not as expected"));
                break;
            }

            case ReturnCode.FileSystemError:
            {
                ReportError.LogOut($"CorrectZipFile: Source File Error {errorMessage}");
                Report.ReportProgress(new bgwShowFixError($"CorrectZipFile: Source File Error {errorMessage}"));
                return(returnCode);
            }

            case ReturnCode.Cancel:
                return(returnCode);

            default:
                throw new FixAZip.ZipFileException(returnCode, fixZippedFile.FullName + " " + fixZippedFile.RepStatus + " " + returnCode + " : " + errorMessage);
            }



            if (lstFixRomTable != null)
            {
                foreach (RvFile f in lstFixRomTable)
                {
                    string fn = f.TreeFullName;
                    if (!filesUserForFix.ContainsKey(fn))
                    {
                        filesUserForFix.Add(fn, f);
                    }
                }
            }


            return(returnCode);
        }
Esempio n. 4
0
        public static ReturnCode MovetoSort(RvFile fixZip, RvFile fixZippedFile, ref RvFile toSortGame, ref ICompress toSortZipOut, int iRom, Dictionary <string, RvFile> filesUserForFix)
        {
            if (!(fixZippedFile.DatStatus == DatStatus.NotInDat && fixZippedFile.GotStatus == GotStatus.Got))
            {
                ReportError.SendAndShow("Error in Fix Rom Status " + fixZippedFile.RepStatus + " : " + fixZippedFile.DatStatus + " : " + fixZippedFile.GotStatus);
            }

            ReportError.LogOut("MovetoSort:");
            ReportError.LogOut(fixZippedFile);
            // move the rom out to the To Sort Directory

            string toSortFullName;

            if (toSortGame == null)
            {
                ReturnCode retCode = FixFileUtils.CreateToSortDirs(fixZip, out RvFile outDir, out string toSortFileName);
                if (retCode != ReturnCode.Good)
                {
                    return(retCode);
                }

                toSortGame = new RvFile(fixZip.FileType)
                {
                    Parent    = outDir,
                    Name      = toSortFileName,
                    DatStatus = DatStatus.InToSort,
                    GotStatus = GotStatus.Got
                };
                toSortFullName = Path.Combine(outDir.FullName, toSortGame.Name);
            }
            else
            {
                toSortFullName = toSortZipOut.ZipFilename;
            }

            // this needs header / alt info added.
            RvFile toSortRom = new RvFile(fixZippedFile.FileType)
            {
                Name           = fixZippedFile.Name,
                Size           = fixZippedFile.Size,
                CRC            = fixZippedFile.CRC,
                SHA1           = fixZippedFile.SHA1,
                MD5            = fixZippedFile.MD5,
                HeaderFileType = fixZippedFile.HeaderFileType,
                AltSize        = fixZippedFile.AltSize,
                AltCRC         = fixZippedFile.AltCRC,
                AltSHA1        = fixZippedFile.AltSHA1,
                AltMD5         = fixZippedFile.AltMD5,
                FileGroup      = fixZippedFile.FileGroup
            };

            toSortRom.SetStatus(DatStatus.InToSort, GotStatus.Got);
            toSortRom.FileStatusSet(
                FileStatus.HeaderFileTypeFromHeader |
                FileStatus.SizeFromHeader | FileStatus.SizeVerified |
                FileStatus.CRCFromHeader | FileStatus.CRCVerified |
                FileStatus.SHA1FromHeader | FileStatus.SHA1Verified |
                FileStatus.MD5FromHeader | FileStatus.MD5Verified |
                FileStatus.AltSizeFromHeader | FileStatus.AltSizeVerified |
                FileStatus.AltCRCFromHeader | FileStatus.AltCRCVerified |
                FileStatus.AltSHA1FromHeader | FileStatus.AltSHA1Verified |
                FileStatus.AltMD5FromHeader | FileStatus.AltMD5Verified
                , fixZippedFile);


            ReturnCode returnCode;
            string     errorMessage;

            if (toSortZipOut == null)
            {
                returnCode = FixAZipFunctions.OpenOutputZip(toSortGame, toSortFullName, out toSortZipOut, out errorMessage);
                if (returnCode != ReturnCode.Good)
                {
                    throw new FixAZip.ZipFileException(returnCode, fixZippedFile.FullName + " " + fixZippedFile.RepStatus + " " + returnCode + Environment.NewLine + errorMessage);
                }
            }

            RvFile        fileIn         = fixZip.Child(iRom);
            List <RvFile> lstFixRomTable = null;

            if (fileIn.FileType == FileType.SevenZipFile && fileIn.Size > 0)
            {
                lstFixRomTable = FindSourceFile.GetFixFileList(fixZippedFile);
                ReportError.LogOut("CorrectZipFile: picking from");
                ReportError.ReportList(lstFixRomTable);

                fileIn = FindSourceFile.FindSourceToUseForFix(fixZippedFile, lstFixRomTable);

                if (fileIn.FileType == FileType.SevenZipFile)
                {
                    ReturnCode returnCode1 = Decompress7ZipFile.DecompressSource7ZipFile(fixZip, true, out errorMessage);
                    if (returnCode1 != ReturnCode.Good)
                    {
                        ReportError.LogOut($"DecompressSource7Zip: OutputOutput {fixZip.FileName} return {returnCode1}");
                        return(returnCode1);
                    }

                    lstFixRomTable = FindSourceFile.GetFixFileList(fixZippedFile);
                    fileIn         = FindSourceFile.FindSourceToUseForFix(fixZippedFile, lstFixRomTable);
                }
            }

            string fixZipFullName = fixZip.TreeFullName;

            Report.ReportProgress(new bgwShowFix(Path.GetDirectoryName(fixZipFullName), Path.GetFileName(fixZipFullName), fixZippedFile.Name, fixZippedFile.Size, "Raw-->", Path.GetDirectoryName(toSortFullName), Path.GetFileName(toSortFullName), toSortRom.Name));

            returnCode = FixFileUtils.CopyFile(fileIn, toSortZipOut, null, toSortRom, true, out errorMessage);
            switch (returnCode)
            {
            case ReturnCode.Good:     // correct reply to continue;
                break;

            default:
                throw new FixAZip.ZipFileException(returnCode, fixZippedFile.FullName + " " + fixZippedFile.RepStatus + " " + returnCode + Environment.NewLine + errorMessage);
            }
            fixZippedFile.GotStatus = GotStatus.NotGot; // Changes RepStatus to Deleted

            fixZippedFile.FileGroup.Files.Add(toSortRom);
            toSortGame.ChildAdd(toSortRom);

            if (lstFixRomTable != null)
            {
                foreach (RvFile f in lstFixRomTable)
                {
                    string fn = f.TreeFullName;
                    if (!filesUserForFix.ContainsKey(fn))
                    {
                        filesUserForFix.Add(fn, f);
                    }
                }
            }


            return(ReturnCode.Good);
        }