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.0KB

  1. using System.Runtime.CompilerServices;
  2. using Svelto.Common;
  3. using Svelto.DataStructures;
  4. using Svelto.DataStructures.Native;
  5. using Svelto.ECS.DataStructures;
  6. using Svelto.ECS.Internal;
  7. namespace Svelto.ECS.Native
  8. {
  9. public static class UnityNativeEntityDBExtensions
  10. {
  11. static NativeEGIDMapper<T> ToNativeEGIDMapper<T>(this UnmanagedTypeSafeDictionary<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 UnmanagedTypeSafeDictionary<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 = NativeEGIDMapper<T>.empty;
  31. if (entitiesDb.SafeQueryEntityDictionary<T>(groupStructId, out var typeSafeDictionary) == false ||
  32. typeSafeDictionary.count == 0)
  33. return false;
  34. mapper = (typeSafeDictionary as UnmanagedTypeSafeDictionary<T>).ToNativeEGIDMapper(groupStructId);
  35. return true;
  36. }
  37. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  38. ///Note: if I use a SharedNativeSveltoDictionary for implUnmg, I may be able to cache NativeEGIDMultiMapper
  39. /// and reuse it
  40. /// TODO: the ability to retain a NativeEGIDMultiMapper thanks to the use of shareable arrays
  41. /// must be unit tested!
  42. public static NativeEGIDMultiMapper<T> QueryNativeMappedEntities<T>(this EntitiesDB entitiesDb,
  43. LocalFasterReadOnlyList<ExclusiveGroupStruct> groups, Allocator allocator)
  44. where T : unmanaged, IBaseEntityComponent
  45. {
  46. var dictionary = new SveltoDictionaryNative<ExclusiveGroupStruct, SharedSveltoDictionaryNative<uint, T>>
  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, ((UnmanagedTypeSafeDictionary<T>)typeSafeDictionary).implUnmgd);
  53. }
  54. return new NativeEGIDMultiMapper<T>(dictionary);
  55. }
  56. }
  57. }