File size: 6,959 Bytes
18a519f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System;
using System.IO;
using UnityEngine;

namespace UnityEditor.SettingsManagement
{
    /// <inheritdoc />
    /// <summary>
    /// A settings repository that stores data local to a Unity project.
    /// </summary>
    [Serializable]
    public sealed class PackageSettingsRepository : ISettingsRepository
    {
        const string k_PackageSettingsDirectory = "ProjectSettings/Packages";
        const bool k_PrettyPrintJson = true;

        bool m_Initialized;

        [SerializeField]
        string m_Name;

        [SerializeField]
        string m_Path;

        [SerializeField]
        SettingsDictionary m_Dictionary = new SettingsDictionary();

        string m_cachedJson;

        /// <summary>
        /// Constructor sets the serialized data path.
        /// </summary>
        /// <param name="package">
        /// The package name.
        /// </param>
        /// <param name="name">
        /// A name for this settings file. Settings are saved in `ProjectSettings/Packages/{package}/{name}.json`.
        /// </param>
        public PackageSettingsRepository(string package, string name)
        {
            m_Name = name;
            m_Path = GetSettingsPath(package, name);
            m_Initialized = false;

            AssemblyReloadEvents.beforeAssemblyReload += Save;
            EditorApplication.quitting += Save;
        }

        void Init()
        {
            if (m_Initialized)
                return;

            m_Initialized = true;

            if (File.Exists(path))
            {
                m_Dictionary = null;
                m_cachedJson = File.ReadAllText(path);
                EditorJsonUtility.FromJsonOverwrite(m_cachedJson, this);
                if (m_Dictionary == null)
                    m_Dictionary = new SettingsDictionary();
            }
        }

        /// <value>
        /// This repository implementation is relevant to the Project scope.
        /// </value>
        /// <inheritdoc cref="ISettingsRepository.scope"/>
        public SettingsScope scope
        {
            get { return SettingsScope.Project; }
        }

        /// <value>
        /// The full path to the settings file.
        /// This corresponds to `Unity Project/Project Settings/Packages/com.unity.package/name`.
        /// </value>
        /// <inheritdoc cref="ISettingsRepository.path"/>
        public string path
        {
            get { return m_Path; }
        }

        /// <summary>
        /// The name of this settings file.
        /// </summary>
        public string name
        {
            get { return m_Name; }
        }

        // Cannot call FindFromAssembly from a constructor or field initializer
//        static string CreateSettingsPath(Assembly assembly, string name)
//        {
//            var info = PackageManager.PackageInfo.FindForAssembly(assembly);
//            return string.Format("{0}/{1}/{2}.json", k_PackageSettingsDirectory, info.name, name);
//        }

        /// <summary>
        /// Get a path for a settings file relative to the calling assembly package directory.
        /// </summary>
        /// <param name="packageName">The name of the package requesting this setting.</param>
        /// <param name="name">An optional name for the settings file. Default is "Settings."</param>
        /// <returns>A package-scoped path to the settings file within Project Settings.</returns>
        public static string GetSettingsPath(string packageName, string name = "Settings")
        {
            return string.Format("{0}/{1}/{2}.json", k_PackageSettingsDirectory, packageName, name);
        }

        /// <summary>
        /// Save all settings to their serialized state.
        /// </summary>
        /// <inheritdoc cref="ISettingsRepository.Save"/>

        public void Save()
        {
            Init();

            if (!File.Exists(path))
            {
                var directory = Path.GetDirectoryName(path);
                Directory.CreateDirectory(directory);
            }

            string newSettingsJson = EditorJsonUtility.ToJson(this, k_PrettyPrintJson);
            bool areJsonsEqual = newSettingsJson == m_cachedJson;

#if UNITY_2019_3_OR_NEWER
            if (!AssetDatabase.IsOpenForEdit(path) && areJsonsEqual == false)
            {
                if (!AssetDatabase.MakeEditable(path))
                {
                    Debug.LogWarning($"Could not save package settings to {path}");
                    return;
                }
            }
#endif

            try
            {
                if (!areJsonsEqual)
                {
                    File.WriteAllText(path, newSettingsJson);
                    m_cachedJson = newSettingsJson;
                }
            }
            catch (UnauthorizedAccessException)
            {
                Debug.LogWarning($"Could not save package settings to {path}");
            }
        }

        /// <summary>
        /// Set a value for key of type T.
        /// </summary>
        /// <param name="key">The settings key.</param>
        /// <param name="value">The value to set. Must be serializable.</param>
        /// <typeparam name="T">Type of value.</typeparam>
        /// <inheritdoc cref="ISettingsRepository.Set{T}"/>
        public void Set<T>(string key, T value)
        {
            Init();
            m_Dictionary.Set<T>(key, value);
        }

        /// <summary>
        /// Get a value with key of type T, or return the fallback value if no matching key is found.
        /// </summary>
        /// <param name="key">The settings key.</param>
        /// <param name="fallback">If no key with a value of type T is found, this value is returned.</param>
        /// <typeparam name="T">Type of value to search for.</typeparam>
        /// <inheritdoc cref="ISettingsRepository.Get{T}"/>
        public T Get<T>(string key, T fallback = default(T))
        {
            Init();
            return m_Dictionary.Get<T>(key, fallback);
        }

        /// <summary>
        /// Does the repository contain a setting with key and type.
        /// </summary>
        /// <param name="key">The settings key.</param>
        /// <typeparam name="T">The type of value to search for.</typeparam>
        /// <returns>True if a setting matching both key and type is found, false if no entry is found.</returns>
        /// <inheritdoc cref="ISettingsRepository.ContainsKey{T}"/>
        public bool ContainsKey<T>(string key)
        {
            Init();
            return m_Dictionary.ContainsKey<T>(key);
        }

        /// <summary>
        /// Remove a key value pair from the settings repository.
        /// </summary>
        /// <param name="key"></param>
        /// <typeparam name="T"></typeparam>
        /// <inheritdoc cref="ISettingsRepository.Remove{T}"/>
        public void Remove<T>(string key)
        {
            Init();
            m_Dictionary.Remove<T>(key);
        }
    }
}