ScriptPropertiesGenerator.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Text;
  4. using Microsoft.CodeAnalysis;
  5. using Microsoft.CodeAnalysis.CSharp.Syntax;
  6. using Microsoft.CodeAnalysis.Text;
  7. namespace Godot.SourceGenerators
  8. {
  9. [Generator]
  10. public class ScriptPropertiesGenerator : ISourceGenerator
  11. {
  12. public void Initialize(GeneratorInitializationContext context)
  13. {
  14. }
  15. public void Execute(GeneratorExecutionContext context)
  16. {
  17. if (context.IsGodotSourceGeneratorDisabled("ScriptProperties"))
  18. return;
  19. INamedTypeSymbol[] godotClasses = context
  20. .Compilation.SyntaxTrees
  21. .SelectMany(tree =>
  22. tree.GetRoot().DescendantNodes()
  23. .OfType<ClassDeclarationSyntax>()
  24. .SelectGodotScriptClasses(context.Compilation)
  25. // Report and skip non-partial classes
  26. .Where(x =>
  27. {
  28. if (x.cds.IsPartial())
  29. {
  30. if (x.cds.IsNested() && !x.cds.AreAllOuterTypesPartial(out var typeMissingPartial))
  31. {
  32. Common.ReportNonPartialGodotScriptOuterClass(context, typeMissingPartial!);
  33. return false;
  34. }
  35. return true;
  36. }
  37. Common.ReportNonPartialGodotScriptClass(context, x.cds, x.symbol);
  38. return false;
  39. })
  40. .Select(x => x.symbol)
  41. )
  42. .Distinct<INamedTypeSymbol>(SymbolEqualityComparer.Default)
  43. .ToArray();
  44. if (godotClasses.Length > 0)
  45. {
  46. var typeCache = new MarshalUtils.TypeCache(context.Compilation);
  47. foreach (var godotClass in godotClasses)
  48. {
  49. VisitGodotScriptClass(context, typeCache, godotClass);
  50. }
  51. }
  52. }
  53. private static void VisitGodotScriptClass(
  54. GeneratorExecutionContext context,
  55. MarshalUtils.TypeCache typeCache,
  56. INamedTypeSymbol symbol
  57. )
  58. {
  59. INamespaceSymbol namespaceSymbol = symbol.ContainingNamespace;
  60. string classNs = namespaceSymbol != null && !namespaceSymbol.IsGlobalNamespace ?
  61. namespaceSymbol.FullQualifiedNameOmitGlobal() :
  62. string.Empty;
  63. bool hasNamespace = classNs.Length != 0;
  64. bool isInnerClass = symbol.ContainingType != null;
  65. string uniqueHint = symbol.FullQualifiedNameOmitGlobal().SanitizeQualifiedNameForUniqueHint()
  66. + "_ScriptProperties.generated";
  67. var source = new StringBuilder();
  68. source.Append("using Godot;\n");
  69. source.Append("using Godot.NativeInterop;\n");
  70. source.Append("\n");
  71. if (hasNamespace)
  72. {
  73. source.Append("namespace ");
  74. source.Append(classNs);
  75. source.Append(" {\n\n");
  76. }
  77. if (isInnerClass)
  78. {
  79. var containingType = symbol.ContainingType;
  80. while (containingType != null)
  81. {
  82. source.Append("partial ");
  83. source.Append(containingType.GetDeclarationKeyword());
  84. source.Append(" ");
  85. source.Append(containingType.NameWithTypeParameters());
  86. source.Append("\n{\n");
  87. containingType = containingType.ContainingType;
  88. }
  89. }
  90. source.Append("partial class ");
  91. source.Append(symbol.NameWithTypeParameters());
  92. source.Append("\n{\n");
  93. var members = symbol.GetMembers();
  94. var propertySymbols = members
  95. .Where(s => !s.IsStatic && s.Kind == SymbolKind.Property)
  96. .Cast<IPropertySymbol>()
  97. .Where(s => !s.IsIndexer && s.ExplicitInterfaceImplementations.Length == 0);
  98. var fieldSymbols = members
  99. .Where(s => !s.IsStatic && s.Kind == SymbolKind.Field && !s.IsImplicitlyDeclared)
  100. .Cast<IFieldSymbol>();
  101. var godotClassProperties = propertySymbols.WhereIsGodotCompatibleType(typeCache).ToArray();
  102. var godotClassFields = fieldSymbols.WhereIsGodotCompatibleType(typeCache).ToArray();
  103. source.Append("#pragma warning disable CS0109 // Disable warning about redundant 'new' keyword\n");
  104. source.Append(" /// <summary>\n")
  105. .Append(" /// Cached StringNames for the properties and fields contained in this class, for fast lookup.\n")
  106. .Append(" /// </summary>\n");
  107. source.Append(
  108. $" public new class PropertyName : {symbol.BaseType.FullQualifiedNameIncludeGlobal()}.PropertyName {{\n");
  109. // Generate cached StringNames for methods and properties, for fast lookup
  110. foreach (var property in godotClassProperties)
  111. {
  112. string propertyName = property.PropertySymbol.Name;
  113. source.Append(" /// <summary>\n")
  114. .Append(" /// Cached name for the '")
  115. .Append(propertyName)
  116. .Append("' property.\n")
  117. .Append(" /// </summary>\n");
  118. source.Append(" public new static readonly global::Godot.StringName ");
  119. source.Append(propertyName);
  120. source.Append(" = \"");
  121. source.Append(propertyName);
  122. source.Append("\";\n");
  123. }
  124. foreach (var field in godotClassFields)
  125. {
  126. string fieldName = field.FieldSymbol.Name;
  127. source.Append(" /// <summary>\n")
  128. .Append(" /// Cached name for the '")
  129. .Append(fieldName)
  130. .Append("' field.\n")
  131. .Append(" /// </summary>\n");
  132. source.Append(" public new static readonly global::Godot.StringName ");
  133. source.Append(fieldName);
  134. source.Append(" = \"");
  135. source.Append(fieldName);
  136. source.Append("\";\n");
  137. }
  138. source.Append(" }\n"); // class GodotInternal
  139. if (godotClassProperties.Length > 0 || godotClassFields.Length > 0)
  140. {
  141. bool isFirstEntry;
  142. // Generate SetGodotClassPropertyValue
  143. bool allPropertiesAreReadOnly = godotClassFields.All(fi => fi.FieldSymbol.IsReadOnly) &&
  144. godotClassProperties.All(pi => pi.PropertySymbol.IsReadOnly || pi.PropertySymbol.SetMethod!.IsInitOnly);
  145. if (!allPropertiesAreReadOnly)
  146. {
  147. source.Append(" /// <inheritdoc/>\n");
  148. source.Append(" [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]\n");
  149. source.Append(" protected override bool SetGodotClassPropertyValue(in godot_string_name name, ");
  150. source.Append("in godot_variant value)\n {\n");
  151. isFirstEntry = true;
  152. foreach (var property in godotClassProperties)
  153. {
  154. if (property.PropertySymbol.IsReadOnly || property.PropertySymbol.SetMethod!.IsInitOnly)
  155. continue;
  156. GeneratePropertySetter(property.PropertySymbol.Name,
  157. property.PropertySymbol.Type, property.Type, source, isFirstEntry);
  158. isFirstEntry = false;
  159. }
  160. foreach (var field in godotClassFields)
  161. {
  162. if (field.FieldSymbol.IsReadOnly)
  163. continue;
  164. GeneratePropertySetter(field.FieldSymbol.Name,
  165. field.FieldSymbol.Type, field.Type, source, isFirstEntry);
  166. isFirstEntry = false;
  167. }
  168. source.Append(" return base.SetGodotClassPropertyValue(name, value);\n");
  169. source.Append(" }\n");
  170. }
  171. // Generate GetGodotClassPropertyValue
  172. bool allPropertiesAreWriteOnly = godotClassFields.Length == 0 && godotClassProperties.All(pi => pi.PropertySymbol.IsWriteOnly);
  173. if (!allPropertiesAreWriteOnly)
  174. {
  175. source.Append(" /// <inheritdoc/>\n");
  176. source.Append(" [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]\n");
  177. source.Append(" protected override bool GetGodotClassPropertyValue(in godot_string_name name, ");
  178. source.Append("out godot_variant value)\n {\n");
  179. isFirstEntry = true;
  180. foreach (var property in godotClassProperties)
  181. {
  182. if (property.PropertySymbol.IsWriteOnly)
  183. continue;
  184. GeneratePropertyGetter(property.PropertySymbol.Name,
  185. property.PropertySymbol.Type, property.Type, source, isFirstEntry);
  186. isFirstEntry = false;
  187. }
  188. foreach (var field in godotClassFields)
  189. {
  190. GeneratePropertyGetter(field.FieldSymbol.Name,
  191. field.FieldSymbol.Type, field.Type, source, isFirstEntry);
  192. isFirstEntry = false;
  193. }
  194. source.Append(" return base.GetGodotClassPropertyValue(name, out value);\n");
  195. source.Append(" }\n");
  196. }
  197. // Generate GetGodotPropertyList
  198. const string dictionaryType = "global::System.Collections.Generic.List<global::Godot.Bridge.PropertyInfo>";
  199. source.Append(" /// <summary>\n")
  200. .Append(" /// Get the property information for all the properties declared in this class.\n")
  201. .Append(" /// This method is used by Godot to register the available properties in the editor.\n")
  202. .Append(" /// Do not call this method.\n")
  203. .Append(" /// </summary>\n");
  204. source.Append(" [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]\n");
  205. source.Append(" internal new static ")
  206. .Append(dictionaryType)
  207. .Append(" GetGodotPropertyList()\n {\n");
  208. source.Append(" var properties = new ")
  209. .Append(dictionaryType)
  210. .Append("();\n");
  211. // To retain the definition order (and display categories correctly), we want to
  212. // iterate over fields and properties at the same time, sorted by line number.
  213. var godotClassPropertiesAndFields = Enumerable.Empty<GodotPropertyOrFieldData>()
  214. .Concat(godotClassProperties.Select(propertyData => new GodotPropertyOrFieldData(propertyData)))
  215. .Concat(godotClassFields.Select(fieldData => new GodotPropertyOrFieldData(fieldData)))
  216. .OrderBy(data => data.Symbol.Locations[0].Path())
  217. .ThenBy(data => data.Symbol.Locations[0].StartLine());
  218. foreach (var member in godotClassPropertiesAndFields)
  219. {
  220. foreach (var groupingInfo in DetermineGroupingPropertyInfo(member.Symbol))
  221. AppendGroupingPropertyInfo(source, groupingInfo);
  222. var propertyInfo = DeterminePropertyInfo(context, typeCache,
  223. member.Symbol, member.Type);
  224. if (propertyInfo == null)
  225. continue;
  226. AppendPropertyInfo(source, propertyInfo.Value);
  227. }
  228. source.Append(" return properties;\n");
  229. source.Append(" }\n");
  230. source.Append("#pragma warning restore CS0109\n");
  231. }
  232. source.Append("}\n"); // partial class
  233. if (isInnerClass)
  234. {
  235. var containingType = symbol.ContainingType;
  236. while (containingType != null)
  237. {
  238. source.Append("}\n"); // outer class
  239. containingType = containingType.ContainingType;
  240. }
  241. }
  242. if (hasNamespace)
  243. {
  244. source.Append("\n}\n");
  245. }
  246. context.AddSource(uniqueHint, SourceText.From(source.ToString(), Encoding.UTF8));
  247. }
  248. private static void GeneratePropertySetter(
  249. string propertyMemberName,
  250. ITypeSymbol propertyTypeSymbol,
  251. MarshalType propertyMarshalType,
  252. StringBuilder source,
  253. bool isFirstEntry
  254. )
  255. {
  256. source.Append(" ");
  257. if (!isFirstEntry)
  258. source.Append("else ");
  259. source.Append("if (name == PropertyName.")
  260. .Append(propertyMemberName)
  261. .Append(") {\n")
  262. .Append(" this.")
  263. .Append(propertyMemberName)
  264. .Append(" = ")
  265. .AppendNativeVariantToManagedExpr("value", propertyTypeSymbol, propertyMarshalType)
  266. .Append(";\n")
  267. .Append(" return true;\n")
  268. .Append(" }\n");
  269. }
  270. private static void GeneratePropertyGetter(
  271. string propertyMemberName,
  272. ITypeSymbol propertyTypeSymbol,
  273. MarshalType propertyMarshalType,
  274. StringBuilder source,
  275. bool isFirstEntry
  276. )
  277. {
  278. source.Append(" ");
  279. if (!isFirstEntry)
  280. source.Append("else ");
  281. source.Append("if (name == PropertyName.")
  282. .Append(propertyMemberName)
  283. .Append(") {\n")
  284. .Append(" value = ")
  285. .AppendManagedToNativeVariantExpr("this." + propertyMemberName,
  286. propertyTypeSymbol, propertyMarshalType)
  287. .Append(";\n")
  288. .Append(" return true;\n")
  289. .Append(" }\n");
  290. }
  291. private static void AppendGroupingPropertyInfo(StringBuilder source, PropertyInfo propertyInfo)
  292. {
  293. source.Append(" properties.Add(new(type: (global::Godot.Variant.Type)")
  294. .Append((int)VariantType.Nil)
  295. .Append(", name: \"")
  296. .Append(propertyInfo.Name)
  297. .Append("\", hint: (global::Godot.PropertyHint)")
  298. .Append((int)PropertyHint.None)
  299. .Append(", hintString: \"")
  300. .Append(propertyInfo.HintString)
  301. .Append("\", usage: (global::Godot.PropertyUsageFlags)")
  302. .Append((int)propertyInfo.Usage)
  303. .Append(", exported: true));\n");
  304. }
  305. private static void AppendPropertyInfo(StringBuilder source, PropertyInfo propertyInfo)
  306. {
  307. source.Append(" properties.Add(new(type: (global::Godot.Variant.Type)")
  308. .Append((int)propertyInfo.Type)
  309. .Append(", name: PropertyName.")
  310. .Append(propertyInfo.Name)
  311. .Append(", hint: (global::Godot.PropertyHint)")
  312. .Append((int)propertyInfo.Hint)
  313. .Append(", hintString: \"")
  314. .Append(propertyInfo.HintString)
  315. .Append("\", usage: (global::Godot.PropertyUsageFlags)")
  316. .Append((int)propertyInfo.Usage)
  317. .Append(", exported: ")
  318. .Append(propertyInfo.Exported ? "true" : "false")
  319. .Append("));\n");
  320. }
  321. private static IEnumerable<PropertyInfo> DetermineGroupingPropertyInfo(ISymbol memberSymbol)
  322. {
  323. foreach (var attr in memberSymbol.GetAttributes())
  324. {
  325. PropertyUsageFlags? propertyUsage = attr.AttributeClass?.FullQualifiedNameOmitGlobal() switch
  326. {
  327. GodotClasses.ExportCategoryAttr => PropertyUsageFlags.Category,
  328. GodotClasses.ExportGroupAttr => PropertyUsageFlags.Group,
  329. GodotClasses.ExportSubgroupAttr => PropertyUsageFlags.Subgroup,
  330. _ => null
  331. };
  332. if (propertyUsage is null)
  333. continue;
  334. if (attr.ConstructorArguments.Length > 0 && attr.ConstructorArguments[0].Value is string name)
  335. {
  336. string? hintString = null;
  337. if (propertyUsage != PropertyUsageFlags.Category && attr.ConstructorArguments.Length > 1)
  338. hintString = attr.ConstructorArguments[1].Value?.ToString();
  339. yield return new PropertyInfo(VariantType.Nil, name, PropertyHint.None, hintString,
  340. propertyUsage.Value, true);
  341. }
  342. }
  343. }
  344. private static PropertyInfo? DeterminePropertyInfo(
  345. GeneratorExecutionContext context,
  346. MarshalUtils.TypeCache typeCache,
  347. ISymbol memberSymbol,
  348. MarshalType marshalType
  349. )
  350. {
  351. var exportAttr = memberSymbol.GetAttributes()
  352. .FirstOrDefault(a => a.AttributeClass?.IsGodotExportAttribute() ?? false);
  353. var propertySymbol = memberSymbol as IPropertySymbol;
  354. var fieldSymbol = memberSymbol as IFieldSymbol;
  355. if (exportAttr != null && propertySymbol != null)
  356. {
  357. if (propertySymbol.GetMethod == null)
  358. {
  359. // This should never happen, as we filtered WriteOnly properties, but just in case.
  360. Common.ReportExportedMemberIsWriteOnly(context, propertySymbol);
  361. return null;
  362. }
  363. if (propertySymbol.SetMethod == null || propertySymbol.SetMethod.IsInitOnly)
  364. {
  365. // This should never happen, as we filtered ReadOnly properties, but just in case.
  366. Common.ReportExportedMemberIsReadOnly(context, propertySymbol);
  367. return null;
  368. }
  369. }
  370. var memberType = propertySymbol?.Type ?? fieldSymbol!.Type;
  371. var memberVariantType = MarshalUtils.ConvertMarshalTypeToVariantType(marshalType)!.Value;
  372. string memberName = memberSymbol.Name;
  373. if (exportAttr == null)
  374. {
  375. return new PropertyInfo(memberVariantType, memberName, PropertyHint.None,
  376. hintString: null, PropertyUsageFlags.ScriptVariable, exported: false);
  377. }
  378. if (!TryGetMemberExportHint(typeCache, memberType, exportAttr, memberVariantType,
  379. isTypeArgument: false, out var hint, out var hintString))
  380. {
  381. var constructorArguments = exportAttr.ConstructorArguments;
  382. if (constructorArguments.Length > 0)
  383. {
  384. var hintValue = exportAttr.ConstructorArguments[0].Value;
  385. hint = hintValue switch
  386. {
  387. null => PropertyHint.None,
  388. int intValue => (PropertyHint)intValue,
  389. _ => (PropertyHint)(long)hintValue
  390. };
  391. hintString = constructorArguments.Length > 1 ?
  392. exportAttr.ConstructorArguments[1].Value?.ToString() :
  393. null;
  394. }
  395. else
  396. {
  397. hint = PropertyHint.None;
  398. }
  399. }
  400. var propUsage = PropertyUsageFlags.Default | PropertyUsageFlags.ScriptVariable;
  401. if (memberVariantType == VariantType.Nil)
  402. propUsage |= PropertyUsageFlags.NilIsVariant;
  403. return new PropertyInfo(memberVariantType, memberName,
  404. hint, hintString, propUsage, exported: true);
  405. }
  406. private static bool TryGetMemberExportHint(
  407. MarshalUtils.TypeCache typeCache,
  408. ITypeSymbol type, AttributeData exportAttr,
  409. VariantType variantType, bool isTypeArgument,
  410. out PropertyHint hint, out string? hintString
  411. )
  412. {
  413. hint = PropertyHint.None;
  414. hintString = null;
  415. if (variantType == VariantType.Nil)
  416. return true; // Variant, no export hint
  417. if (variantType == VariantType.Int &&
  418. type.IsValueType && type.TypeKind == TypeKind.Enum)
  419. {
  420. bool hasFlagsAttr = type.GetAttributes()
  421. .Any(a => a.AttributeClass?.IsSystemFlagsAttribute() ?? false);
  422. hint = hasFlagsAttr ? PropertyHint.Flags : PropertyHint.Enum;
  423. var members = type.GetMembers();
  424. var enumFields = members
  425. .Where(s => s.Kind == SymbolKind.Field && s.IsStatic &&
  426. s.DeclaredAccessibility == Accessibility.Public &&
  427. !s.IsImplicitlyDeclared)
  428. .Cast<IFieldSymbol>().ToArray();
  429. var hintStringBuilder = new StringBuilder();
  430. var nameOnlyHintStringBuilder = new StringBuilder();
  431. // True: enum Foo { Bar, Baz, Qux }
  432. // True: enum Foo { Bar = 0, Baz = 1, Qux = 2 }
  433. // False: enum Foo { Bar = 0, Baz = 7, Qux = 5 }
  434. bool usesDefaultValues = true;
  435. for (int i = 0; i < enumFields.Length; i++)
  436. {
  437. var enumField = enumFields[i];
  438. if (i > 0)
  439. {
  440. hintStringBuilder.Append(",");
  441. nameOnlyHintStringBuilder.Append(",");
  442. }
  443. string enumFieldName = enumField.Name;
  444. hintStringBuilder.Append(enumFieldName);
  445. nameOnlyHintStringBuilder.Append(enumFieldName);
  446. long val = enumField.ConstantValue switch
  447. {
  448. sbyte v => v,
  449. short v => v,
  450. int v => v,
  451. long v => v,
  452. byte v => v,
  453. ushort v => v,
  454. uint v => v,
  455. ulong v => (long)v,
  456. _ => 0
  457. };
  458. uint expectedVal = (uint)(hint == PropertyHint.Flags ? 1 << i : i);
  459. if (val != expectedVal)
  460. usesDefaultValues = false;
  461. hintStringBuilder.Append(":");
  462. hintStringBuilder.Append(val);
  463. }
  464. hintString = !usesDefaultValues ?
  465. hintStringBuilder.ToString() :
  466. // If we use the format NAME:VAL, that's what the editor displays.
  467. // That's annoying if the user is not using custom values for the enum constants.
  468. // This may not be needed in the future if the editor is changed to not display values.
  469. nameOnlyHintStringBuilder.ToString();
  470. return true;
  471. }
  472. if (variantType == VariantType.Object && type is INamedTypeSymbol memberNamedType)
  473. {
  474. if (memberNamedType.InheritsFrom("GodotSharp", "Godot.Resource"))
  475. {
  476. hint = PropertyHint.ResourceType;
  477. hintString = GetTypeName(memberNamedType);
  478. return true;
  479. }
  480. if (memberNamedType.InheritsFrom("GodotSharp", "Godot.Node"))
  481. {
  482. hint = PropertyHint.NodeType;
  483. hintString = GetTypeName(memberNamedType);
  484. return true;
  485. }
  486. }
  487. static string GetTypeName(INamedTypeSymbol memberSymbol)
  488. {
  489. if (memberSymbol.GetAttributes()
  490. .Any(a => a.AttributeClass?.IsGodotGlobalClassAttribute() ?? false))
  491. {
  492. return memberSymbol.Name;
  493. }
  494. return memberSymbol.GetGodotScriptNativeClassName()!;
  495. }
  496. static bool GetStringArrayEnumHint(VariantType elementVariantType,
  497. AttributeData exportAttr, out string? hintString)
  498. {
  499. var constructorArguments = exportAttr.ConstructorArguments;
  500. if (constructorArguments.Length > 0)
  501. {
  502. var presetHintValue = exportAttr.ConstructorArguments[0].Value;
  503. PropertyHint presetHint = presetHintValue switch
  504. {
  505. null => PropertyHint.None,
  506. int intValue => (PropertyHint)intValue,
  507. _ => (PropertyHint)(long)presetHintValue
  508. };
  509. if (presetHint == PropertyHint.Enum)
  510. {
  511. string? presetHintString = constructorArguments.Length > 1 ?
  512. exportAttr.ConstructorArguments[1].Value?.ToString() :
  513. null;
  514. hintString = (int)elementVariantType + "/" + (int)PropertyHint.Enum + ":";
  515. if (presetHintString != null)
  516. hintString += presetHintString;
  517. return true;
  518. }
  519. }
  520. hintString = null;
  521. return false;
  522. }
  523. if (!isTypeArgument && variantType == VariantType.Array)
  524. {
  525. var elementType = MarshalUtils.GetArrayElementType(type);
  526. if (elementType == null)
  527. return false; // Non-generic Array, so there's no hint to add
  528. var elementMarshalType = MarshalUtils.ConvertManagedTypeToMarshalType(elementType, typeCache)!.Value;
  529. var elementVariantType = MarshalUtils.ConvertMarshalTypeToVariantType(elementMarshalType)!.Value;
  530. bool isPresetHint = false;
  531. if (elementVariantType == VariantType.String || elementVariantType == VariantType.StringName)
  532. isPresetHint = GetStringArrayEnumHint(elementVariantType, exportAttr, out hintString);
  533. if (!isPresetHint)
  534. {
  535. bool hintRes = TryGetMemberExportHint(typeCache, elementType,
  536. exportAttr, elementVariantType, isTypeArgument: true,
  537. out var elementHint, out var elementHintString);
  538. // Format: type/hint:hint_string
  539. if (hintRes)
  540. {
  541. hintString = (int)elementVariantType + "/" + (int)elementHint + ":";
  542. if (elementHintString != null)
  543. hintString += elementHintString;
  544. }
  545. else
  546. {
  547. hintString = (int)elementVariantType + "/" + (int)PropertyHint.None + ":";
  548. }
  549. }
  550. hint = PropertyHint.TypeString;
  551. return hintString != null;
  552. }
  553. if (!isTypeArgument && variantType == VariantType.PackedStringArray)
  554. {
  555. if (GetStringArrayEnumHint(VariantType.String, exportAttr, out hintString))
  556. {
  557. hint = PropertyHint.TypeString;
  558. return true;
  559. }
  560. }
  561. if (!isTypeArgument && variantType == VariantType.Dictionary)
  562. {
  563. // TODO: Dictionaries are not supported in the inspector
  564. return false;
  565. }
  566. return false;
  567. }
  568. }
  569. }