summaryrefslogtreecommitdiff
path: root/Library/PackageCache/com.unity.timeline@1.2.13/Editor/Window/ViewModel/ScriptableObjectViewPrefs.cs
blob: 66b5cb8681fd9e5a87e7adcf48ff2d07843e5add (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.IO;
using UnityEditorInternal;
using UnityEngine;
using UnityObject = UnityEngine.Object;

namespace UnityEditor.Timeline
{
    class ScriptableObjectViewPrefs<TViewModel> : IDisposable where TViewModel : ScriptableObject
    {
        const string k_DefaultFilePath = "Library/";
        const string k_Extension = ".pref";

        readonly string m_RelativePath;
        readonly string m_AbsolutePath;
        readonly string m_FileName;
        ScriptableObject m_Asset;
        TViewModel m_ViewModel;

        bool isSavable
        {
            get
            {
                return m_Asset != null &&
                    m_ViewModel != null &&
                    !string.IsNullOrEmpty(m_FileName);
            }
        }

        public ScriptableObjectViewPrefs(ScriptableObject asset, string relativeSavePath)
        {
            m_Asset = asset;
            m_RelativePath = string.IsNullOrEmpty(relativeSavePath) ? k_DefaultFilePath : relativeSavePath;
            if (!m_RelativePath.EndsWith("/", StringComparison.Ordinal))
                m_RelativePath += "/";

            m_AbsolutePath = Application.dataPath + "/../" + m_RelativePath;

            var assetKey = GetAssetKey(asset);
            m_FileName = string.IsNullOrEmpty(assetKey) ? string.Empty : assetKey + k_Extension;
        }

        public TViewModel viewModel
        {
            get
            {
                if (m_ViewModel == null)
                {
                    if (m_Asset == null)
                        m_ViewModel = CreateViewModel();
                    else
                        m_ViewModel = LoadViewModel() ?? CreateViewModel();
                }
                return m_ViewModel;
            }
        }

        public void Save()
        {
            if (!isSavable)
                return;

            // make sure the path exists or file write will fail
            if (!Directory.Exists(m_AbsolutePath))
                Directory.CreateDirectory(m_AbsolutePath);

            const bool saveAsText = true;
            InternalEditorUtility.SaveToSerializedFileAndForget(new UnityObject[] { m_ViewModel }, m_RelativePath + m_FileName, saveAsText);
        }

        public void DeleteFile()
        {
            if (!isSavable)
                return;

            var path = m_AbsolutePath + m_FileName;

            if (!File.Exists(path))
                return;

            File.Delete(path);
        }

        public void Dispose()
        {
            if (m_ViewModel != null)
                UnityObject.DestroyImmediate(m_ViewModel);

            m_Asset = null;
        }

        public static TViewModel CreateViewModel()
        {
            var model = ScriptableObject.CreateInstance<TViewModel>();
            model.hideFlags |= HideFlags.HideAndDontSave;
            return model;
        }

        TViewModel LoadViewModel()
        {
            if (string.IsNullOrEmpty(m_FileName))
                return null;

            var objects = InternalEditorUtility.LoadSerializedFileAndForget(m_RelativePath + m_FileName);
            if (objects.Length <= 0 || objects[0] == null)
                return null;

            var model = (TViewModel)objects[0];
            model.hideFlags |= HideFlags.HideAndDontSave;

            return model;
        }

        static string GetAssetKey(UnityObject asset)
        {
            return asset == null ? string.Empty : AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(asset));
        }
    }
}