Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
3.1KB

  1. #if UNITY_NATIVE
  2. using System.Runtime.CompilerServices;
  3. using Svelto.Common;
  4. using Svelto.DataStructures;
  5. using Svelto.ECS.Internal;
  6. namespace Svelto.ECS
  7. {
  8. public static class UnityEntityDBExtensions
  9. {
  10. internal static NativeEGIDMapper<T> ToNativeEGIDMapper<T>(this TypeSafeDictionary<T> dic,
  11. ExclusiveGroupStruct groupStructId) where T : unmanaged, IEntityComponent
  12. {
  13. var mapper = new NativeEGIDMapper<T>(groupStructId, dic.implUnmgd);
  14. return mapper;
  15. }
  16. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  17. public static NativeEGIDMapper<T> QueryNativeMappedEntities<T>(this EntitiesDB entitiesDb, ExclusiveGroupStruct groupStructId)
  18. where T : unmanaged, IEntityComponent
  19. {
  20. if (entitiesDb.SafeQueryEntityDictionary<T>(groupStructId, out var typeSafeDictionary) == false)
  21. throw new EntityGroupNotFoundException(typeof(T), groupStructId.ToName());
  22. return (typeSafeDictionary as TypeSafeDictionary<T>).ToNativeEGIDMapper(groupStructId);
  23. }
  24. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  25. public static bool TryQueryNativeMappedEntities<T>(this EntitiesDB entitiesDb, ExclusiveGroupStruct groupStructId,
  26. out NativeEGIDMapper<T> mapper)
  27. where T : unmanaged, IEntityComponent
  28. {
  29. mapper = default;
  30. if (entitiesDb.SafeQueryEntityDictionary<T>(groupStructId, out var typeSafeDictionary) == false ||
  31. typeSafeDictionary.count == 0)
  32. return false;
  33. mapper = (typeSafeDictionary as TypeSafeDictionary<T>).ToNativeEGIDMapper(groupStructId);
  34. return true;
  35. }
  36. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  37. public static NativeEGIDMultiMapper<T> QueryNativeMappedEntities<T>(this EntitiesDB entitiesDb,
  38. LocalFasterReadOnlyList<ExclusiveGroupStruct> groups)
  39. where T : unmanaged, IEntityComponent
  40. {
  41. var dictionary =
  42. new SveltoDictionary<ExclusiveGroupStruct, SveltoDictionary<uint, T,
  43. NativeStrategy<SveltoDictionaryNode<uint>>,
  44. NativeStrategy<T>,
  45. NativeStrategy<int>>,
  46. NativeStrategy<SveltoDictionaryNode<ExclusiveGroupStruct>>,
  47. NativeStrategy<SveltoDictionary<uint, T,
  48. NativeStrategy<SveltoDictionaryNode<uint>>,
  49. NativeStrategy<T>,
  50. NativeStrategy<int>>>,
  51. NativeStrategy<int>>
  52. ((uint) groups.count, Allocator.TempJob);
  53. foreach (var group in groups)
  54. {
  55. if (entitiesDb.SafeQueryEntityDictionary<T>(group, out var typeSafeDictionary) == true)
  56. if (typeSafeDictionary.count > 0)
  57. dictionary.Add(group, ((TypeSafeDictionary<T>)typeSafeDictionary).implUnmgd);
  58. }
  59. return new NativeEGIDMultiMapper<T>(dictionary);
  60. }
  61. }
  62. }
  63. #endif