Esempio n. 1
0
        private static int Main2(string[] argv)
        {
            var watch         = Stopwatch.StartNew();
            var runParameters = new RunParameters(argv);

            if (!runParameters.Valid)
            {
                Print(ErrorColor, runParameters.ErrorMessage);
                PrintHelp(argv[0]);
                return(-1);
            }
            Print(WarningColor, runParameters.Description);
            var errors = Run(runParameters);

            watch.Stop();
            if (runParameters.IsFileMode)
            {
                return(errors);
            }
            var completedString = $"Completed in {watch.Elapsed:hh\\:mm\\:ss\\.ffff}";

            if (errors > 0)
            {
                Print(ErrorColor, $"{completedString} ({errors} errors)");
            }
            else
            {
                Print(SuccessColor, completedString);
            }
            return(errors);
        }
Esempio n. 2
0
        private static int Run(RunParameters runParameters)
        {
            var    files        = runParameters.GetFiles();
            int    errors       = 0;
            string formatString = new string(Enumerable.Repeat('0', files.Length.ToString().Length).ToArray());

            if (runParameters.Parallel)
            {
                ParallelErrors = 0;
                files.AsParallel().ForAll(data => ProcessFileParallel(data, runParameters.LogErrorsOnly));
                errors = ParallelErrors;
            }
            else
            {
                foreach (var file in files)
                {
                    //if it returns false, that means user stopped it.
                    if (!ProcessFile(runParameters, files.Length, formatString, file, ref errors))
                    {
                        return(errors);
                    }
                }
            }
            return(errors);
        }
Esempio n. 3
0
        private static bool ProcessFile(RunParameters runParameters, int fileCount, string formatString,
                                        ConvertFileData file, ref int errors)
        {
            if (fileCount > 1)
            {
                if (Console.KeyAvailable)
                {
                    //read key available to skip
                    Console.ReadKey(true);
                    Print(WarningColor, "Key pressed, press Escape to stop or any other key to continue...");
                    var key = Console.ReadKey(true);
                    if (key.Key == ConsoleKey.Escape)
                    {
                        return(false);
                    }
                }
                if (runParameters.LogErrorsOnly)
                {
                    Print(WarningColor, $"Processing file {file.Index.ToString(formatString)}/{fileCount}", true);
                }
                else
                {
                    Print(WarningColor, file.GetDescription(fileCount, formatString));
                }
            }

            try
            {
                var result = file.Mode switch
                {
                    ProcessMode.Unpack => Unpack(file),
                    ProcessMode.Pack => Pack(file),
                    ProcessMode.UnpackAndPack => UnpackAndPack(file),
                    _ => throw new ArgumentOutOfRangeException()
                };
                if (!result)
                {
                    errors++;
                }
            }
            catch (Exception ex)
            {
                errors++;
                Print(ErrorColor, $"\t{file.GetDescription(fileCount, formatString)} - Failed: {ex}");
            }

            return(true);
        }