Skip to content

Commit ab4143d

Browse files
committed
Rename c# class attributes, add [HungarianProperties]
1 parent e971524 commit ab4143d

2 files changed

Lines changed: 50 additions & 10 deletions

File tree

Element.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,9 @@ internal set
201201
if (property.GetIndexParameters().Length == 0 && property.DeclaringType.IsSubclassOf(typeof(Element)))
202202
{
203203
var name = property.Name;
204-
name = property.DeclaringType.GetCustomAttribute<Format.AttributeNameConventionAttribute>()?.GetAttributeName(name) ?? name;
205-
name = property.GetCustomAttribute<Format.Attribute>()?.Name ?? name;
204+
name = property.DeclaringType.GetCustomAttribute<Format.AttributeNamingConventionAttribute>()?
205+
.GetAttributeName(name, property.PropertyType) ?? name;
206+
name = property.GetCustomAttribute<Format.DMProperty>()?.Name ?? name;
206207

207208
properties.Add((name, property));
208209
}

Format/Attribute.cs

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,74 @@
11
using System;
2+
using System.Numerics;
23

34
namespace Datamodel.Format;
45

56
/// <summary>
67
/// Subclass this attribute to define a custom attribute name convention.
78
/// </summary>
89
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
9-
public abstract class AttributeNameConventionAttribute : System.Attribute
10+
public abstract class AttributeNamingConventionAttribute : System.Attribute
1011
{
11-
public abstract string GetAttributeName(string propertyName);
12+
public abstract string GetAttributeName(string propertyName, Type propertyType);
1213
}
1314

14-
public class AttributeNameLowercaseAttribute : AttributeNameConventionAttribute
15+
/// <summary>
16+
/// This class' property names are mostly lowercase.
17+
/// </summary>
18+
public class LowercasePropertiesAttribute : AttributeNamingConventionAttribute
1519
{
16-
public override string GetAttributeName(string propertyName)
20+
public override string GetAttributeName(string propertyName, Type _)
1721
=> propertyName.ToLower();
1822
}
1923

20-
public class AttributeNameCamelCaseAttribute : AttributeNameConventionAttribute
24+
/// <summary>
25+
/// This class' property names are mostly camelCase.
26+
/// </summary>
27+
public class CamelCasePropertiesAttribute : AttributeNamingConventionAttribute
2128
{
22-
public override string GetAttributeName(string propertyName)
29+
public override string GetAttributeName(string propertyName, Type _)
2330
=> char.ToLowerInvariant(propertyName.AsSpan()[0]) + propertyName[1..];
2431
}
2532

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+
2661
[AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
27-
public sealed class Attribute : System.Attribute
62+
public sealed class DMProperty : System.Attribute
2863
{
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)
3067
{
3168
Name = name;
69+
Optional = optional;
3270
}
3371

3472
public string Name { get; }
73+
public bool Optional { get; }
3574
}

0 commit comments

Comments
 (0)