File size: 32,755 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
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using Unity.VisualScripting.Dependencies.Sqlite;
using UnityEditor;
using UnityEngine;

namespace Unity.VisualScripting
{
    [InitializeAfterPlugins]
    public static class UnitBase
    {
        static UnitBase()
        {
            staticUnitsExtensions = new NonNullableList<Func<IEnumerable<IUnitOption>>>();
            dynamicUnitsExtensions = new NonNullableList<Func<IEnumerable<IUnitOption>>>();
            contextualUnitsExtensions = new NonNullableList<Func<GraphReference, IEnumerable<IUnitOption>>>();
            BackgroundWorker.Schedule(AutoLoad);
        }

        private static readonly object @lock = new object();

        private static HashSet<IUnitOption> options;

        private static void AutoLoad()
        {
            lock (@lock)
            {
                // If the fuzzy finder was opened really fast,
                // a load operation might already have started.
                if (options == null)
                {
                    Load();
                }
            }
        }

        private static void Load()
        {
            if (IsUnitOptionsBuilt())
            {
                // Update before loading if required, ensuring no "in-between" state
                // where the loaded options are not yet loaded.
                // The update code will not touch the options array if it is null.
                if (BoltFlow.Configuration.updateNodesAutomatically)
                {
                    try
                    {
                        ProgressUtility.DisplayProgressBar("Checking for codebase changes...", null, 0);

                        if (requiresUpdate)
                        {
                            Update();
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.LogError($"Failed to update node options.\nRetry with '{UnitOptionUtility.GenerateUnitDatabasePath}'.\n{ex}");
                    }
                    finally
                    {
                        ProgressUtility.ClearProgressBar();
                    }
                }

                lock (@lock)
                {
                    using (ProfilingUtility.SampleBlock("Load Node Database"))
                    {
                        using (NativeUtility.Module("sqlite3.dll"))
                        {
                            ProgressUtility.DisplayProgressBar("Loading node database...", null, 0);

                            SQLiteConnection database = null;

                            try
                            {
                                database = new SQLiteConnection(BoltFlow.Paths.unitOptions, SQLiteOpenFlags.ReadOnly);

                                int total;

                                total = database.Table<UnitOptionRow>().Count();

                                var progress = 0f;

                                options = new HashSet<IUnitOption>();

                                var failedOptions = new Dictionary<UnitOptionRow, Exception>();

                                foreach (var row in database.Table<UnitOptionRow>())
                                {
                                    try
                                    {
                                        var option = row.ToOption();

                                        options.Add(option);
                                    }
                                    catch (Exception rowEx)
                                    {
                                        failedOptions.Add(row, rowEx);
                                    }

                                    ProgressUtility.DisplayProgressBar("Loading node database...", BoltCore.Configuration.humanNaming ? row.labelHuman : row.labelProgrammer, progress++ / total);
                                }

                                if (failedOptions.Count > 0)
                                {
                                    var sb = new StringBuilder();

                                    sb.AppendLine($"{failedOptions.Count} node options failed to load and were skipped.");
                                    sb.AppendLine($"Try rebuilding the node options with '{UnitOptionUtility.GenerateUnitDatabasePath}' to purge outdated nodes.");
                                    sb.AppendLine();

                                    foreach (var failedOption in failedOptions)
                                    {
                                        sb.AppendLine(failedOption.Key.favoriteKey);
                                    }

                                    sb.AppendLine();

                                    foreach (var failedOption in failedOptions)
                                    {
                                        sb.AppendLine(failedOption.Key.favoriteKey + ": ");
                                        sb.AppendLine(failedOption.Value.ToString());
                                        sb.AppendLine();
                                    }

                                    Debug.LogWarning(sb.ToString());
                                }
                            }
                            catch (Exception ex)
                            {
                                options = new HashSet<IUnitOption>();
                                Debug.LogError($"Failed to load node options.\nTry to rebuild them with '{UnitOptionUtility.GenerateUnitDatabasePath}'.\n\n{ex}");
                            }
                            finally
                            {
                                database?.Close();
                                //ConsoleProfiler.Dump();
                                ProgressUtility.ClearProgressBar();
                            }
                        }
                    }
                }
            }
        }

        private static bool IsUnitOptionsBuilt()
        {
            return File.Exists(BoltFlow.Paths.unitOptions);
        }

        public static void Rebuild()
        {
            if (IsUnitOptionsBuilt())
            {
                VersionControlUtility.Unlock(BoltFlow.Paths.unitOptions);
                File.Delete(BoltFlow.Paths.unitOptions);
            }

            Build();
        }

        public static void Build(bool initialBuild = false)
        {
            if (IsUnitOptionsBuilt()) return;

            if (initialBuild)
            {
                ProgressUtility.SetTitleOverride("Visual Scripting: Initial Node Generation...");
            }

            const string progressTitle = "Visual Scripting: Building node database...";

            lock (@lock)
            {
                using (ProfilingUtility.SampleBlock("Update Node Database"))
                {
                    using (NativeUtility.Module("sqlite3.dll"))
                    {
                        SQLiteConnection database = null;

                        try
                        {
                            ProgressUtility.DisplayProgressBar(progressTitle, "Creating database...", 0);

                            PathUtility.CreateParentDirectoryIfNeeded(BoltFlow.Paths.unitOptions);
                            database = new SQLiteConnection(BoltFlow.Paths.unitOptions);
                            database.CreateTable<UnitOptionRow>();

                            ProgressUtility.DisplayProgressBar(progressTitle, "Updating codebase...", 0);

                            UpdateCodebase();

                            ProgressUtility.DisplayProgressBar(progressTitle, "Updating type mappings...", 0);

                            UpdateTypeMappings();

                            ProgressUtility.DisplayProgressBar(progressTitle,
                                "Converting codebase to node options...", 0);

                            options = new HashSet<IUnitOption>(GetStaticOptions());

                            var rows = new HashSet<UnitOptionRow>();

                            var progress = 0;
                            var lastShownProgress = 0f;

                            foreach (var option in options)
                            {
                                try
                                {
                                    var shownProgress = (float)progress / options.Count;

                                    if (shownProgress > lastShownProgress + 0.01f)
                                    {
                                        ProgressUtility.DisplayProgressBar(progressTitle,
                                            "Converting codebase to node options...", shownProgress);
                                        lastShownProgress = shownProgress;
                                    }

                                    rows.Add(option.Serialize());
                                }
                                catch (Exception ex)
                                {
                                    Debug.LogError($"Failed to save option '{option.GetType()}'.\n{ex}");
                                }

                                progress++;
                            }

                            ProgressUtility.DisplayProgressBar(progressTitle, "Writing to database...", 1);

                            try
                            {
                                database.CreateTable<UnitOptionRow>();
                                database.InsertAll(rows);
                            }
                            catch (Exception ex)
                            {
                                Debug.LogError($"Failed to write options to database.\n{ex}");
                            }
                        }
                        finally
                        {
                            database?.Close();
                            ProgressUtility.ClearProgressBar();
                            ProgressUtility.ClearTitleOverride();
                            AssetDatabase.Refresh();
                            //ConsoleProfiler.Dump();
                        }
                    }
                }
            }
        }

        public static void Update()
        {
            if (!IsUnitOptionsBuilt())
            {
                Build();
                return;
            }

            lock (@lock)
            {
                using (ProfilingUtility.SampleBlock("Update Node Database"))
                {
                    using (NativeUtility.Module("sqlite3.dll"))
                    {
                        var progressTitle = "Updating node database...";

                        SQLiteConnection database = null;

                        try
                        {
                            VersionControlUtility.Unlock(BoltFlow.Paths.unitOptions);

                            var steps = 7f;
                            var step = 0f;

                            ProgressUtility.DisplayProgressBar(progressTitle, "Connecting to database...", step++ / steps);

                            database = new SQLiteConnection(BoltFlow.Paths.unitOptions);

                            ProgressUtility.DisplayProgressBar(progressTitle, "Updating type mappings...", step++ / steps);

                            UpdateTypeMappings();

                            ProgressUtility.DisplayProgressBar(progressTitle, "Fetching modified scripts...", step++ / steps);

                            var modifiedScriptGuids = GetModifiedScriptGuids().Distinct().ToHashSet();

                            ProgressUtility.DisplayProgressBar(progressTitle, "Fetching deleted scripts...", step++ / steps);

                            var deletedScriptGuids = GetDeletedScriptGuids().Distinct().ToHashSet();

                            ProgressUtility.DisplayProgressBar(progressTitle, "Updating codebase...", step++ / steps);

                            var modifiedScriptTypes = modifiedScriptGuids.SelectMany(GetScriptTypes).ToArray();

                            UpdateCodebase(modifiedScriptTypes);

                            var outdatedScriptGuids = new HashSet<string>();
                            outdatedScriptGuids.UnionWith(modifiedScriptGuids);
                            outdatedScriptGuids.UnionWith(deletedScriptGuids);

                            ProgressUtility.DisplayProgressBar(progressTitle, "Removing outdated node options...", step++ / steps);

                            options?.RemoveWhere(option => outdatedScriptGuids.Overlaps(option.sourceScriptGuids));

                            // We want to use the database level WHERE here for speed,
                            // so we'll run multiple queries, one for each outdated script GUID.

                            foreach (var outdatedScriptGuid in outdatedScriptGuids)
                            {
                                foreach (var outdatedRowId in database.Table<UnitOptionRow>()
                                         .Where(row => row.sourceScriptGuids.Contains(outdatedScriptGuid))
                                         .Select(row => row.id))
                                {
                                    database.Delete<UnitOptionRow>(outdatedRowId);
                                }
                            }

                            ProgressUtility.DisplayProgressBar(progressTitle, "Converting codebase to node options...", step++ / steps);

                            var newOptions = new HashSet<IUnitOption>(modifiedScriptGuids.SelectMany(GetScriptTypes)
                                .Distinct()
                                .SelectMany(GetIncrementalOptions));

                            var rows = new HashSet<UnitOptionRow>();

                            float progress = 0;

                            foreach (var newOption in newOptions)
                            {
                                options?.Add(newOption);

                                try
                                {
                                    ProgressUtility.DisplayProgressBar(progressTitle, newOption.label, (step / steps) + ((1 / step) * (progress / newOptions.Count)));
                                    rows.Add(newOption.Serialize());
                                }
                                catch (Exception ex)
                                {
                                    Debug.LogError($"Failed to serialize option '{newOption.GetType()}'.\n{ex}");
                                }

                                progress++;
                            }

                            ProgressUtility.DisplayProgressBar(progressTitle, "Writing to database...", 1);

                            try
                            {
                                database.InsertAll(rows);
                            }
                            catch (Exception ex)
                            {
                                Debug.LogError($"Failed to write options to database.\n{ex}");
                            }

                            // Make sure the database is touched to the current date,
                            // even if we didn't do any change. This will avoid unnecessary
                            // analysis in future update checks.
                            File.SetLastWriteTimeUtc(BoltFlow.Paths.unitOptions, DateTime.UtcNow);
                        }
                        finally
                        {
                            database?.Close();
                            ProgressUtility.ClearProgressBar();
                            UnityAPI.Async(AssetDatabase.Refresh);
                            //ConsoleProfiler.Dump();
                        }
                    }
                }
            }
        }

        public static IEnumerable<IUnitOption> Subset(UnitOptionFilter filter, GraphReference reference)
        {
            lock (@lock)
            {
                if (options == null)
                {
                    Load();
                }

                var dynamicOptions = UnityAPI.Await(() => GetDynamicOptions().ToHashSet());
                var contextualOptions = UnityAPI.Await(() => GetContextualOptions(reference).ToHashSet());

                return LinqUtility.Concat<IUnitOption>(options, dynamicOptions, contextualOptions)
                    .Where((filter ?? UnitOptionFilter.Any).ValidateOption)
                    .ToArray();
            }
        }

        #region Units

        private static CodebaseSubset codebase;

        private static void UpdateCodebase(IEnumerable<Type> typeSet = null)
        {
            using var profilerScope = ProfilingUtility.SampleBlock("UpdateCodebase");
            if (typeSet == null)
            {
                typeSet = Codebase.settingsTypes;
            }
            else
            {
                typeSet = typeSet.Where(t => Codebase.settingsTypes.Contains(t));
            }

            Codebase.UpdateSettings();
            codebase = Codebase.Subset(typeSet, TypeFilter.Any.Configured(), MemberFilter.Any.Configured(), TypeFilter.Any.Configured(false));
            codebase.Cache();
        }

        private static IEnumerable<IUnitOption> GetStaticOptions()
        {
            // Standalones

            foreach (var unit in Codebase.ludiqRuntimeTypes.Where(t => typeof(IUnit).IsAssignableFrom(t) &&
                t.IsConcrete() &&
                t.GetDefaultConstructor() != null &&
                !t.HasAttribute<SpecialUnitAttribute>() &&
                (EditorPlatformUtility.allowJit || !t.HasAttribute<AotIncompatibleAttribute>()) &&
                t.GetDefaultConstructor() != null)
                     .Select(t => (IUnit)t.Instantiate()))
            {
                yield return unit.Option();
            }

            // Self

            yield return new This().Option();

            // Types

            foreach (var type in codebase.types)
            {
                foreach (var typeOption in GetTypeOptions(type))
                {
                    yield return typeOption;
                }
            }

            // Members

            foreach (var member in codebase.members)
            {
                foreach (var memberOption in GetMemberOptions(member))
                {
                    yield return memberOption;
                }
            }

            // Events

            foreach (var eventType in Codebase.ludiqRuntimeTypes.Where(t => typeof(IEventUnit).IsAssignableFrom(t) && t.IsConcrete()))
            {
                yield return ((IEventUnit)eventType.Instantiate()).Option();
            }

            // Blank Variables

            yield return new GetVariableOption(VariableKind.Flow);
            yield return new GetVariableOption(VariableKind.Graph);
            yield return new GetVariableOption(VariableKind.Object);
            yield return new GetVariableOption(VariableKind.Scene);
            yield return new GetVariableOption(VariableKind.Application);
            yield return new GetVariableOption(VariableKind.Saved);

            yield return new SetVariableOption(VariableKind.Flow);
            yield return new SetVariableOption(VariableKind.Graph);
            yield return new SetVariableOption(VariableKind.Object);
            yield return new SetVariableOption(VariableKind.Scene);
            yield return new SetVariableOption(VariableKind.Application);
            yield return new SetVariableOption(VariableKind.Saved);

            yield return new IsVariableDefinedOption(VariableKind.Flow);
            yield return new IsVariableDefinedOption(VariableKind.Graph);
            yield return new IsVariableDefinedOption(VariableKind.Object);
            yield return new IsVariableDefinedOption(VariableKind.Scene);
            yield return new IsVariableDefinedOption(VariableKind.Application);
            yield return new IsVariableDefinedOption(VariableKind.Saved);

            // Blank Super Unit

            yield return SubgraphUnit.WithInputOutput().Option();

            // Extensions

            foreach (var staticUnitsExtension in staticUnitsExtensions)
            {
                foreach (var extensionStaticUnit in staticUnitsExtension())
                {
                    yield return extensionStaticUnit;
                }
            }
        }

        private static IEnumerable<IUnitOption> GetIncrementalOptions(Type type)
        {
            if (!codebase.ValidateType(type))
            {
                yield break;
            }

            foreach (var typeOption in GetTypeOptions(type))
            {
                yield return typeOption;
            }

            foreach (var member in codebase.FilterMembers(type))
            {
                foreach (var memberOption in GetMemberOptions(member))
                {
                    yield return memberOption;
                }
            }
        }

        private static IEnumerable<IUnitOption> GetTypeOptions(Type type)
        {
            if (type == typeof(object))
            {
                yield break;
            }

            // Struct Initializer

            if (type.IsStruct())
            {
                yield return new CreateStruct(type).Option();
            }

            // Literals

            if (type.HasInspector())
            {
                yield return new Literal(type).Option();

                if (EditorPlatformUtility.allowJit)
                {
                    var listType = typeof(List<>).MakeGenericType(type);

                    yield return new Literal(listType).Option();
                }
            }

            // Exposes

            if (!type.IsEnum)
            {
                yield return new Expose(type).Option();
            }
        }

        private static IEnumerable<IUnitOption> GetMemberOptions(Member member)
        {
            // Operators are handled with special math units
            // that are more elegant than the raw methods
            if (member.isOperator)
            {
                yield break;
            }

            // Conversions are handled automatically by connections
            if (member.isConversion)
            {
                yield break;
            }

            if (member.isAccessor)
            {
                if (member.isPubliclyGettable)
                {
                    yield return new GetMember(member).Option();
                }

                if (member.isPubliclySettable)
                {
                    yield return new SetMember(member).Option();
                }
            }
            else if (member.isPubliclyInvocable)
            {
                yield return new InvokeMember(member).Option();
            }
        }

        private static IEnumerable<IUnitOption> GetDynamicOptions()
        {
            // Super Units

            var flowMacros = AssetUtility.GetAllAssetsOfType<ScriptGraphAsset>().ToArray();

            foreach (var superUnit in flowMacros.Select(flowMacro => new SubgraphUnit(flowMacro)))
            {
                yield return superUnit.Option();
            }

            // Extensions

            foreach (var dynamicUnitsExtension in dynamicUnitsExtensions)
            {
                foreach (var extensionDynamicUnit in dynamicUnitsExtension())
                {
                    yield return extensionDynamicUnit;
                }
            }
        }

        private static IEnumerable<IUnitOption> GetContextualOptions(GraphReference reference)
        {
            foreach (var variableKind in Enum.GetValues(typeof(VariableKind)).Cast<VariableKind>())
            {
                foreach (var graphVariableName in EditorVariablesUtility.GetVariableNameSuggestions(variableKind, reference))
                {
                    yield return new GetVariableOption(variableKind, graphVariableName);
                    yield return new SetVariableOption(variableKind, graphVariableName);
                    yield return new IsVariableDefinedOption(variableKind, graphVariableName);
                }
            }

            // Extensions

            foreach (var contextualUnitsExtension in contextualUnitsExtensions)
            {
                foreach (var extensionContextualUnitOption in contextualUnitsExtension(reference))
                {
                    yield return extensionContextualUnitOption;
                }
            }
        }

        #endregion

        #region Scripts

        private static Dictionary<Type, HashSet<string>> typesToGuids;
        private static Dictionary<string, HashSet<Type>> guidsToTypes;

        private static void UpdateTypeMappings()
        {
            using var profilerScope = ProfilingUtility.SampleBlock("UpdateTypeMappings");

            typesToGuids = new Dictionary<Type, HashSet<string>>();
            guidsToTypes = new Dictionary<string, HashSet<Type>>();

            UnityAPI.AwaitForever(() =>
            {
                foreach (var script in UnityEngine.Resources.FindObjectsOfTypeAll<MonoScript>())
                {
                    var type = script.GetClass();

                    // Skip scripts without types
                    if (type == null)
                    {
                        continue;
                    }

                    var path = AssetDatabase.GetAssetPath(script);
                    // Skip built-in Unity plugins, which are referenced by full path
                    if (!path.StartsWith("Assets"))
                    {
                        continue;
                    }

                    var guid = AssetDatabase.AssetPathToGUID(path);
                    // Add the GUID to the list, even if it doesn't have any type
                    if (!guidsToTypes.ContainsKey(guid))
                    {
                        guidsToTypes.Add(guid, new HashSet<Type>());
                    }

                    if (!typesToGuids.ContainsKey(type))
                    {
                        typesToGuids.Add(type, new HashSet<string>());
                    }

                    typesToGuids[type].Add(guid);
                    guidsToTypes[guid].Add(type);
                }
            });
        }

        public static IEnumerable<string> GetScriptGuids(Type type)
        {
            if (typesToGuids == null)
            {
                UpdateTypeMappings();
            }

            using (var recursion = Recursion.New(1))
            {
                return GetScriptGuids(recursion, type).ToArray(); // No delayed execution for recursion disposal
            }
        }

        private static IEnumerable<string> GetScriptGuids(Recursion recursion, Type type)
        {
            if (!recursion?.TryEnter(type) ?? false)
            {
                yield break;
            }

            if (typesToGuids.ContainsKey(type))
            {
                foreach (var guid in typesToGuids[type])
                {
                    yield return guid;
                }
            }

            // Loop through generic arguments.
            // For example, a List<Enemy> type should return the script GUID for Enemy.
            if (type.IsGenericType)
            {
                foreach (var genericArgument in type.GetGenericArguments())
                {
                    foreach (var genericGuid in GetScriptGuids(recursion, genericArgument))
                    {
                        yield return genericGuid;
                    }
                }
            }
        }

        public static IEnumerable<Type> GetScriptTypes(string guid)
        {
            if (guidsToTypes == null)
            {
                UpdateTypeMappings();
            }

            if (guidsToTypes.ContainsKey(guid))
            {
                return guidsToTypes[guid];
            }
            else
            {
                return Enumerable.Empty<Type>();
            }
        }

        private static bool requiresUpdate => GetModifiedScriptGuids().Any() || GetDeletedScriptGuids().Any();

        private static IEnumerable<string> GetModifiedScriptGuids()
        {
            var guids = new HashSet<string>();

            UnityAPI.AwaitForever(() =>
            {
                var databaseTimestamp = File.GetLastWriteTimeUtc(BoltFlow.Paths.unitOptions);

                foreach (var script in UnityEngine.Resources.FindObjectsOfTypeAll<MonoScript>())
                {
                    var path = AssetDatabase.GetAssetPath(script);
                    var guid = AssetDatabase.AssetPathToGUID(path);

                    // Skip built-in Unity plugins, which are referenced by full path
                    if (!path.StartsWith("Assets"))
                    {
                        continue;
                    }

                    var scriptTimestamp = File.GetLastWriteTimeUtc(Path.Combine(Paths.project, path));

                    if (scriptTimestamp > databaseTimestamp)
                    {
                        guids.Add(guid);
                    }
                }
            });

            return guids;
        }

        private static IEnumerable<string> GetDeletedScriptGuids()
        {
            if (!IsUnitOptionsBuilt())
            {
                return Enumerable.Empty<string>();
            }

            using (NativeUtility.Module("sqlite3.dll"))
            {
                SQLiteConnection database = null;

                try
                {
                    HashSet<string> databaseGuids;

                    lock (@lock)
                    {
                        database = new SQLiteConnection(BoltFlow.Paths.unitOptions);

                        databaseGuids = database.Query<UnitOptionRow>($"SELECT DISTINCT {nameof(UnitOptionRow.sourceScriptGuids)} FROM {nameof(UnitOptionRow)}")
                            .Select(row => row.sourceScriptGuids)
                            .NotNull()
                            .SelectMany(guids => guids.Split(','))
                            .ToHashSet();
                    }

                    var assetGuids = UnityAPI.AwaitForever(() => UnityEngine.Resources
                        .FindObjectsOfTypeAll<MonoScript>()
                        .Where(script => script.GetClass() != null)
                        .Select(script => AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(script)))
                        .ToHashSet());

                    databaseGuids.ExceptWith(assetGuids);

                    return databaseGuids;
                }
                finally
                {
                    database?.Close();
                }
            }
        }

        #endregion

        #region Extensions

        public static NonNullableList<Func<IEnumerable<IUnitOption>>> staticUnitsExtensions { get; }

        public static NonNullableList<Func<IEnumerable<IUnitOption>>> dynamicUnitsExtensions { get; }

        public static NonNullableList<Func<GraphReference, IEnumerable<IUnitOption>>> contextualUnitsExtensions { get; }

        #endregion

        #region Duplicates

        public static IEnumerable<T> WithoutInheritedDuplicates<T>(this IEnumerable<T> items, Func<T, IUnitOption> optionSelector, CancellationToken cancellation)
        {
            // Doing everything we can to avoid reflection here, as it then becomes the main search bottleneck

            var _items = items.ToArray();

            var pseudoDeclarers = new HashSet<Member>();

            foreach (var item in _items.Cancellable(cancellation))
            {
                var option = optionSelector(item);

                if (option is IMemberUnitOption memberOption)
                {
                    if (memberOption.targetType == memberOption.pseudoDeclarer.targetType)
                    {
                        pseudoDeclarers.Add(memberOption.pseudoDeclarer);
                    }
                }
            }

            foreach (var item in _items.Cancellable(cancellation))
            {
                var option = optionSelector(item);

                if (option is IMemberUnitOption memberOption)
                {
                    if (pseudoDeclarers.Contains(memberOption.member) || !pseudoDeclarers.Contains(memberOption.pseudoDeclarer))
                    {
                        yield return item;
                    }
                }
                else
                {
                    yield return item;
                }
            }
        }

        #endregion
    }
}