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.

67 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.Native
  7. {
  8. public static class UnityNativeEntityDBExtensions
  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, Allocator allocator)
  39. where T : unmanaged, IEntityComponent
  40. {
  41. var dictionary = new SveltoDictionary<ExclusiveGroupStruct, //key
  42. SveltoDictionary<uint, T,
  43. NativeStrategy<SveltoDictionaryNode<uint>>, NativeStrategy<T>, NativeStrategy<int>>, //value
  44. NativeStrategy<SveltoDictionaryNode<ExclusiveGroupStruct>>, //strategy to store the key
  45. NativeStrategy<SveltoDictionary<uint, T, NativeStrategy<SveltoDictionaryNode<uint>>, NativeStrategy<T>, NativeStrategy<int>>>, NativeStrategy<int>> //strategy to store the value
  46. ((uint) groups.count, allocator);
  47. foreach (var group in groups)
  48. {
  49. if (entitiesDb.SafeQueryEntityDictionary<T>(group, out var typeSafeDictionary) == true)
  50. if (typeSafeDictionary.count > 0)
  51. dictionary.Add(group, ((TypeSafeDictionary<T>)typeSafeDictionary).implUnmgd);
  52. }
  53. return new NativeEGIDMultiMapper<T>(dictionary);
  54. }
  55. }
  56. }
  57. #endif