// When all files in stack have been compiled. This is called via update because Process events run in another thread.
        private static void DelayedComplete()
        {
            if (InkLibrary.NumFilesInCompilingStackInState(CompilationStackItem.State.Compiling) > 0)
            {
                Debug.LogWarning("Delayed, but a file is now compiling! You can ignore this warning.");
                return;
            }
            bool          errorsFound      = false;
            StringBuilder filesCompiledLog = new StringBuilder("Files compiled:");

            // Create and import compiled files
            AssetDatabase.StartAssetEditing();
            foreach (var compilingFile in InkLibrary.Instance.compilationStack)
            {
                // Complete status is also set when an error occured, in these cases 'compiledJson' will be null so there's no import to process
                if (compilingFile.compiledJson == null)
                {
                    continue;
                }

                // Write new compiled data to the file system
                File.WriteAllText(compilingFile.jsonAbsoluteFilePath, compilingFile.compiledJson, Encoding.UTF8);
                AssetDatabase.ImportAsset(compilingFile.inkFile.jsonPath);
            }
            AssetDatabase.StopAssetEditing();

            foreach (var compilingFile in InkLibrary.Instance.compilationStack)
            {
                // Load and store a reference to the compiled file
                compilingFile.inkFile.FindCompiledJSONAsset();

                filesCompiledLog.AppendLine().Append(compilingFile.inkFile.filePath);
                filesCompiledLog.Append(string.Format(" ({0}s)", compilingFile.timeTaken));
                if (compilingFile.unhandledErrorOutput.Count > 0)
                {
                    filesCompiledLog.Append(" (With unhandled error)");
                    StringBuilder errorLog = new StringBuilder();
                    errorLog.Append("Unhandled error(s) occurred compiling Ink file ");
                    errorLog.Append("'");
                    errorLog.Append(compilingFile.inkFile.filePath);
                    errorLog.Append("'");
                    errorLog.AppendLine("! Please report following error(s) as a bug:");
                    foreach (var error in compilingFile.unhandledErrorOutput)
                    {
                        errorLog.AppendLine(error);
                    }
                    Debug.LogError(errorLog);
                    compilingFile.inkFile.unhandledCompileErrors = compilingFile.unhandledErrorOutput;
                    errorsFound = true;
                }
                else
                {
                    SetOutputLog(compilingFile);
                    bool errorsInEntireStory   = false;
                    bool warningsInEntireStory = false;
                    foreach (var inkFile in compilingFile.inkFile.inkFilesInIncludeHierarchy)
                    {
                        if (inkFile.hasErrors)
                        {
                            errorsInEntireStory = true;
                        }
                        if (inkFile.hasWarnings)
                        {
                            warningsInEntireStory = true;
                        }
                    }
                    if (errorsInEntireStory)
                    {
                        filesCompiledLog.Append(" (With error)");
                        errorsFound = true;
                    }
                    if (warningsInEntireStory)
                    {
                        filesCompiledLog.Append(" (With warning)");
                    }
                }
            }


            foreach (var compilingFile in InkLibrary.Instance.compilationStack)
            {
                if (OnCompileInk != null)
                {
                    OnCompileInk(compilingFile.inkFile);
                }
            }

            StringBuilder outputLog = new StringBuilder();

            if (errorsFound)
            {
                outputLog.Append("Ink compilation completed with errors at ");
                outputLog.AppendLine(DateTime.Now.ToLongTimeString());
                outputLog.Append(filesCompiledLog.ToString());
                Debug.LogError(outputLog);
            }
            else
            {
                outputLog.Append("Ink compilation completed at ");
                outputLog.AppendLine(DateTime.Now.ToLongTimeString());
                outputLog.Append(filesCompiledLog.ToString());
                Debug.Log(outputLog);
            }

            InkLibrary.ClearCompilationStack();

                        #if !UNITY_EDITOR_LINUX
            EditorUtility.ClearProgressBar();
                        #endif

                        #if UNITY_2019_4_OR_NEWER
            if (InkEditorUtils.disallowedAutoRefresh)
            {
                InkEditorUtils.disallowedAutoRefresh = false;
                try {
                    AssetDatabase.AllowAutoRefresh();
                } catch (Exception e) {
                    Debug.LogWarning("Failed AllowAutoRefresh " + e);
                }
            }
                        #endif

            // This is now allowed, if compiled manually. I've left this code commented out because at some point we might want to track what caused a file to compile.
            // if(EditorApplication.isPlayingOrWillChangePlaymode && InkSettings.Instance.delayInPlayMode) {
            //  Debug.LogError("Ink just finished recompiling while in play mode. This should never happen when InkSettings.Instance.delayInPlayMode is true!");
            // }

            buildBlocked = false;

            if (playModeBlocked)
            {
                playModeBlocked = false;
                if (!errorsFound)
                {
                    // Delaying gives the editor a frame to clear the progress bar.
                    EditorApplication.delayCall += () => {
                        Debug.Log("Compilation completed, entering play mode.");
                        EditorApplication.isPlaying = true;
                    };
                }
                else
                {
                    Debug.LogWarning("Play mode not entered after ink compilation because ink had errors.");
                }
            }

            foreach (var onCompleteAction in onCompleteActions)
            {
                if (onCompleteAction != null)
                {
                    onCompleteAction();
                }
            }
            onCompleteActions.Clear();
        }