/
ISpy.cs
581 lines (526 loc) · 18.3 KB
/
ISpy.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
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
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Runtime.CompilerServices;
using UnityEngine;
/// <summary>
/// ISpy lets you spy on your Unity objects. This allows us to do things
/// like synchronizing the state of objects in a networked environment, logging
/// how parameters are changing over time (without throwing around Debug.Logs),
/// and just generally move towards an event-driven style of programming.
/// </summary>
namespace ISpy
{
/// <summary>
/// Struct to describe a change in object state.
/// </summary>
/// <remarks>
/// For purposes of logging and synchronizing state between multiple clients.
/// </remarks>
public readonly struct StateChange
{
public object obj { get; }
public object property { get; }
public object value { get; }
public StateChange(object o, object p, object v)
{
obj = o;
property = p;
value = v;
}
}
/// <summary>
/// Class to inherit from (instead of MonoBehaviour) in order to
/// attach a Spy and track parameter changes.
/// </summary>
/// <remarks>
/// Spyable initializes your spy by default in Awake, so you may
/// override the Awake function if you want to disable the spy.
/// </remarks>
/// <example>
/// <code>
/// public class MyComponent : Spyable
/// {
/// private float number;
///
/// public float MyNumber
/// {
/// get
/// {
/// return number;
/// }
/// set
/// {
/// number = value;
/// Leak();
/// }
/// }
///
/// override private void Awake()
/// {
/// // Comment out line below to disable spy
/// base.Awake();
/// }
/// }
/// </code>
/// </example>
public abstract class Spyable : MonoBehaviour, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public static List<Spy> spies = new List<Spy>();
/// <summary>
/// Awake function that can be overwritten by inheriting components.
/// </summary>
/// <remarks>
/// If overwritten, be sure to call base.Awake() in order to still
/// add the spy.
/// </remarks>
protected virtual void Awake()
{
spies.Add(new Spy(this));
}
/// <summary>
/// When called from a property setter, this method will leak the
/// calling class, the property being set, and the value set back
/// to all spies by invoking its PropertyChanged event.
/// </summary>
/// <param name="propertyName">DO NOT SET. The runtime will fill this with the name of the property being set.</param>
public void Leak([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
/// <summary>
/// Class for tracking changes in a parameter.
/// </summary>
public class Spy
{
public Logging.PropertyLog log;
private Spyable obj;
#region CONSTRUCTORS
public Spy(Spyable o)
{
log = new Logging.PropertyLog();
obj = o;
obj.PropertyChanged += log.Append;
}
public Spy(Spyable o, PropertyChangedEventHandler cb)
{
log = new Logging.PropertyLog();
obj = o;
obj.PropertyChanged += cb;
}
#endregion
#region ADDERS
/// <summary>
/// Method to add a new callback function to an existing spy.
/// </summary>
/// <param name="cb">Callback function that reacts to PropertyChanged events to add.</param>
public void Add(PropertyChangedEventHandler cb)
{
obj.PropertyChanged += cb;
}
/// <summary>
/// Method to add a new callback function to an existing spy.
/// </summary>
/// <param name="spy">Spy to add callback to.</param>
/// <param name="function">Callback function to add to spy.</param>
/// <returns>Spy with added callback.</returns>
public static Spy operator+ (Spy spy, PropertyChangedEventHandler function)
{
spy.obj.PropertyChanged += function;
return spy;
}
#endregion
#region REMOVERS
/// <summary>
/// Method to remove an existing callback function from an existing spy.
/// </summary>
/// <param name="cb">Callback function that reacts to PropertyChanged events to remove.</param>
public void Remove(PropertyChangedEventHandler cb)
{
obj.PropertyChanged -= cb;
}
/// <summary>
/// Method to remove an existing callback function from an existing spy.
/// </summary>
/// <param name="spy">Spy to remove callback from.</param>
/// <param name="function">Callback function to remove from spy.</param>
/// <returns>Spy with removed callback.</returns>
public static Spy operator- (Spy spy, PropertyChangedEventHandler function)
{
spy.obj.PropertyChanged -= function;
return spy;
}
#endregion
}
/// <summary>
/// Class for tracking GameObject positions.
/// </summary>
/// <remarks>
/// This class maintains a set of tracked transforms, with methods
/// to add and remove them. Used in the TrackedObject and
/// OmniscientTracker components.
/// </remarks>
public class Tracker
{
public readonly HashSet<Transform> tracked;
#region CONSTRUCTORS
public Tracker(GameObject obj)
{
tracked = new HashSet<Transform>();
tracked.Add(obj.transform);
}
public Tracker(GameObject[] objs)
{
tracked = new HashSet<Transform>();
foreach (GameObject obj in objs)
{
tracked.Add(obj.transform);
}
}
public Tracker(List<GameObject> objs)
{
tracked = new HashSet<Transform>();
foreach (GameObject obj in objs)
{
tracked.Add(obj.transform);
}
}
public Tracker(IEnumerable<GameObject> objs)
{
tracked = new HashSet<Transform>();
foreach (GameObject obj in objs)
{
tracked.Add(obj.transform);
}
}
#endregion
#region ADDERS
public void Add(GameObject obj)
{
tracked.Add(obj.transform);
}
public void Add(GameObject[] objs)
{
foreach (GameObject obj in objs)
{
tracked.Add(obj.transform);
}
}
public void Add(List<GameObject> objs)
{
foreach (GameObject obj in objs)
{
tracked.Add(obj.transform);
}
}
public void Add(IEnumerable<GameObject> objs)
{
foreach (GameObject obj in objs)
{
tracked.Add(obj.transform);
}
}
#endregion
#region REMOVERS
public bool TryRemove(GameObject obj)
{
return tracked.Remove(obj.transform);
}
public int TryRemove(List<GameObject> objs)
{
return tracked.RemoveWhere(t => objs.Contains(t.gameObject));
}
#endregion
}
/// <summary>
/// Component for tracking GameObject positions, specified to GameObjects
/// with a component of type T.
/// </summary>
/// <remarks>
/// This component maintains a set of tracked transforms, with methods
/// to add and remove them.
/// </remarks>
public class Tracker<T> : MonoBehaviour
where T : MonoBehaviour
{
public readonly HashSet<Transform> tracked;
public Tracker()
{
tracked = new HashSet<Transform>();
T[] components = FindObjectsOfType<T>();
foreach (T component in components)
{
tracked.Add(component.transform);
}
}
private void Update()
{
T[] components = FindObjectsOfType<T>();
foreach (T component in components)
{
tracked.Add(component.transform);
}
}
private void LateUpdate()
{
foreach (Transform t in tracker.tracked)
{
if (t.hasChanged)
{
StateChange change = new StateChange(t.gameObject, typeof(Transform), t);
log.Append(Time.time, change);
}
}
}
}
/// <summary>
/// Component for tracking and logging all movements of a GameObject.
/// </summary>
public class TrackedObject : MonoBehaviour
{
private Tracker tracker;
private Logging.StateLog log;
public event Logging.AppendHandler<float, StateChange> OnAppend
{
add
{
log.OnAppend += value;
}
remove
{
log.OnAppend -= value;
}
}
private void Awake()
{
tracker = new Tracker(gameObject);
log = new Logging.StateLog();
}
private void LateUpdate()
{
foreach (Transform t in tracker.tracked)
{
if (t.hasChanged)
{
StateChange change = new StateChange(t.gameObject, typeof(Transform), t);
log.Append(Time.time, change);
}
}
}
}
/// <summary>
/// Component for tracking and logging all movements within a scene.
/// </summary>
public class OmniscientTracker : MonoBehaviour
{
private Tracker tracker;
private Logging.StateLog log;
/// <summary>
/// Function for callbacks to execute whenever a tracked object moves.
/// </summary>
/// <remarks>
/// In order to have your callback called, add it to OnAppend
/// (OnAppend += YourCallback). You may also stop calling it
/// by subtracting it from OnAppend (OnAppend -= YourCallback).
/// </remarks>
public event Logging.AppendHandler<float, StateChange> OnAppend
{
add
{
log.OnAppend += value;
}
remove
{
log.OnAppend -= value;
}
}
private void Awake()
{
log = new Logging.StateLog();
GameObject[] objects = FindObjectsOfType<GameObject>();
tracker = new Tracker(objects);
}
private void Update()
{
tracker.Add(FindObjectsOfType<GameObject>());
}
private void LateUpdate()
{
foreach (Transform t in tracker.tracked)
{
if (t.hasChanged)
{
StateChange change = new StateChange(t.gameObject, typeof(Transform), t);
log.Append(Time.time, change);
t.hasChanged = false;
}
}
}
}
/// <summary>
/// Component for tracking and logging all movements within a scene
/// specified to GameObjects with a component of type T.
/// </summary>
[Obsolete("OmniscientTracker for specified MonoBehaviours is currently non-functional" +
" because Unity does not allow for generic MonoBehaviours.")]
public class OmniscientTracker<T> : MonoBehaviour
where T : MonoBehaviour
{
private Tracker<T> tracker;
private Logging.StateLog log;
/// <summary>
/// Function for callbacks to execute whenever a tracked object moves.
/// </summary>
/// <remarks>
/// In order to have your callback called, add it to OnAppend
/// (OnAppend += YourCallback). You may also stop calling it
/// by subtracting it from OnAppend (OnAppend -= YourCallback).
/// </remarks>
public event Logging.AppendHandler<float, StateChange> OnAppend
{
add
{
log.OnAppend += value;
}
remove
{
log.OnAppend -= value;
}
}
private void Awake()
{
throw new NotImplementedException("OmniscientTracker for specified MonoBehaviours is currently non-functional" +
" because Unity does not allow for generic MonoBehaviours.");
log = new Logging.StateLog();
tracker = new Tracker<T>();
}
private void LateUpdate()
{
foreach (Transform t in tracker.tracked)
{
if (t.hasChanged)
{
StateChange change = new StateChange(t.gameObject, typeof(Transform), t);
log.Append(Time.time, change);
t.hasChanged = false;
}
}
}
}
/// <summary>
/// Classes for logging the things that your spies see.
/// </summary>
namespace Logging
{
/// <summary>
/// A subclass of event arguments, for custom behavior whenever a new log entry is made.
/// </summary>
/// <typeparam name="T">Type of the log timestamp</typeparam>
/// <typeparam name="D">Type of the log data</typeparam>
public class LogArgs<T, D> : EventArgs
{
public T timestamp { get; set; }
public D data { get; set; }
public LogArgs(T t, D d)
{
timestamp = t;
data = d;
}
}
/// <summary>
/// The form that any callback function you want to add to OnAppend must take.
/// </summary>
/// <typeparam name="T">Type of the log timestamp</typeparam>
/// <typeparam name="D">Type of the log data</typeparam>
/// <param name="args">Log entry passed to the callback</param>
public delegate void AppendHandler<T, D>(LogArgs<T, D> args);
/// <summary>
/// Append-only log interface, with an IComparable type T for its timestamps and type D for its data.
/// </summary>
/// <typeparam name="T">Type of log timestamp</typeparam>
/// <typeparam name="D">Type of log entry</typeparam>
public interface Log<T, D>
where T : IComparable
{
event AppendHandler<T, D> OnAppend;
void Append(T timestamp, D data);
}
/// <summary>
/// Base class for producing a log of records, implemented
/// as an ordered dictionary.
/// </summary>
/// <remarks>
/// This class is not thread safe. State changes should all be
/// committed/read on the same thread, ideally. The first type,
/// T, is the type of the log keys (for example, time or some other
/// ordered key).
/// </remarks>
public class DictionaryLog<T, D> : Log<T, D>
where T : IComparable
{
protected SortedDictionary<object, Collection<D>> records;
public event AppendHandler<T, D> OnAppend;
public DictionaryLog()
{
records = new SortedDictionary<object, Collection<D>>();
}
public virtual void Append(T timestamp, D state)
{
if (records.ContainsKey(timestamp))
{
records[timestamp].Add(state);
} else
{
records[timestamp] = new Collection<D>();
records[timestamp].Add(state);
}
OnAppend?.Invoke(new LogArgs<T, D>(timestamp, state));
}
}
/// <summary>
/// Class for producing an ordered log of StateChange records.
/// </summary>
/// <remarks>
/// This class is not thread safe. State changes should all be
/// committed/read on the same thread, ideally.
/// </remarks>
public class StateLog : DictionaryLog<float, StateChange>
{
public override void Append(float timestamp, StateChange state)
{
base.Append(timestamp, state);
}
}
/// <summary>
/// A default property change logging class.
/// </summary>
/// <remarks>
/// This class is not thread safe. State changes should all be
/// committed/read on the same thread, ideally.
/// </remarks>
public class PropertyLog : StateLog
{
/// <summary>
/// A default property logging function.
/// </summary>
/// <remarks>
/// Callbacks such as this one may be added or removed from the spy
/// with the += and -= syntax (or the .Add() and .Remove() methods).
/// </remarks>
/// <param name="sender">Object being spied on.</param>
/// <param name="args">Property changed object, passed on calling Leak().</param>
public void Append(object sender, PropertyChangedEventArgs args)
{
Type type = sender.GetType();
PropertyInfo property = type.GetProperty(args.PropertyName);
object value = property.GetValue(sender);
StateChange change = new StateChange(sender, property, value);
base.Append(Time.time, change);
}
}
}
}