Mirror of Svelto.ECS because we're a fan of it
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

72 строки
3.4KB

  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 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 = NativeEGIDMapper<T>.empty;
  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. ///Note: if I use a SharedNativeSveltoDictionary for implUnmg, I may be able to cache NativeEGIDMultiMapper
  39. /// and reuse it
  40. public static NativeEGIDMultiMapper<T> QueryNativeMappedEntities<T>(this EntitiesDB entitiesDb,
  41. LocalFasterReadOnlyList<ExclusiveGroupStruct> groups, Allocator allocator)
  42. where T : unmanaged, IEntityComponent
  43. {
  44. var dictionary = new SveltoDictionary<
  45. /*key */ExclusiveGroupStruct,
  46. /*value*/SharedNative<SveltoDictionary<uint, T, NativeStrategy<SveltoDictionaryNode<uint>>, NativeStrategy<T>, NativeStrategy<int>>>,
  47. /*strategy to store the key*/ NativeStrategy<SveltoDictionaryNode<ExclusiveGroupStruct>>,
  48. /*strategy to store the value*/NativeStrategy<
  49. SharedNative<SveltoDictionary<uint, T, NativeStrategy<SveltoDictionaryNode<uint>>, NativeStrategy<T>, NativeStrategy<int>>>>
  50. , NativeStrategy<int>>
  51. ((uint) groups.count, allocator);
  52. foreach (var group in groups)
  53. {
  54. if (entitiesDb.SafeQueryEntityDictionary<T>(group, out var typeSafeDictionary) == true)
  55. //if (typeSafeDictionary.count > 0)
  56. dictionary.Add(group, ((TypeSafeDictionary<T>)typeSafeDictionary).implUnmgd);
  57. }
  58. return new NativeEGIDMultiMapper<T>(dictionary);
  59. }
  60. }
  61. }