Пример #1
0
        private static async Task <FileInfo> EnsureFileIsExtracted(FileInfo file)
        {
            if (file.FullName.EndsWith(".age4scn", StringComparison.OrdinalIgnoreCase) &&
                L33TZipUtils.IsL33TZipFile(file.FullName))
            {
                var data = await L33TZipUtils.ExtractL33TZipFileAsync(file.FullName);

                File.Delete(file.FullName);
                File.WriteAllBytes(file.FullName, data);
                return(new FileInfo(file.FullName));
            }

            return(file);
        }
        public static async Task <bool> ScanAndRepairFile(GameFileInfo fileInfo, string gameFilePath,
                                                          PathTransformer pathTransformer, IProgress <ScanSubProgress> progress = null,
                                                          int concurrentDownload = 0, CancellationToken ct = default)
        {
            var filePath = Path.Combine(gameFilePath, pathTransformer.TransformPath(fileInfo.FileName));

            //#1 Check File
            ct.ThrowIfCancellationRequested();
            progress?.Report(new ScanSubProgress(ScanSubProgressStep.Check, 0));

            Progress <double> subProgressCheck = null;

            if (progress != null)
            {
                subProgressCheck = new Progress <double>();
                subProgressCheck.ProgressChanged += (o, d) =>
                {
                    progress.Report(new ScanSubProgress(
                                        ScanSubProgressStep.Check, d));
                };
            }

            if (await RunFileCheck(filePath, fileInfo.Size, fileInfo.Crc32, ct, subProgressCheck))
            {
                progress?.Report(new ScanSubProgress(ScanSubProgressStep.End, 100));
                return(true);
            }

            //#2 Download File
            ct.ThrowIfCancellationRequested();
            progress?.Report(new ScanSubProgress(ScanSubProgressStep.Download, 0));

            var tempFileName = Path.Combine(GameScannerTempPath, $"{fileInfo.FileName.GetHashCode():X4}.tmp");

            if (File.Exists(tempFileName))
            {
                File.Delete(tempFileName);
            }

            var fileDownloader = concurrentDownload == 1
                ? (IFileDownloader) new SimpleFileDownloader(fileInfo.HttpLink, tempFileName)
                : new ChunkFileDownloader(fileInfo.HttpLink, tempFileName, GameScannerTempPath,
                                          concurrentDownload);

            if (progress != null)
            {
                fileDownloader.ProgressChanged += (sender, eventArg) =>
                {
                    switch (fileDownloader.State)
                    {
                    case FileDownloaderState.Invalid:
                    case FileDownloaderState.Download:
                        progress.Report(new ScanSubProgress(
                                            ScanSubProgressStep.Download, fileDownloader.DownloadProgress * 0.99,
                                            new ScanDownloadProgress(fileDownloader.DownloadSize, fileDownloader.BytesDownloaded,
                                                                     fileDownloader.DownloadSpeed)));
                        break;

                    case FileDownloaderState.Finalize:
                        progress.Report(new ScanSubProgress(
                                            ScanSubProgressStep.Download, 99,
                                            new ScanDownloadProgress(fileDownloader.DownloadSize, fileDownloader.BytesDownloaded,
                                                                     0)));
                        break;

                    case FileDownloaderState.Complete:
                        progress.Report(new ScanSubProgress(
                                            ScanSubProgressStep.Download, 100,
                                            new ScanDownloadProgress(fileDownloader.DownloadSize, fileDownloader.BytesDownloaded,
                                                                     0)));
                        break;

                    case FileDownloaderState.Error:
                    case FileDownloaderState.Abort:
                        break;

                    default:
                        throw new ArgumentOutOfRangeException(nameof(fileDownloader.State),
                                                              fileDownloader.State, null);
                    }
                }
            }
            ;

            await fileDownloader.DownloadAsync(ct);


            //#3 Check Downloaded File
            ct.ThrowIfCancellationRequested();

            Progress <double> subProgressCheckDown = null;

            if (progress != null)
            {
                progress.Report(new ScanSubProgress(ScanSubProgressStep.CheckDownload, 0));

                subProgressCheckDown = new Progress <double>();
                subProgressCheckDown.ProgressChanged += (o, d) =>
                {
                    progress.Report(new ScanSubProgress(
                                        ScanSubProgressStep.CheckDownload, d));
                };
            }

            try
            {
                await EnsureValidGameFile(tempFileName, fileInfo.BinSize, fileInfo.BinCrc32, ct, subProgressCheckDown);
            }
            catch
            {
                if (File.Exists(tempFileName))
                {
                    File.Delete(tempFileName);
                }

                throw;
            }

            //#4 Extract downloaded file
            ct.ThrowIfCancellationRequested();
            if (L33TZipUtils.IsL33TZipFile(tempFileName))
            {
                var tempFileName2 = $"{tempFileName.Replace(".tmp", string.Empty)}.ext.tmp";
                //
                Progress <double> extractProgress = null;
                if (progress != null)
                {
                    progress.Report(new ScanSubProgress(
                                        ScanSubProgressStep.ExtractDownload, 0));

                    extractProgress = new Progress <double>();
                    extractProgress.ProgressChanged += (o, d) =>
                    {
                        progress.Report(new ScanSubProgress(
                                            ScanSubProgressStep.ExtractDownload, d));
                    };
                }

                await L33TZipUtils.ExtractL33TZipFileAsync(tempFileName, tempFileName2, ct, extractProgress);

                //#4.1 Check Extracted File
                ct.ThrowIfCancellationRequested();
                Progress <double> subProgressCheckExt = null;
                if (progress != null)
                {
                    progress.Report(new ScanSubProgress(
                                        ScanSubProgressStep.CheckExtractDownload, 0));

                    subProgressCheckExt = new Progress <double>();
                    subProgressCheckExt.ProgressChanged += (o, d) =>
                    {
                        progress.Report(new ScanSubProgress(
                                            ScanSubProgressStep.CheckExtractDownload, d));
                    };
                }

                await EnsureValidGameFile(tempFileName2, fileInfo.Size, fileInfo.Crc32, ct, subProgressCheckExt);

                File.Delete(tempFileName);

                tempFileName = tempFileName2;
            }

            //#5 Move new file to game folder
            ct.ThrowIfCancellationRequested();

            progress?.Report(new ScanSubProgress(
                                 ScanSubProgressStep.Finalize, 0));

            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            else
            {
                var pathName = Path.GetDirectoryName(filePath);
                if (!string.IsNullOrEmpty(pathName) && !Directory.Exists(pathName))
                {
                    Directory.CreateDirectory(pathName);
                }
            }

            File.Move(tempFileName, filePath);

            //#6 End
            progress?.Report(new ScanSubProgress(
                                 ScanSubProgressStep.End, 100));

            return(true);
        }
Пример #3
0
        private static async Task ConvertFile(bool convertFile, string tempFileName, BarEntry barFileInfo, string filePath)
        {
            //Convert file
            if (convertFile)
            {
                if (L33TZipUtils.IsL33TZipFile(tempFileName) &&
                    !barFileInfo.FileName.EndsWith(".age4scn", StringComparison.OrdinalIgnoreCase))
                {
                    var rnd           = new Random(Guid.NewGuid().GetHashCode());
                    var tempFileName2 =
                        Path.Combine(Path.GetTempPath(),
                                     $"{Path.GetFileName(barFileInfo.FileName)}-{rnd.Next()}.tmp");
                    await L33TZipUtils.ExtractL33TZipFileAsync(tempFileName, tempFileName2);

                    if (File.Exists(tempFileName))
                    {
                        File.Delete(tempFileName);
                    }

                    tempFileName = tempFileName2;
                }

                if (barFileInfo.FileName.EndsWith(".xmb", StringComparison.OrdinalIgnoreCase))
                {
                    try
                    {
                        var rnd           = new Random(Guid.NewGuid().GetHashCode());
                        var tempFileName2 =
                            Path.Combine(Path.GetTempPath(),
                                         $"{Path.GetFileName(barFileInfo.FileName)}-{rnd.Next()}.tmp");
                        XmbFileUtils.XmbToXml(tempFileName, tempFileName2);

                        if (File.Exists(tempFileName))
                        {
                            File.Delete(tempFileName);
                        }

                        tempFileName = tempFileName2;

                        filePath = filePath.Substring(0, filePath.Length - 4);
                    }
                    catch (Exception)
                    {
                        //
                    }
                }
                else if (barFileInfo.FileName.EndsWith(".age4scn",
                                                       StringComparison.OrdinalIgnoreCase) &&
                         !L33TZipUtils.IsL33TZipFile(tempFileName))
                {
                    var rnd           = new Random(Guid.NewGuid().GetHashCode());
                    var tempFileName2 =
                        Path.Combine(Path.GetTempPath(),
                                     $"{Path.GetFileName(barFileInfo.FileName)}-{rnd.Next()}.tmp");
                    await L33TZipUtils.CompressFileAsL33TZipAsync(tempFileName, tempFileName2);

                    if (File.Exists(tempFileName))
                    {
                        File.Delete(tempFileName);
                    }

                    tempFileName = tempFileName2;
                }
            }

            //Move new file
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }

            File.Move(tempFileName, filePath);

            File.SetCreationTimeUtc(filePath,
                                    new DateTime(barFileInfo.LastWriteTime.Year, barFileInfo.LastWriteTime.Month,
                                                 barFileInfo.LastWriteTime.Day, barFileInfo.LastWriteTime.Hour,
                                                 barFileInfo.LastWriteTime.Minute, barFileInfo.LastWriteTime.Second));

            File.SetLastWriteTimeUtc(filePath,
                                     new DateTime(barFileInfo.LastWriteTime.Year, barFileInfo.LastWriteTime.Month,
                                                  barFileInfo.LastWriteTime.Day, barFileInfo.LastWriteTime.Hour,
                                                  barFileInfo.LastWriteTime.Minute, barFileInfo.LastWriteTime.Second));
        }