Mirror of Svelto.ECS because we're a fan of it
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

67 lignes
3.0KB

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