55 lines
No EOL
2.3 KiB
C#
55 lines
No EOL
2.3 KiB
C#
using Svelto.ECS;
|
|
using Svelto.ECS.Hybrid;
|
|
using TechbloxModdingAPI.Blocks;
|
|
|
|
namespace TechbloxModdingAPI.Utility
|
|
{
|
|
public static class ManagedApiExtensions
|
|
{
|
|
/// <summary>
|
|
/// Attempts to query an entity and returns an optional that contains the result if succeeded.
|
|
/// <b>This overload does not take initializer data into account.</b>
|
|
/// </summary>
|
|
/// <param name="entitiesDB">The entities DB</param>
|
|
/// <param name="egid">The EGID to query</param>
|
|
/// <typeparam name="T">The component type to query</typeparam>
|
|
/// <returns>An optional that contains the result on success or is empty if not found</returns>
|
|
public static OptionalRef<T> QueryEntityOptional<T>(this EntitiesDB entitiesDB, EGID egid)
|
|
where T : struct, IEntityViewComponent
|
|
{
|
|
return entitiesDB.TryQueryEntitiesAndIndex<T>(egid, out uint index, out var array)
|
|
? new OptionalRef<T>(array, index)
|
|
: new OptionalRef<T>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Attempts to query an entity and returns the result or a dummy value that can be modified.
|
|
/// </summary>
|
|
/// <param name="entitiesDB"></param>
|
|
/// <param name="obj"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
public static OptionalRef<T> QueryEntityOptional<T>(this EntitiesDB entitiesDB, EcsObjectBase obj)
|
|
where T : struct, IEntityViewComponent
|
|
{
|
|
var opt = QueryEntityOptional<T>(entitiesDB, obj.Id);
|
|
return opt ? opt : new OptionalRef<T>(obj, true);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Attempts to query an entity and returns the result or a dummy value that can be modified.
|
|
/// </summary>
|
|
/// <param name="entitiesDB"></param>
|
|
/// <param name="obj"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
public static ref T QueryEntityOrDefault<T>(this EntitiesDB entitiesDB, EcsObjectBase obj)
|
|
where T : struct, IEntityViewComponent
|
|
{
|
|
var opt = QueryEntityOptional<T>(entitiesDB, obj.Id);
|
|
if (opt) return ref opt.Get();
|
|
if (obj.InitData.Valid) return ref obj.InitData.Initializer(obj.Id).GetOrCreate<T>();
|
|
return ref opt.Get(); //Default value
|
|
}
|
|
}
|
|
} |