Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Potentially change structure of Razer.Devices #180

Closed
njbmartin opened this issue Jun 21, 2016 · 5 comments
Closed

Potentially change structure of Razer.Devices #180

njbmartin opened this issue Jun 21, 2016 · 5 comments
Labels
Milestone

Comments

@njbmartin
Copy link

njbmartin commented Jun 21, 2016

I've been thinking about this for a while now, but if you have a GUID of a device, it's quite difficult to get a string friendly version of the field name that matches.

This would be particularly useful for #145

This is literally just a rough thinking, and I know it won't be the right solution, but I've remedied this with:

/// <summary>
/// Blackwidow Chroma edition.
/// </summary>
[PublicAPI]
public static readonly Device Blackwidow = new Device
{
    Guid = new Guid(
    0x2ea1bb63,
    0xca28,
    0x428d,
    0x9f,
    0x06,
    0x19,
    0x6b,
    0x88,
    0x33,
    0x0b,
    0xbb),
    Name = "BlackWidow Chroma",
};

/// <summary>
/// Contains device IDs for devices that have Chroma support.
/// </summary>
public class Device
{
    /// <summary>
    /// Gets or sets friendly name of the device.
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// Gets or sets Guid for the device.
    /// </summary>
    public Guid Guid { get; set; }
}

I also know that this would be a major change, but I just wanted to see what you guys thought about this?

@njbmartin njbmartin added the Idea label Jun 21, 2016
@Sharparam Sharparam added this to the v6.0 milestone Jun 27, 2016
@Sharparam
Copy link
Member

A way to implement this that would require less drastic changes in the current usage of GUIDs could be to add the names as attributes on the Guid fields like this:

public static class Devices
{
  [DeviceName("Some Device")]
  public static readonly Guid SomeDevice = new Guid(/* ... */);
}

Thoughts?

@njbmartin
Copy link
Author

So, here's how you could get the DeviceName from the attribute, unless I'm missing something...

Set the name

        [PublicAPI]
        [DeviceName("BlackWidow")]
        public static readonly Guid Blackwidow = new Guid(
            0x2ea1bb63,
            0xca28,
            0x428d,
            0x9f,
            0x06,
            0x19,
            0x6b,
            0x88,
            0x33,
            0x0b,
            0xbb);

Create the attribute

    internal class DeviceNameAttribute : Attribute
    {
        private string DeviceName;

        public DeviceNameAttribute(string name)
        {
            this.DeviceName = name;
        }

        public string Name
        {
            get
            {
                return DeviceName;
            }
        }
    }

Retrieve the name from the Guid

        public static string GetName(Guid id)
        {
            var fields = typeof(Devices).GetFields();
            foreach (var field in fields)
            {
                if ((Guid)field.GetValue(null) == id)
                {
                    var attr = (DeviceNameAttribute)field.GetCustomAttributes(typeof(DeviceNameAttribute), false)[0];
                    return attr.Name;
                }
            }

            return null;
        }

@njbmartin
Copy link
Author

Custom attribute is useful if we want to give the full name i.e "BlackWidow Tournament Edition", but if we literally just want to find out the field name, we can do this without using a new attribute:

        public static string GetName(Guid id)
        {
            var fields = typeof(Devices).GetFields();
            foreach (var field in fields)
            {
                if ((Guid)field.GetValue(null) == id)
                {
                    return field.Name;
                }
            }

            return null;
        }

@Sharparam Sharparam modified the milestones: v6.0, v6.1 Mar 11, 2018
@Sharparam
Copy link
Member

Moving this to v6.1. I'm thinking adding two methods: GetName(Guid) and GetDisplayName(Guid) to the devices class. What are your thoughts @njbmartin?

@Sharparam
Copy link
Member

This was implemented early, in bd61119.

@Sharparam Sharparam modified the milestones: v6.1, v6.0 Mar 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants