|
1 | 1 | using System; |
| 2 | +using System.Numerics; |
2 | 3 |
|
3 | 4 | namespace Datamodel.Format; |
4 | 5 |
|
5 | 6 | /// <summary> |
6 | 7 | /// Subclass this attribute to define a custom attribute name convention. |
7 | 8 | /// </summary> |
8 | 9 | [AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)] |
9 | | -public abstract class AttributeNameConventionAttribute : System.Attribute |
| 10 | +public abstract class AttributeNamingConventionAttribute : System.Attribute |
10 | 11 | { |
11 | | - public abstract string GetAttributeName(string propertyName); |
| 12 | + public abstract string GetAttributeName(string propertyName, Type propertyType); |
12 | 13 | } |
13 | 14 |
|
14 | | -public class AttributeNameLowercaseAttribute : AttributeNameConventionAttribute |
| 15 | +/// <summary> |
| 16 | +/// This class' property names are mostly lowercase. |
| 17 | +/// </summary> |
| 18 | +public class LowercasePropertiesAttribute : AttributeNamingConventionAttribute |
15 | 19 | { |
16 | | - public override string GetAttributeName(string propertyName) |
| 20 | + public override string GetAttributeName(string propertyName, Type _) |
17 | 21 | => propertyName.ToLower(); |
18 | 22 | } |
19 | 23 |
|
20 | | -public class AttributeNameCamelCaseAttribute : AttributeNameConventionAttribute |
| 24 | +/// <summary> |
| 25 | +/// This class' property names are mostly camelCase. |
| 26 | +/// </summary> |
| 27 | +public class CamelCasePropertiesAttribute : AttributeNamingConventionAttribute |
21 | 28 | { |
22 | | - public override string GetAttributeName(string propertyName) |
| 29 | + public override string GetAttributeName(string propertyName, Type _) |
23 | 30 | => char.ToLowerInvariant(propertyName.AsSpan()[0]) + propertyName[1..]; |
24 | 31 | } |
25 | 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 | + |
26 | 61 | [AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)] |
27 | | -public sealed class Attribute : System.Attribute |
| 62 | +public sealed class DMProperty : System.Attribute |
28 | 63 | { |
29 | | - public Attribute(string name) |
| 64 | + /// <param name="name">The name to use for serialization.</param> |
| 65 | + /// <param name="optional">Ignore serialization if property is on the default value.</param> |
| 66 | + public DMProperty(string name = null, bool optional = false) |
30 | 67 | { |
31 | 68 | Name = name; |
| 69 | + Optional = optional; |
32 | 70 | } |
33 | 71 |
|
34 | 72 | public string Name { get; } |
| 73 | + public bool Optional { get; } |
35 | 74 | } |
0 commit comments