-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathEditorFieldCollection.cs
More file actions
147 lines (124 loc) · 3.91 KB
/
EditorFieldCollection.cs
File metadata and controls
147 lines (124 loc) · 3.91 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
using RPGCore.DataEditor.Manifest;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
namespace RPGCore.DataEditor;
/// <summary>
/// Represents a collection of <see cref="EditorField"/>s belonging to an <see cref="EditorObject"/>.
/// </summary>
[DebuggerDisplay("Count = {Count,nq}")]
public class EditorFieldCollection : IEnumerable<EditorField>
{
private class EditorFieldComparer : IComparer<EditorField>
{
private readonly SchemaType schemaType;
public EditorFieldComparer(SchemaType schemaType)
{
this.schemaType = schemaType;
}
public int Compare(EditorField x, EditorField y)
{
if (schemaType.Fields == null)
{
return 0;
}
var xSchema = x.Schema;
var ySchema = y.Schema;
int xIndex = xSchema == null ? 0 : schemaType.Fields.IndexOf(xSchema);
int yIndex = ySchema == null ? 0 : schemaType.Fields.IndexOf(ySchema);
return xIndex.CompareTo(yIndex);
}
}
private readonly EditorObject owner;
private readonly List<EditorField> internalCollection;
/// <summary>
/// Gets the number of <see cref="EditorField"/>s contained in this <see cref="EditorFieldCollection"/>.
/// </summary>
/// <returns>
/// The number of <see cref="EditorField"/>s contained in this <see cref="EditorFieldCollection"/>.
/// </returns>
public int Count => internalCollection.Count;
/// <summary>
/// Gets an <see cref="EditorField"/> at the specified <paramref name="index"/>.
/// </summary>
/// <param name="index">The zero-based index of the <see cref="EditorField"/> to get.</param>
/// <returns>The <see cref="EditorField"/> at the specified <paramref name="index"/>.</returns>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is less than 0. -or- <paramref name="index"/> is equal to or greater than <see cref="Count"/>.</exception>
public EditorField this[int index] => internalCollection[index];
/// <summary>
/// Gets an <see cref="EditorField"/> with the specified <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the <see cref="EditorField"/> to search for.</param>
/// <returns>The <see cref="EditorField"/> with the specified <paramref name="name"/> If an <see cref="EditorField"/> cannot be found; returns <c>null</c>.</returns>
public EditorField? this[string name]
{
get
{
foreach (var field in internalCollection)
{
if (field.Name == name)
{
return field;
}
}
return null;
}
}
internal EditorFieldCollection(EditorObject owner)
{
internalCollection = new List<EditorField>();
this.owner = owner;
SyncFieldsWithType();
}
/// <inheritdoc/>
public IEnumerator<EditorField> GetEnumerator()
{
return internalCollection.GetEnumerator();
}
internal void SyncFieldsWithType()
{
var type = owner.Session.Manifest.GetTypeInformation(owner.Type.Identifier);
if (type == null)
{
throw new InvalidOperationException($"Cannot resync fields with type {owner.Type} as it could not be resolved.");
}
AddMissingFields(type);
internalCollection.Sort(new EditorFieldComparer(type));
}
private void AddMissingFields(SchemaType type)
{
if (type == null)
{
throw new InvalidOperationException($"Cannot determine any type information for the type \"{owner.Type}\".");
}
if (type.Fields == null)
{
throw new InvalidOperationException($"Cannot use \"{owner.Type}\" for an object as it doesn't have any fields.");
}
foreach (var field in type.Fields)
{
if (!ContainsField(field.Name))
{
var newField = new EditorField(owner, field.Name);
newField.Value.Value = owner.Session.CreateDefaultValue(field.Type);
internalCollection.Add(newField);
}
}
}
private bool ContainsField(string field)
{
foreach (var otherField in internalCollection)
{
if (otherField.Name == field)
{
return true;
}
}
return false;
}
IEnumerator IEnumerable.GetEnumerator()
{
return internalCollection.GetEnumerator();
}
}