A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
3.0KB

  1. using Svelto.ECS;
  2. namespace TechbloxModdingAPI.Utility
  3. {
  4. public static class NativeApiExtensions
  5. {
  6. /// <summary>
  7. /// Attempts to query an entity and returns an optional that contains the result if succeeded.
  8. /// <b>This overload does not take initializer data into account.</b>
  9. /// </summary>
  10. /// <param name="entitiesDB">The entities DB</param>
  11. /// <param name="egid">The EGID to query</param>
  12. /// <typeparam name="T">The component type to query</typeparam>
  13. /// <returns>An optional that contains the result on success or is empty if not found</returns>
  14. public static OptionalRef<T> QueryEntityOptional<T>(this EntitiesDB entitiesDB, EGID egid)
  15. where T : unmanaged, IEntityComponent
  16. {
  17. return entitiesDB.TryQueryEntitiesAndIndex<T>(egid, out uint index, out var array)
  18. ? new OptionalRef<T>(array, index)
  19. : new OptionalRef<T>();
  20. }
  21. /// <summary>
  22. /// Attempts to query an entity and returns the result in an optional reference.
  23. /// </summary>
  24. /// <param name="entitiesDB">The entities DB to query from</param>
  25. /// <param name="obj">The ECS object to query</param>
  26. /// <param name="group">The group of the entity if the object can have multiple</param>
  27. /// <typeparam name="T">The component to query</typeparam>
  28. /// <returns>A reference to the component or a dummy value</returns>
  29. public static OptionalRef<T> QueryEntityOptional<T>(this EntitiesDB entitiesDB, EcsObjectBase obj, ExclusiveGroupStruct group = default)
  30. where T : unmanaged, IEntityComponent
  31. {
  32. EGID id = group == ExclusiveGroupStruct.Invalid ? obj.Id : new EGID(obj.Id.entityID, group);
  33. var opt = QueryEntityOptional<T>(entitiesDB, id);
  34. return opt ? opt : new OptionalRef<T>(obj, true);
  35. }
  36. /// <summary>
  37. /// Attempts to query an entity and returns the result or a dummy value that can be modified.
  38. /// </summary>
  39. /// <param name="entitiesDB">The entities DB to query from</param>
  40. /// <param name="obj">The ECS object to query</param>
  41. /// <param name="group">The group of the entity if the object can have multiple</param>
  42. /// <typeparam name="T">The component to query</typeparam>
  43. /// <returns>A reference to the component or a dummy value</returns>
  44. public static ref T QueryEntityOrDefault<T>(this EntitiesDB entitiesDB, EcsObjectBase obj, ExclusiveGroupStruct group = default)
  45. where T : unmanaged, IEntityComponent
  46. {
  47. EGID id = group == ExclusiveGroupStruct.Invalid ? obj.Id : new EGID(obj.Id.entityID, group);
  48. var opt = QueryEntityOptional<T>(entitiesDB, id);
  49. if (opt) return ref opt.Get();
  50. if (obj.InitData.Valid) return ref obj.InitData.Initializer(id).GetOrCreate<T>();
  51. return ref opt.Get(); //Default value
  52. }
  53. }
  54. }