File size: 15,723 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using UnityEditor;
using UnityEngine.InputSystem.Utilities;

namespace UnityEngine.InputSystem.Editor
{
    /// <summary>
    /// Helpers for working with <see cref="SerializedProperty"/> in the editor.
    /// </summary>
    internal static class SerializedPropertyHelpers
    {
        // Show a PropertyField with a greyed-out default text if the field is empty and not being edited.
        // This is meant to communicate the fact that filling these properties is optional and that Unity will
        // use reasonable defaults if left empty.
        public static void PropertyFieldWithDefaultText(this SerializedProperty prop, GUIContent label, string defaultText)
        {
            GUI.SetNextControlName(label.text);
            var rt = GUILayoutUtility.GetRect(label, GUI.skin.textField);

            EditorGUI.PropertyField(rt, prop, label);
            if (string.IsNullOrEmpty(prop.stringValue) && GUI.GetNameOfFocusedControl() != label.text && Event.current.type == EventType.Repaint)
            {
                using (new EditorGUI.DisabledScope(true))
                {
                    rt.xMin += EditorGUIUtility.labelWidth;
                    GUI.skin.textField.Draw(rt, new GUIContent(defaultText), false, false, false, false);
                }
            }
        }

        public static SerializedProperty GetParentProperty(this SerializedProperty property)
        {
            var path = property.propertyPath;
            var lastDot = path.LastIndexOf('.');
            if (lastDot == -1)
                return null;
            var parentPath = path.Substring(0, lastDot);
            return property.serializedObject.FindProperty(parentPath);
        }

        public static SerializedProperty GetArrayPropertyFromElement(this SerializedProperty property)
        {
            // Arrays have a structure of 'arrayName.Array.data[index]'.
            // Given property should be element and thus 'data[index]'.
            var arrayProperty = property.GetParentProperty();
            Debug.Assert(arrayProperty.name == "Array", "Expecting 'Array' property");
            return arrayProperty.GetParentProperty();
        }

        public static int GetIndexOfArrayElement(this SerializedProperty property)
        {
            var propertyPath = property.propertyPath;
            if (propertyPath[propertyPath.Length - 1] != ']')
                return -1;
            var lastIndexOfLeftBracket = propertyPath.LastIndexOf('[');
            if (int.TryParse(
                propertyPath.Substring(lastIndexOfLeftBracket + 1, propertyPath.Length - lastIndexOfLeftBracket - 2),
                out var index))
                return index;
            return -1;
        }

        public static Type GetArrayElementType(this SerializedProperty property)
        {
            Debug.Assert(property.isArray, $"Property {property.propertyPath} is not an array");

            var fieldType = property.GetFieldType();
            if (fieldType == null)
                throw new ArgumentException($"Cannot determine managed field type of {property.propertyPath}",
                    nameof(property));

            return fieldType.GetElementType();
        }

        public static void ResetValuesToDefault(this SerializedProperty property)
        {
            var isString = property.propertyType == SerializedPropertyType.String;

            if (property.isArray && !isString)
            {
                property.ClearArray();
            }
            else if (property.hasChildren && !isString)
            {
                foreach (var child in property.GetChildren())
                    ResetValuesToDefault(child);
            }
            else
            {
                switch (property.propertyType)
                {
                    case SerializedPropertyType.Float:
                        property.floatValue = default(float);
                        break;

                    case SerializedPropertyType.Boolean:
                        property.boolValue = default(bool);
                        break;

                    case SerializedPropertyType.Enum:
                    case SerializedPropertyType.Integer:
                        property.intValue = default(int);
                        break;

                    case SerializedPropertyType.String:
                        property.stringValue = string.Empty;
                        break;

                    case SerializedPropertyType.ObjectReference:
                        property.objectReferenceValue = null;
                        break;
                }
            }
        }

        public static string ToJson(this SerializedObject serializedObject)
        {
            return JsonUtility.ToJson(serializedObject, prettyPrint: true);
        }

        // The following is functionality that allows turning Unity data into text and text
        // back into Unity data. Given that this is essential functionality for any kind of
        // copypaste support, I'm not sure why the Unity editor API isn't providing this out
        // of the box. Internally, we do have support for this on a whole-object kind of level
        // but not for parts of serialized objects.

        /// <summary>
        ///
        /// </summary>
        /// <param name="property"></param>
        /// <returns></returns>
        /// <remarks>
        /// Converting entire objects to JSON is easy using Unity's serialization system but we cannot
        /// easily convert just a part of the serialized graph to JSON (or any text format for that matter)
        /// and then recreate the same data from text through SerializedProperties. This method helps by manually
        /// turning an arbitrary part of a graph into JSON which can then be used with <see cref="RestoreFromJson"/>
        /// to write the data back into an existing property.
        ///
        /// The primary use for this is copy-paste where serialized data needs to be stored in
        /// <see cref="EditorGUIUtility.systemCopyBuffer"/>.
        /// </remarks>
        public static string CopyToJson(this SerializedProperty property, bool ignoreObjectReferences = false)
        {
            var buffer = new StringBuilder();
            CopyToJson(property, buffer, ignoreObjectReferences);
            return buffer.ToString();
        }

        public static void CopyToJson(this SerializedProperty property, StringBuilder buffer, bool ignoreObjectReferences = false)
        {
            CopyToJson(property, buffer, noPropertyName: true, ignoreObjectReferences: ignoreObjectReferences);
        }

        private static void CopyToJson(this SerializedProperty property, StringBuilder buffer, bool noPropertyName, bool ignoreObjectReferences)
        {
            var propertyType = property.propertyType;
            if (ignoreObjectReferences && propertyType == SerializedPropertyType.ObjectReference)
                return;

            // Property name.
            if (!noPropertyName)
            {
                buffer.Append('"');
                buffer.Append(property.name);
                buffer.Append('"');
                buffer.Append(':');
            }

            // Strings are classified as arrays and have children.
            var isString = propertyType == SerializedPropertyType.String;

            // Property value.
            if (property.isArray && !isString)
            {
                buffer.Append('[');
                var arraySize = property.arraySize;
                var isFirst = true;
                for (var i = 0; i < arraySize; ++i)
                {
                    var element = property.GetArrayElementAtIndex(i);
                    if (ignoreObjectReferences && element.propertyType == SerializedPropertyType.ObjectReference)
                        continue;
                    if (!isFirst)
                        buffer.Append(',');
                    CopyToJson(element, buffer, true, ignoreObjectReferences);
                    isFirst = false;
                }
                buffer.Append(']');
            }
            else if (property.hasChildren && !isString)
            {
                // Any structured data we represent as a JSON object.

                buffer.Append('{');
                var isFirst = true;
                foreach (var child in property.GetChildren())
                {
                    if (ignoreObjectReferences && child.propertyType == SerializedPropertyType.ObjectReference)
                        continue;
                    if (!isFirst)
                        buffer.Append(',');
                    CopyToJson(child, buffer, false, ignoreObjectReferences);
                    isFirst = false;
                }
                buffer.Append('}');
            }
            else
            {
                switch (propertyType)
                {
                    case SerializedPropertyType.Enum:
                    case SerializedPropertyType.Integer:
                        buffer.Append(property.intValue);
                        break;

                    case SerializedPropertyType.Float:
                        buffer.Append(property.floatValue);
                        break;

                    case SerializedPropertyType.String:
                        buffer.Append('"');
                        buffer.Append(property.stringValue.Escape());
                        buffer.Append('"');
                        break;

                    case SerializedPropertyType.Boolean:
                        if (property.boolValue)
                            buffer.Append("true");
                        else
                            buffer.Append("false");
                        break;

                    ////TODO: other property types
                    default:
                        throw new NotImplementedException($"Support for {property.propertyType} property type");
                }
            }
        }

        public static void RestoreFromJson(this SerializedProperty property, string json)
        {
            var parser = new JsonParser(json);
            RestoreFromJson(property, ref parser);
        }

        public static void RestoreFromJson(this SerializedProperty property, ref JsonParser parser)
        {
            var isString = property.propertyType == SerializedPropertyType.String;

            if (property.isArray && !isString)
            {
                property.ClearArray();
                parser.ParseToken('[');
                while (!parser.ParseToken(']') && !parser.isAtEnd)
                {
                    var index = property.arraySize;
                    property.InsertArrayElementAtIndex(index);
                    var elementProperty = property.GetArrayElementAtIndex(index);
                    RestoreFromJson(elementProperty, ref parser);
                    parser.ParseToken(',');
                }
            }
            else if (property.hasChildren && !isString)
            {
                parser.ParseToken('{');
                while (!parser.ParseToken('}') && !parser.isAtEnd)
                {
                    parser.ParseStringValue(out var propertyName);
                    parser.ParseToken(':');

                    var childProperty = property.FindPropertyRelative(propertyName.ToString());
                    if (childProperty == null)
                        throw new ArgumentException($"Cannot find property '{propertyName}' in {property}", nameof(property));

                    RestoreFromJson(childProperty, ref parser);
                    parser.ParseToken(',');
                }
            }
            else
            {
                switch (property.propertyType)
                {
                    case SerializedPropertyType.Float:
                    {
                        parser.ParseNumber(out var num);
                        property.floatValue = (float)num.ToDouble();
                        break;
                    }

                    case SerializedPropertyType.String:
                    {
                        parser.ParseStringValue(out var str);
                        property.stringValue = str.ToString();
                        break;
                    }

                    case SerializedPropertyType.Boolean:
                    {
                        parser.ParseBooleanValue(out var b);
                        property.boolValue = b.ToBoolean();
                        break;
                    }

                    case SerializedPropertyType.Enum:
                    case SerializedPropertyType.Integer:
                    {
                        parser.ParseNumber(out var num);
                        property.intValue = (int)num.ToInteger();
                        break;
                    }

                    default:
                        throw new NotImplementedException(
                            $"Restoring property value of type {property.propertyType} (property: {property})");
                }
            }
        }

        public static IEnumerable<SerializedProperty> GetChildren(this SerializedProperty property)
        {
            if (!property.hasChildren)
                yield break;

            using (var iter = property.Copy())
            {
                var end = iter.GetEndProperty(true);

                // Go to first child.
                if (!iter.Next(true))
                    yield break; // Shouldn't happen; we've already established we have children.

                // Iterate over children.
                while (!SerializedProperty.EqualContents(iter, end))
                {
                    yield return iter;
                    if (!iter.Next(false))
                        break;
                }
            }
        }

        public static FieldInfo GetField(this SerializedProperty property)
        {
            var objectType = property.serializedObject.targetObject.GetType();
            var currentSerializableType = objectType;
            var pathComponents = property.propertyPath.Split('.');

            FieldInfo result = null;
            foreach (var component in pathComponents)
            {
                // Handle arrays. They are followed by "Array" and "data[N]" elements.
                if (result != null && currentSerializableType.IsArray)
                {
                    if (component == "Array")
                        continue;

                    if (component.StartsWith("data["))
                    {
                        currentSerializableType = currentSerializableType.GetElementType();
                        continue;
                    }
                }

                result = currentSerializableType.GetField(component,
                    BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.FlattenHierarchy);
                if (result == null)
                    return null;
                currentSerializableType = result.FieldType;
            }

            return result;
        }

        public static Type GetFieldType(this SerializedProperty property)
        {
            return GetField(property)?.FieldType;
        }

        public static void SetStringValue(this SerializedProperty property, string propertyName, string value)
        {
            var propertyRelative = property?.FindPropertyRelative(propertyName);
            if (propertyRelative != null)
                propertyRelative.stringValue = value;
        }
    }
}
#endif // UNITY_EDITOR