File size: 12,586 Bytes
18a519f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
using UnityObject = UnityEngine.Object;

namespace Unity.VisualScripting
{
    [Descriptor(typeof(IUnit))]
    public class UnitDescriptor<TUnit> : Descriptor<TUnit, UnitDescription>, IUnitDescriptor
        where TUnit : class, IUnit
    {
        public UnitDescriptor(TUnit target) : base(target)
        {
            unitType = unit.GetType();
        }

        protected Type unitType { get; }

        public TUnit unit => target;

        IUnit IUnitDescriptor.unit => unit;

        private enum State
        {
            Defined,

            NotDefined,

            FailedToDefine
        }

        private State state
        {
            get
            {
                if (unit.isDefined)
                {
                    return State.Defined;
                }
                else if (unit.failedToDefine)
                {
                    return State.FailedToDefine;
                }
                else
                {
                    return State.NotDefined;
                }
            }
        }


        #region Reflected Description

        static UnitDescriptor()
        {
            XmlDocumentation.loadComplete += FreeReflectedDescriptions;
        }

        public static void FreeReflectedDescriptions()
        {
            reflectedDescriptions.Clear();
            reflectedInputDescriptions.Clear();
            reflectedOutputDescriptions.Clear();
        }

        protected UnitDescription reflectedDescription
        {
            get
            {
                if (!reflectedDescriptions.TryGetValue(unitType, out var reflectedDescription))
                {
                    reflectedDescription = FetchReflectedDescription(unitType);
                    reflectedDescriptions.Add(unitType, reflectedDescription);
                }

                return reflectedDescription;
            }
        }

        protected UnitPortDescription ReflectedPortDescription(IUnitPort port)
        {
            if (port is IUnitInvalidPort)
            {
                return null;
            }

            if (port is IUnitInputPort)
            {
                if (!reflectedInputDescriptions.TryGetValue(unitType, out var _reflectedInputDescriptions))
                {
                    _reflectedInputDescriptions = FetchReflectedPortDescriptions<IUnitInputPort>(unitType);
                    reflectedInputDescriptions.Add(unitType, _reflectedInputDescriptions);
                }

                if (_reflectedInputDescriptions.TryGetValue(port.key, out var portDescription))
                {
                    return portDescription;
                }
            }
            else if (port is IUnitOutputPort)
            {
                if (!reflectedOutputDescriptions.TryGetValue(unitType, out var _reflectedOutputDescriptions))
                {
                    _reflectedOutputDescriptions = FetchReflectedPortDescriptions<IUnitOutputPort>(unitType);
                    reflectedOutputDescriptions.Add(unitType, _reflectedOutputDescriptions);
                }

                if (_reflectedOutputDescriptions.TryGetValue(port.key, out var portDescription))
                {
                    return portDescription;
                }
            }

            return null;
        }

        private static readonly Dictionary<Type, UnitDescription> reflectedDescriptions = new Dictionary<Type, UnitDescription>();

        private static readonly Dictionary<Type, Dictionary<string, UnitPortDescription>> reflectedInputDescriptions = new Dictionary<Type, Dictionary<string, UnitPortDescription>>();

        private static readonly Dictionary<Type, Dictionary<string, UnitPortDescription>> reflectedOutputDescriptions = new Dictionary<Type, Dictionary<string, UnitPortDescription>>();

        private static UnitDescription FetchReflectedDescription(Type unitType)
        {
            var oldName = BoltFlowNameUtility.UnitPreviousTitle(unitType);
            var prefix = string.IsNullOrEmpty(oldName) ? string.Empty : $"(Previously named {oldName}) ";

            return new UnitDescription()
            {
                title = BoltFlowNameUtility.UnitTitle(unitType, false, true),
                shortTitle = BoltFlowNameUtility.UnitTitle(unitType, true, true),
                surtitle = unitType.GetAttribute<UnitSurtitleAttribute>()?.surtitle,
                subtitle = unitType.GetAttribute<UnitSubtitleAttribute>()?.subtitle,
                summary = prefix + unitType.Summary()
            };
        }

        private static Dictionary<string, UnitPortDescription> FetchReflectedPortDescriptions<T>(Type unitType) where T : IUnitPort
        {
            var descriptions = new Dictionary<string, UnitPortDescription>();

            foreach (var portMember in unitType.GetMembers().Where(member => typeof(T).IsAssignableFrom(member.GetAccessorType())))
            {
                var key = portMember.GetAttribute<PortKeyAttribute>()?.key ?? portMember.Name;

                if (descriptions.ContainsKey(key))
                {
                    Debug.LogWarning("Duplicate reflected port description for: " + key);

                    continue;
                }

                descriptions.Add(key, FetchReflectedPortDescription(portMember));
            }

            return descriptions;
        }

        private static UnitPortDescription FetchReflectedPortDescription(MemberInfo portMember)
        {
            return new UnitPortDescription()
            {
                label = portMember.GetAttribute<PortLabelAttribute>()?.label ?? portMember.HumanName(),
                showLabel = !(portMember.HasAttribute<PortLabelHiddenAttribute>() || (portMember.GetAttribute<PortLabelAttribute>()?.hidden ?? false)),
                summary = portMember.Summary(),
                getMetadata = (unitMetadata) => unitMetadata[portMember.Name]
            };
        }

        #endregion


        #region Description

        [Assigns]
        public sealed override string Title()
        {
            switch (state)
            {
                case State.Defined: return DefinedTitle();
                case State.NotDefined: return DefaultTitle();
                case State.FailedToDefine: return ErrorTitle(unit.definitionException);
                default: throw new UnexpectedEnumValueException<State>(state);
            }
        }

        [Assigns]
        public string ShortTitle()
        {
            switch (state)
            {
                case State.Defined: return DefinedShortTitle();
                case State.NotDefined: return DefaultShortTitle();
                case State.FailedToDefine: return ErrorShortTitle(unit.definitionException);
                default: throw new UnexpectedEnumValueException<State>(state);
            }
        }

        [Assigns]
        public string Surtitle()
        {
            switch (state)
            {
                case State.Defined: return DefinedSurtitle();
                case State.NotDefined: return DefaultSurtitle();
                case State.FailedToDefine: return ErrorSurtitle(unit.definitionException);
                default: throw new UnexpectedEnumValueException<State>(state);
            }
        }

        [Assigns]
        public string Subtitle()
        {
            switch (state)
            {
                case State.Defined: return DefinedSubtitle();
                case State.NotDefined: return DefaultSubtitle();
                case State.FailedToDefine: return ErrorSubtitle(unit.definitionException);
                default: throw new UnexpectedEnumValueException<State>(state);
            }
        }

        [Assigns]
        public sealed override string Summary()
        {
            switch (state)
            {
                case State.Defined: return DefinedSummary();
                case State.NotDefined: return DefaultSummary();
                case State.FailedToDefine: return ErrorSummary(unit.definitionException);
                default: throw new UnexpectedEnumValueException<State>(state);
            }
        }

        [Assigns]
        [RequiresUnityAPI]
        public sealed override EditorTexture Icon()
        {
            switch (state)
            {
                case State.Defined: return DefinedIcon();
                case State.NotDefined: return DefaultIcon();
                case State.FailedToDefine: return ErrorIcon(unit.definitionException);
                default: throw new UnexpectedEnumValueException<State>(state);
            }
        }

        [Assigns]
        [RequiresUnityAPI]
        public IEnumerable<EditorTexture> Icons()
        {
            switch (state)
            {
                case State.Defined: return DefinedIcons();
                case State.NotDefined: return DefaultIcons();
                case State.FailedToDefine: return ErrorIcons(unit.definitionException);
                default: throw new UnexpectedEnumValueException<State>(state);
            }
        }

        protected virtual string DefinedTitle()
        {
            return reflectedDescription.title;
        }

        protected virtual string DefaultTitle()
        {
            return reflectedDescription.title;
        }

        protected virtual string ErrorTitle(Exception exception)
        {
            return reflectedDescription.title;
        }

        protected virtual string DefinedShortTitle()
        {
            return reflectedDescription.shortTitle;
        }

        protected virtual string DefaultShortTitle()
        {
            return reflectedDescription.shortTitle;
        }

        protected virtual string ErrorShortTitle(Exception exception)
        {
            return ErrorTitle(exception);
        }

        protected virtual string DefinedSurtitle()
        {
            return reflectedDescription.surtitle;
        }

        protected virtual string DefaultSurtitle()
        {
            return reflectedDescription.surtitle;
        }

        protected virtual string ErrorSurtitle(Exception exception)
        {
            return null;
        }

        protected virtual string DefinedSubtitle()
        {
            return reflectedDescription.subtitle;
        }

        protected virtual string DefaultSubtitle()
        {
            return reflectedDescription.subtitle;
        }

        protected virtual string ErrorSubtitle(Exception exception)
        {
            return null;
        }

        protected virtual string DefinedSummary()
        {
            return reflectedDescription.summary;
        }

        protected virtual string DefaultSummary()
        {
            return reflectedDescription.summary;
        }

        protected virtual string ErrorSummary(Exception exception)
        {
            return $"This node failed to define.\n\n{exception.DisplayName()}: {exception.Message}";
        }

        protected virtual EditorTexture DefinedIcon()
        {
            return unit.GetType().Icon();
        }

        protected virtual EditorTexture DefaultIcon()
        {
            return unit.GetType().Icon();
        }

        protected virtual EditorTexture ErrorIcon(Exception exception)
        {
            return BoltCore.Icons.errorState;
        }

        protected virtual IEnumerable<EditorTexture> DefinedIcons()
        {
            return Enumerable.Empty<EditorTexture>();
        }

        protected virtual IEnumerable<EditorTexture> DefaultIcons()
        {
            return Enumerable.Empty<EditorTexture>();
        }

        protected virtual IEnumerable<EditorTexture> ErrorIcons(Exception exception)
        {
            return Enumerable.Empty<EditorTexture>();
        }

        public void DescribePort(IUnitPort port, UnitPortDescription description)
        {
            description.getMetadata = (unitMetadata) => unitMetadata.StaticObject(port);

            // Only defined nodes can have specific ports
            if (state == State.Defined)
            {
                DefinedPort(port, description);
            }
        }

        protected virtual void DefinedPort(IUnitPort port, UnitPortDescription description)
        {
            var reflectedPortDescription = ReflectedPortDescription(port);

            if (reflectedPortDescription != null)
            {
                description.CopyFrom(reflectedPortDescription);
            }
        }

        #endregion
    }
}