MotionStorageManager.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using LitMotion.Collections;
  4. namespace LitMotion
  5. {
  6. internal static class MotionStorageManager
  7. {
  8. static FastListCore<IMotionStorage> storageList;
  9. public static int CurrentStorageId { get; private set; }
  10. public static void AddStorage<TValue, TOptions, TAdapter>(MotionStorage<TValue, TOptions, TAdapter> storage)
  11. where TValue : unmanaged
  12. where TOptions : unmanaged, IMotionOptions
  13. where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
  14. {
  15. storageList.Add(storage);
  16. CurrentStorageId++;
  17. }
  18. public static void CompleteMotion(MotionHandle handle)
  19. {
  20. CheckStorageId(handle);
  21. storageList[handle.StorageId].Complete(handle);
  22. }
  23. public static void CancelMotion(MotionHandle handle)
  24. {
  25. CheckStorageId(handle);
  26. storageList[handle.StorageId].Cancel(handle);
  27. }
  28. public static bool IsActive(MotionHandle handle)
  29. {
  30. if (handle.StorageId < 0 || handle.StorageId >= CurrentStorageId) return false;
  31. return storageList[handle.StorageId].IsActive(handle);
  32. }
  33. public static ref MotionDataCore GetMotionDataRef(MotionHandle handle)
  34. {
  35. CheckStorageId(handle);
  36. return ref storageList[handle.StorageId].GetDataRef(handle);
  37. }
  38. public static ref MotionCallbackData GetMotionCallbackDataRef(MotionHandle handle)
  39. {
  40. CheckStorageId(handle);
  41. return ref storageList[handle.StorageId].GetCallbackDataRef(handle);
  42. }
  43. // For MotionTracker
  44. public static (Type ValueType, Type OptionsType, Type AdapterType) GetMotionType(MotionHandle handle)
  45. {
  46. CheckStorageId(handle);
  47. var storageType = storageList[handle.StorageId].GetType();
  48. var valueType = storageType.GenericTypeArguments[0];
  49. var optionsType = storageType.GenericTypeArguments[1];
  50. var adapterType = storageType.GenericTypeArguments[2];
  51. return (valueType, optionsType, adapterType);
  52. }
  53. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  54. static void CheckStorageId(in MotionHandle handle)
  55. {
  56. if (handle.StorageId < 0 || handle.StorageId >= CurrentStorageId)
  57. throw new ArgumentException("Invalid storage id.");
  58. }
  59. }
  60. }