using Svelto.ECS; using Svelto.ECS.Hybrid; using TechbloxModdingAPI.Blocks; namespace TechbloxModdingAPI.Utility { public static class ManagedApiExtensions { /// /// Attempts to query an entity and returns an optional that contains the result if succeeded. /// This overload does not take initializer data into account. /// /// 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 : struct, IEntityViewComponent { 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 : struct, IEntityViewComponent { var opt = QueryEntityOptional(entitiesDB, obj.Id); return opt ? opt : new OptionalRef(obj, true); } /// /// 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 : struct, IEntityViewComponent { 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 } } }