A game about forced loneliness, made by TACStudios
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using UnityEngine;
5
6namespace UnityEditor.ShaderGraph
7{
8 [GenerationAPI]
9 [Serializable]
10 internal class IncludeCollection : IEnumerable<IncludeDescriptor>
11 {
12 [SerializeField]
13 List<IncludeDescriptor> includes;
14
15 public IncludeCollection()
16 {
17 includes = new List<IncludeDescriptor>();
18 }
19
20 public IncludeCollection Add(IncludeCollection includes)
21 {
22 if (includes != null)
23 {
24 foreach (var include in includes)
25 {
26 AddInternal(include.guid, include.path, include.location, include.fieldConditions, include.shouldIncludeWithPragmas);
27 }
28 }
29 return this;
30 }
31
32 public IncludeCollection Add(string includePath, IncludeLocation location)
33 {
34 var guid = AssetDatabase.AssetPathToGUID(includePath);
35 return AddInternal(guid, includePath, location);
36 }
37
38 public IncludeCollection Add(string includePath, IncludeLocation location, bool shouldIncludeWithPragmas)
39 {
40 var guid = AssetDatabase.AssetPathToGUID(includePath);
41 return AddInternal(guid, includePath, location, null, shouldIncludeWithPragmas);
42 }
43
44 public IncludeCollection Add(string includePath, IncludeLocation location, FieldCondition fieldCondition)
45 {
46 var guid = AssetDatabase.AssetPathToGUID(includePath);
47 return AddInternal(guid, includePath, location, new FieldCondition[] { fieldCondition });
48 }
49
50 public IncludeCollection Add(string includePath, IncludeLocation location, FieldCondition fieldCondition, bool shouldIncludeWithPragmas)
51 {
52 var guid = AssetDatabase.AssetPathToGUID(includePath);
53 return AddInternal(guid, includePath, location, new FieldCondition[] { fieldCondition }, shouldIncludeWithPragmas);
54 }
55
56 public IncludeCollection Add(string includePath, IncludeLocation location, FieldCondition[] fieldConditions)
57 {
58 var guid = AssetDatabase.AssetPathToGUID(includePath);
59 return AddInternal(guid, includePath, location, fieldConditions);
60 }
61
62 public IncludeCollection Add(string includePath, IncludeLocation location, FieldCondition[] fieldConditions, bool shouldIncludeWithPragmas)
63 {
64 var guid = AssetDatabase.AssetPathToGUID(includePath);
65 return AddInternal(guid, includePath, location, fieldConditions, shouldIncludeWithPragmas);
66 }
67
68 public IncludeCollection AddInternal(string includeGUID, string includePath, IncludeLocation location, FieldCondition[] fieldConditions = null, bool shouldIncludeWithPragmas = false)
69 {
70 if (string.IsNullOrEmpty(includeGUID))
71 {
72 // either the file doesn't exist, or this is a placeholder
73 // de-duplicate by path
74 int existing = includes.FindIndex(i => i.path == includePath);
75 if (existing < 0)
76 includes.Add(new IncludeDescriptor(null, includePath, location, fieldConditions, shouldIncludeWithPragmas));
77 return this;
78 }
79 else
80 {
81 // de-duplicate by GUID
82 int existing = includes.FindIndex(i => i.guid == includeGUID);
83 if (existing < 0)
84 {
85 // no duplicates, just add it
86 includes.Add(new IncludeDescriptor(includeGUID, includePath, location, fieldConditions, shouldIncludeWithPragmas));
87 }
88 else
89 {
90 // duplicate file -- we could double check they are the same...
91 // they might have different field conditions that require merging, for example.
92 // But for now we'll just assume they are the same and drop the new one
93 }
94 }
95 return this;
96 }
97
98 public IEnumerator<IncludeDescriptor> GetEnumerator()
99 {
100 return includes.GetEnumerator();
101 }
102
103 IEnumerator IEnumerable.GetEnumerator()
104 {
105 return GetEnumerator();
106 }
107 }
108}