|
- using Svelto.ECS;
- using Svelto.ECS.Hybrid;
-
- 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 in an optional reference.
- /// </summary>
- /// <param name="entitiesDB">The entities DB to query from</param>
- /// <param name="obj">The ECS object to query</param>
- /// <param name="group">The group of the entity if the object can have multiple</param>
- /// <typeparam name="T">The component to query</typeparam>
- /// <returns>A reference to the component or a dummy value</returns>
- public static OptionalRef<T> QueryEntityOptional<T>(this EntitiesDB entitiesDB, EcsObjectBase obj, ExclusiveGroupStruct group = default)
- where T : struct, IEntityViewComponent
- {
- EGID id = group == ExclusiveGroupStruct.Invalid ? obj.Id : new EGID(obj.Id.entityID, group);
- var opt = QueryEntityOptional<T>(entitiesDB, id);
- return opt ? opt : new OptionalRef<T>(obj, false);
- }
-
- /// <summary>
- /// Attempts to query an entity and returns the result or a dummy value that can be modified.
- /// </summary>
- /// <param name="entitiesDB">The entities DB to query from</param>
- /// <param name="obj">The ECS object to query</param>
- /// <param name="group">The group of the entity if the object can have multiple</param>
- /// <typeparam name="T">The component to query</typeparam>
- /// <returns>A reference to the component or a dummy value</returns>
- public static ref T QueryEntityOrDefault<T>(this EntitiesDB entitiesDB, EcsObjectBase obj, ExclusiveGroupStruct group = default)
- where T : struct, IEntityViewComponent
- {
- EGID id = group == ExclusiveGroupStruct.Invalid ? obj.Id : new EGID(obj.Id.entityID, group);
- var opt = QueryEntityOptional<T>(entitiesDB, id);
- if (opt) return ref opt.Get();
- if (obj.InitData.Valid) return ref obj.InitData.Initializer(id).GetOrAdd<T>();
- return ref opt.Get(); //Default value
- }
- }
- }
|