using System;
using System.Collections.Generic;
using Unity.Collections;
namespace Unity.Entities
{
public sealed unsafe partial class EntityManager
{
// ----------------------------------------------------------------------------------------------------------
// PUBLIC
// ----------------------------------------------------------------------------------------------------------
///
/// Gets the dynamic type object required to access a chunk component of type T.
///
///
/// To access a component stored in a chunk, you must have the type registry information for the component.
/// This function provides that information. Use the returned
/// object with the functions of an object to get information about the components
/// in that chunk and to access the component values.
///
/// Specify whether the access to the component through this object is read only
/// or read and write.
/// The compile-time type of the component.
/// The run-time type information of the component.
public ArchetypeChunkComponentType GetArchetypeChunkComponentType(bool isReadOnly)
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
var typeIndex = TypeManager.GetTypeIndex();
return new ArchetypeChunkComponentType(
ComponentJobSafetyManager->GetSafetyHandle(typeIndex, isReadOnly), isReadOnly,
GlobalSystemVersion);
#else
return new ArchetypeChunkComponentType(isReadOnly, GlobalSystemVersion);
#endif
}
///
/// Gets the dynamic type object required to access a chunk buffer containing elements of type T.
///
///
/// To access a component stored in a chunk, you must have the type registry information for the component.
/// This function provides that information for buffer components. Use the returned
/// object with the functions of an
/// object to get information about the components in that chunk and to access the component values.
///
/// Specify whether the access to the component through this object is read only
/// or read and write.
/// The compile-time type of the buffer elements.
/// The run-time type information of the buffer component.
public ArchetypeChunkBufferType GetArchetypeChunkBufferType(bool isReadOnly)
where T : struct, IBufferElementData
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
var typeIndex = TypeManager.GetTypeIndex();
return new ArchetypeChunkBufferType(
ComponentJobSafetyManager->GetSafetyHandle(typeIndex, isReadOnly),
ComponentJobSafetyManager->GetBufferSafetyHandle(typeIndex),
isReadOnly, GlobalSystemVersion);
#else
return new ArchetypeChunkBufferType(isReadOnly,GlobalSystemVersion);
#endif
}
///
/// Gets the dynamic type object required to access a shared component of type T.
///
///
/// To access a component stored in a chunk, you must have the type registry information for the component.
/// This function provides that information for shared components. Use the returned
/// object with the functions of an
/// object to get information about the components in that chunk and to access the component values.
///
/// The compile-time type of the shared component.
/// The run-time type information of the shared component.
public ArchetypeChunkSharedComponentType GetArchetypeChunkSharedComponentType()
where T : struct, ISharedComponentData
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
return new ArchetypeChunkSharedComponentType(
ComponentJobSafetyManager->GetEntityManagerSafetyHandle());
#else
return new ArchetypeChunkSharedComponentType(false);
#endif
}
///
/// Gets the dynamic type object required to access the component of a chunk.
///
///
/// All chunks have an implicit component referring to the entities in that chunk.
///
/// To access any component stored in a chunk, you must have the type registry information for the component.
/// This function provides that information for the implicit component. Use the returned
/// object with the functions of an
/// object to access the component values.
///
/// The run-time type information of the Entity component.
public ArchetypeChunkEntityType GetArchetypeChunkEntityType()
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
return new ArchetypeChunkEntityType(
ComponentJobSafetyManager->GetSafetyHandle(TypeManager.GetTypeIndex(), true));
#else
return new ArchetypeChunkEntityType(false);
#endif
}
///
/// Gets an entity's component types.
///
/// The entity.
/// The type of allocation for creating the NativeArray to hold the ComponentType
/// objects.
/// An array of ComponentType containing all the types of components associated with the entity.
public NativeArray GetComponentTypes(Entity entity, Allocator allocator = Allocator.Temp)
{
EntityComponentStore->AssertEntitiesExist(&entity, 1);
var archetype = EntityComponentStore->GetArchetype(entity);
var components = new NativeArray(archetype->TypesCount - 1, allocator);
for (var i = 1; i < archetype->TypesCount; i++)
components[i - 1] = archetype->Types[i].ToComponentType();
return components;
}
///
/// Gets a list of the types of components that can be assigned to the specified component.
///
/// Assignable components include those with the same compile-time type and those that
/// inherit from the same compile-time type.
/// The type to check.
/// A new List object containing the System.Types that can be assigned to `interfaceType`.
public List GetAssignableComponentTypes(Type interfaceType)
{
// #todo Cache this. It only can change when TypeManager.GetTypeCount() changes
var componentTypeCount = TypeManager.GetTypeCount();
var assignableTypes = new List();
for (var i = 0; i < componentTypeCount; i++)
{
var type = TypeManager.GetType(i);
if (interfaceType.IsAssignableFrom(type)) assignableTypes.Add(type);
}
return assignableTypes;
}
// ----------------------------------------------------------------------------------------------------------
// INTERNAL
// ----------------------------------------------------------------------------------------------------------
internal int GetComponentTypeIndex(Entity entity, int index)
{
EntityComponentStore->AssertEntitiesExist(&entity, 1);
var archetype = EntityComponentStore->GetArchetype(entity);
if ((uint) index >= archetype->TypesCount) return -1;
return archetype->Types[index + 1].TypeIndex;
}
}
}