ME3Explorer.Texplorer2.ImportTree C# (CSharp) Method

ImportTree() private method

private ImportTree ( ) : void
return void
        private async void ImportTree()
        {
            if (Tree == null)
            {
                MessageBox.Show("Game files not found. Unfortunately we seem to need them to use trees...");
                return;
            }
            else if (Tree.TexCount != 0)
                if (MessageBox.Show("This will import the tree and thumbnails (if present) from the specified location. Be sure the game currently selected in Texplorer matches the tree you're importing. Do you want to continue?", "Sure you wanna do that Commander?", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
                    return;
            
            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                ofd.Filter = "ME Trees|*.bin";
                ofd.Title = "Select tree to import.";

                if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    StatusUpdater.UpdateText("Importing tree...");
                    ProgBarUpdater.ChangeProgressBar(0, 1);

                    TreeDB temptree = Tree.Clone();  // KFreon: Copy settings so can back track if tree load fails

                    string destTreePath = ExecFolder + "me" + WhichGame + "tree.bin";
                    File.Copy(ofd.FileName, destTreePath);

                    int status;
                    if (!LoadTreeFromFile(destTreePath, out status, false))  // KFreon: Load actual tree.bin
                    {
                        Tree = temptree;
                        MessageBox.Show("Error occured while loading tree. Likely a corrupted or invalid tree.");
                        return;
                    }

                    StatusUpdater.UpdateText("Importing thumbs...");

                    await Task.Run(() =>
                    {
                        // KFreon: Copy thumbnails
                        string basePath = Path.GetDirectoryName(ofd.FileName);
                        string mainCachePath = Path.Combine(basePath, $"ThumbnailCaches\\ME{WhichGame}ThumbnailCache");

                        bool inMainCachePath = Directory.Exists(mainCachePath);
                        string sourceThumbCachePath = mainCachePath;

                        if (!inMainCachePath)
                        {
                            string nakedPath = Path.Combine(basePath, $"ME{WhichGame}ThumbnailCache");
                            bool isNakedPath = Directory.Exists(nakedPath);  // KFreon: Huehuehue

                            if (isNakedPath)
                                sourceThumbCachePath = nakedPath;
                            else
                            {
                                DebugOutput.PrintLn("Thumbnails not found. Skipping...");
                                return;
                            }
                        }

                        string destThumbCachePath = ThumbnailPath;
                        Directory.CreateDirectory(destThumbCachePath);

                        var files = Directory.GetFiles(sourceThumbCachePath);
                        ProgBarUpdater.ChangeProgressBar(0, files.Length);

                        int count = 0;
                        foreach (var file in files)
                        {
                            string filename = Path.GetFileName(file);
                            string destPath = Path.Combine(destThumbCachePath, filename);  // KFreon: No overwriting. 
                            if (!File.Exists(destPath))
                                File.Copy(file, destPath);

                            if (count++ % 10 == 0)
                                ProgBarUpdater.IncrementBar();
                        }
                    });

                    ChangeTreeIndicators(MEExDirecs.WhichGame, true);
                    ProgBarUpdater.ChangeProgressBar(1, 1);
                    StatusUpdater.UpdateText("Tree imported!");
                }
            }
        }
Texplorer2