forked from stride3d/stride
-
Notifications
You must be signed in to change notification settings - Fork 0
/
YamlAssetPath.cs
466 lines (433 loc) · 19.9 KB
/
YamlAssetPath.cs
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using Stride.Core;
using Stride.Core.Annotations;
using Stride.Core.Reflection;
using Stride.Core.Yaml;
namespace Stride.Core.Assets.Yaml
{
/// <summary>
/// A class representing the path of a member or item of an Asset as it is created/consumed by the YAML asset serializers.
/// </summary>
[DataContract]
public sealed class YamlAssetPath
{
/// <summary>
/// An enum representing the type of an element of the path.
/// </summary>
public enum ElementType
{
/// <summary>
/// An element that is a member.
/// </summary>
Member,
/// <summary>
/// An element that is an index or a key.
/// </summary>
Index,
/// <summary>
/// An element that is an item identifier of a collection with ids
/// </summary>
/// <seealso cref="Core.Reflection.ItemId"/>
ItemId
}
/// <summary>
/// A structure representing an element of a <see cref="YamlAssetPath"/>.
/// </summary>
public struct Element : IEquatable<Element>
{
/// <summary>
/// The type of the element.
/// </summary>
public readonly ElementType Type;
/// <summary>
/// The value of the element, corresonding to its <see cref="Type"/>.
/// </summary>
public readonly object Value;
/// <summary>
/// Initializes a new instance of the <see cref="Element"/> structure.
/// </summary>
/// <param name="type">The type of element.</param>
/// <param name="value">The value of the element.</param>
public Element(ElementType type, object value)
{
Type = type;
Value = value;
}
/// <summary>
/// Fetches the name of the member, considering this element is a <see cref="ElementType.Member"/>.
/// </summary>
/// <returns>The name of the member.</returns>
public string AsMember() { if (Type != ElementType.Member) throw new InvalidOperationException("This item is not a Member"); return (string)Value; }
/// <summary>
/// Returns the <see cref="ItemId"/> of this element, considering this element is a <see cref="ElementType.ItemId"/>.
/// </summary>
/// <returns>The <see cref="ItemId"/> of the item.</returns>
public ItemId AsItemId() { if (Type != ElementType.ItemId) throw new InvalidOperationException("This item is not a item Id"); return (ItemId)Value; }
/// <inheritdoc/>
public bool Equals(Element other)
{
return Type == other.Type && Equals(Value, other.Value);
}
/// <inheritdoc/>
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
return obj is Element && Equals((Element)obj);
}
/// <inheritdoc/>
public override int GetHashCode()
{
unchecked { return ((int)Type*397) ^ (Value?.GetHashCode() ?? 0); }
}
public static bool operator ==(Element left, Element right)
{
return left.Equals(right);
}
public static bool operator !=(Element left, Element right)
{
return !left.Equals(right);
}
}
private readonly List<Element> elements = new List<Element>(16);
/// <summary>
/// Initializes a new instance of the <see cref="YamlAssetPath"/> class.
/// </summary>
public YamlAssetPath()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="YamlAssetPath"/> class.
/// </summary>
/// <param name="elements">The elements constituting this path, in proper order.</param>
public YamlAssetPath([NotNull] IEnumerable<Element> elements)
{
if (elements == null) throw new ArgumentNullException(nameof(elements));
this.elements.AddRange(elements);
}
/// <summary>
/// The elements constituting this path.
/// </summary>
[DataMember]
public IReadOnlyList<Element> Elements => elements;
/// <summary>
/// Indicates whether the current path represents the same path of another object.
/// </summary>
/// <param name="other">An object to compare with this path.</param>
/// <returns><c>true</c> if the current path matches the <paramref name="other"/> parameter; otherwise, <c>false</c>.</returns>
public bool Match(YamlAssetPath other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
if (Elements.Count != other.Elements.Count) return false;
return Elements.SequenceEqual(other.Elements);
}
/// <summary>
/// Adds an additional element to the path representing an access to a member of an object.
/// </summary>
/// <param name="memberName">The name of the member.</param>
public void PushMember(string memberName)
{
elements.Add(new Element(ElementType.Member, memberName));
}
/// <summary>
/// Adds an additional element to the path representing an access to an item of a collection or a value of a dictionary that does not use <see cref="ItemId"/>.
/// </summary>
/// <param name="index">The index of the item.</param>
/// <seealso cref="NonIdentifiableCollectionItemsAttribute"/>
/// <seealso cref="ItemId"/>
public void PushIndex(object index)
{
elements.Add(new Element(ElementType.Index, index));
}
/// <summary>
/// Adds an additional element to the path representing an access to an item of an collection or a value of a dictionary.
/// </summary>
/// <param name="itemId">The <see cref="ItemId"/> of the item.</param>
public void PushItemId(ItemId itemId)
{
elements.Add(new Element(ElementType.ItemId, itemId));
}
/// <summary>
/// Adds an additional element.
/// </summary>
/// <param name="element">The <see cref="Element"/> to add.</param>
public void Push(Element element)
{
elements.Add(element);
}
/// <summary>
/// Appends the given <see cref="YamlAssetPath"/> to this instance.
/// </summary>
/// <param name="other">The <see cref="YamlAssetPath"/></param>
/// <returns>A new instance of <see cref="YamlAssetPath"/> corresonding to the given instance appended to this instance.</returns>
[NotNull, Pure]
public YamlAssetPath Append([CanBeNull] YamlAssetPath other)
{
var result = new YamlAssetPath(elements);
if (other != null)
{
result.elements.AddRange(other.elements);
}
return result;
}
/// <summary>
/// Creates a clone of this <see cref="YamlAssetPath"/> instance.
/// </summary>
/// <returns>A new copy of this <see cref="YamlAssetPath"/>.</returns>
[NotNull]
public YamlAssetPath Clone()
{
var clone = new YamlAssetPath(elements);
return clone;
}
/// <summary>
/// Convert this <see cref="YamlAssetPath"/> into a <see cref="MemberPath"/>.
/// </summary>
/// <param name="root">The actual instance that is root of this path.</param>
/// <returns>An instance of <see cref="MemberPath"/> corresponding to the same target than this <see cref="YamlAssetPath"/>.</returns>
[NotNull, Pure]
public MemberPath ToMemberPath(object root)
{
var currentObject = root;
var memberPath = new MemberPath();
foreach (var item in Elements)
{
if (currentObject == null)
throw new InvalidOperationException($"The path [{ToString()}] contains access to a member of a null object.");
switch (item.Type)
{
case ElementType.Member:
{
var typeDescriptor = TypeDescriptorFactory.Default.Find(currentObject.GetType());
var name = item.AsMember();
var memberDescriptor = typeDescriptor.Members.FirstOrDefault(x => x.Name == name);
if (memberDescriptor == null) throw new InvalidOperationException($"The path [{ToString()}] contains access to non-existing member [{name}].");
memberPath.Push(memberDescriptor);
currentObject = memberDescriptor.Get(currentObject);
break;
}
case ElementType.Index:
{
var typeDescriptor = TypeDescriptorFactory.Default.Find(currentObject.GetType());
if (typeDescriptor is ArrayDescriptor arrayDescriptor)
{
if (!(item.Value is int))
{
throw new InvalidOperationException($"The path [{ToString()}] contains non-integer index on an array.");
}
int value = (int)item.Value;
memberPath.Push(arrayDescriptor, value);
currentObject = arrayDescriptor.GetValue(currentObject, value);
}
else if (typeDescriptor is CollectionDescriptor collectionDescriptor)
{
if (collectionDescriptor is SetDescriptor setDescriptor)
{
if (item.Value == null && !collectionDescriptor.ElementType.IsNullable())
{
throw new InvalidOperationException($"The path [{ToString()}] contains a null item on a set.");
}
if (!setDescriptor.Contains(currentObject, item.Value))
{
throw new InvalidOperationException($"The path [{ToString()}] item doesn't exsit on a set.");
}
memberPath.Push(setDescriptor, item.Value);
currentObject = item.Value;
}
else
{
if (!(item.Value is int))
{
throw new InvalidOperationException($"The path [{ToString()}] contains non-integer index on a collection.");
}
memberPath.Push(collectionDescriptor, item.Value);
currentObject = collectionDescriptor.GetValue(currentObject, item.Value);
}
}
else if (typeDescriptor is DictionaryDescriptor dictionaryDescriptor)
{
if (item.Value == null) throw new InvalidOperationException($"The path [{ToString()}] contains a null key on an dictionary.");
memberPath.Push(dictionaryDescriptor, item.Value);
currentObject = dictionaryDescriptor.GetValue(currentObject, item.Value);
}
break;
}
case ElementType.ItemId:
{
var ids = CollectionItemIdHelper.GetCollectionItemIds(currentObject);
var key = ids.GetKey(item.AsItemId());
var typeDescriptor = TypeDescriptorFactory.Default.Find(currentObject.GetType());
if (typeDescriptor is ArrayDescriptor arrayDescriptor)
{
if (!(key is int)) throw new InvalidOperationException($"The path [{ToString()}] contains a non-valid item id on an array.");
int keyInt = (int)key;
memberPath.Push(arrayDescriptor, keyInt);
currentObject = arrayDescriptor.GetValue(currentObject, keyInt);
}
else if (typeDescriptor is CollectionDescriptor collectionDescriptor)
{
if (collectionDescriptor.Category == DescriptorCategory.Set)
{
if (item.Value == null && !collectionDescriptor.ElementType.IsNullable())
{
throw new InvalidOperationException($"The path [{ToString()}] contains a null item on a set.");
}
}
else
{
if (!(key is int))
{
throw new InvalidOperationException($"The path [{ToString()}] contains a non-valid item id on a collection.");
}
}
memberPath.Push(collectionDescriptor, key);
currentObject = collectionDescriptor.GetValue(currentObject, key);
}
else if (typeDescriptor is DictionaryDescriptor dictionaryDescriptor)
{
if (key == null) throw new InvalidOperationException($"The path [{ToString()}] contains a non-valid item id on an dictionary.");
memberPath.Push(dictionaryDescriptor, key);
currentObject = dictionaryDescriptor.GetValue(currentObject, key);
}
break;
}
default:
throw new ArgumentOutOfRangeException();
}
}
return memberPath;
}
/// <summary>
/// Creates a <see cref="YamlAssetPath"/> out of a <see cref="MemberPath"/> instance.
/// </summary>
/// <param name="path">The <see cref="MemberPath"/> from which to create a <see cref="YamlAssetPath"/>.</param>
/// <param name="root">The root object of the given <see cref="MemberPath"/>.</param>
/// <returns>An instance of <see cref="YamlAssetPath"/> corresponding to the same target than the given <see cref="MemberPath"/>.</returns>
[NotNull]
public static YamlAssetPath FromMemberPath([NotNull] MemberPath path, object root)
{
if (path == null) throw new ArgumentNullException(nameof(path));
var result = new YamlAssetPath();
var clone = new MemberPath();
foreach (var item in path.Decompose())
{
if (item.MemberDescriptor != null)
{
clone.Push(item.MemberDescriptor);
var member = item.MemberDescriptor.Name;
result.PushMember(member);
}
else
{
object index = null;
if (item is MemberPath.ArrayPathItem arrayItem)
{
clone.Push(arrayItem.Descriptor, arrayItem.Index);
index = arrayItem.Index;
}
else if (item is MemberPath.CollectionPathItem collectionItem)
{
clone.Push(collectionItem.Descriptor, collectionItem.Index);
index = collectionItem.Index;
}
else if (item is MemberPath.DictionaryPathItem dictionaryItem)
{
clone.Push(dictionaryItem.Descriptor, dictionaryItem.Key);
index = dictionaryItem.Key;
}
else if (item is MemberPath.SetPathItem setItem)
{
clone.Push(setItem.Descriptor, setItem.Index);
index = setItem.Index;
}
if (!CollectionItemIdHelper.TryGetCollectionItemIds(clone.GetValue(root), out CollectionItemIdentifiers ids))
{
result.PushIndex(index);
}
else
{
var id = ids[index];
// Create a new id if we don't have any so far
if (id == ItemId.Empty)
id = ItemId.New();
result.PushItemId(id);
}
}
}
return result;
}
public bool StartsWith([NotNull] YamlAssetPath path)
{
if (path == null) throw new ArgumentNullException(nameof(path));
if (path.elements.Count > elements.Count)
return false;
for (var i = 0; i < path.Elements.Count; ++i)
{
if (!Elements[i].Equals(path.Elements[i]))
return false;
}
return true;
}
/// <inheritdoc/>
public override string ToString()
{
var sb = new StringBuilder();
sb.Append("(object)");
foreach (var item in elements)
{
switch (item.Type)
{
case ElementType.Member:
sb.Append('.');
sb.Append(item.Value);
break;
case ElementType.Index:
sb.Append('[');
sb.Append(item.Value);
sb.Append(']');
break;
case ElementType.ItemId:
sb.Append('{');
sb.Append(item.Value);
sb.Append('}');
break;
default:
throw new ArgumentOutOfRangeException();
}
}
return sb.ToString();
}
internal static bool IsCollectionWithIdType([NotNull] Type type, object key, out ItemId id, out object actualKey)
{
if (type.IsGenericType)
{
if (type.GetGenericTypeDefinition() == typeof(CollectionWithItemIds<>))
{
id = (ItemId)key;
actualKey = key;
return true;
}
if (type.GetGenericTypeDefinition() == typeof(DictionaryWithItemIds<,>))
{
var keyWithId = (IKeyWithId)key;
id = keyWithId.Id;
actualKey = keyWithId.Key;
return true;
}
}
id = ItemId.Empty;
actualKey = key;
return false;
}
internal static bool IsCollectionWithIdType([NotNull] Type type, object key, out ItemId id)
{
return IsCollectionWithIdType(type, key, out id, out object _);
}
}
}