暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

QueryPairedUserAccountCommand.cs 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using UnityEngine.InputSystem.Utilities;
  4. namespace UnityEngine.InputSystem.LowLevel
  5. {
  6. /// <summary>
  7. /// Query the ID and the name of the user paired to the device the command is sent to.
  8. /// </summary>
  9. /// <remarks>
  10. /// This command is only supported on platforms where devices can be paired to user accounts
  11. /// at the platform level. Currently this is the case for Xbox and PS4. On Switch, <see
  12. /// cref="InitiateUserAccountPairingCommand"/> is supported but the platform does not store
  13. /// associations established between devices and users that way.
  14. /// </remarks>
  15. [StructLayout(LayoutKind.Explicit, Size = kSize)]
  16. public unsafe struct QueryPairedUserAccountCommand : IInputDeviceCommandInfo
  17. {
  18. public static FourCC Type => new FourCC('P', 'A', 'C', 'C');
  19. internal const int kMaxNameLength = 256;
  20. internal const int kMaxIdLength = 256;
  21. ////REVIEW: is this too heavy to allocate on the stack?
  22. internal const int kSize = InputDeviceCommand.kBaseCommandSize + 8 + kMaxNameLength * 2 + kMaxIdLength * 2;
  23. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1714:FlagsEnumsShouldHavePluralNames", Justification = "`Result` matches other command result names")]
  24. [Flags]
  25. public enum Result : long
  26. {
  27. // Leave bit #0 unused so as to not lead to possible confusion with GenericSuccess.
  28. /// <summary>
  29. /// The device is currently paired to a user account.
  30. /// </summary>
  31. DevicePairedToUserAccount = 1 << 1,
  32. /// <summary>
  33. /// The system is currently displaying a prompt for the user to select an account to
  34. /// use the device with.
  35. /// </summary>
  36. UserAccountSelectionInProgress = 1 << 2,
  37. /// <summary>
  38. /// User account selection completed.
  39. /// </summary>
  40. UserAccountSelectionComplete = 1 << 3,
  41. /// <summary>
  42. /// The system had been displaying a prompt
  43. /// </summary>
  44. UserAccountSelectionCanceled = 1 << 4,
  45. }
  46. [FieldOffset(0)]
  47. public InputDeviceCommand baseCommand;
  48. /// <summary>
  49. /// Handle of the user account at the platform level.
  50. /// </summary>
  51. /// <remarks>
  52. /// Note that this is wide enough to store a pointer and does not necessarily need to be a plain integer.
  53. /// How the backend determines handles for user accounts is up to the backend.
  54. ///
  55. /// Be aware that a handle is not guaranteed to be valid beyond the current application run. For stable,
  56. /// persistent user account handles,use <see cref="id"/>.
  57. /// </remarks>
  58. [FieldOffset(InputDeviceCommand.kBaseCommandSize)]
  59. public ulong handle;
  60. [FieldOffset(InputDeviceCommand.kBaseCommandSize + 8)]
  61. internal fixed byte nameBuffer[kMaxNameLength * 2];
  62. [FieldOffset(InputDeviceCommand.kBaseCommandSize + 8 + kMaxNameLength * 2)]
  63. internal fixed byte idBuffer[kMaxNameLength * 2];
  64. /// <summary>
  65. /// Persistent ID of the user account the platform level.
  66. /// </summary>
  67. /// <remarks>
  68. /// This ID is guaranteed to not change between application runs, device restarts, and the user
  69. /// changing user names on the account.
  70. ///
  71. /// Use this ID to associate persistent settings with.
  72. /// </remarks>
  73. public string id
  74. {
  75. get
  76. {
  77. fixed(byte* idBufferPtr = idBuffer)
  78. return StringHelpers.ReadStringFromBuffer(new IntPtr(idBufferPtr), kMaxIdLength);
  79. }
  80. set
  81. {
  82. if (value == null)
  83. throw new ArgumentNullException(nameof(value));
  84. var length = value.Length;
  85. if (length > kMaxIdLength)
  86. throw new ArgumentException($"ID '{value}' exceeds maximum supported length of {kMaxIdLength} characters", nameof(value));
  87. fixed(byte* idBufferPtr = idBuffer)
  88. {
  89. StringHelpers.WriteStringToBuffer(value, new IntPtr(idBufferPtr), kMaxIdLength);
  90. }
  91. }
  92. }
  93. /// <summary>
  94. /// Name of the user account at the platform level.
  95. /// </summary>
  96. public string name
  97. {
  98. get
  99. {
  100. fixed(byte* nameBufferPtr = nameBuffer)
  101. return StringHelpers.ReadStringFromBuffer(new IntPtr(nameBufferPtr), kMaxNameLength);
  102. }
  103. set
  104. {
  105. if (value == null)
  106. throw new ArgumentNullException("value");
  107. var length = value.Length;
  108. if (length > kMaxNameLength)
  109. throw new ArgumentException($"Name '{value}' exceeds maximum supported length of {kMaxNameLength} characters", nameof(value));
  110. fixed(byte* nameBufferPtr = nameBuffer)
  111. {
  112. StringHelpers.WriteStringToBuffer(value, new IntPtr(nameBufferPtr), kMaxNameLength);
  113. }
  114. }
  115. }
  116. public FourCC typeStatic => Type;
  117. public static QueryPairedUserAccountCommand Create()
  118. {
  119. return new QueryPairedUserAccountCommand
  120. {
  121. baseCommand = new InputDeviceCommand(Type, kSize),
  122. };
  123. }
  124. }
  125. }