ScriptPropertiesGenerator.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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. source.Append(" /// <inheritdoc/>\n");
  173. source.Append(" [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]\n");
  174. source.Append(" protected override bool GetGodotClassPropertyValue(in godot_string_name name, ");
  175. source.Append("out godot_variant value)\n {\n");
  176. isFirstEntry = true;
  177. foreach (var property in godotClassProperties)
  178. {
  179. GeneratePropertyGetter(property.PropertySymbol.Name,
  180. property.PropertySymbol.Type, property.Type, source, isFirstEntry);
  181. isFirstEntry = false;
  182. }
  183. foreach (var field in godotClassFields)
  184. {
  185. GeneratePropertyGetter(field.FieldSymbol.Name,
  186. field.FieldSymbol.Type, field.Type, source, isFirstEntry);
  187. isFirstEntry = false;
  188. }
  189. source.Append(" return base.GetGodotClassPropertyValue(name, out value);\n");
  190. source.Append(" }\n");
  191. // Generate GetGodotPropertyList
  192. const string dictionaryType = "global::System.Collections.Generic.List<global::Godot.Bridge.PropertyInfo>";
  193. source.Append(" /// <summary>\n")
  194. .Append(" /// Get the property information for all the properties declared in this class.\n")
  195. .Append(" /// This method is used by Godot to register the available properties in the editor.\n")
  196. .Append(" /// Do not call this method.\n")
  197. .Append(" /// </summary>\n");
  198. source.Append(" [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]\n");
  199. source.Append(" internal new static ")
  200. .Append(dictionaryType)
  201. .Append(" GetGodotPropertyList()\n {\n");
  202. source.Append(" var properties = new ")
  203. .Append(dictionaryType)
  204. .Append("();\n");
  205. // To retain the definition order (and display categories correctly), we want to
  206. // iterate over fields and properties at the same time, sorted by line number.
  207. var godotClassPropertiesAndFields = Enumerable.Empty<GodotPropertyOrFieldData>()
  208. .Concat(godotClassProperties.Select(propertyData => new GodotPropertyOrFieldData(propertyData)))
  209. .Concat(godotClassFields.Select(fieldData => new GodotPropertyOrFieldData(fieldData)))
  210. .OrderBy(data => data.Symbol.Locations[0].Path())
  211. .ThenBy(data => data.Symbol.Locations[0].StartLine());
  212. foreach (var member in godotClassPropertiesAndFields)
  213. {
  214. foreach (var groupingInfo in DetermineGroupingPropertyInfo(member.Symbol))
  215. AppendGroupingPropertyInfo(source, groupingInfo);
  216. var propertyInfo = DeterminePropertyInfo(context, typeCache,
  217. member.Symbol, member.Type);
  218. if (propertyInfo == null)
  219. continue;
  220. AppendPropertyInfo(source, propertyInfo.Value);
  221. }
  222. source.Append(" return properties;\n");
  223. source.Append(" }\n");
  224. source.Append("#pragma warning restore CS0109\n");
  225. }
  226. source.Append("}\n"); // partial class
  227. if (isInnerClass)
  228. {
  229. var containingType = symbol.ContainingType;
  230. while (containingType != null)
  231. {
  232. source.Append("}\n"); // outer class
  233. containingType = containingType.ContainingType;
  234. }
  235. }
  236. if (hasNamespace)
  237. {
  238. source.Append("\n}\n");
  239. }
  240. context.AddSource(uniqueHint, SourceText.From(source.ToString(), Encoding.UTF8));
  241. }
  242. private static void GeneratePropertySetter(
  243. string propertyMemberName,
  244. ITypeSymbol propertyTypeSymbol,
  245. MarshalType propertyMarshalType,
  246. StringBuilder source,
  247. bool isFirstEntry
  248. )
  249. {
  250. source.Append(" ");
  251. if (!isFirstEntry)
  252. source.Append("else ");
  253. source.Append("if (name == PropertyName.")
  254. .Append(propertyMemberName)
  255. .Append(") {\n")
  256. .Append(" this.")
  257. .Append(propertyMemberName)
  258. .Append(" = ")
  259. .AppendNativeVariantToManagedExpr("value", propertyTypeSymbol, propertyMarshalType)
  260. .Append(";\n")
  261. .Append(" return true;\n")
  262. .Append(" }\n");
  263. }
  264. private static void GeneratePropertyGetter(
  265. string propertyMemberName,
  266. ITypeSymbol propertyTypeSymbol,
  267. MarshalType propertyMarshalType,
  268. StringBuilder source,
  269. bool isFirstEntry
  270. )
  271. {
  272. source.Append(" ");
  273. if (!isFirstEntry)
  274. source.Append("else ");
  275. source.Append("if (name == PropertyName.")
  276. .Append(propertyMemberName)
  277. .Append(") {\n")
  278. .Append(" value = ")
  279. .AppendManagedToNativeVariantExpr("this." + propertyMemberName,
  280. propertyTypeSymbol, propertyMarshalType)
  281. .Append(";\n")
  282. .Append(" return true;\n")
  283. .Append(" }\n");
  284. }
  285. private static void AppendGroupingPropertyInfo(StringBuilder source, PropertyInfo propertyInfo)
  286. {
  287. source.Append(" properties.Add(new(type: (global::Godot.Variant.Type)")
  288. .Append((int)VariantType.Nil)
  289. .Append(", name: \"")
  290. .Append(propertyInfo.Name)
  291. .Append("\", hint: (global::Godot.PropertyHint)")
  292. .Append((int)PropertyHint.None)
  293. .Append(", hintString: \"")
  294. .Append(propertyInfo.HintString)
  295. .Append("\", usage: (global::Godot.PropertyUsageFlags)")
  296. .Append((int)propertyInfo.Usage)
  297. .Append(", exported: true));\n");
  298. }
  299. private static void AppendPropertyInfo(StringBuilder source, PropertyInfo propertyInfo)
  300. {
  301. source.Append(" properties.Add(new(type: (global::Godot.Variant.Type)")
  302. .Append((int)propertyInfo.Type)
  303. .Append(", name: PropertyName.")
  304. .Append(propertyInfo.Name)
  305. .Append(", hint: (global::Godot.PropertyHint)")
  306. .Append((int)propertyInfo.Hint)
  307. .Append(", hintString: \"")
  308. .Append(propertyInfo.HintString)
  309. .Append("\", usage: (global::Godot.PropertyUsageFlags)")
  310. .Append((int)propertyInfo.Usage)
  311. .Append(", exported: ")
  312. .Append(propertyInfo.Exported ? "true" : "false")
  313. .Append("));\n");
  314. }
  315. private static IEnumerable<PropertyInfo> DetermineGroupingPropertyInfo(ISymbol memberSymbol)
  316. {
  317. foreach (var attr in memberSymbol.GetAttributes())
  318. {
  319. PropertyUsageFlags? propertyUsage = attr.AttributeClass?.FullQualifiedNameOmitGlobal() switch
  320. {
  321. GodotClasses.ExportCategoryAttr => PropertyUsageFlags.Category,
  322. GodotClasses.ExportGroupAttr => PropertyUsageFlags.Group,
  323. GodotClasses.ExportSubgroupAttr => PropertyUsageFlags.Subgroup,
  324. _ => null
  325. };
  326. if (propertyUsage is null)
  327. continue;
  328. if (attr.ConstructorArguments.Length > 0 && attr.ConstructorArguments[0].Value is string name)
  329. {
  330. string? hintString = null;
  331. if (propertyUsage != PropertyUsageFlags.Category && attr.ConstructorArguments.Length > 1)
  332. hintString = attr.ConstructorArguments[1].Value?.ToString();
  333. yield return new PropertyInfo(VariantType.Nil, name, PropertyHint.None, hintString,
  334. propertyUsage.Value, true);
  335. }
  336. }
  337. }
  338. private static PropertyInfo? DeterminePropertyInfo(
  339. GeneratorExecutionContext context,
  340. MarshalUtils.TypeCache typeCache,
  341. ISymbol memberSymbol,
  342. MarshalType marshalType
  343. )
  344. {
  345. var exportAttr = memberSymbol.GetAttributes()
  346. .FirstOrDefault(a => a.AttributeClass?.IsGodotExportAttribute() ?? false);
  347. var propertySymbol = memberSymbol as IPropertySymbol;
  348. var fieldSymbol = memberSymbol as IFieldSymbol;
  349. if (exportAttr != null && propertySymbol != null)
  350. {
  351. if (propertySymbol.GetMethod == null)
  352. {
  353. // This should never happen, as we filtered WriteOnly properties, but just in case.
  354. Common.ReportExportedMemberIsWriteOnly(context, propertySymbol);
  355. return null;
  356. }
  357. if (propertySymbol.SetMethod == null || propertySymbol.SetMethod.IsInitOnly)
  358. {
  359. // This should never happen, as we filtered ReadOnly properties, but just in case.
  360. Common.ReportExportedMemberIsReadOnly(context, propertySymbol);
  361. return null;
  362. }
  363. }
  364. var memberType = propertySymbol?.Type ?? fieldSymbol!.Type;
  365. var memberVariantType = MarshalUtils.ConvertMarshalTypeToVariantType(marshalType)!.Value;
  366. string memberName = memberSymbol.Name;
  367. if (exportAttr == null)
  368. {
  369. return new PropertyInfo(memberVariantType, memberName, PropertyHint.None,
  370. hintString: null, PropertyUsageFlags.ScriptVariable, exported: false);
  371. }
  372. if (!TryGetMemberExportHint(typeCache, memberType, exportAttr, memberVariantType,
  373. isTypeArgument: false, out var hint, out var hintString))
  374. {
  375. var constructorArguments = exportAttr.ConstructorArguments;
  376. if (constructorArguments.Length > 0)
  377. {
  378. var hintValue = exportAttr.ConstructorArguments[0].Value;
  379. hint = hintValue switch
  380. {
  381. null => PropertyHint.None,
  382. int intValue => (PropertyHint)intValue,
  383. _ => (PropertyHint)(long)hintValue
  384. };
  385. hintString = constructorArguments.Length > 1 ?
  386. exportAttr.ConstructorArguments[1].Value?.ToString() :
  387. null;
  388. }
  389. else
  390. {
  391. hint = PropertyHint.None;
  392. }
  393. }
  394. var propUsage = PropertyUsageFlags.Default | PropertyUsageFlags.ScriptVariable;
  395. if (memberVariantType == VariantType.Nil)
  396. propUsage |= PropertyUsageFlags.NilIsVariant;
  397. return new PropertyInfo(memberVariantType, memberName,
  398. hint, hintString, propUsage, exported: true);
  399. }
  400. private static bool TryGetMemberExportHint(
  401. MarshalUtils.TypeCache typeCache,
  402. ITypeSymbol type, AttributeData exportAttr,
  403. VariantType variantType, bool isTypeArgument,
  404. out PropertyHint hint, out string? hintString
  405. )
  406. {
  407. hint = PropertyHint.None;
  408. hintString = null;
  409. if (variantType == VariantType.Nil)
  410. return true; // Variant, no export hint
  411. if (variantType == VariantType.Int &&
  412. type.IsValueType && type.TypeKind == TypeKind.Enum)
  413. {
  414. bool hasFlagsAttr = type.GetAttributes()
  415. .Any(a => a.AttributeClass?.IsSystemFlagsAttribute() ?? false);
  416. hint = hasFlagsAttr ? PropertyHint.Flags : PropertyHint.Enum;
  417. var members = type.GetMembers();
  418. var enumFields = members
  419. .Where(s => s.Kind == SymbolKind.Field && s.IsStatic &&
  420. s.DeclaredAccessibility == Accessibility.Public &&
  421. !s.IsImplicitlyDeclared)
  422. .Cast<IFieldSymbol>().ToArray();
  423. var hintStringBuilder = new StringBuilder();
  424. var nameOnlyHintStringBuilder = new StringBuilder();
  425. // True: enum Foo { Bar, Baz, Qux }
  426. // True: enum Foo { Bar = 0, Baz = 1, Qux = 2 }
  427. // False: enum Foo { Bar = 0, Baz = 7, Qux = 5 }
  428. bool usesDefaultValues = true;
  429. for (int i = 0; i < enumFields.Length; i++)
  430. {
  431. var enumField = enumFields[i];
  432. if (i > 0)
  433. {
  434. hintStringBuilder.Append(",");
  435. nameOnlyHintStringBuilder.Append(",");
  436. }
  437. string enumFieldName = enumField.Name;
  438. hintStringBuilder.Append(enumFieldName);
  439. nameOnlyHintStringBuilder.Append(enumFieldName);
  440. long val = enumField.ConstantValue switch
  441. {
  442. sbyte v => v,
  443. short v => v,
  444. int v => v,
  445. long v => v,
  446. byte v => v,
  447. ushort v => v,
  448. uint v => v,
  449. ulong v => (long)v,
  450. _ => 0
  451. };
  452. uint expectedVal = (uint)(hint == PropertyHint.Flags ? 1 << i : i);
  453. if (val != expectedVal)
  454. usesDefaultValues = false;
  455. hintStringBuilder.Append(":");
  456. hintStringBuilder.Append(val);
  457. }
  458. hintString = !usesDefaultValues ?
  459. hintStringBuilder.ToString() :
  460. // If we use the format NAME:VAL, that's what the editor displays.
  461. // That's annoying if the user is not using custom values for the enum constants.
  462. // This may not be needed in the future if the editor is changed to not display values.
  463. nameOnlyHintStringBuilder.ToString();
  464. return true;
  465. }
  466. if (variantType == VariantType.Object && type is INamedTypeSymbol memberNamedType)
  467. {
  468. if (memberNamedType.InheritsFrom("GodotSharp", "Godot.Resource"))
  469. {
  470. hint = PropertyHint.ResourceType;
  471. hintString = GetTypeName(memberNamedType);
  472. return true;
  473. }
  474. if (memberNamedType.InheritsFrom("GodotSharp", "Godot.Node"))
  475. {
  476. hint = PropertyHint.NodeType;
  477. hintString = GetTypeName(memberNamedType);
  478. return true;
  479. }
  480. }
  481. static string GetTypeName(INamedTypeSymbol memberSymbol)
  482. {
  483. if (memberSymbol.GetAttributes()
  484. .Any(a => a.AttributeClass?.IsGodotGlobalClassAttribute() ?? false))
  485. {
  486. return memberSymbol.Name;
  487. }
  488. return memberSymbol.GetGodotScriptNativeClassName()!;
  489. }
  490. static bool GetStringArrayEnumHint(VariantType elementVariantType,
  491. AttributeData exportAttr, out string? hintString)
  492. {
  493. var constructorArguments = exportAttr.ConstructorArguments;
  494. if (constructorArguments.Length > 0)
  495. {
  496. var presetHintValue = exportAttr.ConstructorArguments[0].Value;
  497. PropertyHint presetHint = presetHintValue switch
  498. {
  499. null => PropertyHint.None,
  500. int intValue => (PropertyHint)intValue,
  501. _ => (PropertyHint)(long)presetHintValue
  502. };
  503. if (presetHint == PropertyHint.Enum)
  504. {
  505. string? presetHintString = constructorArguments.Length > 1 ?
  506. exportAttr.ConstructorArguments[1].Value?.ToString() :
  507. null;
  508. hintString = (int)elementVariantType + "/" + (int)PropertyHint.Enum + ":";
  509. if (presetHintString != null)
  510. hintString += presetHintString;
  511. return true;
  512. }
  513. }
  514. hintString = null;
  515. return false;
  516. }
  517. if (!isTypeArgument && variantType == VariantType.Array)
  518. {
  519. var elementType = MarshalUtils.GetArrayElementType(type);
  520. if (elementType == null)
  521. return false; // Non-generic Array, so there's no hint to add
  522. var elementMarshalType = MarshalUtils.ConvertManagedTypeToMarshalType(elementType, typeCache)!.Value;
  523. var elementVariantType = MarshalUtils.ConvertMarshalTypeToVariantType(elementMarshalType)!.Value;
  524. bool isPresetHint = false;
  525. if (elementVariantType == VariantType.String || elementVariantType == VariantType.StringName)
  526. isPresetHint = GetStringArrayEnumHint(elementVariantType, exportAttr, out hintString);
  527. if (!isPresetHint)
  528. {
  529. bool hintRes = TryGetMemberExportHint(typeCache, elementType,
  530. exportAttr, elementVariantType, isTypeArgument: true,
  531. out var elementHint, out var elementHintString);
  532. // Format: type/hint:hint_string
  533. if (hintRes)
  534. {
  535. hintString = (int)elementVariantType + "/" + (int)elementHint + ":";
  536. if (elementHintString != null)
  537. hintString += elementHintString;
  538. }
  539. else
  540. {
  541. hintString = (int)elementVariantType + "/" + (int)PropertyHint.None + ":";
  542. }
  543. }
  544. hint = PropertyHint.TypeString;
  545. return hintString != null;
  546. }
  547. if (!isTypeArgument && variantType == VariantType.PackedStringArray)
  548. {
  549. if (GetStringArrayEnumHint(VariantType.String, exportAttr, out hintString))
  550. {
  551. hint = PropertyHint.TypeString;
  552. return true;
  553. }
  554. }
  555. if (!isTypeArgument && variantType == VariantType.Dictionary)
  556. {
  557. // TODO: Dictionaries are not supported in the inspector
  558. return false;
  559. }
  560. return false;
  561. }
  562. }
  563. }