/
EnumerableCollection.cs
265 lines (206 loc) · 6.52 KB
/
EnumerableCollection.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.Serialization;
namespace RPGCore
{
public abstract class EnumerableCollection
{
protected static Dictionary<Type, CollectionInformation> ReflectionCache =
new Dictionary<Type, CollectionInformation> ();
protected int IndexOf (string fieldName)
{
CollectionInformation information = ReflectionInformation ();
for (int i = 0; i < information.fieldNames.Length; i++)
{
if (information.fieldNames[i] == fieldName)
return i;
}
Debug.LogError ("\"" + fieldName + "\" is not a member of " + GetType ().Name + ".");
return -1;
}
protected CollectionInformation ReflectionInformation ()
{
return GetReflectionInformation (GetType ());
}
public static CollectionInformation GetReflectionInformation (Type type)
{
Type collectionType = EnumerableCollection.BaseCollectionType (type);
CollectionInformation information;
bool result = ReflectionCache.TryGetValue (collectionType, out information);
if (!result)
{
information = new CollectionInformation (collectionType);
ReflectionCache.Add (collectionType, information);
}
return information;
}
public static Type BaseCollectionType (Type type)
{
Type currentType = type;
while (true)
{
if (currentType == null)
return null;
Type[] types = currentType.GetGenericArguments ();
if (types.Length != 1)
{
currentType = currentType.BaseType;
continue;
}
return currentType.GetGenericTypeDefinition ();
}
}
}
public class EnumerableCollection<T> : EnumerableCollection, IEnumerable<T>, ISerializationCallbackReceiver
{
[SerializeField, HideInInspector]
private string[] fieldDirectories;
[SerializeField, HideInInspector]
private T[] fieldValues;
[NonSerialized]
private T[] arrayCache = null;
public T this[CollectionEntry entry]
{
get
{
if (arrayCache == null)
Collect ();
if (entry.entryIndex == -1)
entry.entryIndex = IndexOf (entry.Field);
if (entry.entryIndex == -1)
{
Debug.LogError ("EnumerableCollection not working");
return default (T);
}
return arrayCache[entry.entryIndex];
}
}
private void Collect ()
{
List<T> foundObjects = new List<T> ();
CollectionInformation information = ReflectionInformation ();
foreach (FieldInfo info in information.directFields)
{
FieldInfo collectionInfo = GetType ().GetField (info.Name);
if (collectionInfo.FieldType == typeof (T))
{
foundObjects.Add ((T)collectionInfo.GetValue (this));
}
else if (typeof (EnumerableCollection).IsAssignableFrom (info.FieldType))
{
var collection = FieldGetOrCreate<EnumerableCollection> (collectionInfo, this);
CollectionInformation childInformation = GetReflectionInformation (collection.GetType ());
foreach (FieldInfo childInfo in childInformation.directFields)
{
FieldInfo childCollectionInfo = collection.GetType ().GetField (childInfo.Name);
if (childCollectionInfo.FieldType == typeof (T))
{
foundObjects.Add ((T)childCollectionInfo.GetValue (collection));
}
}
}
}
arrayCache = foundObjects.ToArray ();
}
void ISerializationCallbackReceiver.OnBeforeSerialize ()
{
Type thisType = GetType ();
List<string> directories = new List<string> ();
List<T> values = new List<T> ();
CollectionInformation information = ReflectionInformation ();
foreach (FieldInfo info in information.directFields)
{
if (!typeof (EnumerableCollection).IsAssignableFrom (info.FieldType))
continue;
// Find all nested EnumerableCollections
FieldInfo childCollectionValue = thisType.GetField (info.Name);
var collection = FieldGetOrCreate<EnumerableCollection<T>> (childCollectionValue, this);
string parent = info.Name + "/";
CollectionInformation childInformation = collection.ReflectionInformation ();
foreach (FieldInfo childInfo in childInformation.directFields)
{
FieldInfo collectionChildInfo = collection.GetType ().GetField (childInfo.Name);
if (collectionChildInfo.FieldType == typeof (T))
{
directories.Add (parent + childInfo.Name);
values.Add ((T)collectionChildInfo.GetValue (collection));
}
}
}
fieldDirectories = directories.ToArray ();
fieldValues = values.ToArray ();
}
void ISerializationCallbackReceiver.OnAfterDeserialize ()
{
Type thisType = GetType ();
if (fieldDirectories == null || fieldValues == null)
return;
for (int i = 0; i < fieldDirectories.Length; i++)
{
string directory = fieldDirectories[i];
int sperator = directory.IndexOf ('/');
string fieldName = directory.Substring (0, sperator);
FieldInfo thisField = GetSerializationField (thisType, fieldName);
if (thisField == null)
continue;
object obj = FieldGetOrCreate (thisField, this);
string childName = directory.Substring (sperator + 1);
FieldInfo childField = GetSerializationField (obj.GetType (), childName);
if (childField == null)
continue;
T value = fieldValues[i];
childField.SetValue (obj, value);
}
}
public IEnumerator<T> GetEnumerator ()
{
if (arrayCache == null)
Collect ();
return ((IEnumerable<T>)arrayCache).GetEnumerator ();
}
IEnumerator IEnumerable.GetEnumerator ()
{
if (arrayCache == null)
Collect ();
return arrayCache.GetEnumerator ();
}
private static V FieldGetOrCreate<V> (FieldInfo field, object instance)
{
return (V)FieldGetOrCreate (field, instance);
}
private static object FieldGetOrCreate (FieldInfo field, object instance)
{
object obj = field.GetValue (instance);
if (obj == null)
{
obj = Activator.CreateInstance (field.FieldType);
field.SetValue (instance, obj);
}
return obj;
}
private static FieldInfo GetSerializationField (Type type, string name)
{
FieldInfo thisField = type.GetField (name);
if (thisField != null)
return thisField;
// What we serialized got renamed and no longer exists.
FieldInfo[] fields = type.GetFields (BindingFlags.Public | BindingFlags.Instance);
foreach (FieldInfo field in fields)
{
var attributes = field.GetCustomAttributes (typeof (FormerlySerializedAsAttribute), true);
foreach (var attribute in attributes)
{
var serializationAttribute = (FormerlySerializedAsAttribute)attribute;
if (serializationAttribute.oldName == name)
{
return field;
}
}
}
return null;
}
}
}