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.

55 lines
2.3KB

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