using System; using System.Runtime.CompilerServices; namespace UnityEngine.InputSystem.Utilities { /// /// A four-character code. /// /// /// A four-character code is a struct containing four byte characters totalling a single int. /// FourCCs are frequently used in the input system to identify the format of data sent to or from /// the native backend representing events, input device state or commands sent to input devices. /// public struct FourCC : IEquatable { private int m_Code; /// /// Create a FourCC from the given integer. /// /// FourCC code represented as an int. Character order is /// little endian. "ABCD" is stored with A in the highest order 8 bits and D in the /// lowest order 8 bits. /// /// This method does not actually verify whether the four characters in the code /// are printable. /// public FourCC(int code) { m_Code = code; } /// /// Create a FourCC from the given four characters. /// /// First character. /// Second character. /// Third character. /// Fourth character. public FourCC(char a, char b = ' ', char c = ' ', char d = ' ') { m_Code = (a << 24) | (b << 16) | (c << 8) | d; } /// /// Create a FourCC from the given string. /// /// A string with four characters or less but with at least one character. /// is empty or has more than four characters. /// is null. public FourCC(string str) : this() { if (str == null) throw new ArgumentNullException(nameof(str)); var length = str.Length; if (length < 1 || length > 4) throw new ArgumentException("FourCC string must be one to four characters long!", nameof(str)); var a = str[0]; var b = length > 1 ? str[1] : ' '; var c = length > 2 ? str[2] : ' '; var d = length > 3 ? str[3] : ' '; m_Code = (a << 24) | (b << 16) | (c << 8) | d; } /// /// Convert the given FourCC into an int. /// /// A FourCC. /// The four characters of the code packed into one int. Character order is /// little endian. "ABCD" is stored with A in the highest order 8 bits and D in the /// lowest order 8 bits. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static implicit operator int(FourCC fourCC) { return fourCC.m_Code; } /// /// Convert the given int into a FourCC. /// /// FourCC code represented as an int. Character order is /// little endian. "ABCD" is stored with A in the highest order 8 bits and D in the /// lowest order 8 bits. /// The FourCC converted from . [MethodImpl(MethodImplOptions.AggressiveInlining)] public static implicit operator FourCC(int i) { return new FourCC(i); } /// /// Convert the FourCC into a string in the form of "ABCD". /// /// String representation of the FourCC. public override string ToString() { return $"{(char) (m_Code >> 24)}{(char) ((m_Code & 0xff0000) >> 16)}{(char) ((m_Code & 0xff00) >> 8)}{(char) (m_Code & 0xff)}"; } /// /// Compare two FourCCs for equality. /// /// Another FourCC. /// True if the two FourCCs are equal, i.e. have the same exact /// character codes. [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Equals(FourCC other) { return m_Code == other.m_Code; } /// /// Compare the FourCC to the given object. /// /// An object. Can be null. /// True if is a FourCC that has the same /// character code sequence. public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) return false; return obj is FourCC cc && Equals(cc); } /// /// Compute a hash code for the FourCC. /// /// Simply returns the FourCC converted to an int. public override int GetHashCode() { return m_Code; } /// /// Compare two FourCCs for equality. /// /// First FourCC. /// Second FourCC. /// True if the two FourCCs are equal, i.e. have the same exact /// character codes. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator==(FourCC left, FourCC right) { return left.m_Code == right.m_Code; } /// /// Compare two FourCCs for inequality. /// /// First FourCC. /// Second FourCC. /// True if the two FourCCs are not equal, i.e. do not have the same exact /// character codes. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator!=(FourCC left, FourCC right) { return left.m_Code != right.m_Code; } // Make annoying Microsoft code analyzer happy. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static FourCC FromInt32(int i) { return i; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int ToInt32(FourCC fourCC) { return fourCC.m_Code; } } }