File size: 17,553 Bytes
18a519f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 | using System;
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine.InputSystem.LowLevel;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine.InputSystem.Layouts;
using UnityEngine.InputSystem.Utilities;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace UnityEngine.InputSystem
{
/// <summary>
/// An implementation of <see cref="IInputRuntime"/> for use during tests.
/// </summary>
/// <remarks>
/// This class is only available in the editor and in development players.
///
/// The test runtime replaces the services usually supplied by <see cref="UnityEngineInternal.Input.NativeInputSystem"/>.
/// </remarks>
/// <seealso cref="InputTestFixture.runtime"/>
internal class InputTestRuntime : IInputRuntime, IDisposable
{
public unsafe delegate long DeviceCommandCallback(int deviceId, InputDeviceCommand* command);
~InputTestRuntime()
{
Dispose();
}
public int AllocateDeviceId()
{
var result = m_NextDeviceId;
++m_NextDeviceId;
return result;
}
public unsafe void Update(InputUpdateType type)
{
if (!onShouldRunUpdate.Invoke(type))
return;
lock (m_Lock)
{
if (type == InputUpdateType.Dynamic && !dontAdvanceUnscaledGameTimeNextDynamicUpdate)
{
unscaledGameTime += 1 / 30f;
dontAdvanceUnscaledGameTimeNextDynamicUpdate = false;
}
if (m_NewDeviceDiscoveries != null && m_NewDeviceDiscoveries.Count > 0)
{
if (onDeviceDiscovered != null)
foreach (var entry in m_NewDeviceDiscoveries)
onDeviceDiscovered(entry.Key, entry.Value);
m_NewDeviceDiscoveries.Clear();
}
onBeforeUpdate?.Invoke(type);
// Advance time *after* onBeforeUpdate so that events generated from onBeforeUpdate
// don't get bumped into the following update.
if (type == InputUpdateType.Dynamic && !dontAdvanceTimeNextDynamicUpdate)
{
currentTime += advanceTimeEachDynamicUpdate;
dontAdvanceTimeNextDynamicUpdate = false;
}
if (onUpdate != null)
{
var buffer = new InputEventBuffer(
(InputEvent*)NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(m_EventBuffer),
m_EventCount, m_EventWritePosition, m_EventBuffer.Length);
try
{
onUpdate(type, ref buffer);
}
catch (Exception e)
{
// Same order as in NativeInputRuntime
Debug.LogException(e);
Debug.LogError($"{e.GetType().Name} during event processing of {type} update; resetting event buffer");
// Rethrow exception for test runtime to enable us to assert against it in tests.
m_EventCount = 0;
m_EventWritePosition = 0;
throw;
}
m_EventCount = buffer.eventCount;
m_EventWritePosition = (int)buffer.sizeInBytes;
if (NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(buffer.data) !=
NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(m_EventBuffer))
m_EventBuffer = buffer.data;
}
else
{
m_EventCount = 0;
m_EventWritePosition = 0;
}
}
}
public unsafe void QueueEvent(InputEvent* eventPtr)
{
var eventSize = eventPtr->sizeInBytes;
var alignedEventSize = eventSize.AlignToMultipleOf(4);
lock (m_Lock)
{
eventPtr->eventId = m_NextEventId;
eventPtr->handled = false;
++m_NextEventId;
// Enlarge buffer, if we have to.
if ((m_EventWritePosition + alignedEventSize) > m_EventBuffer.Length)
{
var newBufferSize = m_EventBuffer.Length + Mathf.Max((int)alignedEventSize, 1024);
var newBuffer = new NativeArray<byte>(newBufferSize, Allocator.Persistent);
UnsafeUtility.MemCpy(newBuffer.GetUnsafePtr(), m_EventBuffer.GetUnsafePtr(), m_EventWritePosition);
m_EventBuffer.Dispose();
m_EventBuffer = newBuffer;
}
// Copy event.
UnsafeUtility.MemCpy((byte*)m_EventBuffer.GetUnsafePtr() + m_EventWritePosition, eventPtr, eventSize);
m_EventWritePosition += (int)alignedEventSize;
++m_EventCount;
}
}
public unsafe void SetCanRunInBackground(int deviceId)
{
SetDeviceCommandCallback(deviceId,
(id, command) =>
{
if (command->type == QueryCanRunInBackground.Type)
{
((QueryCanRunInBackground*)command)->canRunInBackground = true;
return InputDeviceCommand.GenericSuccess;
}
return InputDeviceCommand.GenericFailure;
});
}
public void SetDeviceCommandCallback(InputDevice device, DeviceCommandCallback callback)
{
SetDeviceCommandCallback(device.deviceId, callback);
}
public void SetDeviceCommandCallback(int deviceId, DeviceCommandCallback callback)
{
lock (m_Lock)
{
if (m_DeviceCommandCallbacks == null)
m_DeviceCommandCallbacks = new List<KeyValuePair<int, DeviceCommandCallback>>();
else
{
for (var i = 0; i < m_DeviceCommandCallbacks.Count; ++i)
{
if (m_DeviceCommandCallbacks[i].Key == deviceId)
{
m_DeviceCommandCallbacks[i] = new KeyValuePair<int, DeviceCommandCallback>(deviceId, callback);
return;
}
}
}
m_DeviceCommandCallbacks.Add(new KeyValuePair<int, DeviceCommandCallback>(deviceId, callback));
}
}
public void SetDeviceCommandCallback<TCommand>(int deviceId, TCommand result)
where TCommand : struct, IInputDeviceCommandInfo
{
bool? receivedCommand = null;
unsafe
{
SetDeviceCommandCallback(deviceId,
(id, commandPtr) =>
{
if (commandPtr->type == result.typeStatic)
{
Assert.That(receivedCommand.HasValue, Is.False);
receivedCommand = true;
UnsafeUtility.MemCpy(commandPtr, UnsafeUtility.AddressOf(ref result),
UnsafeUtility.SizeOf<TCommand>());
return InputDeviceCommand.GenericSuccess;
}
return InputDeviceCommand.GenericFailure;
});
}
}
public unsafe long DeviceCommand(int deviceId, InputDeviceCommand* commandPtr)
{
lock (m_Lock)
{
if (commandPtr->type == QueryPairedUserAccountCommand.Type)
{
foreach (var pairing in userAccountPairings)
{
if (pairing.deviceId != deviceId)
continue;
var queryPairedUser = (QueryPairedUserAccountCommand*)commandPtr;
queryPairedUser->handle = pairing.userHandle;
queryPairedUser->name = pairing.userName;
queryPairedUser->id = pairing.userId;
return (long)QueryPairedUserAccountCommand.Result.DevicePairedToUserAccount;
}
}
var result = InputDeviceCommand.GenericFailure;
if (m_DeviceCommandCallbacks != null)
foreach (var entry in m_DeviceCommandCallbacks)
{
if (entry.Key == deviceId)
{
result = entry.Value(deviceId, commandPtr);
if (result >= 0)
return result;
}
}
return result;
}
}
public void InvokePlayerFocusChanged(bool newFocusState)
{
m_HasFocus = newFocusState;
onPlayerFocusChanged?.Invoke(newFocusState);
}
public void PlayerFocusLost()
{
InvokePlayerFocusChanged(false);
}
public void PlayerFocusGained()
{
InvokePlayerFocusChanged(true);
}
public int ReportNewInputDevice(string deviceDescriptor, int deviceId = InputDevice.InvalidDeviceId)
{
lock (m_Lock)
{
if (deviceId == InputDevice.InvalidDeviceId)
deviceId = AllocateDeviceId();
if (m_NewDeviceDiscoveries == null)
m_NewDeviceDiscoveries = new List<KeyValuePair<int, string>>();
m_NewDeviceDiscoveries.Add(new KeyValuePair<int, string>(deviceId, deviceDescriptor));
return deviceId;
}
}
public int ReportNewInputDevice(InputDeviceDescription description, int deviceId = InputDevice.InvalidDeviceId,
ulong userHandle = 0, string userName = null, string userId = null)
{
deviceId = ReportNewInputDevice(description.ToJson(), deviceId);
// If we have user information, automatically set up
if (userHandle != 0)
AssociateInputDeviceWithUser(deviceId, userHandle, userName, userId);
return deviceId;
}
public int ReportNewInputDevice<TDevice>(int deviceId = InputDevice.InvalidDeviceId,
ulong userHandle = 0, string userName = null, string userId = null)
where TDevice : InputDevice
{
return ReportNewInputDevice(
new InputDeviceDescription {deviceClass = typeof(TDevice).Name, interfaceName = "Test"}, deviceId,
userHandle, userName, userId);
}
public unsafe void ReportInputDeviceRemoved(int deviceId)
{
var removeEvent = DeviceRemoveEvent.Create(deviceId);
var removeEventPtr = UnsafeUtility.AddressOf(ref removeEvent);
QueueEvent((InputEvent*)removeEventPtr);
}
public void ReportInputDeviceRemoved(InputDevice device)
{
if (device == null)
throw new ArgumentNullException(nameof(device));
ReportInputDeviceRemoved(device.deviceId);
}
public void AssociateInputDeviceWithUser(int deviceId, ulong userHandle, string userName = null, string userId = null)
{
var existingIndex = -1;
for (var i = 0; i < userAccountPairings.Count; ++i)
if (userAccountPairings[i].deviceId == deviceId)
{
existingIndex = i;
break;
}
if (userHandle == 0)
{
if (existingIndex != -1)
userAccountPairings.RemoveAt(existingIndex);
}
else if (existingIndex != -1)
{
userAccountPairings[existingIndex] =
new PairedUser
{
deviceId = deviceId,
userHandle = userHandle,
userName = userName,
userId = userId,
};
}
else
{
userAccountPairings.Add(
new PairedUser
{
deviceId = deviceId,
userHandle = userHandle,
userName = userName,
userId = userId,
});
}
}
public void AssociateInputDeviceWithUser(InputDevice device, ulong userHandle, string userName = null, string userId = null)
{
AssociateInputDeviceWithUser(device.deviceId, userHandle, userName, userId);
}
public struct PairedUser
{
public int deviceId;
public ulong userHandle;
public string userName;
public string userId;
}
public InputUpdateDelegate onUpdate { get; set; }
public Action<InputUpdateType> onBeforeUpdate { get; set; }
public Func<InputUpdateType, bool> onShouldRunUpdate { get; set; }
#if UNITY_EDITOR
public Action onPlayerLoopInitialization { get; set; }
#endif
public Action<int, string> onDeviceDiscovered { get; set; }
public Action onShutdown { get; set; }
public Action<bool> onPlayerFocusChanged { get; set; }
public bool isPlayerFocused => m_HasFocus;
public float pollingFrequency { get; set; }
public double currentTime { get; set; }
public double currentTimeForFixedUpdate { get; set; }
public float unscaledGameTime { get; set; } = 1;
public bool dontAdvanceUnscaledGameTimeNextDynamicUpdate { get; set; }
public double advanceTimeEachDynamicUpdate { get; set; } = 1.0 / 60;
public bool dontAdvanceTimeNextDynamicUpdate { get; set; }
public bool runInBackground { get; set; } = false;
public Vector2 screenSize { get; set; } = new Vector2(1024, 768);
public ScreenOrientation screenOrientation { set; get; } = ScreenOrientation.Portrait;
public List<PairedUser> userAccountPairings
{
get
{
if (m_UserPairings == null)
m_UserPairings = new List<PairedUser>();
return m_UserPairings;
}
}
public void Dispose()
{
m_EventBuffer.Dispose();
GC.SuppressFinalize(this);
}
public double currentTimeOffsetToRealtimeSinceStartup
{
get => m_CurrentTimeOffsetToRealtimeSinceStartup;
set
{
m_CurrentTimeOffsetToRealtimeSinceStartup = value;
InputRuntime.s_CurrentTimeOffsetToRealtimeSinceStartup = value;
}
}
public bool isInBatchMode { get; set; }
#if UNITY_EDITOR
public bool isInPlayMode { get; set; } = true;
public bool isPaused { get; set; }
public bool isEditorActive { get; set; } = true;
public Func<IntPtr, bool> onUnityRemoteMessage
{
get => m_UnityRemoteMessageHandler;
set => m_UnityRemoteMessageHandler = value;
}
public bool? unityRemoteGyroEnabled;
public float? unityRemoteGyroUpdateInterval;
public void SetUnityRemoteGyroEnabled(bool value)
{
unityRemoteGyroEnabled = value;
}
public void SetUnityRemoteGyroUpdateInterval(float interval)
{
unityRemoteGyroUpdateInterval = interval;
}
public Action<PlayModeStateChange> onPlayModeChanged { get; set; }
public Action onProjectChange { get; set; }
#endif
public int eventCount => m_EventCount;
internal const int kDefaultEventBufferSize = 1024 * 512;
private bool m_HasFocus = true;
private int m_NextDeviceId = 1;
private int m_NextEventId = 1;
internal int m_EventCount;
private int m_EventWritePosition;
private NativeArray<byte> m_EventBuffer = new NativeArray<byte>(kDefaultEventBufferSize, Allocator.Persistent);
private List<PairedUser> m_UserPairings;
private List<KeyValuePair<int, string>> m_NewDeviceDiscoveries;
private List<KeyValuePair<int, DeviceCommandCallback>> m_DeviceCommandCallbacks;
private object m_Lock = new object();
private double m_CurrentTimeOffsetToRealtimeSinceStartup;
private Func<IntPtr, bool> m_UnityRemoteMessageHandler;
#if UNITY_ANALYTICS || UNITY_EDITOR
public Action<string, int, int> onRegisterAnalyticsEvent { get; set; }
public Action<string, object> onSendAnalyticsEvent { get; set; }
public void RegisterAnalyticsEvent(string name, int maxPerHour, int maxPropertiesPerEvent)
{
onRegisterAnalyticsEvent?.Invoke(name, maxPerHour, maxPropertiesPerEvent);
}
public void SendAnalyticsEvent(string name, object data)
{
onSendAnalyticsEvent?.Invoke(name, data);
}
#endif // UNITY_ANALYTICS || UNITY_EDITOR
}
}
|