|
3 | 3 |
|
4 | 4 | namespace Datamodel.Format; |
5 | 5 |
|
| 6 | +/// <summary> |
| 7 | +/// Subclass this attribute to define a custom attribute name convention. |
| 8 | +/// </summary> |
| 9 | +[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)] |
| 10 | +public abstract class AttributeNamingConventionAttribute : System.Attribute |
| 11 | +{ |
| 12 | + public abstract string GetAttributeName(string propertyName, Type propertyType); |
| 13 | +} |
| 14 | + |
| 15 | +/// <summary> |
| 16 | +/// This class' property names are mostly lowercase. |
| 17 | +/// </summary> |
| 18 | +public class LowercasePropertiesAttribute : AttributeNamingConventionAttribute |
| 19 | +{ |
| 20 | + public override string GetAttributeName(string propertyName, Type _) |
| 21 | + => propertyName.ToLower(); |
| 22 | +} |
| 23 | + |
| 24 | +/// <summary> |
| 25 | +/// This class' property names are mostly camelCase. |
| 26 | +/// </summary> |
| 27 | +public class CamelCasePropertiesAttribute : AttributeNamingConventionAttribute |
| 28 | +{ |
| 29 | + public override string GetAttributeName(string propertyName, Type _) |
| 30 | + => char.ToLowerInvariant(propertyName.AsSpan()[0]) + propertyName[1..]; |
| 31 | +} |
| 32 | + |
| 33 | +/// <summary> |
| 34 | +/// This class' property names are mostly m_hungarian. |
| 35 | +/// </summary> |
| 36 | +public class HungarianPropertiesAttribute : CamelCasePropertiesAttribute |
| 37 | +{ |
| 38 | + public override string GetAttributeName(string propertyName, Type propertyType) |
| 39 | + { |
| 40 | + var typeAnnotation = propertyType switch |
| 41 | + { |
| 42 | + _ when propertyType == typeof(int) => "n", |
| 43 | + _ when propertyType == typeof(float) => "fl", |
| 44 | + _ when propertyType == typeof(bool) => "b", |
| 45 | + _ when propertyType == typeof(Vector2) => "v", |
| 46 | + _ when propertyType == typeof(Vector3) => "v", |
| 47 | + _ when propertyType == typeof(Vector4) => "v", |
| 48 | + _ when propertyType == typeof(Matrix4x4) => "mat", |
| 49 | + _ => string.Empty, |
| 50 | + }; |
| 51 | + |
| 52 | + if (typeAnnotation == string.Empty) |
| 53 | + { |
| 54 | + return "m_" + base.GetAttributeName(propertyName, propertyType); |
| 55 | + } |
| 56 | + |
| 57 | + return "m_" + typeAnnotation + propertyName; |
| 58 | + } |
| 59 | +} |
| 60 | + |
6 | 61 | [AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)] |
7 | 62 | public sealed class DMProperty : System.Attribute |
8 | 63 | { |
|
0 commit comments