Singularity/Library/PackageCache/com.unity.shadergraph@12.1.11/Editor/Generation/Collections/IncludeCollection.cs

91 lines
3.1 KiB
C#
Raw Normal View History

2024-05-06 14:45:45 -04:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[GenerationAPI]
[Serializable]
internal class IncludeCollection : IEnumerable<IncludeDescriptor>
{
[SerializeField]
List<IncludeDescriptor> includes;
public IncludeCollection()
{
includes = new List<IncludeDescriptor>();
}
public IncludeCollection Add(IncludeCollection includes)
{
if (includes != null)
{
foreach (var include in includes)
{
AddInternal(include.guid, include.path, include.location, include.fieldConditions);
}
}
return this;
}
public IncludeCollection Add(string includePath, IncludeLocation location)
{
var guid = AssetDatabase.AssetPathToGUID(includePath);
return AddInternal(guid, includePath, location);
}
public IncludeCollection Add(string includePath, IncludeLocation location, FieldCondition fieldCondition)
{
var guid = AssetDatabase.AssetPathToGUID(includePath);
return AddInternal(guid, includePath, location, new FieldCondition[] { fieldCondition });
}
public IncludeCollection Add(string includePath, IncludeLocation location, FieldCondition[] fieldConditions)
{
var guid = AssetDatabase.AssetPathToGUID(includePath);
return AddInternal(guid, includePath, location, fieldConditions);
}
public IncludeCollection AddInternal(string includeGUID, string includePath, IncludeLocation location, FieldCondition[] fieldConditions = null)
{
if (string.IsNullOrEmpty(includeGUID))
{
// either the file doesn't exist, or this is a placeholder
// de-duplicate by path
int existing = includes.FindIndex(i => i.path == includePath);
if (existing < 0)
includes.Add(new IncludeDescriptor(null, includePath, location, fieldConditions));
return this;
}
else
{
// de-duplicate by GUID
int existing = includes.FindIndex(i => i.guid == includeGUID);
if (existing < 0)
{
// no duplicates, just add it
includes.Add(new IncludeDescriptor(includeGUID, includePath, location, fieldConditions));
}
else
{
// duplicate file -- we could double check they are the same...
// they might have different field conditions that require merging, for example.
// But for now we'll just assume they are the same and drop the new one
}
}
return this;
}
public IEnumerator<IncludeDescriptor> GetEnumerator()
{
return includes.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}