using System.Runtime.CompilerServices; using Svelto.Common; using Svelto.DataStructures; using Svelto.DataStructures.Native; using Svelto.ECS.Internal; namespace Svelto.ECS.Native { public static class UnityNativeEntityDBExtensions { static NativeEGIDMapper ToNativeEGIDMapper(this UnmanagedTypeSafeDictionary dic, ExclusiveGroupStruct groupStructId) where T : unmanaged, IEntityComponent { var mapper = new NativeEGIDMapper(groupStructId, dic.implUnmgd); return mapper; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static NativeEGIDMapper QueryNativeMappedEntities(this EntitiesDB entitiesDb, ExclusiveGroupStruct groupStructId) where T : unmanaged, IEntityComponent { if (entitiesDb.SafeQueryEntityDictionary(groupStructId, out var typeSafeDictionary) == false) throw new EntityGroupNotFoundException(typeof(T), groupStructId.ToName()); return (typeSafeDictionary as UnmanagedTypeSafeDictionary).ToNativeEGIDMapper(groupStructId); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool TryQueryNativeMappedEntities(this EntitiesDB entitiesDb, ExclusiveGroupStruct groupStructId, out NativeEGIDMapper mapper) where T : unmanaged, IEntityComponent { mapper = NativeEGIDMapper.empty; if (entitiesDb.SafeQueryEntityDictionary(groupStructId, out var typeSafeDictionary) == false || typeSafeDictionary.count == 0) return false; mapper = (typeSafeDictionary as UnmanagedTypeSafeDictionary).ToNativeEGIDMapper(groupStructId); return true; } [MethodImpl(MethodImplOptions.AggressiveInlining)] ///Note: if I use a SharedNativeSveltoDictionary for implUnmg, I may be able to cache NativeEGIDMultiMapper /// and reuse it /// TODO: the ability to retain a NativeEGIDMultiMapper thanks to the use of shareable arrays /// must be unit tested! public static NativeEGIDMultiMapper QueryNativeMappedEntities(this EntitiesDB entitiesDb, LocalFasterReadOnlyList groups, Allocator allocator) where T : unmanaged, _IInternalEntityComponent { var dictionary = new SveltoDictionaryNative> ((uint) groups.count, allocator); foreach (var group in groups) { if (entitiesDb.SafeQueryEntityDictionary(group, out var typeSafeDictionary) == true) //if (typeSafeDictionary.count > 0) dictionary.Add(group, ((UnmanagedTypeSafeDictionary)typeSafeDictionary).implUnmgd); } return new NativeEGIDMultiMapper(dictionary); } } }