예제 #1
0
        public static void ResortPngs(string resortDir,
                                      Hgx2pngCallback progressCallback, Hgx2pngErrorCallback errorCallback)
        {
            DateTime startTime = DateTime.UtcNow;

            ImageIdentifier identifier = new ImageIdentifier();
            Hgx2pngArgs     args       = new Hgx2pngArgs();

            string message = "Locating Pngs...";

            Console.WriteLine(message);
            args.FileCount = PathHelper.EnumerateAllFiles(resortDir, "*.png").Count();
            Console.WriteLine($"\r{new string(' ', message.Length)}");

            DateTime  lastRefresh = DateTime.MinValue;
            Stopwatch writeTime   = new Stopwatch();
            TimeSpan  refreshTime = TimeSpan.FromMilliseconds(20);
            int       i           = 0;

            foreach (string file in PathHelper.EnumerateAllFiles(resortDir, "*.png"))
            {
                //string file = files[i];
                ImageIdentification image = identifier.PreIdentifyImage(file);
                string path = Path.Combine(resortDir, image.OutputDirectory, Path.GetFileName(file));
                string name = Path.GetFileName(file);

                args.FileIndex = i++;
                args.FileName  = name;
                args.FilePath  = file;
                args.Ellapsed  = DateTime.UtcNow - startTime;
                // Round to nearest hundredth
                args.Percent = Math.Round((double)i / args.FileCount * 10000) / 100;
                // If true, cancel operation
                TimeSpan sinceRefresh = DateTime.UtcNow - lastRefresh;
                if (sinceRefresh >= refreshTime)
                {
                    lastRefresh = DateTime.UtcNow;
                    writeTime.Start();
                    if (progressCallback?.Invoke(args) ?? false)
                    {
                        return;
                    }
                    writeTime.Stop();
                }

                if (PathHelper.IsPathTheSame(path, file))
                {
                    continue;
                }

                string sortedDir = Path.Combine(resortDir, image.OutputDirectory);
                Directory.CreateDirectory(sortedDir);
                try {
                    BmpToPng(file, Hg3Sorting.Sorted, "", sortedDir);
                }
                catch (ExtractHg3Exception ex) {
                    args.TotalErrors++;
                    // If true, cancel operation
                    if (errorCallback?.Invoke(ex, args) ?? false)
                    {
                        return;
                    }
                }
            }
            args.Ellapsed = DateTime.UtcNow - startTime;
            args.Percent  = 100.0;
            progressCallback?.Invoke(args);
            Trace.WriteLine($"Console Write Time: {writeTime.Elapsed:mm\\:ss\\.fff}");
            PathHelper.DeleteAllEmptyDirectories(resortDir);
        }
예제 #2
0
        /// <summary>Extracts all .hg3 files that meet the pattern into .png's.</summary>
        public static ImageIdentifier ExtractHg3s(string inputDir, string outputDir,
                                                  Hg3Sorting sorting, string pattern,
                                                  Hgx2pngCallback progressCallback, Hgx2pngErrorCallback errorCallback,
                                                  string continueFile = null, string[] remainingFiles = null)
        {
            ImageIdentifier identifier  = new ImageIdentifier();
            DateTime        startTime   = DateTime.UtcNow;
            string          unsortedDir = outputDir;

            if (sorting.HasFlag(Hg3Sorting.Both))
            {
                unsortedDir = Path.Combine(unsortedDir, "Unsorted");
            }
            Directory.CreateDirectory(unsortedDir);

            if (string.IsNullOrWhiteSpace(pattern))
            {
                pattern = "*.hg3";
            }
            else if (!pattern.EndsWith(".hg3", StringComparison.OrdinalIgnoreCase) &&
                     !pattern.EndsWith(".hg3*", StringComparison.OrdinalIgnoreCase))
            {
                pattern += ".hg3";
            }
            string[]    files     = Directory.GetFiles(inputDir, pattern);
            int         fileCount = files.Length;
            Hgx2pngArgs args      = new Hgx2pngArgs()
            {
                FileCount = fileCount,
            };

            // Skip files if we're continuing from a later point
            int start = 0;

            if (continueFile != null)
            {
                for (start = 0; start < fileCount; start++)
                {
                    if (string.Compare(files[start], continueFile, true) >= 0)
                    {
                        break;
                    }
                }
            }

            // Remove non-remaining files.
            // This will be used for retrying files that had errors.
            if (remainingFiles != null)
            {
                List <string> fileList    = new List <string>(files);
                int           remainIndex = 0;
                int           remainCount = remainingFiles.Length;
                for (int i = 0; i < fileList.Count && remainIndex < remainCount &&
                     (continueFile == null || i < start); i++)
                {
                    if (PathHelper.IsPathTheSame(
                            remainingFiles[remainIndex], fileList[i]))
                    {
                        fileList.RemoveAt(i);
                        if (continueFile != null)
                        {
                            start--;
                        }
                        i--;
                        remainIndex++;
                    }
                }
                files = fileList.ToArray();
            }

            DateTime  lastRefresh = DateTime.MinValue;
            Stopwatch writeTime   = new Stopwatch();
            TimeSpan  refreshTime = TimeSpan.FromMilliseconds(20);

            //Stopwatch processTime = new Stopwatch();
            for (int i = start; i < fileCount; i++)
            {
                string file = files[i];
                string name = Path.GetFileNameWithoutExtension(file);

                args.FileIndex = i;
                args.FileName  = name;
                args.FilePath  = file;
                args.Ellapsed  = DateTime.UtcNow - startTime;
                // Round to nearest hundredth
                args.Percent = Math.Round((double)i / fileCount * 10000) / 100;
                // If true, cancel operation
                TimeSpan sinceRefresh = DateTime.UtcNow - lastRefresh;
                if (sinceRefresh >= refreshTime)
                {
                    lastRefresh = DateTime.UtcNow;
                    writeTime.Start();
                    if (progressCallback?.Invoke(args) ?? false)
                    {
                        return(identifier);
                    }
                    writeTime.Stop();
                }

                //processTime.Restart();
                List <Exception> exceptions = new List <Exception>();
                string[]         bmps;
                try {
                    //bmps = Hg3Convert.UnpackHg3(file, null, true);
                    ImageIdentification image = identifier.PreIdentifyImage(file);
                    bmps  = Extracting.Hg3ToBmp(file, image.ExpandImage);
                    image = identifier.IdentifyImage(bmps);
                    string sortedDir = Path.Combine(outputDir, image.OutputDirectory);
                    if (sorting.HasFlag(Hg3Sorting.Sorted))
                    {
                        Directory.CreateDirectory(sortedDir);
                    }
                    for (int j = 0; j < bmps.Length; j++)
                    {
                        try {
                            BmpToPng(bmps[j], sorting, unsortedDir, sortedDir);
                        }
                        catch (ExtractHg3Exception ex) {
                            exceptions.Add(ex);
                        }
                    }
                    //processTime.Stop();
                    //if (processTime.ElapsedMilliseconds >= 500)
                    //	Trace.WriteLine($"Large File: {new FileInfo(file).Length / 1024:###,###,###,###}KB [{processTime.ElapsedMilliseconds}ms]");
                }
                catch (ExtractHg3Exception ex) {
                    exceptions.Add(ex);
                }

                foreach (Exception ex in exceptions)
                {
                    args.TotalErrors++;
                    // If true, cancel operation
                    if (errorCallback?.Invoke(ex, args) ?? false)
                    {
                        return(identifier);
                    }
                }
            }
            args.Ellapsed = DateTime.UtcNow - startTime;
            args.Percent  = 100.0;
            progressCallback?.Invoke(args);
            Trace.WriteLine($"Console Write Time: {writeTime.Elapsed:mm\\:ss\\.fff}");
            return(identifier);
        }