using Svelto.ECS;
using TechbloxModdingAPI.Blocks;
namespace TechbloxModdingAPI.Utility
{
public static class ApiExtensions
{
///
/// Attempts to query an entity and returns an optional that contains the result if succeeded.
///
/// The entities DB
/// The EGID to query
/// The component type to query
/// An optional that contains the result on success or is empty if not found
public static OptionalRef QueryEntityOptional(this EntitiesDB entitiesDB, EGID egid)
where T : unmanaged, IEntityComponent
{
return entitiesDB.TryQueryEntitiesAndIndex(egid, out uint index, out var array)
? new OptionalRef(array, index)
: new OptionalRef();
}
///
/// Attempts to query an entity and returns the result or a dummy value that can be modified.
///
///
///
///
///
public static OptionalRef QueryEntityOptional(this EntitiesDB entitiesDB, EcsObjectBase obj)
where T : unmanaged, IEntityComponent
{
var opt = QueryEntityOptional(entitiesDB, obj.Id);
return opt ? opt : new OptionalRef(obj);
}
///
/// Attempts to query an entity and returns the result or a dummy value that can be modified.
///
///
///
///
///
public static ref T QueryEntityOrDefault(this EntitiesDB entitiesDB, EcsObjectBase obj)
where T : unmanaged, IEntityComponent
{
var opt = QueryEntityOptional(entitiesDB, obj.Id);
if (opt) return ref opt.Get();
if (obj.InitData.Valid) return ref obj.InitData.Initializer(obj.Id).GetOrCreate();
return ref opt.Get(); //Default value
}
}
}