No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UnitDescriptor.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using UnityEngine;
  6. using UnityObject = UnityEngine.Object;
  7. namespace Unity.VisualScripting
  8. {
  9. [Descriptor(typeof(IUnit))]
  10. public class UnitDescriptor<TUnit> : Descriptor<TUnit, UnitDescription>, IUnitDescriptor
  11. where TUnit : class, IUnit
  12. {
  13. public UnitDescriptor(TUnit target) : base(target)
  14. {
  15. unitType = unit.GetType();
  16. }
  17. protected Type unitType { get; }
  18. public TUnit unit => target;
  19. IUnit IUnitDescriptor.unit => unit;
  20. private enum State
  21. {
  22. Defined,
  23. NotDefined,
  24. FailedToDefine
  25. }
  26. private State state
  27. {
  28. get
  29. {
  30. if (unit.isDefined)
  31. {
  32. return State.Defined;
  33. }
  34. else if (unit.failedToDefine)
  35. {
  36. return State.FailedToDefine;
  37. }
  38. else
  39. {
  40. return State.NotDefined;
  41. }
  42. }
  43. }
  44. #region Reflected Description
  45. static UnitDescriptor()
  46. {
  47. XmlDocumentation.loadComplete += FreeReflectedDescriptions;
  48. }
  49. public static void FreeReflectedDescriptions()
  50. {
  51. reflectedDescriptions.Clear();
  52. reflectedInputDescriptions.Clear();
  53. reflectedOutputDescriptions.Clear();
  54. }
  55. protected UnitDescription reflectedDescription
  56. {
  57. get
  58. {
  59. if (!reflectedDescriptions.TryGetValue(unitType, out var reflectedDescription))
  60. {
  61. reflectedDescription = FetchReflectedDescription(unitType);
  62. reflectedDescriptions.Add(unitType, reflectedDescription);
  63. }
  64. return reflectedDescription;
  65. }
  66. }
  67. protected UnitPortDescription ReflectedPortDescription(IUnitPort port)
  68. {
  69. if (port is IUnitInvalidPort)
  70. {
  71. return null;
  72. }
  73. if (port is IUnitInputPort)
  74. {
  75. if (!reflectedInputDescriptions.TryGetValue(unitType, out var _reflectedInputDescriptions))
  76. {
  77. _reflectedInputDescriptions = FetchReflectedPortDescriptions<IUnitInputPort>(unitType);
  78. reflectedInputDescriptions.Add(unitType, _reflectedInputDescriptions);
  79. }
  80. if (_reflectedInputDescriptions.TryGetValue(port.key, out var portDescription))
  81. {
  82. return portDescription;
  83. }
  84. }
  85. else if (port is IUnitOutputPort)
  86. {
  87. if (!reflectedOutputDescriptions.TryGetValue(unitType, out var _reflectedOutputDescriptions))
  88. {
  89. _reflectedOutputDescriptions = FetchReflectedPortDescriptions<IUnitOutputPort>(unitType);
  90. reflectedOutputDescriptions.Add(unitType, _reflectedOutputDescriptions);
  91. }
  92. if (_reflectedOutputDescriptions.TryGetValue(port.key, out var portDescription))
  93. {
  94. return portDescription;
  95. }
  96. }
  97. return null;
  98. }
  99. private static readonly Dictionary<Type, UnitDescription> reflectedDescriptions = new Dictionary<Type, UnitDescription>();
  100. private static readonly Dictionary<Type, Dictionary<string, UnitPortDescription>> reflectedInputDescriptions = new Dictionary<Type, Dictionary<string, UnitPortDescription>>();
  101. private static readonly Dictionary<Type, Dictionary<string, UnitPortDescription>> reflectedOutputDescriptions = new Dictionary<Type, Dictionary<string, UnitPortDescription>>();
  102. private static UnitDescription FetchReflectedDescription(Type unitType)
  103. {
  104. var oldName = BoltFlowNameUtility.UnitPreviousTitle(unitType);
  105. var prefix = string.IsNullOrEmpty(oldName) ? string.Empty : $"(Previously named {oldName}) ";
  106. return new UnitDescription()
  107. {
  108. title = BoltFlowNameUtility.UnitTitle(unitType, false, true),
  109. shortTitle = BoltFlowNameUtility.UnitTitle(unitType, true, true),
  110. surtitle = unitType.GetAttribute<UnitSurtitleAttribute>()?.surtitle,
  111. subtitle = unitType.GetAttribute<UnitSubtitleAttribute>()?.subtitle,
  112. summary = prefix + unitType.Summary()
  113. };
  114. }
  115. private static Dictionary<string, UnitPortDescription> FetchReflectedPortDescriptions<T>(Type unitType) where T : IUnitPort
  116. {
  117. var descriptions = new Dictionary<string, UnitPortDescription>();
  118. foreach (var portMember in unitType.GetMembers().Where(member => typeof(T).IsAssignableFrom(member.GetAccessorType())))
  119. {
  120. var key = portMember.GetAttribute<PortKeyAttribute>()?.key ?? portMember.Name;
  121. if (descriptions.ContainsKey(key))
  122. {
  123. Debug.LogWarning("Duplicate reflected port description for: " + key);
  124. continue;
  125. }
  126. descriptions.Add(key, FetchReflectedPortDescription(portMember));
  127. }
  128. return descriptions;
  129. }
  130. private static UnitPortDescription FetchReflectedPortDescription(MemberInfo portMember)
  131. {
  132. return new UnitPortDescription()
  133. {
  134. label = portMember.GetAttribute<PortLabelAttribute>()?.label ?? portMember.HumanName(),
  135. showLabel = !(portMember.HasAttribute<PortLabelHiddenAttribute>() || (portMember.GetAttribute<PortLabelAttribute>()?.hidden ?? false)),
  136. summary = portMember.Summary(),
  137. getMetadata = (unitMetadata) => unitMetadata[portMember.Name]
  138. };
  139. }
  140. #endregion
  141. #region Description
  142. [Assigns]
  143. public sealed override string Title()
  144. {
  145. switch (state)
  146. {
  147. case State.Defined: return DefinedTitle();
  148. case State.NotDefined: return DefaultTitle();
  149. case State.FailedToDefine: return ErrorTitle(unit.definitionException);
  150. default: throw new UnexpectedEnumValueException<State>(state);
  151. }
  152. }
  153. [Assigns]
  154. public string ShortTitle()
  155. {
  156. switch (state)
  157. {
  158. case State.Defined: return DefinedShortTitle();
  159. case State.NotDefined: return DefaultShortTitle();
  160. case State.FailedToDefine: return ErrorShortTitle(unit.definitionException);
  161. default: throw new UnexpectedEnumValueException<State>(state);
  162. }
  163. }
  164. [Assigns]
  165. public string Surtitle()
  166. {
  167. switch (state)
  168. {
  169. case State.Defined: return DefinedSurtitle();
  170. case State.NotDefined: return DefaultSurtitle();
  171. case State.FailedToDefine: return ErrorSurtitle(unit.definitionException);
  172. default: throw new UnexpectedEnumValueException<State>(state);
  173. }
  174. }
  175. [Assigns]
  176. public string Subtitle()
  177. {
  178. switch (state)
  179. {
  180. case State.Defined: return DefinedSubtitle();
  181. case State.NotDefined: return DefaultSubtitle();
  182. case State.FailedToDefine: return ErrorSubtitle(unit.definitionException);
  183. default: throw new UnexpectedEnumValueException<State>(state);
  184. }
  185. }
  186. [Assigns]
  187. public sealed override string Summary()
  188. {
  189. switch (state)
  190. {
  191. case State.Defined: return DefinedSummary();
  192. case State.NotDefined: return DefaultSummary();
  193. case State.FailedToDefine: return ErrorSummary(unit.definitionException);
  194. default: throw new UnexpectedEnumValueException<State>(state);
  195. }
  196. }
  197. [Assigns]
  198. [RequiresUnityAPI]
  199. public sealed override EditorTexture Icon()
  200. {
  201. switch (state)
  202. {
  203. case State.Defined: return DefinedIcon();
  204. case State.NotDefined: return DefaultIcon();
  205. case State.FailedToDefine: return ErrorIcon(unit.definitionException);
  206. default: throw new UnexpectedEnumValueException<State>(state);
  207. }
  208. }
  209. [Assigns]
  210. [RequiresUnityAPI]
  211. public IEnumerable<EditorTexture> Icons()
  212. {
  213. switch (state)
  214. {
  215. case State.Defined: return DefinedIcons();
  216. case State.NotDefined: return DefaultIcons();
  217. case State.FailedToDefine: return ErrorIcons(unit.definitionException);
  218. default: throw new UnexpectedEnumValueException<State>(state);
  219. }
  220. }
  221. protected virtual string DefinedTitle()
  222. {
  223. return reflectedDescription.title;
  224. }
  225. protected virtual string DefaultTitle()
  226. {
  227. return reflectedDescription.title;
  228. }
  229. protected virtual string ErrorTitle(Exception exception)
  230. {
  231. return reflectedDescription.title;
  232. }
  233. protected virtual string DefinedShortTitle()
  234. {
  235. return reflectedDescription.shortTitle;
  236. }
  237. protected virtual string DefaultShortTitle()
  238. {
  239. return reflectedDescription.shortTitle;
  240. }
  241. protected virtual string ErrorShortTitle(Exception exception)
  242. {
  243. return ErrorTitle(exception);
  244. }
  245. protected virtual string DefinedSurtitle()
  246. {
  247. return reflectedDescription.surtitle;
  248. }
  249. protected virtual string DefaultSurtitle()
  250. {
  251. return reflectedDescription.surtitle;
  252. }
  253. protected virtual string ErrorSurtitle(Exception exception)
  254. {
  255. return null;
  256. }
  257. protected virtual string DefinedSubtitle()
  258. {
  259. return reflectedDescription.subtitle;
  260. }
  261. protected virtual string DefaultSubtitle()
  262. {
  263. return reflectedDescription.subtitle;
  264. }
  265. protected virtual string ErrorSubtitle(Exception exception)
  266. {
  267. return null;
  268. }
  269. protected virtual string DefinedSummary()
  270. {
  271. return reflectedDescription.summary;
  272. }
  273. protected virtual string DefaultSummary()
  274. {
  275. return reflectedDescription.summary;
  276. }
  277. protected virtual string ErrorSummary(Exception exception)
  278. {
  279. return $"This node failed to define.\n\n{exception.DisplayName()}: {exception.Message}";
  280. }
  281. protected virtual EditorTexture DefinedIcon()
  282. {
  283. return unit.GetType().Icon();
  284. }
  285. protected virtual EditorTexture DefaultIcon()
  286. {
  287. return unit.GetType().Icon();
  288. }
  289. protected virtual EditorTexture ErrorIcon(Exception exception)
  290. {
  291. return BoltCore.Icons.errorState;
  292. }
  293. protected virtual IEnumerable<EditorTexture> DefinedIcons()
  294. {
  295. return Enumerable.Empty<EditorTexture>();
  296. }
  297. protected virtual IEnumerable<EditorTexture> DefaultIcons()
  298. {
  299. return Enumerable.Empty<EditorTexture>();
  300. }
  301. protected virtual IEnumerable<EditorTexture> ErrorIcons(Exception exception)
  302. {
  303. return Enumerable.Empty<EditorTexture>();
  304. }
  305. public void DescribePort(IUnitPort port, UnitPortDescription description)
  306. {
  307. description.getMetadata = (unitMetadata) => unitMetadata.StaticObject(port);
  308. // Only defined nodes can have specific ports
  309. if (state == State.Defined)
  310. {
  311. DefinedPort(port, description);
  312. }
  313. }
  314. protected virtual void DefinedPort(IUnitPort port, UnitPortDescription description)
  315. {
  316. var reflectedPortDescription = ReflectedPortDescription(port);
  317. if (reflectedPortDescription != null)
  318. {
  319. description.CopyFrom(reflectedPortDescription);
  320. }
  321. }
  322. #endregion
  323. }
  324. }