// Source: https://gist.github.com/kleonc/a2bab51686ac6f4d7cb28aec88efa5d9 using System; using System.Collections.Generic; using System.Linq; using Godot; namespace Namespace { using UnderlyingType = UInt64; [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)] public class ExportFlagsEnumAttribute : ExportAttribute { public ExportFlagsEnumAttribute(Type enumType) : base(PropertyHint.Flags, GetFlagsEnumHintString(enumType)) { } private static string GetFlagsEnumHintString(Type enumType) { Dictionary> flagNamesByFlag = new Dictionary>(); UnderlyingType flag = (UnderlyingType)1; foreach (string name in Enum.GetNames(enumType)) { UnderlyingType value = (UnderlyingType)Convert.ChangeType(Enum.Parse(enumType, name), typeof(UnderlyingType)); while (value > flag) { if (!flagNamesByFlag.ContainsKey(flag)) { flagNamesByFlag.Add(flag, new List()); } flag <<= 1; } if (value == flag) { if (!flagNamesByFlag.TryGetValue(flag, out List names)) { names = new List(); flagNamesByFlag.Add(flag, names); } names.Add(name); } } return string.Join(", ", flagNamesByFlag.Values.Select(flagNames => string.Join(" / ", flagNames))); } } }