// dnlib: See LICENSE.txt for more info
using System;
namespace dnlib.W32Resources {
///
/// A Win32 resource name. It can be either an integer or a string.
///
public readonly struct ResourceName : IComparable, IEquatable {
readonly int id;
readonly string name;
///
/// true if is valid
///
public bool HasId => name is null;
///
/// true if is valid
///
public bool HasName => name is not null;
///
/// The ID. It's only valid if is true
///
public int Id => id;
///
/// The name. It's only valid if is true
///
public string Name => name;
///
/// Constructor
///
/// ID
public ResourceName(int id) {
this.id = id;
name = null;
}
///
/// Constructor
///
/// Name
public ResourceName(string name) {
id = 0;
this.name = name;
}
/// Converts input to a
public static implicit operator ResourceName(int id) => new ResourceName(id);
/// Converts input to a
public static implicit operator ResourceName(string name) => new ResourceName(name);
/// Overloaded operator
public static bool operator <(ResourceName left, ResourceName right) => left.CompareTo(right) < 0;
/// Overloaded operator
public static bool operator <=(ResourceName left, ResourceName right) => left.CompareTo(right) <= 0;
/// Overloaded operator
public static bool operator >(ResourceName left, ResourceName right) => left.CompareTo(right) > 0;
/// Overloaded operator
public static bool operator >=(ResourceName left, ResourceName right) => left.CompareTo(right) >= 0;
/// Overloaded operator
public static bool operator ==(ResourceName left, ResourceName right) => left.Equals(right);
/// Overloaded operator
public static bool operator !=(ResourceName left, ResourceName right) => !left.Equals(right);
///
public int CompareTo(ResourceName other) {
if (HasId != other.HasId) {
// Sort names before ids
return HasName ? -1 : 1;
}
if (HasId)
return id.CompareTo(other.id);
else
return name.ToUpperInvariant().CompareTo(other.name.ToUpperInvariant());
}
///
public bool Equals(ResourceName other) => CompareTo(other) == 0;
///
public override bool Equals(object obj) {
if (!(obj is ResourceName))
return false;
return Equals((ResourceName)obj);
}
///
public override int GetHashCode() {
if (HasId)
return id;
return name.GetHashCode();
}
///
public override string ToString() => HasId ? id.ToString() : name;
}
}