Break out XivChatType Extensions and InfoAttribute

This commit is contained in:
Raymond 2021-08-09 11:20:34 -04:00
parent 16266f9636
commit f26b1ead9e
3 changed files with 59 additions and 76 deletions

View file

@ -1,6 +1,3 @@
using System;
using System.Linq;
namespace Dalamud.Game.Text
{
/// <summary>
@ -237,77 +234,4 @@ namespace Dalamud.Game.Text
[XivChatTypeInfo("Crossworld Linkshell 8", "cw8", 0xFF1E90FF)]
CrossLinkShell8 = 107,
}
/// <summary>
/// Extension methods for the <see cref="XivChatType"/> type.
/// </summary>
public static class XivChatTypeExtensions
{
/// <summary>
/// Get the InfoAttribute associated with this chat type.
/// </summary>
/// <param name="chatType">The chat type.</param>
/// <returns>The info attribute.</returns>
public static XivChatTypeInfoAttribute GetDetails(this XivChatType chatType)
{
return chatType.GetAttribute<XivChatTypeInfoAttribute>();
}
}
/// <summary>
/// Storage for relevant information associated with the chat type.
/// </summary>
public class XivChatTypeInfoAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="XivChatTypeInfoAttribute"/> class.
/// </summary>
/// <param name="fancyName">The fancy name.</param>
/// <param name="slug">The name slug.</param>
/// <param name="defaultColor">The default color.</param>
internal XivChatTypeInfoAttribute(string fancyName, string slug, uint defaultColor)
{
this.FancyName = fancyName;
this.Slug = slug;
this.DefaultColor = defaultColor;
}
/// <summary>
/// Gets the "fancy" name of the type.
/// </summary>
public string FancyName { get; }
/// <summary>
/// Gets the type name slug or short-form.
/// </summary>
public string Slug { get; }
/// <summary>
/// Gets the type default color.
/// </summary>
public uint DefaultColor { get; }
}
/// <summary>
/// Extension methods for enums.
/// </summary>
public static class EnumExtensions
{
/// <summary>
/// Gets an attribute on an enum.
/// </summary>
/// <typeparam name="TAttribute">The type of attribute to get.</typeparam>
/// <param name="value">The enum value that has an attached attribute.</param>
/// <returns>The attached attribute, if any.</returns>
public static TAttribute GetAttribute<TAttribute>(this Enum value)
where TAttribute : Attribute
{
var type = value.GetType();
var name = Enum.GetName(type, value);
return type.GetField(name) // I prefer to get attributes this way
.GetCustomAttributes(false)
.OfType<TAttribute>()
.SingleOrDefault();
}
}
}

View file

@ -0,0 +1,20 @@
using Dalamud.Utility;
namespace Dalamud.Game.Text
{
/// <summary>
/// Extension methods for the <see cref="XivChatType"/> type.
/// </summary>
public static class XivChatTypeExtensions
{
/// <summary>
/// Get the InfoAttribute associated with this chat type.
/// </summary>
/// <param name="chatType">The chat type.</param>
/// <returns>The info attribute.</returns>
public static XivChatTypeInfoAttribute GetDetails(this XivChatType chatType)
{
return chatType.GetAttribute<XivChatTypeInfoAttribute>();
}
}
}

View file

@ -0,0 +1,39 @@
using System;
namespace Dalamud.Game.Text
{
/// <summary>
/// Storage for relevant information associated with the chat type.
/// </summary>
[AttributeUsage(AttributeTargets.Field)]
public class XivChatTypeInfoAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="XivChatTypeInfoAttribute"/> class.
/// </summary>
/// <param name="fancyName">The fancy name.</param>
/// <param name="slug">The name slug.</param>
/// <param name="defaultColor">The default color.</param>
internal XivChatTypeInfoAttribute(string fancyName, string slug, uint defaultColor)
{
this.FancyName = fancyName;
this.Slug = slug;
this.DefaultColor = defaultColor;
}
/// <summary>
/// Gets the "fancy" name of the type.
/// </summary>
public string FancyName { get; }
/// <summary>
/// Gets the type name slug or short-form.
/// </summary>
public string Slug { get; }
/// <summary>
/// Gets the type default color.
/// </summary>
public uint DefaultColor { get; }
}
}