Class that saves and loads a binary temp file for the TextScene system. It delays each operation by a set amount of frames because it seems to make the operation a lot more stable :S
    /// <summary>
    /// Creates a new TextSceneMonitor and reads in relevant values from PlayerPrefs (such as
    /// last monitored scene).
    /// </summary>
    public TextSceneMonitor()
    {
        process = null;

        currentScene = PlayerPrefs.GetString("TextSceneMonitorCurrentScene", "");

        alarmingEditorScene = PlayerPrefs.GetString("TextSceneAlarmingEditorScene", "");

        currentTempBinaryScene = EditorHelper.GetProjectFolder() + alarmingEditorScene;

        //Use file timestamp instead of DateTime.Now for consistency reasons.
        if (File.Exists(currentTempBinaryScene))
        {
            currentSceneLoaded = File.GetLastWriteTime(currentTempBinaryScene);
        }
        else
        {
            Debug.LogWarning("Unable to find temp file: " + currentTempBinaryScene);
            currentSceneLoaded = DateTime.Now;
        }

        Debug.Log("Creating new TextSceneMonitor instance: " + currentScene);
    }
Пример #2
0
    /// <summary>
    /// Creates a new TextSceneMonitor and reads in relevant values from PlayerPrefs (such as
    /// last monitored scene).
    /// </summary>
    public TextSceneMonitor()
    {
        process = null;

        currentScene = PlayerPrefs.GetString("TextSceneMonitorCurrentScene", "");

        alarmingEditorScene = PlayerPrefs.GetString("TextSceneAlarmingEditorScene", "");

        currentTempBinaryScene = EditorHelper.GetProjectFolder() + alarmingEditorScene;

        //Use file timestamp instead of DateTime.Now for consistency reasons.
        if (File.Exists(currentTempBinaryScene))
        {
            currentSceneLoaded = File.GetLastWriteTime(currentTempBinaryScene);
        }
        else
        {
            Debug.LogWarning("Unable to find temp file: " + currentTempBinaryScene);
            currentSceneLoaded = DateTime.Now;
        }

        Debug.Log("Creating new TextSceneMonitor instance: " + currentScene);
    }
    /// <summary>
    /// Regularly checks if something worthy of notifying the user happens. This includes
    /// unexpected scene changes (double-clicking .unity files), creating new scenes and
    /// source TextScene files having been changed between now and the time it was loaded.
    /// </summary>
    private void Update()
    {
        //HACK: To get around bug (TOOD: Insert case #) where Save immediately
        //      after instantiating prefabs results in weird behaviour.
        if (process != null)
        {
            TextSceneTempCreator.Status status = process.Update();

            switch (status)
            {
                case TextSceneTempCreator.Status.Failed:
                    Debug.LogError("Creating temp files failed!");
                    process = null;
                    break;
                case TextSceneTempCreator.Status.Complete:
                    Debug.Log("Creating temp files succeeded!");

                    //FIXME: Either do this, or get a reference to the delegate and call
                    //       if after clearing tempCreator. The callback might
                    //       end up setting the tempCreator again, typically in a build-cycle,
                    //       for example.
                    TextSceneTempCreator tempCreatorRef = process;

                    process = null;

                    tempCreatorRef.InvokeCallback();

                    break;
                default:
                break;
            }

            return;
        }

        //Did the user create a new scene?
        if (currentScene.Length > 0
            && EditorApplication.currentScene.Length == 0)
        {
            if (CheckForChangedTemp())
                EditorApplication.NewScene();

            currentScene = "";
            alarmingEditorScene = "";

            TextSceneSerializer.SaveCurrent();

            //Warn the user if the save scene dialog was cancelled.
            if (currentScene.Length == 0)
                EditorUtility.DisplayDialog("New Scene", "You have started a new scene after using the TextScene system. Please use the TextScene menu to save it if you want to continue using the TextScene system", "OK");
        }

        //Try to detect when we go from a TextScene to a regular unit scene.
        if ((currentScene.Length > 0
            && EditorApplication.currentScene.Length > 0)
            || (currentScene.Length == 0
            && alarmingEditorScene.Length == 0))
        {
            if (alarmingEditorScene != EditorApplication.currentScene)
            {
                string current = EditorApplication.currentScene;

                if (CheckForChangedTemp())
                    EditorApplication.OpenScene(current);

                if (EditorUtility.DisplayDialog("TextScene/built-in mixed usage", "It is not recommended to mix TextScene usage with built-in Unity scenes. This may cause the TextScene system to miss updates or simply behave totally weird! If you plan on using the TextScene system, you should save the current scene via the TextScene menu item and  - if successfully saved (please inspect the log for errors and warnings) - remove the original from the Assets folder. Please note that not all components/Unity objects can be saved into the TextScene format, so don't delete the original until you are 100% sure you saved what you need!", "Save to TextScene now!", "I know what I'm doing"))
                {
                    TextSceneSerializer.SaveCurrent();
                }
                else
                {
                    alarmingEditorScene = EditorApplication.currentScene;
                    currentScene = "";
                }
            }
        }

        //Regular checks to see if the scene file was edited since our last load.
        if (currentScene.Length > 0
            && EditorApplication.timeSinceStartup > nextCheckForChangedFile)
        {
            if (File.Exists(currentScene))
            {
                DateTime lastWriteTime = File.GetLastWriteTime(currentScene);

                if (lastWriteTime > currentSceneLoaded)
                {
                    int result = EditorUtility.DisplayDialogComplex("Scene changed", "The TextScene you currently have open changed (" + lastWriteTime + "). Do you want to reload it?", "Yes", "Backup mine first", "No");

                    if (result == 0)//Yes
                    {
                        TextSceneDeserializer.Load(currentScene);
                    }
                    else if (result == 1)//Backup first
                    {
                        string filename = EditorUtility.SaveFilePanel("Backup TextScene", currentScene.Substring(0, currentScene.LastIndexOf('/')), "backup", "txt");

                        if (filename.Length != 0)
                        {
                            //This is overwritten during the save.
                            string toLoad = currentScene;

                            TextSceneSerializer.Save(filename);
                            TextSceneDeserializer.Load(toLoad);
                        }
                        else
                        {
                            EditorUtility.DisplayDialog("Unsaved", "You chose to cancel the backup of your own scene file. It is recommended that you manually save a copy, and merge with the updated file from disk (" + currentScene + ")", "OK");
                        }
                    }
                    else//No
                    {
                        //HACK: Shut this message up. We don't want to get asked this again until the
                        //      file changes again.
                        currentSceneLoaded = DateTime.Now;
                    }
                }
            }
            else
            {
                if (EditorUtility.DisplayDialog("TextScene file gone!", "It seems like the TextScene representation of your open file has been deleted. Do you want to re-save it (" + currentScene + ")?", "Yes", "No"))
                    TextSceneSerializer.Save(currentScene);
                else
                    currentScene = "";
            }

            //Also check for changed temp files (.unity in TempScenes). This happens if
            //the user uses the built-in save functionality.
            CheckForChangedTemp();

            nextCheckForChangedFile = EditorApplication.timeSinceStartup + 1.0f;
        }
    }
 public void DoSaveAndReload(string scene, TextSceneDeserializer.TempSceneSaved callback)
 {
     //Must wait a couple of frames before we do the save for prefabs to behave correctly.
     //TODO: Report bug.
     process = new TextSceneTempCreator(scene, callback);
 }
Пример #5
0
    /// <summary>
    /// Regularly checks if something worthy of notifying the user happens. This includes
    /// unexpected scene changes (double-clicking .unity files), creating new scenes and
    /// source TextScene files having been changed between now and the time it was loaded.
    /// </summary>
    private void Update()
    {
        //HACK: To get around bug (TOOD: Insert case #) where Save immediately
        //      after instantiating prefabs results in weird behaviour.
        if (process != null)
        {
            TextSceneTempCreator.Status status = process.Update();

            switch (status)
            {
            case TextSceneTempCreator.Status.Failed:
                Debug.LogError("Creating temp files failed!");
                process = null;
                break;

            case TextSceneTempCreator.Status.Complete:
                Debug.Log("Creating temp files succeeded!");

                //FIXME: Either do this, or get a reference to the delegate and call
                //       if after clearing tempCreator. The callback might
                //       end up setting the tempCreator again, typically in a build-cycle,
                //       for example.
                TextSceneTempCreator tempCreatorRef = process;

                process = null;

                tempCreatorRef.InvokeCallback();

                break;

            default:
                break;
            }

            return;
        }

        //Did the user create a new scene?
        if (currentScene.Length > 0 &&
            EditorApplication.currentScene.Length == 0)
        {
            if (CheckForChangedTemp())
            {
                EditorApplication.NewScene();
            }

            currentScene        = "";
            alarmingEditorScene = "";

            TextSceneSerializer.SaveCurrent();

            //Warn the user if the save scene dialog was cancelled.
            if (currentScene.Length == 0)
            {
                EditorUtility.DisplayDialog("New Scene", "You have started a new scene after using the TextScene system. Please use the TextScene menu to save it if you want to continue using the TextScene system", "OK");
            }
        }

        //Try to detect when we go from a TextScene to a regular unit scene.
        if ((currentScene.Length > 0 &&
             EditorApplication.currentScene.Length > 0) ||
            (currentScene.Length == 0 &&
             alarmingEditorScene.Length == 0))
        {
            if (alarmingEditorScene != EditorApplication.currentScene)
            {
                string current = EditorApplication.currentScene;

                if (CheckForChangedTemp())
                {
                    EditorApplication.OpenScene(current);
                }

                if (EditorUtility.DisplayDialog("TextScene/built-in mixed usage", "It is not recommended to mix TextScene usage with built-in Unity scenes. This may cause the TextScene system to miss updates or simply behave totally weird! If you plan on using the TextScene system, you should save the current scene via the TextScene menu item and  - if successfully saved (please inspect the log for errors and warnings) - remove the original from the Assets folder. Please note that not all components/Unity objects can be saved into the TextScene format, so don't delete the original until you are 100% sure you saved what you need!", "Save to TextScene now!", "I know what I'm doing"))
                {
                    TextSceneSerializer.SaveCurrent();
                }
                else
                {
                    alarmingEditorScene = EditorApplication.currentScene;
                    currentScene        = "";
                }
            }
        }

        //Regular checks to see if the scene file was edited since our last load.
        if (currentScene.Length > 0 &&
            EditorApplication.timeSinceStartup > nextCheckForChangedFile)
        {
            if (File.Exists(currentScene))
            {
                DateTime lastWriteTime = File.GetLastWriteTime(currentScene);

                if (lastWriteTime > currentSceneLoaded)
                {
                    int result = EditorUtility.DisplayDialogComplex("Scene changed", "The TextScene you currently have open changed (" + lastWriteTime + "). Do you want to reload it?", "Yes", "Backup mine first", "No");

                    if (result == 0)                    //Yes
                    {
                        TextSceneDeserializer.Load(currentScene);
                    }
                    else if (result == 1)//Backup first
                    {
                        string filename = EditorUtility.SaveFilePanel("Backup TextScene", currentScene.Substring(0, currentScene.LastIndexOf('/')), "backup", "txt");

                        if (filename.Length != 0)
                        {
                            //This is overwritten during the save.
                            string toLoad = currentScene;

                            TextSceneSerializer.Save(filename);
                            TextSceneDeserializer.Load(toLoad);
                        }
                        else
                        {
                            EditorUtility.DisplayDialog("Unsaved", "You chose to cancel the backup of your own scene file. It is recommended that you manually save a copy, and merge with the updated file from disk (" + currentScene + ")", "OK");
                        }
                    }
                    else//No
                    {
                        //HACK: Shut this message up. We don't want to get asked this again until the
                        //      file changes again.
                        currentSceneLoaded = DateTime.Now;
                    }
                }
            }
            else
            {
                if (EditorUtility.DisplayDialog("TextScene file gone!", "It seems like the TextScene representation of your open file has been deleted. Do you want to re-save it (" + currentScene + ")?", "Yes", "No"))
                {
                    TextSceneSerializer.Save(currentScene);
                }
                else
                {
                    currentScene = "";
                }
            }

            //Also check for changed temp files (.unity in TempScenes). This happens if
            //the user uses the built-in save functionality.
            CheckForChangedTemp();

            nextCheckForChangedFile = EditorApplication.timeSinceStartup + 1.0f;
        }
    }
Пример #6
0
 public void DoSaveAndReload(string scene, TextSceneDeserializer.TempSceneSaved callback)
 {
     //Must wait a couple of frames before we do the save for prefabs to behave correctly.
     //TODO: Report bug.
     process = new TextSceneTempCreator(scene, callback);
 }