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.

54 lines
2.3KB

  1. using Svelto.ECS;
  2. using TechbloxModdingAPI.Blocks;
  3. namespace TechbloxModdingAPI.Utility
  4. {
  5. public static class NativeApiExtensions
  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 : unmanaged, IEntityComponent
  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 or a dummy value that can be modified.
  24. /// </summary>
  25. /// <param name="entitiesDB"></param>
  26. /// <param name="obj"></param>
  27. /// <typeparam name="T"></typeparam>
  28. /// <returns></returns>
  29. public static OptionalRef<T> QueryEntityOptional<T>(this EntitiesDB entitiesDB, EcsObjectBase obj)
  30. where T : unmanaged, IEntityComponent
  31. {
  32. var opt = QueryEntityOptional<T>(entitiesDB, obj.Id);
  33. return opt ? opt : new OptionalRef<T>(obj, true);
  34. }
  35. /// <summary>
  36. /// Attempts to query an entity and returns the result or a dummy value that can be modified.
  37. /// </summary>
  38. /// <param name="entitiesDB"></param>
  39. /// <param name="obj"></param>
  40. /// <typeparam name="T"></typeparam>
  41. /// <returns></returns>
  42. public static ref T QueryEntityOrDefault<T>(this EntitiesDB entitiesDB, EcsObjectBase obj)
  43. where T : unmanaged, IEntityComponent
  44. {
  45. var opt = QueryEntityOptional<T>(entitiesDB, obj.Id);
  46. if (opt) return ref opt.Get();
  47. if (obj.InitData.Valid) return ref obj.InitData.Initializer(obj.Id).GetOrCreate<T>();
  48. return ref opt.Get(); //Default value
  49. }
  50. }
  51. }