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.

68 lines
3.2KB

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