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.

58 lines
3.0KB

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