Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

FourCC.cs 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. namespace UnityEngine.InputSystem.Utilities
  4. {
  5. /// <summary>
  6. /// A four-character code.
  7. /// </summary>
  8. /// <remarks>
  9. /// A four-character code is a struct containing four byte characters totalling a single <c>int</c>.
  10. /// FourCCs are frequently used in the input system to identify the format of data sent to or from
  11. /// the native backend representing events, input device state or commands sent to input devices.
  12. /// </remarks>
  13. public struct FourCC : IEquatable<FourCC>
  14. {
  15. private int m_Code;
  16. /// <summary>
  17. /// Create a FourCC from the given integer.
  18. /// </summary>
  19. /// <param name="code">FourCC code represented as an <c>int</c>. Character order is
  20. /// little endian. "ABCD" is stored with A in the highest order 8 bits and D in the
  21. /// lowest order 8 bits.</param>
  22. /// <remarks>
  23. /// This method does not actually verify whether the four characters in the code
  24. /// are printable.
  25. /// </remarks>
  26. public FourCC(int code)
  27. {
  28. m_Code = code;
  29. }
  30. /// <summary>
  31. /// Create a FourCC from the given four characters.
  32. /// </summary>
  33. /// <param name="a">First character.</param>
  34. /// <param name="b">Second character.</param>
  35. /// <param name="c">Third character.</param>
  36. /// <param name="d">Fourth character.</param>
  37. public FourCC(char a, char b = ' ', char c = ' ', char d = ' ')
  38. {
  39. m_Code = (a << 24) | (b << 16) | (c << 8) | d;
  40. }
  41. /// <summary>
  42. /// Create a FourCC from the given string.
  43. /// </summary>
  44. /// <param name="str">A string with four characters or less but with at least one character.</param>
  45. /// <exception cref="ArgumentException"><paramref name="str"/> is empty or has more than four characters.</exception>
  46. /// <exception cref="ArgumentNullException"><paramref name="str"/> is <c>null</c>.</exception>
  47. public FourCC(string str)
  48. : this()
  49. {
  50. if (str == null)
  51. throw new ArgumentNullException(nameof(str));
  52. var length = str.Length;
  53. if (length < 1 || length > 4)
  54. throw new ArgumentException("FourCC string must be one to four characters long!", nameof(str));
  55. var a = str[0];
  56. var b = length > 1 ? str[1] : ' ';
  57. var c = length > 2 ? str[2] : ' ';
  58. var d = length > 3 ? str[3] : ' ';
  59. m_Code = (a << 24) | (b << 16) | (c << 8) | d;
  60. }
  61. /// <summary>
  62. /// Convert the given FourCC into an <c>int</c>.
  63. /// </summary>
  64. /// <param name="fourCC">A FourCC.</param>
  65. /// <returns>The four characters of the code packed into one <c>int</c>. Character order is
  66. /// little endian. "ABCD" is stored with A in the highest order 8 bits and D in the
  67. /// lowest order 8 bits.</returns>
  68. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  69. public static implicit operator int(FourCC fourCC)
  70. {
  71. return fourCC.m_Code;
  72. }
  73. /// <summary>
  74. /// Convert the given <c>int</c> into a FourCC.
  75. /// </summary>
  76. /// <param name="i">FourCC code represented as an <c>int</c>. Character order is
  77. /// little endian. "ABCD" is stored with A in the highest order 8 bits and D in the
  78. /// lowest order 8 bits.</param>
  79. /// <returns>The FourCC converted from <paramref name="i"/>.</returns>
  80. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  81. public static implicit operator FourCC(int i)
  82. {
  83. return new FourCC(i);
  84. }
  85. /// <summary>
  86. /// Convert the FourCC into a string in the form of "ABCD".
  87. /// </summary>
  88. /// <returns>String representation of the FourCC.</returns>
  89. public override string ToString()
  90. {
  91. return
  92. $"{(char) (m_Code >> 24)}{(char) ((m_Code & 0xff0000) >> 16)}{(char) ((m_Code & 0xff00) >> 8)}{(char) (m_Code & 0xff)}";
  93. }
  94. /// <summary>
  95. /// Compare two FourCCs for equality.
  96. /// </summary>
  97. /// <param name="other">Another FourCC.</param>
  98. /// <returns>True if the two FourCCs are equal, i.e. have the same exact
  99. /// character codes.</returns>
  100. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  101. public bool Equals(FourCC other)
  102. {
  103. return m_Code == other.m_Code;
  104. }
  105. /// <summary>
  106. /// Compare the FourCC to the given object.
  107. /// </summary>
  108. /// <param name="obj">An object. Can be null.</param>
  109. /// <returns>True if <paramref name="obj"/> is a FourCC that has the same
  110. /// character code sequence.</returns>
  111. public override bool Equals(object obj)
  112. {
  113. if (ReferenceEquals(null, obj))
  114. return false;
  115. return obj is FourCC cc && Equals(cc);
  116. }
  117. /// <summary>
  118. /// Compute a hash code for the FourCC.
  119. /// </summary>
  120. /// <returns>Simply returns the FourCC converted to an <c>int</c>.</returns>
  121. public override int GetHashCode()
  122. {
  123. return m_Code;
  124. }
  125. /// <summary>
  126. /// Compare two FourCCs for equality.
  127. /// </summary>
  128. /// <param name="left">First FourCC.</param>
  129. /// <param name="right">Second FourCC.</param>
  130. /// <returns>True if the two FourCCs are equal, i.e. have the same exact
  131. /// character codes.</returns>
  132. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  133. public static bool operator==(FourCC left, FourCC right)
  134. {
  135. return left.m_Code == right.m_Code;
  136. }
  137. /// <summary>
  138. /// Compare two FourCCs for inequality.
  139. /// </summary>
  140. /// <param name="left">First FourCC.</param>
  141. /// <param name="right">Second FourCC.</param>
  142. /// <returns>True if the two FourCCs are not equal, i.e. do not have the same exact
  143. /// character codes.</returns>
  144. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  145. public static bool operator!=(FourCC left, FourCC right)
  146. {
  147. return left.m_Code != right.m_Code;
  148. }
  149. // Make annoying Microsoft code analyzer happy.
  150. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  151. public static FourCC FromInt32(int i)
  152. {
  153. return i;
  154. }
  155. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  156. public static int ToInt32(FourCC fourCC)
  157. {
  158. return fourCC.m_Code;
  159. }
  160. }
  161. }