Skip to content

Commit

Permalink
More extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
TheArcaneBrony committed Jan 11, 2024
1 parent 28c0e5a commit f69bb51
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
25 changes: 25 additions & 0 deletions ArcaneLibs/Attributes/FriendlyNameAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Reflection;

namespace ArcaneLibs.Attributes;

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property, AllowMultiple = false)]
public class FriendlyNameAttribute : Attribute {
public required string Name { get; set; }
public string? NamePlural { get; set; }
}

public static class FriendlyNameAttributeExtensions {
public static string? GetFriendlyNameOrNull(this MemberInfo type) {
var attrs = type.GetCustomAttributes(typeof(FriendlyNameAttribute), false);
return attrs.Length == 0 ? null : ((FriendlyNameAttribute)attrs[0]).Name;
}

public static string? GetFriendlyNamePluralOrNull(this MemberInfo type) {
var attrs = type.GetCustomAttributes(typeof(FriendlyNameAttribute), false);
return attrs.Length == 0 ? null : ((FriendlyNameAttribute)attrs[0]).NamePlural;
}

public static string GetFriendlyName(this MemberInfo type) => type.GetFriendlyNameOrNull() ?? type.Name;

public static string GetFriendlyNamePlural(this MemberInfo type) => type.GetFriendlyNamePluralOrNull() ?? type.Name;
}
6 changes: 6 additions & 0 deletions ArcaneLibs/Attributes/TableHideAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using System.Reflection;

namespace ArcaneLibs.Attributes;

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property, AllowMultiple = false)]
public class TableHideAttribute : Attribute;
13 changes: 13 additions & 0 deletions ArcaneLibs/Extensions/JsonExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Reflection;
using System.Text.Json.Serialization;

namespace ArcaneLibs.Extensions;

public static class JsonExtensions {
public static string? GetJsonPropertyNameOrNull(this MemberInfo member) {
var attrs = member.GetCustomAttributes(typeof(JsonPropertyNameAttribute), false);
return attrs.Length == 0 ? null : ((JsonPropertyNameAttribute)attrs[0]).Name;
}

public static string GetJsonPropertyName(this MemberInfo member) => member.GetJsonPropertyNameOrNull() ?? member.Name;
}
17 changes: 17 additions & 0 deletions ArcaneLibs/RainbowEnumerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace ArcaneLibs;

public class RainbowEnumerator(byte skip = 1, int offset = 0, double lengthFactor = 255.0) {
private const double FullRotation = Math.PI * 2;
private readonly double _oneThird = lengthFactor / 3;
private readonly double _twoThirds = lengthFactor / 3 * 2;

public (byte r, byte g, byte b) Next() {
var v = (
r: (byte) (128 + 127 * Math.Sin(offset / lengthFactor * FullRotation)),
g: (byte) (128 + 127 * Math.Sin((offset + lengthFactor * _oneThird) / lengthFactor * FullRotation)),
b: (byte) (128 + 127 * Math.Sin((offset + lengthFactor * _twoThirds) / lengthFactor * FullRotation))
);
offset += skip;
return v;
}
}

0 comments on commit f69bb51

Please sign in to comment.