using System; using System.Collections.Generic; using System.Reflection; using UnityEngine; using UnityEditor; namespace Unity.GraphAuthoring.Editor.ProviderSystem { internal class ProviderModelAttribute : Attribute { public readonly string ProviderKey; public ProviderModelAttribute(string providerKey) { ProviderKey = providerKey; } } internal class ScriptedProviderAttribute : Attribute { } internal static class ProviderTypeCache { static Dictionary s_providerModels = null; static List s_scriptedProviders = null; private static void BuildCache() { if (s_providerModels == null && s_scriptedProviders == null) { s_providerModels = new(); s_scriptedProviders = new(); foreach(var type in TypeCache.GetTypesWithAttribute()) { var providerKey = type.GetCustomAttribute()?.ProviderKey ?? null; if (providerKey != null) if (!s_providerModels.TryAdd(providerKey, type)) Debug.LogError($"Attempted to register '{type.Name}' with provider key '{providerKey}', but key is already registered to '{s_providerModels[providerKey]}'."); } foreach (var type in TypeCache.GetTypesWithAttribute()) s_scriptedProviders.Add(type); } } internal static IEnumerable GetScriptedProviderTypes() { BuildCache(); return s_scriptedProviders; } internal static bool TryCreateModel(string key, out object model) { BuildCache(); if (s_providerModels.TryGetValue(key, out Type type)) { model = Activator.CreateInstance(type, true); return true; } model = null; return false; } internal static void PopulateLibrary(ProviderLibrary lib) { foreach (var type in GetScriptedProviderTypes()) if (Activator.CreateInstance(type, true) is IProvider provider) lib.TryAdd(provider); } } }