ME3Explorer.Texplorer2.RegenerateThumbs C# (CSharp) Method

RegenerateThumbs() private method

private RegenerateThumbs ( List temptexes ) : bool
temptexes List
return bool
        private bool RegenerateThumbs(List<TreeTexInfo> temptexes)
        {
            this.Invoke(new Action(() =>
            {
                foreach (Bitmap img in ListViewImageList.Images)
                    img.Dispose();

                ListViewImageList.Images.Clear();
            }));
            System.Threading.Thread.Sleep(200);

            if (temptexes.Count > 1000)  // KFreon: Entire thing - no folder has > 1000 texes
            {
                // KFreon: Delete old thumbnails
                if (Directory.Exists(ThumbnailPath))
                {
                    for (int i = 0; i < 5; i++)
                    {
                        try
                        {
                            Directory.Delete(ThumbnailPath, true);
                            System.Threading.Thread.Sleep(100);
                            DebugOutput.PrintLn("Successfully deleted old thumbnails.");
                            break;
                        }
                        catch
                        {
                            DebugOutput.PrintLn("Failed to delete old thumbnails. Sleeping for a bit before trying again.");
                            System.Threading.Thread.Sleep(500);
                        }
                    }
                }
            }
            else
            {
                foreach (var tex in temptexes)
                {
                    for (int i = 0; i < 10; i++)
                    {
                        try
                        {
                            File.Delete(tex.ThumbnailPath);
                        }
                        catch (FileNotFoundException e)
                        {
                            DebugOutput.PrintLn($"Thumbnail: {tex.ThumbnailPath} not found, thus not deleted.");
                            System.Threading.Thread.Sleep(100);
                        }
                        catch (Exception e)
                        {
                            DebugOutput.PrintLn($"Error occured deleting thumbnail: {tex.ThumbnailPath}. {e.Message}");
                            System.Threading.Thread.Sleep(100);
                        }
                    }
                    
                }
            }
            

            // KFreon: Create directory
            Directory.CreateDirectory(ThumbnailPath);

            // KFreon: Generate thumbnails for all textures in file
            int count = 0;
            foreach (var tex in temptexes)
            {
                // KFreon: Generate thumbnails for each texture
                for (int j = 0; j < 3; j++)
                {
                    try
                    {
                        using (Textures.ITexture2D tex2D = KFreonLib.Textures.Creation.CreateTexture2D(tex.Textures[0], WhichGame, pathBIOGame))
                        {
                            string destination = tex.ThumbnailPath ?? Path.Combine(ThumbnailPath, tex.ThumbName);
                            using (MemoryStream ms = new MemoryStream(tex2D.GetImageData()))
                            {
                                var treetex = tex2D.imgList.Where(t => t.offset != -1).First();
                                int max = (int)(treetex.imgSize.height > treetex.imgSize.width ? treetex.imgSize.height : treetex.imgSize.width);
                                double divisor = max > 128 ? max / 128.0 : 1;

                                int newWidth = (int)(treetex.imgSize.width / divisor);
                                int newHeight = (int)(treetex.imgSize.height / divisor);

                                string result = Textures.Creation.GenerateThumbnail(ms, destination, newWidth, newHeight);
                                if (result != null)
                                    tex.ThumbnailPath = destination;
                                else
                                    tex.ThumbnailPath = Path.Combine(ExecFolder, "placeholder.ico");
                            }


                            DebugOutput.PrintLn("Generated thumbnail at: " + tex.ThumbnailPath);
                            ProgBarUpdater.IncrementBar();

                            // KFreon: Update status
                                count++;
                                if (count % 10 == 0)
                                    StatusUpdater.UpdateText("Regenerating thumbnails...  " + count + " of " + temptexes.Count);
                            break;
                        }
                    }
                    catch
                    {
                        DebugOutput.PrintLn("Failed to generate thumbnail from: " + tex.TexName + ". Sleeping before trying again.");
                        System.Threading.Thread.Sleep(100);
                    }
                }
                

            }
            ProgBarUpdater.ChangeProgressBar(1, 1);
            StatusUpdater.UpdateText("Thumbnails Regenerated.");
            OutputBoxPrintLn("Thumbnails Regenerated");
            if (!Tree.AdvancedFeatures && MessageBox.Show("Your current tree doesn't have advanced features enabled. Do you want to save these features to your current tree?", "You probably want to do this", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.Yes)
            {
                for (int i = 0; i < 3; i++)
                    try
                    {
                        Tree.WriteToFile(Tree.TreePath, Path.GetDirectoryName(pathBIOGame));
                        DebugOutput.PrintLn("Tree saved with advanced features");
                        break;
                    }
                    catch
                    {
                        DebugOutput.PrintLn("Tree in use. Sleeping for a bit.");
                        System.Threading.Thread.Sleep(200);
                    }
            }

            UpdateThumbnailDisplays(MainTreeView.SelectedNode as myTreeNode);
            return true;
        }
Texplorer2