using System; using System.Runtime.CompilerServices; using System.Diagnostics; using System.Linq; using System.Reflection; #if UNITY_EDITOR using PackageInfo = UnityEditor.PackageManager.PackageInfo; #endif [assembly: InternalsVisibleTo("Unity.RenderPipelines.Core.Editor.Tests")] namespace UnityEngine.Rendering { /// /// Attribute to define the help url /// /// /// [CoreRPHelpURLAttribute("Volume")] /// public class Volume : MonoBehaviour /// [Conditional("UNITY_EDITOR")] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Enum, AllowMultiple = false)] public class CoreRPHelpURLAttribute : HelpURLAttribute { /// /// The constructor of the attribute /// /// /// public CoreRPHelpURLAttribute(string pageName, string packageName = "com.unity.render-pipelines.core") : base(DocumentationInfo.GetPageLink(packageName, pageName, "")) { } /// /// The constructor of the attribute /// /// /// /// public CoreRPHelpURLAttribute(string pageName, string pageHash, string packageName = "com.unity.render-pipelines.core") : base(DocumentationInfo.GetPageLink(packageName, pageName, pageHash)) { } } //We need to have only one version number amongst packages (so public) /// /// Documentation Info class. /// public class DocumentationInfo { const string fallbackVersion = "13.1"; const string url = "https://docs.unity3d.com/Packages/{0}@{1}/manual/{2}.html{3}"; /// /// Current version of the documentation. /// public static string version { get { #if UNITY_EDITOR var packageInfo = PackageInfo.FindForAssembly(typeof(DocumentationInfo).Assembly); return packageInfo == null ? fallbackVersion : packageInfo.version.Substring(0, 4); #else return fallbackVersion; #endif } } /// /// Generates a help url for the given package and page name /// /// The package name /// The page name /// The full url page public static string GetPageLink(string packageName, string pageName) => string.Format(url, packageName, version, pageName, ""); /// /// Generates a help url for the given package and page name /// /// The package name /// The page name /// The page hash /// The full url page public static string GetPageLink(string packageName, string pageName, string pageHash) => string.Format(url, packageName, version, pageName, pageHash); } /// /// Set of utils for documentation /// public static class DocumentationUtils { /// /// Obtains the help url from an enum /// /// The enum with a /// [Optional] The current value of the enum /// The full url public static string GetHelpURL(TEnum mask = default) where TEnum : struct, IConvertible { var helpURLAttribute = (HelpURLAttribute)mask .GetType() .GetCustomAttributes(typeof(HelpURLAttribute), false) .FirstOrDefault(); return helpURLAttribute == null ? string.Empty : $"{helpURLAttribute.URL}#{mask}"; } /// /// Obtains the help URL from a type. /// /// The type decorated with the HelpURL attribute. /// The full URL from the HelpURL attribute. If the attribute is not present, this value is null. /// Returns true if the attribute is present, and false otherwise. public static bool TryGetHelpURL(Type type, out string url) { var attribute = type.GetCustomAttribute(false); url = attribute?.URL; return attribute != null; } } }