static void QueueBuild()
 {
     if (PlannerAssetDatabase.HasValidProblemDefinition())
     {
         if (EditorApplication.delayCall == null || !EditorApplication.delayCall.GetInvocationList().Any(
                 d =>
         {
             var methodInfo = d.Method;
             return(methodInfo.Name == nameof(BuildWhenReady) && methodInfo.DeclaringType == typeof(PlannerAssemblyBuilder));
         }))
         {
             EditorApplication.delayCall += BuildWhenReady;
         }
     }
 }
        static void OnPlayModeStateChanged(PlayModeStateChange playMode)
        {
            if (AIPlannerPreferences.GetOrCreatePreferences().AutoCompile)
            {
                if (playMode == PlayModeStateChange.EnteredPlayMode && EditorApplication.isCompiling)
                {
                    // If we're still compiling the domain will reload and cause an error, so as a safeguard simply exit play mode
                    EditorApplication.ExitPlaymode();
                    return;
                }

                if (playMode == PlayModeStateChange.ExitingEditMode && !EditorApplication.isCompiling)
                {
                    if (!PlannerAssetDatabase.HasValidProblemDefinition())
                    {
                        return;
                    }

                    var assetChangedPath = string.Empty;

                    bool stateRepresentationNeedBuild = !PlannerAssetDatabase.StateRepresentationPackageExists();
                    if (!stateRepresentationNeedBuild)
                    {
                        DateTime lastStateRepresentationBuildTime = GetAssemblyBuildTime(TypeHelper.StateRepresentationQualifier);

                        stateRepresentationNeedBuild = PlannerAssetDatabase.TryFindNewerAsset(PlannerAssetDatabase.stateRepresentationAssetTypeNames, lastStateRepresentationBuildTime, ref assetChangedPath);
                        if (stateRepresentationNeedBuild)
                        {
                            Debug.Log($"Rebuilding AI Planner State Representation assembly because {assetChangedPath} is newer");
                        }
                    }
                    else
                    {
                        Debug.Log($"Rebuilding AI Planner assemblies because AI Planner State Representation package cannot be found");
                    }

                    bool planNeedBuild = !PlannerAssetDatabase.PlansPackageExists();
                    if (!planNeedBuild)
                    {
                        DateTime lastPlanBuildTime = GetAssemblyBuildTime(TypeHelper.PlansQualifier);
                        planNeedBuild = PlannerAssetDatabase.TryFindNewerAsset(PlannerAssetDatabase.planAssetTypeNames, lastPlanBuildTime, ref assetChangedPath);
                        if (planNeedBuild)
                        {
                            Debug.Log($"Rebuilding AI Plan assembly because {assetChangedPath} is newer");
                        }
                    }
                    else
                    {
                        Debug.Log($"Rebuilding AI Planner assemblies because AI Plan package cannot be found");
                    }

                    if (stateRepresentationNeedBuild || planNeedBuild)
                    {
                        EditorApplication.ExitPlaymode();

                        try
                        {
                            if (Build(stateRepresentationNeedBuild))
                            {
                                CompilationPipeline.compilationFinished += context => EditorApplication.EnterPlaymode();
                            }
                        }
                        finally
                        {
                            EditorUtility.ClearProgressBar();
                        }
                    }
                }
            }
        }