using System; using System.Collections.Generic; // ReSharper disable MemberCanBePrivate.Global namespace NodePipeline.Engine.Abstractions; /// /// Node name settings. /// public static class NodeNamePrefixSettings { private static readonly Dictionary NodeNameCache = new(); private static readonly HashSet PrefixesCache = new(); /// /// List of node name prefix to trim /// public static IReadOnlyCollection Prefixes => PrefixesCache; /// /// Adds prefix to list /// /// public static void SetNodePrefix(string prefix) { if (string.IsNullOrWhiteSpace(prefix)) return; PrefixesCache.Add(string.Concat(TrimPrefix(prefix, "global::").TrimEnd('.'), ".")); } /// /// Adds list of node prefixes /// /// public static void SetNodePrefixes(IEnumerable prefixes) { foreach (var prefix in prefixes) SetNodePrefix(prefix); } /// /// Return node type without specified prefix /// public static string TrimNodeType(string nodeType) { if (string.IsNullOrEmpty(nodeType)) return nodeType; if (NodeNameCache.TryGetValue(nodeType, out var name)) return name; foreach (var prefix in PrefixesCache) { if (!nodeType.StartsWith(prefix, StringComparison.Ordinal)) continue; var result = nodeType.Substring(prefix.Length); NodeNameCache[nodeType] = result; return result; } NodeNameCache[nodeType] = nodeType; return nodeType; } private static string TrimPrefix(string name, string prefix) { if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(prefix) || name.Length < prefix.Length) return name; return name.StartsWith(prefix, StringComparison.Ordinal) ? name.Substring(prefix.Length, name.Length - prefix.Length) : name; } }