forked from ValveResourceFormat/Datamodel.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttributeList.cs
More file actions
621 lines (532 loc) · 21.7 KB
/
AttributeList.cs
File metadata and controls
621 lines (532 loc) · 21.7 KB
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.ComponentModel;
using System.Linq;
using System.Numerics;
using AttrKVP = System.Collections.Generic.KeyValuePair<string, object?>;
using System.Reflection;
using System.IO;
namespace Datamodel
{
/// <summary>
/// A thread-safe collection of <see cref="Attribute"/>s.
/// </summary>
[DebuggerTypeProxy(typeof(DebugView))]
[DebuggerDisplay("Count = {Count}")]
public class AttributeList : IDictionary<string, object?>, IDictionary
{
internal OrderedDictionary PropertyInfos;
internal OrderedDictionary Inner;
protected object Attribute_ChangeLock = new();
private ICollection<Attribute> GetPropertyBasedAttributes(bool useSerializationName)
{
var result = new List<Attribute>();
foreach (DictionaryEntry entry in PropertyInfos)
{
if(entry.Value is null)
{
throw new InvalidDataException("Property value can not be null");
}
var prop = (PropertyInfo)entry.Value;
var name = useSerializationName ? (string)entry.Key : prop.Name;
var attr = new Attribute(name, this, prop.GetValue(this));
result.Add(attr);
}
return result;
}
internal class DebugView
{
public DebugView(AttributeList item)
{
Item = item;
}
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
protected AttributeList Item;
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public DebugAttribute[] Attributes
=> Item.GetPropertyBasedAttributes(useSerializationName: false).Select(attr => new DebugAttribute(attr))
.Concat(Item.Inner.Values.Cast<Attribute>().Select(attr => new DebugAttribute(attr)))
.ToArray();
[DebuggerDisplay("{Value}", Name = "{Attr.Name,nq}", Type = "{Attr.ValueType.FullName,nq}")]
public class DebugAttribute
{
public DebugAttribute(Attribute attr)
{
Attr = attr;
}
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
readonly Attribute Attr;
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
object? Value { get { return Attr.Value; } }
}
}
/// <summary>
/// Contains the names of Datamodel types which are functionally identical to other types and don't have their own CLR representation.
/// </summary>
public enum OverrideType
{
/// <summary>
/// Maps to <see cref="Vector3"/>.
/// </summary>
Angle,
/// <summary>
/// Maps to <see cref="byte[]"/>.
/// </summary>
Binary,
}
public AttributeList(Datamodel? owner)
{
var propertyAttributes = GetPropertyDerivedAttributeList();
PropertyInfos = new OrderedDictionary(propertyAttributes?.Count ?? 0);
if (propertyAttributes != null)
{
foreach (var attr in propertyAttributes)
{
PropertyInfos.Add(attr.Name, attr.Property);
}
}
Inner = [];
Owner = owner;
}
/// <summary>
/// Gets the <see cref="Datamodel"/> that this AttributeList is owned by.
/// </summary>
public virtual Datamodel? Owner { get; internal set; }
/// <summary>
/// Adds a new attribute to this AttributeList.
/// </summary>
/// <param name="key">The name of the attribute. Must be unique to this AttributeList.</param>
/// <param name="value">The value of the Attribute. Must be of a valid Datamodel type.</param>
public void Add(string key, object? value)
{
this[key] = value;
}
protected virtual ICollection<(string Name, PropertyInfo Property)>? GetPropertyDerivedAttributeList()
{
return null;
}
/// <summary>
/// Gets the given atttribute's "override type". This applies when multiple Datamodel types map to the same CLR type.
/// </summary>
/// <param name="key">The name of the attribute.</param>
/// <returns>The attribute's Datamodel type, if different from its CLR type.</returns>
/// <exception cref="KeyNotFoundException">Thrown when the given attribute is not present in the list.</exception>
public OverrideType? GetOverrideType(string key)
{
var attrib = Inner[key];
if(attrib is null)
{
return null;
}
return ((Attribute)attrib).OverrideType;
}
/// <summary>
/// Sets the given attribute's "override type". This applies when multiple Datamodel types map to the same CLR type.
/// </summary>
/// <param name="key">The name of the attribute.</param>
/// <param name="type">The Datamodel type which the attribute should be stored as when written to DMX, or null.</param>
/// <exception cref="AttributeTypeException">Thrown when the attribute's CLR type does not map to the value given in <paramref name="type"/>.</exception>
public void SetOverrideType(string key, OverrideType? type)
{
var attrib = Inner[key];
if (attrib is not null)
{
((Attribute)attrib).OverrideType = type;
}
}
/// <summary>
/// Inserts an Attribute at the given index.
/// </summary>
private void Insert(int index, Attribute item, bool notify = true)
{
lock (Attribute_ChangeLock)
{
Inner.Remove(item.Name);
Inner.Insert(index, item.Name, item);
}
item.Owner = this;
if (notify)
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item.ToKeyValuePair(), index));
}
public bool Remove(string key)
{
lock (Attribute_ChangeLock)
{
var attr = (Attribute?)Inner[key];
if (attr == null) return false;
var index = IndexOf(key);
Inner.Remove(key);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, attr.ToKeyValuePair(), index));
return true;
}
}
public bool TryGetValue(string key, out object? value)
{
Attribute? result;
lock (Attribute_ChangeLock)
result = (Attribute?)Inner[key];
if (result != null)
{
value = result.RawValue;
return true;
}
else
{
value = null;
return false;
}
}
public virtual bool ContainsKey(string key)
{
ArgumentNullException.ThrowIfNull(key);
lock (Attribute_ChangeLock)
return Inner[key] != null;
}
public ICollection<string> Keys
{
get { lock (Attribute_ChangeLock) return Inner.Keys.Cast<string>().ToArray(); }
}
public ICollection<object?> Values
{
get { lock (Attribute_ChangeLock) return Inner.Values.Cast<Attribute>().Select(attr => attr.Value).ToArray(); }
}
/// <summary>
/// Gets or sets the value of the <see cref="Attribute"/> with the given name.
/// </summary>
/// <param name="name">The name to search for. Cannot be null.</param>
/// <returns>The value associated with the given name.</returns>
/// <exception cref="ArgumentNullException">Thrown when the value of name is null.</exception>
/// <exception cref="KeyNotFoundException">Thrown when an attempt is made to get a name that is not present in this AttributeList.</exception>
/// <exception cref="ElementOwnershipException">Thrown when an attempt is made to set the value of the attribute to an Element from a different <see cref="Datamodel"/>.</exception>
/// <exception cref="AttributeTypeException">Thrown when an attempt is made to set a value that is not of a valid Datamodel attribute type.</exception>
/// <exception cref="IndexOutOfRangeException">Thrown when the maximum number of Attributes allowed in an AttributeList has been reached.</exception>
public virtual object? this[string name]
{
get
{
ArgumentNullException.ThrowIfNull(name);
var attr = (Attribute?)Inner[name];
if (attr == null)
{
var prop_attr = (PropertyInfo?)PropertyInfos[name];
if (prop_attr != null)
{
return prop_attr.GetValue(this);
}
throw new KeyNotFoundException($"{this} does not have an attribute called \"{name}\"");
}
return attr.Value;
}
set
{
ArgumentNullException.ThrowIfNull(name);
if (value != null && !Datamodel.IsDatamodelType(value.GetType()))
throw new AttributeTypeException($"{value.GetType().FullName} is not a valid Datamodel attribute type. (If this is an array, it must implement IList<T>).");
if (Owner != null && this == Owner.PrefixAttributes && value?.GetType() == typeof(Element))
throw new AttributeTypeException("Elements are not supported as prefix attributes.");
var prop = (PropertyInfo?)PropertyInfos[name];
if (prop != null)
{
if (prop.CanWrite)
{
// were actually fine with this being null, it will just set the value to null
// but need to check so the type check doesn't fail if it is null
if(value != null)
{
var valueType = value.GetType();
// types must be equal, or a superclass
if (prop.PropertyType != typeof(Element) && valueType.IsSubclassOf(prop.PropertyType))
{
throw new InvalidDataException($"class property '{prop.Name}' with type '{prop.PropertyType}' does not match the type '{valueType}' of the value being set, this is likely a mismatch between the real class and the class from the datamodel");
}
}
prop.SetValue(this, value);
}
else
{
var existingArray = prop.GetValue(this) as Array<Element>;
var incomingArray = value as Array<Element>;
if (existingArray is not null && incomingArray is not null)
{
// special case for reflection based deserialization
if (existingArray.Count == 0)
{
existingArray.AddRange(incomingArray);
return;
}
else
{
throw new InvalidOperationException($"Attribute '{name}' modifies property {prop.DeclaringType!.Name}.{prop.Name}, which is write only and can't be replaced.");
}
}
throw new InvalidDataException("Property of deserialisation class must be writeable, make sure it's public and has a public setter");
}
return;
}
Attribute? old_attr;
Attribute? new_attr;
int old_index = -1;
lock (Attribute_ChangeLock)
{
old_attr = (Attribute?)Inner[name];
new_attr = new Attribute(name, this, value);
if (old_attr != null)
{
old_index = IndexOf(old_attr.Name);
Inner.Remove(old_attr);
}
Insert(old_index == -1 ? Count : old_index, new Attribute(name, this, value), notify: false);
}
NotifyCollectionChangedEventArgs change_args;
if (old_attr != null)
change_args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, new_attr.ToKeyValuePair(), old_attr.ToKeyValuePair(), old_index);
else
change_args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, new_attr.ToKeyValuePair(), Count);
OnCollectionChanged(change_args);
}
}
/// <summary>
/// Gets or sets the attribute at the given index.
/// </summary>
public AttrKVP this[int index]
{
get
{
var attr = (Attribute?)Inner[index];
if(attr is null)
{
throw new InvalidOperationException($"attribute at index {index} doesn't exist");
}
return attr.ToKeyValuePair();
}
set
{
RemoveAt(index);
Insert(index, new Attribute(value.Key, this, value.Value));
}
}
/// <summary>
/// Removes the attribute at the given index.
/// </summary>
public void RemoveAt(int index)
{
Attribute? attr;
lock (Attribute_ChangeLock)
{
attr = (Attribute?)Inner[index];
if(attr is not null)
{
attr.Owner = null;
Inner.RemoveAt(index);
}
}
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, attr, index));
}
public int IndexOf(string key)
{
lock (Attribute_ChangeLock)
{
int i = 0;
foreach (string name in Inner.Keys)
{
if (name == key) return i;
i++;
}
}
return -1;
}
/// <summary>
/// Removes all Attributes from the Collection.
/// </summary>
public void Clear()
{
lock (Attribute_ChangeLock)
Inner.Clear();
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
public int Count
{
get
{
lock (Attribute_ChangeLock)
return Inner.Count;
}
}
public bool IsFixedSize { get { return false; } }
public bool IsReadOnly { get { return false; } }
public bool IsSynchronized { get { return true; } }
/// <summary>
/// Gets an object which can be used to synchronise access to the items within this AttributeCollection.
/// </summary>
public object SyncRoot { get { return Attribute_ChangeLock; } }
public IEnumerable<AttrKVP> GetAllAttributesForSerialization()
{
foreach (var attr in GetPropertyBasedAttributes(useSerializationName: true))
yield return attr.ToKeyValuePair();
foreach (var attr in this)
yield return attr;
}
public IEnumerator<AttrKVP> GetEnumerator()
{
foreach (var attr in Inner.Values.Cast<Attribute>().ToArray())
yield return attr.ToKeyValuePair();
}
#region Interfaces
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
/// <summary>
/// Raised when <see cref="Element.Name"/>, <see cref="Element.ClassName"/>, <see cref="Element.ID"/> or
/// a custom Element property has changed.
/// </summary>
public event PropertyChangedEventHandler? PropertyChanged;
protected virtual void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName()] string property = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
/// <summary>
/// Raised when an <see cref="Attribute"/> is added, removed, or replaced.
/// </summary>
public event NotifyCollectionChangedEventHandler? CollectionChanged;
protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
Debug.Assert(!(e.NewItems != null && e.NewItems.OfType<Attribute>().Any()) && !(e.OldItems != null && e.OldItems.OfType<Attribute>().Any()));
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
case NotifyCollectionChangedAction.Remove:
case NotifyCollectionChangedAction.Reset:
OnPropertyChanged("Count");
break;
}
OnPropertyChanged("Item[]"); // this is the magic value of System.Windows.Data.Binding.IndexerName that tells the binding engine an indexer has changed
CollectionChanged?.Invoke(this, e);
}
IDictionaryEnumerator IDictionary.GetEnumerator()
{
throw new NotImplementedException();
}
void IDictionary.Remove(object key)
{
Remove((string)key);
}
void IDictionary.Add(object key, object? value)
{
Add((string)key, value);
}
object? IDictionary.this[object key]
{
get
{
return this[(string)key];
}
set
{
this[(string)key] = value;
}
}
bool IDictionary.Contains(object key)
{
return ContainsKey((string)key);
}
ICollection IDictionary.Keys { get { return (ICollection)Keys; } }
ICollection IDictionary.Values { get { return (ICollection)Values; } }
bool ICollection<AttrKVP>.Remove(AttrKVP item)
{
lock (Attribute_ChangeLock)
{
var attr = (Attribute?)Inner[item.Key];
if (attr == null || attr.Value != item.Value) return false;
Remove(attr.Name);
return true;
}
}
void ICollection<AttrKVP>.CopyTo(AttrKVP[] array, int arrayIndex)
{
((ICollection)this).CopyTo(array, arrayIndex);
}
void ICollection.CopyTo(Array array, int index)
{
lock (Attribute_ChangeLock)
foreach (Attribute attr in Inner.Values)
{
array.SetValue(attr.ToKeyValuePair(), index);
index++;
}
}
void ICollection<AttrKVP>.Add(AttrKVP item)
{
this[item.Key] = item.Value;
}
bool ICollection<AttrKVP>.Contains(AttrKVP item)
{
lock (Attribute_ChangeLock)
{
var attr = (Attribute?)Inner[item.Key];
return attr != null && attr.Value == item.Value;
}
}
#endregion
}
/// <summary>
/// Compares two Attribute values, using <see cref="Element.IDComparer"/> for AttributeList comparisons.
/// </summary>
public class ValueComparer : IEqualityComparer
{
/// <summary>
/// Gets a default Attribute value equality comparer.
/// </summary>
public static ValueComparer Default
{
get
{
_Default ??= new ValueComparer();
return _Default;
}
}
static ValueComparer? _Default;
public new bool Equals(object? x, object? y)
{
if(x is null || y is null)
{
return false;
}
var type_x = x.GetType();
var type_y = y.GetType();
if (type_x == null && type_y == null)
return true;
if (type_x != type_y)
return false;
var inner = Datamodel.GetArrayInnerType(type_x);
if (inner != null)
{
var array_left = (IList)x;
var array_right = (IList)y;
if (array_left.Count != array_right.Count) return false;
return !Enumerable.Range(0, array_left.Count).Any(i => !Equals(array_left[i], array_right[i]));
}
else if (type_x == typeof(Element))
return Element.IDComparer.Default.Equals((Element)x, (Element)y);
else
return EqualityComparer<object>.Default.Equals(x, y);
}
public int GetHashCode(object obj)
{
if (obj is Element elem)
return elem.ID.GetHashCode();
var inner = Datamodel.GetArrayInnerType(obj.GetType());
if (inner != null)
{
int hash = 0;
foreach (var item in (IList)obj)
hash ^= item.GetHashCode();
return hash;
}
return obj.GetHashCode();
}
}
}