private static bool CancelConversion()
        {
            if (m_CurrentTask != null)
            {
                if (m_ConversionHelper.m_AssetEditingStarted)
                {
                    AssetDatabase.StopAssetEditing();
                    AssetDatabase.Refresh();
                }

                m_CurrentTask.Cancel("Canceled");
                m_CurrentTask = null;
            }
            m_ConversionHelper  = null;
            m_CurrentListDialog = null;

            return(true);
        }
        private static void OnConfirmUpdateProjectFiles()
        {
            if (m_ConversionHelper == null)
            {
                // Something wrong happened midway but we can't recover, so let users restart the process if they need to.
                return;
            }

            m_CurrentListDialog = null;

            AssetDatabase.StartAssetEditing();
            m_ConversionHelper.m_AssetEditingStarted = true;
            m_ConversionHelper.m_CurrentIndex        = 0;

            m_CurrentTask =
                new GUIDConversionTask(kAssetGUIDConverterName, "Saving modified assets", SaveModifiedAssets, CancelConversion);
            m_CurrentTask.Start();
        }
        private static void SaveModifiedAssets()
        {
            // The index value should always be good but making sure we don't crash and burn in case something is wrong.
            if (m_ConversionHelper.m_CurrentIndex >= m_ConversionHelper.m_ProjectAssetsToConvert.Count)
            {
                if (m_ConversionHelper.m_ShowUIDialogs)
                {
                    EditorUtility.DisplayDialog(kAssetGUIDConverterName,
                                                "Error saving assets.", "OK", null);
                }
                m_CurrentTask.Stop("Error saving assets", true);
                m_CurrentTask      = null;
                m_ConversionHelper = null;
                return;
            }

            var currentAsset = m_ConversionHelper.m_ProjectAssetsToConvert[m_ConversionHelper.m_CurrentIndex++];

            try
            {
                File.WriteAllText(Path.Combine(m_ConversionHelper.m_ProjectPath, currentAsset.m_AssetFilePath), currentAsset.m_AssetDataFile);
            }
            catch (Exception e)
            {
                Debug.LogError($"{ kAssetGUIDConverterName } - Unable to write file { currentAsset.m_AssetFilePath }: " + e);

                m_ConversionHelper.m_AssetNamesToShowInUI.Remove(currentAsset.m_AssetNameForUI);
                m_ConversionHelper.m_ErrorsFound = true;
            }

            if (m_ConversionHelper.m_CurrentIndex == m_ConversionHelper.m_ProjectAssetsToConvert.Count)
            {
                m_CurrentTask.Stop("Assets saved!");

                m_ConversionHelper.m_AssetEditingStarted = false;
                AssetDatabase.StopAssetEditing();
                AssetDatabase.Refresh();

                if (m_ConversionHelper.m_ShowUIDialogs)
                {
                    string conversionDoneMessage = m_ConversionHelper.m_Inverted ?
                                                   "The following assets were reverted and will only function with the UI Toolkit package installed:" :
                                                   "The following assets were converted and will function without the UI Toolkit package installed:";

                    if (m_ConversionHelper.m_ErrorsFound)
                    {
                        conversionDoneMessage =
                            "Assets saved but some errors were found. Check the Console for details.\n\n" +
                            conversionDoneMessage;
                    }

                    GUIDConverterListDialog.OpenListDialog(kAssetGUIDConverterName, conversionDoneMessage,
                                                           m_ConversionHelper.m_AssetNamesToShowInUI, "OK");
                }
                m_CurrentTask      = null;
                m_ConversionHelper = null;
            }
            else
            {
                m_CurrentTask.SetProgress((float)m_ConversionHelper.m_CurrentIndex / m_ConversionHelper.m_ProjectAssetsToConvert.Count);
            }
        }
        private static void ScanProjectFiles()
        {
            // The index value should always be good but making sure we don't crash and burn in case something is wrong.
            if (m_ConversionHelper.m_CurrentIndex >= m_ConversionHelper.m_ProjectAssetsGUIDs.Count)
            {
                if (m_ConversionHelper.m_ShowUIDialogs)
                {
                    EditorUtility.DisplayDialog(kAssetGUIDConverterName,
                                                "Error scanning project files.", "OK", null);
                }
                m_CurrentTask.Stop("Error scanning project files", true);
                m_CurrentTask      = null;
                m_ConversionHelper = null;
                return;
            }

            var currGUID = m_ConversionHelper.m_ProjectAssetsGUIDs[m_ConversionHelper.m_CurrentIndex++];

            string assetFilePath = AssetDatabase.GUIDToAssetPath(currGUID);

            // Filter out file types we have no interest in searching
            if (!ShouldIgnoreFile(assetFilePath))
            {
                string assetMetaFilePath = AssetDatabase.GetTextMetaFilePathFromAssetPath(assetFilePath);

                // Read the asset data file
                string assetDataFile = string.Empty;

                try
                {
                    assetDataFile = File.ReadAllText(Path.Combine(m_ConversionHelper.m_ProjectPath, assetFilePath));
                }
                catch
                {
                    // Continue to the next asset if we can't read the current one.
                    Debug.LogError($"{kAssetGUIDConverterName} - Unable to open file {assetFilePath}; file skipped.");
                    return;
                }

                // Read the asset meta data file
                string assetMetaFile = File.ReadAllText(Path.Combine(m_ConversionHelper.m_ProjectPath, assetMetaFilePath));

                bool hasFileChanged     = false;
                bool hasMetaFileChanged = false;

                foreach (AssetConversionRecord record in m_ConversionHelper.m_ConversionData)
                {
                    if (assetDataFile.Contains(record.target))
                    {
                        hasFileChanged = true;
                        assetDataFile  = assetDataFile.Replace(record.target, record.replacement);
                    }

                    // Check meta file
                    if (assetMetaFile.Contains(record.target))
                    {
                        hasMetaFileChanged = true;
                        assetMetaFile      = assetMetaFile.Replace(record.target, record.replacement);
                    }
                }

                if (hasFileChanged)
                {
                    m_ConversionHelper.m_ProjectAssetsToConvert.Add(
                        new GUIDConversionHelper.AssetInfo()
                    {
                        m_AssetFilePath = assetFilePath, m_AssetDataFile = assetDataFile, m_AssetNameForUI = assetFilePath
                    });
                    m_ConversionHelper.m_AssetNamesToShowInUI.Add(assetFilePath);
                }

                if (hasMetaFileChanged)
                {
                    m_ConversionHelper.m_ProjectAssetsToConvert.Add(
                        new GUIDConversionHelper.AssetInfo()
                    {
                        m_AssetFilePath = assetMetaFilePath, m_AssetDataFile = assetMetaFile, m_AssetNameForUI = assetFilePath
                    });

                    if (!m_ConversionHelper.m_AssetNamesToShowInUI.Contains(assetFilePath))
                    {
                        m_ConversionHelper.m_AssetNamesToShowInUI.Add(assetFilePath);
                    }
                }
            }

            if (m_ConversionHelper.m_CurrentIndex == m_ConversionHelper.m_ProjectAssetsGUIDs.Count)
            {
                m_CurrentTask.Stop("Project asset scan done!");
                m_CurrentTask = null;

                if (m_ConversionHelper.m_ProjectAssetsToConvert.Count > 0)
                {
                    if (m_ConversionHelper.m_ShowUIDialogs)
                    {
                        string confirmButtonText, message;
                        if (m_ConversionHelper.m_Inverted)
                        {
                            confirmButtonText = "Revert";
                            message           = "The following assets were identified for reversion. Revert assets?";
                        }
                        else
                        {
                            confirmButtonText = "Convert";
                            message           = "The following assets were identified for conversion. Convert assets?";
                        }
                        m_CurrentListDialog = GUIDConverterListDialog.OpenListDialog(kAssetGUIDConverterName,
                                                                                     message, m_ConversionHelper.m_AssetNamesToShowInUI, confirmButtonText, true, OnConfirmUpdateProjectFiles,
                                                                                     () => CancelConversion());
                    }
                    else
                    {
                        OnConfirmUpdateProjectFiles();
                    }
                }
                else
                {
                    if (m_ConversionHelper.m_ShowUIDialogs)
                    {
                        EditorUtility.DisplayDialog(kAssetGUIDConverterName,
                                                    "No UI Toolkit package asset found to convert.", "OK", null);
                    }

                    m_CurrentTask      = null;
                    m_ConversionHelper = null;
                }
            }
            else
            {
                m_CurrentTask.SetProgress((float)m_ConversionHelper.m_CurrentIndex / m_ConversionHelper.m_ProjectAssetsGUIDs.Count);
            }
        }
        internal static bool StartConversion(List <AssetConversionRecord> conversionData, List <string> pathsToScan, bool invert = false, bool showUIDialogs = true)
        {
            if (pathsToScan == null || pathsToScan.Count == 0)
            {
                if (showUIDialogs)
                {
                    EditorUtility.DisplayDialog(kAssetGUIDConverterName,
                                                "Error: No paths to scan, aborting process.", "OK", null);
                }
                return(false);
            }

            if (conversionData == null || conversionData.Count == 0)
            {
                if (showUIDialogs)
                {
                    EditorUtility.DisplayDialog(kAssetGUIDConverterName,
                                                "Error: conversion data not found, aborting process.", "OK", null);
                }
                return(false);
            }

            if (!CheckConversionPossible(showUIDialogs))
            {
                return(false);
            }

            if (m_ConversionHelper != null)
            {
                if (showUIDialogs)
                {
                    // A conversion is already in process, we need to cancel it if we want to run a new one.
                    if (!EditorUtility.DisplayDialog(kAssetGUIDConverterName,
                                                     "A conversion is already in progress, do you want to interrupt that and start a new one?",
                                                     "Yes", "No"))
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }

                if (m_CurrentTask != null)
                {
                    m_CurrentTask.Stop("Interrupted by new conversion request");
                    m_CurrentTask = null;
                }

                if (m_CurrentListDialog != null)
                {
                    m_CurrentListDialog.CloseWindow();
                    m_CurrentListDialog = null;
                }

                m_ConversionHelper = null;
            }

            m_ConversionHelper = new GUIDConversionHelper
            {
                m_ConversionData = new List <AssetConversionRecord>(conversionData)
            };

            if (invert)
            {
                InvertConversion(m_ConversionHelper.m_ConversionData);
            }

            m_ConversionHelper.m_ProjectPath = Path.GetFullPath(kAssetsFolderName + "/..");
            string[] searchInFolders = pathsToScan.ToArray();

            // Get list of GUIDs for assets that might contain references to previous GUIDs that require updating.
            m_ConversionHelper.m_ProjectAssetsGUIDs = new List <string>(AssetDatabase.FindAssets("t:Object", searchInFolders).Distinct());

            if (m_ConversionHelper.m_ProjectAssetsGUIDs.Count == 0)
            {
                if (showUIDialogs)
                {
                    if (m_ConversionHelper.m_Inverted)
                    {
                        EditorUtility.DisplayDialog(kAssetGUIDConverterName,
                                                    "No UI Toolkit package asset found to revert.", "OK", null);
                    }
                    else
                    {
                        EditorUtility.DisplayDialog(kAssetGUIDConverterName,
                                                    "No UI Toolkit package asset found to convert.", "OK", null);
                    }
                }

                m_ConversionHelper = null;

                return(false);
            }

            m_ConversionHelper.m_ShowUIDialogs          = showUIDialogs;
            m_ConversionHelper.m_Inverted               = invert;
            m_ConversionHelper.m_CurrentIndex           = 0;
            m_ConversionHelper.m_ProjectAssetsToConvert = new List <GUIDConversionHelper.AssetInfo>();
            m_ConversionHelper.m_AssetNamesToShowInUI   = new List <string>();

            m_CurrentTask =
                new GUIDConversionTask(kAssetGUIDConverterName,
                                       "Scanning project assets", ScanProjectFiles, CancelConversion);
            m_CurrentTask.Start();

            return(true);
        }